12c091418258b4cac479a6c9259a73354a6eec71
[platform/core/messaging/email-service.git] / email-core / email-storage / email-storage.c
1 /*
2  *  email-service
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22
23 /******************************************************************************
24  * File: email-storage.c
25  * Desc: email-service Storage Library on Sqlite3
26  *
27  * Auth:
28  *
29  * History:
30  *      2007.03.02 : created
31  *****************************************************************************/
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <stdbool.h>
37 #include <time.h>
38 #include <sys/types.h>
39 #include <sys/time.h>
40 #include <sys/wait.h>
41 #include <glib.h>
42 #include <sys/stat.h>
43 #include <unistd.h>
44 #include <errno.h>
45 #include <dirent.h>
46 #include <vconf.h>
47 #include <sys/mman.h>
48 #include <fcntl.h>
49 #include <tzplatform_config.h>
50
51 #define __USE_UNIX98
52 #define __USE_GNU
53 #include <pthread.h>
54 #include <grp.h>
55 #include <pwd.h>
56
57 #include "email-internal-types.h"
58 #include "email-convert.h"
59 #include "email-core-utils.h"
60 #include "email-utilities.h"
61 #include "email-storage.h"
62 #include "email-debug-log.h"
63 #include "email-types.h"
64 #include "email-convert.h"
65 #include "email-core-signal.h"
66 #include "email-core-event.h"
67 #include "email-core-container.h"
68 #include "email-core-gmime.h"
69 #include "email-core-key-manager.h"
70
71 #define SETTING_MEMORY_TEMP_FILE_PATH "/tmp/email_tmp_file"
72 #define CONTENT_DATA                  "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset="
73 #define CONTENT_TYPE_DATA             "<meta http-equiv=\"Content-Type\" content=\"text/html; charset="
74
75 #define FLAG_SEEN       0x01
76 #define FLAG_DELETED    0x02
77 #define FLAG_FLAGGED    0x04
78 #define FLAG_ANSWERED   0x08
79 #define FLAG_OLD        0x10
80 #define FLAG_DRAFT      0x20
81
82 #undef close
83
84 #define ISSUE_ORGANIZATION_LEN_IN_MAIL_CERTIFICATE_TBL  256
85 #define EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL       128
86 #define SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL      1027
87 #define FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL           256
88
89 #define ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL            50
90 #define RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL   50
91 #define EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL              128
92 #define USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL               50
93 #define PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL                50
94 #define SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL     50
95 #define SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL            50
96 #define SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL        50
97 #define DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL            30
98 #define REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL           128
99 #define RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL             128
100 #define LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL          256
101 #define DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL       256
102 #define SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL               256
103 #define MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL                128
104 #define ALIAS_LEN_IN_MAIL_BOX_TBL                       128
105 #define LOCAL_MBOX_LEN_IN_MAIL_READ_MAIL_UID_TBL        128
106 #define MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL      128
107 #define S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL             128
108 #define DATA2_LEN_IN_MAIL_READ_MAIL_UID_TBL             256
109 #define FILTER_NAME_LEN_IN_MAIL_RULE_TBL                256
110 #define VALUE_LEN_IN_MAIL_RULE_TBL                      256
111 #define VALUE2_LEN_IN_MAIL_RULE_TBL                     256
112 #define DEST_MAILBOX_LEN_IN_MAIL_RULE_TBL               128
113 #define MAILBOX_NAME_LEN_IN_MAIL_ATTACHMENT_TBL         128
114 #define ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL      256
115 #define ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL      256
116 #define CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL           256
117 #define ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL 128
118 #define MAILBOX_LEN_IN_MAIL_TBL                         128
119 #define SERVER_MAILBOX_LEN_IN_MAIL_TBL                  128
120 #define SERVER_MAIL_ID_LEN_IN_MAIL_TBL                  128
121 #define FROM_LEN_IN_MAIL_TBL                            256
122 #define SENDER_LEN_IN_MAIL_TBL                          256
123 #define REPLY_TO_LEN_IN_MAIL_TBL                        256
124 #define TO_LEN_IN_MAIL_TBL                              3999
125 #define CC_LEN_IN_MAIL_TBL                              3999
126 #define BCC_LEN_IN_MAIL_TBL                             3999
127 #define RETURN_PATH_LEN_IN_MAIL_TBL                     3999
128 #define SUBJECT_LEN_IN_MAIL_TBL                         1027
129 #define THREAD_TOPIC_LEN_IN_MAIL_TBL                    256
130 #define TEXT_1_LEN_IN_MAIL_TBL                          256
131 #define TEXT_2_LEN_IN_MAIL_TBL                          256
132 #define MIME_ENTITY_LEN_IN_MAIL_TBL                     256
133 #define DATETIME_LEN_IN_MAIL_TBL                        128
134 #define MESSAGE_ID_LEN_IN_MAIL_TBL                      256
135 #define FROM_CONTACT_NAME_LEN_IN_MAIL_TBL               3999
136 #define FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL              3999
137 #define TO_CONTACT_NAME_LEN_IN_MAIL_TBL                 3999
138 #define TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL                3999
139 #define MAILBOX_LEN_IN_MAIL_MEETING_TBL                 128
140 #define LOCATION_LEN_IN_MAIL_MEETING_TBL                1024
141 #define GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL        512
142 #define STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL           32
143 #define DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL           32
144 #define PREVIEWBODY_LEN_IN_MAIL_TBL                     512
145 #define CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL        256
146 #define UID                                             "root"          /* UID = root */
147 #define UID_DEFAULT                                     0               /* UID = root */
148 #define GID                                             "priv_email"    /* GID = priv_email */
149 #define GID_DEFAULT                                     10901           /* GID = priv_email */
150 #define MAX_GRP_BUF_SIZE (1024 * 4)
151
152 /*  this define is used for query to change data (delete, insert, update) */
153 #define EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction_flag, error_code) \
154         do { \
155                 if (transaction_flag) { \
156                         if (emstorage_begin_transaction(multi_user_name, NULL, NULL, &error_code) == false) { \
157                                 EM_DEBUG_EXCEPTION("emstorage_begin_transaction error [%d]", error_code); \
158                                 goto FINISH_OFF; \
159                         } \
160                 } \
161         } while (0)
162
163 /*  this define is used for query to change data (delete, insert, update) */
164 #define EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction_flag, result_code, error_code) \
165         do { \
166                 if (transaction_flag) { \
167                         if (result_code == true) { \
168                                 if (emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL) == false) { \
169                                         EM_DEBUG_EXCEPTION("emstorage_commit_transaction error"); \
170                                         error_code = EMAIL_ERROR_DB_FAILURE; \
171                                         result_code = false; \
172                                 } \
173                         } \
174                         else { \
175                                 if (emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL) == false) { \
176                                         EM_DEBUG_EXCEPTION("emstorage_rollback_transaction error"); \
177                                         error_code = EMAIL_ERROR_DB_FAILURE; \
178                                 } \
179                         } \
180                 } \
181         } while (0)
182
183 /*  this define is used for query to read (select) */
184 #define EMSTORAGE_START_READ_TRANSACTION(transaction_flag) \
185         if (transaction_flag) { \
186                 /*_timedlock_shm_mutex(mapped_for_db_lock, 2);*/\
187         }
188
189 /*  this define is used for query to read (select) */
190 #define EMSTORAGE_FINISH_READ_TRANSACTION(transaction_flag) \
191         if (transaction_flag) { \
192                 /*_unlockshm_mutex(mapped_for_db_lock);*/\
193         }
194
195 /*  for safety DB operation */
196 static pthread_mutex_t _db_handle_lock = PTHREAD_MUTEX_INITIALIZER;
197
198
199 #define _MULTIPLE_DB_HANDLE
200
201 #ifdef _MULTIPLE_DB_HANDLE
202
203 typedef struct {
204         char *user_name;
205         pthread_t thread_id;
206         sqlite3 *db_handle;
207 } db_handle_t;
208
209 #define MAX_DB_CLIENT 100
210
211 /* static int _db_handle_count = 0; */
212 db_handle_t _db_handle_list[MAX_DB_CLIENT] = {{NULL, 0, 0}, };
213
214
215 sqlite3 *emstorage_get_db_handle(char *multi_user_name)
216 {
217         EM_DEBUG_FUNC_BEGIN();
218         int i;
219         pthread_t current_thread_id = THREAD_SELF();
220         sqlite3 *result_db_handle = NULL;
221
222         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
223                 ENTER_CRITICAL_SECTION(_db_handle_lock);
224                 for (i = 0; i < MAX_DB_CLIENT; i++) {
225                         if (EM_SAFE_STRCASECMP(_db_handle_list[i].user_name, multi_user_name) != 0)
226                                 continue;
227
228                         if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
229                                 EM_DEBUG_LOG_DEV("found db handle at [%d]", i);
230                                 result_db_handle = _db_handle_list[i].db_handle;
231                                 break;
232                         }
233                 }
234                 LEAVE_CRITICAL_SECTION(_db_handle_lock);
235         } else {
236                 ENTER_CRITICAL_SECTION(_db_handle_lock);
237                 for (i = 0; i < MAX_DB_CLIENT; i++) {
238                         if (EM_SAFE_STRLEN(_db_handle_list[i].user_name) > 0)
239                                 continue;
240
241                         if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
242                                 EM_DEBUG_LOG_DEV("found db handle at [%d]", i);
243                                 result_db_handle = _db_handle_list[i].db_handle;
244                                 break;
245                         }
246                 }
247                 LEAVE_CRITICAL_SECTION(_db_handle_lock);
248         }
249
250         if (!result_db_handle)
251                 EM_DEBUG_LOG("no db_handle for [%lu] found", current_thread_id);
252
253         EM_DEBUG_FUNC_END();
254         return result_db_handle;
255 }
256
257 int emstorage_set_db_handle(char *multi_user_name, sqlite3 *db_handle)
258 {
259         EM_DEBUG_FUNC_BEGIN();
260         int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
261         pthread_t current_thread_id = THREAD_SELF();
262
263         ENTER_CRITICAL_SECTION(_db_handle_lock);
264         for (i = 0; i < MAX_DB_CLIENT; i++)     {
265                 if (_db_handle_list[i].thread_id == 0) {
266                         _db_handle_list[i].thread_id = current_thread_id;
267                         _db_handle_list[i].db_handle = db_handle;
268                         /* Only distinguished container and host  */
269                         _db_handle_list[i].user_name = EM_SAFE_STRDUP(multi_user_name);
270                         EM_DEBUG_LOG("current_thread_id [%lu], index [%d]", current_thread_id, i);
271                         error_code =  EMAIL_ERROR_NONE;
272                         break;
273                 }
274         }
275         LEAVE_CRITICAL_SECTION(_db_handle_lock);
276
277         if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
278                 EM_DEBUG_EXCEPTION("Exceeded the limitation of db client. Can't find empty slot in _db_handle_list.");
279
280         EM_DEBUG_FUNC_END("error_code [%d]", error_code);
281         return error_code;
282 }
283
284 int emstorage_remove_db_handle()
285 {
286         EM_DEBUG_FUNC_BEGIN();
287         int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
288         ENTER_CRITICAL_SECTION(_db_handle_lock);
289         for (i = 0; i < MAX_DB_CLIENT; i++) {
290                 if (_db_handle_list[i].thread_id == THREAD_SELF()) {
291                         _db_handle_list[i].thread_id = 0;
292                         _db_handle_list[i].db_handle = NULL;
293                         EM_SAFE_FREE(_db_handle_list[i].user_name);
294
295                         EM_DEBUG_LOG("index [%d]", i);
296                         error_code = EMAIL_ERROR_NONE;
297                         break;
298                 }
299         }
300         LEAVE_CRITICAL_SECTION(_db_handle_lock);
301
302         if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
303                 EM_DEBUG_EXCEPTION("Can't find proper thread_id");
304
305         EM_DEBUG_FUNC_END("error_code [%d]", error_code);
306         return error_code;
307 }
308
309 int emstorage_reset_db_handle_list()
310 {
311         EM_DEBUG_FUNC_BEGIN();
312         int i;
313
314         ENTER_CRITICAL_SECTION(_db_handle_lock);
315         for (i = 0; i < MAX_DB_CLIENT; i++) {
316                 _db_handle_list[i].thread_id = 0;
317                 _db_handle_list[i].db_handle = NULL;
318                 EM_SAFE_FREE(_db_handle_list[i].user_name);
319         }
320         LEAVE_CRITICAL_SECTION(_db_handle_lock)
321
322                 EM_DEBUG_FUNC_END();
323         return EMAIL_ERROR_NONE;
324 }
325
326
327 sqlite3 *emstorage_get_db_connection(char *multi_user_name)
328 {
329         return emstorage_db_open(multi_user_name, NULL);
330 }
331
332 #else   /*  _MULTIPLE_DB_HANDLE */
333
334 sqlite3 *_db_handle = NULL;
335
336 sqlite3 *emstorage_get_db_connection()
337 {
338         if (NULL == _db_handle)
339                 emstorage_db_open(NULL);
340         return _db_handle;
341 }
342 #endif  /*  _MULTIPLE_DB_HANDLE */
343
344 /* ------------------------------------------------------------------------------ */
345 /*  Mutex using shared memory */
346 typedef struct {
347         pthread_mutex_t mutex;
348         int data;
349 } mmapped_t;
350
351 mmapped_t       *mapped_for_db_lock = NULL;
352 int              shm_fd_for_db_lock = 0;
353
354 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
355 mmapped_t       *mapped_for_generating_mail_id = NULL;
356 int              shm_fd_for_generating_mail_id = 0;
357 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
358
359 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__
360 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
361         do {\
362                 _timedlock_shm_mutex(mapped_for_db_lock, 2);\
363                 return_value = function_call;\
364                 _unlockshm_mutex(mapped_for_db_lock);\
365         } while (0)
366
367 #else /*  __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
368 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
369 {  return_value = function_call; }
370 #endif /*  __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
371
372
373 static int emstorage_exec_query_by_prepare_v2(sqlite3 *local_db_handle, char *query_string)
374 {
375         EM_DEBUG_FUNC_BEGIN("local_db_handle[%p] query_string[%p]", local_db_handle, query_string);
376         int error = EMAIL_ERROR_NONE;
377         int rc = 0;
378         DB_STMT db_statement = NULL;
379
380         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, query_string, EM_SAFE_STRLEN(query_string), &db_statement, NULL), rc);
381         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
382                         ("sqlite3_prepare failed [%d] [%s]", rc, query_string));
383
384         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(db_statement), rc);
385         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
386                         ("sqlite3_step failed [%d] [%s]", rc, query_string));
387
388 FINISH_OFF:
389
390         if (db_statement != NULL) {
391                 rc = sqlite3_finalize(db_statement);
392                 if (rc != SQLITE_OK) {
393                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
394                         error = EMAIL_ERROR_DB_FAILURE;
395                 }
396         }
397
398         return error;
399 }
400
401 INTERNAL_FUNC int emstorage_shm_file_init(const char *shm_file_name)
402 {
403         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
404         char errno_buf[ERRNO_BUF_SIZE] = {0};
405
406         if (!shm_file_name) {
407                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
408                 return EMAIL_ERROR_INVALID_PARAM;
409         }
410
411         int fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); /*  note: permission is not working */
412         if (fd < 0) {
413                 EM_DEBUG_EXCEPTION("shm_open errno [%d] [%s]", errno, EM_STRERROR(errno_buf));
414                 return EMAIL_ERROR_SYSTEM_FAILURE;
415         }
416
417         fchmod(fd, 0666);
418         EM_DEBUG_LOG("** Create SHM FILE **");
419         if (ftruncate(fd, sizeof(mmapped_t)) != 0) {
420                 EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
421                 return EMAIL_ERROR_SYSTEM_FAILURE;
422         }
423
424         mmapped_t *m = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
425         if (m == MAP_FAILED) {
426                 EM_DEBUG_EXCEPTION("mmap errno [%d]", errno);
427                 EM_SAFE_CLOSE(fd);
428                 return EMAIL_ERROR_SYSTEM_FAILURE;
429         }
430
431         m->data = 0;
432
433         pthread_mutexattr_t mattr;
434         pthread_mutexattr_init(&mattr);
435         pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
436         pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
437         pthread_mutex_init(&(m->mutex), &mattr);
438         pthread_mutexattr_destroy(&mattr);
439
440         pthread_mutex_destroy(&(m->mutex));
441         munmap(m, sizeof(mmapped_t));
442
443         EM_SAFE_CLOSE(fd);
444         EM_DEBUG_FUNC_END();
445         return EMAIL_ERROR_NONE;
446 }
447
448 int emstorage_shm_file_destroy(const char *shm_file_name)
449 {
450         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
451         char errno_buf[ERRNO_BUF_SIZE] = {0};
452
453         if (!shm_file_name) {
454                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
455                 return EMAIL_ERROR_INVALID_PARAM;
456         }
457
458         if (shm_unlink(shm_file_name) != 0)
459                 EM_DEBUG_EXCEPTION("shm_unlink failed: %s", EM_STRERROR(errno_buf));
460         EM_DEBUG_FUNC_END();
461         return EMAIL_ERROR_NONE;
462 }
463
464 int _initialize_shm_mutex(const char *shm_file_name, int *param_shm_fd, mmapped_t **param_mapped)
465 {
466         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p] param_shm_fd [%p], param_mapped [%p]", shm_file_name, param_shm_fd, param_mapped);
467         char errno_buf[ERRNO_BUF_SIZE] = {0};
468
469         if (!shm_file_name || !param_shm_fd || !param_mapped) {
470                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
471                 return EMAIL_ERROR_INVALID_PARAM;
472         }
473
474         if (!(*param_mapped)) {
475                 EM_DEBUG_LOG("** mapping begin **");
476                 if (!(*param_shm_fd)) { /*  open shm_file_name at first. Otherwise, the num of files in /proc/pid/fd will be increasing  */
477                         *param_shm_fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
478                         if ((*param_shm_fd) == -1) {
479                                 EM_DEBUG_EXCEPTION("FAIL: shm_open(): %s", EM_STRERROR(errno_buf));
480                                 return EMAIL_ERROR_SYSTEM_FAILURE;
481                         }
482                 }
483
484                 fchmod((*param_shm_fd), 0666);
485                 EM_DEBUG_LOG("** Create SHM FILE **");
486                 if (ftruncate((*param_shm_fd), sizeof(mmapped_t)) != 0) {
487                         EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
488                         return EMAIL_ERROR_SYSTEM_FAILURE;
489                 }
490
491                 mmapped_t *tmp = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ|PROT_WRITE, MAP_SHARED, (*param_shm_fd), 0);
492                 if (tmp == MAP_FAILED) {
493                         EM_DEBUG_EXCEPTION("mmap failed: %s", EM_STRERROR(errno_buf));
494                         return EMAIL_ERROR_SYSTEM_FAILURE;
495                 }
496
497                 tmp->data = 0;
498
499                 pthread_mutexattr_t mattr;
500                 pthread_mutexattr_init(&mattr);
501                 pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
502                 pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
503                 pthread_mutex_init(&(tmp->mutex), &mattr);
504                 pthread_mutexattr_destroy(&mattr);
505
506                 *param_mapped = tmp;
507         }
508
509         EM_DEBUG_FUNC_END();
510         return EMAIL_ERROR_NONE;
511 }
512
513 int _timedlock_shm_mutex(mmapped_t *param_mapped, int sec)
514 {
515         EM_DEBUG_FUNC_BEGIN("param_mapped [%p], sec [%d]", param_mapped, sec);
516
517         if (!param_mapped) {
518                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
519                 return EMAIL_ERROR_INVALID_PARAM;
520         }
521
522         struct timespec abs_time;
523         clock_gettime(CLOCK_REALTIME, &abs_time);
524         abs_time.tv_sec += sec;
525         char errno_buf[ERRNO_BUF_SIZE] = {0};
526
527         int err = pthread_mutex_timedlock(&(param_mapped->mutex), &abs_time);
528
529         if (err == EOWNERDEAD) {
530                 err = pthread_mutex_consistent(&(param_mapped->mutex));
531                 EM_DEBUG_EXCEPTION("Previous owner is dead with lock. Fix mutex : %s", EM_STRERROR(errno_buf));
532         } else if (err != 0) {
533                 EM_DEBUG_EXCEPTION("ERROR : %s", EM_STRERROR(errno_buf));
534                 return err;
535         }
536
537         EM_DEBUG_FUNC_END();
538         return EMAIL_ERROR_NONE;
539 }
540
541 void _unlockshm_mutex(mmapped_t *param_mapped)
542 {
543         EM_DEBUG_FUNC_BEGIN();
544         pthread_mutex_unlock(&(param_mapped->mutex));
545         EM_DEBUG_FUNC_END();
546 }
547 /* ------------------------------------------------------------------------------ */
548
549
550 static int _open_counter = 0;
551
552 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name);
553 static int _read_password_from_secure_storage(char *file_name, char **password);
554
555 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
556 static int _get_cert_password_file_name(int index, char *cert_password_file_name);
557 #endif
558
559 typedef struct {
560         const char *object_name;
561         unsigned int data_flag;
562 } email_db_object_t;
563
564 static const email_db_object_t _g_db_tables[] = {
565         { "mail_read_mail_uid_tbl", 1},
566         { "mail_tbl", 1},
567         { "mail_attachment_tbl", 1},
568         { NULL,  0},
569 };
570
571 static const email_db_object_t _g_db_indexes[] = {
572         { "mail_read_mail_uid_idx1", 1},
573         { "mail_idx1", 1},
574         { "mail_attachment_idx1", 1},
575         { NULL,  1},
576 };
577
578 enum {
579         CREATE_TABLE_MAIL_ACCOUNT_TBL,
580         CREATE_TABLE_MAIL_BOX_TBL,
581         CREATE_TABLE_MAIL_READ_MAIL_UID_TBL,
582         CREATE_TABLE_MAIL_RULE_TBL,
583         CREATE_TABLE_MAIL_TBL,
584         CREATE_TABLE_MAIL_ATTACHMENT_TBL,
585 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
586         CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL,
587 #else
588         CREATE_TABLE_DUMMY_INDEX1,
589 #endif
590         CREATE_TABLE_MAIL_MEETING_TBL,
591 #ifdef __FEATURE_LOCAL_ACTIVITY__
592         CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL,
593 #else
594         CREATE_TABLE_DUMMY_INDEX2,
595 #endif
596         CREATE_TABLE_MAIL_CERTIFICATE_TBL,
597         CREATE_TABLE_MAIL_TASK_TBL,
598 #ifdef __FEATURE_BODY_SEARCH__
599         CREATE_TABLE_MAIL_TEXT_TBL,
600 #else
601         CREATE_TABLE_DUMMY_INDEX3,
602 #endif
603
604 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
605         CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
606 #else
607         CREATE_TABLE_DUMMY_INDEX4,
608 #endif
609
610         /*CREATE INDEX*/
611         CREATE_TABLE_MAIL_ACCOUNT_IDX,
612         CREATE_TABLE_MAIL_BOX_IDX,
613         CREATE_TABLE_MAIL_READ_MAIL_UID_IDX,
614         CREATE_TABLE_MAIL_IDX,
615         CREATE_TABLE_MAIL_ATTACHMENT_IDX,
616         CREATE_TABLE_MAIL_MEETING_IDX,
617         CREATE_TABLE_MAIL_TASK_IDX,
618         CREATE_TABLE_MAIL_DATETIME_IDX,
619         CREATE_TABLE_MAIL_THREAD_IDX,
620         CREATE_TABLE_MAX,
621 };
622
623 enum {
624         DATA1_IDX_IN_MAIL_ACTIVITY_TBL = 0,
625         TRANSTYPE_IDX_IN_MAIL_ACTIVITY_TBL,
626         FLAG_IDX_IN_MAIL_ACTIVITY_TBL,
627 };
628
629 enum {
630         CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL = 0,
631         ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
632         ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
633         ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
634         EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
635         EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
636         EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
637         ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL,
638         EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL,
639         SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL,
640         FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL,
641 };
642
643 enum {
644         TO_RECIPIENT = 0,
645         CC_RECIPIENT,
646         BCC_RECIPIENT,
647 };
648 enum {
649         ACCOUNT_ID_IDX_IN_MAIL_BOX_TBL = 0,
650         LOCAL_YN_IDX_IN_MAIL_BOX_TBL,
651         MAILBOX_NAME_IDX_IN_MAIL_BOX_TBL,
652         MAILBOX_TYPE_IDX_IN_MAIL_BOX_TBL,
653         ALIAS_IDX_IN_MAIL_BOX_TBL,
654         SYNC_WITH_SERVER_YN_IDX_IN_MAIL_BOX_TBL,
655         MODIFIABLE_YN_IDX_IN_MAIL_BOX_TBL,
656         TOTAL_MAIL_COUNT_ON_SERVER_IDX_IN_MAIL_BOX_TBL,
657         ARCHIVE_IDX_IN_MAIL_BOX_TBL,
658         MAIL_SLOT_SIZE_IDX_IN_MAIL_BOX_TBL,
659 };
660
661 enum {
662         ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL = 0,
663         LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
664         MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL,
665         LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
666         SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
667         RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL,
668         SYNC_STATUS_IDX_IN_MAIL_READ_MAIL_UID_TBL,
669         FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL,
670         FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL,
671         IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL,  /* unused */
672 };
673
674 #ifdef __FEATURE_BODY_SEARCH__
675 enum {
676         MAIL_ID_IDX_IN_MAIL_TEXT_TBL = 0,
677         ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL,
678         MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL,
679         BODY_TEXT_IDX_IN_MAIL_TEXT_TBL,
680         FIELD_COUNT_OF_MAIL_TEXT_TBL,
681 };
682 #endif
683
684 enum {
685         ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL = 0,
686         RULE_ID_IDX_IN_MAIL_RULE_TBL,
687         FILTER_NAME_IDX_IN_MAIL_RULE_TBL,
688         TYPE_IDX_IN_MAIL_RULE_TBL,
689         VALUE_IDX_IN_MAIL_RULE_TBL,
690         VALUE2_IDX_IN_MAIL_RULE_TBL,
691         ACTION_TYPE_IDX_IN_MAIL_RULE_TBL,
692         TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL,
693         FLAG1_IDX_IN_MAIL_RULE_TBL,
694         FLAG2_IDX_IN_MAIL_RULE_TBL,
695 };
696
697 enum {
698         MAIL_ID_IDX_IN_MAIL_TBL = 0,
699         ACCOUNT_ID_IDX_IN_MAIL_TBL,
700         MAILBOX_ID_IDX_IN_MAIL_TBL,
701         MAILBOX_TYPE_IDX_IN_MAIL_TBL,
702         SUBJECT_IDX_IN_MAIL_TBL,
703         DATETIME_IDX_IN_MAIL_TBL,
704         SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL,
705         SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL,
706         SERVER_MAIL_ID_IDX_IN_MAIL_TBL,
707         MESSAGE_ID_IDX_IN_MAIL_TBL,
708         REFERENCE_ID_IDX_IN_MAIL_TBL,
709         FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL,
710         FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL,
711         FULL_ADDRESS_TO_IDX_IN_MAIL_TBL,
712         FULL_ADDRESS_CC_IDX_IN_MAIL_TBL,
713         FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL,
714         FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL,
715         EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL,
716         EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL,
717         ALIAS_SENDER_IDX_IN_MAIL_TBL,
718         ALIAS_RECIPIENT_IDX_IN_MAIL_TBL,
719         BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL,
720         FILE_PATH_PLAIN_IDX_IN_MAIL_TBL,
721         FILE_PATH_HTML_IDX_IN_MAIL_TBL,
722         FILE_PATH_MIME_ENTITY_IDX_IN_MAIL_TBL,
723         MAIL_SIZE_IDX_IN_MAIL_TBL,
724         FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL,
725         FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL,
726         FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL,
727         FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL,
728         FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL,
729         FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL,
730         FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL,
731         DRM_STATUS_IDX_IN_MAIL_TBL,
732         PRIORITY_IDX_IN_MAIL_TBL,
733         SAVE_STATUS_IDX_IN_MAIL_TBL,
734         LOCK_STATUS_IDX_IN_MAIL_TBL,
735         REPORT_STATUS_IDX_IN_MAIL_TBL,
736         ATTACHMENT_COUNT_IDX_IN_MAIL_TBL,
737         INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL,
738         THREAD_ID_IDX_IN_MAIL_TBL,
739         THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL,
740         PREVIEW_TEXT_IDX_IN_MAIL_TBL,
741         MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL,
742         MESSAGE_CLASS_IDX_IN_MAIL_TBL,
743         DIGEST_TYPE_IDX_IN_MAIL_TBL,
744         SMIME_TYPE_IDX_IN_MAIL_TBL,
745         SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL,
746         REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL,
747         TAG_ID_IDX_IN_MAIL_TBL,
748         REPLIED_TIME_IDX_IN_MAIL_TBL,
749         FORWARDED_TIME_IDX_IN_MAIL_TBL,
750         DEFAULT_CHARSET_IDX_IN_MAIL_TBL,
751         EAS_DATA_LENGTH_IDX_IN_MAIL_TBL,
752         EAS_DATA_IDX_IN_MAIL_TBL,
753         USER_NAME_IDX_IN_MAIL_TBL,
754         FIELD_COUNT_OF_MAIL_TBL,        /* End of mail_tbl */
755 };
756
757 enum {
758         ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL = 0,
759         ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL,
760         ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL,
761         CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
762         ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL,
763         MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
764         ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
765         MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
766         ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
767         ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
768         ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL,
769         ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
770         ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
771 #ifdef __ATTACHMENT_OPTI__
772         ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL,
773         SECTION_IDX_IN_MAIL_ATTACHMENT_TBL,
774 #endif
775 };
776
777 enum {
778         IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL = 0,
779 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
780         MAIL_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
781         ACCOUNT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
782         ADDRESS_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
783         ADDRESS_IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL,
784 #endif
785         ADDRESS_IDX_IN_MAIL_CONTACT_SYNC_TBL,
786         CONTACT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
787         STORAGE_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
788         CONTACT_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
789 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
790         DISPLAY_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
791         FLAG1_IDX_IN_MAIL_CONTACT_SYNC_TBL,
792 #endif
793 };
794
795 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
796 enum {
797         ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL = 0,
798         STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
799         ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
800         MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
801         SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
802         MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
803         MULTI_USER_NAME_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
804 };
805 #endif
806
807 /* sowmya.kr 03032010, changes for get list of mails for given addr list */
808 typedef struct _em_mail_id_list {
809         int mail_id;
810         struct _em_mail_id_list *next;
811 } em_mail_id_list;
812
813 static char *g_test_query[] = {
814         /*  1. select mail_account_tbl */
815         "SELECT"
816                 " account_name, "
817                 " incoming_server_type, "
818                 " incoming_server_address, "
819                 " user_email_address, "
820                 " incoming_server_user_name, "
821                 " incoming_server_password, "
822                 " retrieval_mode, "
823                 " incoming_server_port_number, "
824                 " incoming_server_secure_connection, "
825                 " incoming_server_authentication_method,"
826                 " outgoing_server_type, "
827                 " outgoing_server_address, "
828                 " outgoing_server_port_number, "
829                 " outgoing_server_need_authentication, "
830                 " outgoing_server_secure_connection, "
831                 " outgoing_server_user_name, "
832                 " outgoing_server_password, "
833                 " display_name, "
834                 " reply_to_addr, "
835                 " return_addr, "
836                 " account_id, "
837                 " keep_mails_on_pop_server_after_download, "
838                 " auto_resend_times, "
839                 " outgoing_server_size_limit, "
840                 " wifi_auto_download, "
841                 " pop_before_smtp, "
842                 " incoming_server_requires_apop,"
843                 " logo_icon_path, "
844                 " is_preset_account, "
845                 " check_interval, "
846                 " priority, "
847                 " keep_local_copy, "
848                 " req_delivery_receipt, "
849                 " req_read_receipt, "
850                 " download_limit, "
851                 " block_address, "
852                 " block_subject, "
853                 " display_name_from, "
854                 " reply_with_body, "
855                 " forward_with_files, "
856                 " add_myname_card, "
857                 " add_signature, "
858                 " signature"
859                 ", add_my_address_to_bcc"
860                 ", notification_status "
861                 ", vibrate_status "
862                 ", display_content_status "
863                 ", default_ringtone_status "
864                 ", alert_ringtone_path "
865                 ", account_svc_id "
866                 ", index_color "
867                 ", sync_status "
868                 ", sync_disabled "
869                 ", smime_type"
870                 ", certificate_path"
871                 ", cipher_type"
872                 ", digest_type"
873                 " FROM mail_account_tbl",
874         /*  2. select mail_box_tbl */
875         "SELECT "
876                 "   mailbox_id, "
877                 "   account_id, "
878                 "   local_yn,  "
879                 "   mailbox_name,  "
880                 "   mailbox_type,   "
881                 "   alias,  "
882                 "   deleted_flag,  "
883                 "   modifiable_yn,  "
884                 "   total_mail_count_on_server,  "
885                 "   has_archived_mails, "
886                 "   mail_slot_size, "
887                 "   no_select, "
888                 "   last_sync_time "
889                 " FROM mail_box_tbl ",
890         /*  3. select mail_read_mail_uid_tbl */
891         "SELECT  "
892                 "   account_id,  "
893                 "   mailbox_id,  "
894                 "   mailbox_name,  "
895                 "   local_uid,  "
896                 "   server_uid,  "
897                 "   rfc822_size ,  "
898                 "   sync_status,  "
899                 "   flags_seen_field,  "
900                 "   idx_num "
901                 " FROM mail_read_mail_uid_tbl ",
902         /*  4. select mail_rule_tbl */
903         "SELECT "
904                 "   account_id, "
905                 "   rule_id, "
906                 "   filter_name, "
907                 "   type, "
908                 "   value, "
909                 "   value2, "
910                 "   action_type, "
911                 "   target_mailbox_id,  "
912                 "   flag1, "
913                 "   flag2 "
914                 " FROM mail_rule_tbl    ",
915         /*  5. select mail_tbl */
916         "SELECT"
917                 "       mail_id, "
918                 "       account_id, "
919                 "       mailbox_id, "
920                 "       mailbox_name, "
921                 "   mailbox_type, "
922                 "   subject, "
923                 "       date_time, "
924                 "       server_mail_status, "
925                 "       server_mailbox_name, "
926                 "       server_mail_id, "
927                 "   message_id, "
928                 "       reference_mail_id, "
929                 "   full_address_from, "
930                 "   full_address_reply, "
931                 "   full_address_to, "
932                 "   full_address_cc, "
933                 "   full_address_bcc, "
934                 "   full_address_return, "
935                 "   email_address_sender, "
936                 "   email_address_recipient, "
937                 "   alias_sender, "
938                 "   alias_recipient, "
939                 "       body_download_status, "
940                 "       file_path_plain, "
941                 "       file_path_html, "
942                 "   file_path_mime_entity, "
943                 "       mail_size, "
944                 "   flags_seen_field     ,"
945                 "   flags_deleted_field  ,"
946                 "   flags_flagged_field  ,"
947                 "   flags_answered_field ,"
948                 "   flags_recent_field   ,"
949                 "   flags_draft_field    ,"
950                 "   flags_forwarded_field,"
951                 "       DRM_status, "
952                 "       priority, "
953                 "       save_status, "
954                 "       lock_status, "
955                 "       report_status, "
956                 "   attachment_count, "
957                 "       inline_content_count, "
958                 "       thread_id, "
959                 "       thread_item_count, "
960                 "   preview_text, "
961                 "       meeting_request_status, "
962                 "   message_class, "
963                 "   digest_type, "
964                 "   smime_type "
965                 " FROM mail_tbl",
966         /*  6. select mail_attachment_tbl */
967         "SELECT "
968                 "       attachment_id, "
969                 "       attachment_name, "
970                 "       attachment_path, "
971                 "       attachment_size, "
972                 "       mail_id,  "
973                 "       account_id, "
974                 "       mailbox_id, "
975                 "       attachment_save_status,  "
976                 "       attachment_drm_type,  "
977                 "       attachment_drm_method,  "
978                 "       attachment_inline_content_status,  "
979                 "       attachment_mime_type  "
980                 " FROM mail_attachment_tbl ",
981
982 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
983         "SELECT  "
984                 "   account_id, "
985                 "   mail_id, "
986                 "   server_mail_id, "
987                 "   activity_id, "
988                 "   activity_type, "
989                 "   mailbox_id, "
990                 "   mailbox_name "
991                 "   multi_user_name "
992                 " FROM mail_partial_body_activity_tbl ",
993 #endif
994
995         "SELECT  "
996                 "   mail_id, "
997                 "   account_id, "
998                 "   mailbox_id, "
999                 "   meeting_response, "
1000                 "   start_time, "
1001                 "   end_time, "
1002                 "   location, "
1003                 "   global_object_id, "
1004                 "   offset, "
1005                 "   standard_name, "
1006                 "   standard_time_start_date, "
1007                 "   standard_bias, "
1008                 "   daylight_name, "
1009                 "   daylight_time_start_date, "
1010                 "   daylight_bias "
1011                 " FROM mail_meeting_tbl ",
1012
1013 #ifdef __FEATURE_LOCAL_ACTIVITY__
1014         "SELECT "
1015                 "       activity_id, "
1016                 "   account_id, "
1017                 "   mail_id, "
1018                 "   activity_type, "
1019                 "   server_mailid, "
1020                 "   src_mbox , "
1021                 "       dest_mbox "
1022                 " FROM mail_local_activity_tbl  ",
1023 #endif
1024         "SELECT "
1025                 "       certificate_id, "
1026                 "   issue_year, "
1027                 "   issue_month, "
1028                 "   issue_day, "
1029                 "   expiration_year, "
1030                 "   expiration_month, "
1031                 "   expiration_day, "
1032                 "   issue_organization_name, "
1033                 "   email_address, "
1034                 "   subject_str, "
1035                 "   filepath, "
1036                 "   password "
1037                 " FROM mail_certificate_tbl     ",
1038         "SELECT "
1039                 "       task_id, "
1040                 "   task_type, "
1041                 "   task_status, "
1042                 "   task_priority, "
1043                 "   task_parameter_length, "
1044                 "   task_parameter , "
1045                 "       date_time "
1046                 " FROM mail_task_tbl    ",
1047 #ifdef __FEATURE_BODY_SEARCH__
1048         "SELECT "
1049                 "       mail_id, "
1050                 "   account_id, "
1051                 "   mailbox_id, "
1052                 "   body_text "
1053                 " FROM mail_text_tbl    ",
1054 #endif
1055 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
1056         "SELECT  "
1057                 "   activity_id, "
1058                 "   status, "
1059                 "   account_id, "
1060                 "   mail_id, "
1061                 "   server_mail_id, "
1062                 "   mailbox_id, "
1063                 "   multi_user_name, "
1064                 " FROM mail_auto_download_activity_tbl ",
1065 #endif
1066         NULL,
1067 };
1068
1069 int _field_count_of_table[CREATE_TABLE_MAX] = { 0, };
1070
1071 static int _get_table_field_data_char(char  **table, char *buf, int index)
1072 {
1073         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1074                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1075                 return false;
1076         }
1077
1078         if (table[index] != NULL) {
1079                 *buf = (char)atoi(table[index]);
1080                 return true;
1081         }
1082
1083         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
1084
1085         *buf = 0;
1086         return false;
1087 }
1088
1089 static int _get_table_field_data_int(char  **table, int *buf, int index)
1090 {
1091         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1092                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1093                 return false;
1094         }
1095
1096         if (table[index] != NULL) {
1097                 *buf = atoi(table[index]);
1098                 return true;
1099         }
1100
1101         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
1102
1103         *buf = 0;
1104         return false;
1105 }
1106
1107 static int _get_table_field_data_time_t(char  **table, time_t *buf, int index)
1108 {
1109         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1110                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1111                 return false;
1112         }
1113
1114         if (table[index] != NULL) {
1115                 *buf = (time_t)atol(table[index]);
1116                 return true;
1117         }
1118
1119         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
1120
1121         *buf = 0;
1122         return false;
1123 }
1124
1125 static int _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
1126 {
1127         int ret = false;
1128
1129         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1130                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1131                 return false;
1132         }
1133
1134         char *pTemp = table[index];
1135         int sLen = 0;
1136         if (pTemp == NULL)
1137                 *buf = NULL;
1138         else {
1139                 sLen = EM_SAFE_STRLEN(pTemp);
1140                 if (sLen) {
1141                         *buf = (char *) em_malloc(sLen + 1);
1142                         if (*buf == NULL) {
1143                                 EM_DEBUG_EXCEPTION("malloc is failed");
1144                                 goto FINISH_OFF;
1145                         }
1146                         strncpy(*buf, pTemp, sLen+1);
1147                         (*buf)[sLen] = '\0';
1148                 } else
1149                         *buf = NULL;
1150         }
1151 #ifdef _PRINT_STORAGE_LOG_
1152         if (*buf)
1153                 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", *buf, index);
1154         else
1155                 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1156 #endif
1157         ret = true;
1158 FINISH_OFF:
1159
1160         return ret;
1161 }
1162
1163 static int _get_table_field_data_string_without_allocation(char **table, char *buf, int buffer_size, int ucs2, int index)
1164 {
1165         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1166                 EM_DEBUG_EXCEPTION(" table[%p], buf[%p], index[%d]", table, buf, index);
1167                 return false;
1168         }
1169
1170         char *pTemp = table[index];
1171
1172         if (pTemp == NULL)
1173                 buf = NULL;
1174         else {
1175                 memset(buf, 0, buffer_size);
1176                 strncpy(buf, pTemp, buffer_size - 1);
1177         }
1178 #ifdef _PRINT_STORAGE_LOG_
1179         if (buf)
1180                 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", buf, index);
1181         else
1182                 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1183 #endif
1184
1185         return true;
1186 }
1187
1188 static int _get_table_field_data_blob(char **table, void **buffer, int buffer_size, int index)
1189 {
1190         if ((table == NULL) || (buffer == NULL) || (index < 0)) {
1191                 EM_DEBUG_EXCEPTION(" table[%p], buffer[%p], buffer_size [%d], index[%d]", table, buffer, buffer_size, index);
1192                 return false;
1193         }
1194
1195         char *temp_buffer = table[index];
1196
1197         if (temp_buffer == NULL)
1198                 buffer = NULL;
1199         else {
1200                 *buffer = malloc(buffer_size);
1201                 if (*buffer == NULL) {
1202                         EM_DEBUG_EXCEPTION("allocation failed.");
1203                         return false;
1204                 }
1205                 memset(*buffer, 0, buffer_size);
1206                 memcpy(*buffer, temp_buffer, buffer_size);
1207         }
1208 #ifdef _PRINT_STORAGE_LOG_
1209         if (buf)
1210                 EM_DEBUG_LOG("_get_table_field_data_string - buffer[%s], index[%d]", buffer, index);
1211         else
1212                 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1213 #endif
1214
1215         return true;
1216 }
1217
1218 static int _get_stmt_field_data_char(DB_STMT hStmt, char *buf, int index)
1219 {
1220         if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1221                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1222                 return false;
1223         }
1224
1225         if (sqlite3_column_text(hStmt, index) != NULL) {
1226                 *buf = (char)sqlite3_column_int(hStmt, index);
1227 #ifdef _PRINT_STORAGE_LOG_
1228                 EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1229 #endif
1230                 return true;
1231         }
1232
1233         EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1234
1235         return false;
1236 }
1237
1238 static int _get_stmt_field_data_int(DB_STMT hStmt, int *buf, int index)
1239 {
1240         if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1241                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1242                 return false;
1243         }
1244
1245         if (sqlite3_column_text(hStmt, index) != NULL) {
1246                 *buf = sqlite3_column_int(hStmt, index);
1247 #ifdef _PRINT_STORAGE_LOG_
1248                 EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1249 #endif
1250                 return true;
1251         }
1252
1253         EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1254
1255         return false;
1256 }
1257
1258 static int _get_stmt_field_data_time_t(DB_STMT hStmt, time_t *buf, int index)
1259 {
1260         if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1261                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1262                 return false;
1263         }
1264
1265         if (sqlite3_column_text(hStmt, index) != NULL) {
1266                 *buf = (time_t)sqlite3_column_int(hStmt, index);
1267 #ifdef _PRINT_STORAGE_LOG_
1268                 EM_DEBUG_LOG("_get_stmt_field_data_time_t [%d]", *buf);
1269 #endif
1270                 return true;
1271         }
1272
1273         EM_DEBUG_LOG("_get_stmt_field_data_time_t fail. index [%d]", index);
1274         return false;
1275 }
1276
1277 static int _get_stmt_field_data_string(DB_STMT hStmt, char **buf, int ucs2, int index)
1278 {
1279         if (!hStmt || !buf || (index < 0)) { /*prevent 39619*/
1280                 EM_DEBUG_EXCEPTION("No hStmt, buf[%p], index[%d]", buf, index);
1281                 return false;
1282         }
1283
1284         int sLen = 0;
1285         sLen = sqlite3_column_bytes(hStmt, index);
1286
1287 #ifdef _PRINT_STORAGE_LOG_
1288         EM_DEBUG_LOG("_get_stmt_field_data_string sqlite3_column_bytes sLen[%d]", sLen);
1289 #endif
1290
1291         if (sLen > 0) {
1292                 *buf = (char *) em_malloc(sLen + 1);
1293                 if (*buf == NULL) {
1294                         EM_DEBUG_EXCEPTION("em_mallocfailed");
1295                         return false;
1296                 }
1297
1298                 strncpy(*buf, (char *)sqlite3_column_text(hStmt, index), sLen);
1299         } else
1300                 *buf = NULL;
1301
1302 #ifdef _PRINT_STORAGE_LOG_
1303         if (*buf)
1304                 EM_DEBUG_LOG("buf[%s], index[%d]", *buf, index);
1305         else
1306                 EM_DEBUG_LOG("_get_stmt_field_data_string - No string got");
1307 #endif
1308
1309         return false;
1310 }
1311
1312 static void _get_stmt_field_data_blob(DB_STMT hStmt, void **buf, int index)
1313 {
1314         if (!hStmt || !buf || (index < 0)) { /*prevent 39618*/
1315                 EM_DEBUG_EXCEPTION("No hStmt, buf[%p], index[%d]", buf, index);
1316                 return;
1317         }
1318
1319         int sLen = 0;
1320         sLen = sqlite3_column_bytes(hStmt, index);
1321
1322 #ifdef _PRINT_STORAGE_LOG_
1323         EM_DEBUG_LOG("_get_stmt_field_data_blob sqlite3_column_bytes sLen[%d]", sLen);
1324 #endif
1325
1326         if (sLen > 0) {
1327                 *buf = (char *) em_malloc(sLen);
1328                 if (*buf == NULL) {
1329                         EM_DEBUG_EXCEPTION("em_mallocfailed");
1330                         return;
1331                 }
1332
1333                 memcpy(*buf, (void *)sqlite3_column_blob(hStmt, index), sLen);
1334         } else
1335                 *buf = NULL;
1336
1337 }
1338
1339 static int _bind_stmt_field_data_char(DB_STMT hStmt, int index, char value)
1340 {
1341         if ((hStmt == NULL) || (index < 0)) {
1342                 EM_DEBUG_EXCEPTION("index[%d]", index);
1343                 return false;
1344         }
1345
1346         int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1347
1348         if (ret != SQLITE_OK) {
1349                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1350                 return false;
1351         }
1352
1353         return true;
1354 }
1355
1356 static int _bind_stmt_field_data_int(DB_STMT hStmt, int index, int value)
1357 {
1358         if ((hStmt == NULL) || (index < 0)) {
1359                 EM_DEBUG_EXCEPTION("index[%d]", index);
1360                 return false;
1361         }
1362
1363         int ret = sqlite3_bind_int(hStmt, index+1, value);
1364
1365         if (ret != SQLITE_OK) {
1366                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1367                 return false;
1368         }
1369
1370         return true;
1371 }
1372
1373 static int _bind_stmt_field_data_time_t(DB_STMT hStmt, int index, time_t value)
1374 {
1375         if ((hStmt == NULL) || (index < 0)) {
1376                 EM_DEBUG_EXCEPTION("index[%d]", index);
1377                 return false;
1378         }
1379
1380         int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1381
1382         if (ret != SQLITE_OK) {
1383                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1384                 return false;
1385         }
1386
1387         return true;
1388 }
1389
1390 static int _bind_stmt_field_data_string(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1391 {
1392         if ((hStmt == NULL) || (index < 0)) {
1393                 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1394                 return false;
1395         }
1396
1397 #ifdef _PRINT_STORAGE_LOG_
1398         EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1399 #endif
1400
1401         int ret = 0;
1402         if (value != NULL)
1403                 ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1404         else
1405                 ret = sqlite3_bind_text(hStmt, index+1, "", -1, SQLITE_STATIC);
1406
1407         if (ret != SQLITE_OK) {
1408                 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1409                 return false;
1410         }
1411         return true;
1412 }
1413
1414 static int _bind_stmt_field_data_nstring(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1415 {
1416         if ((hStmt == NULL) || (index < 0)) {
1417                 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1418                 return false;
1419         }
1420
1421 #ifdef _PRINT_STORAGE_LOG_
1422         EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1423 #endif
1424
1425         int ret = 0;
1426         if (value != NULL) {
1427                 if (strlen(value) <= max_len)
1428                         ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1429                 else {
1430                         char *buf = (char*)em_malloc(sizeof(char) * (max_len));
1431                         if (buf == NULL) {
1432                                 EM_DEBUG_EXCEPTION("em_mallocfailed");
1433                                 return false;
1434                         }
1435                         snprintf(buf, max_len-1, "%s", value);
1436                         ret = sqlite3_bind_text(hStmt, index+1, buf, -1, SQLITE_TRANSIENT);
1437                         g_free(buf);
1438                 }
1439         } else
1440                 ret = sqlite3_bind_text(hStmt, index+1, "", -1, SQLITE_STATIC);
1441
1442         if (ret != SQLITE_OK) {
1443                 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1444                 return false;
1445         }
1446         return true;
1447 }
1448
1449
1450 static int _bind_stmt_field_data_blob(DB_STMT hStmt, int index, void *blob, int blob_size)
1451 {
1452         if ((hStmt == NULL) || (index < 0)) {
1453                 EM_DEBUG_EXCEPTION("index[%d], blob_size[%d]", index, blob_size);
1454                 return false;
1455         }
1456
1457 #ifdef _PRINT_STORAGE_LOG_
1458         EM_DEBUG_LOG("hStmt = %p, index = %d, blob_size = %d, blob = [%p]", hStmt, index, blob_size, blob);
1459 #endif
1460
1461         int ret = 0;
1462         if (blob_size > 0)
1463                 ret = sqlite3_bind_blob(hStmt, index+1, blob, blob_size, SQLITE_STATIC);
1464         else
1465                 ret = sqlite3_bind_null(hStmt, index+1);
1466
1467         if (ret != SQLITE_OK) {
1468                 EM_DEBUG_EXCEPTION("sqlite3_bind_blob fail [%d]", ret);
1469                 return false;
1470         }
1471         return true;
1472 }
1473
1474 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)
1475 {
1476
1477         EM_DEBUG_FUNC_BEGIN("Prepared Statement: local_db_handle[%p] query_string[%p]", local_db_handle, query_string);
1478         int error = EMAIL_ERROR_NONE;
1479         int rc = 0, count = 0;
1480         DB_STMT db_statement = NULL;
1481
1482         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, query_string, EM_SAFE_STRLEN(query_string), &db_statement, NULL), rc);
1483
1484         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
1485                         ("sqlite3_prepare_stmt failed [%d] [%s]", rc, query_string));
1486
1487         _bind_stmt_field_data_string(db_statement, count++, target_mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
1488         _bind_stmt_field_data_int(db_statement, count++, input_mailbox_id);
1489         _bind_stmt_field_data_int(db_statement, count++, target_account_id);
1490
1491
1492
1493         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(db_statement), rc);
1494         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
1495                         ("sqlite3_step_stmt failed [%d] [%s]", rc, query_string));
1496
1497 FINISH_OFF:
1498
1499         if (db_statement != NULL) {
1500                 rc = sqlite3_finalize(db_statement);
1501                 if (rc != SQLITE_OK) {
1502                         EM_DEBUG_EXCEPTION("sqlite3_finalize_stmt error [%d]", rc);
1503                         error = EMAIL_ERROR_DB_FAILURE;
1504                 }
1505         }
1506
1507         return error;
1508
1509
1510 }
1511
1512 static int _delete_temp_file(const char *path)
1513 {
1514         EM_DEBUG_FUNC_BEGIN("path[%p]", path);
1515
1516         DIR *dp = NULL;
1517         struct dirent *entry = NULL;
1518
1519         char buf[1024] = {0x00, };
1520
1521         if ((dp = opendir(path)) == NULL) {
1522                 EM_DEBUG_EXCEPTION("opendir(\"%s\") failed...", path);
1523                 return false;
1524         }
1525
1526         while ((entry = readdir(dp)) != NULL) {
1527                 SNPRINTF(buf, sizeof(buf), "%s/%s", path, entry->d_name);
1528                 if (remove(buf) != 0) {
1529                         EM_DEBUG_EXCEPTION("File Deletion Failed`");
1530                 }
1531         }
1532
1533         closedir(dp);
1534         EM_DEBUG_FUNC_END();
1535         return true;
1536 }
1537
1538 char *cpy_str(char *src)
1539 {
1540         char *p = NULL;
1541
1542         if (src) {
1543                 if (!(p = em_malloc((int)EM_SAFE_STRLEN(src) + 1))) {
1544                         EM_DEBUG_EXCEPTION("mailoc failed...");
1545                         return NULL;
1546                 }
1547                 strncpy(p, src, EM_SAFE_STRLEN(src)+1);
1548         }
1549
1550         return p;
1551 }
1552
1553 static void _emstorage_close_once(void)
1554 {
1555         EM_DEBUG_FUNC_BEGIN();
1556
1557         EM_DEBUG_FUNC_END();
1558 }
1559
1560 INTERNAL_FUNC int emstorage_close(int *err_code)
1561 {
1562         EM_DEBUG_FUNC_BEGIN();
1563
1564         int ret = false;
1565         int error = EMAIL_ERROR_NONE;
1566
1567         if (!emstorage_db_close(NULL, &error))
1568
1569                 if (--_open_counter == 0)
1570                         _emstorage_close_once();
1571
1572         ret = true;
1573
1574         if (err_code != NULL)
1575                 *err_code = error;
1576
1577         EM_DEBUG_FUNC_END("ret [%d]", ret);
1578         return ret;
1579 }
1580
1581 static void *_emstorage_open_once(char *multi_user_name, int *err_code)
1582 {
1583         EM_DEBUG_FUNC_BEGIN();
1584
1585         int error = EMAIL_ERROR_NONE;
1586
1587         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1588                 char buf[MAX_PATH] = {0};
1589                 char *prefix_path = NULL;
1590
1591                 error = emcore_get_container_path(multi_user_name, &prefix_path);
1592                 if (error != EMAIL_ERROR_NONE) {
1593                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
1594                         goto FINISH_OFF;
1595                 }
1596
1597                 memset(buf, 0x00, sizeof(buf));
1598                 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, EMAIL_PATH);
1599                 mkdir(buf, DIRECTORY_PERMISSION);
1600
1601                 memset(buf, 0x00, sizeof(buf));
1602                 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAIL_HOME);
1603                 mkdir(buf, DIRECTORY_PERMISSION);
1604
1605                 memset(buf, 0x00, sizeof(buf));
1606                 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAIL_TEMP);
1607                 mkdir(buf, DIRECTORY_PERMISSION);
1608
1609                 _delete_temp_file(buf);
1610                 EM_SAFE_FREE(prefix_path);
1611         } else {
1612                 mkdir(DATA_PATH, DIRECTORY_PERMISSION);
1613                 mkdir(EMAIL_PATH, DIRECTORY_PERMISSION);
1614                 mkdir(MAIL_HOME, DIRECTORY_PERMISSION);
1615                 mkdir(MAIL_TEMP, DIRECTORY_PERMISSION);
1616
1617                 _delete_temp_file(MAIL_TEMP);
1618         }
1619
1620         if (!emstorage_create_table(multi_user_name, EMAIL_CREATE_DB_NORMAL, &error)) {
1621                 EM_DEBUG_EXCEPTION(" emstorage_create_table failed - %d", error);
1622                 goto FINISH_OFF;
1623         }
1624
1625 FINISH_OFF:
1626
1627
1628         if (err_code != NULL)
1629                 *err_code = error;
1630
1631         return NULL;
1632 }
1633
1634 /*  pData : a parameter which is registered when busy handler is registerd */
1635 /*  count : retry count */
1636 static int _callback_sqlite_busy_handler(void *pData, int count)
1637 {
1638         if (10 - count > 0) {
1639                 struct timespec time = {
1640                         .tv_sec = 0,
1641                         .tv_nsec = (count + 1) * 100 * 1000 * 1000
1642                 };
1643                 EM_DEBUG_LOG("Busy handler called!!: PID[%d] / CNT [%d]", getpid(), count);
1644                 nanosleep(&time, NULL);
1645                 return 1;
1646         } else {
1647                 EM_DEBUG_EXCEPTION("Busy handler will be returned SQLITE_BUSY error PID[%d] / CNT[%d]", getpid(), count);
1648                 return 0;
1649         }
1650 }
1651
1652 static int _callback_collation_utf7_sort(void *data, int length_text_a, const void *text_a,
1653                 int length_text_b, const void *text_b)
1654 {
1655         EM_DEBUG_FUNC_BEGIN();
1656         int result = 0;
1657         char *converted_string_a = NULL;
1658         char *converted_string_b = NULL;
1659
1660         EM_DEBUG_LOG_DEV("text_a : [%s]", text_a);
1661         converted_string_a = emcore_convert_mutf7_to_utf8((char *)text_a);
1662         EM_DEBUG_LOG_DEV("Converted text_a : [%s]", converted_string_a);
1663
1664         EM_DEBUG_LOG_DEV("text_b : [%s]", text_b);
1665         converted_string_b = emcore_convert_mutf7_to_utf8((char *)text_b);
1666         EM_DEBUG_LOG_DEV("Converted text_b : [%s]", converted_string_b);
1667
1668         if (converted_string_a && converted_string_b)
1669                 result = strcmp(converted_string_a, converted_string_b);
1670
1671         EM_SAFE_FREE(converted_string_a);
1672         EM_SAFE_FREE(converted_string_b);
1673
1674         EM_DEBUG_FUNC_END();
1675         return result;
1676 }
1677
1678 static int _delete_all_files_and_directories(char *db_file_path, int *err_code)
1679 {
1680         EM_DEBUG_FUNC_BEGIN();
1681
1682         int error = EMAIL_ERROR_NONE;
1683         int ret = false;
1684
1685         if (!emstorage_delete_file(db_file_path, &error)) {
1686                 if (error != EMAIL_ERROR_FILE_NOT_FOUND) {
1687                         EM_DEBUG_EXCEPTION_SEC("remove failed - %s", EMAIL_SERVICE_DB_FILE_PATH);
1688                         goto FINISH_OFF;
1689                 }
1690         }
1691
1692         if (!emstorage_delete_dir((char *)MAIL_HOME, &error)) {
1693                 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed");
1694                 goto FINISH_OFF;
1695         }
1696
1697         ret = true;
1698
1699 FINISH_OFF:
1700         if (err_code)
1701                 *err_code = error;
1702         EM_DEBUG_FUNC_END();
1703         return ret;
1704 }
1705
1706 static int _recovery_from_malformed_db_file(char *db_file_path, int *err_code)
1707 {
1708         EM_DEBUG_FUNC_BEGIN();
1709
1710         int error = EMAIL_ERROR_NONE;
1711         int ret = false;
1712
1713         /* Delete all files and directories */
1714         if (!_delete_all_files_and_directories(db_file_path, &error)) {
1715                 EM_DEBUG_EXCEPTION("_delete_all_files_and_directories failed [%d]", error);
1716                 goto FINISH_OFF;
1717         }
1718
1719         /* Delete all accounts on MyAccount */
1720
1721         /* Delete all managed connection to DB */
1722         emstorage_reset_db_handle_list();
1723
1724         ret = true;
1725
1726 FINISH_OFF:
1727         if (err_code)
1728                 *err_code = error;
1729         EM_DEBUG_FUNC_END();
1730         return ret;
1731 }
1732
1733 int _xsystem(const char *argv[])
1734 {
1735         int status = 0;
1736         pid_t pid;
1737         pid = fork();
1738
1739         switch (pid) {
1740         case -1:
1741                 perror("fork failed");
1742                 return -1;
1743         case 0:
1744                 if (execvp(argv[0], (char *const *)argv) == -1) {
1745                         perror("execute init db script");
1746                         _exit(-1);
1747                 }
1748                 _exit(2);
1749         default:
1750                 /* parent */
1751                 break;
1752         }
1753
1754         if (waitpid(pid, &status, 0) == -1) {
1755                 perror("waitpid failed");
1756                 return -1;
1757         }
1758
1759         if (WIFSIGNALED(status)) {
1760                 perror("signal");
1761                 return -1;
1762         }
1763
1764         if (!WIFEXITED(status)) {
1765                 perror("should not happen");
1766                 return -1;
1767         }
1768
1769         return WEXITSTATUS(status);
1770 }
1771
1772 #define SCRIPT_INIT_DB "/usr/bin/email-service_init_db.sh"
1773
1774 INTERNAL_FUNC int emstorage_init_db(char *multi_user_name)
1775 {
1776         EM_DEBUG_FUNC_BEGIN();
1777         int err = EMAIL_ERROR_NONE;
1778         char *prefix_path = NULL;
1779         char *output_file_path = NULL;
1780         char temp_file_path[MAX_PATH] = {0};
1781         gid_t email_gid = -1;
1782         uid_t email_uid = -1;
1783         struct group *gr;
1784         struct group buf_group;
1785         struct passwd *pw;
1786         struct passwd buf_passwd;
1787         char buf_gr[MAX_GRP_BUF_SIZE];
1788         char buf_pw[MAX_GRP_BUF_SIZE];
1789         int result = 0;
1790
1791         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1792                 err = emcore_get_container_path(multi_user_name, &prefix_path);
1793                 if (err != EMAIL_ERROR_NONE) {
1794                         if (err != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1795                                 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", err);
1796                                 goto FINISH_OFF;
1797                         }
1798                 }
1799         } else {
1800                 prefix_path = strdup("");
1801         }
1802
1803         if (err == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1804                 err = emcore_get_canonicalize_path((char *)EMAIL_SERVICE_DB_FILE_PATH, &output_file_path);
1805                 if (err != EMAIL_ERROR_NONE) {
1806                         EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", err);
1807                         goto FINISH_OFF;
1808                 }
1809
1810                 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
1811         } else {
1812                 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
1813         }
1814
1815         EM_DEBUG_LOG("db file path : [%s]", temp_file_path);
1816
1817         if (!g_file_test(temp_file_path, G_FILE_TEST_EXISTS)) {
1818                 int ret = true;
1819                 const char *argv_script[] = {"/bin/sh", SCRIPT_INIT_DB, NULL};
1820                 ret = _xsystem(argv_script);
1821
1822                 if (ret == -1) {
1823                         EM_DEBUG_EXCEPTION("_xsystem failed");
1824                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1825                 }
1826
1827                 result = getgrnam_r(GID, &buf_group, buf_gr, sizeof(buf_gr), &gr);
1828                 if (result == 0 && NULL != gr)
1829                         email_gid = gr->gr_gid;
1830                 else
1831                         email_gid = GID_DEFAULT;
1832
1833
1834                 result = getpwnam_r(UID, &buf_passwd, buf_pw, sizeof(buf_pw), &pw);
1835                 if (result == 0 && NULL != pw)
1836                         email_uid = pw->pw_uid;
1837                 else
1838                         email_uid = UID_DEFAULT;
1839
1840
1841                 chmod(EMAIL_SERVICE_DB_FILE_PATH, 0660);
1842                 chmod(EMAIL_SERVICE_DB_JOURNAL_FILE_PATH, 0660);
1843                 chmod(EMAIL_SQL_PATH, 0660);
1844
1845                 ret = chown(EMAIL_SERVICE_DB_FILE_PATH, email_uid, email_gid);
1846                 if (0 != ret)
1847                         EM_DEBUG_EXCEPTION("chown() Fail[errno=%d]", errno);
1848                 ret = chown(EMAIL_SERVICE_DB_JOURNAL_FILE_PATH, email_uid, email_gid);
1849                 if (0 != ret)
1850                         EM_DEBUG_EXCEPTION("chown() Fail[errno=%d]", errno);
1851                 ret = chown(EMAIL_SQL_PATH, email_uid, email_gid);
1852                 if (0 != ret)
1853                         EM_DEBUG_EXCEPTION("chown() Fail[errno=%d]", errno);
1854
1855         }
1856
1857 FINISH_OFF:
1858
1859         EM_SAFE_FREE(prefix_path);
1860         EM_SAFE_FREE(output_file_path);
1861
1862         EM_DEBUG_FUNC_END();
1863         return err;
1864 }
1865
1866 INTERNAL_FUNC int em_db_open(char *db_file_path, sqlite3 **sqlite_handle, int *err_code)
1867 {
1868         EM_DEBUG_FUNC_BEGIN();
1869         int rc = 0;
1870         int error = EMAIL_ERROR_NONE;
1871         int ret = false;
1872
1873         EM_DEBUG_LOG_DEV("*sqlite_handle[%p]", *sqlite_handle);
1874
1875         if (*sqlite_handle) { /*prevent 33351*/
1876                 EM_DEBUG_LOG_DEV(">>>>> DB Already Opened......");
1877                 if (err_code != NULL)
1878                         *err_code = error;
1879                 return true;
1880         }
1881
1882         EM_DEBUG_LOG("DB file path : [%s]", db_file_path);
1883
1884         /*  db open */
1885         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1886         if (SQLITE_OK != rc) {
1887                 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1888                 if (SQLITE_PERM == rc || SQLITE_CANTOPEN == rc)
1889                         error = EMAIL_ERROR_PERMISSION_DENIED;
1890                 else
1891                         error = EMAIL_ERROR_DB_FAILURE;
1892
1893                 sqlite3_close(*sqlite_handle);
1894                 *sqlite_handle = NULL;
1895
1896                 if (SQLITE_CORRUPT == rc) /* SQLITE_CORRUPT : The database disk image is malformed */ {/* Recovery DB file */
1897                         EM_DEBUG_LOG("The database disk image is malformed. Trying to remove and create database disk image and directories");
1898                         if (!_recovery_from_malformed_db_file(db_file_path, &error)) {
1899                                 EM_DEBUG_EXCEPTION("_recovery_from_malformed_db_file failed [%d]", error);
1900                                 goto FINISH_OFF;
1901                         }
1902
1903                         EM_DEBUG_LOG("Open DB again");
1904                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1905                         if (SQLITE_OK != rc) {
1906                                 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1907                                 if (SQLITE_PERM == rc)
1908                                         error = EMAIL_ERROR_PERMISSION_DENIED;
1909                                 else
1910                                         error = EMAIL_ERROR_DB_FAILURE;
1911
1912                                 sqlite3_close(*sqlite_handle);
1913                                 *sqlite_handle = NULL;
1914                                 goto FINISH_OFF; /*prevent 33351*/
1915                         }
1916                 } else
1917                         goto FINISH_OFF;
1918         }
1919
1920         /* register busy handler */
1921         EM_DEBUG_LOG_DEV(">>>>> Register DB Handle to busy handler: *sqlite_handle[%p]", *sqlite_handle);
1922         rc = sqlite3_busy_handler(*sqlite_handle, _callback_sqlite_busy_handler, NULL);  /*  Busy Handler registration, NULL is a parameter which will be passed to handler */
1923         if (SQLITE_OK != rc) {
1924                 EM_DEBUG_EXCEPTION("sqlite3_busy_handler fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1925                 error = EMAIL_ERROR_DB_FAILURE;
1926                 sqlite3_close(*sqlite_handle);
1927                 *sqlite_handle = NULL;
1928                 goto FINISH_OFF;
1929         }
1930
1931         /* Register collation callback function */
1932         rc = sqlite3_create_collation(*sqlite_handle, "CONVERTUTF8", SQLITE_UTF8, NULL, _callback_collation_utf7_sort);
1933         if (SQLITE_OK != rc) {
1934                 EM_DEBUG_EXCEPTION("sqlite3_create_collation failed : [%d][%s]", rc, sqlite3_errmsg(*sqlite_handle));
1935                 error = EMAIL_ERROR_DB_FAILURE;
1936                 sqlite3_close(*sqlite_handle);
1937                 *sqlite_handle = NULL;
1938                 goto FINISH_OFF;
1939         }
1940
1941         ret = true;
1942
1943 FINISH_OFF:
1944         if (err_code != NULL)
1945                 *err_code = error;
1946
1947         EM_DEBUG_FUNC_END("ret [%d]", ret);
1948         return ret;
1949 }
1950
1951 INTERNAL_FUNC sqlite3* emstorage_db_open(char *multi_user_name, int *err_code)
1952 {
1953         EM_DEBUG_FUNC_BEGIN();
1954
1955         sqlite3 *_db_handle = NULL;
1956
1957         int error = EMAIL_ERROR_NONE;
1958         char *prefix_path = NULL;
1959
1960         _db_handle = emstorage_get_db_handle(multi_user_name);
1961
1962         if (_db_handle == NULL) {
1963                 char *output_file_path = NULL;
1964                 char temp_file_path[MAX_PATH] = {0};
1965
1966                 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1967                         error = emcore_get_container_path(multi_user_name, &prefix_path);
1968                         if (error != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION && error != EMAIL_ERROR_NONE) {
1969                                 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", error);
1970                                 goto FINISH_OFF;
1971                         }
1972                 } else {
1973                         prefix_path = strdup("");
1974                 }
1975
1976                 if (error == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1977                         if ((error = emcore_get_canonicalize_path((char *)EMAIL_SERVICE_DB_FILE_PATH, &output_file_path)) != EMAIL_ERROR_NONE) {
1978                                 EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", error);
1979                                 goto FINISH_OFF;
1980                         }
1981
1982                         SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
1983                         EM_SAFE_FREE(output_file_path);
1984                 } else {
1985                         SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
1986                 }
1987
1988                 if (!em_db_open(temp_file_path, &_db_handle, &error)) {
1989                         EM_DEBUG_EXCEPTION("em_db_open failed[%d]", error);
1990                         goto FINISH_OFF;
1991                 }
1992
1993                 _initialize_shm_mutex(SHM_FILE_FOR_DB_LOCK, &shm_fd_for_db_lock, &mapped_for_db_lock);
1994
1995 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
1996                 _initialize_shm_mutex(SHM_FILE_FOR_MAIL_ID_LOCK, &shm_fd_for_generating_mail_id, &mapped_for_generating_mail_id);
1997 #endif /*__FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
1998
1999                 emstorage_set_db_handle(multi_user_name, _db_handle);
2000
2001                 emstorage_initialize_field_count();
2002         }
2003
2004 FINISH_OFF:
2005
2006         EM_SAFE_FREE(prefix_path);
2007
2008         if (err_code != NULL)
2009                 *err_code = error;
2010
2011         EM_DEBUG_FUNC_END("ret [%p]", _db_handle);
2012         return _db_handle;
2013 }
2014
2015 INTERNAL_FUNC int emstorage_db_close(char *multi_user_name, int *err_code)
2016 {
2017         EM_DEBUG_FUNC_BEGIN();
2018 #ifdef _MULTIPLE_DB_HANDLE
2019         sqlite3 *_db_handle = emstorage_get_db_handle(multi_user_name);
2020 #endif
2021
2022         int error = EMAIL_ERROR_NONE;
2023         int ret = false;
2024
2025         if (_db_handle) {
2026                 ret = sqlite3_close(_db_handle);
2027                 if (ret != SQLITE_OK) {
2028                         EM_DEBUG_EXCEPTION(" sqlite3_close fail - %d", ret);
2029                         error = EMAIL_ERROR_DB_FAILURE;
2030                         ret = false;
2031                         goto FINISH_OFF;
2032                 }
2033 #ifdef _MULTIPLE_DB_HANDLE
2034                 emstorage_remove_db_handle();
2035 #endif
2036                 _db_handle = NULL;
2037         }
2038
2039         ret = true;
2040
2041 FINISH_OFF:
2042         if (err_code != NULL)
2043                 *err_code = error;
2044
2045         EM_DEBUG_FUNC_END("ret [%d]", ret);
2046         return ret;
2047 }
2048
2049 INTERNAL_FUNC int emstorage_open(char *multi_user_name, int *err_code)
2050 {
2051         EM_DEBUG_FUNC_BEGIN();
2052
2053         int ret = false;
2054         int error = EMAIL_ERROR_NONE;
2055         int retValue;
2056         char *prefix_path = NULL;
2057         char buf[MAX_PATH] = {0};
2058
2059         if (EM_SAFE_STRLEN(multi_user_name) <= 0) {
2060                 SNPRINTF(buf, sizeof(buf), "%s", DB_PATH);
2061         } else {
2062                 error = emcore_get_container_path(multi_user_name, &prefix_path);
2063                 if (error != EMAIL_ERROR_NONE) {
2064                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
2065                         goto FINISH_OFF;
2066                 }
2067                 SNPRINTF(buf, sizeof(buf), "%s/%s", prefix_path, DB_PATH);
2068         }
2069
2070         if (!g_file_test(buf, G_FILE_TEST_EXISTS)) {
2071                 retValue = mkdir(buf, DIRECTORY_PERMISSION);
2072
2073                 EM_DEBUG_LOG("mkdir return- %d", retValue);
2074                 EM_DEBUG_LOG("emstorage_open - before sqlite3_open - pid = %d", getpid());
2075         }
2076
2077         if (emstorage_db_open(multi_user_name, &error) == NULL) {
2078                 EM_DEBUG_EXCEPTION("emstorage_db_open failed[%d]", error);
2079                 goto FINISH_OFF;
2080         }
2081
2082         if (_open_counter++ == 0)
2083                 _emstorage_open_once(multi_user_name, &error);
2084
2085         ret = true;
2086
2087 FINISH_OFF:
2088
2089         EM_SAFE_FREE(prefix_path);
2090
2091         if (err_code != NULL)
2092                 *err_code = error;
2093
2094         EM_DEBUG_FUNC_END("ret [%d]", ret);
2095         return ret;
2096 }
2097
2098 static int emstorage_get_field_count_from_create_table_query(char *input_create_table_query, int *output_field_count)
2099 {
2100         EM_DEBUG_FUNC_BEGIN("input_create_table_query[%d], output_field_count[%p]", input_create_table_query, output_field_count);
2101         int err = EMAIL_ERROR_NONE;
2102         int field_count = 0;
2103         char *pos = NULL;
2104
2105         if (input_create_table_query == NULL || output_field_count == NULL) {
2106                 err = EMAIL_ERROR_INVALID_PARAM;
2107                 goto FINISH_OFF;
2108         }
2109
2110         pos = input_create_table_query;
2111
2112         do {
2113                 field_count++;
2114                 if (pos == NULL || *pos == (char)0)
2115                         break;
2116                 pos += 1;
2117                 pos = strchr(pos, ',');
2118         } while (pos);
2119
2120         *output_field_count = field_count;
2121
2122         EM_DEBUG_LOG_DEV("field_count [%d]", field_count);
2123
2124 FINISH_OFF:
2125
2126         EM_DEBUG_FUNC_END("err [%d]", err);
2127         return err;
2128 }
2129
2130 INTERNAL_FUNC int emstorage_initialize_field_count()
2131 {
2132         EM_DEBUG_FUNC_BEGIN();
2133         int err = EMAIL_ERROR_NONE;
2134         int query_len = 0;
2135         char **create_table_query = NULL;
2136
2137         if (_field_count_of_table[CREATE_TABLE_MAIL_ACCOUNT_TBL] != 0) {
2138                 err = EMAIL_ERROR_ALREADY_INITIALIZED;
2139                 goto FINISH_OFF;
2140         }
2141
2142         err = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2143         if (err != EMAIL_ERROR_NONE) {
2144                 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", err);
2145                 goto FINISH_OFF;
2146         }
2147
2148         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]));
2149         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]));
2150         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]));
2151         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]));
2152         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_TBL]));
2153         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]));
2154         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]));
2155         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]));
2156 #ifdef __FEATURE_LOCAL_ACTIVITY__
2157         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]));
2158 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2159         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]));
2160         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]));
2161         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]));
2162 FINISH_OFF:
2163
2164         if (create_table_query) {
2165                 int i = 0;
2166                 for (i = 0; i < query_len; i++) {
2167                         if (create_table_query[i])
2168                                 EM_SAFE_FREE(create_table_query[i]);
2169                 }
2170                 EM_SAFE_FREE(create_table_query);
2171         }
2172
2173         EM_DEBUG_FUNC_END("err [%d]", err);
2174         return err;
2175 }
2176
2177 INTERNAL_FUNC int emstorage_create_table(char *multi_user_name, emstorage_create_db_type_t type, int *err_code)
2178 {
2179         EM_DEBUG_FUNC_BEGIN();
2180
2181         int error = EMAIL_ERROR_NONE;
2182         int rc = -1, ret = false;
2183         int query_len = 0;
2184         char sql_query_string[QUERY_SIZE] = {0, };
2185         char **create_table_query = NULL;
2186
2187         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2188
2189         error = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2190         if (error != EMAIL_ERROR_NONE) {
2191                 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
2192                 goto FINISH_OFF2;
2193         }
2194
2195         if (query_len < CREATE_TABLE_MAX) {
2196                 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
2197                 error = EMAIL_ERROR_SYSTEM_FAILURE;
2198                 goto FINISH_OFF2;
2199         }
2200
2201         EM_DEBUG_LOG("local_db_handle = %p.", local_db_handle);
2202
2203         char *sql;
2204         char **result = NULL;
2205
2206         /*  1. create mail_account_tbl */
2207         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_account_tbl';";
2208         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2209         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2210                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2211
2212         EM_DEBUG_LOG("emstorage_create_table - result[1] = %s %s", result[1], result[1]);
2213
2214         if (atoi(result[1]) < 1) {
2215                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2216                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2217                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2218
2219                 EM_DEBUG_LOG("CREATE TABLE mail_account_tbl");
2220                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2221                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2222                 if (error != EMAIL_ERROR_NONE) {
2223                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2224                         goto FINISH_OFF;
2225                 }
2226
2227                 /*  create mail_account_tbl unique index */
2228                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ACCOUNT_IDX]);
2229                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2230                 if (error != EMAIL_ERROR_NONE) {
2231                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2232                         goto FINISH_OFF;
2233                 }
2234
2235                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2236         } /*  mail_account_tbl */
2237         else if (type == EMAIL_CREATE_DB_CHECK) {
2238                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], NULL, NULL, NULL), rc);
2239                 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)));
2240         }
2241
2242         sqlite3_free_table(result);
2243         result = NULL;
2244
2245         /*  2. create mail_box_tbl */
2246         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_box_tbl';";
2247
2248         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2249         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2250                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2251
2252         if (atoi(result[1]) < 1) {
2253                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2254                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2255                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2256
2257                 EM_DEBUG_LOG("CREATE TABLE mail_box_tbl");
2258
2259                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_BOX_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2260                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2261                 if (error != EMAIL_ERROR_NONE) {
2262                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2263                         goto FINISH_OFF;
2264                 }
2265
2266                 /*  create mail_local_mailbox_tbl unique index */
2267                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_BOX_IDX]);
2268                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2269                 if (error != EMAIL_ERROR_NONE) {
2270                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2271                         goto FINISH_OFF;
2272                 }
2273
2274                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2275         } /*  mail_box_tbl */
2276         else if (type == EMAIL_CREATE_DB_CHECK) {
2277                 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_BOX_TBL], NULL, NULL, NULL);
2278                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2279                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_BOX_TBL], rc, sqlite3_errmsg(local_db_handle)));
2280         }
2281         sqlite3_free_table(result);
2282         result = NULL;
2283
2284         /*  3. create mail_read_mail_uid_tbl */
2285         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_read_mail_uid_tbl';";
2286         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2287         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2288                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2289
2290
2291         if (atoi(result[1]) < 1) {
2292                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2293                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2294                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2295
2296                 EM_DEBUG_LOG("CREATE TABLE mail_read_mail_uid_tbl");
2297
2298                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2299                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2300                 if (error != EMAIL_ERROR_NONE) {
2301                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2302                         goto FINISH_OFF;
2303                 }
2304
2305                 /*  create mail_read_mail_uid_tbl unique index */
2306                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_IDX]);
2307                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2308                 if (error != EMAIL_ERROR_NONE) {
2309                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2310                         goto FINISH_OFF;
2311                 }
2312
2313                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2314         } /*  mail_read_mail_uid_tbl */
2315         else if (type == EMAIL_CREATE_DB_CHECK) {
2316                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], NULL, NULL, NULL), rc);
2317                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2318                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], rc, sqlite3_errmsg(local_db_handle)));
2319         }
2320         sqlite3_free_table(result);
2321         result = NULL;
2322
2323         /*  4. create mail_rule_tbl */
2324         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_rule_tbl';";
2325
2326         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2327         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2328                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2329
2330         if (atoi(result[1]) < 1) {
2331                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2332                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2333                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2334
2335                 EM_DEBUG_LOG("CREATE TABLE mail_rule_tbl");
2336
2337                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_RULE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2338                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2339                 if (error != EMAIL_ERROR_NONE) {
2340                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2341                         goto FINISH_OFF;
2342                 }
2343
2344                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2345         } /*  mail_rule_tbl */
2346         else if (type == EMAIL_CREATE_DB_CHECK) {
2347                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_RULE_TBL], NULL, NULL, NULL), rc);
2348                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2349                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_RULE_TBL], rc, sqlite3_errmsg(local_db_handle)));
2350         }
2351         sqlite3_free_table(result);
2352         result = NULL;
2353
2354         /*  5. create mail_tbl */
2355         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_tbl';";
2356         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2357         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2358                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2359
2360         if (atoi(result[1]) < 1) {
2361                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2362                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2363                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2364                 EM_DEBUG_LOG("CREATE TABLE mail_tbl");
2365
2366                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2367                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2368                 if (error != EMAIL_ERROR_NONE) {
2369                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2370                         goto FINISH_OFF;
2371                 }
2372
2373                 /*  create mail_tbl unique index */
2374                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_IDX]);
2375                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2376                 if (error != EMAIL_ERROR_NONE) {
2377                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2378                         goto FINISH_OFF;
2379                 }
2380
2381                 /*  create mail_tbl index for date_time */
2382                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_DATETIME_IDX]);
2383                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2384                 if (error != EMAIL_ERROR_NONE) {
2385                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2386                         goto FINISH_OFF;
2387                 }
2388
2389                 /*  create mail_tbl index for thread_item_count */
2390                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_THREAD_IDX]);
2391                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2392                 if (error != EMAIL_ERROR_NONE) {
2393                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2394                         goto FINISH_OFF;
2395                 }
2396
2397                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2398                 /*  just one time call */
2399                 /*      EFTSInitFTSIndex(FTS_EMAIL_IDX); */
2400         } /* mail_tbl */
2401         else if (type == EMAIL_CREATE_DB_CHECK) {
2402                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TBL], NULL, NULL, NULL), rc);
2403                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2404                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TBL], rc, sqlite3_errmsg(local_db_handle)));
2405         }
2406         sqlite3_free_table(result);
2407         result = NULL;
2408
2409         /*  6. create mail_attachment_tbl */
2410         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_attachment_tbl';";
2411         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2412         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2413                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2414
2415         if (atoi(result[1]) < 1) {
2416                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2417                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2418                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2419
2420                 EM_DEBUG_LOG("CREATE TABLE mail_attachment_tbl");
2421
2422                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2423                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2424                 if (error != EMAIL_ERROR_NONE) {
2425                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2426                         goto FINISH_OFF;
2427                 }
2428
2429                 /*  create mail_attachment_tbl unique index */
2430                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_IDX]);
2431                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2432                 if (error != EMAIL_ERROR_NONE) {
2433                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2434                         goto FINISH_OFF;
2435                 }
2436
2437                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2438         } /*  mail_attachment_tbl */
2439         else if (type == EMAIL_CREATE_DB_CHECK) {
2440                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], NULL, NULL, NULL), rc);
2441                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2442                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], rc, sqlite3_errmsg(local_db_handle)));
2443         }
2444         sqlite3_free_table(result);
2445         result = NULL;
2446
2447 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
2448
2449         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_partial_body_activity_tbl';";
2450         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2451         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2452                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2453
2454         if (atoi(result[1]) < 1) {
2455
2456                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2457                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2458                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2459
2460                 EM_DEBUG_LOG("CREATE TABLE mail_partial_body_activity_tbl");
2461
2462                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2463                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2464                 if (error != EMAIL_ERROR_NONE) {
2465                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2466                         goto FINISH_OFF;
2467                 }
2468
2469                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2470         } /*  mail_rule_tbl */
2471         else if (type == EMAIL_CREATE_DB_CHECK) {
2472                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2473                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2474                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2475         }
2476         sqlite3_free_table(result);
2477         result = NULL;
2478
2479 #endif /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
2480
2481         /*  create mail_meeting_tbl */
2482         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_meeting_tbl';";
2483         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2484         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2485                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2486
2487         if (atoi(result[1]) < 1) {
2488                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2489                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2490                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2491
2492                 EM_DEBUG_LOG("CREATE TABLE mail_meeting_tbl");
2493
2494                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_MEETING_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2495                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2496                 if (error != EMAIL_ERROR_NONE) {
2497                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2498                         goto FINISH_OFF;
2499                 }
2500
2501                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_MEETING_IDX]);
2502                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2503                 if (error != EMAIL_ERROR_NONE) {
2504                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2505                         goto FINISH_OFF;
2506                 }
2507
2508                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2509         } /*  mail_contact_sync_tbl */
2510         else if (type == EMAIL_CREATE_DB_CHECK) {
2511                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], NULL, NULL, NULL), rc);
2512                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2513                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], rc, sqlite3_errmsg(local_db_handle)));
2514         }
2515         sqlite3_free_table(result);
2516         result = NULL;
2517
2518 #ifdef __FEATURE_LOCAL_ACTIVITY__
2519
2520         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_local_activity_tbl';";
2521         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2522         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2523                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2524
2525         if (atoi(result[1]) < 1) {
2526
2527                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2528                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2529                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2530
2531                 EM_DEBUG_LOG(" CREATE TABLE mail_local_activity_tbl");
2532
2533                 SNPRINTF(sql_query_string, sizeof(sql_query_string), create_table_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL]);
2534                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2535                 if (error != EMAIL_ERROR_NONE) {
2536                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2537                         goto FINISH_OFF;
2538                 }
2539
2540                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2541         } /*  mail_rule_tbl */
2542         else if (type == EMAIL_CREATE_DB_CHECK) {
2543                 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL);
2544                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2545                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2546                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2547         }
2548         sqlite3_free_table(result);
2549         result = NULL;
2550 #endif /*  __FEATURE_LOCAL_ACTIVITY__ */
2551         /*  create mail_certificate_tbl */
2552         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_certificate_tbl';";
2553         /*  rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL);   */
2554         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2555         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)));
2556
2557         if (atoi(result[1]) < 1) {
2558                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2559                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2560
2561                 EM_DEBUG_LOG("CREATE TABLE mail_certificate_tbl");
2562
2563                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2564                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2565                 if (error != EMAIL_ERROR_NONE) {
2566                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2567                         goto FINISH_OFF;
2568                 }
2569
2570                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2571         } /*  mail_contact_sync_tbl */
2572         else if (type == EMAIL_CREATE_DB_CHECK) {
2573                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], NULL, NULL, NULL), rc);
2574                 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)));
2575         }
2576
2577         sqlite3_free_table(result);
2578         result = NULL;
2579
2580         /*  create mail_task_tbl */
2581         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_task_tbl';";
2582         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2583         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)));
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; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2588
2589                 EM_DEBUG_LOG("CREATE TABLE mail_task_tbl");
2590
2591                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TASK_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2592                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2593                 if (error != EMAIL_ERROR_NONE) {
2594                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2595                         goto FINISH_OFF;
2596                 }
2597
2598                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_TASK_IDX]);
2599                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2600                 if (error != EMAIL_ERROR_NONE) {
2601                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2602                         goto FINISH_OFF;
2603                 }
2604
2605                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2606         } /*  mail_task_tbl */
2607         else if (type == EMAIL_CREATE_DB_CHECK) {
2608                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TASK_TBL], NULL, NULL, NULL), rc);
2609                 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)));
2610         }
2611
2612         sqlite3_free_table(result);
2613         result = NULL;
2614
2615 #ifdef __FEATURE_BODY_SEARCH__
2616         /*  create mail_text_tbl */
2617         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_text_tbl';";
2618         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2619         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)));
2620
2621         if (atoi(result[1]) < 1) {
2622                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2623                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2624
2625                 EM_DEBUG_LOG("CREATE TABLE mail_text_tbl");
2626
2627                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TEXT_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2628                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2629                 if (error != EMAIL_ERROR_NONE) {
2630                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2631                         goto FINISH_OFF;
2632                 }
2633
2634                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2635         } /*  mail_text_tbl */
2636         else if (type == EMAIL_CREATE_DB_CHECK) {
2637                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TEXT_TBL], NULL, NULL, NULL), rc);
2638                 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)));
2639         }
2640
2641         sqlite3_free_table(result);
2642         result = NULL;
2643
2644 #endif
2645
2646 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
2647         /*  create mail_auto_download_activity_tbl */
2648         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_auto_download_activity_tbl';";
2649         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2650         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)));
2651
2652         if (atoi(result[1]) < 1) {
2653                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2654                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2655
2656                 EM_DEBUG_LOG("CREATE TABLE mail_auto_download_activity_tbl");
2657
2658                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], sizeof(sql_query_string)-1);
2659                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2660                 if (error != EMAIL_ERROR_NONE) {
2661                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2662                         goto FINISH_OFF;
2663                 }
2664
2665                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2666         } /*  mail_auto_download_activity_tbl */
2667         else if (type == EMAIL_CREATE_DB_CHECK) {
2668                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2669                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2670         }
2671
2672         sqlite3_free_table(result);
2673         result = NULL;
2674 #endif
2675
2676
2677         ret = true;
2678
2679 FINISH_OFF:
2680
2681         if (result) sqlite3_free_table(result);
2682
2683         if (ret == true) {
2684                 EMSTORAGE_PROTECTED_FUNC_CALL(
2685                         sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2686         } else {
2687                 EMSTORAGE_PROTECTED_FUNC_CALL(
2688                         sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
2689         }
2690
2691 FINISH_OFF2:
2692         if (create_table_query) {
2693                 int i = 0;
2694                 for (i = 0; i < query_len; i++) {
2695                         if (create_table_query[i])
2696                                 EM_SAFE_FREE(create_table_query[i]);
2697                 }
2698                 EM_SAFE_FREE(create_table_query);
2699         }
2700
2701         if (err_code != NULL)
2702                 *err_code = error;
2703
2704         EM_DEBUG_FUNC_END("ret [%d]", ret);
2705         return ret;
2706 }
2707
2708 /* Query series --------------------------------------------------------------*/
2709 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)
2710 {
2711         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);
2712         int rc = -1;
2713         int query_size = 0;
2714         int error = EMAIL_ERROR_NONE;
2715         DB_STMT hStmt = NULL;
2716         char *sql_query_string = NULL;
2717         char **result;
2718         sqlite3 *local_db_handle = NULL;
2719
2720         if (!input_conditional_clause || (!output_total_mail_count && !output_unseen_mail_count)) {
2721                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2722                 return EMAIL_ERROR_INVALID_PARAM;
2723         }
2724
2725         query_size = EM_SAFE_STRLEN(input_conditional_clause) + QUERY_SIZE;
2726         sql_query_string = em_malloc(query_size);
2727         if (sql_query_string == NULL) {
2728                 EM_DEBUG_EXCEPTION("em_mallocfailed");
2729                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2730                 goto FINISH_OFF;
2731         }
2732
2733         local_db_handle = emstorage_get_db_connection(multi_user_name);
2734
2735         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2736
2737         SNPRINTF(sql_query_string, query_size, "SELECT COUNT(*) FROM mail_tbl");
2738         EM_SAFE_STRNCAT(sql_query_string, (char*)input_conditional_clause, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
2739
2740         if (output_total_mail_count) {
2741                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
2742                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2743                                 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
2744
2745                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2746                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2747                                 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
2748                 _get_stmt_field_data_int(hStmt, output_total_mail_count, 0);
2749         }
2750
2751         if (output_unseen_mail_count) {
2752                 EM_SAFE_STRNCAT(sql_query_string, " AND flags_seen_field = 0 ", query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
2753
2754                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
2755                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2756                                 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2757
2758                 *output_unseen_mail_count = atoi(result[1]);
2759                 sqlite3_free_table(result);
2760         }
2761
2762 FINISH_OFF:
2763
2764         if (hStmt != NULL) {
2765                 rc = sqlite3_finalize(hStmt);
2766                 if (rc != SQLITE_OK) {
2767                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
2768                         error = EMAIL_ERROR_DB_FAILURE;
2769                 }
2770         }
2771
2772         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2773
2774         EM_SAFE_FREE(sql_query_string);
2775
2776         EM_DEBUG_FUNC_END("error [%d]", error);
2777         return error;
2778 }
2779
2780 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)
2781 {
2782         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);
2783
2784         int      i = 0;
2785         int      count = 0;
2786         int      rc = -1;
2787         int      cur_query = 0;
2788         int      col_index;
2789         int      query_size = 0;
2790         int      error = EMAIL_ERROR_NONE;
2791         int     *result_mail_id_list = NULL;
2792         char   **result = NULL;
2793         char     *sql_query_string = NULL;
2794         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2795
2796         EM_IF_NULL_RETURN_VALUE(input_conditional_clause, EMAIL_ERROR_INVALID_PARAM);
2797         EM_IF_NULL_RETURN_VALUE(output_mail_id_list, EMAIL_ERROR_INVALID_PARAM);
2798         EM_IF_NULL_RETURN_VALUE(output_mail_id_count, EMAIL_ERROR_INVALID_PARAM);
2799
2800         query_size = strlen(input_conditional_clause) + strlen("SELECT mail_id FROM mail_tbl ") + 10;  // 10 is extra space
2801         sql_query_string = em_malloc(query_size);
2802         if (sql_query_string == NULL) {
2803                 EM_DEBUG_EXCEPTION("em_mallocfailed");
2804                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2805                 goto FINISH_OFF;
2806         }
2807
2808         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2809
2810         /* Composing query */
2811         SNPRINTF_OFFSET(sql_query_string, cur_query, query_size, "SELECT mail_id FROM mail_tbl ");
2812         EM_SAFE_STRNCAT(sql_query_string, (char*)input_conditional_clause, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
2813
2814         EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
2815
2816         /* Performing query */
2817         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2818         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2819                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2820
2821         col_index = 1;
2822
2823         /*  to get mail list */
2824         if (count == 0) {
2825                 EM_DEBUG_LOG("No mail found...");
2826                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2827                 goto FINISH_OFF;
2828         }
2829
2830         EM_DEBUG_LOG("There are [%d] mails.", count);
2831
2832         if (!(result_mail_id_list = (int*)em_malloc(sizeof(int) * count))) {
2833                 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
2834                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2835                 goto FINISH_OFF;
2836         }
2837
2838         EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2839
2840         for (i = 0; i < count; i++)
2841                 _get_table_field_data_int(result, result_mail_id_list + i, col_index++);
2842
2843         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >> ", count);
2844
2845         *output_mail_id_list  = result_mail_id_list;
2846         *output_mail_id_count = count;
2847
2848 FINISH_OFF:
2849
2850         if (result)
2851                 sqlite3_free_table(result);
2852
2853         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2854
2855         EM_SAFE_FREE(sql_query_string);
2856
2857         if (error != EMAIL_ERROR_NONE)
2858                 EM_SAFE_FREE(result_mail_id_list);
2859
2860         EM_DEBUG_FUNC_END("error [%d]", error);
2861         return error;
2862 }
2863
2864 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)
2865 {
2866         EM_DEBUG_FUNC_BEGIN();
2867         EM_PROFILE_BEGIN(emstorage_query_mail_list_func);
2868
2869         int i = 0, count = 0, rc = -1, to_get_count = (result_mail_list) ? 0 : 1;
2870         int sql_query_string_length = 0;
2871         int local_inline_content_count = 0, local_attachment_count = 0;
2872         int cur_query = 0, base_count = 0, col_index;
2873         int ret = false, error = EMAIL_ERROR_NONE;
2874         char *date_time_string = NULL;
2875         char **result = NULL;
2876         char *field_mail_id = "mail_id";
2877         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 ";
2878         char *select_query_form = "SELECT %s FROM mail_tbl ";
2879         char *target_field = NULL;
2880         char *sql_query_string = NULL;
2881         email_mail_list_item_t *mail_list_item_from_tbl = NULL;
2882         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2883
2884         EM_IF_NULL_RETURN_VALUE(conditional_clause, false);
2885         EM_IF_NULL_RETURN_VALUE(result_count, false);
2886
2887         EMSTORAGE_START_READ_TRANSACTION(transaction);
2888
2889         /*  select clause */
2890         if (to_get_count) /*  count only */
2891                 target_field = field_mail_id;
2892         else /* mail list in plain form */
2893                 target_field = field_all;
2894
2895         sql_query_string_length = EM_SAFE_STRLEN(select_query_form) + EM_SAFE_STRLEN(target_field) + EM_SAFE_STRLEN(conditional_clause);
2896
2897         if (sql_query_string_length)
2898                 sql_query_string = em_malloc(sql_query_string_length);
2899
2900         if (sql_query_string == NULL) {
2901                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
2902                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2903                 goto FINISH_OFF;
2904         }
2905
2906         cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, sql_query_string_length, select_query_form, target_field);
2907
2908         strncat(sql_query_string, conditional_clause, sql_query_string_length - cur_query);
2909
2910         EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
2911
2912         /*  performing query */
2913         EM_PROFILE_BEGIN(emstorage_query_mail_list_performing_query);
2914         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2915         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2916                         ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2917         EM_PROFILE_END(emstorage_query_mail_list_performing_query);
2918
2919         if (!base_count)
2920                 base_count = ({
2921                                 int i = 0;
2922                                 char *tmp = NULL;
2923                                 for (tmp = field_all; tmp && *(tmp + 1); tmp = index(tmp + 1, ',')) i++ ;
2924                                 i;
2925                                 });
2926
2927         col_index = base_count;
2928
2929         EM_DEBUG_LOG_DEV("base_count [%d]", base_count);
2930
2931         if (to_get_count) {
2932                 /*  to get count */
2933                 if (!count) {
2934                         EM_DEBUG_LOG_DEV("No mail found...");
2935                         ret = false;
2936                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
2937                         goto FINISH_OFF;
2938                 }
2939                 EM_DEBUG_LOG_DEV("There are [%d] mails.", count);
2940         } else {
2941                 /*  to get mail list */
2942                 if (!count) {
2943                         EM_DEBUG_LOG_DEV("No mail found...");
2944                         ret = false;
2945                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
2946                         goto FINISH_OFF;
2947                 }
2948
2949                 EM_DEBUG_LOG_DEV("There are [%d] mails.", count);
2950                 if (!(mail_list_item_from_tbl = (email_mail_list_item_t*)em_malloc(sizeof(email_mail_list_item_t) * count))) {
2951                         EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
2952                         error = EMAIL_ERROR_OUT_OF_MEMORY;
2953                         goto FINISH_OFF;
2954                 }
2955
2956                 EM_PROFILE_BEGIN(emstorage_query_mail_list_loop);
2957                 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >> ");
2958                 for (i = 0; i < count; i++) {
2959                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_id), col_index++);
2960                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].account_id), col_index++);
2961                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mailbox_id), col_index++);
2962                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].mailbox_type), col_index++);
2963                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].full_address_from, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2964                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_sender, MAX_EMAIL_ADDRESS_LENGTH, 1, col_index++);
2965                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_recipient, STRING_LENGTH_FOR_DISPLAY,  1, col_index++);
2966                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].subject, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2967                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].body_download_status), col_index++);
2968                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_size), col_index++);
2969                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_seen_field), col_index++);
2970                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_deleted_field), col_index++);
2971                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_flagged_field), col_index++);
2972                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_answered_field), col_index++);
2973                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_recent_field), col_index++);
2974                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_draft_field), col_index++);
2975                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_forwarded_field), col_index++);
2976                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].DRM_status), col_index++);
2977                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].priority), col_index++);
2978                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].save_status), col_index++);
2979                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].lock_status), col_index++);
2980                         _get_table_field_data_int(result, &local_attachment_count, col_index++);
2981                         _get_table_field_data_int(result, &local_inline_content_count, col_index++);
2982                         _get_table_field_data_time_t(result, &(mail_list_item_from_tbl[i].date_time), col_index++);
2983                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].preview_text, MAX_PREVIEW_TEXT_LENGTH, 1, col_index++);
2984                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_id), col_index++);
2985                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_item_count), col_index++);
2986                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].meeting_request_status), col_index++);
2987                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].message_class), col_index++);
2988                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].smime_type), col_index++);
2989                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].scheduled_sending_time), col_index++);
2990                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].remaining_resend_times), col_index++);
2991                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].tag_id), col_index++);
2992                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].eas_data_length), col_index++);
2993                         _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++);
2994
2995                         mail_list_item_from_tbl[i].attachment_count = (local_attachment_count > 0) ? 1 : 0;
2996                 }
2997                 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >> ", count);
2998                 EM_PROFILE_END(emstorage_query_mail_list_loop);
2999         }
3000
3001         ret = true;
3002
3003 FINISH_OFF:
3004         EM_DEBUG_LOG("MAIL_COUNT [%d]", count);
3005
3006         if (result)
3007                 sqlite3_free_table(result);
3008
3009         if (to_get_count)
3010                 *result_count = count;
3011         else {
3012                 if (ret == true) {
3013                         if (result_mail_list)
3014                                 *result_mail_list = mail_list_item_from_tbl;
3015                         else
3016                                 EM_SAFE_FREE(mail_list_item_from_tbl);
3017                         *result_count = count;
3018                 } else
3019                         EM_SAFE_FREE(mail_list_item_from_tbl);
3020         }
3021
3022         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3023
3024         //      sqlite3_db_release_memory(local_db_handle);
3025
3026         EM_SAFE_FREE(sql_query_string);
3027         EM_SAFE_FREE(date_time_string);
3028
3029         if (err_code != NULL)
3030                 *err_code = error;
3031
3032         EM_PROFILE_END(emstorage_query_mail_list_func);
3033         EM_DEBUG_FUNC_END("ret [%d]", ret);
3034         return ret;
3035 }
3036
3037
3038 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)
3039 {
3040         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);
3041
3042         if (!conditional_clause || !result_mail_tbl || !result_count) {
3043                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: [%p] [%p] [%p]", conditional_clause, result_mail_tbl, result_mail_tbl);
3044                 if (err_code != NULL)
3045                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3046                 return false;
3047         }
3048
3049         int i, col_index = FIELD_COUNT_OF_MAIL_TBL, rc, ret = false, count;
3050         int error = EMAIL_ERROR_NONE;
3051         char **result = NULL, sql_query_string[QUERY_SIZE] = {0, };
3052         emstorage_mail_tbl_t* p_data_tbl = NULL;
3053         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3054
3055         EMSTORAGE_START_READ_TRANSACTION(transaction);
3056
3057         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl %s", conditional_clause);
3058
3059         EM_DEBUG_LOG_DEV("Query[%s]", sql_query_string);
3060
3061         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3062         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3063                         ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
3064
3065         if (!count) {
3066                 EM_DEBUG_LOG("No mail found...");
3067                 ret = false;
3068                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
3069                 goto FINISH_OFF;
3070         }
3071
3072         EM_DEBUG_LOG("There are [%d] mails.", count);
3073         if (!(p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * count))) {
3074                 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
3075                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3076                 goto FINISH_OFF;
3077         }
3078
3079         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >> ");
3080         for (i = 0; i < count; i++) {
3081                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
3082                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3083                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3084                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_type), col_index++);
3085                 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 1, col_index++);
3086                 _get_table_field_data_time_t (result, &(p_data_tbl[i].date_time), col_index++);
3087                 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
3088                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
3089                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
3090                 _get_table_field_data_string(result, &(p_data_tbl[i].message_id), 0, col_index++);
3091                 _get_table_field_data_int(result, &(p_data_tbl[i].reference_mail_id), col_index++);
3092                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_from), 1, col_index++);
3093                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_reply), 1, col_index++);
3094                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_to), 1, col_index++);
3095                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_cc), 1, col_index++);
3096                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_bcc), 1, col_index++);
3097                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_return), 1, col_index++);
3098                 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_sender), 1, col_index++);
3099                 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_recipient), 1, col_index++);
3100                 _get_table_field_data_string(result, &(p_data_tbl[i].alias_sender), 1, col_index++);
3101                 _get_table_field_data_string(result, &(p_data_tbl[i].alias_recipient), 1, col_index++);
3102                 _get_table_field_data_int(result, &(p_data_tbl[i].body_download_status), col_index++);
3103                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
3104                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
3105                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
3106                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_size), col_index++);
3107                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_seen_field), col_index++);
3108                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_deleted_field), col_index++);
3109                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_flagged_field), col_index++);
3110                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_answered_field), col_index++);
3111                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_recent_field), col_index++);
3112                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_draft_field), col_index++);
3113                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_forwarded_field), col_index++);
3114                 _get_table_field_data_int(result, &(p_data_tbl[i].DRM_status), col_index++);
3115                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].priority), col_index++);
3116                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
3117                 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
3118                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].report_status), col_index++);
3119                 _get_table_field_data_int(result, &(p_data_tbl[i].attachment_count), col_index++);
3120                 _get_table_field_data_int(result, &(p_data_tbl[i].inline_content_count), col_index++);
3121                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
3122                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
3123                 _get_table_field_data_string(result, &(p_data_tbl[i].preview_text), 1, col_index++);
3124                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].meeting_request_status), col_index++);
3125                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].message_class), col_index++);
3126                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].digest_type), col_index++);
3127                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].smime_type), col_index++);
3128                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].scheduled_sending_time), col_index++);
3129                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].remaining_resend_times), col_index++);
3130                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].tag_id), col_index++);
3131                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].replied_time), col_index++);
3132                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].forwarded_time), col_index++);
3133                 _get_table_field_data_string(result, &(p_data_tbl[i].default_charset), 0, col_index++);
3134                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3135                 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3136                 _get_table_field_data_string(result, &(p_data_tbl[i].user_name), 0, col_index++);
3137         }
3138
3139         ret = true;
3140
3141 FINISH_OFF:
3142         if (result)
3143                 sqlite3_free_table(result);
3144
3145         if (ret) {
3146                 *result_mail_tbl = p_data_tbl;
3147                 *result_count = count;
3148         } else {
3149                 *result_mail_tbl = NULL;
3150                 *result_count = 0;
3151         }
3152
3153         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3154
3155         //      sqlite3_db_release_memory(local_db_handle);
3156
3157         if (err_code != NULL)
3158                 *err_code = error;
3159
3160         EM_DEBUG_FUNC_END("ret [%d]", ret);
3161         return ret;
3162 }
3163
3164 #ifdef __FEATURE_BODY_SEARCH__
3165 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)
3166 {
3167         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);
3168
3169         if (!conditional_clause || !result_mail_text_tbl || !result_count) { /*prevent 50930*/
3170                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3171                 if (err_code != NULL)
3172                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3173                 return false;
3174         }
3175
3176         int i = 0;
3177         int rc = 0;
3178         int ret = false;
3179         int count = 0;
3180         int col_index = FIELD_COUNT_OF_MAIL_TEXT_TBL;
3181         int error = EMAIL_ERROR_NONE;
3182         char **result = NULL;
3183         char sql_query_string[QUERY_SIZE] = {0, };
3184         emstorage_mail_text_tbl_t* p_data_tbl = NULL;
3185         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3186
3187         EMSTORAGE_START_READ_TRANSACTION(transaction);
3188
3189         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_text_tbl %s", conditional_clause);
3190
3191         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
3192
3193         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3194         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3195                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3196
3197         if (!count) {
3198                 EM_DEBUG_LOG("No mail found...");
3199                 ret = false;
3200                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
3201                 goto FINISH_OFF;
3202         }
3203
3204         EM_DEBUG_LOG("There are [%d] mails.", count);
3205         if (!(p_data_tbl = (emstorage_mail_text_tbl_t *)em_malloc(sizeof(emstorage_mail_text_tbl_t) * count))) {
3206                 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_text_tbl_t failed...");
3207                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3208                 goto FINISH_OFF;
3209         }
3210
3211         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
3212         for (i = 0; i < count; i++) {
3213                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
3214                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3215                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3216                 _get_table_field_data_string(result, &(p_data_tbl[i].body_text), 0, col_index++);
3217         }
3218
3219         ret = true;
3220
3221 FINISH_OFF:
3222         if (result)
3223                 sqlite3_free_table(result);
3224
3225         if (ret == true) {
3226                 *result_mail_text_tbl = p_data_tbl;
3227                 *result_count = count;
3228         } else
3229                 EM_SAFE_FREE(p_data_tbl);
3230
3231         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3232
3233         //      sqlite3_db_release_memory(local_db_handle);
3234
3235         if (err_code != NULL)
3236                 *err_code = error;
3237
3238         EM_DEBUG_FUNC_END("ret [%d]", ret);
3239         return ret;
3240 }
3241 #endif
3242
3243 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)
3244 {
3245         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);
3246
3247         int i = 0;
3248         int rc;
3249         int count = 0;
3250         int col_index = 0;
3251         int error = EMAIL_ERROR_NONE;
3252         int read_count = 0;
3253         int total_count = 0;
3254         char **result;
3255         char sql_query_string[QUERY_SIZE] = {0, };
3256         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 ";
3257         emstorage_mailbox_tbl_t* p_data_tbl = NULL;
3258         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3259
3260         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
3261
3262         if (input_get_mail_count == 0) {        /* without mail count */
3263                 col_index = 15;
3264                 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);
3265         } else {        /* with read count and total count */
3266                 col_index = 17;
3267                 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);
3268         }
3269
3270         EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
3271
3272         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
3273         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3274                         ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string))
3275
3276                 EM_DEBUG_LOG_DEV("result count [%d]", count);
3277
3278         if (count == 0) {
3279                 EM_DEBUG_LOG_SEC("Can't find mailbox query[%s]", sql_query_string);
3280                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
3281                 goto FINISH_OFF;
3282         }
3283
3284         if ((p_data_tbl = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t) * count)) == NULL) {
3285                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
3286                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3287                 goto FINISH_OFF;
3288         }
3289
3290         for (i = 0; i < count; i++) {
3291                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3292                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3293                 _get_table_field_data_int(result, &(p_data_tbl[i].local_yn), col_index++);
3294                 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
3295                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].mailbox_type), col_index++);
3296                 _get_table_field_data_string(result, &(p_data_tbl[i].alias), 0, col_index++);
3297                 _get_table_field_data_int(result, &(p_data_tbl[i].deleted_flag), col_index++);
3298                 _get_table_field_data_int(result, &(p_data_tbl[i].modifiable_yn), col_index++);
3299                 _get_table_field_data_int(result, &(p_data_tbl[i].total_mail_count_on_server), col_index++);
3300                 _get_table_field_data_int(result, &(p_data_tbl[i].has_archived_mails), col_index++);
3301                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_slot_size), col_index++);
3302                 _get_table_field_data_int(result, &(p_data_tbl[i].no_select), col_index++);
3303                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].last_sync_time), col_index++);
3304                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3305                 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3306
3307                 if (input_get_mail_count == 1) {
3308                         _get_table_field_data_int(result, &(total_count), col_index++);
3309                         p_data_tbl[i].total_mail_count_on_local = total_count;
3310                         _get_table_field_data_int(result, &(read_count), col_index++);
3311                         p_data_tbl[i].unread_count = total_count - read_count;                  /*  return unread count, NOT  */
3312                 }
3313         }
3314
3315
3316 FINISH_OFF:
3317         if (result)
3318                 sqlite3_free_table(result);
3319
3320         if (error == EMAIL_ERROR_NONE) {
3321                 *output_mailbox_list = p_data_tbl;
3322                 *output_mailbox_count = count;
3323                 EM_DEBUG_LOG("Mailbox Count [%d]", count);
3324         }
3325
3326         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
3327
3328         //      sqlite3_db_release_memory(local_db_handle);
3329
3330         EM_DEBUG_FUNC_END("error [%d]", error);
3331         return error;
3332 }
3333
3334 /* Query series --------------------------------------------------------------*/
3335 INTERNAL_FUNC int emstorage_check_duplicated_account(char *multi_user_name, email_account_t* account, int transaction, int *err_code)
3336 {
3337         EM_DEBUG_FUNC_BEGIN();
3338         int rc = -1, ret = false;
3339         int error = EMAIL_ERROR_NONE;
3340         char **result;
3341         int count;
3342         char sql_query_string[QUERY_SIZE] = {0, };
3343         sqlite3 *local_db_handle = NULL;
3344
3345         if (account == NULL || account->user_email_address == NULL || account->incoming_server_user_name == NULL || account->incoming_server_address == NULL ||
3346                         account->outgoing_server_user_name == NULL || account->outgoing_server_address == NULL) {
3347                 if (err_code != NULL)
3348                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3349                 return false;
3350         }
3351
3352         local_db_handle = emstorage_get_db_connection(multi_user_name);
3353
3354         EMSTORAGE_START_READ_TRANSACTION(transaction);
3355
3356         SNPRINTF(sql_query_string, sizeof(sql_query_string),
3357                         "SELECT COUNT(*) FROM mail_account_tbl "
3358                         " WHERE "
3359                         " user_email_address = '%s' AND "
3360                         " incoming_server_user_name = '%s' AND "
3361                         " incoming_server_type = %d AND "
3362                         " incoming_server_address = '%s' AND "
3363                         " outgoing_server_user_name = '%s' AND "
3364                         " outgoing_server_type = %d AND "
3365                         " outgoing_server_address = '%s'; ",
3366                         account->user_email_address,
3367                         account->incoming_server_user_name, account->incoming_server_type, account->incoming_server_address,
3368                         account->outgoing_server_user_name, account->outgoing_server_type, account->outgoing_server_address
3369                         );
3370         EM_DEBUG_LOG_SEC("Query[%s]", sql_query_string);
3371         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
3372         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3373                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3374
3375         count = atoi(result[1]);
3376         sqlite3_free_table(result);
3377
3378         EM_DEBUG_LOG("Count of Duplicated Account Information: [%d]", count);
3379
3380         if (count == 0) {       /*  not duplicated account */
3381                 ret = true;
3382                 EM_DEBUG_LOG_SEC("NOT duplicated account: user_email_address[%s]", account->user_email_address);
3383         } else {        /*  duplicated account */
3384                 ret = false;
3385                 EM_DEBUG_LOG_SEC("The same account already exists. Duplicated account: user_email_address[%s]", account->user_email_address);
3386                 error = EMAIL_ERROR_ALREADY_EXISTS;
3387         }
3388
3389 FINISH_OFF:
3390
3391         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3392
3393         if (err_code != NULL)
3394                 *err_code = error;
3395
3396         EM_DEBUG_FUNC_END("ret [%d]", ret);
3397         return ret;
3398
3399 }
3400
3401 INTERNAL_FUNC int emstorage_get_account_count(char *multi_user_name, int *count, int transaction, int *err_code)
3402 {
3403         EM_DEBUG_FUNC_BEGIN();
3404
3405         if (!count) {
3406                 if (err_code != NULL)
3407                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3408                 return false;
3409         }
3410
3411         int rc = -1, ret = false;
3412         int error = EMAIL_ERROR_NONE;
3413
3414         DB_STMT hStmt = NULL;
3415         char sql_query_string[QUERY_SIZE] = {0, };
3416
3417
3418         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3419         EMSTORAGE_START_READ_TRANSACTION(transaction);
3420         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_account_tbl");
3421         EM_DEBUG_LOG_SEC("SQL STMT [%s]", sql_query_string);
3422         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string,
3423                                 EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3424         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
3425         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3426                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3427
3428         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3429         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3430                         ("sqlite3_step fail:%d", rc));
3431
3432         *count = sqlite3_column_int(hStmt, 0);
3433
3434         ret = true;
3435
3436 FINISH_OFF:
3437
3438         if (hStmt != NULL) {
3439                 rc = sqlite3_finalize(hStmt);
3440                 hStmt = NULL;
3441                 if (rc != SQLITE_OK) {
3442                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3443                         error = EMAIL_ERROR_DB_FAILURE;
3444                 }
3445         }
3446
3447         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3448
3449         if (err_code != NULL)
3450                 *err_code = error;
3451
3452         EM_DEBUG_FUNC_END("ret [%d]", ret);
3453         return ret;
3454 }
3455
3456 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)
3457 {
3458         EM_DEBUG_FUNC_BEGIN();
3459
3460         int i = 0, count = 0, rc = -1, ret = false;
3461         int field_index = 0;
3462         int sql_len = 0;
3463         int error = EMAIL_ERROR_NONE;
3464         emstorage_account_tbl_t *p_data_tbl = NULL;
3465         char sql_query_string[QUERY_SIZE] = {0, };
3466         char *sql = "SELECT count(*) FROM mail_account_tbl;";
3467         char **result;
3468         sqlite3 *local_db_handle = NULL;
3469         DB_STMT hStmt = NULL;
3470
3471         if (!select_num || !account_list) {
3472                 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, account_list);
3473                 if (err_code != NULL)
3474                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3475                 return false;
3476         }
3477
3478         local_db_handle = emstorage_get_db_connection(multi_user_name);
3479
3480         EMSTORAGE_START_READ_TRANSACTION(transaction);
3481
3482         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
3483         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3484                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
3485
3486         count = atoi(result[1]);
3487         sqlite3_free_table(result);
3488
3489         EM_DEBUG_LOG_DEV("count = %d", rc);
3490
3491         if (count <= 0) {
3492                 EM_DEBUG_LOG("no account found...");
3493                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3494                 ret = true;
3495                 goto FINISH_OFF;
3496         }
3497         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
3498         sql_len = EM_SAFE_STRLEN(sql_query_string);
3499
3500         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
3501                         "account_id, "
3502                         "account_name, "
3503                         "logo_icon_path, "
3504                         "user_data, "
3505                         "user_data_length, "
3506                         "account_svc_id, "
3507                         "sync_status, "
3508                         "sync_disabled, "
3509                         "default_mail_slot_size, "
3510                         "roaming_option, "
3511                         "color_label, "
3512                         "user_display_name, "
3513                         "user_email_address, "
3514                         "reply_to_address, "
3515                         "return_address, "
3516                         "incoming_server_type, "
3517                         "incoming_server_address, "
3518                         "incoming_server_port_number, "
3519                         "incoming_server_user_name, "
3520                         "incoming_server_password, "
3521                         "incoming_server_secure_connection, "
3522                         "incoming_server_authentication_method, "
3523                         "retrieval_mode, "
3524                         "keep_mails_on_pop_server_after_download, "
3525                         "check_interval, "
3526                         "auto_download_size, "
3527                         "peak_interval, "
3528                         "peak_days, "
3529                         "peak_start_time, "
3530                         "peak_end_time, "
3531                         "outgoing_server_type, "
3532                         "outgoing_server_address, "
3533                         "outgoing_server_port_number, "
3534                         "outgoing_server_user_name, "
3535                         "outgoing_server_password, "
3536                         "outgoing_server_secure_connection, "
3537                         "outgoing_server_need_authentication, "
3538                         "outgoing_server_use_same_authenticator, "
3539                         "priority, "
3540                         "keep_local_copy, "
3541                         "req_delivery_receipt, "
3542                         "req_read_receipt, "
3543                         "download_limit, "
3544                         "block_address, "
3545                         "block_subject, "
3546                         "display_name_from, "
3547                         "reply_with_body, "
3548                         "forward_with_files, "
3549                         "add_myname_card, "
3550                         "add_signature, "
3551                         "signature, "
3552                         "add_my_address_to_bcc, "
3553                         "notification_status, "
3554                         "vibrate_status, "
3555                         "display_content_status, "
3556                         "default_ringtone_status, "
3557                         "alert_ringtone_path, "
3558                         "auto_resend_times, "
3559                         "outgoing_server_size_limit, "
3560                         "wifi_auto_download, "
3561                         "pop_before_smtp, "
3562                         "incoming_server_requires_apop, "
3563                         "smime_type, "
3564                         "certificate_path, "
3565                         "cipher_type, "
3566                         "digest_type"
3567                         );
3568
3569         sql_len = EM_SAFE_STRLEN(sql_query_string);
3570
3571         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " FROM mail_account_tbl ORDER BY account_id");
3572
3573         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3574
3575         EM_DEBUG_LOG_DEV("After sqlite3_prepare_v2 hStmt = %p", hStmt);
3576         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3577                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3578
3579         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3580         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3581                         ("sqlite3_step fail:%d", rc));
3582         if (rc == SQLITE_DONE) {
3583                 EM_DEBUG_EXCEPTION("no account found...");
3584
3585                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3586                 count = 0;
3587                 ret = true;
3588                 goto FINISH_OFF;
3589         }
3590
3591         if (!(p_data_tbl = (emstorage_account_tbl_t*)malloc(sizeof(emstorage_account_tbl_t) * count))) {
3592                 EM_DEBUG_EXCEPTION("malloc failed...");
3593                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3594                 goto FINISH_OFF;
3595         }
3596         memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * count);
3597         for (i = 0; i < count; i++) {
3598                 /*  get recordset */
3599                 field_index = 0;
3600                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_id), field_index++);
3601                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].account_name), 0, field_index++);
3602                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].logo_icon_path), 0, field_index++);
3603                 _get_stmt_field_data_blob(hStmt, &(p_data_tbl[i].user_data), field_index++);
3604                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].user_data_length), field_index++);
3605                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_svc_id), field_index++);
3606                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_status), field_index++);
3607                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_disabled), field_index++);
3608                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].default_mail_slot_size), field_index++);
3609                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].roaming_option), field_index++);
3610                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].color_label), field_index++);
3611                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_display_name), 0, field_index++);
3612                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_email_address), 0, field_index++);
3613                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].reply_to_address), 0, field_index++);
3614                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].return_address), 0, field_index++);
3615                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].incoming_server_type), field_index++);
3616                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_address), 0, field_index++);
3617                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_port_number), field_index++);
3618                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_user_name), 0, field_index++);
3619                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_password), 0, field_index++);
3620                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_secure_connection), field_index++);
3621                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].incoming_server_authentication_method), field_index++);
3622                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].retrieval_mode), field_index++);
3623                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].keep_mails_on_pop_server_after_download), field_index++);
3624                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].check_interval), field_index++);
3625                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_download_size), field_index++);
3626                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_interval), field_index++);
3627                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_days), field_index++);
3628                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_start_time), field_index++);
3629                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_end_time), field_index++);
3630                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].outgoing_server_type), field_index++);
3631                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_address), 0, field_index++);
3632                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_port_number), field_index++);
3633                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_user_name), 0, field_index++);
3634                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_password), 0, field_index++);
3635                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_secure_connection), field_index++);
3636                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_need_authentication), field_index++);
3637                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_use_same_authenticator), field_index++);
3638                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.priority), field_index++);
3639                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.keep_local_copy), field_index++);
3640                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_delivery_receipt), field_index++);
3641                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_read_receipt), field_index++);
3642                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.download_limit), field_index++);
3643                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_address), field_index++);
3644                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_subject), field_index++);
3645                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.display_name_from), 0, field_index++);
3646                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.reply_with_body), field_index++);
3647                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.forward_with_files), field_index++);
3648                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_myname_card), field_index++);
3649                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_signature), field_index++);
3650                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.signature), 0, field_index++);
3651                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.add_my_address_to_bcc), field_index++);
3652                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.notification_status), field_index++);
3653                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.vibrate_status), field_index++);
3654                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.display_content_status), field_index++);
3655                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.default_ringtone_status), field_index++);
3656                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.alert_ringtone_path), 0, field_index++);
3657                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_resend_times), field_index++);
3658                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_size_limit), field_index++);
3659                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].wifi_auto_download), field_index++);
3660                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].pop_before_smtp), field_index++);
3661                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_requires_apop), field_index++);
3662                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].smime_type), field_index++);
3663                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].certificate_path), 0, field_index++);
3664                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].cipher_type), field_index++);
3665                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].digest_type), field_index++);
3666
3667                 /* EAS passwd is not accessible */
3668                 if (with_password == true && p_data_tbl[i].incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
3669                         /*  get password from the secure storage */
3670                         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3671                         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3672
3673                         EM_SAFE_FREE(p_data_tbl[i].incoming_server_password);
3674                         EM_SAFE_FREE(p_data_tbl[i].outgoing_server_password);
3675
3676                         /*  get password file name */
3677                         error = _get_password_file_name(multi_user_name, p_data_tbl[i].account_id,
3678                                         recv_password_file_name,
3679                                         send_password_file_name);
3680                         if (error != EMAIL_ERROR_NONE) {
3681                                 EM_DEBUG_EXCEPTION("_get_password_file_name error [%d]", error);
3682                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3683                                 goto FINISH_OFF;
3684                         }
3685
3686                         /*  read password from secure storage */
3687                         error = _read_password_from_secure_storage(recv_password_file_name,
3688                                         &(p_data_tbl[i].incoming_server_password));
3689                         if (error < 0) {
3690                                 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s] error [%d]",
3691                                                 recv_password_file_name, error);
3692                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3693                                 goto FINISH_OFF;
3694                         }
3695                         error = _read_password_from_secure_storage(send_password_file_name,
3696                                         &(p_data_tbl[i].outgoing_server_password));
3697                         if (error < 0) {
3698                                 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s]  error [%d]",
3699                                                 send_password_file_name, error);
3700                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3701                                 goto FINISH_OFF;
3702                         }
3703                 }
3704
3705                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3706                 EM_DEBUG_LOG_DEV("after sqlite3_step(), i = %d, rc = %d.", i,  rc);
3707                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3708                                 ("sqlite3_step fail:%d", rc));
3709         }
3710
3711         ret = true;
3712
3713 FINISH_OFF:
3714         if (ret == true) {
3715                 *account_list = p_data_tbl;
3716                 *select_num = count;
3717                 EM_DEBUG_LOG_DEV("COUNT : %d", count);
3718         } else if (p_data_tbl != NULL)
3719                 emstorage_free_account(&p_data_tbl, count, NULL);
3720
3721         if (hStmt != NULL) {
3722                 rc = sqlite3_finalize(hStmt);
3723                 hStmt = NULL;
3724                 if (rc != SQLITE_OK) {
3725                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3726                         error = EMAIL_ERROR_DB_FAILURE;
3727                 }
3728         }
3729
3730         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3731
3732         if (err_code != NULL)
3733                 *err_code = error;
3734
3735         EM_DEBUG_FUNC_END("ret [%d]", ret);
3736         return ret;
3737 }
3738
3739 INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(char *multi_user_name,
3740                 char *server_mail_id,
3741                 int mailbox_id,
3742                 emstorage_mail_tbl_t **mail,
3743                 int transaction,
3744                 int *err_code)
3745 {
3746         EM_DEBUG_FUNC_BEGIN_SEC("mailbox_id [%d], server_mail_id[%s], mail[%p], transaction[%d], err_code[%p]",
3747                         mailbox_id, server_mail_id, mail, transaction, err_code);
3748
3749         int ret = false, error = EMAIL_ERROR_NONE, result_count;
3750         char conditional_clause[QUERY_SIZE] = {0, };
3751         emstorage_mail_tbl_t* p_data_tbl = NULL;
3752
3753         if (!server_mail_id || !mail) {
3754                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3755                 error = EMAIL_ERROR_INVALID_PARAM;
3756                 goto FINISH_OFF;
3757         }
3758
3759         sqlite3_snprintf(sizeof(conditional_clause), conditional_clause, "WHERE server_mail_id = '%q'", server_mail_id);
3760
3761         if (mailbox_id > 0) {
3762                 SNPRINTF(conditional_clause + strlen(conditional_clause),
3763                                 QUERY_SIZE - strlen(conditional_clause),
3764                                 " AND mailbox_id = %d",
3765                                 mailbox_id);
3766         }
3767
3768         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
3769         if (!emstorage_query_mail_tbl(multi_user_name,
3770                                 conditional_clause,
3771                                 transaction,
3772                                 &p_data_tbl,
3773                                 &result_count,
3774                                 &error)) {
3775                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3776                 goto FINISH_OFF;
3777         }
3778
3779         ret = true;
3780
3781 FINISH_OFF:
3782
3783         if (ret == true)
3784                 *mail = p_data_tbl;
3785
3786         if (err_code != NULL)
3787                 *err_code = error;
3788
3789         EM_DEBUG_FUNC_END("ret [%d]", ret);
3790         return ret;
3791 }
3792
3793
3794 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)
3795 {
3796         int cur_clause = 0, conditional_clause_count = 0, i = 0;
3797
3798         if (account_id < ALL_ACCOUNT) {
3799                 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3800                 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3801         }
3802
3803         /*  where clause */
3804         if (account_id != ALL_ACCOUNT) {
3805                 cur_clause += (conditional_clause_count++ == 0) ?
3806                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE account_id = %d", account_id) :
3807                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND account_id = %d", account_id);
3808         }
3809
3810         if (mailbox_id > 0) {
3811                 cur_clause += (conditional_clause_count++ == 0) ?
3812                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_id = %d", mailbox_id) :
3813                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_id = %d", mailbox_id);
3814         }
3815
3816         if (thread_id > 0) {
3817                 cur_clause += (conditional_clause_count++ == 0) ?
3818                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_id = %d ", thread_id) :
3819                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_id = %d ", thread_id);
3820         } else if (thread_id == EMAIL_LIST_TYPE_THREAD) {
3821                 cur_clause += (conditional_clause_count++ == 0) ?
3822                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_item_count > 0") :
3823                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_item_count > 0");
3824         } else if (thread_id == EMAIL_LIST_TYPE_LOCAL) {
3825                 cur_clause += (conditional_clause_count++ == 0) ?
3826                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE server_mail_status = 0") :
3827                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND server_mail_status = 0");
3828         } else if (thread_id == EMAIL_LIST_TYPE_UNREAD) {
3829                 cur_clause += (conditional_clause_count++ == 0) ?
3830                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_seen_field == 0") :
3831                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_seen_field == 0");
3832         }
3833
3834         /*  EM_DEBUG_LOG("where clause added [%s]", conditional_clause_string); */
3835         if (addr_list && addr_list->address_count > 0) {
3836                 if (!addr_list->address_type) {
3837                         cur_clause += (conditional_clause_count++ == 0) ?
3838                                 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE email_address_sender IN(\"%s\"", addr_list->address_list[0]) :
3839                                 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND email_address_sender IN(\"%s\"", addr_list->address_list[0]);
3840
3841                         for (i = 1; i < addr_list->address_count; i++)
3842                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3843
3844                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3845                 } else {
3846                         cur_clause += (conditional_clause_count++ == 0) ?
3847                                 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]) :
3848                                 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]);
3849
3850                         for (i = 1; i < addr_list->address_count; i++)
3851                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3852
3853                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3854                 }
3855         }
3856
3857         if (input_except_delete_flagged_mails) {
3858                 cur_clause += (conditional_clause_count++ == 0) ?
3859                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_deleted_field = 0") :
3860                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_deleted_field = 0");
3861         }
3862
3863         if (search_value) {
3864                 switch (search_type) {
3865                 case EMAIL_SEARCH_FILTER_SUBJECT:
3866                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3867                                         " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3868                         break;
3869                 case EMAIL_SEARCH_FILTER_SENDER:
3870                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3871                                         " %s  ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3872                                         ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3873                         break;
3874                 case EMAIL_SEARCH_FILTER_RECIPIENT:
3875                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3876                                         " %s ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3877                                         "       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3878                                         "       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3879                                         ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value);
3880                         break;
3881                 case EMAIL_SEARCH_FILTER_ALL:
3882                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3883                                         " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\' "
3884                                         "       OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3885                                         "                       OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3886                                         "                       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3887                                         "                       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3888                                         "               ) "
3889                                         "       )"
3890                                         ")", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value, search_value, search_value);
3891                         break;
3892                 }
3893         }
3894
3895         /*  EM_DEBUG_LOG("where clause [%s]", conditional_clause_string); */
3896         static char sorting_str[][50] = {
3897                 " ORDER BY date_time DESC",                                              /* case EMAIL_SORT_DATETIME_HIGH: */
3898                 " ORDER BY date_time ASC",                                                /* case EMAIL_SORT_DATETIME_LOW: */
3899                 " ORDER BY full_address_from DESC, date_time DESC", /* case EMAIL_SORT_SENDER_HIGH: */
3900                 " ORDER BY full_address_from ASC, date_time DESC",  /* case EMAIL_SORT_SENDER_LOW: */
3901                 " ORDER BY full_address_to DESC, date_time DESC",   /* case EMAIL_SORT_RCPT_HIGH: */
3902                 " ORDER BY full_address_to ASC, date_time DESC",        /* case EMAIL_SORT_RCPT_LOW: */
3903                 " ORDER BY subject DESC, date_time DESC",                  /* case EMAIL_SORT_SUBJECT_HIGH: */
3904                 " ORDER BY subject ASC, date_time DESC",                        /* case EMAIL_SORT_SUBJECT_LOW: */
3905                 " ORDER BY priority DESC, date_time DESC",                /* case EMAIL_SORT_PRIORITY_HIGH: */
3906                 " ORDER BY priority ASC, date_time DESC",                  /* case EMAIL_SORT_PRIORITY_LOW: */
3907                 " ORDER BY attachment_count DESC, date_time DESC",  /* case EMAIL_SORT_ATTACHMENT_HIGH: */
3908                 " ORDER BY attachment_count ASC, date_time DESC",   /* case EMAIL_SORT_ATTACHMENT_LOW: */
3909                 " ORDER BY lock_status DESC, date_time DESC",      /* case EMAIL_SORT_FAVORITE_HIGH: */
3910                 " ORDER BY lock_status ASC, date_time DESC",            /* case EMAIL_SORT_FAVORITE_LOW: */
3911                 " ORDER BY mailbox_id DESC, date_time DESC",      /* case EMAIL_SORT_MAILBOX_ID_HIGH: */
3912                 " ORDER BY mailbox_id ASC, date_time DESC",             /* case EMAIL_SORT_MAILBOX_ID_LOW: */
3913                 " ORDER BY flags_flagged_field DESC, date_time DESC",  /* case EMAIL_SORT_FLAGGED_FLAG_HIGH: */
3914                 " ORDER BY flags_flagged_field ASC, date_time DESC",   /* case EMAIL_SORT_FLAGGED_FLAG_LOW: */
3915                 " ORDER BY flags_seen_field DESC, date_time DESC",     /* case EMAIL_SORT_SEEN_FLAG_HIGH: */
3916                 " ORDER BY flags_seen_field ASC, date_time DESC"       /* case EMAIL_SORT_SEEN_FLAG_LOW: */
3917         };
3918
3919         if (sorting < EMAIL_SORT_END)
3920                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " %s", sorting_str[sorting]);
3921         else
3922                 EM_DEBUG_LOG(" Invalid Sorting order ");
3923
3924         /*  limit clause */
3925         if (start_index != -1 && limit_count != -1)
3926                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " LIMIT %d, %d", start_index, limit_count);
3927
3928         return true;
3929 }
3930
3931
3932 /**
3933  *      emstorage_get_mail_list - Get the mail list information.
3934  *
3935  *
3936  */
3937 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)
3938 {
3939         EM_DEBUG_FUNC_BEGIN();
3940         EM_PROFILE_BEGIN(emstorage_get_mail_list_func);
3941
3942         int ret = false, error = EMAIL_ERROR_NONE;
3943         char conditional_clause_string[QUERY_SIZE] = { 0, };
3944
3945         if (account_id < ALL_ACCOUNT) {
3946                 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3947                 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3948         }
3949         EM_IF_NULL_RETURN_VALUE(result_count, false);
3950
3951         _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);
3952
3953         EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
3954
3955         if (!emstorage_query_mail_list(multi_user_name, conditional_clause_string, transaction, mail_list, result_count, &error)) {
3956                 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3957                 goto FINISH_OFF;
3958         }
3959
3960         ret = true;
3961
3962 FINISH_OFF:
3963         if (err_code != NULL)
3964                 *err_code = error;
3965
3966         EM_PROFILE_END(emstorage_get_mail_list_func);
3967         EM_DEBUG_FUNC_END("ret [%d]", ret);
3968         return ret;
3969 }
3970
3971
3972 /**
3973  *      emstorage_get_mails - Get the Mail list information based on mailbox_name name
3974  *
3975  *
3976  */
3977 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)
3978 {
3979         EM_DEBUG_FUNC_BEGIN();
3980         EM_PROFILE_BEGIN(emStorageGetMails);
3981
3982         int count = 0, ret = false, error = EMAIL_ERROR_NONE;
3983         emstorage_mail_tbl_t *p_data_tbl = NULL;
3984         char conditional_clause_string[QUERY_SIZE] = {0, };
3985
3986         EM_IF_NULL_RETURN_VALUE(mail_list, false);
3987         EM_IF_NULL_RETURN_VALUE(result_count, false);
3988
3989         if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3990                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3991                 if (err_code != NULL)
3992                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3993                 return false;
3994         }
3995
3996         _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);
3997
3998         EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
3999
4000         if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause_string, transaction, &p_data_tbl, &count,  &error)) {
4001                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
4002                 goto FINISH_OFF;
4003         }
4004
4005         ret = true;
4006
4007 FINISH_OFF:
4008
4009         if (ret == true) {
4010                 *mail_list = p_data_tbl;
4011                 *result_count = count;
4012                 EM_DEBUG_LOG("COUNT : %d", count);
4013         } else if (p_data_tbl != NULL)
4014                 emstorage_free_mail(&p_data_tbl, count, NULL);
4015
4016         if (err_code != NULL)
4017                 *err_code = error;
4018
4019         EM_PROFILE_END(emStorageGetMails);
4020         EM_DEBUG_FUNC_END("ret [%d]", ret);
4021         return ret;
4022 }
4023
4024
4025
4026 /**
4027  *      emstorage_get_searched_mail_list - Get the mail list information after filtering
4028  *
4029  *
4030  */
4031 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)
4032 {
4033         EM_DEBUG_FUNC_BEGIN();
4034
4035         int ret = false, error = EMAIL_ERROR_NONE;
4036         char conditional_clause[QUERY_SIZE] = {0, };
4037         char *temp_search_value = NULL;
4038         char *temp_search_value2 = NULL;
4039
4040         EM_IF_NULL_RETURN_VALUE(mail_list, false);
4041         EM_IF_NULL_RETURN_VALUE(result_count, false);
4042
4043         if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
4044                 EM_DEBUG_EXCEPTION("select_num[%p], Mail_list[%p]", result_count, mail_list);
4045                 error = EMAIL_ERROR_INVALID_PARAM;
4046                 goto FINISH_OFF;
4047         }
4048
4049         temp_search_value = em_replace_all_string((char*)search_value, "_", "\\_");
4050         temp_search_value2 = em_replace_all_string(temp_search_value, "%", "\\%");
4051
4052         _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);
4053
4054         EM_DEBUG_LOG("conditional_clause[%s]", conditional_clause);
4055
4056         if (!emstorage_query_mail_list(multi_user_name, conditional_clause, transaction, mail_list, result_count, &error)) {
4057                 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
4058                 goto FINISH_OFF;
4059         }
4060
4061         ret = true;
4062
4063 FINISH_OFF:
4064         EM_DEBUG_LOG("emstorage_get_searched_mail_list finish off");
4065
4066         if (err_code != NULL)
4067                 *err_code = error;
4068
4069         EM_SAFE_FREE(temp_search_value);
4070         EM_SAFE_FREE(temp_search_value2);
4071
4072         EM_DEBUG_FUNC_END("ret [%d]", ret);
4073         return ret;
4074 }
4075
4076
4077 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name)
4078 {
4079         EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
4080
4081         if (account_id <= 0 || !recv_password_file_name || !send_password_file_name) {
4082                 EM_DEBUG_EXCEPTION("Invalid parameter");
4083                 return EMAIL_ERROR_INVALID_PARAM;
4084         }
4085
4086         EM_DEBUG_LOG("MULTI_USER_NAME : [%s]", multi_user_name);
4087
4088         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
4089                 snprintf(recv_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_recv_%s", account_id, multi_user_name);
4090                 snprintf(send_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_send_%s", account_id, multi_user_name);
4091         } else {
4092                 snprintf(recv_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_recv", account_id);
4093                 snprintf(send_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_send", account_id);
4094         }
4095
4096         EM_DEBUG_FUNC_END();
4097         return EMAIL_ERROR_NONE;
4098 }
4099
4100 static int _read_password_from_secure_storage(char *file_name, char **password)
4101 {
4102         EM_DEBUG_FUNC_BEGIN_SEC("file_name[%s]", file_name);
4103
4104         if (!file_name || !password) {
4105                 EM_DEBUG_EXCEPTION("Invalid Parameter");
4106                 return EMAIL_ERROR_INVALID_PARAM;
4107         }
4108
4109         int ret = EMAIL_ERROR_NONE;
4110
4111         ret = emcore_get_password_in_key_manager(file_name, password);
4112         if (ret != EMAIL_ERROR_NONE) {
4113                 EM_DEBUG_EXCEPTION("emcore_get_password_in_key_manager failed : [%d]", ret);
4114                 goto FINISH_OFF;
4115         }
4116
4117 FINISH_OFF:
4118
4119         EM_DEBUG_FUNC_END("ret [%d]", ret);
4120         return ret;
4121 }
4122
4123 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)
4124 {
4125         EM_DEBUG_FUNC_BEGIN("account_id[%d], pulloption[%d], account[%p], transaction[%d], err_code[%p]", account_id, pulloption, account, transaction, err_code);
4126
4127         if (!account) {
4128                 EM_DEBUG_EXCEPTION("account_id[%d], account[%p]", account_id, account);
4129                 if (err_code != NULL)
4130                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4131                 return false;
4132         }
4133
4134         int ret = false;
4135         int error = EMAIL_ERROR_NONE;
4136         emstorage_account_tbl_t* p_data_tbl = NULL;
4137         DB_STMT hStmt = NULL;
4138         char sql_query_string[QUERY_SIZE] = {0, };
4139         int rc = -1;
4140         int sql_len = 0;
4141         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4142         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4143
4144         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4145
4146         EMSTORAGE_START_READ_TRANSACTION(transaction);
4147
4148         /*  Make query string */
4149         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
4150         sql_len = EM_SAFE_STRLEN(sql_query_string);
4151
4152         if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4153                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4154                                 "incoming_server_type,"
4155                                 "incoming_server_address,"
4156                                 "user_email_address,"
4157                                 "incoming_server_user_name,"
4158                                 "retrieval_mode,"
4159                                 "incoming_server_port_number,"
4160                                 "incoming_server_secure_connection,"
4161                                 "incoming_server_authentication_method,"
4162                                 "outgoing_server_type,"
4163                                 "outgoing_server_address,"
4164                                 "outgoing_server_port_number,"
4165                                 "outgoing_server_need_authentication,"
4166                                 "outgoing_server_secure_connection,"
4167                                 "outgoing_server_user_name,"
4168                                 "user_display_name,"
4169                                 "reply_to_address,"
4170                                 "return_address,"
4171                                 "account_id,"
4172                                 "keep_mails_on_pop_server_after_download,"
4173                                 "auto_download_size,"
4174                                 "peak_interval,"
4175                                 "peak_days,"
4176                                 "peak_start_time,"
4177                                 "peak_end_time,"
4178                                 "outgoing_server_use_same_authenticator,"
4179                                 "auto_resend_times,"
4180                                 "outgoing_server_size_limit,"
4181                                 "wifi_auto_download,"
4182                                 "pop_before_smtp,"
4183                                 "incoming_server_requires_apop,"
4184                                 "logo_icon_path,"
4185                                 "user_data,"
4186                                 "user_data_length,"
4187                                 "color_label,"
4188                                 "check_interval,"
4189                                 "sync_status,");
4190                 sql_len = EM_SAFE_STRLEN(sql_query_string);
4191         }
4192
4193         if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME) {
4194                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " account_name, ");
4195                 sql_len = EM_SAFE_STRLEN(sql_query_string);
4196         }
4197
4198         /*  get from secure storage, not from db */
4199         if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4200                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4201                                 "priority,"
4202                                 "keep_local_copy,"
4203                                 "req_delivery_receipt,"
4204                                 "req_read_receipt,"
4205                                 "download_limit,"
4206                                 "block_address,"
4207                                 "block_subject,"
4208                                 "display_name_from,"
4209                                 "reply_with_body,"
4210                                 "forward_with_files,"
4211                                 "add_myname_card,"
4212                                 "add_signature,"
4213                                 "signature,"
4214                                 "add_my_address_to_bcc,"
4215                                 "notification_status,"
4216                                 "vibrate_status,"
4217                                 "display_content_status,"
4218                                 "default_ringtone_status,"
4219                                 "alert_ringtone_path,"
4220                                 "account_svc_id,"
4221                                 "sync_disabled,"
4222                                 "default_mail_slot_size,"
4223                                 "roaming_option,"
4224                                 "smime_type,"
4225                                 "certificate_path,"
4226                                 "cipher_type,"
4227                                 "digest_type,");
4228
4229                 sql_len = EM_SAFE_STRLEN(sql_query_string);
4230         }
4231
4232         /*  dummy value, FROM WHERE clause */
4233         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "0 FROM mail_account_tbl WHERE account_id = %d", account_id);
4234
4235         /*  FROM clause */
4236         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
4237
4238         /*  execute a sql and count rows */
4239         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4240         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4241                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4242
4243         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4244         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4245                         ("sqlite3_step fail:%d", rc));
4246
4247         if (rc == SQLITE_DONE) {
4248                 EM_DEBUG_EXCEPTION("no matched account found...");
4249                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4250                 goto FINISH_OFF;
4251         }
4252
4253         /*  Assign query result to structure */
4254         if (!(p_data_tbl = (emstorage_account_tbl_t *)malloc(sizeof(emstorage_account_tbl_t) * 1))) {
4255                 EM_DEBUG_EXCEPTION("malloc failed...");
4256                 error = EMAIL_ERROR_OUT_OF_MEMORY;
4257                 goto FINISH_OFF;
4258         }
4259
4260         memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * 1);
4261         int col_index = 0;
4262
4263         if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4264                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_type), col_index++);
4265                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_address),  0, col_index++);
4266                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_email_address), 0, col_index++);
4267                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_user_name), 0, col_index++);
4268                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->retrieval_mode), col_index++);
4269                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_port_number), col_index++);
4270                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_secure_connection), col_index++);
4271                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_authentication_method), col_index++);
4272                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->outgoing_server_type), col_index++);
4273                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_address), 0, col_index++);
4274                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_port_number), col_index++);
4275                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_need_authentication), col_index++);
4276                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_secure_connection), col_index++);
4277                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_user_name), 0, col_index++);
4278                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_display_name), 0, col_index++);
4279                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->reply_to_address), 0, col_index++);
4280                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->return_address), 0, col_index++);
4281                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
4282                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->keep_mails_on_pop_server_after_download), col_index++);
4283                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_download_size), col_index++);
4284                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_interval), col_index++);
4285                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_days), col_index++);
4286                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_start_time), col_index++);
4287                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_end_time), col_index++);
4288                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_use_same_authenticator), col_index++);
4289                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_resend_times), col_index++);
4290                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_size_limit), col_index++);
4291                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->wifi_auto_download), col_index++);
4292                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->pop_before_smtp), col_index++);
4293                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_requires_apop), col_index++);
4294                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->logo_icon_path), 0, col_index++);
4295                 _get_stmt_field_data_blob(hStmt, &p_data_tbl->user_data, col_index++);
4296                 _get_stmt_field_data_int(hStmt, &p_data_tbl->user_data_length, col_index++);
4297                 _get_stmt_field_data_int(hStmt, &p_data_tbl->color_label, col_index++);
4298                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->check_interval), col_index++);
4299                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->sync_status), col_index++);
4300         }
4301
4302         if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME)
4303                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->account_name), 0, col_index++);
4304
4305         if (pulloption & EMAIL_ACC_GET_OPT_PASSWORD) {
4306                 /*  get password file name */
4307                 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) {
4308                         EM_DEBUG_EXCEPTION("_get_password_file_name failed [%d]", error);
4309                         goto FINISH_OFF;
4310                 }
4311
4312                 /*  read password from secure storage */
4313                 if ((error = _read_password_from_secure_storage(recv_password_file_name, &(p_data_tbl->incoming_server_password))) < 0) {
4314                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4315                         goto FINISH_OFF;
4316                 }
4317
4318                 EM_DEBUG_LOG_DEV("recv_password_file_name[%s], password[%s]", recv_password_file_name,  p_data_tbl->incoming_server_password);
4319
4320                 if (p_data_tbl->outgoing_server_use_same_authenticator == 0) {
4321                         if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl->outgoing_server_password))) < 0) {
4322                                 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4323                                 goto FINISH_OFF;
4324                         }
4325                         EM_DEBUG_LOG_DEV("send_password_file_name[%s], password[%s]", send_password_file_name,  p_data_tbl->outgoing_server_password);
4326                 }
4327         }
4328
4329         if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4330                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.priority), col_index++);
4331                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.keep_local_copy), col_index++);
4332                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_delivery_receipt), col_index++);
4333                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_read_receipt), col_index++);
4334                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.download_limit), col_index++);
4335                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_address), col_index++);
4336                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_subject), col_index++);
4337                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.display_name_from), 0, col_index++);
4338                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.reply_with_body), col_index++);
4339                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.forward_with_files), col_index++);
4340                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_myname_card), col_index++);
4341                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_signature), col_index++);
4342                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.signature), 0, col_index++);
4343                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.add_my_address_to_bcc), col_index++);
4344                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.notification_status), col_index++);
4345                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.vibrate_status), col_index++);
4346                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.display_content_status), col_index++);
4347                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.default_ringtone_status), col_index++);
4348                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.alert_ringtone_path), 0, col_index++);
4349                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_svc_id), col_index++);
4350                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->sync_disabled), col_index++);
4351                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->default_mail_slot_size), col_index++);
4352                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->roaming_option), col_index++);
4353                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->smime_type), col_index++);
4354                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->certificate_path), 0, col_index++);
4355                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->cipher_type), col_index++);
4356                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->digest_type), col_index++);
4357         }
4358
4359         ret = true;
4360
4361 FINISH_OFF:
4362         if (ret == true)
4363                 *account = p_data_tbl;
4364         else {
4365                 if (p_data_tbl)
4366                         emstorage_free_account((emstorage_account_tbl_t **)&p_data_tbl, 1, NULL);
4367         }
4368         if (hStmt != NULL) {
4369                 rc = sqlite3_finalize(hStmt);
4370                 if (rc != SQLITE_OK) {
4371                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4372                         error = EMAIL_ERROR_DB_FAILURE;
4373                 }
4374         }
4375
4376         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4377
4378         if (err_code != NULL)
4379                 *err_code = error;
4380
4381         EM_DEBUG_FUNC_END("ret [%d]", ret);
4382         return ret;
4383 }
4384
4385 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)
4386 {
4387         EM_DEBUG_FUNC_BEGIN("account_id[%d], password_length[%p], err_code[%p]", account_id, password_length, err_code);
4388
4389         if (account_id <= 0 || password_length == NULL) {
4390                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4391                 if (err_code != NULL)
4392                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4393                 return false;
4394         }
4395
4396         int ret = false;
4397         int error = EMAIL_ERROR_NONE;
4398         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4399         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4400         char *temp_password = NULL;
4401
4402
4403         /*  get password file name */
4404         if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4405                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4406                 goto FINISH_OFF;
4407         }
4408
4409         /*  read password from secure storage */
4410         if (password_type == EMAIL_GET_INCOMING_PASSWORD_LENGTH) {
4411                 if ((error = _read_password_from_secure_storage(recv_password_file_name, &temp_password)) < 0 || !temp_password) {
4412                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed...");
4413                         goto FINISH_OFF;
4414                 }
4415         } else if (password_type == EMAIL_GET_OUTGOING_PASSWORD_LENGTH) {
4416                 if ((error = _read_password_from_secure_storage(send_password_file_name, &temp_password)) < 0 || !temp_password) {
4417                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed...");
4418                         goto FINISH_OFF;
4419                 }
4420         } else {
4421                 EM_DEBUG_LOG("Invalid password type");
4422                 if (err_code != NULL)
4423                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4424                 goto FINISH_OFF;
4425         }
4426
4427         *password_length = EM_SAFE_STRLEN(temp_password);
4428
4429         EM_DEBUG_LOG_SEC("recv_password_file_name[%s], *password_length[%d]", recv_password_file_name,  *password_length);
4430
4431         ret = true;
4432
4433 FINISH_OFF:
4434         EM_SAFE_FREE(temp_password);
4435
4436         if (err_code != NULL)
4437                 *err_code = error;
4438
4439         EM_DEBUG_FUNC_END("ret [%d]", ret);
4440         return ret;
4441 }
4442
4443 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)
4444 {
4445         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);
4446
4447         int err = EMAIL_ERROR_NONE;
4448         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4449         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4450
4451         if (input_incoming_server_password == NULL && input_outgoing_server_password == NULL) {
4452                 EM_DEBUG_EXCEPTION_SEC("Invalid param");
4453                 err = EMAIL_ERROR_INVALID_PARAM;
4454                 goto FINISH_OFF;
4455         }
4456
4457         /*  get password file name */
4458         if ((err = _get_password_file_name(multi_user_name,
4459                                         input_account_id,
4460                                         recv_password_file_name,
4461                                         send_password_file_name)) != EMAIL_ERROR_NONE) {
4462                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4463                 goto FINISH_OFF;
4464         }
4465
4466         EM_DEBUG_LOG_SEC("recv_password_file_name [%s] input_incoming_server_password [%s]",
4467                         recv_password_file_name, input_incoming_server_password);
4468         EM_DEBUG_LOG_SEC("send_password_file_name [%s] input_outgoing_server_password [%s]",
4469                         send_password_file_name, input_outgoing_server_password);
4470
4471         if (input_incoming_server_password) {
4472                 err = emcore_remove_password_in_key_manager(recv_password_file_name);
4473                 if (err != EMAIL_ERROR_NONE) {
4474                         EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", recv_password_file_name);
4475                         goto FINISH_OFF;
4476                 }
4477
4478                 /*  save recv passwords to the secure storage */
4479                 err = emcore_add_password_in_key_manager(recv_password_file_name, input_incoming_server_password);
4480                 if (err != EMAIL_ERROR_NONE) {
4481                         EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4482                         goto FINISH_OFF;
4483                 }
4484         }
4485
4486         if (input_outgoing_server_password) {
4487                 err = emcore_remove_password_in_key_manager(send_password_file_name);
4488                 if (err != EMAIL_ERROR_NONE) {
4489                         EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", send_password_file_name);
4490                         goto FINISH_OFF;
4491                 }
4492
4493                 /*  save send passwords to the secure storage */
4494                 err = emcore_add_password_in_key_manager(send_password_file_name, input_outgoing_server_password);
4495                 if (err != EMAIL_ERROR_NONE) {
4496                         EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4497                         goto FINISH_OFF;
4498                 }
4499         }
4500 FINISH_OFF:
4501
4502         EM_DEBUG_FUNC_END("err [%d]", err);
4503         return err;
4504 }
4505
4506 INTERNAL_FUNC int emstorage_update_account(char *multi_user_name, int account_id, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4507 {
4508         EM_DEBUG_FUNC_BEGIN("account_id[%d], account[%p], transaction[%d], err_code[%p]", account_id, account_tbl, transaction, err_code);
4509
4510         if (account_id < FIRST_ACCOUNT_ID || !account_tbl) {
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 error = EMAIL_ERROR_NONE;
4518         int rc, ret = false;
4519
4520         DB_STMT hStmt = NULL;
4521         char sql_query_string[QUERY_SIZE] = {0, };
4522
4523         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4524
4525         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4526
4527         SNPRINTF(sql_query_string, sizeof(sql_query_string),
4528                         "UPDATE mail_account_tbl SET"
4529                         "  account_name = ?"
4530                         ", logo_icon_path = ?"
4531                         ", user_data = ?"
4532                         ", user_data_length = ?"
4533                         ", account_svc_id = ?"
4534                         ", sync_status = ?"
4535                         ", sync_disabled = ?"
4536                         ", default_mail_slot_size = ?"
4537                         ", roaming_option = ?"
4538                         ", color_label = ?"
4539                         ", user_display_name = ?"
4540                         ", user_email_address = ?"
4541                         ", reply_to_address = ?"
4542                         ", return_address = ?"
4543                         ", incoming_server_type = ?"
4544                         ", incoming_server_address = ?"
4545                         ", incoming_server_port_number = ?"
4546                         ", incoming_server_user_name = ?"
4547                         ", incoming_server_secure_connection = ?"
4548                         ", incoming_server_authentication_method = ?"
4549                         ", retrieval_mode = ?"
4550                         ", keep_mails_on_pop_server_after_download = ?"
4551                         ", check_interval = ?"
4552                         ", auto_download_size = ?"
4553                         ", peak_interval = ?"
4554                         ", peak_days = ?"
4555                         ", peak_start_time = ?"
4556                         ", peak_end_time = ?"
4557                         ", outgoing_server_type = ?"
4558                         ", outgoing_server_address = ?"
4559                         ", outgoing_server_port_number = ?"
4560                         ", outgoing_server_user_name = ?"
4561                         ", outgoing_server_secure_connection = ?"
4562                         ", outgoing_server_need_authentication = ?"
4563                         ", outgoing_server_use_same_authenticator = ?"
4564                         ", priority = ?"
4565                         ", keep_local_copy = ?"
4566                         ", req_delivery_receipt = ?"
4567                         ", req_read_receipt = ?"
4568                         ", download_limit = ?"
4569                         ", block_address = ?"
4570                         ", block_subject = ?"
4571                         ", display_name_from = ?"
4572                         ", reply_with_body = ?"
4573                         ", forward_with_files = ?"
4574                         ", add_myname_card = ?"
4575                         ", add_signature = ?"
4576                         ", signature = ?"
4577                         ", add_my_address_to_bcc = ?"
4578                         ", notification_status = ?"
4579                         ", vibrate_status = ?"
4580                         ", display_content_status = ?"
4581                         ", default_ringtone_status = ?"
4582                         ", alert_ringtone_path = ?"
4583                         ", auto_resend_times = ?"
4584                         ", outgoing_server_size_limit = ?"
4585                         ", wifi_auto_download = ?"
4586                         ", pop_before_smtp = ?"
4587                         ", incoming_server_requires_apop = ?"
4588                         ", smime_type = ?"
4589                         ", certificate_path = ?"
4590                         ", cipher_type = ?"
4591                         ", digest_type = ?"
4592                         ", user_name = ?"
4593                         " WHERE account_id = ?");
4594
4595         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4596         EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
4597         EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
4598         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4599                         ("sqlite3_prepare fail:(%d) %s", rc, sqlite3_errmsg(local_db_handle)));
4600
4601         int i = 0;
4602
4603         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4604         _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4605         _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4606         _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4607         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4608         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4609         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4610         _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4611         _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
4612         _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
4613         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4614         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4615         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4616         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4617         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4618         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4619         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4620         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4621         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4622         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
4623         _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
4624         _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
4625         _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
4626         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
4627         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
4628         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
4629         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
4630         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
4631         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
4632         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4633         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
4634         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
4635         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
4636         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
4637         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
4638         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
4639         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
4640         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
4641         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
4642         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
4643         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
4644         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
4645         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4646         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
4647         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
4648         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
4649         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
4650         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
4651         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
4652         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
4653         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
4654         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
4655         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
4656         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4657         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
4658         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
4659         _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
4660         _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
4661         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
4662         _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
4663         _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4664         _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
4665         _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
4666         _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4667         _bind_stmt_field_data_int(hStmt, i++, account_id);
4668
4669         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4670         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
4671                         ("sqlite3_step fail:%d", rc));
4672         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4673                         ("sqlite3_step fail:%d", rc));
4674
4675         /*  validate account existence */
4676         rc = sqlite3_changes(local_db_handle);
4677         if (rc == 0) {
4678                 EM_DEBUG_EXCEPTION("no matched account found...");
4679                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4680                 goto FINISH_OFF;
4681         }
4682
4683         if (account_tbl->incoming_server_password || account_tbl->outgoing_server_password) {
4684                 if ((error = emstorage_update_account_password(multi_user_name, account_id, account_tbl->incoming_server_password, account_tbl->outgoing_server_password)) != EMAIL_ERROR_NONE) {
4685                         EM_DEBUG_EXCEPTION("emstorage_update_account_password failed [%d]", error);
4686                         goto FINISH_OFF;
4687                 }
4688         }
4689
4690         ret = true;
4691
4692 FINISH_OFF:
4693         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4694
4695         if (hStmt != NULL) {
4696                 rc = sqlite3_finalize(hStmt);
4697                 if (rc != SQLITE_OK) {
4698                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4699                         error = EMAIL_ERROR_DB_FAILURE;
4700                 }
4701         }
4702
4703         if (error == EMAIL_ERROR_NONE) {
4704                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_tbl->account_id, 0, NULL, 0))
4705                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_ACCOUNT_UPDATE] : Notification Failed >>> ");
4706         }
4707
4708         if (err_code != NULL)
4709                 *err_code = error;
4710
4711         EM_DEBUG_FUNC_END("ret [%d]", ret);
4712         return ret;
4713 }
4714
4715 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)
4716 {
4717         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], field_name[%s], value[%d], transaction[%d]", account_id, field_name, value, transaction);
4718         int error = EMAIL_ERROR_NONE;
4719         int result = 0;
4720         char sql_query_string[QUERY_SIZE] = {0, };
4721         sqlite3 *local_db_handle = NULL;
4722
4723         if (!account_id  || !field_name) {
4724                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4725                 return EMAIL_ERROR_INVALID_PARAM;
4726         }
4727
4728         local_db_handle = emstorage_get_db_connection(multi_user_name);
4729
4730         /* Write query string */
4731         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_account_tbl SET %s = %d WHERE account_id = %d", field_name, value, account_id);
4732
4733         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4734
4735         /* Execute query */
4736         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4737
4738         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4739         if (error != EMAIL_ERROR_NONE) {
4740                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4741                 goto FINISH_OFF;
4742         }
4743
4744         if (sqlite3_changes(local_db_handle) == 0)
4745                 EM_DEBUG_LOG("no mail matched...");
4746
4747
4748 FINISH_OFF:
4749         result = (error == EMAIL_ERROR_NONE) ? true : false;
4750         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, error);
4751
4752         if (error == EMAIL_ERROR_NONE) {
4753                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_id, 0, field_name, value))
4754                         EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_ACCOUNT_UPDATE [%s,%d]", field_name, value);
4755         }
4756         EM_DEBUG_FUNC_END("error [%d]", error);
4757         return error;
4758 }
4759
4760 INTERNAL_FUNC int emstorage_get_sync_status_of_account(char *multi_user_name, int account_id, int *result_sync_status, int *err_code)
4761 {
4762         EM_DEBUG_FUNC_BEGIN("account_id[%d], result_sync_status [%p], err_code[%p]", account_id, result_sync_status, err_code);
4763
4764         if (!result_sync_status) {
4765                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4766                 if (err_code != NULL)
4767                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4768                 return false;
4769         }
4770
4771         int error = EMAIL_ERROR_NONE, rc, ret = false, sync_status = 0, count, i, col_index;
4772         char sql_query_string[QUERY_SIZE] = {0, };
4773         char **result = NULL;
4774         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4775
4776         if (account_id)
4777                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl WHERE account_id = %d", account_id);
4778         else
4779                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl");
4780
4781         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
4782         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
4783                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4784
4785         if (!count) {
4786                 EM_DEBUG_EXCEPTION("no matched account found...");
4787                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4788                 goto FINISH_OFF;
4789         }
4790
4791         col_index = 1;
4792         *result_sync_status = 0;
4793
4794         for (i = 0; i < count; i++) {
4795                 _get_table_field_data_int(result, &sync_status, col_index++);
4796                 *result_sync_status |= sync_status;
4797         }
4798
4799         EM_DEBUG_LOG("sync_status [%d]", sync_status);
4800
4801         sqlite3_free_table(result);
4802
4803         ret = true;
4804
4805 FINISH_OFF:
4806
4807
4808         if (err_code != NULL)
4809                 *err_code = error;
4810
4811         EM_DEBUG_FUNC_END("ret [%d]", ret);
4812         return ret;
4813 }
4814
4815 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)
4816 {
4817         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);
4818
4819         int error = EMAIL_ERROR_NONE, rc, ret = false, set_value = sync_status, result_sync_status;
4820         char sql_query_string[QUERY_SIZE] = {0, };
4821         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4822
4823         if (set_operator != SET_TYPE_SET && account_id) {
4824                 if (!emstorage_get_sync_status_of_account(multi_user_name, account_id, &result_sync_status, &error)) {
4825                         EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", error);
4826                         if (err_code != NULL)
4827                                 *err_code = error;
4828                         return false;
4829                 }
4830                 switch (set_operator) {
4831                 case SET_TYPE_UNION:
4832                         set_value = result_sync_status | set_value;
4833                         break;
4834                 case SET_TYPE_MINUS:
4835                         set_value = result_sync_status & (~set_value);
4836                         break;
4837                 default:
4838                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_NOT_SUPPORTED [%d]", set_operator);
4839                         error = EMAIL_ERROR_NOT_SUPPORTED;
4840                         break;
4841                 }
4842                 EM_DEBUG_LOG("set_value [%d]", set_value);
4843         }
4844
4845         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4846
4847         if (account_id)
4848                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE account_id = %d", set_value, account_id);
4849         else
4850                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE incoming_server_type <> 5", set_value);
4851
4852         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4853
4854         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4855         if (error != EMAIL_ERROR_NONE) {
4856                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4857                 goto FINISH_OFF;
4858         }
4859
4860         rc = sqlite3_changes(local_db_handle);
4861
4862         if (rc == 0) {
4863                 EM_DEBUG_EXCEPTION("no matched account found...");
4864                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4865                 goto FINISH_OFF;
4866         }
4867
4868         ret = true;
4869
4870 FINISH_OFF:
4871         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4872
4873         if (error == EMAIL_ERROR_NONE) {
4874                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE_SYNC_STATUS, account_id, set_value, NULL, 0))
4875                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_UPDATE_SYNC_STATUS] : Notification failed");
4876         }
4877
4878         if (err_code != NULL)
4879                 *err_code = error;
4880
4881         EM_DEBUG_FUNC_END("ret [%d]", ret);
4882         return ret;
4883 }
4884
4885 INTERNAL_FUNC int emstorage_add_account(char *multi_user_name, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4886 {
4887         EM_DEBUG_FUNC_BEGIN("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4888
4889         if (!account_tbl) {
4890                 EM_DEBUG_EXCEPTION("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4891                 if (err_code != NULL)
4892                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4893                 return false;
4894         }
4895
4896         int row_count = 0;
4897         int i = 0;
4898         int rc = -1, ret = false;
4899         int error = EMAIL_ERROR_NONE;
4900         DB_STMT hStmt = NULL;
4901         char sql_query_string[QUERY_SIZE] = {0, };
4902         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4903         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4904
4905         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4906
4907         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4908
4909         char *sql = "SELECT rowid FROM mail_account_tbl;";
4910         char **result = NULL;
4911
4912         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, &row_count, NULL, NULL), rc);
4913         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
4914                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
4915
4916         if (NULL == result[1]) rc = 1;
4917         else {
4918                 for (i = 1; i <= row_count; i++) {
4919                         if (i != atoi(result[i]))
4920                                 break;
4921                 }
4922
4923                 rc = i;
4924         }
4925         sqlite3_free_table(result);
4926         result = NULL;
4927
4928         if (rc < 0 || rc > EMAIL_ACCOUNT_MAX) {
4929                 EM_DEBUG_EXCEPTION("OVERFLOWED THE MAX ACCOUNT");
4930                 error = EMAIL_ERROR_ACCOUNT_MAX_COUNT;
4931                 goto FINISH_OFF;
4932         }
4933
4934         account_tbl->account_id = rc;
4935
4936         if ((error = _get_password_file_name(multi_user_name, account_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4937                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4938                 goto FINISH_OFF;
4939         }
4940
4941         EM_DEBUG_LOG(" >>>> ACCOUNT_ID [ %d ] ", account_tbl->account_id);
4942         SNPRINTF(sql_query_string, sizeof(sql_query_string),
4943                         "INSERT INTO mail_account_tbl VALUES "
4944                         "(        "
4945                         "    ? "  /*   account_id */
4946                         "  , ? "  /*   account_name */
4947                         "  , ? "  /*   logo_icon_path */
4948                         "  , ? "  /*   user_data */
4949                         "  , ? "  /*   user_data_length */
4950                         "  , ? "  /*   account_svc_id */
4951                         "  , ? "  /*   sync_status */
4952                         "  , ? "  /*   sync_disabled */
4953                         "  , ? "  /*   default_mail_slot_size */
4954                         "  , ? "  /*   roaming_option */
4955                         "  , ? "  /*   color_label */
4956                         "  , ? "  /*   user_display_name */
4957                         "  , ? "  /*   user_email_address */
4958                         "  , ? "  /*   reply_to_address */
4959                         "  , ? "  /*   return_address */
4960                         "  , ? "  /*   incoming_server_type */
4961                         "  , ? "  /*   incoming_server_address */
4962                         "  , ? "  /*   incoming_server_port_number */
4963                         "  , ? "  /*   incoming_server_user_name */
4964                         "  , ? "  /*   incoming_server_password */
4965                         "  , ? "  /*   incoming_server_secure_connection */
4966                         "  , ? "  /*   incoming_server_authentication_method */
4967                         "  , ? "  /*   retrieval_mode */
4968                         "  , ? "  /*   keep_mails_on_pop_server_after_download */
4969                         "  , ? "  /*   check_interval */
4970                         "  , ? "  /*   auto_download_size */
4971                         "  , ? "  /*   peak_interval */
4972                         "  , ? "  /*   peak_days */
4973                         "  , ? "  /*   peak_start_time */
4974                         "  , ? "  /*   peak_end_time */
4975                         "  , ? "  /*   outgoing_server_type */
4976                         "  , ? "  /*   outgoing_server_address */
4977                         "  , ? "  /*   outgoing_server_port_number */
4978                         "  , ? "  /*   outgoing_server_user_name */
4979                         "  , ? "  /*   outgoing_server_password */
4980                         "  , ? "  /*   outgoing_server_secure_connection */
4981                         "  , ? "  /*   outgoing_server_need_authentication */
4982                         "  , ? "  /*   outgoing_server_use_same_authenticator */
4983                         "  , ? "  /*   priority */
4984                         "  , ? "  /*   keep_local_copy */
4985                         "  , ? "  /*   req_delivery_receipt */
4986                         "  , ? "  /*   req_read_receipt */
4987                         "  , ? "  /*   download_limit */
4988                         "  , ? "  /*   block_address */
4989                         "  , ? "  /*   block_subject */
4990                         "  , ? "  /*   display_name_from */
4991                         "  , ? "  /*   reply_with_body */
4992                         "  , ? "  /*   forward_with_files */
4993                         "  , ? "  /*   add_myname_card */
4994                         "  , ? "  /*   add_signature */
4995                         "  , ? "  /*   signature */
4996                         "  , ? "  /*   add_my_address_to_bcc */
4997                         "  , ? "  /*   auto_resend_times */
4998                         "  , ? "  /*   outgoing_server_size_limit */
4999                         "  , ? "  /*   wifi_auto_download */
5000                         "  , ? "  /*   pop_before_smtp */
5001                         "  , ? "  /*   incoming_server_requires_apop */
5002                         "  , ? "  /*   smime_type */
5003                         "  , ? "  /*   certificate_path */
5004                         "  , ? "  /*   cipher_type */
5005                         "  , ? "  /*   digest_type */
5006                         "  , ? "  /*   notification_status */
5007                         "  , ? "  /*   vibrate_status */
5008                         "  , ? "  /*   display_content_status */
5009                         "  , ? "  /*   default_ringtone_status */
5010                         "  , ? "  /*   alert_ringtone_path */
5011                         "  , ? "  /*   user_name */
5012                         ") ");
5013
5014         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5015         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5016                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5017
5018         i = 0;
5019
5020         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_id);
5021         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
5022         _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5023         _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
5024         _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
5025         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
5026         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
5027         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
5028         _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
5029         _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
5030         _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
5031         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
5032         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5033         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5034         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5035         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
5036         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5037         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
5038         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
5039         _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
5040         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
5041         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
5042         _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
5043         _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
5044         _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
5045         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
5046         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
5047         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
5048         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
5049         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
5050         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
5051         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5052         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
5053         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
5054         _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
5055         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
5056         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
5057         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
5058         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
5059         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
5060         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
5061         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
5062         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
5063         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
5064         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
5065         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
5066         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
5067         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
5068         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
5069         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
5070         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
5071         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
5072         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
5073         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
5074         _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
5075         _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
5076         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
5077         _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
5078         _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
5079         _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
5080         _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
5081         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
5082         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
5083         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
5084         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
5085         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5086         _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5087
5088         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5089
5090         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
5091                         ("sqlite3_step fail:%d", rc));
5092         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5093                         ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
5094
5095
5096         /*  save passwords to the secure storage */
5097         EM_DEBUG_LOG_SEC("save to the secure storage : recv_file[%s], send_file[%s]", recv_password_file_name, send_password_file_name);
5098         error = emcore_add_password_in_key_manager(recv_password_file_name, account_tbl->incoming_server_password);
5099         if (error != EMAIL_ERROR_NONE) {
5100                 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
5101                 goto FINISH_OFF;
5102         }
5103
5104         error = emcore_add_password_in_key_manager(send_password_file_name, account_tbl->outgoing_server_password);
5105         if (error != EMAIL_ERROR_NONE) {
5106                 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
5107                 goto FINISH_OFF;
5108         }
5109
5110         ret = true;
5111
5112 FINISH_OFF:
5113         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5114
5115         if (hStmt != NULL) {
5116                 rc = sqlite3_finalize(hStmt);
5117                 if (rc != SQLITE_OK) {
5118                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5119                         error = EMAIL_ERROR_DB_FAILURE;
5120                 }
5121         }
5122
5123         if (error == EMAIL_ERROR_NONE) {
5124                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_ADD, account_tbl->account_id, 0, NULL, 0))
5125                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_ADD] : Notification failed");
5126         }
5127
5128         if (err_code != NULL)
5129                 *err_code = error;
5130
5131         EM_DEBUG_FUNC_END("ret [%d]", ret);
5132         return ret;
5133 }
5134
5135 INTERNAL_FUNC int emstorage_delete_account(char *multi_user_name, int account_id, int transaction, int *err_code)
5136 {
5137         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
5138
5139         if (account_id < FIRST_ACCOUNT_ID) {
5140                 EM_DEBUG_EXCEPTION(" account_id[%d]", account_id);
5141
5142                 if (err_code != NULL)
5143                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5144                 return false;
5145         }
5146
5147         int rc = -1, ret = false;
5148         int error = EMAIL_ERROR_NONE;
5149
5150         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5151
5152         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5153
5154         /*  TODO : delete password files - file names can be obtained from db or a rule that makes a name */
5155         char sql_query_string[QUERY_SIZE] = {0, };
5156         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5157         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5158
5159         /*  get password file name */
5160         if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
5161                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
5162                 goto FINISH_OFF;
5163         }
5164
5165         /*  delete from db */
5166         memset(sql_query_string, 0x00, sizeof(sql_query_string));
5167         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_account_tbl WHERE account_id = %d", account_id);
5168         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5169         if (error != EMAIL_ERROR_NONE) {
5170                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5171                 goto FINISH_OFF;
5172         }
5173
5174         /*  validate account existence */
5175         rc = sqlite3_changes(local_db_handle);
5176         if (rc == 0) {
5177                 EM_DEBUG_EXCEPTION(" no matched account found...");
5178                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
5179                 goto FINISH_OFF;
5180         }
5181
5182         /*  delete from secure storage */
5183         error = emcore_remove_password_in_key_manager(recv_password_file_name);
5184         if (error != EMAIL_ERROR_NONE) {
5185                 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5186                 goto FINISH_OFF;
5187         }
5188
5189         error = emcore_remove_password_in_key_manager(send_password_file_name);
5190         if (error != EMAIL_ERROR_NONE) {
5191                 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5192                 goto FINISH_OFF;
5193         }
5194
5195         ret = true;
5196
5197 FINISH_OFF:
5198         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5199
5200         if (err_code != NULL)
5201                 *err_code = error;
5202
5203         EM_DEBUG_FUNC_END("ret [%d]", ret);
5204         return ret;
5205 }
5206
5207
5208 INTERNAL_FUNC int emstorage_free_account(emstorage_account_tbl_t** account_list, int count, int *err_code)
5209 {
5210         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%d], err_code[%p]", account_list, count, err_code);
5211
5212         int ret = false;
5213         int error = EMAIL_ERROR_NONE;
5214
5215         if (count > 0) {
5216                 if (!account_list || !*account_list) {
5217                         EM_DEBUG_EXCEPTION("account_list[%p], count[%d]", account_list, count);
5218                         error = EMAIL_ERROR_INVALID_PARAM;
5219                         goto FINISH_OFF;
5220                 }
5221
5222                 emstorage_account_tbl_t* p = *account_list;
5223                 int i = 0;
5224
5225                 for (; i < count; i++) {
5226                         EM_SAFE_FREE(p[i].account_name);
5227                         EM_SAFE_FREE(p[i].incoming_server_address);
5228                         EM_SAFE_FREE(p[i].user_email_address);
5229                         EM_SAFE_FREE(p[i].user_data);
5230                         EM_SAFE_FREE(p[i].incoming_server_user_name);
5231                         EM_SAFE_FREE(p[i].incoming_server_password);
5232                         EM_SAFE_FREE(p[i].outgoing_server_address);
5233                         EM_SAFE_FREE(p[i].outgoing_server_user_name);
5234                         EM_SAFE_FREE(p[i].outgoing_server_password);
5235                         EM_SAFE_FREE(p[i].user_display_name);
5236                         EM_SAFE_FREE(p[i].reply_to_address);
5237                         EM_SAFE_FREE(p[i].return_address);
5238                         EM_SAFE_FREE(p[i].logo_icon_path);
5239                         EM_SAFE_FREE(p[i].options.display_name_from);
5240                         EM_SAFE_FREE(p[i].options.signature);
5241                         EM_SAFE_FREE(p[i].options.alert_ringtone_path);
5242                         EM_SAFE_FREE(p[i].certificate_path);
5243                         EM_SAFE_FREE(p[i].user_name);
5244                 }
5245
5246                 EM_SAFE_FREE(p);
5247                 *account_list = NULL;
5248         }
5249
5250         ret = true;
5251
5252 FINISH_OFF:
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_get_mailbox_count(char *multi_user_name, int account_id, int local_yn, int *count, int transaction, int *err_code)
5261 {
5262         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);
5263
5264         if ((account_id < FIRST_ACCOUNT_ID) || (count == NULL)) {
5265                 EM_DEBUG_EXCEPTION(" account_list[%d], local_yn[%d], count[%p]", account_id, local_yn, count);
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         char sql_query_string[QUERY_SIZE] = {0, };
5275
5276         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5277         EMSTORAGE_START_READ_TRANSACTION(transaction);
5278
5279         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);
5280
5281         char **result;
5282
5283         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5284         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
5285                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5286
5287         *count = atoi(result[1]);
5288         sqlite3_free_table(result);
5289
5290
5291         ret = true;
5292
5293 FINISH_OFF:
5294
5295         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5296
5297         if (err_code != NULL)
5298                 *err_code = error;
5299
5300         EM_DEBUG_FUNC_END("ret [%d]", ret);
5301         return ret;
5302 }
5303
5304 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)
5305 {
5306         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);
5307
5308         if (!select_num || !mailbox_list) {
5309                 EM_DEBUG_EXCEPTION("Invalid parameters");
5310
5311                 if (err_code != NULL)
5312                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5313
5314                 return false;
5315         }
5316
5317         int ret = false;
5318         int error = EMAIL_ERROR_NONE;
5319         char conditional_clause_string[QUERY_SIZE] = {0, };
5320         char ordering_clause_string[QUERY_SIZE] = {0, };
5321
5322         if (account_id == ALL_ACCOUNT) {
5323                 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5324                         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);
5325         } else {
5326                 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d  ", account_id);
5327                 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5328                         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);
5329         }
5330
5331         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5332
5333         switch (sort_type) {
5334         case EMAIL_MAILBOX_SORT_BY_NAME_ASC:
5335                 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name ASC");
5336                 break;
5337
5338         case EMAIL_MAILBOX_SORT_BY_NAME_DSC:
5339                 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name DESC");
5340                 break;
5341
5342         case EMAIL_MAILBOX_SORT_BY_TYPE_ASC:
5343                 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type ASC");
5344                 break;
5345
5346         case EMAIL_MAILBOX_SORT_BY_TYPE_DSC:
5347                 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type DEC");
5348                 break;
5349         }
5350
5351         EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5352
5353         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5354                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5355                 goto FINISH_OFF;
5356         }
5357
5358         ret = true;
5359
5360 FINISH_OFF:
5361
5362         if (err_code != NULL)
5363                 *err_code = error;
5364
5365         EM_DEBUG_FUNC_END("ret [%d]", ret);
5366         return ret;
5367 }
5368
5369 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)
5370 {
5371         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);
5372
5373         int ret = false;
5374         int error = EMAIL_ERROR_NONE;
5375         int where_clause_count = 0;
5376         char conditional_clause_string[QUERY_SIZE] = {0, };
5377         char ordering_clause_string[QUERY_SIZE] = {0, };
5378
5379         if (!select_num || !mailbox_list) {
5380                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5381                 error = EMAIL_ERROR_INVALID_PARAM;
5382                 goto FINISH_OFF;
5383         }
5384
5385         if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL) {
5386                 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);
5387                 where_clause_count++;
5388         }
5389
5390         if (account_id > 0) {
5391                 if (where_clause_count == 0) {
5392                         SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " WHERE ");
5393                         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);
5394                 } else
5395                         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);
5396         }
5397
5398         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY CASE WHEN MBT.mailbox_name"
5399                         " GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\'"
5400                         " THEN 2 ELSE 1 END ASC,"
5401                         " MBT.mailbox_name COLLATE CONVERTUTF8 ASC ");
5402         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5403         EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5404
5405         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 1, 1, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5406                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5407                 goto FINISH_OFF;
5408         }
5409
5410         ret = true;
5411
5412 FINISH_OFF:
5413
5414         if (err_code != NULL)
5415                 *err_code = error;
5416
5417         EM_DEBUG_FUNC_END("ret [%d]", ret);
5418         return ret;
5419 }
5420
5421 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)
5422 {
5423         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);
5424
5425         int ret = false;
5426         int error = EMAIL_ERROR_NONE;
5427         char conditional_clause_string[QUERY_SIZE] = {0, };
5428
5429         if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list || !parent_mailbox_name) {
5430                 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);
5431                 if (err_code != NULL)
5432                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5433                 return false;
5434         }
5435
5436         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);
5437         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5438
5439         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) {
5440                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5441                 goto FINISH_OFF;
5442         }
5443
5444         ret = true;
5445
5446 FINISH_OFF:
5447
5448
5449         if (err_code != NULL)
5450                 *err_code = error;
5451
5452         EM_DEBUG_FUNC_END("ret [%d]", ret);
5453         return ret;
5454 }
5455
5456 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)
5457 {
5458         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);
5459         if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list) {
5460                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5461
5462                 if (err_code != NULL)
5463                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5464
5465                 return false;
5466         }
5467
5468         int ret = false;
5469         int error = EMAIL_ERROR_NONE;
5470         char conditional_clause_string[QUERY_SIZE] = {0, };
5471
5472         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND modifiable_yn = 0", account_id);
5473         EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
5474
5475         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) {
5476                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5477                 goto FINISH_OFF;
5478         }
5479
5480         ret = true;
5481
5482 FINISH_OFF:
5483
5484         if (err_code != NULL)
5485                 *err_code = error;
5486
5487         EM_DEBUG_FUNC_END("ret [%d]", ret);
5488         return ret;
5489 }
5490
5491 INTERNAL_FUNC int emstorage_stamp_last_sync_time_of_mailbox(char *multi_user_name, int input_mailbox_id, int input_transaction)
5492 {
5493         EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_transaction [%d]", input_mailbox_id, input_transaction);
5494
5495         int      result_code = false;
5496         int      error = EMAIL_ERROR_NONE;
5497         time_t   current_time = 0;
5498         char     sql_query_string[QUERY_SIZE] = {0, };
5499         sqlite3 *local_db_handle = NULL;
5500
5501         if (!input_mailbox_id) {
5502                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5503                 return EMAIL_ERROR_INVALID_PARAM;
5504         }
5505
5506         time(&current_time);
5507
5508         local_db_handle = emstorage_get_db_connection(multi_user_name);
5509         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
5510
5511         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5512                         "UPDATE mail_box_tbl SET"
5513                         " last_sync_time = %d"
5514                         " WHERE mailbox_id = %d"
5515                         , (int)current_time
5516                         , input_mailbox_id);
5517
5518         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
5519         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5520         if (error != EMAIL_ERROR_NONE) {
5521                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5522                 goto FINISH_OFF;
5523         }
5524
5525 FINISH_OFF:
5526
5527         if (error == EMAIL_ERROR_NONE)
5528                 result_code = true;
5529
5530         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, result_code, error);
5531
5532         EM_DEBUG_FUNC_END("error [%d]", error);
5533         return error;
5534 }
5535
5536 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)
5537 {
5538         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);
5539         EM_PROFILE_BEGIN(profile_emstorage_get_mailbox_by_name);
5540
5541         if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !result_mailbox) {
5542                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], mailbox_name[%s], result_mailbox[%p]", account_id, local_yn, mailbox_name, result_mailbox);
5543
5544                 if (err_code != NULL)
5545                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5546                 return false;
5547         }
5548
5549         int ret = false;
5550         int error = EMAIL_ERROR_NONE;
5551         int result_count = 0;
5552         char conditional_clause_string[QUERY_SIZE] = {0, };
5553
5554         if (strcmp(mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
5555                 if (!(*result_mailbox = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t)))) {
5556                         EM_DEBUG_EXCEPTION("malloc failed...");
5557                         error = EMAIL_ERROR_OUT_OF_MEMORY;
5558                         goto FINISH_OFF;
5559                 }
5560
5561                 (*result_mailbox)->mailbox_id                 = 0;
5562                 (*result_mailbox)->account_id                 = account_id;
5563                 (*result_mailbox)->local_yn                   = 1;
5564                 (*result_mailbox)->mailbox_name               = EM_SAFE_STRDUP(mailbox_name);
5565                 (*result_mailbox)->mailbox_type               = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
5566                 (*result_mailbox)->alias                      = EM_SAFE_STRDUP(mailbox_name);
5567                 (*result_mailbox)->deleted_flag               = 0;
5568                 (*result_mailbox)->modifiable_yn              = 1;
5569                 (*result_mailbox)->total_mail_count_on_server = 1;
5570                 (*result_mailbox)->has_archived_mails         = 0;
5571                 (*result_mailbox)->mail_slot_size             = 0x0FFFFFFF;
5572                 (*result_mailbox)->no_select                  = 0;
5573         } else {
5574                 if (local_yn == -1)
5575                         sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d AND mailbox_name = '%q'", account_id, mailbox_name);
5576                 else
5577                         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);
5578
5579                 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5580
5581                 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5582                         EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5583                         goto FINISH_OFF;
5584                 }
5585         }
5586
5587         ret = true;
5588
5589 FINISH_OFF:
5590
5591         if (err_code != NULL)
5592                 *err_code = error;
5593
5594         EM_PROFILE_END(profile_emstorage_get_mailbox_by_name);
5595         EM_DEBUG_FUNC_END("ret [%d]", ret);
5596         return ret;
5597 }
5598
5599 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)
5600 {
5601         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);
5602
5603         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_USER_DEFINED) || !output_mailbox) {
5604                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_type[%d], output_mailbox[%p]", account_id, mailbox_type, output_mailbox);
5605                 if (err_code != NULL)
5606                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5607                 return false;
5608         }
5609
5610         int ret = false;
5611         int error = EMAIL_ERROR_NONE;
5612         int result_count = 0;
5613         emstorage_mailbox_tbl_t *result_mailbox = NULL;
5614         char conditional_clause_string[QUERY_SIZE] = {0,};
5615
5616
5617         SNPRINTF(conditional_clause_string, QUERY_SIZE, "WHERE account_id = %d AND mailbox_type = %d ", account_id, mailbox_type);
5618
5619         EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5620
5621         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", true, false, &result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5622                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl error [%d]", error);
5623                 goto FINISH_OFF;
5624         }
5625
5626         ret = true;
5627
5628 FINISH_OFF:
5629         if (ret == true)
5630                 *output_mailbox = result_mailbox;
5631
5632         if (err_code != NULL)
5633                 *err_code = error;
5634
5635         EM_DEBUG_FUNC_END("ret [%d]", ret);
5636         return ret;
5637 }
5638
5639 INTERNAL_FUNC int emstorage_get_mailbox_by_id(char *multi_user_name, int input_mailbox_id, emstorage_mailbox_tbl_t** output_mailbox)
5640 {
5641         EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_mailbox[%p]", input_mailbox_id, output_mailbox);
5642
5643         if (input_mailbox_id <= 0 || !output_mailbox) {
5644                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: input_mailbox_id[%d] output_mailbox[%p]", input_mailbox_id, output_mailbox);
5645                 return  EMAIL_ERROR_INVALID_PARAM;
5646         }
5647
5648         int  ret = EMAIL_ERROR_NONE;
5649         int  result_count = 0;
5650         char conditional_clause_string[QUERY_SIZE] = {0, };
5651
5652         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE MBT.mailbox_id = %d", input_mailbox_id);
5653
5654         EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5655
5656         if ((ret = emstorage_query_mailbox_tbl(multi_user_name,
5657                                         conditional_clause_string,
5658                                         "", true, false, output_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5659                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", ret);
5660                 goto FINISH_OFF;
5661         }
5662
5663 FINISH_OFF:
5664
5665         EM_DEBUG_FUNC_END("ret [%d]", ret);
5666         return ret;
5667 }
5668
5669 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)
5670 {
5671         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);
5672
5673         if (account_id < 0 || !keyword || !result_mailbox) {
5674                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], keyword[%s], result_mailbox[%p]", account_id, keyword, result_mailbox);
5675
5676                 if (err_code != NULL)
5677                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5678                 return false;
5679         }
5680
5681         int ret = false;
5682         int error = EMAIL_ERROR_NONE;
5683         char conditional_clause_string[QUERY_SIZE] = {0, };
5684
5685         if (account_id == 0)
5686                 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5687                                 "WHERE alias LIKE \'%%%q%%\'", keyword);
5688         else if (account_id > 0)
5689                 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5690                                 "WHERE account_id = %d AND alias LIKE \'%%%q%%\'", account_id, keyword);
5691
5692         EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5693
5694         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, result_count)) != EMAIL_ERROR_NONE) {
5695                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5696                 goto FINISH_OFF;
5697         }
5698
5699         ret = true;
5700
5701 FINISH_OFF:
5702
5703         if (err_code != NULL)
5704                 *err_code = error;
5705
5706         EM_DEBUG_FUNC_END("ret [%d]", ret);
5707         return ret;
5708 }
5709
5710 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)
5711 {
5712         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);
5713         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_id) {
5714                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_id[%p]", account_id, mailbox_type, mailbox_id);
5715                 if (err_code != NULL)
5716                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5717                 return false;
5718         }
5719
5720         int rc, ret = false;
5721         int error = EMAIL_ERROR_NONE;
5722         emstorage_account_tbl_t* account = NULL;
5723         DB_STMT hStmt = NULL;
5724         char sql_query_string[QUERY_SIZE] = {0, };
5725
5726         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5727         EMSTORAGE_START_READ_TRANSACTION(transaction);
5728
5729         /*  Check whether the account exists. */
5730         if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
5731                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5732                 goto FINISH_OFF;
5733         }
5734
5735         if (account)
5736                 emstorage_free_account(&account, 1, NULL);
5737
5738         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);
5739
5740         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5741
5742
5743         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5744
5745         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5746                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5747
5748
5749         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5750         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5751                         ("sqlite3_step fail:%d", rc));
5752
5753         if (rc == SQLITE_DONE) {
5754                 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5755                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5756                 goto FINISH_OFF;
5757         }
5758
5759         _get_stmt_field_data_int(hStmt, mailbox_id, 0);
5760
5761         ret = true;
5762
5763 FINISH_OFF:
5764         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5765         if (hStmt != NULL) {
5766                 rc = sqlite3_finalize(hStmt);
5767                 if (rc != SQLITE_OK) {
5768                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5769                         error = EMAIL_ERROR_DB_FAILURE;
5770                 }
5771         }
5772
5773         if (err_code != NULL)
5774                 *err_code = error;
5775
5776         EM_DEBUG_FUNC_END("ret [%d]", ret);
5777         return ret;
5778 }
5779
5780 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)
5781 {
5782         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);
5783         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_name) {
5784                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
5785                 if (err_code != NULL)
5786                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5787                 return false;
5788         }
5789
5790         int rc, ret = false;
5791         int error = EMAIL_ERROR_NONE;
5792         emstorage_account_tbl_t* account = NULL;
5793         DB_STMT hStmt = NULL;
5794         char sql_query_string[QUERY_SIZE] = {0, };
5795
5796         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5797         EMSTORAGE_START_READ_TRANSACTION(transaction);
5798
5799         /*  Check whether the account exists. */
5800         if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
5801                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5802                 goto FINISH_OFF;
5803         }
5804
5805         if (account)
5806                 emstorage_free_account(&account, 1, NULL);
5807
5808         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);
5809
5810         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5811
5812
5813         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5814
5815         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5816                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5817
5818
5819         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5820         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5821                         ("sqlite3_step fail:%d", rc));
5822
5823         if (rc == SQLITE_DONE) {
5824                 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5825                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5826                 goto FINISH_OFF;
5827         }
5828
5829         _get_stmt_field_data_string(hStmt, mailbox_name, 0, 0);
5830
5831         ret = true;
5832
5833 FINISH_OFF:
5834         if (hStmt != NULL) {
5835                 rc = sqlite3_finalize(hStmt);
5836                 if (rc != SQLITE_OK) {
5837                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5838                         error = EMAIL_ERROR_DB_FAILURE;
5839                 }
5840         }
5841
5842         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5843
5844         if (err_code != NULL)
5845                 *err_code = error;
5846
5847         EM_DEBUG_FUNC_END("ret [%d]", ret);
5848         return ret;
5849 }
5850
5851 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)
5852 {
5853         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);
5854         int ret = false;
5855         int error = EMAIL_ERROR_NONE;
5856         char sql_query_string[QUERY_SIZE] = {0, };
5857         char *replaced_mailbox_name = NULL;
5858
5859         if (mailbox_name) {
5860                 if (strstr(mailbox_name, "'"))
5861                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5862                 else
5863                         replaced_mailbox_name = strdup(mailbox_name);
5864         }
5865
5866         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5867
5868         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5869
5870         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5871                         "UPDATE mail_box_tbl SET"
5872                         " modifiable_yn = %d"
5873                         " WHERE account_id = %d"
5874                         " AND local_yn = %d"
5875                         " AND mailbox_name = '%s'"
5876                         , modifiable_yn
5877                         , account_id
5878                         , local_yn
5879                         , replaced_mailbox_name);
5880
5881         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5882         if (error != EMAIL_ERROR_NONE) {
5883                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5884                 goto FINISH_OFF;
5885         }
5886
5887         ret = true;
5888
5889 FINISH_OFF:
5890         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5891
5892         EM_SAFE_FREE(replaced_mailbox_name);
5893
5894         if (err_code != NULL)
5895                 *err_code = error;
5896
5897         EM_DEBUG_FUNC_END("ret [%d]", ret);
5898         return ret;
5899
5900 }
5901
5902 INTERNAL_FUNC int emstorage_update_mailbox_total_count(char *multi_user_name,
5903                 int account_id,
5904                 int input_mailbox_id,
5905                 int total_count_on_server,
5906                 int transaction,
5907                 int *err_code)
5908 {
5909         EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], total_count_on_server[%d], "
5910                         "transaction[%d], err_code[%p]",
5911                         account_id, input_mailbox_id, total_count_on_server,  transaction, err_code);
5912
5913         int ret = false;
5914         int error = EMAIL_ERROR_NONE;
5915         char sql_query_string[QUERY_SIZE] = {0, };
5916
5917         if (account_id <= 0 || input_mailbox_id <= 0) {
5918                 EM_DEBUG_EXCEPTION("account_id[%d], input_mailbox_id[%d]", account_id, input_mailbox_id);
5919                 if (err_code != NULL)
5920                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5921                 EM_DEBUG_FUNC_END("ret [%d]", ret);
5922                 return false;
5923         }
5924
5925         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5926
5927         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5928
5929         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5930                         "UPDATE mail_box_tbl SET"
5931                         " total_mail_count_on_server = %d"
5932                         " WHERE account_id = %d"
5933                         " AND mailbox_id = %d"
5934                         , total_count_on_server
5935                         , account_id
5936                         , input_mailbox_id);
5937
5938         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
5939         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5940         if (error != EMAIL_ERROR_NONE) {
5941                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5942                 goto FINISH_OFF;
5943         }
5944
5945         ret = true;
5946
5947 FINISH_OFF:
5948
5949         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5950
5951         if (err_code != NULL)
5952                 *err_code = error;
5953
5954         EM_DEBUG_FUNC_END("ret [%d]", ret);
5955         return ret;
5956
5957 }
5958
5959 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)
5960 {
5961         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);
5962
5963         if (account_id < FIRST_ACCOUNT_ID || input_mailbox_id <= 0 || !result_mailbox) {
5964                 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);
5965
5966                 if (err_code != NULL)
5967                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5968                 EM_DEBUG_FUNC_END("ret [%d]", EMAIL_ERROR_INVALID_PARAM);
5969                 return false;
5970         }
5971
5972         int rc, ret = false;
5973         int error = EMAIL_ERROR_NONE;
5974         char sql_query_string[QUERY_SIZE] = {0, };
5975         DB_STMT hStmt = NULL;
5976         int i = 0;
5977
5978         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5979         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5980
5981         if (local_yn != -1) {
5982                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5983                                 "UPDATE mail_box_tbl SET"
5984                                 "  mailbox_id = ?"
5985                                 ", mailbox_name = ?"
5986                                 ", mailbox_type = ?"
5987                                 ", alias = ?"
5988                                 ", deleted_flag = ?"
5989                                 ", modifiable_yn= ?"
5990                                 ", mail_slot_size= ?"
5991                                 ", total_mail_count_on_server = ?"
5992                                 " WHERE account_id = %d"
5993                                 " AND local_yn = %d"
5994                                 " AND mailbox_id = '%d'"
5995                                 , account_id
5996                                 , local_yn
5997                                 , input_mailbox_id);
5998         } else {
5999                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6000                                 "UPDATE mail_box_tbl SET"
6001                                 "  mailbox_id = ?"
6002                                 ", mailbox_name = ?"
6003                                 ", mailbox_type = ?"
6004                                 ", alias = ?"
6005                                 ", deleted_flag = ?"
6006                                 ", modifiable_yn= ?"
6007                                 ", mail_slot_size= ?"
6008                                 ", total_mail_count_on_server = ?"
6009                                 " WHERE account_id = %d"
6010                                 " AND mailbox_id = '%d'"
6011                                 , account_id
6012                                 , input_mailbox_id);
6013         }
6014
6015
6016
6017         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6018         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6019                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6020
6021         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_id);
6022         _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);
6023         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_type);
6024         _bind_stmt_field_data_string(hStmt, i++, (char *)result_mailbox->alias ? result_mailbox->alias : "", 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
6025         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->deleted_flag);
6026         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->modifiable_yn);
6027         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mail_slot_size);
6028         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->total_mail_count_on_server);
6029
6030
6031         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6032         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
6033                         ("sqlite3_step fail:%d", rc));
6034         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6035                         ("sqlite3_step fail:%d", rc));
6036
6037         ret = true;
6038
6039 FINISH_OFF:
6040         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6041
6042         if (hStmt != NULL) {
6043                 rc = sqlite3_finalize(hStmt);
6044                 if (rc != SQLITE_OK) {
6045                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6046                         error = EMAIL_ERROR_DB_FAILURE;
6047                 }
6048         }
6049
6050         if (err_code != NULL)
6051                 *err_code = error;
6052
6053         EM_DEBUG_FUNC_END("ret [%d]", ret);
6054         return ret;
6055 }
6056
6057 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)
6058 {
6059         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);
6060
6061         if (account_id < FIRST_ACCOUNT_ID) {
6062                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
6063
6064                 if (err_code != NULL)
6065                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6066                 return false;
6067         }
6068
6069         int rc, ret = false;
6070         int error = EMAIL_ERROR_NONE;
6071         char sql_query_string[QUERY_SIZE] = {0, };
6072
6073         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6074         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6075
6076         EM_DEBUG_LOG("emstorage_update_mailbox_type");
6077
6078         DB_STMT hStmt_box_tbl = NULL;
6079         DB_STMT hStmt_mail_tbl = NULL;
6080         int i = 0;
6081
6082         /*  Update mail_box_tbl */
6083         if (local_yn != -1) {
6084                 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
6085                                 "UPDATE mail_box_tbl SET"
6086                                 " mailbox_type = ?"
6087                                 " WHERE account_id = %d"
6088                                 " AND local_yn = %d"
6089                                 " AND mailbox_id = '%d'"
6090                                 , account_id
6091                                 , local_yn
6092                                 , input_mailbox_id);
6093         } else {
6094                 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
6095                                 "UPDATE mail_box_tbl SET"
6096                                 " mailbox_type = ?"
6097                                 " WHERE account_id = %d"
6098                                 " AND mailbox_id = '%d'"
6099                                 , account_id
6100                                 , input_mailbox_id);
6101         }
6102
6103         EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6104
6105         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_box_tbl, NULL), rc);
6106
6107         if (SQLITE_OK != rc) {
6108                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6109                 error = EMAIL_ERROR_DB_FAILURE;
6110                 goto FINISH_OFF;
6111         }
6112
6113         _bind_stmt_field_data_int(hStmt_box_tbl, i++, new_mailbox_type);
6114
6115
6116         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_box_tbl), rc);
6117
6118         if (rc == SQLITE_FULL) {
6119                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6120                 error   = EMAIL_ERROR_MAIL_MEMORY_FULL;
6121                 goto FINISH_OFF;
6122         }
6123
6124         if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
6125                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6126                 error = EMAIL_ERROR_DB_FAILURE;
6127                 goto FINISH_OFF;
6128         }
6129
6130
6131         /*  Update mail_tbl */
6132         i = 0;
6133         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6134                         "UPDATE mail_tbl SET"
6135                         " mailbox_type = ?"
6136                         " WHERE account_id = %d"
6137                         " AND mailbox_id = '%d'"
6138                         , account_id
6139                         , input_mailbox_id);
6140
6141         EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
6142
6143         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_mail_tbl, NULL), rc);
6144         if (SQLITE_OK != rc) {
6145                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6146                 error = EMAIL_ERROR_DB_FAILURE;
6147                 goto FINISH_OFF;
6148         }
6149
6150         _bind_stmt_field_data_int(hStmt_mail_tbl, i++, new_mailbox_type);
6151
6152         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_mail_tbl), rc);
6153         if (rc == SQLITE_FULL) {
6154                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6155                 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
6156                 goto FINISH_OFF;
6157         }
6158
6159         if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
6160                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6161                 error = EMAIL_ERROR_DB_FAILURE;
6162                 goto FINISH_OFF;
6163         }
6164
6165         ret = true;
6166
6167 FINISH_OFF:
6168         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6169
6170         if (hStmt_box_tbl != NULL) {
6171                 rc = sqlite3_finalize(hStmt_box_tbl);
6172                 if (rc != SQLITE_OK) {
6173                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6174                         error = EMAIL_ERROR_DB_FAILURE;
6175                 }
6176         }
6177
6178         if (hStmt_mail_tbl != NULL) {
6179                 rc = sqlite3_finalize(hStmt_mail_tbl);
6180                 if (rc != SQLITE_OK) {
6181                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6182                         error = EMAIL_ERROR_DB_FAILURE;
6183                 }
6184         }
6185
6186         if (err_code != NULL)
6187                 *err_code = error;
6188
6189         EM_DEBUG_FUNC_END("ret [%d]", ret);
6190         return ret;
6191 }
6192
6193 INTERNAL_FUNC int emstorage_set_local_mailbox(char *multi_user_name, int input_mailbox_id, int input_is_local_mailbox, int transaction)
6194 {
6195         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);
6196
6197         int rc, ret = false;
6198         int error = EMAIL_ERROR_NONE;
6199         char sql_query_string[QUERY_SIZE] = {0, };
6200
6201         if (input_mailbox_id < 0) {
6202                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6203                 return EMAIL_ERROR_INVALID_PARAM;
6204         }
6205
6206         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6207
6208         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6209
6210         EM_DEBUG_LOG("emstorage_update_mailbox_type");
6211
6212         DB_STMT hStmt = NULL;
6213         int i = 0;
6214
6215         /*  Update mail_box_tbl */
6216         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6217                         "UPDATE mail_box_tbl SET"
6218                         " local_yn = ?"
6219                         " WHERE mailbox_id = %d"
6220                         , input_mailbox_id);
6221
6222         EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6223
6224         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6225         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6226                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6227
6228         _bind_stmt_field_data_int(hStmt, i++, input_is_local_mailbox);
6229
6230         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6231         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
6232                         ("sqlite3_step fail:%d", rc));
6233         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6234                         ("sqlite3_step fail:%d", rc));
6235         /*
6236            if (hStmt != NULL) {
6237            rc = sqlite3_finalize(hStmt);
6238            if (rc != SQLITE_OK) {
6239            EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6240            error = EMAIL_ERROR_DB_FAILURE;
6241            }
6242            hStmt = NULL;
6243            }
6244            */
6245         ret = true;
6246
6247 FINISH_OFF:
6248         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6249
6250         if (hStmt != NULL) {
6251                 rc = sqlite3_finalize(hStmt);
6252                 if (rc != SQLITE_OK) {
6253                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6254                         error = EMAIL_ERROR_DB_FAILURE;
6255                 }
6256         }
6257
6258         EM_DEBUG_FUNC_END("error [%d]", error);
6259         return error;
6260 }
6261
6262 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)
6263 {
6264         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);
6265         int i = 0;
6266         int err = EMAIL_ERROR_NONE;
6267         int result = false;
6268         int cur_mailbox_id_string = 0;
6269         int mailbox_id_string_buffer_length = 0;
6270         char  sql_query_string[QUERY_SIZE] = {0, };
6271         char *mailbox_id_string_buffer = NULL;
6272         char *parameter_string = NULL;
6273         sqlite3 *local_db_handle = NULL;
6274
6275         if (input_mailbox_id_array == NULL || input_mailbox_id_count == 0 || input_field_name == NULL) {
6276                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6277                 return EMAIL_ERROR_INVALID_PARAM;
6278         }
6279
6280         local_db_handle = emstorage_get_db_connection(multi_user_name);
6281
6282         /* Generating mail id list string */
6283         mailbox_id_string_buffer_length = MAILBOX_ID_STRING_LENGTH * input_mailbox_id_count;
6284
6285         mailbox_id_string_buffer = em_malloc(mailbox_id_string_buffer_length);
6286
6287         if (!mailbox_id_string_buffer) {
6288                 EM_DEBUG_EXCEPTION("em_mallocfailed");
6289                 err = EMAIL_ERROR_OUT_OF_MEMORY;
6290                 goto FINISH_OFF;
6291         }
6292
6293         for (i = 0; i < input_mailbox_id_count; i++)
6294                 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]);
6295
6296         if (EM_SAFE_STRLEN(mailbox_id_string_buffer) > 1)
6297                 mailbox_id_string_buffer[EM_SAFE_STRLEN(mailbox_id_string_buffer) - 1] = NULL_CHAR;
6298
6299         /* Generating notification parameter string */
6300         parameter_string = em_malloc(mailbox_id_string_buffer_length + EM_SAFE_STRLEN(input_field_name) + 2);
6301
6302         if (!parameter_string) {
6303                 EM_DEBUG_EXCEPTION("em_mallocfailed");
6304                 err = EMAIL_ERROR_OUT_OF_MEMORY;
6305                 goto FINISH_OFF;
6306         }
6307
6308         SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", input_field_name, 0x01, mailbox_id_string_buffer);
6309
6310         /* Write query string */
6311         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);
6312
6313         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
6314
6315         /* Execute query */
6316         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
6317         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6318         if (err != EMAIL_ERROR_NONE) {
6319                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
6320                 goto FINISH_OFF;
6321         }
6322
6323         if (sqlite3_changes(local_db_handle) == 0)
6324                 EM_DEBUG_LOG("no mail matched...");
6325
6326         result = true;
6327
6328 FINISH_OFF:
6329         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, err);
6330
6331         if (err == EMAIL_ERROR_NONE && parameter_string) {
6332                 if (!emcore_notify_storage_event(NOTI_MAILBOX_FIELD_UPDATE, input_account_id, 0, parameter_string, input_value))
6333                         EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_MAILBOX_FIELD_UPDATE [%s,%d]",
6334                                         input_field_name, input_value);
6335         }
6336
6337         EM_SAFE_FREE(mailbox_id_string_buffer);
6338         EM_SAFE_FREE(parameter_string);
6339
6340         EM_DEBUG_FUNC_END("err [%d]", err);
6341         return err;
6342 }
6343
6344 INTERNAL_FUNC int emstorage_add_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox_tbl, int transaction, int *err_code)
6345 {
6346         EM_DEBUG_FUNC_BEGIN("mailbox_tbl[%p], transaction[%d], err_code[%p]", mailbox_tbl, transaction, err_code);
6347
6348         if (!mailbox_tbl) {
6349                 if (err_code != NULL)
6350                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6351                 return false;
6352         }
6353
6354         int rc, ret = false;
6355         int error = EMAIL_ERROR_NONE;
6356         DB_STMT hStmt = NULL;
6357         char sql_query_string[QUERY_SIZE] = {0,};
6358         char **result = NULL;
6359         time_t current_time;
6360         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6361         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6362
6363         EM_SAFE_STRNCPY(sql_query_string, "SELECT max(rowid) FROM mail_box_tbl;", sizeof(sql_query_string));
6364
6365         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6366         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6367                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6368
6369         time(&current_time);
6370
6371         if (NULL == result[1])
6372                 rc = 1;
6373         else
6374                 rc = atoi(result[1]) + 1;
6375         sqlite3_free_table(result);
6376
6377         memset(sql_query_string, 0, sizeof(char) * QUERY_SIZE);
6378
6379         mailbox_tbl->mailbox_id = rc;
6380
6381         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6382                         "INSERT INTO mail_box_tbl VALUES "
6383                         "(?"    /* mailbox_id */
6384                         ", ?"    /* account_id */
6385                         ", ?"    /* local_yn */
6386                         ", ?"    /* mailbox_name */
6387                         ", ?"    /* mailbox_type */
6388                         ", ?"    /* alias */
6389                         ", ?"    /* deleted_flag */
6390                         ", ?"    /* modifiable_yn */
6391                         ", ?"    /* total_mail_count_on_server */
6392                         ", ?"    /* has_archived_mails */
6393                         ", ?"    /* mail_slot_size */
6394                         ", ?"    /* no_select */
6395                         ", ?"    /* last_sync_time */
6396                         ", ?"    /* eas_data_length */
6397                         ", ?"    /* eas_data */
6398                         ")");
6399
6400
6401         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6402         EM_DEBUG_LOG_DEV("After sqlite3_prepare hStmt = %p", hStmt);
6403         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6404                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6405
6406         int col_index = 0;
6407
6408         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_id);
6409         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->account_id);
6410         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->local_yn);
6411         _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
6412         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_type);
6413         _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->alias, 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
6414         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->deleted_flag);
6415         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->modifiable_yn);
6416         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->total_mail_count_on_server);
6417         _bind_stmt_field_data_int(hStmt, col_index++, 0);
6418         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mail_slot_size);
6419         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->no_select);
6420         _bind_stmt_field_data_int(hStmt, col_index++, current_time);
6421         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->eas_data_length);
6422         _bind_stmt_field_data_blob(hStmt, col_index++, (void*)mailbox_tbl->eas_data, mailbox_tbl->eas_data_length);
6423
6424
6425         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6426         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6427                         ("sqlite3_step fail:%dn", rc));
6428
6429         ret = true;
6430
6431 FINISH_OFF:
6432         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6433         if (hStmt != NULL) {
6434                 rc = sqlite3_finalize(hStmt);
6435                 if (rc != SQLITE_OK) {
6436                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6437                         error = EMAIL_ERROR_DB_FAILURE;
6438                 }
6439         }
6440
6441         if (error == EMAIL_ERROR_NONE) {
6442                 if (!emcore_notify_storage_event(NOTI_MAILBOX_ADD, mailbox_tbl->account_id, mailbox_tbl->mailbox_id,
6443                                         mailbox_tbl->mailbox_name, mailbox_tbl->mailbox_type))
6444                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6445         }
6446
6447         if (err_code != NULL)
6448                 *err_code = error;
6449
6450         EM_DEBUG_FUNC_END("ret [%d]", ret);
6451         return ret;
6452 }
6453
6454 INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(char *multi_user_name, int account_id, int modifiable_yn, int transaction, int *err_code)
6455 {
6456         EM_DEBUG_FUNC_BEGIN("account_id[%d], modifiable_yn[%d], err_code[%p]", account_id, modifiable_yn, err_code);
6457
6458         if (account_id < FIRST_ACCOUNT_ID) {
6459
6460                 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
6461
6462                 if (err_code != NULL)
6463                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6464                 return false;
6465         }
6466         int rc, ret = false;
6467         int error = EMAIL_ERROR_NONE;
6468         char sql_query_string[QUERY_SIZE] = {0,};
6469         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6470         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6471
6472
6473         SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET modifiable_yn = %d WHERE account_id = %d", modifiable_yn, account_id);
6474
6475         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6476         if (error != EMAIL_ERROR_NONE) {
6477                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6478                 goto FINISH_OFF;
6479         }
6480
6481         rc = sqlite3_changes(local_db_handle);
6482         if (rc == 0)
6483                 EM_DEBUG_EXCEPTION("All mailbox_name modifiable_yn set to 0 already");
6484
6485
6486         ret = true;
6487
6488 FINISH_OFF:
6489         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6490
6491         if (err_code != NULL)
6492                 *err_code = error;
6493         EM_DEBUG_FUNC_END("ret [%d]", ret);
6494         return ret;
6495
6496
6497 }
6498
6499 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)
6500 {
6501         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);
6502
6503         if (account_id < FIRST_ACCOUNT_ID) {
6504                 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
6505
6506                 if (err_code != NULL)
6507                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6508                 return false;
6509         }
6510
6511         int rc, ret = false;
6512         int error = EMAIL_ERROR_NONE;
6513         char sql_query_string[QUERY_SIZE] = {0, };
6514         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6515         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6516
6517         if (local_yn == -1)
6518                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_box_tbl WHERE account_id = %d ", account_id);
6519         else
6520                 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);
6521
6522         if (input_mailbox_id > 0) {             /* 0 means all mailbox */
6523                 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);
6524         }
6525
6526         EM_DEBUG_LOG_SEC("mailbox sql_query_string [%s]", sql_query_string);
6527         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6528         if (error != EMAIL_ERROR_NONE) {
6529                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6530                 goto FINISH_OFF;
6531         }
6532
6533         rc = sqlite3_changes(local_db_handle);
6534         if (rc == 0) {
6535                 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
6536                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
6537                 ret = true;
6538         }
6539         ret = true;
6540
6541 FINISH_OFF:
6542
6543         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6544
6545         if (error == EMAIL_ERROR_NONE) {
6546                 if (!emcore_notify_storage_event(NOTI_MAILBOX_DELETE, account_id, input_mailbox_id, NULL, 0))
6547                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6548         }
6549
6550         if (err_code != NULL)
6551                 *err_code = error;
6552
6553         EM_DEBUG_FUNC_END("ret [%d]", ret);
6554         return ret;
6555 }
6556
6557 INTERNAL_FUNC int emstorage_free_mailbox(emstorage_mailbox_tbl_t** mailbox_list, int count, int *err_code)
6558 {
6559         EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d], err_code[%p]", mailbox_list, count, err_code);
6560
6561         int ret = false;
6562         int error = EMAIL_ERROR_NONE;
6563
6564         if (count > 0) {
6565                 if (!mailbox_list || !*mailbox_list) {
6566                         EM_DEBUG_EXCEPTION(" mailbox_list[%p], count[%d]", mailbox_list, count);
6567
6568                         error = EMAIL_ERROR_INVALID_PARAM;
6569                         goto FINISH_OFF;
6570                 }
6571
6572                 emstorage_mailbox_tbl_t* p = *mailbox_list;
6573                 int i = 0;
6574
6575                 for (; i < count; i++) {
6576                         EM_SAFE_FREE(p[i].mailbox_name);
6577                         EM_SAFE_FREE(p[i].alias);
6578                         EM_SAFE_FREE(p[i].eas_data); /*valgrind*/
6579                 }
6580
6581                 EM_SAFE_FREE(p); *mailbox_list = NULL;
6582         }
6583
6584         ret = true;
6585
6586 FINISH_OFF:
6587         if (err_code != NULL)
6588                 *err_code = error;
6589
6590         EM_DEBUG_FUNC_END("ret [%d]", ret);
6591         return ret;
6592 }
6593
6594 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)
6595 {
6596         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);
6597
6598         if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !count) {
6599                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%s], count[%d] ", account_id, mailbox_name, *count);
6600
6601                 if (err_code != NULL)
6602                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6603                 return false;
6604         }
6605
6606         int rc = -1, ret = false;
6607         int error = EMAIL_ERROR_NONE;
6608         char sql_query_string[QUERY_SIZE] = {0, };
6609         char *replaced_mailbox_name = NULL;
6610
6611         if (strstr(mailbox_name, "'"))
6612                 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6613         else
6614                 replaced_mailbox_name = EM_SAFE_STRDUP(mailbox_name);
6615
6616         EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6617
6618
6619         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6620         EMSTORAGE_START_READ_TRANSACTION(transaction);
6621         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);
6622         EM_DEBUG_LOG_SEC(">>> SQL [ %s ] ", sql_query_string);
6623
6624         char **result;
6625
6626         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6627         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6628                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6629
6630         *count = atoi(result[1]);
6631         sqlite3_free_table(result);
6632
6633         ret = true;
6634
6635 FINISH_OFF:
6636         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6637
6638         EM_SAFE_FREE(replaced_mailbox_name);
6639
6640         if (err_code != NULL)
6641                 *err_code = error;
6642
6643         EM_DEBUG_FUNC_END("ret [%d]", ret);
6644         return ret;
6645 }
6646
6647 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)
6648 {
6649         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);
6650
6651         if (account_id < FIRST_ACCOUNT_ID || !uid || !exist) {
6652                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], uid[%p], exist[%p]", account_id, mailbox_name , uid, exist);
6653
6654                 if (err_code != NULL)
6655                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6656                 return false;
6657         }
6658
6659         int rc = -1, ret = false;
6660         int error = EMAIL_ERROR_NONE;
6661         char sql_query_string[QUERY_SIZE] = {0, };
6662         char *replaced_mailbox_name = NULL;
6663
6664         EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6665
6666         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6667         EMSTORAGE_START_READ_TRANSACTION(transaction);
6668
6669         if (mailbox_name) {
6670                 if (strstr(mailbox_name, "'"))
6671                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6672                 else
6673                         replaced_mailbox_name = strdup(mailbox_name);
6674
6675                 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);
6676         } else {
6677                 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);
6678         }
6679
6680         char **result = NULL;
6681
6682         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6683         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6684                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6685
6686         *exist = atoi(result[1]);
6687         sqlite3_free_table(result);
6688
6689         if (*exist > 0)
6690                 *exist = 1;
6691         else
6692                 *exist = 0;
6693
6694         ret = true;
6695
6696 FINISH_OFF:
6697         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6698
6699         EM_SAFE_FREE(replaced_mailbox_name);
6700
6701         if (err_code != NULL)
6702                 *err_code = error;
6703
6704         EM_DEBUG_FUNC_END("ret [%d]", ret);
6705         return ret;
6706 }
6707
6708 INTERNAL_FUNC int emstorage_get_downloaded_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code)
6709 {
6710         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], err_code[%p]", mail_id, mail, err_code);
6711
6712         if (!mail || mail_id <= 0) {
6713                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
6714                 if (err_code != NULL)
6715                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6716                 return false;
6717         }
6718
6719         int rc, ret = false;
6720         int error = EMAIL_ERROR_NONE;
6721         DB_STMT hStmt = NULL;
6722         char sql_query_string[QUERY_SIZE] = {0, };
6723
6724         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6725         EMSTORAGE_START_READ_TRANSACTION(transaction);
6726
6727         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE local_uid = %d", mail_id);
6728
6729         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6730         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6731
6732         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6733                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6734
6735
6736         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6737         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6738                         ("sqlite3_step fail:%d", rc));
6739
6740         *mail = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
6741         if (*mail == NULL) {
6742                 error = EMAIL_ERROR_OUT_OF_MEMORY;
6743                 EM_DEBUG_EXCEPTION("Memory allocation for mail failed.");
6744                 goto FINISH_OFF;
6745
6746         }
6747         memset(*mail, 0x00, sizeof(emstorage_mail_tbl_t));
6748
6749         _get_stmt_field_data_int(hStmt, &((*mail)->account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6750         _get_stmt_field_data_int(hStmt, &((*mail)->mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6751         _get_stmt_field_data_string(hStmt, &((*mail)->server_mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6752         _get_stmt_field_data_int(hStmt, &((*mail)->mail_id), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6753         _get_stmt_field_data_string(hStmt, &((*mail)->server_mail_id), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6754         _get_stmt_field_data_int(hStmt, &((*mail)->mail_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6755         _get_stmt_field_data_char(hStmt, &((*mail)->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6756
6757         (*mail)->server_mail_status = 1;
6758
6759         ret = true;
6760
6761 FINISH_OFF:
6762
6763         if (hStmt != NULL) {
6764                 rc = sqlite3_finalize(hStmt);
6765                 if (rc != SQLITE_OK) {
6766                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6767                         error = EMAIL_ERROR_DB_FAILURE;
6768                 }
6769         }
6770
6771         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6772
6773         if (err_code != NULL)
6774                 *err_code = error;
6775
6776         EM_DEBUG_FUNC_END("ret [%d]", ret);
6777         return ret;
6778 }
6779
6780 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)
6781 {
6782         EM_PROFILE_BEGIN(emStorageGetDownloadList);
6783         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);
6784         if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid || !count) {
6785                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_id[%d], read_mail_uid[%p], count[%p]", account_id, mailbox_id, read_mail_uid, count);
6786
6787                 if (err_code != NULL)
6788                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6789                 return false;
6790         }
6791
6792         int rc, ret = false;
6793         int error = EMAIL_ERROR_NONE;
6794
6795         DB_STMT hStmt = NULL;
6796         char sql_query_string[QUERY_SIZE] = {0, };
6797
6798         emstorage_read_mail_uid_tbl_t* p_data_tbl = NULL;
6799         int i = 0;
6800
6801         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6802         EMSTORAGE_START_READ_TRANSACTION(transaction);
6803
6804         if (mailbox_id)
6805                 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);
6806         else
6807                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
6808
6809         EM_DEBUG_LOG_SEC(" sql_query_string : %s", sql_query_string);
6810
6811
6812
6813         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6814         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6815         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6816                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6817
6818
6819         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6820         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6821                         ("sqlite3_step fail:%d", rc));
6822
6823         char **result;
6824         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL); */
6825         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL), rc);
6826         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6827                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6828
6829         sqlite3_free_table(result);
6830         if (*count == 0) {
6831                 EM_DEBUG_LOG("No mail found in mail_read_mail_uid_tbl");
6832                 ret = true;
6833                 goto FINISH_OFF;
6834         }
6835
6836
6837         if (!(p_data_tbl = (emstorage_read_mail_uid_tbl_t*)malloc(sizeof(emstorage_read_mail_uid_tbl_t) * *count))) {
6838                 EM_DEBUG_EXCEPTION(" malloc failed...");
6839                 error = EMAIL_ERROR_OUT_OF_MEMORY;
6840                 goto FINISH_OFF;
6841         }
6842
6843         memset(p_data_tbl, 0x00, sizeof(emstorage_read_mail_uid_tbl_t)*(*count));
6844
6845         for (i = 0; i < *count; ++i) {
6846                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6847                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6848                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6849                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].local_uid), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6850                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].server_uid), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6851                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rfc822_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6852                 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6853                 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_flagged_field), FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6854
6855                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6856                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6857                                 ("sqlite3_step fail:%d", rc));
6858         }
6859
6860         ret = true;
6861
6862 FINISH_OFF:
6863         if (ret == true)
6864                 *read_mail_uid = p_data_tbl;
6865         else if (p_data_tbl)
6866                 emstorage_free_read_mail_uid(&p_data_tbl, *count, NULL);
6867
6868         if (hStmt != NULL) {
6869                 rc = sqlite3_finalize(hStmt);
6870                 if (rc != SQLITE_OK) {
6871                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6872                         error = EMAIL_ERROR_DB_FAILURE;
6873                 }
6874         }
6875
6876         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6877
6878         if (err_code != NULL)
6879                 *err_code = error;
6880
6881         EM_PROFILE_END(emStorageGetDownloadList);
6882         EM_DEBUG_FUNC_END("ret [%d]", ret);
6883         return ret;
6884 }
6885
6886 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)
6887 {
6888         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);
6889
6890         if (account_id < FIRST_ACCOUNT_ID || !mail_size) {
6891                 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);
6892
6893                 if (err_code != NULL)
6894                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6895                 return false;
6896         }
6897
6898         int rc, ret = false;
6899         int error = EMAIL_ERROR_NONE;
6900         DB_STMT hStmt = NULL;
6901         char sql_query_string[QUERY_SIZE] = {0, };
6902         char *replaced_mailbox_name = NULL;
6903
6904         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6905         EMSTORAGE_START_READ_TRANSACTION(transaction);
6906
6907         if (mailbox_name) {
6908                 if (strstr(mailbox_name, "'"))
6909                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6910                 else
6911                         replaced_mailbox_name = strdup(mailbox_name);
6912
6913                 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6914
6915                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6916                                 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6917                                 "WHERE account_id = %d "
6918                                 "AND mailbox_id = '%s' "
6919                                 "AND local_uid = %d "
6920                                 "AND mailbox_name = '%s' "
6921                                 "AND server_uid = '%s'",
6922                                 account_id, mailbox_id, local_uid, replaced_mailbox_name, uid);
6923         } else {
6924                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6925                                 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6926                                 "WHERE account_id = %d "
6927                                 "AND mailbox_id = '%s' "
6928                                 "AND local_uid = %d "
6929                                 "AND server_uid = '%s'",
6930                                 account_id, mailbox_id, local_uid, uid);
6931         }
6932
6933
6934         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6935         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6936                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6937
6938
6939         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6940         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6941                         ("sqlite3_step fail:%d", rc));
6942
6943         if (rc == SQLITE_DONE) {
6944                 EM_DEBUG_LOG("no matched mail found....");
6945                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
6946                 goto FINISH_OFF;
6947         }
6948
6949         _get_stmt_field_data_int(hStmt, mail_size, 0);
6950
6951         ret = true;
6952
6953 FINISH_OFF:
6954         EM_SAFE_FREE(replaced_mailbox_name);
6955
6956         if (hStmt != NULL) {
6957                 rc = sqlite3_finalize(hStmt);
6958                 if (rc != SQLITE_OK) {
6959                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6960                         error = EMAIL_ERROR_DB_FAILURE;
6961                 }
6962         }
6963
6964         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6965
6966         if (err_code != NULL)
6967                 *err_code = error;
6968
6969         EM_DEBUG_FUNC_END("ret [%d]", ret);
6970         return ret;
6971 }
6972
6973 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)
6974 {
6975         EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], transaction[%d], err_code[%p]", read_mail_uid, transaction, err_code);
6976
6977         if (!read_mail_uid) {
6978                 EM_DEBUG_EXCEPTION("read_mail_uid[%p]", read_mail_uid);
6979                 if (err_code != NULL)
6980                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6981                 return false;
6982         }
6983
6984         int rc, rc2,  ret = false;
6985         int error = EMAIL_ERROR_NONE;
6986         DB_STMT hStmt = NULL;
6987         char sql_query_string[QUERY_SIZE] = {0, };
6988
6989         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6990         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6991
6992         char *sql = "SELECT max(rowid) FROM mail_read_mail_uid_tbl;";
6993         char **result = NULL;
6994
6995
6996         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6997         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6998                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6999
7000         if (NULL == result[1]) rc = 1;
7001         else rc = atoi(result[1])+1;
7002         sqlite3_free_table(result);
7003
7004         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7005                         "INSERT INTO mail_read_mail_uid_tbl VALUES "
7006                         "(?"  /* account_id */
7007                         ", ?"  /* mailbox_id */
7008                         ", ?"  /* mailbox_name */
7009                         ", ?"  /* local_uid */
7010                         ", ?"  /* server_uid */
7011                         ", ?"  /* rfc822_size */
7012                         ", ?"  /* sync_status */
7013                         ", ?"  /* flags_seen_field */
7014                         ", ?"  /* flags_flagged_field */
7015                         ", ?)");
7016
7017
7018         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
7019         if (rc2 != SQLITE_OK) {
7020                 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7021                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
7022
7023                 error = EMAIL_ERROR_DB_FAILURE;
7024                 goto FINISH_OFF;
7025         }
7026
7027         EM_DEBUG_LOG("account_id[%d] mailbox_id[%d] local_uid [%d]"
7028                         "server_uid[%s] rfc822_size[%d] rc[%d]",
7029                         read_mail_uid->account_id, read_mail_uid->mailbox_id, read_mail_uid->local_uid,
7030                         read_mail_uid->server_uid, read_mail_uid->rfc822_size, rc);
7031
7032         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->account_id);
7033         _bind_stmt_field_data_int(hStmt, LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->mailbox_id);
7034         _bind_stmt_field_data_int(hStmt, LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->local_uid);
7035         _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);
7036         _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);
7037         _bind_stmt_field_data_int(hStmt, RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->rfc822_size);
7038         _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_seen_field);
7039         _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_flagged_field);
7040         _bind_stmt_field_data_int(hStmt, IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, rc);
7041
7042
7043         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7044         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
7045                         ("sqlite3_step fail:%d", rc));
7046         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7047                         ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
7048
7049
7050         ret = true;
7051
7052 FINISH_OFF:
7053         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7054         if (hStmt != NULL) {
7055                 rc = sqlite3_finalize(hStmt);
7056                 if (rc != SQLITE_OK) {
7057                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7058                         error = EMAIL_ERROR_DB_FAILURE;
7059                 }
7060         }
7061
7062         if (err_code != NULL)
7063                 *err_code = error;
7064
7065         EM_DEBUG_FUNC_END("ret [%d]", ret);
7066         return ret;
7067 }
7068
7069 #ifdef __FEATURE_BODY_SEARCH__
7070 INTERNAL_FUNC int emstorage_add_mail_text(char *multi_user_name, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
7071 {
7072         EM_DEBUG_FUNC_BEGIN("mail_text[%p], transaction[%d], err_code[%p]", mail_text, transaction, err_code);
7073
7074         if (!mail_text) {
7075                 EM_DEBUG_EXCEPTION("mail_text[%p]", mail_text);
7076                 if (err_code != NULL)
7077                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7078                 return false;
7079         }
7080
7081         int rc, rc2,  ret = false;
7082         int error = EMAIL_ERROR_NONE;
7083         DB_STMT hStmt = NULL;
7084         char sql_query_string[QUERY_SIZE] = {0, };
7085
7086         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7087         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7088
7089         char *sql = "SELECT max(rowid) FROM mail_text_tbl;";
7090         char **result = NULL;
7091
7092         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7093         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7094                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7095         sqlite3_free_table(result);
7096
7097         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7098                         "INSERT INTO mail_text_tbl VALUES "
7099                         "(?"
7100                         ", ?"
7101                         ", ?"
7102                         ", ?)");
7103
7104         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
7105         if (rc2 != SQLITE_OK) {
7106                 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7107                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc2, sqlite3_errmsg(local_db_handle));
7108
7109                 error = EMAIL_ERROR_DB_FAILURE;
7110                 goto FINISH_OFF;
7111         }
7112
7113         EM_DEBUG_LOG("mail_id[%d] account_id[%d] mailbox_id[%d]", mail_text->mail_id,
7114                         mail_text->account_id, mail_text->mailbox_id);
7115         EM_DEBUG_LOG_DEV("body_text VALUE [%s] ", mail_text->body_text);
7116
7117         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mail_id);
7118         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->account_id);
7119         _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mailbox_id);
7120         _bind_stmt_field_data_string(hStmt, BODY_TEXT_IDX_IN_MAIL_TEXT_TBL, (char *)mail_text->body_text, 0, -1);
7121
7122         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7123         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
7124                         ("sqlite3_step fail:%d", rc));
7125         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7126                         ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
7127
7128         ret = true;
7129
7130 FINISH_OFF:
7131         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7132         if (hStmt != NULL) {
7133                 rc = sqlite3_finalize(hStmt);
7134                 if (rc != SQLITE_OK) {
7135                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7136                         error = EMAIL_ERROR_DB_FAILURE;
7137                 }
7138         }
7139
7140         if (err_code != NULL)
7141                 *err_code = error;
7142
7143         EM_DEBUG_FUNC_END("ret [%d]", ret);
7144         return ret;
7145 }
7146 #endif
7147
7148 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)
7149 {
7150         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);
7151
7152         if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid) {
7153                 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);
7154
7155                 if (err_code != NULL)
7156                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7157                 return false;
7158         }
7159
7160         int rc, ret = false;
7161         int error = EMAIL_ERROR_NONE;
7162         DB_STMT hStmt = NULL;
7163         char sql_query_string[QUERY_SIZE] = {0, };
7164
7165         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7166
7167         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7168
7169         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7170                         "UPDATE mail_read_mail_uid_tbl SET"
7171                         "  account_id = ?"
7172                         ", mailbox_id = ?"
7173                         ", mailbox_name = ?"
7174                         ", local_uid  = ?"
7175                         ", server_uid = ?"
7176                         ", rfc822_size = ?"
7177                         ", flags_seen_field  = ?"
7178                         ", flags_flagged_field  = ?"
7179                         " WHERE account_id = ?"
7180                         " AND mailbox_id  = ?"
7181                         " AND local_uid   = ?"
7182                         " AND mailbox_name= ?"
7183                         " AND server_uid = ?");
7184
7185
7186         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7187         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7188         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7189                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7190
7191
7192         int i = 0;
7193
7194         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->account_id);
7195         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->mailbox_id);
7196         _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7197         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->local_uid);
7198         _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->server_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7199         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->rfc822_size);
7200         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_seen_field);
7201         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_flagged_field);
7202         _bind_stmt_field_data_int(hStmt, i++, account_id);
7203         _bind_stmt_field_data_int(hStmt, i++, mailbox_id);
7204         _bind_stmt_field_data_int(hStmt, i++, local_uid);
7205         _bind_stmt_field_data_string(hStmt, i++, (char*)mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7206         _bind_stmt_field_data_string(hStmt, i++, (char*)uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7207
7208
7209         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7210         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7211                         ("sqlite3_step fail:%d", rc));
7212
7213         ret = true;
7214
7215 FINISH_OFF:
7216         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7217         if (hStmt != NULL) {
7218                 rc = sqlite3_finalize(hStmt);
7219                 if (rc != SQLITE_OK) {
7220                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7221                         error = EMAIL_ERROR_DB_FAILURE;
7222                 }
7223         }
7224
7225         if (err_code != NULL)
7226                 *err_code = error;
7227
7228         EM_DEBUG_FUNC_END("ret [%d]", ret);
7229         return ret;
7230 }
7231
7232 INTERNAL_FUNC int emstorage_remove_downloaded_mail(char *multi_user_name,
7233                 int account_id,
7234                 int mailbox_id,
7235                 char *mailbox_name,
7236                 char *uid,
7237                 int transaction,
7238                 int *err_code)
7239 {
7240         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], mailbox_id[%d], mailbox_name[%s], "
7241                         "uid[%s], transaction[%d], err_code[%p]",
7242                         account_id, mailbox_id, mailbox_name, uid, transaction, err_code);
7243
7244         if (account_id < FIRST_ACCOUNT_ID) {
7245                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_name[%s], uid[%s]", account_id, mailbox_name, uid);
7246
7247                 if (err_code != NULL)
7248                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7249                 return false;
7250         }
7251
7252         int ret = false;
7253         int error = EMAIL_ERROR_NONE;
7254         char sql_query_string[QUERY_SIZE] = {0, };
7255         char *replaced_mailbox_name = NULL;
7256         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7257
7258         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7259
7260         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7261                         "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d ", account_id);
7262
7263         if (mailbox_id > 0) {
7264                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7265                                 sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7266                                 "AND mailbox_id = %d ", mailbox_id);
7267         }
7268
7269         if (mailbox_name) {             /*  NULL means all mailbox_name */
7270                 if (strstr(mailbox_name, "'"))
7271                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
7272                 else
7273                         replaced_mailbox_name = strdup(mailbox_name);
7274
7275                 SNPRINTF(sql_query_string+EM_SAFE_STRLEN(sql_query_string),
7276                                 sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7277                                 "AND mailbox_name = '%s' ", replaced_mailbox_name);
7278         }
7279
7280         if (uid) {              /*  NULL means all mail */
7281                 sqlite3_snprintf(sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7282                                 sql_query_string + EM_SAFE_STRLEN(sql_query_string), "AND server_uid = '%q' ",
7283                                 uid);
7284         }
7285
7286         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7287         if (error != EMAIL_ERROR_NONE) {
7288                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7289                 goto FINISH_OFF;
7290         }
7291
7292         ret = true;
7293
7294 FINISH_OFF:
7295         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7296
7297         EM_SAFE_FREE(replaced_mailbox_name);
7298
7299         if (err_code != NULL)
7300                 *err_code = error;
7301
7302         EM_DEBUG_FUNC_END("ret [%d]", ret);
7303         return ret;
7304 }
7305
7306 INTERNAL_FUNC int emstorage_free_read_mail_uid(emstorage_read_mail_uid_tbl_t** read_mail_uid, int count, int *err_code)
7307 {
7308         EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], count[%d], err_code[%p]", read_mail_uid, count, err_code);
7309
7310         int ret = false;
7311         int error = EMAIL_ERROR_NONE;
7312
7313         if (count > 0) {
7314                 if (!read_mail_uid || !*read_mail_uid) {
7315                         EM_DEBUG_EXCEPTION(" read_mail_uid[%p], count[%d]", read_mail_uid, count);
7316
7317                         error = EMAIL_ERROR_INVALID_PARAM;
7318                         goto FINISH_OFF;
7319                 }
7320
7321                 emstorage_read_mail_uid_tbl_t* p = *read_mail_uid;
7322                 int i;
7323
7324                 for (i = 0; i < count; i++) {
7325                         EM_SAFE_FREE(p[i].mailbox_name);
7326                         EM_SAFE_FREE(p[i].server_uid);
7327                 }
7328
7329                 EM_SAFE_FREE(p); *read_mail_uid = NULL;
7330         }
7331
7332         ret = true;
7333
7334 FINISH_OFF:
7335         if (err_code != NULL)
7336                 *err_code = error;
7337
7338         EM_DEBUG_FUNC_END("ret [%d]", ret);
7339         return ret;
7340 }
7341
7342 INTERNAL_FUNC int emstorage_get_rule_count_by_account_id(char *multi_user_name, int account_id, int *count, int transaction, int *err_code)
7343 {
7344         EM_DEBUG_FUNC_BEGIN("account_id [%d], count[%p], transaction[%d], err_code[%p]", count, transaction, err_code);
7345
7346         if (!count) {
7347                 EM_DEBUG_EXCEPTION("count[%p]", count);
7348
7349                 if (err_code != NULL)
7350                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7351                 return false;
7352         }
7353
7354         int rc = -1, ret = false;
7355         int error =  EMAIL_ERROR_NONE;
7356         char sql_query_string[QUERY_SIZE] = {0, };
7357
7358         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7359         EMSTORAGE_START_READ_TRANSACTION(transaction);
7360
7361         if (account_id != ALL_ACCOUNT)
7362                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl where account_id = %d", account_id);
7363         else
7364                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl");
7365
7366         char **result;
7367
7368         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7369         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7370                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7371
7372         *count = atoi(result[1]);
7373         sqlite3_free_table(result);
7374
7375         ret = true;
7376
7377 FINISH_OFF:
7378         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7379
7380         if (err_code != NULL)
7381                 *err_code = error;
7382
7383         EM_DEBUG_FUNC_END("ret [%d]", ret);
7384         return ret;
7385 }
7386
7387 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)
7388 {
7389         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);
7390
7391         if (!select_num || !is_completed || !rule_list) {               /*  only global rule supported. */
7392                 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);
7393
7394                 if (err_code != NULL)
7395                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7396                 return false;
7397         }
7398
7399         int ret = false;
7400         int error = EMAIL_ERROR_NONE;
7401
7402         emstorage_rule_tbl_t* p_data_tbl = NULL;
7403         int i = 0, count = 0;
7404         DB_STMT hStmt = NULL;
7405         char sql_query_string[QUERY_SIZE] = {0, };
7406
7407         int rc;
7408
7409         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7410         EMSTORAGE_START_READ_TRANSACTION(transaction);
7411
7412         if (account_id != ALL_ACCOUNT) {
7413                 if (type)
7414                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d AND type = %d", account_id, type);
7415                 else
7416                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d ORDER BY type", account_id);
7417         } else {
7418                 if (type)
7419                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE type = %d", type);
7420                 else
7421                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl ORDER BY type");
7422         }
7423
7424         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7425         /*      EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt); */
7426         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7427                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7428
7429
7430         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7431         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7432                         ("sqlite3_step fail:%d", rc));
7433
7434         char **result;
7435         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL); */
7436         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
7437         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7438                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7439
7440         sqlite3_free_table(result);
7441
7442         if (count == 0) {
7443                 EM_DEBUG_LOG_DEV("No matching rule found...");
7444                 ret = true;
7445                 error = EMAIL_ERROR_FILTER_NOT_FOUND; /*there is no matched rule*/
7446                 goto FINISH_OFF;
7447         }
7448
7449
7450         if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t) * count))) {
7451                 EM_DEBUG_EXCEPTION(" malloc failed...");
7452
7453                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7454                 goto FINISH_OFF;
7455         }
7456
7457         memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t) * count);
7458
7459         for (i = 0; i < count; i++) {
7460                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7461                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7462                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7463                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].type), TYPE_IDX_IN_MAIL_RULE_TBL);
7464                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7465                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7466                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7467                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7468                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7469                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7470
7471                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7472                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7473                                 ("sqlite3_step fail:%d", rc));
7474         }
7475
7476         ret = true;
7477
7478 FINISH_OFF:
7479
7480         EM_DEBUG_LOG("[%d] rules found.", count);
7481
7482         if (ret == true) {
7483                 *rule_list = p_data_tbl;
7484                 *select_num = count;
7485         } else if (p_data_tbl != NULL)
7486                 emstorage_free_rule(&p_data_tbl, count, NULL); /* CID FIX */
7487
7488         if (hStmt != NULL) {
7489                 rc = sqlite3_finalize(hStmt);
7490                 if (rc != SQLITE_OK) {
7491                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7492                         error = EMAIL_ERROR_DB_FAILURE;
7493                 }
7494         }
7495
7496         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7497
7498         if (err_code != NULL)
7499                 *err_code = error;
7500
7501         EM_DEBUG_FUNC_END("ret [%d]", ret);
7502         return ret;
7503 }
7504
7505 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)
7506 {
7507         EM_DEBUG_FUNC_BEGIN("rule_id[%d], rule[%p], transaction[%d], err_code[%p]", rule_id, rule, transaction, err_code);
7508         int error = EMAIL_ERROR_NONE;
7509         int ret = false;
7510         DB_STMT hStmt = NULL;
7511
7512         if (rule_id <= 0) {
7513                 EM_DEBUG_EXCEPTION("Invalid parameter");
7514                 error = EMAIL_ERROR_INVALID_PARAM;
7515                 goto FINISH_OFF;
7516         }
7517
7518         if (!rule) {
7519                 EM_DEBUG_EXCEPTION("rule_id[%d], rule[%p]", rule_id, rule);
7520                 error = EMAIL_ERROR_INVALID_PARAM;
7521                 goto FINISH_OFF;
7522         }
7523
7524         emstorage_rule_tbl_t* p_data_tbl = NULL;
7525         int rc;
7526
7527         char sql_query_string[QUERY_SIZE] = {0, };
7528         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7529         EMSTORAGE_START_READ_TRANSACTION(transaction);
7530
7531         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7532
7533         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7534         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7535                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7536
7537
7538         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7539         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7540                         ("sqlite3_step fail:%d", rc));
7541
7542         if (rc == SQLITE_DONE) {
7543                 EM_DEBUG_EXCEPTION(" no matched rule found...");
7544                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7545                 goto FINISH_OFF;
7546         }
7547
7548         if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t)))) {
7549                 EM_DEBUG_EXCEPTION(" malloc failed...");
7550                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7551                 goto FINISH_OFF;
7552         }
7553
7554         memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t));
7555         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7556         _get_stmt_field_data_int(hStmt, &(p_data_tbl->rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7557         _get_stmt_field_data_string(hStmt, &(p_data_tbl->filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7558         _get_stmt_field_data_int(hStmt, &(p_data_tbl->type), TYPE_IDX_IN_MAIL_RULE_TBL);
7559         _get_stmt_field_data_string(hStmt, &(p_data_tbl->value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7560         _get_stmt_field_data_string(hStmt, &(p_data_tbl->value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7561         _get_stmt_field_data_int(hStmt, &(p_data_tbl->action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7562         _get_stmt_field_data_int(hStmt, &(p_data_tbl->target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7563         _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7564         _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7565
7566         ret = true;
7567
7568 FINISH_OFF:
7569
7570         if (ret == true)
7571                 *rule = p_data_tbl;
7572
7573         if (hStmt != NULL) {
7574                 rc = sqlite3_finalize(hStmt);
7575                 if (rc != SQLITE_OK) {
7576                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7577                         error = EMAIL_ERROR_DB_FAILURE;
7578                 }
7579         }
7580
7581         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7582
7583         if (err_code != NULL)
7584                 *err_code = error;
7585
7586         EM_DEBUG_FUNC_END("ret [%d]", ret);
7587         return ret;
7588 }
7589
7590 INTERNAL_FUNC int emstorage_change_rule(char *multi_user_name, int rule_id, emstorage_rule_tbl_t* new_rule, int transaction, int *err_code)
7591 {
7592         EM_DEBUG_FUNC_BEGIN("rule_id[%d], new_rule[%p], transaction[%d], err_code[%p]", rule_id, new_rule, transaction, err_code);
7593
7594         if (!new_rule) {                /*  only global rule supported. */
7595                 EM_DEBUG_EXCEPTION("rule_id[%d], new_rule[%p]", rule_id, new_rule);
7596
7597                 if (err_code != NULL)
7598                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7599                 return false;
7600         }
7601
7602         int rc, ret = false;
7603         int error = EMAIL_ERROR_NONE;
7604
7605         DB_STMT hStmt = NULL;
7606         char sql_query_string[QUERY_SIZE] = {0, };
7607         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7608         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7609
7610         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7611                         "UPDATE mail_rule_tbl SET"
7612                         "  filter_name = ?"
7613                         ", type = ?"
7614                         ", value = ?"
7615                         ", value2 = ?"
7616                         ", action_type = ?"
7617                         ", target_mailbox_id = ?"
7618                         ", flag1 = ?"
7619                         ", flag2 = ?"
7620                         ", account_id = ?"
7621                         ", rule_id = ?"
7622                         " WHERE rule_id = %d"
7623                         , rule_id);
7624
7625
7626         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7627         EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
7628         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7629                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7630
7631         int i = 0;
7632
7633         _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->filter_name, 0, FILTER_NAME_LEN_IN_MAIL_RULE_TBL);
7634         _bind_stmt_field_data_int(hStmt, i++, new_rule->type);
7635         _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7636         _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7637         _bind_stmt_field_data_int(hStmt, i++, new_rule->action_type);
7638         _bind_stmt_field_data_int(hStmt, i++, new_rule->target_mailbox_id);
7639         _bind_stmt_field_data_int(hStmt, i++, new_rule->flag1);
7640         _bind_stmt_field_data_int(hStmt, i++, new_rule->flag2);
7641         _bind_stmt_field_data_int(hStmt, i++, new_rule->account_id);
7642         _bind_stmt_field_data_int(hStmt, i++, rule_id);
7643
7644
7645         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7646         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7647                         ("sqlite3_step fail:%d", rc));
7648
7649         ret = true;
7650
7651 FINISH_OFF:
7652         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7653         if (hStmt != NULL) {
7654                 rc = sqlite3_finalize(hStmt);
7655                 if (rc != SQLITE_OK) {
7656                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7657                         error = EMAIL_ERROR_DB_FAILURE;
7658                 }
7659         }
7660
7661         if (err_code != NULL)
7662                 *err_code = error;
7663
7664         EM_DEBUG_FUNC_END("ret [%d]", ret);
7665         return ret;
7666 }
7667
7668 INTERNAL_FUNC int emstorage_find_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7669 {
7670         EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7671
7672         if (!rule) {
7673                 EM_DEBUG_LOG("rule is NULL");
7674                 if (err_code != NULL)
7675                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7676                 return false;
7677         }
7678
7679         DB_STMT hStmt = NULL;
7680         char sql_query_string[QUERY_SIZE] = {0,};
7681         int error = EMAIL_ERROR_NONE;
7682         int rc = 0;
7683         int ret = false;
7684
7685         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7686         EMSTORAGE_START_READ_TRANSACTION(transaction);
7687
7688         switch (rule->action_type) {
7689         case EMAIL_FILTER_MOVE:
7690                 if (rule->type == EMAIL_PRIORITY_SENDER) {
7691                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7692                                         "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7693                                         rule->action_type, rule->type, rule->value2);
7694                 } else {
7695                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7696                                         "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);
7697                 }
7698                 break;
7699         case EMAIL_FILTER_BLOCK:
7700                 if (rule->type == EMAIL_FILTER_FROM)
7701                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7702                                         "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7703                                         rule->action_type, rule->type, rule->value2);
7704                 else if (rule->type == EMAIL_FILTER_SUBJECT)
7705                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7706                                         "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value) = UPPER(\'%q\')",
7707                                         rule->action_type, rule->type, rule->value);
7708                 else if (rule->type == (EMAIL_FILTER_SUBJECT | EMAIL_FILTER_FROM))
7709                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7710                                         "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\'))",
7711                                         rule->action_type, EMAIL_FILTER_SUBJECT, rule->value, EMAIL_FILTER_FROM, rule->value2);
7712                 break;
7713
7714         default:
7715                 EM_DEBUG_EXCEPTION("Invalid parameter : rule->action_type[%d]", rule->action_type);
7716                 error = EMAIL_ERROR_INVALID_PARAM;
7717                 goto FINISH_OFF;
7718                 break;
7719         }
7720
7721         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7722         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7723                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7724
7725         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7726         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7727                         ("sqlite3_step fail:%d", rc));
7728
7729         if (rc == SQLITE_DONE) {
7730                 EM_DEBUG_EXCEPTION(" no matched rule found...");
7731                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7732         }
7733
7734         ret = true;
7735
7736 FINISH_OFF:
7737
7738         if (hStmt != NULL) {
7739                 rc = sqlite3_finalize(hStmt);
7740                 if (rc != SQLITE_OK) {
7741                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7742                         error = EMAIL_ERROR_DB_FAILURE;
7743                 }
7744         }
7745
7746         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7747
7748         if (err_code)
7749                 *err_code = error;
7750
7751         EM_DEBUG_FUNC_END("ret [%d]", ret);
7752         return ret;
7753 }
7754
7755 INTERNAL_FUNC int emstorage_add_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7756 {
7757         EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7758
7759         if (!rule) {    /*  only global rule supported. */
7760                 EM_DEBUG_LOG("rule is NULL");
7761                 if (err_code != NULL)
7762                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7763                 return false;
7764         }
7765
7766         int rc, rc_2, ret = false;
7767         int error = EMAIL_ERROR_NONE;
7768         DB_STMT hStmt = NULL;
7769         char sql_query_string[QUERY_SIZE] = {0, };
7770
7771         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7772
7773         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7774
7775         char *sql;
7776         char **result;
7777         sql = "SELECT max(rowid) FROM mail_rule_tbl;";
7778
7779         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7780         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7781                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7782
7783         if (NULL == result[1])
7784                 rc = 1;
7785         else
7786                 rc = atoi(result[1])+1;
7787
7788         sqlite3_free_table(result);
7789
7790         rule->rule_id = rc;
7791
7792         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7793                         "INSERT INTO mail_rule_tbl VALUES "
7794                         "(?"            /*  account id */
7795                         ", ?"           /*  rule_id */
7796                         ", ?"           /*  filter_name */
7797                         ", ?"           /*  type */
7798                         ", ?"           /*  value */
7799                         ", ?"           /*  value2 */
7800                         ", ?"           /*  action_type */
7801                         ", ?"           /*  target_mailbox_id */
7802                         ", ?"           /*  flag1 */
7803                         ", ?)");        /*  flag2 */
7804
7805         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc_2);
7806         if (rc_2 != SQLITE_OK) {
7807                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc_2, sqlite3_errmsg(local_db_handle));
7808                 error = EMAIL_ERROR_DB_FAILURE;
7809                 goto FINISH_OFF;
7810         }
7811
7812         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL, rule->account_id);
7813         _bind_stmt_field_data_int(hStmt, RULE_ID_IDX_IN_MAIL_RULE_TBL, rule->rule_id);
7814         _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);
7815         _bind_stmt_field_data_int(hStmt, TYPE_IDX_IN_MAIL_RULE_TBL, rule->type);
7816         _bind_stmt_field_data_string(hStmt, VALUE_IDX_IN_MAIL_RULE_TBL, (char*)rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7817         _bind_stmt_field_data_string(hStmt, VALUE2_IDX_IN_MAIL_RULE_TBL, (char*)rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7818         _bind_stmt_field_data_int(hStmt, ACTION_TYPE_IDX_IN_MAIL_RULE_TBL, rule->action_type);
7819         _bind_stmt_field_data_int(hStmt, TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL, rule->target_mailbox_id);
7820         _bind_stmt_field_data_int(hStmt, FLAG1_IDX_IN_MAIL_RULE_TBL, rule->flag1);
7821         _bind_stmt_field_data_int(hStmt, FLAG2_IDX_IN_MAIL_RULE_TBL, rule->flag2);
7822
7823         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7824         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7825                         ("sqlite3_step fail:%d", rc));
7826
7827         ret = true;
7828
7829 FINISH_OFF:
7830         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7831         if (hStmt != NULL) {
7832                 rc = sqlite3_finalize(hStmt);
7833                 if (rc != SQLITE_OK) {
7834                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7835                         error = EMAIL_ERROR_DB_FAILURE;
7836                 }
7837         }
7838
7839         if (err_code != NULL)
7840                 *err_code = error;
7841
7842         EM_DEBUG_FUNC_END("ret [%d]", ret);
7843         return ret;
7844 }
7845
7846 INTERNAL_FUNC int emstorage_delete_rule(char *multi_user_name, int rule_id, int transaction, int *err_code)
7847 {
7848         EM_DEBUG_FUNC_BEGIN("rule_id[%d], transaction[%d], err_code[%p]", rule_id, transaction, err_code);
7849
7850         if (rule_id <= 0) {             /*  only global rule supported. */
7851                 EM_DEBUG_EXCEPTION("rule_id[%d]", rule_id);
7852
7853                 if (err_code != NULL)
7854                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7855                 return false;
7856         }
7857
7858         int rc, ret = false;
7859         int error = EMAIL_ERROR_NONE;
7860         char sql_query_string[QUERY_SIZE] = {0, };
7861         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7862         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7863
7864         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7865         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7866         if (error != EMAIL_ERROR_NONE) {
7867                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7868                 goto FINISH_OFF;
7869         }
7870
7871         rc = sqlite3_changes(local_db_handle);
7872         if (rc == 0) {
7873                 EM_DEBUG_EXCEPTION(" no matched rule found...");
7874
7875                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7876                 goto FINISH_OFF;
7877         }
7878
7879         ret = true;
7880
7881 FINISH_OFF:
7882         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7883
7884         if (err_code != NULL)
7885                 *err_code = error;
7886
7887         EM_DEBUG_FUNC_END("ret [%d]", ret);
7888         return ret;
7889 }
7890
7891 INTERNAL_FUNC int emstorage_free_rule(emstorage_rule_tbl_t** rule_list, int count, int *err_code)
7892 {
7893         EM_DEBUG_FUNC_BEGIN("rule_list[%p], conut[%d], err_code[%p]", rule_list, count, err_code);
7894
7895         int ret = false;
7896         int error = EMAIL_ERROR_NONE;
7897
7898         if (count > 0) {
7899                 if (!rule_list || !*rule_list) {
7900                         EM_DEBUG_EXCEPTION(" rule_list[%p], conut[%d]", rule_list, count);
7901
7902                         error = EMAIL_ERROR_INVALID_PARAM;
7903                         goto FINISH_OFF;
7904                 }
7905
7906                 emstorage_rule_tbl_t* p = *rule_list;
7907                 int i = 0;
7908
7909                 for (; i < count; i++)
7910                         EM_SAFE_FREE(p[i].value);
7911
7912                 EM_SAFE_FREE(p); *rule_list = NULL;
7913         }
7914
7915 FINISH_OFF:
7916         if (err_code != NULL)
7917                 *err_code = error;
7918
7919         EM_DEBUG_FUNC_END("ret [%d]", ret);
7920         return ret;
7921 }
7922
7923 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)
7924 {
7925         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);
7926
7927         if (!total && !unseen) {
7928                 EM_DEBUG_EXCEPTION(" accoun_id[%d], mailbox_id[%d], total[%p], unseen[%p]", account_id, mailbox_id, total, unseen);
7929                 if (err_code != NULL)
7930                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7931                 return false;
7932         }
7933
7934         int rc = -1, ret = false;
7935         int error = EMAIL_ERROR_NONE;
7936         DB_STMT hStmt = NULL;
7937         char sql_query_string[QUERY_SIZE] = {0, };
7938         char *replaced_mailbox_name = NULL;
7939
7940         memset(&sql_query_string, 0x00, sizeof(sql_query_string));
7941         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7942         EMSTORAGE_START_READ_TRANSACTION(transaction);
7943
7944         if (total) {
7945                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl");
7946
7947                 if (account_id != ALL_ACCOUNT) {
7948                         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);
7949                         if (mailbox_id)
7950                                 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);
7951                 } else if (mailbox_id)
7952                         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);
7953
7954 #ifdef USE_GET_RECORD_COUNT_API
7955                 char **result;
7956
7957                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7958                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF2; },
7959                                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7960
7961                 *total = atoi(result[1]);
7962                 sqlite3_free_table(result);
7963 #else
7964
7965                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7966                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF2; },
7967                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7968
7969                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7970                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF2; },
7971                                 ("sqlite3_step fail:%d", rc));
7972                 _get_stmt_field_data_int(hStmt, total, 0);
7973 #endif          /*  USE_GET_RECORD_COUNT_API */
7974         }
7975
7976         if (unseen) {
7977                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl WHERE flags_seen_field = 0");               /*  fSEEN = 0x01 */
7978
7979                 if (account_id != ALL_ACCOUNT) {
7980                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7981                                 sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND account_id = %d", account_id);
7982                 }
7983
7984                 if (mailbox_id) {
7985                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7986                                 sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_id = %d", mailbox_id);
7987                 } else
7988                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7989                                 sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_type NOT IN (3, 5)");
7990
7991                 char **result;
7992                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7993                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc,
7994                                 { error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7995                                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7996
7997                 *unseen = atoi(result[1]);
7998                 sqlite3_free_table(result);
7999
8000         }
8001 FINISH_OFF:
8002         ret = true;
8003
8004 FINISH_OFF2:
8005
8006 #ifndef USE_PREPARED_QUERY_
8007         if (hStmt != NULL) {
8008                 rc = sqlite3_finalize(hStmt);
8009                 if (rc != SQLITE_OK) {
8010                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8011                         error = EMAIL_ERROR_DB_FAILURE;
8012                 }
8013         }
8014 #endif
8015
8016         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8017
8018         EM_SAFE_FREE(replaced_mailbox_name);
8019
8020         if (err_code != NULL)
8021                 *err_code = error;
8022
8023         EM_DEBUG_FUNC_END("ret [%d]", ret);
8024         return ret;
8025 }
8026
8027 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)
8028 {
8029         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
8030
8031         if (mail_id <= 0 || !mail) {
8032                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
8033                 if (err_code != NULL)
8034                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8035                 return false;
8036         }
8037
8038         int col_index = 0;
8039         emstorage_mail_tbl_t* p_data_tbl = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
8040
8041         if (p_data_tbl == NULL) {
8042                 EM_DEBUG_EXCEPTION("malloc failed...");
8043                 if (err_code != NULL)
8044                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8045                 return false;
8046         }
8047
8048         memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8049         DB_STMT hStmt = NULL;
8050         char sql_query_string[QUERY_SIZE] = {0, };
8051
8052         int rc, ret = false;
8053         int error = EMAIL_ERROR_NONE;
8054
8055         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8056         EMSTORAGE_START_READ_TRANSACTION(transaction);
8057
8058         switch (type) {
8059         case RETRIEVE_SUMMARY:
8060                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8061                                 "SELECT account_id, "
8062                                 "mail_id, "
8063                                 "mailbox_id, "
8064                                 "server_mail_status, "
8065                                 "server_mailbox_name, "
8066                                 "server_mail_id, "
8067                                 "file_path_plain, "
8068                                 "file_path_html,"
8069                                 "file_path_mime_entity, "
8070                                 "flags_seen_field, "
8071                                 "save_status, "
8072                                 "lock_status, "
8073                                 "thread_id, "
8074                                 "thread_item_count "
8075                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8076                 break;
8077
8078         case RETRIEVE_FIELDS_FOR_DELETE:
8079                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8080                                 "SELECT account_id, "
8081                                 "mail_id, "
8082                                 "server_mail_status, "
8083                                 "server_mailbox_name, "
8084                                 "server_mail_id "
8085                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8086                 break;
8087
8088         case RETRIEVE_ACCOUNT:
8089                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8090                                 "SELECT account_id "
8091                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8092                 break;
8093
8094         case RETRIEVE_FLAG:
8095                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8096                                 "SELECT account_id, "
8097                                 "flags_seen_field, "
8098                                 "thread_id, "
8099                                 "mailbox_id "
8100                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8101                 break;
8102
8103         default:
8104                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8105                 error = EMAIL_ERROR_INVALID_PARAM;
8106                 goto FINISH_OFF;
8107         }
8108
8109         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8110
8111         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8112         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8113                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8114
8115
8116         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8117         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8118                         ("sqlite3_step fail:%d", rc));
8119
8120         if (rc == SQLITE_DONE) {
8121                 EM_DEBUG_LOG("no matched mail found...");
8122                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8123                 goto FINISH_OFF;
8124         }
8125         switch (type) {
8126         case RETRIEVE_SUMMARY:
8127                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8128                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8129                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8130                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8131                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8132                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8133                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, col_index++);
8134                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, col_index++);
8135                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, col_index++);
8136                 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8137                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), col_index++);
8138                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), col_index++);
8139                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8140                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_item_count), col_index++);
8141                 break;
8142
8143         case RETRIEVE_FIELDS_FOR_DELETE:
8144                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8145                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8146                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8147                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8148                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8149                 break;
8150
8151         case RETRIEVE_ACCOUNT:
8152                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8153                 break;
8154
8155         case RETRIEVE_FLAG:
8156                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8157                 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8158                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8159                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8160                 break;
8161         }
8162
8163         ret = true;
8164
8165 FINISH_OFF:
8166         if (ret == true)
8167                 *mail = p_data_tbl;
8168         else if (p_data_tbl != NULL)
8169                 emstorage_free_mail(&p_data_tbl,  1, NULL);
8170
8171         if (hStmt != NULL) {
8172                 rc = sqlite3_finalize(hStmt);
8173                 if (rc != SQLITE_OK) {
8174                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8175                         error = EMAIL_ERROR_DB_FAILURE;
8176                 }
8177         }
8178
8179
8180         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8181
8182         if (err_code != NULL)
8183                 *err_code = error;
8184
8185         EM_DEBUG_FUNC_END("ret [%d]", ret);
8186         return ret;
8187 }
8188
8189 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)
8190 {
8191         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);
8192
8193         int ret = false;
8194         int error = EMAIL_ERROR_NONE;
8195         int query_string_length = 0;
8196         int i = 0, item_count = 0, rc = -1, field_count, col_index, cur_sql_query_string = 0;
8197         char **result = NULL;
8198         char *sql_query_string = NULL;
8199         emstorage_mail_tbl_t* p_data_tbl = NULL;
8200         sqlite3 *local_db_handle = NULL;
8201
8202         if (number_of_mails <= 0 || !mail_ids) {
8203                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8204                 if (err_code != NULL)
8205                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8206                 return false;
8207         }
8208
8209         p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * number_of_mails);
8210
8211         query_string_length = (sizeof(char) * 8 * number_of_mails) + 512;
8212         sql_query_string = (char*)em_malloc(query_string_length);
8213
8214         if (p_data_tbl == NULL || sql_query_string == NULL) {
8215                 EM_DEBUG_EXCEPTION("malloc failed...");
8216
8217                 EM_SAFE_FREE(p_data_tbl);
8218                 EM_SAFE_FREE(sql_query_string);
8219
8220                 if (err_code != NULL)
8221                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8222                 return false;
8223         }
8224
8225         local_db_handle = emstorage_get_db_connection(multi_user_name);
8226
8227         EMSTORAGE_START_READ_TRANSACTION(transaction);
8228
8229         switch (type) {
8230         case RETRIEVE_SUMMARY:
8231                 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8232                                 "SELECT account_id, "
8233                                 "mail_id, "
8234                                 "mailbox_id, "
8235                                 "server_mail_status, "
8236                                 "server_mailbox_name, "
8237                                 "server_mail_id, "
8238                                 "file_path_plain, "
8239                                 "file_path_html, "
8240                                 "file_path_mime_entity, "
8241                                 "subject, "
8242                                 "flags_seen_field, "
8243                                 "save_status, "
8244                                 "lock_status, "
8245                                 "thread_id, "
8246                                 "thread_item_count "
8247                                 "FROM mail_tbl WHERE mail_id in (");
8248                 field_count = 15;
8249                 break;
8250
8251         case RETRIEVE_FIELDS_FOR_DELETE:
8252                 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8253                                 "SELECT account_id, "
8254                                 "mail_id, "
8255                                 "server_mail_status, "
8256                                 "server_mailbox_name, "
8257                                 "server_mail_id "
8258                                 "FROM mail_tbl WHERE mail_id in (");
8259                 field_count = 5;
8260                 break;
8261
8262         case RETRIEVE_ACCOUNT:
8263                 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8264                                 "SELECT account_id FROM mail_tbl WHERE mail_id in (");
8265                 field_count = 1;
8266                 break;
8267
8268         case RETRIEVE_FLAG:
8269                 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8270                                 "SELECT account_id, "
8271                                 "mail_id, "
8272                                 "mailbox_id, "
8273                                 "flags_seen_field, "
8274                                 "thread_id "
8275                                 "FROM mail_tbl WHERE mail_id in (");
8276                 field_count = 5;
8277                 break;
8278
8279         default:
8280                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8281                 error = EMAIL_ERROR_INVALID_PARAM;
8282                 goto FINISH_OFF;
8283         }
8284
8285         for (i = 0; i < number_of_mails; i++)
8286                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_string_length, "%d,", mail_ids[i]);
8287         sql_query_string[EM_SAFE_STRLEN(sql_query_string) - 1] = ')';
8288
8289         EM_DEBUG_LOG_SEC("Query [%s], Length [%zu]", sql_query_string, EM_SAFE_STRLEN(sql_query_string));
8290
8291         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &item_count, 0, NULL), rc);
8292         if (SQLITE_OK != rc && -1 != rc) {
8293                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
8294                 error = EMAIL_ERROR_DB_FAILURE;
8295                 goto FINISH_OFF;
8296         }
8297         EM_DEBUG_LOG("item_count [%d]", item_count);
8298
8299         if (number_of_mails != item_count) {
8300                 EM_DEBUG_EXCEPTION("Can't find all emails");
8301                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8302                 goto FINISH_OFF;
8303         }
8304
8305         col_index = field_count;
8306
8307         for (i = 0; i < item_count; i++)        {
8308                 switch (type) {
8309                 case RETRIEVE_SUMMARY:
8310                         _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8311                         _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8312                         _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8313                         _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8314                         _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8315                         _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8316                         _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
8317                         _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
8318                         _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
8319                         _get_table_field_data_string(result, &(p_data_tbl[i].subject), 0, col_index++);
8320                         _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8321                         _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
8322                         _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
8323                         _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8324                         _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
8325                         break;
8326
8327                 case RETRIEVE_FIELDS_FOR_DELETE:
8328                         _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8329                         _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8330                         _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8331                         _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8332                         _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8333                         break;
8334
8335                 case RETRIEVE_ACCOUNT:
8336                         _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8337                         break;
8338
8339                 case RETRIEVE_FLAG:
8340                         _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8341                         _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8342                         _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8343                         _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8344                         _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8345                         break;
8346                 }
8347         }
8348
8349         ret = true;
8350
8351 FINISH_OFF:
8352         if (ret == true)
8353                 *mail = p_data_tbl;
8354         else
8355                 emstorage_free_mail(&p_data_tbl, number_of_mails, NULL);
8356
8357         if (result)
8358                 sqlite3_free_table(result);
8359
8360         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8361
8362         EM_SAFE_FREE(sql_query_string);
8363
8364         if (err_code != NULL)
8365                 *err_code = error;
8366
8367         EM_DEBUG_FUNC_END("ret [%d]", ret);
8368         return ret;
8369 }
8370
8371 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)
8372 {
8373         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8374
8375         if (mail_id <= 0 || !mail) {
8376                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
8377                 if (err_code != NULL)
8378                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8379                 return false;
8380         }
8381
8382         int ret = false, error = EMAIL_ERROR_NONE, count;
8383         char conditional_clause[QUERY_SIZE] = {0, };
8384         emstorage_mail_tbl_t* p_data_tbl = NULL;
8385
8386         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8387         EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8388
8389         if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8390                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8391                 goto FINISH_OFF;
8392         }
8393
8394         ret = true;
8395
8396 FINISH_OFF:
8397         if (ret == true)
8398                 *mail = p_data_tbl;
8399         else if (p_data_tbl != NULL)
8400                 emstorage_free_mail(&p_data_tbl, 1, &error);
8401
8402         if (err_code != NULL)
8403                 *err_code = error;
8404
8405         EM_DEBUG_FUNC_END("ret [%d]", ret);
8406         return ret;
8407 }
8408
8409 #ifdef __FEATURE_BODY_SEARCH__
8410 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)
8411 {
8412         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
8413
8414         if (mail_id <= 0 || !mail_text) {
8415                 EM_DEBUG_EXCEPTION("mail_id[%d], mail_text[%p]", mail_id, mail_text);
8416                 if (err_code != NULL)
8417                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8418                 return false;
8419         }
8420
8421         int ret = false;
8422         int error = EMAIL_ERROR_NONE;
8423         int count = 0;
8424         char conditional_clause[QUERY_SIZE] = {0, };
8425         emstorage_mail_text_tbl_t *p_data_tbl = NULL;
8426
8427         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8428         EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8429
8430         if (!emstorage_query_mail_text_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8431                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8432                 goto FINISH_OFF;
8433         }
8434
8435         ret = true;
8436
8437 FINISH_OFF:
8438         if (ret == true)
8439                 *mail_text = p_data_tbl;
8440         else if (p_data_tbl != NULL)
8441                 emstorage_free_mail_text(&p_data_tbl, 1, &error);
8442
8443         if (err_code != NULL)
8444                 *err_code = error;
8445
8446         EM_DEBUG_FUNC_END("ret [%d]", ret);
8447         return ret;
8448 }
8449 #endif
8450
8451 INTERNAL_FUNC int emstorage_mail_search_start(char *multi_user_name,
8452                 emstorage_search_filter_t *search,
8453                 int account_id,
8454                 int mailbox_id,
8455                 int sorting,
8456                 DB_STMT *search_handle,
8457                 int *searched,
8458                 int transaction,
8459                 int *err_code)
8460 {
8461         EM_DEBUG_FUNC_BEGIN("search[%p], account_id[%d], mailbox_id[%d], sorting[%d], "
8462                         "search_handle[%p], searched[%p], transaction[%d], err_code[%p]",
8463                         search, account_id, mailbox_id, sorting, search_handle,
8464                         searched, transaction, err_code);
8465
8466         if (!search_handle || !searched) {
8467                 if (err_code != NULL)
8468                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8469                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8470                 EM_DEBUG_FUNC_END("false");
8471                 return false;
8472         }
8473
8474         emstorage_search_filter_t* p = search;
8475         int error = EMAIL_ERROR_NONE;
8476         DB_STMT hStmt = NULL;
8477         char sql_query_string[QUERY_SIZE] = {0, };
8478         int rc, ret = false;
8479         int and = false, mail_count = 0;
8480
8481         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8482         EMSTORAGE_START_READ_TRANSACTION(transaction);
8483
8484         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl");
8485
8486         if (account_id != ALL_ACCOUNT) {
8487                 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);
8488                 and = true;
8489         }
8490
8491         if (mailbox_id) {
8492                 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);
8493                 and = true;
8494         }
8495
8496         while (p) {
8497                 if (p->key_type) {
8498                         if (!strncmp(p->key_type, "subject", strlen("subject"))) {
8499                                 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);
8500                                 and = true;
8501                         } else if (!strncmp(p->key_type, "full_address_from", strlen("full_address_from"))) {
8502                                 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);
8503                                 and = true;
8504                         } else if (!strncmp(p->key_type, "full_address_to", strlen("full_address_to"))) {
8505                                 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);
8506                                 and = true;
8507                         } else if (!strncmp(p->key_type, "email_address", strlen("email_address"))) {
8508                                 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);
8509                                 and = true;
8510                         }
8511                         p = p->next;
8512                 }
8513         }
8514
8515         if (sorting)
8516                 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");
8517
8518         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
8519
8520
8521         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8522         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8523                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8524
8525
8526         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8527         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8528                         ("sqlite3_step fail:%d", rc));
8529
8530         char **result;
8531
8532         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &mail_count, NULL, NULL), rc);
8533         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
8534                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8535
8536         sqlite3_free_table(result);
8537
8538         ret = true;
8539
8540 FINISH_OFF:
8541         if (ret == true) {
8542                 *search_handle = hStmt;
8543                 *searched = mail_count;
8544                 EM_DEBUG_LOG("mail_count [%d]", mail_count);
8545         } else {
8546                 if (hStmt != NULL) {
8547                         rc = sqlite3_finalize(hStmt);
8548                         if (rc != SQLITE_OK) {
8549                                 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8550                                 error = EMAIL_ERROR_DB_FAILURE;
8551                         }
8552                 }
8553
8554                 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8555         }
8556
8557         if (err_code != NULL)
8558                 *err_code = error;
8559
8560         EM_DEBUG_FUNC_END("ret [%d]", ret);
8561         return ret;
8562 }
8563
8564 INTERNAL_FUNC int emstorage_mail_search_result(DB_STMT search_handle, emstorage_mail_field_type_t type, void** data, int transaction, int *err_code)
8565 {
8566         EM_DEBUG_FUNC_BEGIN("search_handle[%d], type[%d], data[%p], transaction[%d], err_code[%p]", search_handle, type, data, transaction, err_code);
8567
8568         if (search_handle == 0 || !data) {
8569                 EM_DEBUG_EXCEPTION("No Search_handle type[%d], data[%p]", type, data);
8570
8571                 if (err_code != NULL)
8572                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8573                 return false;
8574         }
8575
8576         emstorage_mail_tbl_t* p_data_tbl = NULL;
8577         DB_STMT hStmt = search_handle;
8578         int rc, ret = false;
8579         int error = EMAIL_ERROR_NONE;
8580
8581         switch (type) {
8582         case RETRIEVE_ID:
8583                 _get_stmt_field_data_int(hStmt, (int *)data, MAIL_ID_IDX_IN_MAIL_TBL);
8584                 break;
8585
8586         case RETRIEVE_ENVELOPE:
8587         case RETRIEVE_ALL:
8588                 if (!(p_data_tbl = em_malloc(sizeof(emstorage_mail_tbl_t)))) {
8589                         EM_DEBUG_EXCEPTION(" em_mallocfailed...");
8590                         error = EMAIL_ERROR_OUT_OF_MEMORY;
8591                         goto FINISH_OFF;
8592                 }
8593
8594                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8595                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8596                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_size), MAIL_SIZE_IDX_IN_MAIL_TBL);
8597                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8598                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_from), 1, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL);
8599                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_to), 1, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL);
8600                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject), 1, SUBJECT_IDX_IN_MAIL_TBL);
8601                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->body_download_status), BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL);
8602                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL);
8603                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8604                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8605                 _get_stmt_field_data_time_t(hStmt, &(p_data_tbl->date_time), DATETIME_IDX_IN_MAIL_TBL);
8606                 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL);
8607                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->DRM_status), DRM_STATUS_IDX_IN_MAIL_TBL);
8608                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->priority), PRIORITY_IDX_IN_MAIL_TBL);
8609                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), SAVE_STATUS_IDX_IN_MAIL_TBL);
8610                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), LOCK_STATUS_IDX_IN_MAIL_TBL);
8611                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->report_status), REPORT_STATUS_IDX_IN_MAIL_TBL);
8612                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8613                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->meeting_request_status), MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL);
8614                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->message_class), MESSAGE_CLASS_IDX_IN_MAIL_TBL);
8615                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->digest_type), DIGEST_TYPE_IDX_IN_MAIL_TBL);
8616                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->smime_type), SMIME_TYPE_IDX_IN_MAIL_TBL);
8617                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->scheduled_sending_time), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8618                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->remaining_resend_times), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8619                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->tag_id), TAG_ID_IDX_IN_MAIL_TBL);
8620                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->eas_data_length), EAS_DATA_LENGTH_IDX_IN_MAIL_TBL);
8621                 _get_stmt_field_data_blob(hStmt, (void**)&(p_data_tbl->eas_data), EAS_DATA_IDX_IN_MAIL_TBL);
8622
8623                 if (type == RETRIEVE_ALL) {
8624                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8625                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8626                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_reply), 1, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL);
8627                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_cc), 1, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL);
8628                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_bcc), 1, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL);
8629                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_return), 1, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL);
8630                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->message_id), 0, MESSAGE_ID_IDX_IN_MAIL_TBL);
8631                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8632                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8633                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_count), ATTACHMENT_COUNT_IDX_IN_MAIL_TBL);
8634                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8635                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->replied_time), REPLIED_TIME_IDX_IN_MAIL_TBL);
8636                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->forwarded_time), FORWARDED_TIME_IDX_IN_MAIL_TBL);
8637                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->default_charset), 0, DEFAULT_CHARSET_IDX_IN_MAIL_TBL);
8638                 }
8639
8640                 if (p_data_tbl->body_download_status) {
8641                         struct stat buf;
8642
8643                         if (p_data_tbl->file_path_html) {
8644                                 if (stat(p_data_tbl->file_path_html, &buf) == -1)
8645                                         p_data_tbl->body_download_status = 0;
8646                         } else if (p_data_tbl->file_path_plain) {
8647                                 if (stat(p_data_tbl->file_path_plain, &buf) == -1)
8648                                         p_data_tbl->body_download_status = 0;
8649                         } else
8650                                 p_data_tbl->body_download_status = 0;
8651                 }
8652
8653                 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8654                 break;
8655
8656         case RETRIEVE_SUMMARY:
8657                 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8658                         EM_DEBUG_EXCEPTION(" malloc failed...");
8659
8660                         error = EMAIL_ERROR_OUT_OF_MEMORY;
8661                         goto FINISH_OFF;
8662                 }
8663
8664                 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8665
8666                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8667                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8668                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8669                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8670                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8671
8672                 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8673                 break;
8674
8675         case RETRIEVE_ADDRESS:
8676                 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8677                         EM_DEBUG_EXCEPTION(" malloc failed...");
8678                         error = EMAIL_ERROR_OUT_OF_MEMORY;
8679                         goto FINISH_OFF;
8680                 }
8681
8682                 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8683                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8684                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8685                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8686                 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8687                 break;
8688
8689         default:
8690                 break;
8691         }
8692
8693         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8694         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8695                         ("sqlite3_step fail:%d", rc));
8696
8697         ret = true;
8698
8699 FINISH_OFF:
8700
8701         if (err_code != NULL)
8702                 *err_code = error;
8703
8704         if (ret == false && p_data_tbl)
8705                 emstorage_free_mail(&p_data_tbl, 1, NULL);
8706
8707         EM_DEBUG_FUNC_END("ret [%d]", ret);
8708         return ret;
8709 }
8710
8711 INTERNAL_FUNC int emstorage_mail_search_end(DB_STMT search_handle, int transaction, int *err_code)
8712 {
8713         EM_DEBUG_FUNC_BEGIN("search_handle[%d], transaction[%d], err_code[%p]", search_handle, transaction, err_code);
8714
8715         int error = EMAIL_ERROR_NONE;
8716         int rc, ret = false;
8717
8718         if (search_handle == 0) {
8719                 EM_DEBUG_EXCEPTION("No search_handle");
8720                 error = EMAIL_ERROR_INVALID_PARAM;
8721                 goto FINISH_OFF;
8722         }
8723
8724         DB_STMT hStmt = search_handle;
8725
8726         rc = sqlite3_finalize(hStmt);
8727         if (rc != SQLITE_OK) {
8728                 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8729                 error = EMAIL_ERROR_DB_FAILURE;
8730         }
8731
8732         ret = true;
8733
8734 FINISH_OFF:
8735         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8736
8737         if (err_code != NULL)
8738                 *err_code = error;
8739
8740         EM_DEBUG_FUNC_END("ret [%d]", ret);
8741         return ret;
8742 }
8743
8744 INTERNAL_FUNC int emstorage_change_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t *mail, int transaction, int *err_code)
8745 {
8746         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8747
8748         if (mail_id <= 0 || !mail) {
8749                 EM_DEBUG_EXCEPTION(" mail_id[%d], mail[%p]", mail_id, mail);
8750
8751                 if (err_code != NULL)
8752                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8753                 return false;
8754         }
8755
8756         DB_STMT hStmt = NULL;
8757         char sql_query_string[QUERY_SIZE] = {0, };
8758         int rc = -1;
8759         int ret = false;
8760         int error = EMAIL_ERROR_NONE;
8761         int i = 0;
8762         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8763         char mailbox_id_param_string[10] = {0,};
8764
8765         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
8766
8767         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8768                         "UPDATE mail_tbl SET"
8769                         "  mail_id = ?"
8770                         ", account_id = ?"
8771                         ", mailbox_id = ?"
8772                         ", mail_size = ?"
8773                         ", server_mail_status = ?"
8774                         ", server_mailbox_name = ?"
8775                         ", server_mail_id = ?"
8776                         ", reference_mail_id = ?"
8777                         ", full_address_from = ?"
8778                         ", full_address_reply = ?"  /* 10 */
8779                         ", full_address_to = ?"
8780                         ", full_address_cc = ?"
8781                         ", full_address_bcc = ?"
8782                         ", full_address_return = ?"
8783                         ", subject = ?"
8784                         ", body_download_status = ?"
8785                         ", file_path_plain = ?"
8786                         ", file_path_html = ?"
8787                         ", file_path_mime_entity = ?"
8788                         ", date_time = ?"
8789                         ", flags_seen_field      = ?"
8790                         ", flags_deleted_field   = ?"
8791                         ", flags_flagged_field   = ?"
8792                         ", flags_answered_field  = ?"
8793                         ", flags_recent_field    = ?"
8794                         ", flags_draft_field     = ?"
8795                         ", flags_forwarded_field = ?"
8796                         ", DRM_status = ?"
8797                         ", priority = ?"
8798                         ", save_status = ?"
8799                         ", lock_status = ?"
8800                         ", message_id = ?"
8801                         ", report_status = ?"
8802                         ", preview_text = ?"
8803                         ", smime_type = ?"
8804                         ", scheduled_sending_time = ?"
8805                         ", remaining_resend_times = ?"
8806                         ", tag_id = ?"
8807                         ", replied_time = ?"
8808                         ", forwarded_time = ?"
8809                         ", default_charset = ?"
8810                         ", eas_data_length = ?"
8811                         ", eas_data = ?"
8812                         " WHERE mail_id = %d AND account_id != 0 "
8813                         , mail_id);
8814
8815
8816         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8817         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8818                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8819
8820         _bind_stmt_field_data_int(hStmt, i++, mail->mail_id);
8821         _bind_stmt_field_data_int(hStmt, i++, mail->account_id);
8822         _bind_stmt_field_data_int(hStmt, i++, mail->mailbox_id);
8823         _bind_stmt_field_data_int(hStmt, i++, mail->mail_size);
8824         _bind_stmt_field_data_int(hStmt, i++, mail->server_mail_status);
8825         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mailbox_name, 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
8826         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mail_id, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
8827         _bind_stmt_field_data_int(hStmt, i++, mail->reference_mail_id);
8828         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
8829         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
8830         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
8831         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
8832         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
8833         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
8834         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8835         _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8836         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8837         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8838         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
8839         _bind_stmt_field_data_int(hStmt, i++, mail->date_time);
8840         _bind_stmt_field_data_char(hStmt, i++, mail->flags_seen_field);
8841         _bind_stmt_field_data_char(hStmt, i++, mail->flags_deleted_field);
8842         _bind_stmt_field_data_char(hStmt, i++, mail->flags_flagged_field);
8843         _bind_stmt_field_data_char(hStmt, i++, mail->flags_answered_field);
8844         _bind_stmt_field_data_char(hStmt, i++, mail->flags_recent_field);
8845         _bind_stmt_field_data_char(hStmt, i++, mail->flags_draft_field);
8846         _bind_stmt_field_data_char(hStmt, i++, mail->flags_forwarded_field);
8847         _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
8848         _bind_stmt_field_data_int(hStmt, i++, mail->priority);
8849         _bind_stmt_field_data_int(hStmt, i++, mail->save_status);
8850         _bind_stmt_field_data_int(hStmt, i++, mail->lock_status);
8851         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->message_id, 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
8852         _bind_stmt_field_data_int(hStmt, i++, mail->report_status);
8853         _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8854         _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
8855         _bind_stmt_field_data_int(hStmt, i++, mail->scheduled_sending_time);
8856         _bind_stmt_field_data_int(hStmt, i++, mail->remaining_resend_times);
8857         _bind_stmt_field_data_int(hStmt, i++, mail->tag_id);
8858         _bind_stmt_field_data_int(hStmt, i++, mail->replied_time);
8859         _bind_stmt_field_data_int(hStmt, i++, mail->forwarded_time);
8860         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
8861         _bind_stmt_field_data_int(hStmt, i++, mail->eas_data_length);
8862         _bind_stmt_field_data_blob(hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
8863
8864         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8865         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8866                         ("sqlite3_step fail:%d", rc));
8867
8868         rc = sqlite3_changes(local_db_handle);
8869         if (rc == 0) {
8870                 EM_DEBUG_LOG(" no matched mail found...");
8871                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8872                 goto FINISH_OFF;
8873         }
8874
8875         ret = true;
8876
8877 FINISH_OFF:
8878         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
8879         if (hStmt != NULL) {
8880                 rc = sqlite3_finalize(hStmt);
8881                 if (rc != SQLITE_OK) {
8882                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8883                         error = EMAIL_ERROR_DB_FAILURE;
8884                 }
8885         }
8886
8887
8888         if (error == EMAIL_ERROR_NONE && mail) {
8889                 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
8890                 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail->mail_id, mailbox_id_param_string, 0))
8891                         EM_DEBUG_EXCEPTION("emcore_notify_storage_eventfailed [NOTI_MAIL_UPDATE]");
8892         }
8893
8894         if (err_code != NULL)
8895                 *err_code = error;
8896
8897         EM_DEBUG_FUNC_END("ret [%d]", ret);
8898         return ret;
8899 }
8900
8901 /**
8902  *  emstorage_clean_save_status(int save_status, int  *err_code) - set the all mail status to the set value
8903  *
8904  *
8905  **/
8906 INTERNAL_FUNC int emstorage_clean_save_status(char *multi_user_name, int save_status, int  *err_code)
8907 {
8908         EM_DEBUG_FUNC_BEGIN("save_status[%d], err_code[%p]", save_status, err_code);
8909
8910         EM_IF_NULL_RETURN_VALUE(err_code, false);
8911
8912         int ret = false;
8913         int error = EMAIL_ERROR_NONE;
8914         int rc = 0;
8915         char sql_query_string[QUERY_SIZE] = {0, };
8916         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8917
8918         memset(sql_query_string, 0x00, sizeof(sql_query_string));
8919         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);
8920
8921         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8922         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
8923         if (error != EMAIL_ERROR_NONE) {
8924                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
8925                 goto FINISH_OFF;
8926         }
8927
8928         rc = sqlite3_changes(local_db_handle);
8929         if (rc == 0) {
8930                 EM_DEBUG_LOG(" No Matched Mail Exists ");
8931                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8932         }
8933
8934         ret = true;
8935
8936 FINISH_OFF:
8937
8938         if (err_code != NULL)
8939                 *err_code = error;
8940
8941         EM_DEBUG_FUNC_END("ret [%d]", ret);
8942         return ret;
8943 }
8944
8945 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)
8946 {
8947         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);
8948         int i = 0;
8949         int error = EMAIL_ERROR_NONE;
8950         int ret = false;
8951         int query_size = 0;
8952         int cur_mail_id_string = 0;
8953         int mail_id_string_buffer_length = 0;
8954         int parameter_string_length = 0;
8955         char  *sql_query_string = NULL;
8956         char *mail_id_string_buffer = NULL;
8957         char *parameter_string = NULL;
8958         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8959         email_mail_attribute_type target_mail_attribute_type = 0;
8960
8961         if (!mail_ids  || !field_name || account_id == 0) {
8962                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8963                 if (err_code != NULL)
8964                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8965                 return false;
8966         }
8967
8968         if ((error = emcore_get_attribute_type_by_mail_field_name(field_name, &target_mail_attribute_type)) != EMAIL_ERROR_NONE) {
8969                 EM_DEBUG_EXCEPTION("emstorageemcore_get_attribute_type_by_mail_field_name failed [%d]", error);
8970                 if (err_code != NULL)
8971                         *err_code = error;
8972                 return false;
8973         }
8974
8975         /* Generating mail id list string */
8976         mail_id_string_buffer_length = MAIL_ID_STRING_LENGTH * mail_ids_count;
8977
8978         mail_id_string_buffer = em_malloc(mail_id_string_buffer_length);
8979
8980         if (!mail_id_string_buffer) {
8981                 EM_DEBUG_EXCEPTION("em_mallocfailed");
8982                 if (err_code != NULL)
8983                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8984                 return false;
8985         }
8986
8987         for (i = 0; i < mail_ids_count; i++)
8988                 cur_mail_id_string += SNPRINTF_OFFSET(mail_id_string_buffer, cur_mail_id_string, mail_id_string_buffer_length, "%d,", mail_ids[i]);
8989
8990         if (EM_SAFE_STRLEN(mail_id_string_buffer) > 1)
8991                 mail_id_string_buffer[EM_SAFE_STRLEN(mail_id_string_buffer) - 1] = NULL_CHAR;
8992
8993         /* Generating notification parameter string */
8994         parameter_string_length = mail_id_string_buffer_length + EM_SAFE_STRLEN(field_name) + 2;
8995         parameter_string = em_malloc(parameter_string_length);
8996
8997         if (!parameter_string) {
8998                 EM_DEBUG_EXCEPTION("em_mallocfailed");
8999                 if (err_code != NULL)
9000                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
9001                 EM_SAFE_FREE(mail_id_string_buffer);
9002                 return false;
9003         }
9004
9005         SNPRINTF(parameter_string, parameter_string_length, "%s%c%s", field_name, 0x01, mail_id_string_buffer);
9006         query_size = EM_SAFE_STRLEN(mail_id_string_buffer) + EM_SAFE_STRLEN(field_name) + 250;
9007
9008         sql_query_string = em_malloc(query_size);
9009         if (sql_query_string == NULL) {
9010                 EM_DEBUG_EXCEPTION("em_mallocfailed");
9011                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9012                 goto FINISH_OFF;
9013         }
9014         /* Write query string */
9015         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);
9016
9017         EM_DEBUG_LOG_DEV("sql_query_string [%s]", sql_query_string);
9018
9019         /* Execute query */
9020         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9021         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9022         if (error != EMAIL_ERROR_NONE) {
9023                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9024                 goto FINISH_OFF;
9025         }
9026
9027         if (sqlite3_changes(local_db_handle) == 0)
9028                 EM_DEBUG_LOG("no mail matched...");
9029
9030         ret = true;
9031
9032 FINISH_OFF:
9033         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9034
9035         if (error == EMAIL_ERROR_NONE && parameter_string) {
9036                 if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, target_mail_attribute_type, parameter_string, value))
9037                         EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_MAIL_FIELD_UPDATE [%s,%d]", field_name, value);
9038         }
9039
9040         EM_SAFE_FREE(mail_id_string_buffer);
9041         EM_SAFE_FREE(parameter_string);
9042         EM_SAFE_FREE(sql_query_string);
9043
9044
9045         if (err_code != NULL)
9046                 *err_code = error;
9047
9048         EM_DEBUG_FUNC_END("error [%d]", error);
9049         return ret;
9050 }
9051
9052 #ifdef __FEATURE_BODY_SEARCH__
9053 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)
9054 {
9055         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
9056
9057         if (mail_id <= 0 || !mail_text) {
9058                 EM_DEBUG_EXCEPTION(" mail_id[%d], mail_text[%p]", mail_id, mail_text);
9059                 if (err_code != NULL)
9060                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9061                 return false;
9062         }
9063
9064         int ret = false;
9065         int error = EMAIL_ERROR_NONE;
9066         DB_STMT hStmt = NULL;
9067         char sql_query_string[QUERY_SIZE] = {0, };
9068
9069         int i = 0;
9070         int rc = 0;
9071
9072         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9073         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9074
9075         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9076                         "UPDATE mail_text_tbl SET"
9077                         " body_text = ?"
9078                         " WHERE mail_id = %d AND account_id != 0"
9079                         , mail_id);
9080         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9081
9082         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9083         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9084                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9085
9086         i = 0;
9087         _bind_stmt_field_data_string(hStmt, i++, (char *)mail_text->body_text, 0, -1);
9088
9089         if (hStmt != NULL) {
9090                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9091                 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9092                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9093
9094                 rc = sqlite3_changes(local_db_handle);
9095                 if (rc == 0) {
9096                         EM_DEBUG_LOG(" no matched mail found...");
9097                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
9098                         goto FINISH_OFF;
9099                 }
9100         }
9101
9102         ret = true;
9103
9104 FINISH_OFF:
9105         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9106
9107         if (hStmt != NULL) {
9108                 rc = sqlite3_finalize(hStmt);
9109                 if (rc != SQLITE_OK) {
9110                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9111                         error = EMAIL_ERROR_DB_FAILURE;
9112                 }
9113                 hStmt = NULL;
9114         }
9115
9116         if (err_code != NULL)
9117                 *err_code = error;
9118
9119         EM_DEBUG_FUNC_END("ret [%d]", ret);
9120         return ret;
9121 }
9122 #endif
9123
9124 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)
9125 {
9126         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
9127
9128         int ret = false;
9129         int error = EMAIL_ERROR_NONE;
9130         int move_flag = 0;
9131         int rc = 0;
9132         int i = 0;
9133         int mailbox_id = 0;
9134         DB_STMT hStmt = NULL;
9135         char sql_query_string[QUERY_SIZE] = {0, };
9136         char mailbox_id_param_string[10] = {0,};
9137         sqlite3 *local_db_handle = NULL;
9138
9139         if (mail_id <= 0 || !mail) {
9140                 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], mail[%p]", mail_id, type, mail);
9141                 if (err_code != NULL)
9142                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9143                 return false;
9144         }
9145
9146         local_db_handle = emstorage_get_db_connection(multi_user_name);
9147
9148         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9149
9150         switch (type) {
9151         case APPEND_BODY:
9152                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9153                                 "UPDATE mail_tbl SET"
9154                                 "  body_download_status = ?"
9155                                 ", file_path_plain = ?"
9156                                 ", file_path_html = ?"
9157                                 ", file_path_mime_entity = ?"
9158                                 ", flags_seen_field      = ?"
9159                                 ", flags_deleted_field   = ?"
9160                                 ", flags_flagged_field   = ?"
9161                                 ", flags_answered_field  = ?"
9162                                 ", flags_recent_field    = ?"
9163                                 ", flags_draft_field     = ?"
9164                                 ", flags_forwarded_field = ?"
9165                                 ", DRM_status = ?"
9166                                 ", attachment_count = ?"
9167                                 ", preview_text= ?"
9168                                 ", meeting_request_status = ? "
9169                                 ", message_class = ? "
9170                                 ", digest_type = ? "
9171                                 ", smime_type = ? "
9172                                 " WHERE mail_id = %d AND account_id != 0"
9173                                 , mail_id);
9174
9175
9176                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9177                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9178                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9179                 i = 0;
9180
9181                 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9182                 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9183                 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9184                 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9185                 _bind_stmt_field_data_int(hStmt, i++, mail->flags_seen_field);
9186                 _bind_stmt_field_data_int(hStmt, i++, mail->flags_deleted_field);
9187                 _bind_stmt_field_data_int(hStmt, i++, mail->flags_flagged_field);
9188                 _bind_stmt_field_data_int(hStmt, i++, mail->flags_answered_field);
9189                 _bind_stmt_field_data_int(hStmt, i++, mail->flags_recent_field);
9190                 _bind_stmt_field_data_int(hStmt, i++, mail->flags_draft_field);
9191                 _bind_stmt_field_data_int(hStmt, i++, mail->flags_forwarded_field);
9192                 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9193                 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9194                 _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9195                 _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9196                 _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9197                 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9198                 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9199                 break;
9200
9201         case UPDATE_MAILBOX: {
9202                                                          int err;
9203                                                          emstorage_mailbox_tbl_t *mailbox_tbl;
9204
9205                                                          if ((err = emstorage_get_mailbox_by_id(multi_user_name, mail->mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
9206                                                                  EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_id failed [%d]", err);
9207                                                                  goto FINISH_OFF;
9208                                                          }
9209
9210                                                          SNPRINTF(sql_query_string, sizeof(sql_query_string),
9211                                                                          "UPDATE mail_tbl SET"
9212                                                                          " mailbox_id = '%d'"
9213                                                                          ",mailbox_type = '%d'"
9214                                                                          " WHERE mail_id = %d AND account_id != 0"
9215                                                                          , mailbox_tbl->mailbox_id
9216                                                                          , mailbox_tbl->mailbox_type
9217                                                                          , mail_id);
9218                                                          move_flag = 1;
9219
9220
9221                                                          EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9222                                                          EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9223                                                                          ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9224
9225                                                          i = 0;
9226                                                          _bind_stmt_field_data_string(hStmt, i++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
9227
9228                                                          emstorage_free_mailbox(&mailbox_tbl, 1, NULL); /*prevent 26251*/
9229
9230                                                  }
9231                                                  break;
9232
9233         case UPDATE_FLAG:
9234                                                  SNPRINTF(sql_query_string, sizeof(sql_query_string),
9235                                                                  "UPDATE mail_tbl SET"
9236                                                                  " flags_seen_field      = %d"
9237                                                                  ",flags_deleted_field   = %d"
9238                                                                  ",flags_flagged_field   = %d"
9239                                                                  ",flags_answered_field  = %d"
9240                                                                  ",flags_recent_field    = %d"
9241                                                                  ",flags_draft_field     = %d"
9242                                                                  ",flags_forwarded_field = %d"
9243                                                                  "  WHERE mail_id = %d AND account_id != 0"
9244                                                                  , mail->flags_seen_field
9245                                                                  , mail->flags_deleted_field
9246                                                                  , mail->flags_flagged_field
9247                                                                  , mail->flags_answered_field
9248                                                                  , mail->flags_recent_field
9249                                                                  , mail->flags_draft_field
9250                                                                  , mail->flags_forwarded_field
9251                                                                  , mail_id);
9252                                                  EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9253
9254
9255                                                  EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9256                                                  EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9257                                                                  ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9258
9259                                                  break;
9260
9261         case UPDATE_EXTRA_FLAG:
9262                                                  SNPRINTF(sql_query_string, sizeof(sql_query_string),
9263                                                                  "UPDATE mail_tbl SET"
9264                                                                  "  priority = %d"
9265                                                                  ", save_status = %d"
9266                                                                  ", lock_status = %d"
9267                                                                  ", report_status = %d"
9268                                                                  ", DRM_status = %d"
9269                                                                  " WHERE mail_id = %d AND account_id != 0"
9270                                                                  , mail->priority
9271                                                                  , mail->save_status
9272                                                                  , mail->lock_status
9273                                                                  , mail->report_status
9274                                                                  , mail->DRM_status
9275                                                                  , mail_id);
9276                                                  EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9277
9278
9279                                                  EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9280                                                  EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9281                                                                  ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9282                                                  break;
9283
9284         case UPDATE_STICKY_EXTRA_FLAG:
9285                                                  SNPRINTF(sql_query_string, sizeof(sql_query_string),
9286                                                                  "UPDATE mail_tbl SET"
9287                                                                  "  lock_status = %d"
9288                                                                  "  WHERE mail_id = %d AND account_id != 0"
9289                                                                  , mail->lock_status
9290                                                                  , mail_id);
9291                                                  EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9292
9293
9294                                                  EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9295                                                  EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9296                                                                  ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9297                                                  break;
9298
9299         case UPDATE_MAIL:
9300                                                  SNPRINTF(sql_query_string, sizeof(sql_query_string),
9301                                                                  "UPDATE mail_tbl SET"
9302                                                                  "  full_address_from = ?"
9303                                                                  ", full_address_reply = ?"
9304                                                                  ", full_address_to = ?"
9305                                                                  ", full_address_cc = ?"
9306                                                                  ", full_address_bcc = ?"
9307                                                                  ", full_address_return = ?"
9308                                                                  ", subject = ?"
9309                                                                  ", file_path_plain = ?"
9310                                                                  ", date_time = ?"
9311                                                                  ", flags_seen_field = ?"
9312                                                                  ", flags_deleted_field = ?"
9313                                                                  ", flags_flagged_field = ?"
9314                                                                  ", flags_answered_field = ?"
9315                                                                  ", flags_recent_field = ?"
9316                                                                  ", flags_draft_field = ?"
9317                                                                  ", flags_forwarded_field = ?"
9318                                                                  ", priority = ?"
9319                                                                  ", save_status = ?"
9320                                                                  ", lock_status = ?"
9321                                                                  ", report_status = ?"
9322                                                                  ", DRM_status = ?"
9323                                                                  ", file_path_html = ?"
9324                                                                  ", file_path_mime_entity = ?"
9325                                                                  ", mail_size = ?"
9326                                                                  ", preview_text = ?"
9327                                                                  ", body_download_status = ?"
9328                                                                  ", attachment_count = ?"
9329                                                                  ", inline_content_count = ?"
9330                                                                  ", meeting_request_status = ?"
9331                                                                  ", message_class = ?"
9332                                                                  ", digest_type = ?"
9333                                                                  ", smime_type = ?"
9334                                                                  ", scheduled_sending_time = ?"
9335                                                                  ", remaining_resend_times = ?"
9336                                                                  ", tag_id = ?"
9337                                                                  ", replied_time = ?"
9338                                                                  ", forwarded_time = ?"
9339                                                                  ", default_charset = ?"
9340                                                                  ", eas_data_length = ?"
9341                                                                  ", eas_data = ?"
9342                                                                  " WHERE mail_id = %d AND account_id != 0"
9343                                                                  , mail_id);
9344
9345
9346                                                  EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9347                                                  EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
9348                                                  EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9349                                                                  ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9350                                                  i = 0;
9351                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
9352                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
9353                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
9354                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
9355                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
9356                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
9357                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9358                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9359                                                  _bind_stmt_field_data_time_t(hStmt, i++, mail->date_time);
9360                                                  _bind_stmt_field_data_char(hStmt, i++, mail->flags_seen_field);
9361                                                  _bind_stmt_field_data_char(hStmt, i++, mail->flags_deleted_field);
9362                                                  _bind_stmt_field_data_char(hStmt, i++, mail->flags_flagged_field);
9363                                                  _bind_stmt_field_data_char(hStmt, i++, mail->flags_answered_field);
9364                                                  _bind_stmt_field_data_char(hStmt, i++, mail->flags_recent_field);
9365                                                  _bind_stmt_field_data_char(hStmt, i++, mail->flags_draft_field);
9366                                                  _bind_stmt_field_data_char(hStmt, i++, mail->flags_forwarded_field);
9367                                                  _bind_stmt_field_data_int(hStmt, i++, mail->priority);
9368                                                  _bind_stmt_field_data_int(hStmt, i++, mail->save_status);
9369                                                  _bind_stmt_field_data_int(hStmt, i++, mail->lock_status);
9370                                                  _bind_stmt_field_data_int(hStmt, i++, mail->report_status);
9371                                                  _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9372                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9373                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9374                                                  _bind_stmt_field_data_int(hStmt, i++, mail->mail_size);
9375                                                  _bind_stmt_field_data_nstring(hStmt, i++, (char*)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9376                                                  _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9377                                                  _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9378                                                  _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9379                                                  _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9380                                                  _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9381                                                  _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9382                                                  _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9383                                                  _bind_stmt_field_data_int(hStmt, i++, mail->scheduled_sending_time);
9384                                                  _bind_stmt_field_data_int(hStmt, i++, mail->remaining_resend_times);
9385                                                  _bind_stmt_field_data_int(hStmt, i++, mail->tag_id);
9386                                                  _bind_stmt_field_data_int(hStmt, i++, mail->replied_time);
9387                                                  _bind_stmt_field_data_int(hStmt, i++, mail->forwarded_time);
9388                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
9389                                                  _bind_stmt_field_data_int(hStmt, i++, mail->eas_data_length);
9390                                                  _bind_stmt_field_data_blob(hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
9391                                                  break;
9392
9393         case UPDATE_DATETIME: {
9394                                                           SNPRINTF(sql_query_string, sizeof(sql_query_string),
9395                                                                           "UPDATE mail_tbl SET"
9396                                                                           " date_time = '%ld'"
9397                                                                           " WHERE mail_id = %d AND account_id != 0"
9398                                                                           , mail->date_time
9399                                                                           , mail_id);
9400
9401                                                           EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9402                                                           EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9403                                                                           ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9404                                                           break;
9405                                                   }
9406
9407         case UPDATE_FROM_CONTACT_INFO:
9408                                                   EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_FROM_CONTACT_INFO");
9409                                                   SNPRINTF(sql_query_string, sizeof(sql_query_string),
9410                                                                   "UPDATE mail_tbl SET"
9411                                                                   " email_address_sender = ?,"
9412                                                                   " WHERE mail_id = %d",
9413                                                                   mail_id);
9414
9415                                                   hStmt = NULL;
9416
9417                                                   EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9418                                                   EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9419                                                                   ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9420                                                   i = 0;
9421                                                   _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9422                                                   break;
9423
9424         case UPDATE_TO_CONTACT_INFO:
9425                                                   EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_TO_CONTACT_INFO");
9426                                                   SNPRINTF(sql_query_string, sizeof(sql_query_string),
9427                                                                   "UPDATE mail_tbl SET"
9428                                                                   " email_address_recipient = ?,"
9429                                                                   " WHERE mail_id = %d",
9430                                                                   mail_id);
9431
9432                                                   hStmt = NULL;
9433
9434                                                   EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9435                                                   EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9436                                                                   ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9437                                                   i = 0;
9438                                                   _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9439                                                   break;
9440
9441         case UPDATE_ALL_CONTACT_INFO:
9442                                                   EM_DEBUG_LOG("emstorage_change_mail_field - mail change type is UPDATE_ALL_CONTACT_INFO");
9443                                                   SNPRINTF(sql_query_string, sizeof(sql_query_string),
9444                                                                   "UPDATE mail_tbl SET"
9445                                                                   " email_address_sender = ?,"
9446                                                                   " email_address_recipient = ?,"
9447                                                                   " WHERE mail_id = %d",
9448                                                                   mail_id);
9449
9450                                                   hStmt = NULL;
9451
9452                                                   EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9453                                                   EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9454                                                                   ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9455                                                   i = 0;
9456                                                   _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9457                                                   _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9458                                                   break;
9459
9460
9461 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
9462         case UPDATE_PARTIAL_BODY_DOWNLOAD:
9463
9464                                                   SNPRINTF(sql_query_string, sizeof(sql_query_string),
9465                                                                   "UPDATE mail_tbl SET"
9466                                                                   "  body_download_status = ?"
9467                                                                   ", file_path_plain = ?"
9468                                                                   ", file_path_html = ?"
9469                                                                   ", file_path_mime_entity = ?"
9470                                                                   ", attachment_count = ?"
9471                                                                   ", inline_content_count = ?"
9472                                                                   ", preview_text = ?"
9473                                                                   ", digest_type = ?"
9474                                                                   ", smime_type = ?"
9475                                                                   " WHERE mail_id = %d"
9476                                                                   , mail_id);
9477
9478
9479                                                   EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9480                                                   EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9481                                                                   ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9482                                                   i = 0;
9483
9484                                                   _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9485                                                   _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9486                                                   _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html,  0, TEXT_2_LEN_IN_MAIL_TBL);
9487                                                   _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9488                                                   _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9489                                                   _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9490                                                   _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text,    0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9491                                                   _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9492                                                   _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9493
9494                                                   break;
9495
9496 #endif
9497         case UPDATE_FILE_PATH:
9498                                                   SNPRINTF(sql_query_string, sizeof(sql_query_string),
9499                                                                   "UPDATE mail_tbl SET"
9500                                                                   ", file_path_plain = ?"
9501                                                                   ", file_path_html = ?"
9502                                                                   ", file_path_mime_entity = ?"
9503                                                                   " WHERE mail_id = %d"
9504                                                                   , mail_id);
9505
9506
9507                                                   EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9508                                                   EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
9509                                                   EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9510                                                                   ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9511                                                   i = 0;
9512                                                   _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9513                                                   _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9514                                                   _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9515                                                   break;
9516
9517         default:
9518                                                   EM_DEBUG_LOG(" type[%d]", type);
9519
9520                                                   error = EMAIL_ERROR_INVALID_PARAM;
9521                                                   goto FINISH_OFF;
9522         }
9523
9524         if (hStmt != NULL) {
9525
9526                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9527                 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
9528                                 ("sqlite3_step fail:%d", rc));
9529                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9530                                 ("sqlite3_step fail:%d", rc));
9531                 rc = sqlite3_changes(local_db_handle);
9532                 if (rc == 0) {
9533                         EM_DEBUG_LOG(" no matched mail found...");
9534                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
9535                         goto FINISH_OFF;
9536                 }
9537         }
9538
9539         if (mail->account_id == 0) {
9540                 emstorage_mail_tbl_t* mail_for_account_tbl = NULL;
9541                 if (!emstorage_get_mail_field_by_id(multi_user_name, mail_id, RETRIEVE_ACCOUNT, &mail_for_account_tbl, true, &error) || !mail_for_account_tbl) {
9542                         EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id error [%d]", error);
9543                         goto FINISH_OFF;
9544                 }
9545                 mail->account_id = mail_for_account_tbl->account_id;
9546                 if (mail_for_account_tbl)
9547                         emstorage_free_mail(&mail_for_account_tbl, 1, NULL);
9548         }
9549
9550         ret = true;
9551
9552 FINISH_OFF:
9553         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9554
9555         if (hStmt != NULL) {
9556                 rc = sqlite3_finalize(hStmt);
9557                 if (rc != SQLITE_OK) {
9558                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9559                         error = EMAIL_ERROR_DB_FAILURE;
9560                 }
9561                 hStmt = NULL;
9562         }
9563
9564         if (error == EMAIL_ERROR_NONE &&  move_flag != 1 && transaction) {
9565                 if (!emstorage_get_mailbox_id_by_mailbox_type(multi_user_name, mail->account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &mailbox_id, false, &error))
9566                         EM_DEBUG_EXCEPTION("emstorage_get_mailbox_id_by_mailbox_type error [%d]", error);
9567
9568                 if (mail->mailbox_id == mailbox_id) {
9569                         SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
9570                         if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, mailbox_id_param_string, type))
9571                                 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventerror [ NOTI_MAIL_UPDATE ] >>>> ");
9572                 } else {
9573                         /* h.gahlaut@samsung.com: Jan 10, 2011 Publishing noti to refresh outbox when email sending status changes */
9574                         if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, NULL, type))
9575                                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventerror [ NOTI_MAIL_UPDATE ]");
9576                 }
9577         }
9578
9579         if (err_code != NULL)
9580                 *err_code = error;
9581
9582         EM_DEBUG_FUNC_END("ret [%d]", ret);
9583         return ret;
9584 }
9585
9586 INTERNAL_FUNC int emstorage_increase_mail_id(char *multi_user_name, int *mail_id, int transaction, int *err_code)
9587 {
9588         EM_DEBUG_FUNC_BEGIN("mail_id[%p], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9589
9590         int rc, ret = false;
9591         int error = EMAIL_ERROR_NONE;
9592         int latest_mail_id = 0;
9593         sqlite3 *local_db_handle = NULL;
9594         char *sql = "SELECT MAX(mail_id) FROM mail_tbl;";
9595         char **result = NULL;
9596
9597 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9598         _timedlock_shm_mutex(mapped_for_generating_mail_id, 2);
9599 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9600
9601         ret = vconf_get_int(VCONF_KEY_LATEST_MAIL_ID, &latest_mail_id);
9602         if (ret < 0 || latest_mail_id == 0) {
9603                 EM_DEBUG_LOG("vconf_get_int() failed [%d] or latest_mail_id is zero", ret);
9604
9605                 local_db_handle = emstorage_get_db_connection(multi_user_name);
9606
9607                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9608                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
9609                                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9610                 if (NULL == result[1])
9611                         rc = 1;
9612                 else
9613                         rc = atoi(result[1]) + 1;
9614
9615                 sqlite3_free_table(result);
9616                 latest_mail_id = rc;
9617         }
9618
9619         latest_mail_id++;
9620
9621         ret = vconf_set_int(VCONF_KEY_LATEST_MAIL_ID, latest_mail_id);
9622
9623         if (mail_id)
9624                 *mail_id = latest_mail_id;
9625
9626         ret = true;
9627
9628 FINISH_OFF:
9629
9630 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9631                 _unlockshm_mutex(mapped_for_generating_mail_id);
9632 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9633
9634         if (err_code != NULL)
9635                 *err_code = error;
9636
9637         EM_DEBUG_FUNC_END("ret [%d]", ret);
9638         return ret;
9639 }
9640
9641 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)
9642 {
9643         EM_PROFILE_BEGIN(profile_emstorage_add_mail);
9644         EM_DEBUG_FUNC_BEGIN("mail_tbl_data[%p], get_id[%d], transaction[%d], err_code[%p]", mail_tbl_data, get_id, transaction, err_code);
9645
9646         if (!mail_tbl_data) {
9647                 EM_DEBUG_EXCEPTION("mail_tbl_data[%p], get_id[%d]", mail_tbl_data, get_id);
9648                 if (err_code != NULL)
9649                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9650                 return false;
9651         }
9652
9653         int rc, ret = false;
9654         int error = EMAIL_ERROR_NONE;
9655         char sql_query_string[QUERY_SIZE] = {0, };
9656         DB_STMT hStmt = NULL;
9657
9658         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9659
9660         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9661
9662         if (get_id) {
9663                 /*  increase unique id */
9664                 char *sql = "SELECT max(rowid) FROM mail_tbl;";
9665                 char **result;
9666
9667                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9668                 /*              EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
9669                                 ("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle))); */
9670                 if (rc != SQLITE_OK) {
9671                         EM_DEBUG_EXCEPTION("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle));
9672                         error = EMAIL_ERROR_DB_FAILURE;
9673                         sqlite3_free_table(result);
9674                         goto FINISH_OFF;
9675                 }
9676
9677                 if (NULL == result[1])
9678                         rc = 1;
9679                 else
9680                         rc = atoi(result[1])+1;
9681
9682                 sqlite3_free_table(result);
9683
9684                 mail_tbl_data->mail_id   = rc;
9685                 mail_tbl_data->thread_id = rc;
9686         }
9687
9688         if (mail_tbl_data->date_time == 0)
9689                 mail_tbl_data->date_time = time(NULL);
9690
9691         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9692                         "INSERT INTO mail_tbl VALUES "
9693                         "(?" /*  mail_id */
9694                         ", ?" /*  account_id */
9695                         ", ?" /*  mailbox_id */
9696                         ", ?" /*  mailbox_type */
9697                         ", ?" /*  subject */
9698
9699                         ", ?" /*  date_time */
9700                         ", ?" /*  server_mail_status */
9701                         ", ?" /*  server_mailbox_name */
9702                         ", ?" /*  server_mail_id */
9703                         ", ?" /*  message_id */
9704
9705                         ", ?" /*  reference_mail_id */
9706                         ", ?" /*  full_address_from */
9707                         ", ?" /*  full_address_reply */
9708                         ", ?" /*  full_address_to */
9709                         ", ?" /*  full_address_cc */
9710
9711                         ", ?" /*  full_address_bcc */
9712                         ", ?" /*  full_address_return */
9713                         ", ?" /*  email_address_sender */
9714                         ", ?" /*  email_address_recipient */
9715                         ", ?" /*  alias_sender */
9716
9717                         ", ?" /*  alias_recipient */
9718                         ", ?" /*  body_download_status */
9719                         ", ?" /*  file_path_plain */
9720                         ", ?" /*  file_path_html */
9721                         ", ?" /*  file_path_mime_entity */
9722
9723                         ", ?" /*  mail_size */
9724                         ", ?" /*  flags_seen_field */
9725                         ", ?" /*  flags_deleted_field */
9726                         ", ?" /*  flags_flagged_field */
9727                         ", ?" /*  flags_answered_field */
9728
9729                         ", ?" /*  flags_recent_field */
9730                         ", ?" /*  flags_draft_field */
9731                         ", ?" /*  flags_forwarded_field */
9732                         ", ?" /*  DRM_status */
9733                         ", ?" /*  priority */
9734
9735                         ", ?" /*  save_status */
9736                         ", ?" /*  lock_status */
9737                         ", ?" /*  report_status */
9738                         ", ?" /*  attachment_count */
9739                         ", ?" /*  inline_content_count */
9740
9741                         ", ?" /*  thread_id */
9742                         ", ?" /*  thread_item_count */
9743                         ", ?" /*  preview_text */
9744                         ", ?" /*  meeting_request_status */
9745                         ", ?" /*  message_class */
9746
9747                         ", ?" /*  digest_type */
9748                         ", ?" /*  smime_type */
9749                         ", ?" /*  scheduled_sending_time */
9750                         ", ?" /*  remaining_resend_times */
9751                         ", ?" /*  tag_id */
9752
9753                         ", ?" /*  replied_time */
9754                         ", ?" /*  forwarded_time */
9755                         ", ?" /*  default charset */
9756                         ", ?" /*  eas_data_length */
9757                         ", ?" /*  eas_data */
9758                         ", ?" /*  user_name */
9759                         ")");
9760
9761         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle,
9762                                 sql_query_string,
9763                                 EM_SAFE_STRLEN(sql_query_string),
9764                                 &hStmt,
9765                                 NULL),
9766                         rc);
9767         if (rc != SQLITE_OK) {
9768                 EM_DEBUG_EXCEPTION("sqlite3_prepare error [%d] [%s] SQL(%s) ",
9769                                 rc, sql_query_string, sqlite3_errmsg(local_db_handle));
9770                 error = EMAIL_ERROR_DB_FAILURE;
9771                 goto FINISH_OFF;
9772         }
9773
9774         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mail_id);
9775         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, mail_tbl_data->account_id);
9776         _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_id);
9777         _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_type);
9778         _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9779
9780         _bind_stmt_field_data_int(hStmt, DATETIME_IDX_IN_MAIL_TBL, mail_tbl_data->date_time);
9781         _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->server_mail_status);
9782         _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);
9783         _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);
9784         _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);
9785
9786         _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, mail_tbl_data->reference_mail_id);
9787         _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);
9788         _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);
9789         _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);
9790         _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);
9791
9792         _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);
9793         _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);
9794         _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);
9795         _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);
9796         _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);
9797
9798         _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);
9799         _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->body_download_status);
9800         _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);
9801         _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);
9802         _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);
9803
9804         _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, mail_tbl_data->mail_size);
9805         _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_seen_field);
9806         _bind_stmt_field_data_int(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_deleted_field);
9807         _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_flagged_field);
9808         _bind_stmt_field_data_int(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_answered_field);
9809
9810         _bind_stmt_field_data_int(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_recent_field);
9811         _bind_stmt_field_data_int(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_draft_field);
9812         _bind_stmt_field_data_int(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_forwarded_field);
9813         _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->DRM_status);
9814         _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, mail_tbl_data->priority);
9815
9816         _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->save_status);
9817         _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->lock_status);
9818         _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->report_status);
9819         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->attachment_count);
9820         _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->inline_content_count);
9821
9822         _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, mail_tbl_data->thread_id);
9823         _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->thread_item_count);
9824         _bind_stmt_field_data_nstring(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9825         _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->meeting_request_status);
9826         _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, mail_tbl_data->message_class);
9827
9828         _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->digest_type);
9829         _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->smime_type);
9830         _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->scheduled_sending_time);
9831         _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, mail_tbl_data->remaining_resend_times);
9832         _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, mail_tbl_data->tag_id);
9833
9834         _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->replied_time);
9835         _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->forwarded_time);
9836         _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);
9837         _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, mail_tbl_data->eas_data_length);
9838         _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, (void*)mail_tbl_data->eas_data, mail_tbl_data->eas_data_length);
9839         _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);
9840
9841         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9842         if (rc == SQLITE_FULL) {
9843                 EM_DEBUG_EXCEPTION("sqlite3_step error [%d]", rc);
9844                 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
9845                 goto FINISH_OFF;
9846         }
9847         if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
9848                 EM_DEBUG_EXCEPTION("sqlite3_step error [%d]", rc);
9849                 error = EMAIL_ERROR_DB_FAILURE;
9850                 goto FINISH_OFF;
9851         }
9852         ret = true;
9853
9854 FINISH_OFF:
9855         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9856
9857         if (hStmt != NULL) {
9858                 rc = sqlite3_finalize(hStmt);
9859                 if (rc != SQLITE_OK) {
9860                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9861                         error = EMAIL_ERROR_DB_FAILURE;
9862                 }
9863         }
9864
9865         if (err_code != NULL)
9866                 *err_code = error;
9867
9868         EM_PROFILE_END(profile_emstorage_add_mail);
9869         EM_DEBUG_FUNC_END("ret [%d]", ret);
9870         return ret;
9871 }
9872
9873 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)
9874 {
9875         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);
9876
9877         int ret = false, i, cur_conditional_clause = 0;
9878         int error = EMAIL_ERROR_NONE;
9879         int target_account_id;
9880         int conditional_clause_len = 0;
9881         char *sql_query_string = NULL, *conditional_clause = NULL;
9882         emstorage_mailbox_tbl_t *result_mailbox = NULL;
9883         email_mailbox_type_e target_mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED;
9884         char* target_mailbox_name = NULL;
9885
9886         if (!mail_ids || input_mailbox_id <= 0) {
9887                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9888                 if (err_code != NULL)
9889                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9890                 return false;
9891         }
9892
9893         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9894
9895         if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &result_mailbox)) != EMAIL_ERROR_NONE || !result_mailbox) {
9896                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", error);
9897                 if (err_code != NULL)
9898                         *err_code = error;
9899                 return false;
9900         }
9901
9902         if (result_mailbox->mailbox_name) {
9903                 if (strstr(result_mailbox->mailbox_name, "'"))
9904                         target_mailbox_name = em_replace_all_string(result_mailbox->mailbox_name, "'", "''");
9905                 else
9906                         target_mailbox_name = strdup(result_mailbox->mailbox_name);
9907         }
9908
9909         target_mailbox_type = result_mailbox->mailbox_type;
9910         target_account_id   = result_mailbox->account_id;
9911         emstorage_free_mailbox(&result_mailbox, 1, NULL);
9912
9913         conditional_clause_len =  (sizeof(char) * 8 * number_of_mails) + 512;
9914         conditional_clause = em_malloc(conditional_clause_len);
9915         if (conditional_clause == NULL) {
9916                 EM_DEBUG_EXCEPTION("em_mallocfailed");
9917                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9918                 goto FINISH_OFF;
9919         }
9920         cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE mail_id in (");
9921
9922         for (i = 0; i < number_of_mails; i++)
9923                 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
9924
9925         /* prevent 34415 */
9926         char *last_comma = rindex(conditional_clause, ',');
9927         if (last_comma) *last_comma = ')'; /* replace , with) */
9928
9929         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9930
9931         /* Updating a mail_tbl */
9932
9933         sql_query_string = em_malloc(conditional_clause_len);
9934         if (sql_query_string == NULL) {
9935                 EM_DEBUG_EXCEPTION("em_mallocfailed");
9936                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9937                 goto FINISH_OFF;
9938         }
9939         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);
9940         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9941
9942         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9943         if (error != EMAIL_ERROR_NONE) {
9944                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9945                 goto FINISH_OFF;
9946         }
9947
9948         /* Updating a mail_attachment_tbl */
9949         memset(sql_query_string, 0x00, conditional_clause_len);
9950         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);
9951         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9952         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9953         if (error != EMAIL_ERROR_NONE) {
9954                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9955                 goto FINISH_OFF;
9956         }
9957
9958         /* Updating a mail_meeting_tbl */
9959         memset(sql_query_string, 0x00, conditional_clause_len);
9960         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);
9961         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9962         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9963         if (error != EMAIL_ERROR_NONE) {
9964                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9965                 goto FINISH_OFF;
9966         }
9967
9968 #ifdef __FEATURE_BODY_SEARCH__
9969         /* Updating mail_text_tbl */
9970         memset(sql_query_string, 0x00, conditional_clause_len);
9971         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);
9972         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9973         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9974         if (error != EMAIL_ERROR_NONE) {
9975                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9976                 goto FINISH_OFF;
9977         }
9978 #endif
9979
9980         /* Updating a mail_read_mail_uid_tbl */
9981         memset(conditional_clause, 0x00, conditional_clause_len);
9982         cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE local_uid in (");
9983
9984         for (i = 0; i < number_of_mails; i++)
9985                 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
9986
9987         /* prevent 34415 */
9988         last_comma = rindex(conditional_clause, ',');
9989         if (last_comma) *last_comma = ')'; /* replace , with) */
9990
9991         memset(sql_query_string, 0x00, conditional_clause_len);
9992         SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_read_mail_uid_tbl SET mailbox_name = ? mailbox_id = ? account_id = ? %s", conditional_clause);
9993
9994         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9995         error = emstorage_exec_query_by_prepare_v2_stmt(local_db_handle, sql_query_string, target_mailbox_name, input_mailbox_id, target_account_id);
9996         if (error != EMAIL_ERROR_NONE) {
9997                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2_stmt failed:[%d]", error);
9998                 goto FINISH_OFF;
9999         }
10000
10001         ret = true;
10002
10003 FINISH_OFF:
10004         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10005
10006         EM_SAFE_FREE(target_mailbox_name);
10007         EM_SAFE_FREE(conditional_clause);
10008         EM_SAFE_FREE(sql_query_string);
10009
10010         if (err_code != NULL)
10011                 *err_code = error;
10012
10013         EM_DEBUG_FUNC_END("ret [%d]", ret);
10014         return ret;
10015 }
10016
10017 INTERNAL_FUNC int emstorage_delete_mail(char *multi_user_name, int mail_id, int from_server, int transaction, int *err_code)
10018 {
10019         EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
10020
10021         if (!mail_id) {
10022                 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
10023                 if (err_code != NULL)
10024                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10025                 return false;
10026         }
10027
10028         int ret = false;
10029         int error = EMAIL_ERROR_NONE;
10030         char sql_query_string[QUERY_SIZE] = {0, };
10031         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10032         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10033
10034         memset(sql_query_string, 0x00, sizeof(sql_query_string));
10035
10036         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id = %d ", mail_id);
10037         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10038         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10039         if (error != EMAIL_ERROR_NONE) {
10040                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10041                 goto FINISH_OFF;
10042         }
10043
10044         ret = true;
10045
10046 FINISH_OFF:
10047         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10048
10049         if (err_code != NULL)
10050                 *err_code = error;
10051
10052         EM_DEBUG_FUNC_END("ret [%d]", ret);
10053         return ret;
10054 }
10055
10056 INTERNAL_FUNC int emstorage_delete_multiple_mails(char *multi_user_name, int mail_ids[], int number_of_mails, int transaction, int *err_code)
10057 {
10058         EM_DEBUG_FUNC_BEGIN("mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", mail_ids, number_of_mails, transaction, err_code);
10059
10060         int ret = false, i, cur_sql_query_string = 0;
10061         int error = EMAIL_ERROR_NONE;
10062         int query_size = 0;
10063         char *sql_query_string = NULL;
10064
10065         if (!mail_ids) {
10066                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10067                 if (err_code != NULL)
10068                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10069                 return false;
10070         }
10071
10072         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10073         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10074
10075         query_size = (sizeof(char) * 8 * number_of_mails) + 512;
10076         sql_query_string =  em_malloc(query_size);
10077         if (sql_query_string == NULL) {
10078                 EM_DEBUG_EXCEPTION("em_mallocfailed");
10079                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10080                 goto FINISH_OFF;
10081         }
10082
10083         cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_tbl WHERE mail_id in (");
10084
10085         for (i = 0; i < number_of_mails; i++)
10086                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
10087
10088         /* prevent 34414 */
10089         char *last_comma = rindex(sql_query_string, ',');
10090         if (last_comma != NULL) *last_comma = ')'; /* replace , with) */
10091
10092         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10093         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10094         if (error != EMAIL_ERROR_NONE) {
10095                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10096                 goto FINISH_OFF;
10097         }
10098
10099 #ifdef __FEATURE_BODY_SEARCH__
10100         /* delete mail_text from mail_text_tbl */
10101         cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_text_tbl WHERE mail_id in (");
10102
10103         for (i = 0; i < number_of_mails; i++)
10104                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
10105
10106         last_comma = rindex(sql_query_string, ',');
10107         *last_comma = ')'; /* replace , with) */
10108
10109         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10110         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10111         if (error != EMAIL_ERROR_NONE) {
10112                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10113                 goto FINISH_OFF;
10114         }
10115 #endif
10116
10117         ret = true;
10118
10119 FINISH_OFF:
10120         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10121
10122         EM_SAFE_FREE(sql_query_string);
10123         if (err_code != NULL)
10124                 *err_code = error;
10125
10126         EM_DEBUG_FUNC_END("ret [%d]", ret);
10127         return ret;
10128 }
10129
10130 INTERNAL_FUNC int emstorage_delete_mail_by_account(char *multi_user_name, int account_id, int transaction, int *err_code)
10131 {
10132         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
10133
10134         if (account_id < FIRST_ACCOUNT_ID) {
10135                 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
10136
10137                 if (err_code != NULL)
10138                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10139                 return false;
10140         }
10141
10142         int rc, ret = false;
10143         int error = EMAIL_ERROR_NONE;
10144         char sql_query_string[QUERY_SIZE] = {0, };
10145
10146         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10147         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10148
10149         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d", account_id);
10150         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10151         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10152         if (error != EMAIL_ERROR_NONE) {
10153                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10154                 goto FINISH_OFF;
10155         }
10156
10157         rc = sqlite3_changes(local_db_handle);
10158         if (rc == 0) {
10159                 EM_DEBUG_LOG(" no mail found...");
10160                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10161         }
10162
10163         /* Delete all mails  mail_read_mail_uid_tbl table based on account id */
10164         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
10165         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10166         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10167         if (error != EMAIL_ERROR_NONE) {
10168                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10169                 goto FINISH_OFF;
10170         }
10171
10172         rc = sqlite3_changes(local_db_handle);
10173         if (rc == 0) {
10174                 EM_DEBUG_LOG("No mail found...");
10175                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10176         }
10177
10178 #ifdef __FEATURE_BODY_SEARCH__
10179         /* Delete all mail_text in mail_text_tbl table based on account id */
10180         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE account_id = %d", account_id);
10181         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10182         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10183         if (error != EMAIL_ERROR_NONE) {
10184                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10185                 goto FINISH_OFF;
10186         }
10187
10188         rc = sqlite3_changes(local_db_handle);
10189         if (rc == 0) {
10190                 EM_DEBUG_LOG("No mail found...");
10191                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10192         }
10193 #endif
10194
10195         ret = true;
10196
10197 FINISH_OFF:
10198         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10199
10200         if (error == EMAIL_ERROR_NONE) {
10201                 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_WITH_ACCOUNT, account_id, 0 , NULL, 0))
10202                         EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAIL_DELETE_ALL ]");
10203         }
10204
10205         if (err_code != NULL)
10206                 *err_code = error;
10207
10208         EM_DEBUG_FUNC_END("ret [%d]", ret);
10209         return ret;
10210 }
10211
10212 INTERNAL_FUNC int emstorage_delete_mail_by_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox, int transaction, int *err_code)
10213 {
10214         EM_DEBUG_FUNC_BEGIN("mailbox[%p], transaction[%d], err_code[%p]", mailbox, transaction, err_code);
10215
10216         if (mailbox == NULL) {
10217                 EM_DEBUG_EXCEPTION("mailbox [%p]", mailbox);
10218                 if (err_code != NULL)
10219                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10220                 return false;
10221         }
10222
10223         int ret = false;
10224         int error = EMAIL_ERROR_NONE;
10225         char sql_query_string[QUERY_SIZE] = {0, };
10226         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10227         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10228
10229         if (strcmp(mailbox->mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
10230                 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);
10231                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10232                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10233                 if (error != EMAIL_ERROR_NONE) {
10234                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10235                         goto FINISH_OFF;
10236                 }
10237
10238                 /* Delete Mails from mail_read_mail_uid_tbl */
10239                 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);
10240                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10241                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10242                 if (error != EMAIL_ERROR_NONE) {
10243                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10244                         goto FINISH_OFF;
10245                 }
10246         } else {
10247                 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);
10248                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10249                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10250                 if (error != EMAIL_ERROR_NONE) {
10251                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10252                         goto FINISH_OFF;
10253                 }
10254
10255                 /* Delete Mails from mail_read_mail_uid_tbl */
10256                 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);
10257                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10258                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10259                 if (error != EMAIL_ERROR_NONE) {
10260                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10261                         goto FINISH_OFF;
10262                 }
10263
10264 #ifdef __FEATURE_BODY_SEARCH__
10265                 /* Delete Mails from mail_text_tbl */
10266                 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);
10267                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10268                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10269                 if (error != EMAIL_ERROR_NONE) {
10270                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10271                         goto FINISH_OFF;
10272                 }
10273 #endif
10274         }
10275         ret = true;
10276
10277 FINISH_OFF:
10278         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10279
10280         if (error == EMAIL_ERROR_NONE) {
10281                 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_ALL, mailbox->account_id, mailbox->mailbox_id , mailbox->mailbox_name, 0))
10282                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventFailed [ NOTI_MAIL_DELETE_ALL ] >>>> ");
10283         }
10284
10285         if (err_code != NULL)
10286                 *err_code = error;
10287
10288         EM_DEBUG_FUNC_END("ret [%d]", ret);
10289         return ret;
10290 }
10291
10292 INTERNAL_FUNC int emstorage_free_mail(emstorage_mail_tbl_t** mail_list, int count, int *err_code)
10293 {
10294         EM_DEBUG_FUNC_BEGIN("mail_list[%p], count[%d], err_code[%p]", mail_list, count, err_code);
10295
10296         if (count > 0) {
10297                 if ((mail_list == NULL) || (*mail_list == NULL)) {
10298                         EM_DEBUG_EXCEPTION("mail_ilst[%p], count[%d]", mail_list, count);
10299
10300                         if (err_code)
10301                                 *err_code = EMAIL_ERROR_INVALID_PARAM;
10302                         return false;
10303                 }
10304
10305                 emstorage_mail_tbl_t* p = *mail_list;
10306                 int i = 0;
10307
10308                 for (; i < count; i++, p++) {
10309                         EM_SAFE_FREE(p->server_mailbox_name);
10310                         EM_SAFE_FREE(p->server_mail_id);
10311                         EM_SAFE_FREE(p->full_address_from);
10312                         EM_SAFE_FREE(p->full_address_reply);
10313                         EM_SAFE_FREE(p->full_address_to);
10314                         EM_SAFE_FREE(p->full_address_cc);
10315                         EM_SAFE_FREE(p->full_address_bcc);
10316                         EM_SAFE_FREE(p->full_address_return);
10317                         EM_SAFE_FREE(p->subject);
10318                         EM_SAFE_FREE(p->file_path_plain);
10319                         EM_SAFE_FREE(p->file_path_html);
10320                         EM_SAFE_FREE(p->file_path_mime_entity);
10321                         EM_SAFE_FREE(p->message_id);
10322                         EM_SAFE_FREE(p->email_address_sender);
10323                         EM_SAFE_FREE(p->email_address_recipient);
10324                         EM_SAFE_FREE(p->preview_text);
10325                         EM_SAFE_FREE(p->alias_sender);
10326                         EM_SAFE_FREE(p->alias_recipient);
10327                         EM_SAFE_FREE(p->default_charset);
10328                         EM_SAFE_FREE(p->pgp_password);
10329                         EM_SAFE_FREE(p->eas_data);
10330                         EM_SAFE_FREE(p->user_name);
10331                 }
10332                 EM_SAFE_FREE(*mail_list);
10333         }
10334
10335         if (err_code != NULL)
10336                 *err_code = EMAIL_ERROR_NONE;
10337
10338         EM_DEBUG_FUNC_END();
10339         return true;
10340 }
10341
10342 #ifdef __FEATURE_BODY_SEARCH__
10343 INTERNAL_FUNC void emstorage_free_mail_text(emstorage_mail_text_tbl_t** mail_text_list, int count, int *err_code)
10344 {
10345         EM_DEBUG_FUNC_BEGIN("mail_text_list[%p], count[%d], err_code[%p]", mail_text_list, count, err_code);
10346
10347         if (count > 0) {
10348                 if ((mail_text_list == NULL) || (*mail_text_list == NULL)) {
10349                         EM_DEBUG_LOG("Nothing to free: mail_text_list[%p]", mail_text_list);
10350                         return;
10351                 }
10352
10353                 emstorage_mail_text_tbl_t *p = *mail_text_list;
10354                 int i = 0;
10355
10356                 for (; i < count; i++, p++)
10357                         EM_SAFE_FREE(p->body_text);
10358
10359                 EM_SAFE_FREE(*mail_text_list);
10360         }
10361
10362         EM_DEBUG_FUNC_END();
10363 }
10364 #endif
10365
10366 INTERNAL_FUNC int emstorage_get_attachment_count(char *multi_user_name, int mail_id, int *count, int transaction, int *err_code)
10367 {
10368         EM_DEBUG_FUNC_BEGIN("mail_id[%d], count[%p], transaction[%d], err_code[%p]", mail_id, count, transaction, err_code);
10369
10370         if (mail_id <= 0 || !count) {
10371                 EM_DEBUG_EXCEPTION("mail_id[%d], count[%p]", mail_id, count);
10372                 if (err_code != NULL)
10373                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10374                 return false;
10375         }
10376
10377         int rc = -1, ret = false;
10378         int error = EMAIL_ERROR_NONE;
10379         char sql_query_string[QUERY_SIZE] = {0, };
10380
10381         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10382         EMSTORAGE_START_READ_TRANSACTION(transaction);
10383
10384         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
10385
10386         char **result;
10387
10388         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10389         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10390                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10391
10392         *count = atoi(result[1]);
10393         sqlite3_free_table(result);
10394
10395         ret = true;
10396
10397 FINISH_OFF:
10398         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10399
10400         if (err_code != NULL)
10401                 *err_code = error;
10402
10403         EM_DEBUG_FUNC_END("ret [%d]", ret);
10404         return ret;
10405 }
10406
10407 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)
10408 {
10409         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);
10410
10411         if (input_mail_id <= 0 || !output_attachment_list || !output_attachment_count) {
10412                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10413                 return EMAIL_ERROR_INVALID_PARAM;
10414         }
10415
10416         int                         error = EMAIL_ERROR_NONE;
10417         int                         i = 0;
10418         int                         rc = -1;
10419         char                      **result = NULL;
10420         char                        sql_query_string[QUERY_SIZE] = {0, };
10421         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10422         DB_STMT hStmt = NULL;
10423         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10424
10425         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
10426         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", input_mail_id);
10427         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10428         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10429                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10430
10431         *output_attachment_count = atoi(result[1]);
10432         sqlite3_free_table(result);
10433
10434         if (*output_attachment_count == 0) {
10435                 error = EMAIL_ERROR_NONE;
10436                 goto FINISH_OFF;
10437         }
10438
10439         p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * (*output_attachment_count));
10440
10441         if (!p_data_tbl) {
10442                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
10443                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10444                 goto FINISH_OFF;
10445         }
10446
10447         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE mail_id = %d ORDER BY attachment_id", input_mail_id);
10448         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
10449
10450         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10451         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)));
10452
10453         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10454         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },  ("sqlite3_step fail:%d", rc));
10455
10456         if (rc == SQLITE_DONE) {
10457                 EM_DEBUG_EXCEPTION("no matched attachment found...");
10458                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10459                 goto FINISH_OFF;
10460         }
10461         for (i = 0; i < *output_attachment_count; i++) {
10462                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10463                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10464                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10465                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10466                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10467                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10468                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10469                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10470                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10471                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10472                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10473                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10474                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10475                 EM_DEBUG_LOG("attachment[%d].attachment_id : %d", i, p_data_tbl[i].attachment_id);
10476                 EM_DEBUG_LOG("attachment_mime_type : %s", p_data_tbl[i].attachment_mime_type);
10477                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10478                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
10479         }
10480
10481 FINISH_OFF:
10482
10483         if (error == EMAIL_ERROR_NONE)
10484                 *output_attachment_list = p_data_tbl;
10485         else if (p_data_tbl != NULL)
10486                 emstorage_free_attachment(&p_data_tbl, *output_attachment_count, NULL);
10487
10488         if (hStmt) {
10489                 rc = sqlite3_finalize(hStmt);
10490                 if (rc != SQLITE_OK) {
10491                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10492                         error = EMAIL_ERROR_DB_FAILURE;
10493                         if (*output_attachment_list)
10494                                 emstorage_free_attachment(output_attachment_list, *output_attachment_count, NULL); /* prevent */
10495                 }
10496         }
10497
10498         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
10499
10500         EM_DEBUG_FUNC_END("error [%d]", error);
10501         return error;
10502 }
10503
10504 INTERNAL_FUNC int emstorage_get_attachment(char *multi_user_name, int attachment_id, emstorage_attachment_tbl_t **attachment, int transaction, int *err_code)
10505 {
10506         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], transaction[%d], err_code[%p]", attachment_id, attachment, transaction, err_code);
10507
10508         if (attachment_id <= 0 || !attachment) {
10509                 EM_DEBUG_EXCEPTION("attachment_id[%d], attachment[%p]", attachment_id, attachment);
10510                 if (err_code != NULL)
10511                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10512                 return false;
10513         }
10514
10515         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10516         int rc, ret = false;
10517         int error = EMAIL_ERROR_NONE;
10518         char sql_query_string[QUERY_SIZE] = {0, };
10519
10520         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10521         EMSTORAGE_START_READ_TRANSACTION(transaction);
10522
10523         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_id = %d",  attachment_id);
10524
10525         sqlite3_stmt* hStmt = NULL;
10526
10527         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10528         EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
10529
10530         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10531                         ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10532
10533
10534         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10535         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10536                         ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10537
10538         if (rc == SQLITE_DONE) {
10539                 EM_DEBUG_LOG("no matched attachment found...");
10540                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10541                 goto FINISH_OFF;
10542         }
10543
10544         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10545                 EM_DEBUG_EXCEPTION("malloc failed...");
10546                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10547                 goto FINISH_OFF;
10548         }
10549
10550         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10551         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10552         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10553         _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10554         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10555         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10556         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10557         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10558         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10559         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10560         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10561         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10562         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10563
10564 #ifdef __ATTACHMENT_OPTI__
10565         _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10566         _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10567 #endif
10568
10569         ret = true;
10570
10571 FINISH_OFF:
10572         if (ret == true)
10573                 *attachment = p_data_tbl;
10574
10575         if (hStmt != NULL) {
10576                 rc = sqlite3_finalize(hStmt);
10577                 if (rc != SQLITE_OK) {
10578                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10579                         error = EMAIL_ERROR_DB_FAILURE;
10580                 }
10581         }
10582
10583         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10584
10585         if (err_code != NULL)
10586                 *err_code = error;
10587
10588         EM_DEBUG_FUNC_END("ret [%d]", ret);
10589         return ret;
10590 }
10591
10592 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)
10593 {
10594         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);
10595
10596         if (mail_id <= 0 || nth <= 0 || !attachment_tbl) {
10597                 EM_DEBUG_EXCEPTION(" mail_id[%d], nth[%d], attachment[%p]", mail_id, nth, attachment_tbl);
10598
10599                 if (err_code != NULL)
10600                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10601                 return false;
10602         }
10603
10604         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10605         char *p = NULL;
10606         int rc, ret = false;
10607         int error = EMAIL_ERROR_NONE;
10608         char sql_query_string[QUERY_SIZE] = {0, };
10609
10610         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10611         EMSTORAGE_START_READ_TRANSACTION(transaction);
10612
10613         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));
10614         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10615
10616         DB_STMT hStmt = NULL;
10617
10618         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10619         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10620                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10621
10622
10623         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10624         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10625                         ("sqlite3_step fail:%d", rc));
10626
10627         if (rc == SQLITE_DONE) {
10628                 EM_DEBUG_EXCEPTION("no matched attachment found: mail_id[%d] nth[%d]", mail_id, nth);
10629                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10630                 goto FINISH_OFF;
10631         }
10632
10633         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10634                 EM_DEBUG_EXCEPTION(" malloc failed...");
10635                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10636                 goto FINISH_OFF;
10637         }
10638
10639         p_data_tbl->attachment_id = sqlite3_column_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10640         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10641                 p_data_tbl->attachment_name = cpy_str(p);
10642         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10643                 p_data_tbl->attachment_path = cpy_str(p);
10644         if ((p = (char *)sqlite3_column_text(hStmt, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10645                 p_data_tbl->content_id = cpy_str(p);
10646         p_data_tbl->attachment_size = sqlite3_column_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10647         p_data_tbl->mail_id = sqlite3_column_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10648         p_data_tbl->account_id = sqlite3_column_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10649         p_data_tbl->mailbox_id = sqlite3_column_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10650         p_data_tbl->attachment_save_status = sqlite3_column_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10651         p_data_tbl->attachment_drm_type = sqlite3_column_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10652         p_data_tbl->attachment_drm_method = sqlite3_column_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10653         p_data_tbl->attachment_inline_content_status = sqlite3_column_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10654         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10655                 p_data_tbl->attachment_mime_type = cpy_str(p);
10656 #ifdef __ATTACHMENT_OPTI__
10657         p_data_tbl->encoding = sqlite3_column_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10658         if ((p = (char *)sqlite3_column_text(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10659                 p_data_tbl->section = cpy_str(p);
10660 #endif
10661         ret = true;
10662
10663 FINISH_OFF:
10664         if (ret == true)
10665                 *attachment_tbl = p_data_tbl;
10666
10667         if (hStmt != NULL) {
10668                 rc = sqlite3_finalize(hStmt);
10669                 if (rc != SQLITE_OK) {
10670                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10671                         error = EMAIL_ERROR_DB_FAILURE;
10672                 }
10673         }
10674
10675         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10676
10677         if (err_code != NULL)
10678                 *err_code = error;
10679
10680         EM_DEBUG_FUNC_END("ret [%d]", ret);
10681         return ret;
10682 }
10683
10684 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)
10685 {
10686         EM_DEBUG_FUNC_BEGIN("attachment_path[%p], attachment[%p], transaction[%d], err_code[%p]", attachment_path, attachment, transaction, err_code);
10687
10688         if (attachment_path == NULL || !attachment) {
10689                 EM_DEBUG_EXCEPTION("attachment_path[%p], attachment[%p]", attachment_path, attachment);
10690                 if (err_code != NULL)
10691                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10692                 return false;
10693         }
10694
10695         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10696         int rc, ret = false;
10697         int error = EMAIL_ERROR_NONE;
10698         char sql_query_string[QUERY_SIZE] = {0, };
10699
10700         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10701         EMSTORAGE_START_READ_TRANSACTION(transaction);
10702
10703         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_path = '%s'", attachment_path);
10704
10705         sqlite3_stmt* hStmt = NULL;
10706
10707         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10708         EM_DEBUG_LOG_DEV("before sqlite3_prepare hStmt = %p", hStmt);
10709
10710         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10711                         ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10712
10713
10714         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10715         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10716                         ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10717
10718         if (rc == SQLITE_DONE) {
10719                 EM_DEBUG_LOG("no matched attachment found...");
10720                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10721                 goto FINISH_OFF;
10722         }
10723
10724         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10725                 EM_DEBUG_EXCEPTION("malloc failed...");
10726                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10727                 goto FINISH_OFF;
10728         }
10729
10730         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10731         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10732         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10733         _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10734         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10735         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10736         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10737         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10738         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10739         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10740         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10741         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10742         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10743
10744 #ifdef __ATTACHMENT_OPTI__
10745         _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10746         _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10747 #endif
10748
10749         ret = true;
10750
10751 FINISH_OFF:
10752         if (ret == true)
10753                 *attachment = p_data_tbl;
10754
10755         if (hStmt != NULL) {
10756                 rc = sqlite3_finalize(hStmt);
10757                 if (rc != SQLITE_OK) {
10758                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10759                         error = EMAIL_ERROR_DB_FAILURE;
10760                 }
10761         }
10762
10763         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10764
10765         if (err_code != NULL)
10766                 *err_code = error;
10767
10768         EM_DEBUG_FUNC_END("ret [%d]", ret);
10769         return ret;
10770 }
10771
10772 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)
10773 {
10774         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], attachment[%p], transaction[%d], err_code[%p]", mail_id, type, attachment, transaction, err_code);
10775
10776         if (mail_id <= 0 || !attachment) {
10777                 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], attachment[%p]", mail_id, type, attachment);
10778                 if (err_code != NULL)
10779                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10780                 return false;;
10781         }
10782
10783         int rc, ret = false;
10784         int error = EMAIL_ERROR_NONE;
10785         DB_STMT hStmt = NULL;
10786         char sql_query_string[QUERY_SIZE] = {0, };
10787
10788         int i = 0;
10789
10790         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10791
10792         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10793
10794         switch (type) {
10795         case UPDATE_MAILBOX:
10796                 EM_DEBUG_LOG("UPDATE_MAILBOX");
10797                 if (!attachment->mailbox_id) {
10798                         EM_DEBUG_EXCEPTION(" attachment->mailbox_id[%d]", attachment->mailbox_id);
10799                         error = EMAIL_ERROR_INVALID_PARAM;
10800                         goto FINISH_OFF;
10801                 }
10802                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10803                                 "UPDATE mail_attachment_tbl SET mailbox_id = ? WHERE mail_id = %d", mail_id);
10804
10805                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10806                 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10807                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10808                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10809
10810                 _bind_stmt_field_data_int(hStmt, i++, attachment->mailbox_id);
10811                 break;
10812
10813         case UPDATE_SAVENAME:
10814                 EM_DEBUG_LOG("UPDATE_SAVENAME");
10815                 if (!attachment->attachment_path) {
10816                         EM_DEBUG_EXCEPTION(" attachment->attachment_path[%p]", attachment->attachment_path);
10817                         error = EMAIL_ERROR_INVALID_PARAM;
10818                         goto FINISH_OFF;
10819                 }
10820
10821                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10822                                 "UPDATE mail_attachment_tbl SET"
10823                                 "  attachment_size = ?"
10824                                 ", attachment_save_status = ?"
10825                                 ", attachment_path = ?"
10826                                 " WHERE mail_id = %d"
10827                                 " AND attachment_id = %d"
10828                                 , attachment->mail_id
10829                                 , attachment->attachment_id);
10830
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(" Before sqlite3_prepare hStmt = %p", hStmt);
10834                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10835                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10836
10837                 _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_size);
10838                 _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_save_status);
10839                 _bind_stmt_field_data_string(hStmt, i++, (char *)attachment->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10840                 break;
10841
10842         default:
10843                 EM_DEBUG_LOG("type[%d]", type);
10844                 error = EMAIL_ERROR_INVALID_PARAM;
10845                 goto FINISH_OFF;
10846         }
10847         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10848
10849         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10850         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10851                         ("sqlite3_step fail:%d", rc));
10852         ret = true;
10853
10854 FINISH_OFF:
10855
10856         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10857
10858         if (hStmt != NULL) {
10859                 rc = sqlite3_finalize(hStmt);
10860                 if (rc != SQLITE_OK) {
10861                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10862                         error = EMAIL_ERROR_DB_FAILURE;
10863                 }
10864         }
10865
10866         if (err_code != NULL)
10867                 *err_code = error;
10868         EM_DEBUG_FUNC_END("ret [%d]", ret);
10869         return ret;
10870 }
10871
10872 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)
10873 {
10874         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);
10875
10876         int rc = 0;
10877         int ret = false;
10878         int error = EMAIL_ERROR_NONE;
10879         int field_idx = 0;
10880         int account_id = 0;
10881         char sql_query_string[QUERY_SIZE] = {0, };
10882         char *replaced_mailbox_name = NULL;
10883         char *replaced_alias = NULL;
10884         sqlite3 *local_db_handle = NULL;
10885         DB_STMT hStmt = NULL;
10886         emstorage_mailbox_tbl_t *old_mailbox_data = NULL;
10887
10888         if (input_mailbox_id <= 0 || !input_new_mailbox_name || !input_new_mailbox_alias) {
10889                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10890                 return EMAIL_ERROR_INVALID_PARAM;
10891         }
10892
10893         if (strstr(input_new_mailbox_name, "'"))
10894                 replaced_mailbox_name = em_replace_all_string(input_new_mailbox_name, "'", "''");
10895         else
10896                 replaced_mailbox_name = strdup(input_new_mailbox_name);
10897
10898         if (strstr(input_new_mailbox_alias, "'"))
10899                 replaced_alias = em_replace_all_string(input_new_mailbox_alias, "'", "''");
10900         else
10901                 replaced_alias = strdup(input_new_mailbox_alias);
10902
10903         local_db_handle = emstorage_get_db_connection(multi_user_name);
10904
10905         if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
10906                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", error);
10907                 EM_SAFE_FREE(replaced_mailbox_name);
10908                 EM_SAFE_FREE(replaced_alias);
10909                 return error;
10910         }
10911
10912         if (old_mailbox_data == NULL) {
10913                 EM_DEBUG_LOG("old_mailbox_data is NULL");
10914                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
10915                 goto FINISH_OFF;
10916         }
10917
10918         account_id = old_mailbox_data->account_id;
10919
10920         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
10921
10922         if (input_eas_data && input_eas_data_length > 0) {
10923                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10924                                 "UPDATE mail_box_tbl SET"
10925                                 " mailbox_name = ?"
10926                                 ",alias = ?"
10927                                 ",eas_data = ?"
10928                                 ",eas_data_length = ?"
10929                                 " WHERE mailbox_id = ?");
10930
10931                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10932
10933                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10934                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10935
10936                 _bind_stmt_field_data_string(hStmt, field_idx++, input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10937                 _bind_stmt_field_data_string(hStmt, field_idx++, input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10938                 _bind_stmt_field_data_blob(hStmt, field_idx++, input_eas_data, input_eas_data_length);
10939                 _bind_stmt_field_data_int(hStmt, field_idx++, input_eas_data_length);
10940                 _bind_stmt_field_data_int(hStmt, field_idx++, input_mailbox_id);
10941         } else {
10942                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10943                                 "UPDATE mail_box_tbl SET"
10944                                 " mailbox_name = ?"
10945                                 ",alias = ?"
10946                                 " WHERE mailbox_id = ?");
10947
10948                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10949
10950                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10951                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10952
10953                 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10954                 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10955                 _bind_stmt_field_data_int(hStmt, field_idx++ , input_mailbox_id);
10956         }
10957
10958
10959         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10960         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
10961                         ("sqlite3_step fail:%d", rc));
10962         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10963                         ("sqlite3_step fail:%d", rc));
10964
10965         if (sqlite3_changes(local_db_handle) == 0)
10966                 EM_DEBUG_LOG("no mail_meeting_tbl matched...");
10967
10968         ret = true;
10969
10970 FINISH_OFF:
10971         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, error);
10972         if (error == EMAIL_ERROR_NONE) {
10973                 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME, account_id, input_mailbox_id, input_new_mailbox_name, 0))
10974                         EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAILBOX_RENAME ] >>>> ");
10975         } else {
10976                 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME_FAIL, account_id, input_mailbox_id, input_new_mailbox_name, error))
10977                         EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAILBOX_RENAME_FAIL ] >>>> ");
10978         }
10979
10980         EM_SAFE_FREE(replaced_mailbox_name);
10981         EM_SAFE_FREE(replaced_alias);
10982
10983         if (old_mailbox_data)
10984                 emstorage_free_mailbox(&old_mailbox_data, 1, NULL);
10985
10986         if (hStmt != NULL) {
10987                 rc = sqlite3_finalize(hStmt);
10988                 if (rc != SQLITE_OK) {
10989                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10990                         error = EMAIL_ERROR_DB_FAILURE;
10991                 }
10992         }
10993
10994         EM_DEBUG_FUNC_END("error [%d]", error);
10995         return error;
10996 }
10997
10998 INTERNAL_FUNC int emstorage_get_new_attachment_no(char *multi_user_name, int *attachment_no, int *err_code)
10999 {
11000         EM_DEBUG_FUNC_BEGIN("attachment_no [%p], err_code[%p]", attachment_no, err_code);
11001         int rc, ret = false;
11002         int error = EMAIL_ERROR_NONE;
11003         char *sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
11004         char **result;
11005
11006         if (!attachment_no) {
11007                 EM_DEBUG_EXCEPTION("Invalid attachment");
11008                 if (err_code != NULL)
11009                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11010                 return false;
11011         }
11012
11013         *attachment_no = -1;
11014
11015         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11016
11017
11018         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
11019         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
11020                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
11021
11022         if (NULL == result[1])
11023                 rc = 1;
11024         else
11025                 rc = atoi(result[1])+1;
11026
11027         sqlite3_free_table(result);
11028
11029         *attachment_no = rc;
11030         EM_DEBUG_LOG("attachment_no [%d]", *attachment_no);
11031         ret = true;
11032
11033 FINISH_OFF:
11034
11035         if (err_code != NULL)
11036                 *err_code = error;
11037
11038         EM_DEBUG_FUNC_END("ret [%d]", ret);
11039         return ret;
11040 }
11041
11042 INTERNAL_FUNC int emstorage_add_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int iscopy, int transaction, int *err_code)
11043 {
11044         EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], iscopy[%d], transaction[%d], err_code[%p]", attachment_tbl, iscopy, transaction, err_code);
11045
11046         char *sql = NULL;
11047         char **result;
11048         int rc, ret = false;
11049         int error = EMAIL_ERROR_NONE;
11050         char sql_query_string[QUERY_SIZE] = {0, };
11051
11052         DB_STMT hStmt = NULL;
11053         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11054
11055         if (!attachment_tbl) {
11056                 EM_DEBUG_EXCEPTION("attachment_tbl[%p], iscopy[%d]", attachment_tbl, iscopy);
11057                 if (err_code != NULL)
11058                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11059                 return false;
11060         }
11061
11062         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11063
11064         sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
11065
11066         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
11067         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
11068                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
11069
11070         if (NULL == result[1]) rc = 1;
11071         else rc = atoi(result[1]) + 1;
11072         sqlite3_free_table(result);
11073
11074         attachment_tbl->attachment_id = rc;
11075
11076         SNPRINTF(sql_query_string, sizeof(sql_query_string),
11077                         "INSERT INTO mail_attachment_tbl VALUES "
11078                         "(?"    /* attachment_id */
11079                         ", ?"   /* attachment_name */
11080                         ", ?"   /* attachment_path */
11081                         ", ?"   /* content_id */
11082                         ", ?"   /* attachment_size */
11083                         ", ?"   /* mail_id */
11084                         ", ?"   /* account_id */
11085                         ", ?"   /* mailbox_id */
11086                         ", ?"   /* attachment_save_status */
11087                         ", ?"   /* attachment_drm_type */
11088                         ", ?"   /* attachment_drm_method */
11089                         ", ?"   /* attachment_inline_content_status */
11090                         ", ?"   /* attachment_mime_type */
11091 #ifdef __ATTACHMENT_OPTI__
11092                         ", ?"
11093                         ", ?"
11094 #endif
11095                         ")");
11096
11097
11098         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11099         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11100                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11101
11102         _bind_stmt_field_data_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_id);
11103         _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);
11104         _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);
11105         _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);
11106         _bind_stmt_field_data_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_size);
11107         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mail_id);
11108         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->account_id);
11109         _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mailbox_id);
11110         _bind_stmt_field_data_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_save_status);
11111         _bind_stmt_field_data_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_type);
11112         _bind_stmt_field_data_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_method);
11113         _bind_stmt_field_data_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_inline_content_status);
11114         _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);
11115 #ifdef __ATTACHMENT_OPTI__
11116         _bind_stmt_field_data_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->encoding);
11117         _bind_stmt_field_data_string(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->section, 0, ATTACHMENT_LEN_IN_MAIL_ATTACHMENT_TBL);
11118 #endif
11119
11120
11121         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11122         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11123                         ("sqlite3_step fail:%d", rc));
11124         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11125                         ("sqlite3_step fail:%d", rc));
11126
11127         rc = sqlite3_changes(local_db_handle);
11128         if (rc == 0) {
11129                 EM_DEBUG_LOG(" no matched mail found...");
11130                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11131                 goto FINISH_OFF;
11132         }
11133
11134         ret = true;
11135
11136 FINISH_OFF:
11137         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11138
11139         if (hStmt != NULL) {
11140                 rc = sqlite3_finalize(hStmt);
11141                 if (rc != SQLITE_OK) {
11142                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11143                         error = EMAIL_ERROR_DB_FAILURE;
11144                 }
11145         }
11146
11147         if (err_code != NULL)
11148                 *err_code = error;
11149
11150         EM_DEBUG_FUNC_END("ret [%d]", ret);
11151         return ret;
11152 }
11153
11154 INTERNAL_FUNC int emstorage_update_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int transaction, int *err_code)
11155 {
11156         EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], transaction[%d], err_code[%p]", attachment_tbl, transaction, err_code);
11157
11158         int rc, ret = false, field_idx = 0;
11159         int error = EMAIL_ERROR_NONE;
11160         DB_STMT hStmt = NULL;
11161         char sql_query_string[QUERY_SIZE] = {0, };
11162
11163         if (!attachment_tbl) {
11164                 EM_DEBUG_EXCEPTION(" attachment_tbl[%p] ", attachment_tbl);
11165                 if (err_code)
11166                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11167                 return false;
11168         }
11169
11170         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11171
11172         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11173
11174         SNPRINTF(sql_query_string, sizeof(sql_query_string),
11175                         "UPDATE mail_attachment_tbl SET  "
11176                         "  attachment_name = ?"
11177                         ", attachment_path =  ?"
11178                         ", content_id = ?"
11179                         ", attachment_size = ?"
11180                         ", mail_id = ?"
11181                         ", account_id = ?"
11182                         ", mailbox_id = ?"
11183                         ", attachment_save_status = ?"
11184                         ", attachment_drm_type = ?"
11185                         ", attachment_drm_method = ?"
11186                         ", attachment_inline_content_status = ? "
11187                         ", attachment_mime_type = ? "
11188                         " WHERE attachment_id = ?;");
11189
11190
11191         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11192
11193         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11194                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11195
11196         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
11197         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
11198         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->content_id, 0, CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL);
11199         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_size);
11200         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->mail_id);
11201         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->account_id);
11202         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->mailbox_id);
11203         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_save_status);
11204         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_drm_type);
11205         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_drm_method);
11206         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_inline_content_status);
11207         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_mime_type, 0, ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL);
11208         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_id);
11209
11210
11211         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11212         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11213                         ("sqlite3_step fail:%d", rc));
11214         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11215                         ("sqlite3_step fail:%d", rc));
11216
11217         rc = sqlite3_changes(local_db_handle);
11218         if (rc == 0) {
11219                 EM_DEBUG_LOG(" no matched mail found...");
11220                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11221                 goto FINISH_OFF;
11222         }
11223
11224         ret = true;
11225
11226 FINISH_OFF:
11227         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11228         if (hStmt != NULL) {
11229                 rc = sqlite3_finalize(hStmt);
11230                 if (rc != SQLITE_OK) {
11231                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11232                         error = EMAIL_ERROR_DB_FAILURE;
11233                 }
11234         }
11235
11236         if (err_code != NULL)
11237                 *err_code = error;
11238
11239         EM_DEBUG_FUNC_END("ret [%d]", ret);
11240         return ret;
11241 }
11242
11243 INTERNAL_FUNC int emstorage_delete_attachment_on_db(char *multi_user_name, int attachment_id, int transaction, int *err_code)
11244 {
11245         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], transaction[%d], err_code[%p]", attachment_id, transaction, err_code);
11246
11247         if (attachment_id < 0) {
11248                 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
11249                 if (err_code != NULL)
11250                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11251                 return false;
11252         }
11253
11254         int ret = false;
11255         int error = EMAIL_ERROR_NONE;
11256         char sql_query_string[QUERY_SIZE] = {0, };
11257         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11258
11259         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11260
11261         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
11262
11263         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11264         if (error != EMAIL_ERROR_NONE) {
11265                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11266                 goto FINISH_OFF;
11267         }
11268
11269         ret = true;
11270
11271 FINISH_OFF:
11272         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11273
11274         if (err_code)
11275                 *err_code = error;
11276
11277         EM_DEBUG_FUNC_END("ret [%d]", ret);
11278         return ret;
11279 }
11280
11281 INTERNAL_FUNC int emstorage_delete_all_attachments_of_mail(char *multi_user_name, int mail_id, int transaction, int *err_code)
11282 {
11283         EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
11284         int ret = false;
11285         int error = EMAIL_ERROR_NONE;
11286         char sql_query_string[QUERY_SIZE] = {0, };
11287         sqlite3 *local_db_handle = NULL;
11288
11289         if (mail_id <= 0) {
11290                 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
11291                 if (err_code != NULL)
11292                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11293                 return false;
11294         }
11295
11296         local_db_handle = emstorage_get_db_connection(multi_user_name);
11297
11298         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11299
11300         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
11301         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11302         if (error != EMAIL_ERROR_NONE) {
11303                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11304                 goto FINISH_OFF;
11305         }
11306
11307         ret = true;
11308
11309 FINISH_OFF:
11310         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11311
11312         if (err_code)
11313                 *err_code = error;
11314
11315         EM_DEBUG_FUNC_END("ret [%d]", ret);
11316         return ret;
11317 }
11318
11319 INTERNAL_FUNC int emstorage_delete_attachment_all_on_db(char *multi_user_name, int account_id, char *mailbox, int transaction, int *err_code)
11320 {
11321         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox, transaction, err_code);
11322
11323         int error = EMAIL_ERROR_NONE;
11324         int ret = false;
11325         char sql_query_string[QUERY_SIZE] = {0, };
11326         char *replaced_mailbox = NULL;
11327         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11328
11329         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11330
11331         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl");
11332
11333         if (account_id != ALL_ACCOUNT) /*  '0' means all account */
11334                 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);
11335
11336         if (mailbox) /*  NULL means all mailbox_name */ {
11337                 if (strstr(mailbox, "'"))
11338                         replaced_mailbox = em_replace_all_string(mailbox, "'", "''");
11339                 else
11340                         replaced_mailbox = strdup(mailbox);
11341
11342                 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);
11343                 EM_SAFE_FREE(replaced_mailbox); /*prevent 49434*/
11344         }
11345
11346         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11347         if (error != EMAIL_ERROR_NONE) {
11348                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11349                 goto FINISH_OFF;
11350         }
11351
11352         ret = true;
11353
11354 FINISH_OFF:
11355         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11356
11357         if (err_code != NULL)
11358                 *err_code = error;
11359
11360         EM_DEBUG_FUNC_END("ret [%d]", ret);
11361         return ret;
11362 }
11363
11364 INTERNAL_FUNC int emstorage_free_attachment(emstorage_attachment_tbl_t** attachment_tbl_list, int count, int *err_code)
11365 {
11366         EM_DEBUG_FUNC_BEGIN("attachment_tbl_list[%p], count[%d], err_code[%p]", attachment_tbl_list, count, err_code);
11367
11368         if (count > 0) {
11369                 if ((attachment_tbl_list == NULL) || (*attachment_tbl_list == NULL)) {
11370                         EM_DEBUG_LOG("Nothing to free: attachment_tbl_list[%p], count[%d]", attachment_tbl_list, count);
11371                         if (err_code != NULL)
11372                                 *err_code = EMAIL_ERROR_INVALID_PARAM;
11373                         return false;
11374                 }
11375
11376                 emstorage_attachment_tbl_t* p = *attachment_tbl_list;
11377                 int i;
11378
11379                 for (i = 0; i < count; i++) {
11380                         EM_SAFE_FREE(p[i].attachment_name);
11381                         EM_SAFE_FREE(p[i].attachment_path);
11382                         EM_SAFE_FREE(p[i].content_id);
11383                         EM_SAFE_FREE(p[i].attachment_mime_type);
11384 #ifdef __ATTACHMENT_OPTI__
11385                         EM_SAFE_FREE(p[i].section);
11386 #endif
11387                 }
11388
11389                 EM_SAFE_FREE(p);
11390                 *attachment_tbl_list = NULL;
11391         }
11392
11393         if (err_code != NULL)
11394                 *err_code = EMAIL_ERROR_NONE;
11395         EM_DEBUG_FUNC_END();
11396         return true;
11397 }
11398
11399 INTERNAL_FUNC int emstorage_begin_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11400 {
11401         EM_DEBUG_FUNC_BEGIN();
11402         EM_PROFILE_BEGIN(emStorageBeginTransaction);
11403         int ret = true;
11404
11405         _timedlock_shm_mutex(mapped_for_db_lock, 2);
11406
11407         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11408
11409         int rc;
11410         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN immediate;", NULL, NULL, NULL), rc);
11411         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11412                         ("SQL(BEGIN) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11413
11414         if (ret == false) {
11415                 if (err_code != NULL) *err_code = EMAIL_ERROR_DB_FAILURE;
11416         }
11417
11418         EM_PROFILE_END(emStorageBeginTransaction);
11419         EM_DEBUG_FUNC_END("ret [%d]", ret);
11420         return ret;
11421 }
11422
11423 INTERNAL_FUNC int emstorage_commit_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11424 {
11425         EM_DEBUG_FUNC_BEGIN();
11426         int ret = true;
11427         int rc;
11428
11429         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11430
11431         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
11432         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; }, ("SQL(END) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11433
11434         if (ret == false && err_code != NULL)
11435                 *err_code = EMAIL_ERROR_DB_FAILURE;
11436
11437         _unlockshm_mutex(mapped_for_db_lock);
11438
11439         EM_DEBUG_FUNC_END("ret [%d]", ret);
11440         return ret;
11441 }
11442
11443 INTERNAL_FUNC int emstorage_rollback_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11444 {
11445         EM_DEBUG_FUNC_BEGIN();
11446         int ret = true;
11447         int rc;
11448         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11449
11450         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "ROLLBACK;", NULL, NULL, NULL), rc);
11451         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11452                         ("SQL(ROLLBACK) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11453
11454         if (ret == false && err_code != NULL)
11455                 *err_code = EMAIL_ERROR_DB_FAILURE;
11456
11457         _unlockshm_mutex(mapped_for_db_lock);
11458
11459         EM_DEBUG_FUNC_END("ret [%d]", ret);
11460         return ret;
11461 }
11462
11463 INTERNAL_FUNC int emstorage_is_mailbox_full(char *multi_user_name, int account_id, email_mailbox_t *mailbox, int *result, int *err_code)
11464 {
11465         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], result[%p], err_code[%p]", account_id, mailbox, result, err_code);
11466
11467         if (account_id < FIRST_ACCOUNT_ID || !mailbox || !result) {
11468                 if (mailbox)
11469                         EM_DEBUG_EXCEPTION("Invalid Parameter. accoun_id[%d], mailbox[%p]", account_id, mailbox);
11470
11471                 if (err_code != NULL)
11472                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11473
11474                 return false;
11475         }
11476
11477         int ret = false;
11478         int error = EMAIL_ERROR_NONE;
11479         int mail_count = 0;
11480
11481         if (!emstorage_get_mail_count(multi_user_name, account_id, mailbox->mailbox_id, &mail_count, NULL, true, &error)) {
11482                 EM_DEBUG_EXCEPTION("emstorage_get_mail_count failed [%d]", error);
11483                 goto FINISH_OFF;
11484         }
11485
11486         if (mailbox) {
11487                 EM_DEBUG_LOG("mail_count[%d] mail_slot_size[%d]", mail_count, mailbox->mail_slot_size);
11488                 if (mail_count >= mailbox->mail_slot_size)
11489                         *result = true;
11490                 else
11491                         *result = false;
11492
11493                 ret = true;
11494         }
11495
11496         ret = true;
11497 FINISH_OFF:
11498
11499         if (err_code != NULL)
11500                 *err_code = error;
11501
11502         EM_DEBUG_FUNC_END("ret [%d]", ret);
11503         return ret;
11504 }
11505
11506 INTERNAL_FUNC int emstorage_clear_mail_data(char *multi_user_name, int transaction, int *err_code)
11507 {
11508         EM_DEBUG_FUNC_BEGIN("transaction[%d], err_code[%p]", transaction, err_code);
11509
11510         int ret = false;
11511         int error = EMAIL_ERROR_NONE;
11512         char sql_query_string[QUERY_SIZE] = {0, };
11513         const email_db_object_t* tables = _g_db_tables;
11514         const email_db_object_t* indexes = _g_db_indexes;
11515
11516         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11517         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11518
11519         if (!emstorage_delete_dir((char *)MAIL_HOME, &error)) {
11520                 EM_DEBUG_EXCEPTION(" emstorage_delete_dir failed - %d", error);
11521
11522                 goto FINISH_OFF;
11523         }
11524
11525         mkdir(MAIL_HOME, DIRECTORY_PERMISSION);
11526         mkdir(MAIL_TEMP, DIRECTORY_PERMISSION);
11527         chmod(MAIL_TEMP, 0777);
11528
11529         /*  first clear index. */
11530         while (indexes->object_name) {
11531                 if (indexes->data_flag) {
11532                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP index %s", indexes->object_name);
11533                         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11534                         if (error != EMAIL_ERROR_NONE) {
11535                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11536                                 goto FINISH_OFF;
11537                         }
11538                 }
11539                 indexes++;
11540         }
11541
11542         while (tables->object_name) {
11543                 if (tables->data_flag) {
11544                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP table %s", tables->object_name);
11545                         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11546                         if (error != EMAIL_ERROR_NONE) {
11547                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11548                                 goto FINISH_OFF;
11549                         }
11550                 }
11551
11552                 tables++;
11553         }
11554         ret = true;
11555
11556 FINISH_OFF:
11557         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11558
11559         if (err_code != NULL)
11560                 *err_code = error;
11561
11562         EM_DEBUG_FUNC_END("ret [%d]", ret);
11563         return ret;
11564 }
11565 /*======================= DB File Utils =============================================*/
11566 #include <dirent.h>
11567 #include <sys/types.h>
11568 #define  DIR_SEPERATOR "/"
11569
11570 INTERNAL_FUNC char *emstorage_make_directory_path_from_file_path(char *file_name)
11571 {
11572         EM_DEBUG_FUNC_BEGIN("Filename [ %p ]", file_name);
11573         char delims[] = "/";
11574         char *result = NULL;
11575         gchar **token = NULL;
11576
11577         token = g_strsplit_set(file_name, delims, 1);
11578
11579         if (token && token[0]) {
11580                 EM_DEBUG_LOG_SEC(">>>> Directory_name [ %s ]", token[0]);
11581                 result = EM_SAFE_STRDUP(token[0]);
11582         } else
11583                 EM_DEBUG_LOG(">>>> No Need to create Directory");
11584
11585         g_strfreev(token);
11586
11587         return result;
11588 }
11589
11590 INTERNAL_FUNC int emstorage_get_save_name(char *multi_user_name, int account_id, int mail_id, int atch_id,
11591                 char *fname, char *move_buf, char *path_buf, int maxlen, int *err_code)
11592 {
11593         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);
11594         EM_PROFILE_BEGIN(profile_emstorage_get_save_name);
11595
11596         int ret = false;
11597         int error = EMAIL_ERROR_NONE;
11598         char *dir_name = NULL;
11599         char create_dir[1024] = {0};
11600         char *temp_file = NULL;
11601         char *prefix_path = NULL;
11602         char *modified_fname = NULL;
11603
11604         if (!move_buf || !path_buf || account_id < FIRST_ACCOUNT_ID || mail_id < 0 || atch_id < 0) {
11605                 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);
11606                 error = EMAIL_ERROR_INVALID_PARAM;
11607                 goto FINISH_OFF;
11608         }
11609
11610         snprintf(path_buf, 512, "%s", MAIL_HOME);
11611         snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, account_id);
11612
11613         if (mail_id > 0)
11614                 snprintf(path_buf+EM_SAFE_STRLEN(path_buf),   512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, mail_id);
11615
11616         if (atch_id > 0)
11617                 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, atch_id);
11618
11619         if (fname) {
11620                 temp_file = EM_SAFE_STRDUP(fname);
11621                 if (temp_file && strstr(temp_file, "/"))
11622                         dir_name = emstorage_make_directory_path_from_file_path(temp_file);
11623         }
11624
11625         if (dir_name) {
11626                 snprintf(create_dir, sizeof(create_dir), "%s%s%s", path_buf, DIR_SEPERATOR, dir_name);
11627                 EM_DEBUG_LOG(">>>>> DIR PATH [%s]", create_dir);
11628                 ret = mkdir(create_dir, DIRECTORY_PERMISSION);
11629                 EM_DEBUG_LOG("mkdir result = [%d]", ret);
11630                 EM_SAFE_FREE(dir_name);
11631         }
11632
11633         if (fname) {
11634                 EM_DEBUG_LOG_DEV(">>>>> fname [%s]", fname);
11635
11636                 /* Did not allow the slash */
11637                 modified_fname = reg_replace_new(fname, "/", "_");
11638                 EM_DEBUG_LOG_SEC("modified_fname : [%s]", modified_fname);
11639
11640                 if (modified_fname == NULL) modified_fname = g_strdup(fname);
11641
11642                 if (EM_SAFE_STRLEN(modified_fname) + EM_SAFE_STRLEN(path_buf) + strlen(DIR_SEPERATOR) > maxlen - 1) {
11643                         char *modified_name = NULL;
11644                         int remain_len  = (maxlen - 1) - EM_SAFE_STRLEN(path_buf) - strlen(DIR_SEPERATOR);
11645
11646                         if (remain_len <= 0) {
11647                                 error = EMAIL_ERROR_MAX_EXCEEDED;
11648                                 goto FINISH_OFF;
11649                         }
11650
11651                         if (remain_len > MAX_FILENAME)
11652                                 remain_len = MAX_FILENAME;
11653
11654                         modified_name = em_shrink_filename(modified_fname, remain_len);
11655
11656                         if (!modified_name) {
11657                                 error = EMAIL_ERROR_MAX_EXCEEDED;
11658                                 goto FINISH_OFF;
11659                         }
11660
11661                         snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),
11662                                 "%s%s", DIR_SEPERATOR, modified_name);
11663                         EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11664                         EM_SAFE_FREE(modified_name);
11665                 } else {
11666                         if (EM_SAFE_STRLEN(modified_fname) > MAX_FILENAME - 1) {
11667                                 char *modified_name = NULL;
11668
11669                                 modified_name = em_shrink_filename(modified_fname, MAX_FILENAME);
11670                                 if (!modified_name) {
11671                                         error = EMAIL_ERROR_MAX_EXCEEDED;
11672                                         goto FINISH_OFF;
11673                                 }
11674
11675                                 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),
11676                                         "%s%s", DIR_SEPERATOR, modified_name);
11677                                 EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11678                                 EM_SAFE_FREE(modified_name);
11679                         } else {
11680                                 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),
11681                                         "%s%s", DIR_SEPERATOR, modified_fname);
11682                         }
11683                 }
11684         }
11685
11686         EM_DEBUG_LOG_SEC(">>>>> path_buf [%s]", path_buf);
11687
11688         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11689                 error = emcore_get_container_path(multi_user_name, &prefix_path);
11690                 if (error != EMAIL_ERROR_NONE) {
11691                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11692                         goto FINISH_OFF;
11693                 }
11694                 snprintf(move_buf, 512, "%s/%s", prefix_path, path_buf);
11695                 EM_DEBUG_LOG_SEC("move_buf : [%s]", move_buf);
11696         } else {
11697                 snprintf(move_buf, 512, "%s", path_buf);
11698                 EM_DEBUG_LOG_SEC("move_buf : [%s]", move_buf);
11699         }
11700
11701         ret = true;
11702
11703 FINISH_OFF:
11704
11705         EM_SAFE_FREE(temp_file);
11706         EM_SAFE_FREE(prefix_path);
11707         EM_SAFE_FREE(modified_fname);
11708
11709         if (err_code != NULL)
11710                 *err_code = error;
11711
11712         EM_PROFILE_END(profile_emstorage_get_save_name);
11713         EM_DEBUG_FUNC_END("ret [%d]", ret);
11714         return ret;
11715 }
11716
11717 /*
11718    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)
11719    {
11720    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);
11721
11722    if (!name_buf || account_id < FIRST_ACCOUNT_ID) {
11723    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);
11724    if (err_code != NULL)
11725  *err_code = EMAIL_ERROR_INVALID_PARAM;
11726  return false;
11727  }
11728
11729  sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
11730
11731  if (mail_id > 0)
11732  sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, mail_id);
11733  else
11734  goto FINISH_OFF;
11735
11736  if (atch_id > 0)
11737  sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, atch_id);
11738  else
11739  goto FINISH_OFF;
11740
11741 FINISH_OFF:
11742 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), ".DELE");
11743
11744 EM_DEBUG_FUNC_END();
11745 return true;
11746 }
11747 */
11748
11749 INTERNAL_FUNC int emstorage_create_dir(char *multi_user_name, int account_id, int mail_id, int atch_id, int *err_code)
11750 {
11751         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], err_code[%p]", account_id, mail_id, atch_id, err_code);
11752         EM_PROFILE_BEGIN(profile_emcore_save_create_dir);
11753         int ret = false;
11754         int error = EMAIL_ERROR_NONE;
11755         char buf[512];
11756         struct stat sbuf;
11757         char *prefix_path = NULL;
11758         char errno_buf[ERRNO_BUF_SIZE] = {0};
11759
11760         memset(buf, 0x00, sizeof(buf));
11761
11762         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11763                 error = emcore_get_container_path(multi_user_name, &prefix_path);
11764                 if (error != EMAIL_ERROR_NONE) {
11765                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11766                         goto FINISH_OFF;
11767                 }
11768         } else {
11769                 prefix_path = strdup("");
11770         }
11771
11772         if (account_id >= FIRST_ACCOUNT_ID) {
11773                 SNPRINTF(buf, sizeof(buf), "%s%s%s%s%d", prefix_path,
11774                                 DIR_SEPERATOR,
11775                                 MAIL_HOME,
11776                                 DIR_SEPERATOR,
11777                                 account_id);
11778
11779                 if (stat(buf, &sbuf) == 0) {
11780                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11781                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11782                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11783                                 goto FINISH_OFF;
11784                         }
11785                 } else {
11786                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11787                                 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11788                                 EM_DEBUG_EXCEPTION("mkdir failed: %s", EM_STRERROR(errno_buf));
11789                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11790                                 if (errno == 28)
11791                                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11792                                 goto FINISH_OFF;
11793                         }
11794
11795                         if (account_id == EML_FOLDER)
11796                                 chmod(buf, 0777);
11797                 }
11798         }
11799
11800         if (mail_id > 0) {
11801                 int space_left_in_buffer = sizeof(buf) - EM_SAFE_STRLEN(buf);
11802
11803                 if (account_id < FIRST_ACCOUNT_ID) {
11804                         EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11805                         error = EMAIL_ERROR_INVALID_PARAM;
11806                         goto FINISH_OFF;
11807                 }
11808
11809                 if (space_left_in_buffer + 10 > sizeof(buf)) {
11810                         EM_DEBUG_EXCEPTION("Buffer overflowed");
11811                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11812                         goto FINISH_OFF;
11813                 }
11814
11815                 SNPRINTF(buf+EM_SAFE_STRLEN(buf), space_left_in_buffer, "%s%d", DIR_SEPERATOR, mail_id);
11816
11817                 if (stat(buf, &sbuf) == 0) {
11818                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11819                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11820                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11821                                 goto FINISH_OFF;
11822                         }
11823                 } else {
11824                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11825                                 EM_DEBUG_EXCEPTION("mkdir failed [%s]", buf);
11826                                 EM_DEBUG_EXCEPTION("mkdir failed [%d][%s]", errno, EM_STRERROR(errno_buf));
11827                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11828                                 if (errno == 28)
11829                                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11830                                 goto FINISH_OFF;
11831                         }
11832
11833                         if (account_id == EML_FOLDER)
11834                                 chmod(buf, 0777);
11835                 }
11836         }
11837
11838         if (atch_id > 0) {
11839                 if (account_id < FIRST_ACCOUNT_ID || mail_id <= 0) {
11840                         EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11841                         error = EMAIL_ERROR_INVALID_PARAM;
11842                         goto FINISH_OFF;
11843                 }
11844
11845                 SNPRINTF(buf+EM_SAFE_STRLEN(buf), sizeof(buf)-(EM_SAFE_STRLEN(buf)+1), "%s%d", DIR_SEPERATOR, atch_id);
11846
11847                 if (stat(buf, &sbuf) == 0) {
11848                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11849                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11850                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11851                                 goto FINISH_OFF;
11852                         }
11853                 } else {
11854                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11855                                 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11856                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11857                                 if (errno == 28)
11858                                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11859                                 goto FINISH_OFF;
11860                         }
11861
11862                         if (account_id == EML_FOLDER)
11863                                 chmod(buf, 0777);
11864                 }
11865         }
11866
11867         ret = true;
11868
11869 FINISH_OFF:
11870
11871         EM_SAFE_FREE(prefix_path);
11872
11873         if (err_code != NULL)
11874                 *err_code = error;
11875
11876         EM_PROFILE_END(profile_emcore_save_create_dir);
11877         EM_DEBUG_FUNC_END("ret [%d]", ret);
11878         return ret;
11879 }
11880
11881 INTERNAL_FUNC int emstorage_copy_file(char *src_file, char *dst_file, int sync_status, int *err_code)
11882 {
11883         EM_DEBUG_FUNC_BEGIN_SEC("src_file[%s], dst_file[%s], err_code[%p]", src_file, dst_file, err_code);
11884         EM_DEBUG_LOG("Using the fsync function");
11885         int ret = false;
11886         int error = EMAIL_ERROR_NONE;
11887         struct stat st_buf;
11888
11889         int fp_src = 0;
11890         int fp_dst = 0;
11891         int nread = 0;
11892         char buf[FILE_MAX_BUFFER_SIZE] = {0};
11893         char errno_buf[ERRNO_BUF_SIZE] = {0};
11894
11895         if (!src_file || !dst_file) {
11896                 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
11897
11898                 error = EMAIL_ERROR_INVALID_PARAM;
11899                 goto FINISH_OFF;
11900         }
11901
11902         if (stat(src_file, &st_buf) < 0) {
11903                 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", src_file);
11904
11905                 error = EMAIL_ERROR_SYSTEM_FAILURE;             /* EMAIL_ERROR_INVALID_PATH; */
11906                 goto FINISH_OFF;
11907         }
11908
11909         error = em_open(src_file, O_RDONLY, 0, &fp_src);
11910         if (error != EMAIL_ERROR_NONE) {
11911                 EM_DEBUG_EXCEPTION(">>>> Source Fail em_open %s Failed: %d", src_file, error);
11912                 goto FINISH_OFF;
11913         }
11914
11915         error = em_open(dst_file, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, &fp_dst); /*prevent 24474*/
11916         if (error != EMAIL_ERROR_NONE) {
11917                 EM_DEBUG_EXCEPTION(">>>> Destination Fail em_open %s:  %d", dst_file, error);
11918                 goto FINISH_OFF;
11919         }
11920
11921         memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
11922
11923         while ((nread = read(fp_src, buf, FILE_MAX_BUFFER_SIZE)) > 0) {
11924                 if (nread > 0 && nread <= FILE_MAX_BUFFER_SIZE) {
11925                         EM_DEBUG_LOG("Nread Value [%d]", nread);
11926                         char *buf_ptr;
11927                         ssize_t byte_written = 0;
11928                         size_t remain_byte = nread;
11929                         buf_ptr = buf;
11930                         errno = 0;
11931
11932                         while (remain_byte > 0 && buf_ptr && errno == 0) {
11933                                 byte_written = write(fp_dst, buf_ptr, remain_byte);
11934
11935                                 if (byte_written < 0) {
11936                                         /* interrupted by a signal */
11937                                         if (errno == EINTR) {
11938                                                 errno = 0;
11939                                                 continue;
11940                                         }
11941
11942                                         EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
11943                                         error = EMAIL_ERROR_UNKNOWN;
11944                                         goto FINISH_OFF;
11945                                 }
11946                                 EM_DEBUG_LOG("NWRITTEN [%zu]", byte_written);
11947                                 remain_byte -= byte_written;
11948                                 buf_ptr += byte_written;
11949                         }
11950                 }
11951
11952                 memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
11953         }
11954
11955         ret = true;
11956
11957 FINISH_OFF:
11958         EM_SAFE_CLOSE(fp_src);
11959
11960         if (fp_dst >= 0) { /*prevent 24474*/
11961                 if (sync_status) {
11962                         EM_DEBUG_LOG("Before fsync");
11963                         fsync(fp_dst);
11964                 }
11965                 close(fp_dst);
11966         }
11967
11968         if (nread < 0 || error == EMAIL_ERROR_UNKNOWN)
11969                 remove(dst_file);
11970
11971         if (err_code != NULL)
11972                 *err_code = error;
11973         EM_DEBUG_FUNC_END("ret [%d]", ret);
11974         return ret;
11975 }
11976 /* create Directory if user has deleted [deepam.p@samsung.com] */
11977 INTERNAL_FUNC void emstorage_create_dir_if_delete()
11978 {
11979         EM_DEBUG_FUNC_BEGIN();
11980
11981         mkdir(DATA_PATH, DIRECTORY_PERMISSION);
11982         mkdir(EMAIL_PATH, DIRECTORY_PERMISSION);
11983         mkdir(MAIL_HOME, DIRECTORY_PERMISSION);
11984         mkdir(MAIL_TEMP, DIRECTORY_PERMISSION);
11985         chmod(MAIL_TEMP, 0777);
11986
11987         EM_DEBUG_FUNC_END();
11988 }
11989
11990 static int _get_temp_file_name(char **filename, int *err_code)
11991 {
11992         EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
11993
11994         int ret = false;
11995         int error = EMAIL_ERROR_NONE;
11996
11997         if (filename == NULL) {
11998                 EM_DEBUG_EXCEPTION(" filename[%p]", filename);
11999                 error = EMAIL_ERROR_INVALID_PARAM;
12000                 goto FINISH_OFF;
12001         }
12002
12003         char tempname[512] = {0x00, };
12004         struct timeval tv;
12005
12006         gettimeofday(&tv, NULL);
12007         srand(tv.tv_usec);
12008         unsigned int seed = time(NULL);
12009         SNPRINTF(tempname, sizeof(tempname), "%s%c%d", MAIL_TEMP, '/', rand_r(&seed));
12010
12011         char *p = EM_SAFE_STRDUP(tempname);
12012         if (p == NULL) {
12013                 EM_DEBUG_EXCEPTION(" strdup failed...");
12014                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12015                 goto FINISH_OFF;
12016         }
12017
12018         *filename = p;
12019
12020         ret = true;
12021
12022 FINISH_OFF:
12023         if (err_code != NULL)
12024                 *err_code = error;
12025
12026         EM_DEBUG_FUNC_END("ret [%d]", ret);
12027         return ret;
12028 }
12029
12030 INTERNAL_FUNC int emstorage_add_content_type(char *file_path, char *char_set, int *err_code)
12031 {
12032         EM_DEBUG_FUNC_BEGIN("File path [ %p ]  Character Set [ %p ] err_code [ %p]", file_path, char_set, err_code);
12033
12034         EM_IF_NULL_RETURN_VALUE(file_path, false);
12035         EM_IF_NULL_RETURN_VALUE(char_set, false);
12036         EM_IF_NULL_RETURN_VALUE(err_code, false);
12037
12038         char *buf =  NULL;
12039         char *buf1 = NULL;
12040         struct stat st_buf;
12041         int buf_size = 0;
12042         char *low_char_set = NULL;
12043         char *match_str = NULL;
12044         int nwritten = 0;
12045         int ret = false;
12046         int error = EMAIL_ERROR_NONE;
12047         int data_count_to_written = 0;
12048         char *temp_file_name = NULL;
12049         int err = 0;
12050         FILE* fp_src = NULL;
12051         FILE* fp_dest = NULL;
12052         int nread = 0;
12053
12054
12055         if (stat(file_path, &st_buf) < 0) {
12056                 EM_DEBUG_EXCEPTION_SEC(" stat(\"%s\") failed...", file_path);
12057                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12058                 goto FINISH_OFF;
12059         }
12060
12061         buf_size =  st_buf.st_size;
12062         EM_DEBUG_LOG(">>>> File Size [ %d ] ", buf_size);
12063         buf = (char *)calloc(1, buf_size+1);
12064
12065         if (!buf) {
12066                 EM_DEBUG_LOG(">>> Memory cannot be allocated ");
12067                 goto FINISH_OFF;
12068         }
12069
12070         error = em_fopen(file_path, "rb", &fp_src);
12071         if (error != EMAIL_ERROR_NONE || fp_src == NULL) {
12072                 EM_DEBUG_EXCEPTION_SEC(" file_path fopen failed - %s [%d]", file_path, error);
12073                 goto FINISH_OFF;
12074         }
12075
12076         if ((nread = fread(buf, 1, buf_size, fp_src)) > 0) {
12077                 if (nread > 0 && nread <= buf_size) {
12078                         EM_DEBUG_LOG(">>>> Nread Value [ %d ] ", nread);
12079
12080                         /**
12081                          *   1.Add check for whether content type is there.
12082                          *   2. If not based on the character set, Append it in File
12083                          **/
12084
12085                         low_char_set = calloc(1, EM_SAFE_STRLEN(char_set) + strlen(" \" /></head>") +1); /*prevent 34359*/
12086                         if (low_char_set == NULL) {
12087                                 EM_DEBUG_EXCEPTION("calloc failed");
12088                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12089                                 goto FINISH_OFF;
12090                         }
12091
12092                         strncat(low_char_set, char_set, sizeof(low_char_set)-EM_SAFE_STRLEN(low_char_set)-1);
12093                         EM_DEBUG_LOG(">>>> CHAR SET [ %s ] ", low_char_set);
12094                         strncat(low_char_set, " \" /></head>", sizeof(low_char_set)-EM_SAFE_STRLEN(low_char_set)-1); /*prevent 34359*/
12095                         EM_DEBUG_LOG(">>> CHARSET [ %s ] ", low_char_set);
12096                         buf[nread] = '\0';
12097
12098                         match_str = strstr(buf, CONTENT_TYPE_DATA);
12099
12100                         if (match_str == NULL) {
12101                                 EM_DEBUG_LOG(">>>>emstorage_add_content_type 3 ");
12102                                 if (fp_src != NULL) {
12103                                         fclose(fp_src);
12104                                         fp_src = NULL;
12105                                 }
12106                                 data_count_to_written = EM_SAFE_STRLEN(low_char_set)+strlen(CONTENT_DATA)+1; /*prevent 34359*/
12107                                 buf1 = (char *)calloc(1, data_count_to_written);
12108
12109                                 if (buf1) {
12110                                         strncat(buf1, CONTENT_DATA, sizeof(buf1)-EM_SAFE_STRLEN(buf1)-1); /*prevent 34359*/
12111                                         EM_DEBUG_LOG(">>>>> BUF 1 [ %s ] ", buf1);
12112                                         strncat(buf1, low_char_set, sizeof(buf1)-EM_SAFE_STRLEN(buf1)-1);
12113                                         EM_DEBUG_LOG(">>>> HTML TAG DATA  [ %s ] ", buf1);
12114
12115                                         /* 1. Create a temporary file name */
12116                                         if (!_get_temp_file_name(&temp_file_name, &err)) {
12117                                                 EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed - %d", err);
12118                                                 if (err_code != NULL) *err_code = err;
12119                                                 goto FINISH_OFF;
12120                                         }
12121                                         EM_DEBUG_LOG_SEC(">>>>>>> TEMP APPEND FILE PATH [ %s ] ", temp_file_name);
12122
12123                                         /* Open the Temp file in Append mode */
12124                                         error = em_fopen(temp_file_name, "ab", &fp_dest);
12125                                         if (error != EMAIL_ERROR_NONE) {
12126                                                 EM_DEBUG_EXCEPTION_SEC(" fopen failed - %s [%d]", temp_file_name, error);
12127                                                 goto FINISH_OFF;
12128                                         }
12129
12130                                         /* 2. write the Latest data */
12131                                         nwritten = fwrite(buf1, data_count_to_written-1, 1, fp_dest);
12132
12133                                         if (nwritten > 0) {
12134                                                 EM_DEBUG_LOG(" Latest Data  : [%d ] bytes written ", nwritten);
12135                                                 nwritten = 0;
12136                                                 /* 3. Append old data */
12137                                                 nwritten = fwrite(buf, nread-1, 1, fp_dest);
12138
12139                                                 if (nwritten <= 0) {
12140                                                         EM_DEBUG_EXCEPTION(" Error Occured while writing Old data : [%d ] bytes written ", nwritten);
12141                                                         error = EMAIL_ERROR_SYSTEM_FAILURE;
12142                                                         goto FINISH_OFF;
12143                                                 } else {
12144                                                         EM_DEBUG_LOG(">>>> OLD data appended [ %d ] ", nwritten);
12145
12146                                                         if (!emstorage_move_file(temp_file_name, file_path, false, &err)) {
12147                                                                 EM_DEBUG_EXCEPTION(" emstorage_move_file failed - %d", err);
12148                                                                 goto FINISH_OFF;
12149                                                         }
12150                                                 }
12151
12152                                         } else {
12153                                                 EM_DEBUG_EXCEPTION(" Error Occured while writing New data : [%d ] bytes written ", nwritten);
12154                                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12155                                                 goto FINISH_OFF;
12156                                         }
12157                                 }
12158                         }
12159                 }
12160         }
12161
12162         ret = true;
12163 FINISH_OFF:
12164
12165         EM_SAFE_FREE(buf);
12166         EM_SAFE_FREE(buf1);
12167         EM_SAFE_FREE(low_char_set);
12168         EM_SAFE_FREE(temp_file_name);
12169
12170         if (fp_src != NULL) {
12171                 fclose(fp_src);
12172                 fp_src = NULL;
12173         }
12174
12175         if (fp_dest != NULL) {
12176                 fclose(fp_dest);
12177                 fp_dest = NULL;
12178         }
12179
12180         if (err_code)
12181                 *err_code = error;
12182
12183         EM_DEBUG_FUNC_END("ret [%d]", ret);
12184         return ret;
12185
12186 }
12187
12188 INTERNAL_FUNC int emstorage_move_file(char *src_file, char *dst_file, int sync_status, int *err_code)
12189 {
12190         EM_DEBUG_FUNC_BEGIN("src_file[%p], dst_file[%p], err_code[%p]", src_file, dst_file, err_code);
12191
12192         int ret = false;
12193         int error = EMAIL_ERROR_NONE;
12194         char errno_buf[ERRNO_BUF_SIZE] = {0};
12195
12196         if (src_file == NULL || dst_file == NULL) {
12197                 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
12198                 error = EMAIL_ERROR_INVALID_PARAM;
12199                 goto FINISH_OFF;
12200         }
12201
12202         EM_DEBUG_LOG_SEC("src_file[%s], dst_file[%s]", src_file, dst_file);
12203
12204         if (strcmp(src_file, dst_file) != 0) {
12205                 EM_DEBUG_LOG("oldpath and newpath are not on the same mounted file system.");
12206                 if (!emstorage_copy_file(src_file, dst_file, sync_status, &error)) {
12207                         EM_DEBUG_EXCEPTION("emstorage_copy_file failed - %d", error);
12208
12209                         struct stat temp_file_stat;
12210                         if (stat(src_file, &temp_file_stat) < 0)
12211                                 EM_DEBUG_EXCEPTION("no src file found [%s] : %s", src_file, EM_STRERROR(errno_buf));
12212
12213                         if (stat(dst_file, &temp_file_stat) < 0)
12214                                 EM_DEBUG_EXCEPTION("no dst file found [%s] : %s", dst_file, EM_STRERROR(errno_buf));
12215
12216                         error = EMAIL_ERROR_FILE_NOT_FOUND;
12217                         goto FINISH_OFF;
12218
12219
12220                 }
12221                 remove(src_file);
12222                 EM_DEBUG_LOG("src[%s] removed", src_file);
12223         } else {
12224                 EM_DEBUG_LOG("src[%s] = dst[%s]", src_file, dst_file);
12225         }
12226
12227         ret = true;
12228
12229 FINISH_OFF:
12230         if (err_code != NULL)
12231                 *err_code = error;
12232
12233         EM_DEBUG_FUNC_END("ret [%d]", ret);
12234         return ret;
12235 }
12236
12237 INTERNAL_FUNC int emstorage_delete_file(char *src_file, int *err_code)
12238 {
12239         EM_DEBUG_FUNC_BEGIN("src_file[%p], err_code[%p]", src_file, err_code);
12240
12241         int ret = false;
12242         int error = EMAIL_ERROR_NONE;
12243
12244         if (src_file == NULL) {
12245                 EM_DEBUG_EXCEPTION(" src_file[%p]", src_file);
12246
12247                 error = EMAIL_ERROR_INVALID_PARAM;
12248                 goto FINISH_OFF;
12249         }
12250
12251         if (remove(src_file) != 0) {
12252                 if (errno != ENOENT) {
12253                         EM_DEBUG_EXCEPTION(" remove failed - %d", errno);
12254
12255                         error = EMAIL_ERROR_SYSTEM_FAILURE;
12256                         goto FINISH_OFF;
12257                 } else {
12258                         EM_DEBUG_EXCEPTION(" no file found...");
12259
12260                         error = EMAIL_ERROR_FILE_NOT_FOUND;
12261                 }
12262         }
12263
12264         ret = true;
12265
12266 FINISH_OFF:
12267         if (err_code != NULL)
12268                 *err_code = error;
12269
12270         EM_DEBUG_FUNC_END("ret [%d]", ret);
12271         return ret;
12272 }
12273
12274
12275 INTERNAL_FUNC int emstorage_delete_dir(char *src_dir, int *err_code)
12276 {
12277         EM_DEBUG_FUNC_BEGIN("src_dir[%p], err_code[%p]", src_dir, err_code);
12278
12279         if (src_dir == NULL) {
12280                 EM_DEBUG_EXCEPTION("src_dir[%p]", src_dir);
12281
12282                 if (err_code != NULL)
12283                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12284                 return false;
12285         }
12286
12287         int error = EMAIL_ERROR_NONE;
12288
12289         DIR *dirp;
12290         struct dirent *dp;
12291         struct stat sbuf;
12292         char buf[512];
12293
12294         dirp = opendir(src_dir);
12295
12296         if (dirp == NULL) {
12297                 if (errno == ENOENT) {
12298                         EM_DEBUG_EXCEPTION("directory[%s] does not exist...", src_dir);
12299                         if (err_code != NULL)
12300                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12301                         return true;
12302                 } else {
12303                         EM_DEBUG_EXCEPTION("opendir failed [%s] [%d]", src_dir, errno);
12304                         if (err_code != NULL)
12305                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12306                         return false;
12307                 }
12308         }
12309
12310         while ((dp = readdir(dirp))) {
12311                 if (strncmp(dp->d_name, ".", 1) == 0 || strncmp(dp->d_name, "..", 2) == 0) /* prevent 34360 */
12312                         continue;
12313
12314                 SNPRINTF(buf, sizeof(buf), "%s/%s", src_dir, dp->d_name);
12315
12316                 if (lstat(buf, &sbuf) == 0 || stat(buf, &sbuf) == 0) {
12317                         /*  check directory */
12318                         if ((sbuf.st_mode & S_IFMT) == S_IFDIR) {       /*  directory */
12319                                 /*  recursive call */
12320                                 if (!emstorage_delete_dir(buf, &error)) {
12321                                         closedir(dirp);
12322                                         if (err_code != NULL)
12323                                                 *err_code = error;
12324                                         return false;
12325                                 }
12326                         } else {        /*  file */
12327                                 if (remove(buf) < 0) {
12328                                         EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", buf, errno);
12329                                         closedir(dirp);
12330                                         if (err_code != NULL)
12331                                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12332                                         return false;
12333                                 }
12334                         }
12335                 } else
12336                         EM_DEBUG_EXCEPTION("content does not exist...");
12337         }
12338
12339         closedir(dirp);
12340
12341         EM_DEBUG_LOG_DEV("remove direcotory [%s]", src_dir);
12342
12343         /* EM_DEBUG_FUNC_BEGIN(); */
12344
12345         if (remove(src_dir) < 0) {
12346                 EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", src_dir, errno);
12347                 if (err_code != NULL)
12348                         *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12349                 return false;
12350         }
12351
12352         if (err_code != NULL)
12353                 *err_code = error;
12354
12355         return true;
12356 }
12357
12358 /* faizan.h@samsung.com */
12359 INTERNAL_FUNC int emstorage_update_server_uid(char *multi_user_name,
12360                 int mail_id,
12361                 char *old_server_uid,
12362                 char *new_server_uid,
12363                 int *err_code)
12364 {
12365         EM_DEBUG_FUNC_BEGIN("new_server_uid[%s], old_server_uid[%s]", new_server_uid, old_server_uid);
12366         int ret = false;
12367         int error = EMAIL_ERROR_NONE;
12368         int transaction = true;
12369         int temp_strlen = 0;
12370         int and_operation = 0;
12371         char sql_query_string[QUERY_SIZE] = {0, };
12372         char conditional_clause_string[QUERY_SIZE] = {0};
12373
12374         if ((mail_id <= 0 || !old_server_uid) && !new_server_uid) {
12375                 EM_DEBUG_EXCEPTION("Invalid parameters");
12376                 if (err_code)
12377                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12378                 return false;
12379         }
12380
12381         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12382         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12383
12384         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE ");
12385
12386         if (mail_id > 0) {
12387                 temp_strlen = strlen(conditional_clause_string);
12388                 SNPRINTF(conditional_clause_string + temp_strlen, sizeof(conditional_clause_string) - temp_strlen,
12389                                 "mail_id = %d ", mail_id);
12390                 and_operation = 1;
12391         }
12392
12393         if (old_server_uid) {
12394                 temp_strlen = strlen(conditional_clause_string);
12395                 if (!and_operation) {
12396                         sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
12397                                         "server_mail_id = '%q'", old_server_uid);
12398                 } else {
12399                         sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
12400                                         "and server_mail_id = '%q'", old_server_uid);
12401                 }
12402         }
12403
12404         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
12405                         "UPDATE mail_tbl SET server_mail_id = '%q' %s", new_server_uid, conditional_clause_string);
12406
12407         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12408         if (error != EMAIL_ERROR_NONE) {
12409                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12410                 goto FINISH_OFF;
12411         }
12412
12413         ret = true;
12414
12415 FINISH_OFF:
12416
12417         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12418
12419         if (err_code != NULL)
12420                 *err_code = error;
12421
12422         EM_DEBUG_FUNC_END("ret [%d]", ret);
12423         return ret;
12424
12425 }
12426
12427 INTERNAL_FUNC int emstorage_update_read_mail_uid(char *multi_user_name, int mail_id, char *new_server_uid, char *mbox_name, int *err_code)
12428 {
12429         EM_DEBUG_FUNC_BEGIN_SEC("mail_id[%d], new_server_uid[%s], mbox_name[%s]", mail_id, new_server_uid, mbox_name);
12430
12431         int ret = false;
12432         int error = EMAIL_ERROR_NONE;
12433         char sql_query_string[QUERY_SIZE] = {0, };
12434         int transaction = true;
12435
12436         if (!mail_id || !new_server_uid || !mbox_name) {
12437                 EM_DEBUG_EXCEPTION("Invalid parameters");
12438                 if (err_code != NULL)
12439                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12440                 return false;
12441         }
12442
12443         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12444         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12445
12446
12447         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12448                         "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);
12449
12450         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12451         if (error != EMAIL_ERROR_NONE) {
12452                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12453                 goto FINISH_OFF;
12454         }
12455
12456         ret     = true;
12457
12458 FINISH_OFF:
12459
12460         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12461
12462         if (err_code != NULL)
12463                 *err_code = error;
12464
12465         EM_DEBUG_FUNC_END("ret [%d]", ret);
12466         return ret;
12467
12468 }
12469
12470 INTERNAL_FUNC int emstorage_update_save_status(char *multi_user_name, int account_id, int *err_code)
12471 {
12472         EM_DEBUG_FUNC_BEGIN();
12473
12474         int ret = false;
12475         int transaction = true;
12476         int error = EMAIL_ERROR_NONE;
12477         char sql_query_string[QUERY_SIZE] = {0,};
12478
12479         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12480
12481         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12482
12483         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12484
12485         if (account_id <= ALL_ACCOUNT)
12486                 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);
12487         else
12488                 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);
12489
12490         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12491         if (error != EMAIL_ERROR_NONE) {
12492                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12493                 goto FINISH_OFF;
12494         }
12495
12496         ret = true;
12497
12498 FINISH_OFF:
12499         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12500
12501         if (err_code != NULL)
12502                 *err_code = error;
12503
12504         EM_DEBUG_FUNC_END("ret [%d]", ret);
12505         return ret;
12506
12507 }
12508
12509 int emstorage_get_unread_mailid(char *multi_user_name, int account_id, int vip_mode, int **mail_ids, int *mail_number, int *err_code)
12510 {
12511         EM_DEBUG_FUNC_BEGIN();
12512
12513         if ((!mail_ids) || (account_id <= 0 &&  account_id != -1)) {
12514                 EM_DEBUG_EXCEPTION(" mail_id[%p], account_id[%d] ", mail_ids, account_id);
12515                 if (err_code != NULL)
12516                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12517                 return false;
12518         }
12519
12520         int ret = false;
12521         int rc = -1;
12522         int error = EMAIL_ERROR_NONE;
12523         int count = 0;
12524         int i = 0;
12525         int col_index = 0;
12526         int *p_mail_ids = NULL;
12527         int transaction = false;
12528         char **result = NULL;
12529         char sql_query_string[QUERY_SIZE] = {0, };
12530         char temp_query_string[QUERY_SIZE] = {0,};
12531         char sql_select_query_string[QUERY_SIZE] = {0, };
12532
12533         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12534         EMSTORAGE_START_READ_TRANSACTION(transaction);
12535
12536         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12537
12538         if (account_id == -1) {
12539                 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string),
12540                         "SELECT mail_id FROM mail_tbl WHERE flags_seen_field = 0 AND (save_status = %d or save_status = %d)",
12541                         EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12542         } else {
12543                 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string),
12544                         "SELECT mail_id FROM mail_tbl WHERE account_id = %d AND flags_seen_field = 0 AND (save_status = %d or save_status = %d)",
12545                         account_id, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12546         }
12547
12548         if (vip_mode) {
12549                 SNPRINTF(temp_query_string, sizeof(temp_query_string),
12550                         "%s AND tag_id < 0", sql_select_query_string);
12551         } else {
12552                 SNPRINTF(temp_query_string, sizeof(temp_query_string), "%s", sql_select_query_string);
12553         }
12554
12555         SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s ORDER BY date_time ASC", temp_query_string);
12556
12557         EM_DEBUG_LOG_SEC("query: [%s]", sql_query_string);
12558
12559         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
12560         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
12561                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12562
12563         EM_DEBUG_LOG("Count : %d", count);
12564
12565         if (count == 0) {
12566                 EM_DEBUG_EXCEPTION("no Mails found...");
12567                 ret = false;
12568                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
12569                 goto FINISH_OFF;
12570         }
12571
12572         p_mail_ids = em_malloc(count * sizeof(int));
12573         if (p_mail_ids == NULL) {
12574                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
12575                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12576                 goto FINISH_OFF;
12577         }
12578
12579         col_index = 1;
12580
12581         for (i = 0; i < count; i++)
12582                 _get_table_field_data_int(result, &(p_mail_ids[i]), col_index++);
12583
12584         ret = true;
12585
12586 FINISH_OFF:
12587
12588         if (result)
12589                 sqlite3_free_table(result);
12590
12591         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12592
12593         //      sqlite3_db_release_memory(local_db_handle);
12594
12595         if (ret == true) {
12596                 if (mail_ids != NULL)
12597                         *mail_ids = p_mail_ids;
12598
12599                 if (mail_number != NULL)
12600                         *mail_number = count;
12601         } else {
12602                 EM_SAFE_FREE(p_mail_ids);
12603         }
12604
12605         if (err_code != NULL)
12606                 *err_code = error;
12607
12608         EM_DEBUG_FUNC_END("ret [%d]", ret);
12609         return ret;
12610 }
12611
12612 int setting_system_command(const char *command)
12613
12614 {
12615         int pid = 0, status = 0;
12616         char *const environ[] = { NULL };
12617
12618         if (command == 0)
12619                 return 1;
12620
12621         pid = fork();
12622
12623         if (pid == -1)
12624                 return -1;
12625
12626         if (pid == 0) {
12627                 char *argv[4];
12628
12629                 argv[0] = "sh";
12630                 argv[1] = "-c";
12631                 argv[2] = (char *)command;
12632                 argv[3] = 0;
12633
12634                 execve("/bin/sh", argv, environ);
12635                 abort();
12636         }
12637
12638         do {
12639                 if (waitpid(pid, &status, 0) == -1) {
12640                         if (errno != EINTR)
12641                                 return -1;
12642                 } else {
12643                         return status;
12644                 }
12645         } while (1);
12646
12647         return 0;
12648 }
12649
12650
12651 INTERNAL_FUNC int emstorage_mail_get_total_diskspace_usage(unsigned long *total_usage,  int transaction, int *err_code)
12652 {
12653         EM_DEBUG_FUNC_BEGIN("total_usage[%p],  transaction[%d], err_code[%p]", total_usage, transaction, err_code);
12654
12655         if (!total_usage) {
12656                 EM_DEBUG_EXCEPTION("total_usage[%p]", total_usage);
12657
12658                 if (err_code != NULL)
12659                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12660                 return false;
12661         }
12662
12663         int   ret = false;
12664         int   error = EMAIL_ERROR_NONE;
12665         char  syscmd[256] = {0, };
12666         char  line[256] = {0, };
12667         char *line_from_file = NULL;
12668         FILE *fp = NULL;
12669         unsigned long total_diskusage = 0;
12670
12671         SNPRINTF(syscmd, sizeof(syscmd), "touch %s", SETTING_MEMORY_TEMP_FILE_PATH);
12672         if (setting_system_command(syscmd) == -1) {
12673                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
12674
12675                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12676                 goto FINISH_OFF;
12677         }
12678
12679         SNPRINTF(syscmd, sizeof(syscmd), "du -hsk %s > %s", EMAIL_PATH, SETTING_MEMORY_TEMP_FILE_PATH);
12680         EM_DEBUG_LOG(" cmd : %s", syscmd);
12681         if (setting_system_command(syscmd) == -1) {
12682                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : Setting > Memory] System Command [%s] is failed", syscmd);
12683
12684                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12685                 goto FINISH_OFF;
12686         }
12687
12688         error = em_fopen(SETTING_MEMORY_TEMP_FILE_PATH, "r", &fp);
12689         if (error != EMAIL_ERROR_NONE) {
12690                 perror(SETTING_MEMORY_TEMP_FILE_PATH);
12691                 goto FINISH_OFF;
12692         }
12693
12694         line_from_file = fgets(line, sizeof(line), fp);
12695
12696         if (line_from_file == NULL) {
12697                 EM_DEBUG_EXCEPTION("fgets failed");
12698                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12699                 goto FINISH_OFF;
12700         }
12701         total_diskusage = strtoul(line, NULL, 10);
12702
12703         memset(syscmd, 0, sizeof(syscmd));
12704         SNPRINTF(syscmd, sizeof(syscmd), "rm -f %s", SETTING_MEMORY_TEMP_FILE_PATH);
12705         if (setting_system_command(syscmd) == -1) {
12706                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage :  [Setting > Memory] System Command [%s] is failed", syscmd);
12707                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12708                 goto FINISH_OFF;
12709         }
12710
12711         EM_DEBUG_LOG("[Setting > Memory] @@@@@ Size of Directory [%s] is %ld KB", EMAIL_PATH, total_diskusage);
12712
12713         ret = true;
12714
12715 FINISH_OFF:
12716         if (err_code != NULL)
12717                 *err_code = error;
12718
12719         if (ret)
12720                 *total_usage = total_diskusage;
12721         else
12722                 *total_usage = 0;
12723
12724         if (fp) fclose(fp); /* prevent 32730 */
12725
12726         EM_DEBUG_FUNC_END("ret [%d]", ret);
12727         return ret;
12728 }
12729 #define MAILHOME_UTF8    tzplatform_mkpath(TZ_USER_DATA, "email/.email_data/7/348/UTF-8")
12730
12731 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)
12732 {
12733         DB_STMT hStmt = NULL;
12734         int ret = false;
12735         int error = EMAIL_ERROR_NONE;
12736         int rc = 0;
12737         char sql_query_string[QUERY_SIZE] = {0, };
12738
12739         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12740                         "INSERT INTO mail_tbl VALUES "
12741                         "(?" /*  mail_id */
12742                         ", ?" /*  account_id */
12743                         ", ?" /*  mail_size */
12744                         ", ?" /*  server_mail_status */
12745                         ", ?" /*  server_mailbox_name */
12746                         ", ?" /*  server_mail_id */
12747                         ", ?" /*  reference_mail_id */
12748                         ", ?" /*  full_address_from */
12749                         ", ?" /*  full_address_reply */
12750                         ", ?" /*  full_address_to */
12751                         ", ?" /*  full_address_cc */
12752                         ", ?" /*  full_address_bcc */
12753                         ", ?" /*  full_address_return */
12754                         ", ?" /*  subject */
12755                         ", ?" /*  body_download_status */
12756                         ", ?" /*  file_path_plain */
12757                         ", ?" /*  file_path_html */
12758                         ", ?" /*  date_time */
12759                         ", ?" /*  flags_seen_field */
12760                         ", ?" /*  flags_deleted_field */
12761                         ", ?" /*  flags_flagged_field */
12762                         ", ?" /*  flags_answered_field */
12763                         ", ?" /*  flags_recent_field */
12764                         ", ?" /*  flags_draft_field */
12765                         ", ?" /*  flags_forwarded_field */
12766                         ", ?" /*  DRM_status */
12767                         ", ?" /*  priority */
12768                         ", ?" /*  save_status */
12769                         ", ?" /*  lock_status */
12770                         ", ?" /*  message_id */
12771                         ", ?" /*  report_status */
12772                         ", ?" /*  email_address_sender */
12773                         ", ?" /*  email_address_recipient */
12774                         ", ?" /*  attachment_count */
12775                         ", ?" /*  inline_content_count */
12776                         ", ?" /*  preview_text */
12777                         ", ?" /*  thread_id */
12778                         ", ?" /*  mailbox_type */
12779                         ", ?" /*  alias_sender */
12780                         ", ?" /*  alias_recipient */
12781                         ", ?" /*  thread_item_count */
12782                         ", ?" /*  meeting_request_status */
12783                         ", ?" /*  message_class */
12784                         ", ?" /*  digest_type */
12785                         ", ?" /*  smime_type */
12786                         ", ?" /*  scheduled_sending_time */
12787                         ", ?" /*  remaining_resend_times */
12788                         ", ?" /*  tag_id   */
12789                         ", ?" /*  replied_time */
12790                         ", ?" /*  forwarded_time */
12791                         ", ?" /*  default_charset */
12792                         ", ?" /*  eas_data_length */
12793                         ", ?" /*  eas_data */
12794                         ")");
12795
12796         int transaction = true;
12797         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12798
12799         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12800
12801
12802         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12803         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
12804                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12805
12806         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_id);
12807         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, account_id);
12808         _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, EMAIL_MAILBOX_TYPE_OUTBOX);
12809         _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, "save test - long", 1, SUBJECT_LEN_IN_MAIL_TBL);
12810         _bind_stmt_field_data_string(hStmt, DATETIME_IDX_IN_MAIL_TBL, "20100316052908", 0, DATETIME_LEN_IN_MAIL_TBL);
12811         _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, 0);
12812         _bind_stmt_field_data_string(hStmt, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL, "", 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
12813         _bind_stmt_field_data_string(hStmt, SERVER_MAIL_ID_IDX_IN_MAIL_TBL, "", 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
12814         _bind_stmt_field_data_string(hStmt, MESSAGE_ID_IDX_IN_MAIL_TBL, "", 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
12815         _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, 0);
12816         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL, "<test08@streaming.s3glab.net>", 1, FROM_LEN_IN_MAIL_TBL);
12817         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL, "", 1, REPLY_TO_LEN_IN_MAIL_TBL);
12818         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL, full_address_to, 1, TO_LEN_IN_MAIL_TBL);
12819         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL, full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
12820         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL, full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
12821         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL, "", 1, RETURN_PATH_LEN_IN_MAIL_TBL);
12822         _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);
12823         _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);
12824         _bind_stmt_field_data_string(hStmt, ALIAS_SENDER_IDX_IN_MAIL_TBL, "send_alias", 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12825         _bind_stmt_field_data_string(hStmt, ALIAS_RECIPIENT_IDX_IN_MAIL_TBL, "recipient_alias", 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12826         _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, 1);
12827         _bind_stmt_field_data_string(hStmt, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL, (char *)MAILHOME_UTF8, 0, TEXT_1_LEN_IN_MAIL_TBL);
12828         _bind_stmt_field_data_string(hStmt, FILE_PATH_HTML_IDX_IN_MAIL_TBL, "", 0, TEXT_2_LEN_IN_MAIL_TBL);
12829         _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, 4);
12830         _bind_stmt_field_data_char(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, 0);
12831         _bind_stmt_field_data_char(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, 0);
12832         _bind_stmt_field_data_char(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, 0);
12833         _bind_stmt_field_data_char(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, 0);
12834         _bind_stmt_field_data_char(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, 0);
12835         _bind_stmt_field_data_char(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, 0);
12836         _bind_stmt_field_data_char(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, 0);
12837         _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, 0);
12838         _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, 0);
12839         _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, 0);
12840         _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, 0);
12841         _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, 0);
12842         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12843         _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, 0);
12844         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12845         _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, 0);
12846         _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, 0);
12847         _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, "preview body", 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
12848         _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, 0);
12849         _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, 0);
12850         _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, 0);
12851         _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, 0);
12852         _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, 0);
12853         _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, 0);
12854         _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, 0);
12855         _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, 0);
12856         _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, 0);
12857         _bind_stmt_field_data_string(hStmt, DEFAULT_CHARSET_IDX_IN_MAIL_TBL, "UTF-8", 0, TEXT_2_LEN_IN_MAIL_TBL);
12858         _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, 0);
12859         _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, NULL, 0);
12860
12861         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12862         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
12863                         ("sqlite3_step fail:%d", rc));
12864         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
12865                         ("sqlite3_step fail:%d", rc));
12866         ret = true;
12867
12868 FINISH_OFF:
12869         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12870         if (hStmt != NULL) {
12871                 rc = sqlite3_finalize(hStmt);
12872                 if (rc != SQLITE_OK) {
12873                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
12874                         error = EMAIL_ERROR_DB_FAILURE;
12875                 }
12876         }
12877
12878         if (err_code != NULL)
12879                 *err_code = error;
12880
12881         EM_DEBUG_FUNC_END("ret [%d]", ret);
12882         return ret;
12883 }
12884
12885
12886 INTERNAL_FUNC int emstorage_get_max_mail_count()
12887 {
12888         return EMAIL_MAIL_MAX_COUNT;
12889 }
12890
12891 #define STRIPPED_SUBJECT_BUFFER_SIZE 4086
12892
12893 INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(char *multi_user_name,
12894                 emstorage_mail_tbl_t *mail_tbl,
12895                 int *thread_id,
12896                 int *result_latest_mail_id_in_thread,
12897                 int *thread_item_count)
12898 {
12899         EM_DEBUG_FUNC_BEGIN("mail_tbl [%p], thread_id [%p], "
12900                         "result_latest_mail_id_in_thread [%p], thread_item_count [%p]",
12901                         mail_tbl, thread_id, result_latest_mail_id_in_thread, thread_item_count);
12902         EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
12903
12904         int rc = 0, query_size = 0, query_size_account = 0;
12905         int i = 0;
12906         int search_thread = false;
12907         int account_id = 0;
12908         int err_code = EMAIL_ERROR_NONE;
12909         int count = 0, result_thread_id = -1, latest_mail_id_in_thread = -1;
12910         time_t latest_date_time = 0;
12911         char *subject = NULL;
12912         char *p_subject = NULL;
12913         char *sql_query_string = NULL, *sql_account = NULL;
12914         int col_index = 4;
12915         int temp_thread_id = -1;
12916         char *sql_format = "SELECT mail_id, thread_id, date_time, subject "
12917                 "FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
12918         char *sql_format_account = " AND account_id = %d ";
12919         char *sql_format_order_by = " ORDER BY thread_id, date_time DESC ";
12920         char **result = NULL;
12921         char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
12922         char stripped_subject2[STRIPPED_SUBJECT_BUFFER_SIZE];
12923
12924         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12925
12926         EM_DEBUG_LOG("subject: [%p], mail_id: [%d]", subject, mail_tbl->mail_id);
12927
12928         EM_IF_NULL_RETURN_VALUE(mail_tbl, EMAIL_ERROR_INVALID_PARAM);
12929         EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
12930         EM_IF_NULL_RETURN_VALUE(result_latest_mail_id_in_thread, EMAIL_ERROR_INVALID_PARAM);
12931         EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
12932
12933         account_id   = mail_tbl->account_id;
12934         subject      = mail_tbl->subject;
12935
12936         EM_DEBUG_LOG_SEC("subject: [%s]", subject);
12937
12938         if (EM_SAFE_STRLEN(subject) == 0 && mail_tbl->mail_id != 0) {
12939                 result_thread_id = mail_tbl->mail_id;
12940                 count = 1;
12941                 goto FINISH_OFF;
12942         }
12943
12944         if (em_find_pos_stripped_subject_for_thread_view(subject,
12945                                 stripped_subject,
12946                                 STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE)      {
12947                 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
12948                 err_code =  EMAIL_ERROR_UNKNOWN;
12949                 result_thread_id = -1;
12950                 goto FINISH_OFF;
12951         }
12952
12953         EM_DEBUG_LOG_SEC("stripped_subject: [%s]", stripped_subject);
12954
12955         if (EM_SAFE_STRLEN(stripped_subject) == 0) {
12956                 result_thread_id = -1;
12957                 goto FINISH_OFF;
12958         }
12959
12960         EM_DEBUG_LOG_SEC("em_find_pos_stripped_subject_for_thread_view returns[len = %zu] = %s",
12961                         EM_SAFE_STRLEN(stripped_subject), stripped_subject);
12962
12963         if (account_id > 0) {
12964                 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
12965                 sql_account = malloc(query_size_account);
12966                 if (sql_account == NULL) {
12967                         EM_DEBUG_EXCEPTION("malloc for sql_account  is failed %d", query_size_account);
12968                         err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
12969                         goto FINISH_OFF;
12970                 }
12971                 snprintf(sql_account, query_size_account, sql_format_account, account_id);
12972         }
12973
12974         /* prevent 34362 */
12975         query_size = strlen(sql_format) + strlen(stripped_subject)*2 + 50 + query_size_account + strlen(sql_format_order_by); /*  + query_size_mailbox; */
12976         sql_query_string = malloc(query_size);
12977
12978         if (sql_query_string == NULL) {
12979                 EM_DEBUG_EXCEPTION("malloc for sql  is failed %d", query_size);
12980                 err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
12981                 goto FINISH_OFF;
12982         }
12983
12984         sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mail_tbl->mailbox_id);
12985
12986         if (account_id > 0)
12987                 EM_SAFE_STRNCAT(sql_query_string, sql_account, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
12988
12989         EM_SAFE_STRNCAT(sql_query_string, sql_format_order_by, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
12990         EM_SAFE_STRNCAT(sql_query_string, ";", query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
12991
12992         EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
12993
12994         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL),
12995                         rc);
12996
12997         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
12998                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12999
13000         EM_DEBUG_LOG("Result rows count : %d", count);
13001
13002         if (count == 0)
13003                 result_thread_id = -1;
13004         else {
13005                 for (i = 0; i < count; i++) {
13006                         EM_SAFE_FREE(p_subject);
13007
13008                         _get_table_field_data_int(result, &latest_mail_id_in_thread, col_index++);
13009                         _get_table_field_data_int(result, &result_thread_id, col_index++);
13010                         _get_table_field_data_time_t(result, &latest_date_time, col_index++);
13011                         _get_table_field_data_string(result, &p_subject, 0, col_index++);
13012
13013                         if (temp_thread_id == result_thread_id)
13014                                 continue;
13015
13016                         temp_thread_id = result_thread_id;
13017
13018                         if (em_find_pos_stripped_subject_for_thread_view(p_subject,
13019                                                 stripped_subject2,
13020                                                 STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE)      {
13021                                 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
13022                                 err_code = EMAIL_ERROR_UNKNOWN;
13023                                 result_thread_id = -1;
13024                                 goto FINISH_OFF;
13025                         }
13026
13027                         if (g_strcmp0(stripped_subject2, stripped_subject) == 0) {
13028                                 if (latest_date_time < mail_tbl->date_time)
13029                                         *result_latest_mail_id_in_thread = latest_mail_id_in_thread;
13030                                 else
13031                                         *result_latest_mail_id_in_thread = mail_tbl->mail_id;
13032
13033                                 search_thread = true;
13034                         }
13035
13036                         if (search_thread) {
13037                                 EM_DEBUG_LOG("latest_mail_id_in_thread [%d], mail_id [%d]",
13038                                                 latest_mail_id_in_thread, mail_tbl->mail_id);
13039                                 break;
13040                         } else {
13041                                 result_thread_id = -1;
13042                                 count = 0;
13043                         }
13044                 }
13045
13046         }
13047
13048 FINISH_OFF:
13049
13050         *thread_id = result_thread_id;
13051         *thread_item_count = count;
13052
13053         EM_DEBUG_LOG("Result thread id : %d", *thread_id);
13054         EM_DEBUG_LOG("Result count : %d", *thread_item_count);
13055         EM_DEBUG_LOG("err_code : %d", err_code);
13056
13057         EM_SAFE_FREE(sql_account);
13058         EM_SAFE_FREE(sql_query_string);
13059         EM_SAFE_FREE(p_subject);
13060
13061         sqlite3_free_table(result);
13062
13063         EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
13064
13065         return err_code;
13066 }
13067
13068 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)
13069 {
13070         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);
13071         EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
13072
13073         int rc = 0;
13074         int query_size = 0;
13075         int query_size_account = 0;
13076         int err_code = EMAIL_ERROR_NONE;
13077         int count = 0;
13078         int result_thread_id = -1;
13079         char *sql_query_string = NULL;
13080         char *sql_account = NULL;
13081         char *sql_format = "SELECT thread_id FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
13082         char *sql_format_account = " AND account_id = %d ";
13083         char *sql_format_order_by = " ORDER BY date_time DESC ";
13084         char **result = NULL;
13085         char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
13086         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13087
13088         EM_IF_NULL_RETURN_VALUE(mail_subject, EMAIL_ERROR_INVALID_PARAM);
13089         EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
13090         EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
13091
13092         EM_DEBUG_LOG_SEC("subject: [%s]", mail_subject);
13093
13094         if (em_find_pos_stripped_subject_for_thread_view(mail_subject, stripped_subject, STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE)     {
13095                 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view  is failed");
13096                 err_code =  EMAIL_ERROR_UNKNOWN;
13097                 result_thread_id = -1;
13098                 goto FINISH_OFF;
13099         }
13100
13101         if (EM_SAFE_STRLEN(stripped_subject) == 0) {
13102                 result_thread_id = -1;
13103                 goto FINISH_OFF;
13104         }
13105
13106         EM_DEBUG_LOG("em_find_pos_stripped_subject_for_thread_view returns[len = %zu] = %s", EM_SAFE_STRLEN(stripped_subject), stripped_subject);
13107
13108         if (account_id > 0) {
13109                 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
13110                 sql_account = malloc(query_size_account);
13111                 if (sql_account == NULL) {
13112                         EM_DEBUG_EXCEPTION("malloc for sql_account  is failed %d", query_size_account);
13113                         err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
13114                         goto FINISH_OFF;
13115                 }
13116                 snprintf(sql_account, query_size_account, sql_format_account, account_id);
13117         }
13118
13119         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; */
13120         sql_query_string = malloc(query_size);
13121
13122         if (sql_query_string == NULL) {
13123                 EM_DEBUG_EXCEPTION("malloc for sql  is failed %d", query_size);
13124                 err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
13125                 goto FINISH_OFF;
13126         }
13127
13128         sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mailbox_id);
13129
13130         if (account_id > 0)
13131                 EM_SAFE_STRNCAT(sql_query_string, sql_account, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13132         EM_SAFE_STRNCAT(sql_query_string, sql_format_order_by, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13133         EM_SAFE_STRNCAT(sql_query_string, ";", query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13134
13135         EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
13136
13137         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
13138
13139         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13140                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13141
13142         EM_DEBUG_LOG("Result rows count : %d", count);
13143
13144         if (count == 0)
13145                 result_thread_id = -1;
13146         else
13147                 _get_table_field_data_int(result, &result_thread_id, 1);
13148
13149 FINISH_OFF:
13150         *thread_id = result_thread_id;
13151         *thread_item_count = count;
13152
13153         EM_DEBUG_LOG("Result thread id : %d", *thread_id);
13154         EM_DEBUG_LOG("Result count : %d", *thread_item_count);
13155         EM_DEBUG_LOG("err_code : %d", err_code);
13156
13157         EM_SAFE_FREE(sql_account);
13158         EM_SAFE_FREE(sql_query_string);
13159
13160         sqlite3_free_table(result);
13161
13162         EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
13163
13164         return err_code;
13165 }
13166
13167 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)
13168 {
13169         EM_DEBUG_FUNC_BEGIN();
13170
13171         int count = 0, ret = false;
13172         int error = EMAIL_ERROR_NONE;
13173         emstorage_mail_tbl_t *p_data_tbl = NULL;
13174         char conditional_clause[QUERY_SIZE] = {0, };
13175
13176         EM_IF_NULL_RETURN_VALUE(mail_tbl, false);
13177
13178         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE thread_id = %d AND thread_item_count > 0", thread_id);
13179         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
13180
13181         if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
13182                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
13183                 goto FINISH_OFF;
13184         }
13185
13186         if (p_data_tbl)
13187                 EM_DEBUG_LOG("thread_id : %d, thread_item_count : %d", p_data_tbl[0].thread_id, p_data_tbl[0].thread_item_count);
13188
13189         ret = true;
13190
13191 FINISH_OFF:
13192         if (ret == true)
13193                 *mail_tbl = p_data_tbl;
13194         else if (p_data_tbl != NULL)
13195                 emstorage_free_mail(&p_data_tbl, 1, NULL);
13196
13197         if (err_code != NULL)
13198                 *err_code = error;
13199
13200         EM_DEBUG_FUNC_END("ret [%d]", ret);
13201         return ret;
13202 }
13203
13204 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)
13205 {
13206         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]"
13207                         , account_id , mailbox_id , search_type , search_value , sorting , sender_list, sender_count, err_code);
13208
13209         if ((!sender_list) || (!sender_count)) {
13210                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
13211                 if (err_code != NULL)
13212                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13213                 return false;
13214         }
13215
13216         int rc = -1, ret = false;
13217         int error = EMAIL_ERROR_NONE;
13218         int count = 0;
13219         int i, col_index = 0;
13220         int read_count = 0;
13221         email_sender_list_t *p_sender_list = NULL;
13222         char sql_query_string[QUERY_SIZE] = {0, };
13223         char **result = NULL;
13224         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13225
13226         SNPRINTF(sql_query_string, sizeof(sql_query_string),
13227                         "SELECT email_address_sender, alias_sender, COUNT(email_address_sender), SUM(flags_seen_field = 1) "
13228                         "FROM mail_tbl ");
13229
13230         /*  mailbox_id */
13231         if (mailbox_id)
13232                 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);
13233         else    /*  NULL  means all mailbox_name. but except for trash(3), spambox(5), all emails(for GMail, 7) */
13234                 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) ");
13235
13236         /*  account id */
13237         /*  '0' (ALL_ACCOUNT) means all account */
13238         if (account_id > ALL_ACCOUNT)
13239                 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);
13240
13241         if (search_value) {
13242                 switch (search_type) {
13243                 case EMAIL_SEARCH_FILTER_SUBJECT:
13244                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13245                                         " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\')) ", search_value);
13246                         break;
13247                 case EMAIL_SEARCH_FILTER_SENDER:
13248                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13249                                         " AND  ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13250                                         ") ", search_value);
13251                         break;
13252                 case EMAIL_SEARCH_FILTER_RECIPIENT:
13253                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13254                                         " AND ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13255                                         "       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13256                                         "       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13257                                         ") ", search_value, search_value, search_value);
13258                         break;
13259                 case EMAIL_SEARCH_FILTER_ALL:
13260                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13261                                         " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') "
13262                                         "       OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13263                                         "                       OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13264                                         "                       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13265                                         "                       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13266                                         "               ) "
13267                                         "       )"
13268                                         ")", search_value, search_value, search_value, search_value, search_value);
13269                         break;
13270                 }
13271         }
13272
13273
13274         /*  sorting option is not available now. The order of sender list is ascending order by display name */
13275         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
13276                         "GROUP BY email_address_sender "
13277                         "ORDER BY UPPER(alias_sender) ");
13278
13279         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
13280
13281         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
13282         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13283                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13284
13285         EM_DEBUG_LOG("Count of Sender [%d]", count);
13286
13287         if (!(p_sender_list = (email_sender_list_t*)em_malloc(sizeof(email_sender_list_t) * count))) {
13288                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
13289                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13290                 goto FINISH_OFF;
13291         }
13292
13293         col_index = 4;
13294
13295         EM_DEBUG_LOG(">>>> DATA ASSIGN START >>");
13296         for (i = 0; i < count; i++) {
13297                 _get_table_field_data_string(result, &(p_sender_list[i].address), 1, col_index++);
13298                 _get_table_field_data_string(result, &(p_sender_list[i].display_name), 1, col_index++);
13299                 _get_table_field_data_int(result, &(p_sender_list[i].total_count), col_index++);
13300                 _get_table_field_data_int(result, &(read_count), col_index++);
13301                 p_sender_list[i].unread_count = p_sender_list[i].total_count - read_count;              /*  unread count = total - read          */
13302         }
13303         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >>", count);
13304
13305         sqlite3_free_table(result);
13306         result = NULL;
13307
13308         ret = true;
13309
13310 FINISH_OFF:
13311         if (ret == true) {
13312                 *sender_list = p_sender_list;
13313                 *sender_count = count;
13314                 EM_DEBUG_LOG(">>>> COUNT : %d >>", count);
13315         }
13316
13317
13318         if (err_code != NULL)
13319                 *err_code = error;
13320
13321         EM_DEBUG_FUNC_END("ret [%d]", ret);
13322         return ret;
13323 }
13324
13325 INTERNAL_FUNC int emstorage_free_sender_list(email_sender_list_t **sender_list, int count)
13326 {
13327         EM_DEBUG_FUNC_BEGIN("sender_list[%p], count[%d]", sender_list, count);
13328
13329         int err = EMAIL_ERROR_NONE;
13330
13331         if (count > 0) {
13332                 if (!sender_list || !*sender_list) {
13333                         EM_DEBUG_EXCEPTION("sender_list[%p], count[%d]", sender_list, count);
13334                         err = EMAIL_ERROR_INVALID_PARAM;
13335                         return err;
13336                 }
13337
13338                 email_sender_list_t* p = *sender_list;
13339                 int i = 0;
13340
13341                 for (; i < count; i++) {
13342                         EM_SAFE_FREE(p[i].address);
13343                         EM_SAFE_FREE(p[i].display_name);
13344                 }
13345
13346                 EM_SAFE_FREE(p);
13347                 *sender_list = NULL;
13348         }
13349
13350         return err;
13351 }
13352
13353
13354 INTERNAL_FUNC int emstorage_free_address_info_list(email_address_info_list_t **address_info_list)
13355 {
13356         EM_DEBUG_FUNC_BEGIN("address_info_list[%p]", address_info_list);
13357
13358         int err = EMAIL_ERROR_NONE;
13359         email_address_info_t *p_address_info = NULL;
13360         GList *list = NULL;
13361         GList *node = NULL;
13362         int i = 0;
13363
13364         if (!address_info_list || !*address_info_list) {
13365                 EM_DEBUG_EXCEPTION("address_info_list[%p]", address_info_list);
13366                 err = EMAIL_ERROR_INVALID_PARAM;
13367                 return err;
13368         }
13369
13370         /*  delete GLists */
13371         for (i = EMAIL_ADDRESS_TYPE_FROM; i <= EMAIL_ADDRESS_TYPE_BCC; i++) {
13372                 switch (i) {
13373                 case EMAIL_ADDRESS_TYPE_FROM:
13374                         list = (*address_info_list)->from;
13375                         break;
13376                 case EMAIL_ADDRESS_TYPE_TO:
13377                         list = (*address_info_list)->to;
13378                         break;
13379                 case EMAIL_ADDRESS_TYPE_CC:
13380                         list = (*address_info_list)->cc;
13381                         break;
13382                 case EMAIL_ADDRESS_TYPE_BCC:
13383                         list = (*address_info_list)->bcc;
13384                         break;
13385                 }
13386
13387                 /*  delete dynamic-allocated memory for each item */
13388                 node = g_list_first(list);
13389                 while (node != NULL) {
13390                         p_address_info = (email_address_info_t*)node->data;
13391                         EM_SAFE_FREE(p_address_info->address);
13392                         EM_SAFE_FREE(p_address_info->display_name);
13393                         EM_SAFE_FREE(node->data);
13394
13395                         node = g_list_next(node);
13396                 }
13397                 g_list_free(list);
13398         }
13399
13400         EM_SAFE_FREE(*address_info_list);
13401         *address_info_list = NULL;
13402
13403         EM_DEBUG_FUNC_END("err [%d]", err);
13404         return err;
13405 }
13406 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
13407
13408 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)
13409 {
13410         EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
13411
13412         if (!local_activity || !activity_id) {
13413                 EM_DEBUG_EXCEPTION("local_activity[%p], transaction[%d], activity_id[%p], err_code[%p]", local_activity, transaction, activity_id, err_code);
13414                 if (err_code != NULL)
13415                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13416                 return false;
13417         }
13418
13419         int rc = -1;
13420         int ret = false;
13421         int error = EMAIL_ERROR_NONE;
13422         int i = 0;
13423
13424         char sql_query_string[QUERY_SIZE] = {0, };
13425         DB_STMT hStmt = NULL;
13426         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13427
13428         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13429         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13430         SNPRINTF(sql_query_string, sizeof(sql_query_string),
13431                         "INSERT INTO mail_partial_body_activity_tbl VALUES "
13432                         "("
13433                         "? "  /* Account ID */
13434                         ",?"  /* Local Mail ID */
13435                         ",?"  /* Server mail ID */
13436                         ",?"  /* Activity ID */
13437                         ",?"  /* Activity type*/
13438                         ",?"  /* Mailbox ID*/
13439                         ",?"  /* Mailbox name*/
13440                         ",?"  /* Multi User Name */
13441                         ") ");
13442
13443         char *sql = "SELECT max(rowid) FROM mail_partial_body_activity_tbl;";
13444         char **result = NULL;
13445
13446
13447         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13448
13449         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13450                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13451
13452         if (NULL == result[1]) rc = 1;
13453         else rc = atoi(result[1])+1;
13454         sqlite3_free_table(result);
13455         result = NULL;
13456
13457         *activity_id = local_activity->activity_id = rc;
13458
13459         EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [ %d ], MAIL ID [ %d ], ACTIVITY TYPE [ %d ], SERVER MAIL ID [ %lu ]", \
13460                         local_activity->activity_id, local_activity->mail_id, local_activity->activity_type, local_activity->server_mail_id);
13461
13462         if (local_activity->mailbox_id)
13463                 EM_DEBUG_LOG(" MAILBOX ID [ %d ]", local_activity->mailbox_id);
13464
13465
13466         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13467         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13468                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13469
13470
13471         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
13472         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
13473         _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
13474         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
13475         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
13476         _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
13477         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->mailbox_name, 0, 3999);
13478         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
13479
13480
13481         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13482
13483         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
13484                         ("sqlite3_step fail:%d", rc));
13485         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13486                         ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
13487
13488         ret = true;
13489
13490 FINISH_OFF:
13491         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13492         if (hStmt != NULL) {
13493                 rc = sqlite3_finalize(hStmt);
13494                 hStmt = NULL;
13495                 if (rc != SQLITE_OK) {
13496                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13497                         error = EMAIL_ERROR_DB_FAILURE;
13498                 }
13499         }
13500
13501         if (err_code != NULL)
13502                 *err_code = error;
13503
13504         EM_DEBUG_FUNC_END("ret [%d]", ret);
13505         return ret;
13506 }
13507
13508 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)
13509 {
13510         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13511
13512         if (account_id < FIRST_ACCOUNT_ID || NULL == mailbox_list || *mailbox_list == NULL || NULL == count) {
13513                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13514                 if (err_code != NULL)
13515                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13516                 return false;
13517         }
13518
13519         int ret = false;
13520         int error = EMAIL_ERROR_NONE;
13521         char **result;
13522         int i = 0, rc = -1;
13523         int *mbox_list = NULL;
13524         DB_STMT hStmt = NULL;
13525         char sql_query_string[QUERY_SIZE] = {0, };
13526
13527         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13528         EMSTORAGE_START_READ_TRANSACTION(transaction);
13529
13530         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13531         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);
13532
13533
13534         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13535         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13536                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13537
13538         *count = atoi(result[1]);
13539         sqlite3_free_table(result);
13540
13541         if (*count <= 0) {
13542                 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
13543                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13544                 ret = true;
13545                 goto FINISH_OFF;
13546         }
13547         EM_DEBUG_LOG("Mailbox count = %d", *count);
13548
13549         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13550
13551         /* 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); */
13552         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);
13553
13554         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13555
13556
13557         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13558
13559
13560         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13561                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13562
13563
13564         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13565         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13566                         ("sqlite3_step fail:%d", rc));
13567
13568         mbox_list = (int *)em_malloc(sizeof(int) * (*count));
13569         if (NULL == mbox_list) {
13570                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
13571                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13572                 goto FINISH_OFF;
13573         }
13574
13575         memset(mbox_list, 0x00, sizeof(int) * (*count));
13576
13577         for (i = 0; i < (*count); i++) {
13578                 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
13579                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13580                 /* EM_DEBUG_LOG("In emstorage_get_pdb_mailbox_list() loop, After sqlite3_step(), , i = %d, rc = %d.", i,  rc); */
13581                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13582                                 ("sqlite3_step fail:%d", rc));
13583                 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
13584         }
13585
13586         ret = true;
13587
13588 FINISH_OFF:
13589         if (ret == true)
13590                 *mailbox_list = mbox_list;
13591         else
13592                 EM_SAFE_FREE(mbox_list);
13593
13594         if (hStmt != NULL) {
13595                 rc = sqlite3_finalize(hStmt);
13596                 hStmt = NULL;
13597                 if (rc != SQLITE_OK) {
13598                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13599                         error = EMAIL_ERROR_DB_FAILURE;
13600                 }
13601         }
13602
13603         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13604         if (err_code != NULL)
13605                 *err_code = error;
13606         EM_DEBUG_FUNC_END("ret [%d]", ret);
13607         return ret;
13608 }
13609
13610 INTERNAL_FUNC int emstorage_get_pbd_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
13611 {
13612         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13613
13614         if (NULL == account_list || NULL == count) {
13615                 EM_DEBUG_EXCEPTION("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13616                 if (err_code != NULL)
13617                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13618                 return false;
13619         }
13620
13621         int ret = false;
13622         int error = EMAIL_ERROR_NONE;
13623         char *sql = "SELECT count(distinct account_id) FROM mail_partial_body_activity_tbl";
13624         char **result;
13625         int i = 0, rc = -1;
13626         int *result_account_list = NULL;
13627         DB_STMT hStmt = NULL;
13628         char sql_query_string[QUERY_SIZE] = {0, };
13629
13630         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13631
13632         EMSTORAGE_START_READ_TRANSACTION(transaction);
13633
13634         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13635         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13636                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13637
13638         *count = atoi(result[1]);
13639         sqlite3_free_table(result);
13640
13641         if (*count <= 0) {
13642                 EM_DEBUG_EXCEPTION("no account found...");
13643                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13644                 ret = true;
13645                 goto FINISH_OFF;
13646         }
13647
13648         EM_DEBUG_LOG("Account count [%d]", *count);
13649
13650         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13651
13652         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_partial_body_activity_tbl");
13653
13654         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13655
13656
13657         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13658
13659         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
13660         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13661                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13662
13663
13664         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13665         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13666                         ("sqlite3_step fail:%d", rc));
13667
13668         if (NULL == (result_account_list = (int *)em_malloc(sizeof(int) * (*count)))) {
13669                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
13670                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13671                 goto FINISH_OFF;
13672         }
13673
13674         memset(result_account_list, 0x00, sizeof(int) * (*count));
13675
13676         for (i = 0; i < (*count); i++) {
13677                 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
13678
13679                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13680                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13681                                 ("sqlite3_step fail:%d", rc));
13682                 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
13683         }
13684
13685         ret = true;
13686
13687 FINISH_OFF:
13688         if (ret == true)
13689                 *account_list = result_account_list;
13690         else
13691                 EM_SAFE_FREE(result_account_list);
13692
13693         if (hStmt != NULL) {
13694                 rc = sqlite3_finalize(hStmt);
13695                 hStmt = NULL;
13696                 if (rc != SQLITE_OK) {
13697                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13698                         error = EMAIL_ERROR_DB_FAILURE;
13699                 }
13700         }
13701
13702         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13703         if (err_code != NULL)
13704                 *err_code = error;
13705         EM_DEBUG_FUNC_END("ret [%d]", ret);
13706         return ret;
13707 }
13708
13709 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)
13710 {
13711         EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
13712
13713         if (account_id < FIRST_ACCOUNT_ID || NULL == event_start || 0 == input_mailbox_id || NULL == count) {
13714                 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);
13715
13716                 if (err_code != NULL)
13717                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13718                 return false;
13719         }
13720
13721         int rc = -1;
13722         int ret = false;
13723         char **result;
13724         int error = EMAIL_ERROR_NONE;
13725         int i = 0;
13726         DB_STMT hStmt = NULL;
13727         email_event_partial_body_thd* event_list = NULL;
13728         char sql_query_string[QUERY_SIZE] = {0, };
13729
13730         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13731
13732         EMSTORAGE_START_READ_TRANSACTION(transaction);
13733
13734         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13735         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);
13736
13737
13738         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13739         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13740                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13741
13742         *count = atoi(result[1]);
13743         sqlite3_free_table(result);
13744
13745         EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
13746
13747         if (*count <= 0) {
13748                 EM_DEBUG_LOG("No matched activity found in mail_partial_body_activity_tbl");
13749                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
13750                 ret = true;
13751                 goto FINISH_OFF;
13752         }
13753         EM_DEBUG_LOG("Activity Count = %d", *count);
13754
13755         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13756         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);
13757
13758         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13759
13760
13761         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13762
13763         EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
13764         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13765                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13766
13767
13768         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13769         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13770                         ("sqlite3_step fail:%d", rc));
13771
13772         if (!(event_list = (email_event_partial_body_thd*)em_malloc(sizeof(email_event_partial_body_thd) * (*count)))) {
13773                 EM_DEBUG_EXCEPTION("Malloc failed");
13774
13775                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13776                 goto FINISH_OFF;
13777         }
13778         memset(event_list, 0x00, sizeof(email_event_partial_body_thd) * (*count));
13779
13780         for (i = 0; i < (*count); i++) {
13781                 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_IDX_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13782                 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13783                 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13784                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13785                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_type), ACTIVITY_TYPE_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13786                 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13787                 _get_stmt_field_data_string(hStmt, &(event_list[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13788                 _get_stmt_field_data_string(hStmt, &(event_list[i].multi_user_name), 0, MULTI_USER_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13789
13790                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13791                 /* EM_DEBUG_LOG("In emstorage_get_pbd_activity_data() loop, After sqlite3_step(), , i = %d, rc = %d.", i,  rc); */
13792                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13793                                 ("sqlite3_step fail:%d", rc));
13794
13795                 event_list[i].event_type = 0;
13796         }
13797
13798         ret = true;
13799
13800 FINISH_OFF:
13801         if (true == ret)
13802                 *event_start = event_list;
13803         else {
13804                 for (i = 0; i < (*count); i++)
13805                         emcore_free_partial_body_thd_event(event_list, NULL);
13806                 EM_SAFE_FREE(event_list); /*prevent 54559*/
13807                 *event_start = NULL;
13808                 *count = 0;
13809         }
13810
13811         if (hStmt != NULL) {
13812                 rc = sqlite3_finalize(hStmt);
13813                 hStmt = NULL;
13814                 if (rc != SQLITE_OK) {
13815                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13816                         error = EMAIL_ERROR_DB_FAILURE;
13817                 }
13818         }
13819
13820         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13821         if (err_code != NULL)
13822                 *err_code = error;
13823
13824         EM_DEBUG_FUNC_END("ret [%d]", ret);
13825         return ret;
13826 }
13827
13828 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)
13829 {
13830         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);
13831
13832
13833         if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
13834                 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);
13835
13836                 if (err_code != NULL)
13837                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13838                 return false;
13839         }
13840
13841         int rc = -1;
13842         int ret = false;
13843         int error = EMAIL_ERROR_NONE;
13844         char sql_query_string[QUERY_SIZE] = {0, };
13845         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13846
13847         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13848         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13849
13850         if (activity_id == 0)
13851                 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);
13852         else
13853                 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);
13854
13855         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13856         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
13857         if (error != EMAIL_ERROR_NONE) {
13858                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
13859                 goto FINISH_OFF;
13860         }
13861
13862         /*  validate activity existence */
13863         rc = sqlite3_changes(local_db_handle);
13864         if (rc == 0) {
13865                 EM_DEBUG_EXCEPTION("No matching activity found");
13866                 error = EMAIL_ERROR_DATA_NOT_FOUND;
13867                 ret = true;
13868                 goto FINISH_OFF;
13869         }
13870
13871         ret = true;
13872
13873 FINISH_OFF:
13874         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13875
13876         if (err_code != NULL)
13877                 *err_code = error;
13878
13879         EM_DEBUG_FUNC_END("ret [%d]", ret);
13880         return ret;
13881 }
13882
13883 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)
13884 {
13885         EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
13886
13887         if (account_id < FIRST_ACCOUNT_ID || NULL == activity_count || NULL == err_code) {
13888                 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
13889                 if (err_code != NULL)
13890                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13891                 return false;
13892         }
13893         int rc = -1;
13894         int ret = false;
13895         int error = EMAIL_ERROR_NONE;
13896         char sql_query_string[QUERY_SIZE] = {0, };
13897
13898         DB_STMT hStmt = NULL;
13899
13900         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13901
13902         EMSTORAGE_START_READ_TRANSACTION(transaction);
13903         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13904
13905         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);
13906
13907         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13908
13909
13910         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13911         EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
13912         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13913                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13914
13915
13916         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13917         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13918                         ("sqlite3_step fail:%d", rc));
13919
13920         _get_stmt_field_data_int(hStmt, activity_count, 0);
13921
13922         EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
13923
13924         ret = true;
13925
13926 FINISH_OFF:
13927
13928         if (hStmt != NULL) {
13929                 rc = sqlite3_finalize(hStmt);
13930                 hStmt = NULL;
13931                 if (rc != SQLITE_OK) {
13932                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13933                         error = EMAIL_ERROR_DB_FAILURE;
13934                 }
13935         }
13936
13937         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13938
13939         if (err_code != NULL)
13940                 *err_code = error;
13941
13942         EM_DEBUG_FUNC_END("ret [%d]", ret);
13943         return ret;
13944 }
13945
13946 INTERNAL_FUNC int emstorage_get_pbd_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
13947 {
13948         EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
13949
13950         if (NULL == activity_count || NULL == err_code) {
13951                 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
13952                 if (err_code != NULL)
13953                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13954                 return false;
13955         }
13956         int rc = -1;
13957         int ret = false;
13958         int error = EMAIL_ERROR_NONE;
13959         DB_STMT hStmt = NULL;
13960         char sql_query_string[QUERY_SIZE] = {0, };
13961
13962         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13963
13964         EMSTORAGE_START_READ_TRANSACTION(transaction);
13965         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13966
13967         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl;");
13968
13969         EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
13970
13971         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13972         EM_DEBUG_LOG_DEV("  before sqlite3_prepare hStmt = %p", hStmt);
13973         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13974                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13975
13976         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13977         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13978                         ("sqlite3_step fail:%d", rc));
13979
13980         _get_stmt_field_data_int(hStmt, activity_count, 0);
13981
13982         EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
13983
13984         ret = true;
13985
13986 FINISH_OFF:
13987
13988
13989         if (hStmt != NULL) {
13990                 rc = sqlite3_finalize(hStmt);
13991                 hStmt = NULL;
13992                 if (rc != SQLITE_OK) {
13993                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13994                         error = EMAIL_ERROR_DB_FAILURE;
13995                 }
13996         }
13997
13998         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13999         if (err_code != NULL)
14000                 *err_code = error;
14001
14002         EM_DEBUG_FUNC_END("ret [%d]", ret);
14003         return ret;
14004 }
14005
14006 INTERNAL_FUNC int emstorage_delete_full_pbd_activity_data(char *multi_user_name, int account_id, int transaction, int *err_code)
14007 {
14008         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
14009         if (account_id < FIRST_ACCOUNT_ID) {
14010                 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
14011                 if (err_code != NULL)
14012                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14013                 return false;
14014         }
14015
14016         int rc = -1;
14017         int ret = false;
14018         int error = EMAIL_ERROR_NONE;
14019         char sql_query_string[QUERY_SIZE] = {0, };
14020
14021         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14022
14023         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14024         memset(sql_query_string, 0x00, sizeof(sql_query_string));
14025         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d", account_id);
14026
14027         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14028         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14029         if (error != EMAIL_ERROR_NONE) {
14030                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14031                 goto FINISH_OFF;
14032         }
14033
14034         rc = sqlite3_changes(local_db_handle);
14035         if (rc == 0) {
14036                 EM_DEBUG_EXCEPTION("No matching activities found in mail_partial_body_activity_tbl");
14037                 error = EMAIL_ERROR_DATA_NOT_FOUND;
14038                 ret = true;
14039                 goto FINISH_OFF;
14040         }
14041
14042         ret = true;
14043
14044 FINISH_OFF:
14045
14046         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14047
14048         if (err_code != NULL)
14049                 *err_code = error;
14050
14051         EM_DEBUG_FUNC_END("ret [%d]", ret);
14052         return ret;
14053 }
14054
14055 /*Himanshu[h.gahlaut]-> Added below API to update mail_partial_body_activity_tbl
14056   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*/
14057
14058 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)
14059 {
14060         EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
14061
14062         int rc = -1, ret = false;
14063         int error = EMAIL_ERROR_NONE;
14064         char sql_query_string[QUERY_SIZE] = {0, };
14065
14066         int transaction = true;
14067
14068         if (!old_server_uid || !new_server_uid || !mbox_name) {
14069                 EM_DEBUG_EXCEPTION("Invalid parameters");
14070                 error = EMAIL_ERROR_INVALID_PARAM;
14071                 return false;
14072         }
14073
14074         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14075
14076         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14077         memset(sql_query_string, 0x00, sizeof(sql_query_string));
14078         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14079                         "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);
14080
14081         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14082         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14083         if (error != EMAIL_ERROR_NONE) {
14084                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14085                 goto FINISH_OFF;
14086         }
14087
14088         rc = sqlite3_changes(local_db_handle);
14089         if (rc == 0)
14090                 EM_DEBUG_LOG("No matching found in mail_partial_body_activity_tbl");
14091
14092         ret = true;
14093
14094 FINISH_OFF:
14095         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14096
14097         if (err_code != NULL)
14098                 *err_code = error;
14099
14100         EM_DEBUG_FUNC_END("ret [%d]", ret);
14101         return ret;
14102 }
14103
14104
14105 INTERNAL_FUNC int emstorage_create_file(char *data_string, size_t file_size, char *dst_file_name, int *err_code)
14106 {
14107         EM_DEBUG_FUNC_BEGIN_SEC("file_size[%d] , dst_file_name[%s], err_code[%p]", file_size, dst_file_name, err_code);
14108
14109         int ret = false;
14110         int error = EMAIL_ERROR_NONE;
14111         FILE* fp_dst = NULL;
14112         char errno_buf[ERRNO_BUF_SIZE] = {0};
14113
14114         if (!data_string || !dst_file_name) {
14115                 EM_DEBUG_LOG("data_string[%p], dst_file_name[%p]", data_string, dst_file_name);
14116                 error = EMAIL_ERROR_INVALID_PARAM;
14117                 goto FINISH_OFF;
14118         }
14119
14120         error = em_fopen(dst_file_name, "w", &fp_dst);
14121         if (error != EMAIL_ERROR_NONE) {
14122                 EM_DEBUG_EXCEPTION_SEC("em_fopen failed - %s: %d", dst_file_name, error);
14123                 goto FINISH_OFF;
14124         }
14125
14126         if (fwrite(data_string, 1, file_size, fp_dst) == 0) {
14127                 EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
14128                 error = EMAIL_ERROR_UNKNOWN;
14129                 goto FINISH_OFF;
14130         }
14131
14132         ret = true;
14133
14134 FINISH_OFF:
14135
14136         if (fp_dst != NULL)
14137                 fclose(fp_dst);
14138
14139         if (err_code != NULL)
14140                 *err_code = error;
14141
14142         EM_DEBUG_FUNC_END("ret [%d]", ret);
14143         return ret;
14144 }
14145
14146 #endif /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
14147
14148
14149
14150 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
14151 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)
14152 {
14153         EM_DEBUG_FUNC_BEGIN();
14154         int rc = -1;
14155         int ret = false;
14156         int error = EMAIL_ERROR_NONE;
14157         char sql_query_string[QUERY_SIZE] = {0, };
14158
14159         int transaction = true;
14160
14161         if (!old_server_uid || !new_server_uid || !mbox_name) {
14162                 EM_DEBUG_EXCEPTION("Invalid parameters");
14163                 if (err_code != NULL)
14164                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14165                 return false;
14166         }
14167
14168         EM_DEBUG_LOG_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
14169
14170         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14171         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14172
14173
14174         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14175                         "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);
14176
14177         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
14178         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14179         if (error != EMAIL_ERROR_NONE) {
14180                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14181                 goto FINISH_OFF;
14182         }
14183
14184         rc = sqlite3_changes(local_db_handle);
14185         if (rc == 0) {
14186                 EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
14187                 error = EMAIL_ERROR_DATA_NOT_FOUND;
14188                 goto FINISH_OFF;
14189         }
14190
14191         ret = true;
14192
14193 FINISH_OFF:
14194         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14195
14196         if (err_code != NULL)
14197                 *err_code = error;
14198
14199         EM_DEBUG_FUNC_END("ret [%d]", ret);
14200         return ret;
14201
14202 }
14203
14204
14205 /**
14206  * @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);
14207  * Prepare an array of mail_id and corresponding server mail id.
14208  *
14209  * @author                                      h.gahlaut@samsung.com
14210  * @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)
14211  *                                                      where 88 is the length of fixed keywords including ending null character in the QUERY to be formed
14212  * @param[out] idset                    Returns the array of mail_id and corresponding server_mail_id sorted by server_mail_ids
14213  * @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
14214  * @param[out] err_code         Returns the error code.
14215  * @remarks                                     An Example of Query to be exexuted in this API:
14216  *                                                      SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (12, 13, 56, 78);
14217  * @return This function returns true on success or false on failure.
14218  */
14219 INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *multi_user_name,
14220                 char *mail_ids,
14221                 email_id_set_t** idset,
14222                 int *id_set_count,
14223                 int *err_code)
14224 {
14225         EM_DEBUG_FUNC_BEGIN();
14226         EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds);
14227
14228         int error = EMAIL_ERROR_NONE;
14229         int ret = false;
14230         email_id_set_t* p_id_set = NULL;
14231         int count = 0;
14232         const int buf_size = QUERY_SIZE;
14233         char sql_query_string[QUERY_SIZE] = {0, };
14234         int space_left_in_query_buffer = buf_size;
14235         int i = 0;
14236         int rc = -1;
14237         char *server_mail_id = NULL;
14238         char **result = NULL;
14239         int col_index = 0;
14240
14241
14242         if (NULL == mail_ids || NULL == idset || NULL == id_set_count) {
14243                 EM_DEBUG_EXCEPTION("Invalid Parameters mail_ids[%p] idset[%p] id_set_count [%p]",
14244                                 mail_ids, idset, id_set_count);
14245                 if (err_code != NULL)
14246                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14247                 return false;
14248         }
14249
14250         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14251
14252         SNPRINTF(sql_query_string, space_left_in_query_buffer,
14253                         "SELECT local_uid, server_uid FROM mail_read_mail_uid_tbl WHERE local_uid in (%s) ORDER BY server_uid",
14254                         mail_ids);
14255
14256         EM_DEBUG_LOG_SEC("SQL Query formed [%s] ", sql_query_string);
14257
14258         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14259         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14260         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result);
14261                         goto FINISH_OFF; },     ("SQL(%s) sqlite3_get_table fail:%d -%s",
14262                                 sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14263
14264         EM_DEBUG_LOG(" Count of mails [%d ]", count);
14265
14266         if (count <= 0) {
14267                 EM_DEBUG_EXCEPTION("Can't find proper mail");
14268                 error = EMAIL_ERROR_DATA_NOT_FOUND;
14269                 goto FINISH_OFF;
14270         }
14271
14272         if (NULL == (p_id_set = (email_id_set_t*)em_malloc(sizeof(email_id_set_t) * count))) {
14273                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
14274                 error = EMAIL_ERROR_OUT_OF_MEMORY;
14275                 goto FINISH_OFF;
14276         }
14277
14278         col_index = 2;
14279
14280         EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds_Loop);
14281         EM_DEBUG_LOG(">>>> DATA ASSIGN START");
14282         for (i = 0; i < count; i++) {
14283                 _get_table_field_data_int(result, &(p_id_set[i].mail_id), col_index++);
14284                 _get_table_field_data_string(result, &server_mail_id, 1, col_index++);
14285                 if (server_mail_id) {
14286                         p_id_set[i].server_mail_id = strtoul(server_mail_id, NULL, 10);
14287                         EM_SAFE_FREE(server_mail_id);
14288                 }
14289         }
14290         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d]", count);
14291         EM_PROFILE_END(EmStorageGetIdSetFromMailIds_Loop);
14292
14293         sqlite3_free_table(result);
14294         result = NULL;
14295
14296         ret = true;
14297
14298 FINISH_OFF:
14299
14300         if (ret == true) {
14301                 *idset = p_id_set;
14302                 *id_set_count = count;
14303                 EM_DEBUG_LOG(" idset[%p] id_set_count [%d]", *idset, *id_set_count);
14304         } else
14305                 EM_SAFE_FREE(p_id_set);
14306
14307
14308         if (err_code != NULL)
14309                 *err_code = error;
14310
14311         EM_PROFILE_END(EmStorageGetIdSetFromMailIds);
14312
14313         EM_DEBUG_FUNC_END("ret [%d]", ret);
14314         return ret;
14315 }
14316
14317
14318
14319 #endif
14320
14321 INTERNAL_FUNC int emstorage_delete_triggers_from_lucene(char *multi_user_name)
14322 {
14323         EM_DEBUG_FUNC_BEGIN();
14324         int ret = true, transaction = true;
14325         int error = EMAIL_ERROR_NONE;
14326         char sql_query_string[QUERY_SIZE] = {0, };
14327         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14328         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14329
14330         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerDelete;");
14331         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14332         if (error != EMAIL_ERROR_NONE) {
14333                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14334                 goto FINISH_OFF;
14335         }
14336
14337         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerInsert;");
14338         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14339         if (error != EMAIL_ERROR_NONE) {
14340                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14341                 goto FINISH_OFF;
14342         }
14343
14344         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerUpdate;");
14345         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14346         if (error != EMAIL_ERROR_NONE) {
14347                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14348                 goto FINISH_OFF;
14349         }
14350
14351         ret = true;
14352
14353 FINISH_OFF:
14354         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14355
14356         EM_DEBUG_FUNC_END("ret [%d]", ret);
14357         return ret;
14358 }
14359
14360 INTERNAL_FUNC int emstorage_update_tag_id(char *multi_user_name, int old_filter_id, int new_filter_id, int *err_code)
14361 {
14362         EM_DEBUG_FUNC_BEGIN("new_filter_id[%d], old_filter_id[%d]", new_filter_id, old_filter_id);
14363         int ret = false;
14364         int error = EMAIL_ERROR_NONE;
14365         char sql_query_string[QUERY_SIZE] = {0, };
14366         int transaction = true;
14367
14368         if (old_filter_id < 0 || new_filter_id < 0) {
14369                 EM_DEBUG_EXCEPTION("Invalid parameters");
14370                 if (err_code != NULL)
14371                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14372                 return false;
14373         }
14374
14375         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14376
14377         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14378
14379         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14380                         "UPDATE mail_tbl SET tag_id=%d WHERE tag_id=%d ", new_filter_id, old_filter_id);
14381
14382         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14383         if (error != EMAIL_ERROR_NONE) {
14384                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14385                 goto FINISH_OFF;
14386         }
14387
14388         ret = true;
14389
14390 FINISH_OFF:
14391         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14392
14393         if (err_code != NULL)
14394                 *err_code = error;
14395
14396         EM_DEBUG_FUNC_END("ret [%d]", ret);
14397         return ret;
14398 }
14399
14400 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)
14401 {
14402         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);
14403
14404         if ((account_id < 0) || (dest_mailbox_id < 0) || (!rule)) {
14405                 EM_DEBUG_EXCEPTION("Invalid Parameter");
14406
14407                 if (err_code != NULL)
14408                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14409                 return false;
14410         }
14411
14412         int rc = -1, ret = false, error = EMAIL_ERROR_NONE;
14413         int count = 0, col_index = 0, i = 0, where_pararaph_length = 0, *mail_list = NULL;
14414         int tag_id = rule->rule_id;
14415         char **result = NULL, *where_pararaph = NULL;
14416         char sql_query_string[QUERY_SIZE] = {0, };
14417         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14418
14419         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl ");
14420
14421         where_pararaph_length = EM_SAFE_STRLEN(rule->value) + 2 * (EM_SAFE_STRLEN(rule->value2)) + 100;
14422         where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
14423         if (where_pararaph == NULL) {
14424                 EM_DEBUG_EXCEPTION("malloc failed for where_pararaph.");
14425                 error = EMAIL_ERROR_OUT_OF_MEMORY;
14426                 goto FINISH_OFF;
14427         }
14428
14429         if (account_id != ALL_ACCOUNT)
14430                 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (0)", account_id);
14431         else
14432                 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_type NOT in (0)");
14433
14434         if (rule->type & EMAIL_FILTER_SUBJECT) {
14435                 if (rule->flag2 == RULE_TYPE_INCLUDES)
14436                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject like \'%%%q%%\'", rule->value);
14437                 else /*  RULE_TYPE_EXACTLY */
14438                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject = \'%q\'", rule->value);
14439         }
14440
14441         if (rule->type & EMAIL_FILTER_FROM) {
14442                 if (rule->flag2 == RULE_TYPE_INCLUDES)
14443                         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);
14444 #ifdef __FEATURE_COMPARE_DOMAIN__
14445                 else if (rule->flag2 == RULE_TYPE_COMPARE_DOMAIN)
14446                         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);
14447 #endif /*__FEATURE_COMPARE_DOMAIN__ */
14448                 else /*  RULE_TYPE_EXACTLY */
14449                         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);
14450         }
14451
14452         if (rule->type == EMAIL_PRIORITY_SENDER) {
14453                 if (rule->flag2 == RULE_TYPE_INCLUDES)
14454                         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);
14455                 else /*  RULE_TYPE_EXACTLY */
14456                         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);
14457
14458                 tag_id = PRIORITY_SENDER_TAG_ID;
14459         }
14460
14461         /* prevent 34361 */
14462         if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14463                 EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14464         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14465
14466         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14467         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14468         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
14469                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14470
14471         EM_DEBUG_LOG("Count of mails [%d]", count);
14472
14473         if (count) {
14474                 mail_list = malloc(sizeof(int) * count);
14475                 if (mail_list == NULL) {
14476                         EM_DEBUG_EXCEPTION("malloc failed for mail_list.");
14477                         error = EMAIL_ERROR_OUT_OF_MEMORY;
14478                         goto FINISH_OFF;
14479                 }
14480
14481                 col_index = 1;
14482
14483                 for (i = 0; i < count; i++)
14484                         _get_table_field_data_int(result, &(mail_list[i]), col_index++);
14485
14486                 memset(sql_query_string, 0x00, QUERY_SIZE);
14487                 if (reset) {
14488                         switch (rule->action_type) {
14489                         case EMAIL_FILTER_MOVE:
14490                                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = 0 ");
14491                                 break;
14492                         case EMAIL_FILTER_BLOCK:
14493                         default:
14494                                 EM_DEBUG_LOG("Not support : action_type[%d]", rule->action_type);
14495                                 ret = true;
14496                                 goto FINISH_OFF;
14497                         }
14498                 } else {
14499                         switch (rule->action_type) {
14500                         case EMAIL_FILTER_MOVE:
14501                                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = %d ", tag_id);
14502                                 break;
14503                         case EMAIL_FILTER_BLOCK:
14504                                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET mailbox_id = %d, mailbox_type = %d ", dest_mailbox_id, dest_mailbox_type);
14505                                 break;
14506                         default:
14507                                 EM_DEBUG_LOG("Not support");
14508                                 ret = true;
14509                                 goto FINISH_OFF;
14510                         }
14511                 }
14512                 /* prevent 34361 */
14513                 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14514                         EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14515
14516                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14517                 if (error != EMAIL_ERROR_NONE) {
14518                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14519                         goto FINISH_OFF;
14520                 }
14521
14522 #ifdef __FEATURE_BODY_SEARCH__
14523                 /* Updating mail_text_tbl */
14524                 if (rule->action_type == EMAIL_FILTER_BLOCK) {
14525                         memset(sql_query_string, 0x00, QUERY_SIZE);
14526                         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_text_tbl SET mailbox_id = %d ", dest_mailbox_id);
14527                         if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14528                                 EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14529
14530                         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14531                         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14532                         if (error != EMAIL_ERROR_NONE) {
14533                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14534                                 goto FINISH_OFF;
14535                         }
14536                 }
14537 #endif
14538         }
14539
14540         ret = true;
14541
14542 FINISH_OFF:
14543
14544         if (ret) {
14545                 if (filtered_mail_id_list)
14546                         *filtered_mail_id_list = mail_list;
14547
14548                 if (count_of_mails)
14549                         *count_of_mails = count;
14550         } else
14551                 EM_SAFE_FREE(mail_list);
14552
14553         sqlite3_free_table(result);
14554         result = NULL;
14555
14556
14557         EM_SAFE_FREE(where_pararaph);
14558
14559         if (err_code != NULL)
14560                 *err_code = error;
14561
14562         EM_DEBUG_FUNC_END("ret [%d]", ret);
14563         return ret;
14564 }
14565
14566 #define EMAIL_SLOT_UNIT 25
14567 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)
14568 {
14569         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);
14570         int ret = false, err = EMAIL_ERROR_NONE;
14571         int where_pararaph_length = 0;
14572         char *where_pararaph = NULL;
14573         char sql_query_string[QUERY_SIZE] = {0, };
14574         int and = 0;
14575         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14576
14577         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
14578
14579         if (new_slot_size > 0)
14580                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = %d ", new_slot_size);
14581         else if (new_slot_size == 0)
14582                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", EMAIL_SLOT_UNIT);
14583         else
14584                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", new_slot_size * -1);
14585
14586
14587         if (mailbox_id)
14588                 where_pararaph_length = 80;
14589         else
14590                 where_pararaph_length = 50;
14591
14592         if (new_slot_size == 0)
14593                 where_pararaph_length += 70;
14594
14595         where_pararaph = malloc(sizeof(char) * where_pararaph_length);
14596         if (where_pararaph == NULL) {
14597                 EM_DEBUG_EXCEPTION("Memory allocation failed for where_pararaph");
14598                 err = EMAIL_ERROR_OUT_OF_MEMORY;
14599                 goto FINISH_OFF;
14600         }
14601         memset(where_pararaph, 0x00, where_pararaph_length);
14602
14603         if (account_id > ALL_ACCOUNT) {
14604                 and = 1;
14605                 if (mailbox_id)
14606                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_id = %d ", mailbox_id);
14607                 else
14608                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d ", account_id);
14609         }
14610
14611         if (new_slot_size == 0)
14612                 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"));
14613
14614         if (strlen(sql_query_string) + EM_SAFE_STRLEN(where_pararaph) < QUERY_SIZE) /* prevent 34363 */
14615                 EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14616         else {
14617                 EM_DEBUG_EXCEPTION("Query buffer overflowed !!!");
14618                 err = EMAIL_ERROR_OUT_OF_MEMORY;
14619                 goto FINISH_OFF;
14620         }
14621
14622         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14623         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14624         if (err != EMAIL_ERROR_NONE) {
14625                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
14626                 goto FINISH_OFF;
14627         }
14628
14629         ret = true;
14630
14631 FINISH_OFF:
14632         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
14633
14634         EM_SAFE_FREE(where_pararaph);
14635
14636         if (err_code != NULL)
14637                 *err_code = err;
14638
14639         EM_DEBUG_FUNC_END("ret [%d]", ret);
14640         return ret;
14641 }
14642
14643 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)
14644 {
14645         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);
14646
14647         if (!meeting_req || meeting_req->mail_id <= 0) {
14648                 if (meeting_req)
14649                         EM_DEBUG_EXCEPTION("mail_id[%d]", meeting_req->mail_id);
14650
14651                 if (err_code != NULL)
14652                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14653
14654                 return false;
14655         }
14656
14657         int rc = -1;
14658         int ret = false;
14659         int error = EMAIL_ERROR_NONE;
14660         DB_STMT hStmt = NULL;
14661         char sql_query_string[QUERY_SIZE] = {0, };
14662         int col_index = 0;
14663         time_t temp_unix_time = 0;
14664
14665         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14666         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14667
14668         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14669                         "INSERT INTO mail_meeting_tbl VALUES "
14670                         "(?"            /*  mail_id */
14671                         ", ?"           /*  account_id */
14672                         ", ?"           /*  mailbox_id */
14673                         ", ?"           /*  meeting_response */
14674                         ", ?"           /*  start_time */
14675                         ", ?"           /*  end_time */
14676                         ", ?"           /*  location */
14677                         ", ?"           /*  global_object_id */
14678                         ", ?"           /*  offset */
14679                         ", ?"           /*  standard_name */
14680                         ", ?"           /*  standard_time_start_date */
14681                         ", ?"           /*  standard_biad */
14682                         ", ?"           /*  daylight_name */
14683                         ", ?"           /*  daylight_time_start_date */
14684                         ", ?"           /*  daylight_bias */
14685                         ")");
14686
14687         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14688         if (rc != SQLITE_OK) {
14689                 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
14690                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
14691
14692                 error = EMAIL_ERROR_DB_FAILURE;
14693                 goto FINISH_OFF;
14694         }
14695
14696         col_index = 0;
14697         /*
14698            EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14699            EM_DEBUG_LOG_SEC(">>>>> account_id[%d]", account_id);
14700            EM_DEBUG_LOG_SEC(">>>>> mailbox_name[%s]", mailbox_name);
14701            EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14702            EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14703            EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14704            EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14705            EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14706            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14707            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14708            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14709            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14710            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14711            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14712            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14713            */
14714         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->mail_id);
14715         _bind_stmt_field_data_int(hStmt, col_index++, account_id);
14716         _bind_stmt_field_data_int(hStmt, col_index++, input_mailbox_id);
14717         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14718
14719         temp_unix_time = timegm(&(meeting_req->start_time));
14720         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14721         temp_unix_time = timegm(&(meeting_req->end_time));
14722         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14723
14724         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->location, 0, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14725         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->global_object_id, 0, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14726
14727         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14728         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.standard_name, 0, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14729         temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14730         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14731         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14732
14733         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.daylight_name, 0, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14734         temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14735         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14736         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
14737
14738
14739         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14740         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
14741                         ("sqlite3_step fail:%d", rc));
14742         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14743                         ("sqlite3_step fail:%d", rc));
14744
14745         ret = true;
14746
14747 FINISH_OFF:
14748         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14749         if (hStmt != NULL) {
14750                 rc = sqlite3_finalize(hStmt);
14751                 if (rc != SQLITE_OK) {
14752                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14753                         error = EMAIL_ERROR_DB_FAILURE;
14754                 }
14755         }
14756
14757         if (err_code != NULL)
14758                 *err_code = error;
14759
14760         EM_DEBUG_FUNC_END("ret [%d]", ret);
14761         return ret;
14762 }
14763
14764 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)
14765 {
14766         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);
14767
14768         int i = 0;
14769         int col_index = 0;
14770         int rc;
14771         int count = 0;
14772         int dummy = 0;
14773         int err = EMAIL_ERROR_NONE;
14774         char **result = NULL;
14775         char sql_query_string[QUERY_SIZE] = {0, };
14776         email_meeting_request_t* p_temp_meeting_req = NULL;
14777         sqlite3 *local_db_handle = NULL;
14778         time_t temp_unix_time;
14779
14780         if (conditional_clause == NULL || output_meeting_req == NULL || output_result_count == NULL) {
14781                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14782                 err = EMAIL_ERROR_INVALID_PARAM;
14783                 goto FINISH_OFF;
14784         }
14785
14786         col_index = _field_count_of_table[CREATE_TABLE_MAIL_MEETING_TBL];
14787         EM_DEBUG_LOG("col_index [%d]", col_index);
14788
14789         local_db_handle = emstorage_get_db_connection(multi_user_name);
14790
14791         EMSTORAGE_START_READ_TRANSACTION(transaction);
14792
14793         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_meeting_tbl %s", conditional_clause);
14794
14795         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14796
14797         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14798         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
14799                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14800
14801         if (!count) {
14802                 EM_DEBUG_EXCEPTION("No meeting_request found...");
14803                 err = EMAIL_ERROR_DATA_NOT_FOUND;
14804                 goto FINISH_OFF;
14805         }
14806
14807         EM_DEBUG_LOG("There are [%d] meeting requests.", count);
14808         if (!(p_temp_meeting_req = (email_meeting_request_t*)em_malloc(sizeof(email_meeting_request_t) * count))) {
14809                 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
14810                 err = EMAIL_ERROR_OUT_OF_MEMORY;
14811                 goto FINISH_OFF;
14812         }
14813
14814         EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
14815
14816         for (i = 0; i < count; i++) {
14817                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].mail_id), col_index++);
14818                 _get_table_field_data_int(result, &dummy, col_index++); /* account_id. but why should this field exist in DB table? */
14819                 _get_table_field_data_int(result, &dummy, col_index++); /* mailbox_id */
14820                 _get_table_field_data_int(result, (int*)&(p_temp_meeting_req[i].meeting_response), col_index++);
14821                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* start time */
14822                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].start_time));
14823                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* end time */
14824                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].end_time));
14825                 _get_table_field_data_string(result, &p_temp_meeting_req[i].location, 1, col_index++);
14826                 _get_table_field_data_string(result, &p_temp_meeting_req[i].global_object_id, 1, col_index++);
14827                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.offset_from_GMT), col_index++);
14828                 _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++);
14829                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14830                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.standard_time_start_date));
14831                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.standard_bias), col_index++);
14832                 _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++);
14833                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14834                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.daylight_time_start_date));
14835                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.daylight_bias), col_index++);
14836         }
14837
14838
14839 FINISH_OFF:
14840         if (result)
14841                 sqlite3_free_table(result);
14842
14843         if (err == EMAIL_ERROR_NONE) {
14844                 if (p_temp_meeting_req)
14845                         *output_meeting_req = p_temp_meeting_req;
14846                 *output_result_count = count;
14847         } else
14848                 EM_SAFE_FREE(p_temp_meeting_req);
14849
14850         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14851
14852         //      sqlite3_db_release_memory(local_db_handle);
14853
14854         EM_DEBUG_FUNC_END("err [%d]", err);
14855         return err;
14856 }
14857
14858 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)
14859 {
14860         EM_DEBUG_FUNC_BEGIN();
14861
14862         int count = 0;
14863         int ret = false;
14864         int error = EMAIL_ERROR_NONE;
14865         char conditional_clause[QUERY_SIZE] = {0, };
14866
14867         EM_IF_NULL_RETURN_VALUE(meeting_req, false);
14868
14869
14870         SNPRINTF(conditional_clause, QUERY_SIZE, " WHERE mail_id = %d", mail_id);
14871         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
14872
14873         if ((error = emstorage_query_meeting_request(multi_user_name, conditional_clause, meeting_req, &count, transaction)) != EMAIL_ERROR_NONE) {
14874                 EM_DEBUG_EXCEPTION("emstorage_query_meeting_request failed. [%d]", error);
14875                 goto FINISH_OFF;
14876         }
14877
14878         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->mail_id[%d]", (*meeting_req)->mail_id);
14879         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->meeting_response[%d]", (*meeting_req)->meeting_response);
14880         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->start_time[%s]", asctime(&((*meeting_req)->start_time)));
14881         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->end_time[%s]", asctime(&((*meeting_req)->end_time)));
14882         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->location[%s]", (*meeting_req)->location);
14883         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->global_object_id[%s]", (*meeting_req)->global_object_id);
14884         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.offset_from_GMT[%d]", (*meeting_req)->time_zone.offset_from_GMT);
14885         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_name[%s]", (*meeting_req)->time_zone.standard_name);
14886         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.standard_time_start_date)));
14887         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_bias[%d]", (*meeting_req)->time_zone.standard_bias);
14888         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_name[%s]", (*meeting_req)->time_zone.daylight_name);
14889         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.daylight_time_start_date)));
14890         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_bias[%d]", (*meeting_req)->time_zone.daylight_bias);
14891         ret = true;
14892
14893 FINISH_OFF:
14894
14895         if (err_code != NULL)
14896                 *err_code = error;
14897
14898         EM_DEBUG_FUNC_END("ret [%d]", ret);
14899         return ret;
14900 }
14901
14902 INTERNAL_FUNC int emstorage_update_meeting_request(char *multi_user_name, email_meeting_request_t* meeting_req, int transaction, int *err_code)
14903 {
14904         EM_DEBUG_FUNC_BEGIN("meeting_req[%p], transaction[%d], err_code[%p]", meeting_req, transaction, err_code);
14905
14906         int ret = false;
14907         int error = EMAIL_ERROR_NONE;
14908         int rc;
14909         DB_STMT hStmt = NULL;
14910         char sql_query_string[QUERY_SIZE] = {0, };
14911         time_t temp_unix_time = 0;
14912
14913         if (!meeting_req) {
14914                 EM_DEBUG_EXCEPTION("Invalid Parameter!");
14915                 if (err_code != NULL)
14916                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14917                 return false;
14918         }
14919
14920         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14921         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14922
14923         memset(sql_query_string, 0x00, sizeof(sql_query_string));
14924         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14925                         "UPDATE mail_meeting_tbl "
14926                         "SET "
14927                         /* "  account_id = ?, "         //  not update here, this can be changed when move or copy */
14928                         /* "  mailbox_name = ?, "               //  not update here, this can be changed when move or copy */
14929                         "  meeting_response = ?, "
14930                         "  start_time = ?, "
14931                         "  end_time = ?, "
14932                         "  location = ?, "
14933                         "  global_object_id = ?, "
14934                         "  offset = ?, "
14935                         "  standard_name = ?, "
14936                         "  standard_time_start_date = ?, "
14937                         "  standard_bias = ?, "
14938                         "  daylight_name = ?, "
14939                         "  daylight_time_start_date = ?, "
14940                         "  daylight_bias = ? "
14941                         "WHERE mail_id = %d",
14942                         meeting_req->mail_id);
14943
14944         EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
14945
14946         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14947         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14948                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14949         /*
14950            EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14951            EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14952            EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14953            EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14954            EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14955            EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14956            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14957            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14958            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14959            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14960            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14961            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14962            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14963            */
14964         int col_index = 0;
14965
14966         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14967         temp_unix_time = timegm(&(meeting_req->start_time));
14968         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14969         temp_unix_time = timegm(&(meeting_req->end_time));
14970         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14971         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->location, 1, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14972         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->global_object_id, 1, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14973         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14974         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.standard_name, 1, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14975         temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14976         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14977         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14978         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.daylight_name, 1, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14979         temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14980         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14981         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
14982
14983
14984         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14985         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14986                         ("sqlite3_step fail:%d", rc));
14987         ret = true;
14988
14989
14990 FINISH_OFF:
14991         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14992
14993         if (hStmt != NULL) {
14994                 rc = sqlite3_finalize(hStmt);
14995                 if (rc != SQLITE_OK) {
14996                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14997                         error = EMAIL_ERROR_DB_FAILURE;
14998                 }
14999         }
15000
15001         if (err_code != NULL)
15002                 *err_code = error;
15003
15004         EM_DEBUG_FUNC_END("ret [%d]", ret);
15005         return ret;
15006 }
15007
15008 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)
15009 {
15010         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);
15011
15012         if (account_id < ALL_ACCOUNT || mail_id < 0) {
15013                 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d]", account_id, mail_id);
15014
15015                 if (err_code != NULL)
15016                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15017                 return false;
15018         }
15019
15020         int ret = false;
15021         int error = EMAIL_ERROR_NONE;
15022         int and = false;
15023         char sql_query_string[QUERY_SIZE] = {0, };
15024
15025         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15026         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15027
15028         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_meeting_tbl ");
15029
15030         if (account_id != ALL_ACCOUNT) {                /*  NOT '0' means a specific account. '0' means all account */
15031                 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);
15032                 and = true;
15033         }
15034         if (mail_id > 0) {
15035                 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);
15036                 and = true;
15037         }
15038         if (input_mailbox_id > 0) {             /*  0 means all mailbox_id */
15039                 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);
15040         }
15041
15042         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15043         if (error != EMAIL_ERROR_NONE) {
15044                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
15045                 goto FINISH_OFF;
15046         }
15047
15048         ret = true;
15049
15050 FINISH_OFF:
15051         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15052
15053         if (err_code)
15054                 *err_code = error;
15055
15056         EM_DEBUG_FUNC_END("ret [%d]", ret);
15057         return ret;
15058 }
15059
15060
15061 INTERNAL_FUNC void emstorage_free_meeting_request(email_meeting_request_t *meeting_req)
15062 {
15063         EM_DEBUG_FUNC_BEGIN();
15064
15065         if (!meeting_req) return;
15066
15067         EM_SAFE_FREE(meeting_req->location);
15068         EM_SAFE_FREE(meeting_req->global_object_id);
15069
15070         EM_DEBUG_FUNC_END();
15071 }
15072
15073 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)
15074 {
15075         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);
15076         EM_PROFILE_BEGIN(profile_emstorage_get_overflowed_mail_id_list);
15077         char sql_query_string[QUERY_SIZE] = {0, };
15078         char **result = NULL;
15079         int rc = -1, ret = false;
15080         int error = EMAIL_ERROR_NONE;
15081         int counter = 0, col_index = 0;
15082         int result_mail_id_count = 0;
15083         int *result_mail_id_list = NULL;
15084
15085         if (input_mailbox_id <= 0 || !mail_id_list || !mail_id_count || account_id < 1) {
15086                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15087                 if (err_code)
15088                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15089                 return false;
15090         }
15091
15092         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);
15093
15094         EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
15095
15096         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15097         EMSTORAGE_START_READ_TRANSACTION(transaction);
15098
15099         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_mail_id_count, 0, NULL), rc);
15100         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15101                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15102
15103         if (!result_mail_id_count) {
15104                 EM_DEBUG_LOG("No mail found...");
15105                 ret = false;
15106                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
15107                 goto FINISH_OFF;
15108         }
15109
15110         EM_DEBUG_LOG("There are [%d] overflowed mails in mailbox_id [%d]", result_mail_id_count, input_mailbox_id);
15111
15112         if (!(result_mail_id_list = (int *)malloc(sizeof(int) * result_mail_id_count))) {
15113                 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
15114                 error = EMAIL_ERROR_OUT_OF_MEMORY;
15115                 sqlite3_free_table(result);
15116                 goto FINISH_OFF;
15117         }
15118
15119         memset(result_mail_id_list, 0x00, sizeof(int) * result_mail_id_count);
15120
15121         col_index = 1;
15122
15123         for (counter = 0; counter < result_mail_id_count; counter++)
15124                 _get_table_field_data_int(result, result_mail_id_list + counter, col_index++);
15125
15126         ret = true;
15127
15128 FINISH_OFF:
15129         EM_DEBUG_LOG("finish off [%d]", ret);
15130
15131         if (result)
15132                 sqlite3_free_table(result);
15133
15134         if (ret == true) {
15135                 *mail_id_list = result_mail_id_list;
15136                 *mail_id_count = result_mail_id_count;
15137         } else
15138                 EM_SAFE_FREE(result_mail_id_list);
15139
15140         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15141
15142         if (err_code != NULL)
15143                 *err_code = error;
15144
15145         EM_PROFILE_END(profile_emstorage_get_overflowed_mail_id_list);
15146         EM_DEBUG_FUNC_END("ret [%d]", ret);
15147         return ret;
15148 }
15149
15150 INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(char *multi_user_name, int mail_id, int *thread_id, int *err_code)
15151 {
15152         EM_DEBUG_FUNC_BEGIN("mail_id[%d], thread_id[%p], err_code[%p]", mail_id, thread_id, err_code);
15153
15154         int rc = -1, ret = false;
15155         int err = EMAIL_ERROR_NONE;
15156         char sql_query_string[QUERY_SIZE] = {0, };
15157         char **result;
15158         int result_count = 0;
15159
15160         if (mail_id == 0 || thread_id == NULL) {
15161                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15162                 if (err_code)
15163                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15164                 return false;
15165         }
15166
15167         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15168
15169         memset(sql_query_string, 0, QUERY_SIZE);
15170         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT thread_id FROM mail_tbl WHERE mail_id = %d", mail_id);
15171
15172         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15173         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15174         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15175                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15176
15177         if (!result_count) {
15178                 EM_DEBUG_LOG("No mail found...");
15179                 ret = false;
15180                 err = EMAIL_ERROR_MAIL_NOT_FOUND;
15181                 /* sqlite3_free_table(result); */
15182                 goto FINISH_OFF;
15183         }
15184
15185         _get_table_field_data_int(result, thread_id, 1);
15186
15187         sqlite3_free_table(result);
15188
15189         ret = true;
15190
15191 FINISH_OFF:
15192
15193         if (err_code != NULL)
15194                 *err_code = err;
15195
15196         EM_DEBUG_FUNC_END("ret [%d]", ret);
15197         return ret;
15198 }
15199
15200 INTERNAL_FUNC int emstorage_update_latest_thread_mail(char *multi_user_name,
15201                 int account_id,
15202                 int mailbox_id,
15203                 int mailbox_type,
15204                 int thread_id,
15205                 int *updated_thread_id,
15206                 int latest_mail_id,
15207                 int thread_item_count,
15208                 int noti_type,
15209                 int transaction,
15210                 int *err_code)
15211 {
15212         EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id [%d], thread_id[%d], updated_thread_id[%p], "
15213                         "latest_mail_id [%d], thread_item_count[%d], err_code[%p]",
15214                         account_id, mailbox_id, thread_id, updated_thread_id,
15215                         latest_mail_id, thread_item_count, err_code);
15216
15217         int rc = -1, ret = false;
15218         int err = EMAIL_ERROR_NONE;
15219         char sql_query_string[QUERY_SIZE] = {0, };
15220         char **result = NULL;
15221         int result_count = 0;
15222
15223         if (thread_id == 0) {
15224                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15225                 if (err_code)
15226                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15227                 return false;
15228         }
15229
15230         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15231
15232         if (thread_item_count == 0 && latest_mail_id == 0) {
15233                 memset(sql_query_string, 0, QUERY_SIZE);
15234                 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);
15235
15236                 /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15237                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15238                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15239                                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15240                 EM_DEBUG_LOG("result_count[%d]", result_count);
15241                 if (result_count == 0) {
15242                         EM_DEBUG_LOG("No mail found...");
15243                         ret = false;
15244                         if (err_code)
15245                                 *err_code =  EMAIL_ERROR_MAIL_NOT_FOUND;
15246                         sqlite3_free_table(result);
15247                         return false;
15248                 }
15249
15250                 _get_table_field_data_int(result, &latest_mail_id, 2);
15251                 _get_table_field_data_int(result, &thread_item_count, 3);
15252
15253                 EM_DEBUG_LOG("latest_mail_id[%d]", latest_mail_id);
15254                 EM_DEBUG_LOG("thread_item_count[%d]", thread_item_count);
15255
15256                 sqlite3_free_table(result);
15257         }
15258
15259         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15260
15261         if (thread_item_count < 0) {
15262                 memset(sql_query_string, 0, QUERY_SIZE);
15263                 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);
15264                 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15265                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15266                 if (err != EMAIL_ERROR_NONE) {
15267                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15268                         goto FINISH_OFF;
15269                 }
15270         } else if (thread_id != latest_mail_id) {
15271                 /* Initialize the thread id */
15272                 memset(sql_query_string, 0, QUERY_SIZE);
15273                 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);
15274                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15275                 if (err != EMAIL_ERROR_NONE) {
15276                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15277                         goto FINISH_OFF;
15278                 }
15279
15280                 /* update the thread item count */
15281                 memset(sql_query_string, 0, QUERY_SIZE);
15282                 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);
15283                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15284                 if (err != EMAIL_ERROR_NONE) {
15285                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15286                         goto FINISH_OFF;
15287                 }
15288         } else {
15289                 memset(sql_query_string, 0, QUERY_SIZE);
15290                 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);
15291                 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15292                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15293                 if (err != EMAIL_ERROR_NONE) {
15294                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15295                         goto FINISH_OFF;
15296                 }
15297         }
15298         ret = true;
15299
15300 FINISH_OFF:
15301         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15302
15303         if (thread_id != latest_mail_id) {
15304                 if (err == EMAIL_ERROR_NONE) {
15305                         EM_DEBUG_LOG("noti_type[%d]", noti_type);
15306
15307                         if (latest_mail_id > 0 && thread_id > 0 && noti_type > 0) {
15308                                 char mailbox_id_str[25] = {0,};
15309                                 snprintf(mailbox_id_str, sizeof(mailbox_id_str), "%d", mailbox_id);
15310                                 if (!emcore_notify_storage_event(noti_type, thread_id, latest_mail_id, mailbox_id_str, account_id))
15311                                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventfailed [NOTI_THREAD_ID_CHANGED] >>>> ");
15312
15313                                 if (updated_thread_id) *updated_thread_id = latest_mail_id;
15314                         }
15315                 }
15316         } else if (thread_item_count >= 0) {
15317                 if (err == EMAIL_ERROR_NONE) {
15318                         char parameter_string[500] = {0,};
15319                         SNPRINTF(parameter_string, sizeof(parameter_string), "%s%c%d", "thread_item_count", 0x01, latest_mail_id);
15320                         if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT, parameter_string, thread_item_count))
15321                                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventfailed [NOTI_MAIL_FIELD_UPDATE] >>>> ");
15322                 }
15323         }
15324
15325         if (err_code != NULL)
15326                 *err_code = err;
15327
15328         EM_DEBUG_FUNC_END("ret [%d]", ret);
15329         return ret;
15330 }
15331
15332 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)
15333 {
15334         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);
15335
15336         int ret = false;
15337         int err = EMAIL_ERROR_NONE;
15338         char sql_query_string[QUERY_SIZE] = {0, };
15339
15340         if (thread_id == 0) {
15341                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15342                 if (err_code)
15343                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15344                 return false;
15345         }
15346
15347         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15348
15349         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15350
15351         memset(sql_query_string, 0, QUERY_SIZE);
15352         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);
15353         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15354         if (err != EMAIL_ERROR_NONE) {
15355                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15356                 goto FINISH_OFF;
15357         }
15358
15359         ret = true;
15360
15361 FINISH_OFF:
15362         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15363
15364         if (err_code != NULL)
15365                 *err_code = err;
15366
15367         EM_DEBUG_FUNC_END("ret [%d]", ret);
15368         return ret;
15369 }
15370
15371 #ifdef __FEATURE_LOCAL_ACTIVITY__
15372 /**
15373  * emstorage_add_activity - Add Email Local activity during OFFLINE mode
15374  *
15375  */
15376 INTERNAL_FUNC int emstorage_add_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15377 {
15378         EM_DEBUG_FUNC_BEGIN();
15379
15380         EM_DEBUG_LOG(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15381
15382         int rc = -1, ret = false;
15383         int error = EMAIL_ERROR_NONE;
15384         DB_STMT hStmt = NULL;
15385         char sql_query_string[8192] = { 0x00, };
15386         int i = 0;
15387
15388         if (!local_activity) {
15389                 EM_DEBUG_EXCEPTION(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15390                 if (err_code != NULL)
15391                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15392                 return false;
15393         }
15394
15395         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15396
15397         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15398
15399         memset(sql_query_string, 0x00 , sizeof(sql_query_string));
15400         SNPRINTF(sql_query_string, sizeof(sql_query_string), "INSERT INTO mail_local_activity_tbl VALUES (?, ?, ?, ?, ?, ?, ?)");
15401
15402         EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ] ", local_activity->activity_id);
15403         EM_DEBUG_LOG(">>>>> MAIL ID [ %d ] ", local_activity->mail_id);
15404         EM_DEBUG_LOG(">>>>> ACCOUNT ID [ %d ] ", local_activity->account_id);
15405         EM_DEBUG_LOG(">>>>> ACTIVITY TYPE [ %d ] ", local_activity->activity_type);
15406         EM_DEBUG_LOG_SEC(">>>>> SERVER MAIL ID [ %s ] ", local_activity->server_mailid);
15407         EM_DEBUG_LOG(">>>>> SOURCE MAILBOX [ %s ] ", local_activity->src_mbox);
15408         EM_DEBUG_LOG(">>>>> DEST MAILBOX   [ %s ] ", local_activity->dest_mbox);
15409
15410         EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15411
15412
15413         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15414         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15415                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15416
15417         EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15418
15419         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
15420         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
15421         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
15422         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
15423         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->server_mailid, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
15424         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->src_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15425         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->dest_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15426
15427
15428         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15429
15430         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
15431                         ("sqlite3_step fail:%d", rc));
15432         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15433                         ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
15434
15435         ret = true;
15436
15437 FINISH_OFF:
15438         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15439
15440         if (hStmt != NULL) {
15441                 rc = sqlite3_finalize(hStmt);
15442                 if (rc != SQLITE_OK) {
15443                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15444                         error = EMAIL_ERROR_DB_FAILURE;
15445                 }
15446         }
15447
15448         if (err_code != NULL)
15449                 *err_code = error;
15450
15451         EM_DEBUG_FUNC_END("ret [%d]", ret);
15452         return ret;
15453 }
15454
15455 /**
15456  *      emstorage_get_activity - Get the Local activity Information
15457  *
15458  *
15459  */
15460 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)
15461 {
15462         EM_DEBUG_FUNC_BEGIN();
15463
15464         int i = 0, count = 0, rc = -1, ret = false;
15465         int error = EMAIL_ERROR_NONE;
15466         emstorage_activity_tbl_t *p_activity_tbl = NULL;
15467         char sql_query_string[1024] = {0x00, };
15468         char **result = NULL;
15469         int col_index ;
15470
15471         EM_IF_NULL_RETURN_VALUE(activity_list, false);
15472         EM_IF_NULL_RETURN_VALUE(select_num, false);
15473
15474
15475         if (!select_num || !activity_list || account_id <= 0 || activityid < 0) {
15476                 EM_DEBUG_LOG(" select_num[%p], activity_list[%p] account_id [%d] activityid [%d] ", select_num, activity_list, account_id, activityid);
15477                 if (err_code)
15478                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15479                 return false;
15480         }
15481
15482         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15483
15484         EMSTORAGE_START_READ_TRANSACTION(transaction);
15485
15486         memset(sql_query_string, 0x00, sizeof(sql_query_string));
15487
15488         if (activityid == ALL_ACTIVITIES) {
15489                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
15490                         "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d order by activity_id", account_id);
15491         } else {
15492                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
15493                         "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d AND activity_id = %d ", account_id, activityid);
15494         }
15495
15496         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
15497
15498
15499
15500         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15501         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15502                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15503
15504         col_index = 7;
15505
15506         if (!(p_activity_tbl = (emstorage_activity_tbl_t*)em_malloc(sizeof(emstorage_activity_tbl_t) * count))) {
15507                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
15508                 error = EMAIL_ERROR_OUT_OF_MEMORY;
15509                 goto FINISH_OFF;
15510         }
15511
15512
15513         for (i = 0; i < count; i++) {
15514                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15515                 if (result[col_index])
15516                         p_activity_tbl[i].activity_id = atoi(result[col_index++]);
15517
15518                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15519                 if (result[col_index])
15520                         p_activity_tbl[i].account_id = atoi(result[col_index++]);
15521
15522                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15523                 if (result[col_index])
15524                         p_activity_tbl[i].mail_id = atoi(result[col_index++]);
15525
15526                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15527                 if (result[col_index])
15528                         p_activity_tbl[i].activity_type = atoi(result[col_index++]);
15529
15530
15531                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15532                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15533                         p_activity_tbl[i].server_mailid = EM_SAFE_STRDUP(result[col_index++]);
15534                 else
15535                         col_index++;
15536
15537                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15538                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15539                         p_activity_tbl[i].src_mbox = EM_SAFE_STRDUP(result[col_index++]);
15540                 else
15541                         col_index++;
15542
15543                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15544                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15545                         p_activity_tbl[i].dest_mbox = EM_SAFE_STRDUP(result[col_index++]);
15546                 else
15547                         col_index++;
15548
15549         }
15550
15551         if (result)
15552                 sqlite3_free_table(result);
15553
15554         ret = true;
15555
15556 FINISH_OFF:
15557
15558         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15559
15560         if (ret == true) {
15561                 *activity_list = p_activity_tbl;
15562                 *select_num = count;
15563                 EM_DEBUG_LOG(">>>> COUNT : %d >> ", count);
15564         } else if (p_activity_tbl != NULL) {
15565                 emstorage_free_local_activity(&p_activity_tbl, count, NULL);
15566         }
15567
15568
15569         if (err_code != NULL)
15570                 *err_code = error;
15571
15572         EM_DEBUG_FUNC_END("ret [%d]", ret);
15573         return ret;
15574 }
15575
15576
15577 INTERNAL_FUNC int emstorage_get_next_activity_id(int *activity_id, int *err_code)
15578 {
15579
15580         EM_DEBUG_FUNC_BEGIN();
15581
15582         int ret = false;
15583         int err = EMAIL_ERROR_NONE;
15584         int rc = -1;
15585         char *sql = NULL;
15586         char **result = NULL;
15587
15588         if (NULL == activity_id) {
15589                 EM_DEBUG_EXCEPTION(" activity_id[%p]", activity_id);
15590                 if (err_code)
15591                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15592                 return false;
15593         }
15594
15595         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15596
15597         /*  increase unique id */
15598
15599         sql = "SELECT max(rowid) FROM mail_local_activity_tbl;";
15600
15601         /*  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); */
15602         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15603                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
15604
15605         if (NULL == result[1])
15606                 rc = 1;
15607         else
15608                 rc = atoi(result[1])+1;
15609
15610         *activity_id = rc;
15611
15612         if (result)
15613                 sqlite3_free_table(result);
15614
15615         ret = true;
15616
15617 FINISH_OFF:
15618
15619         if (err_code)
15620                 *err_code = err;
15621
15622         EM_DEBUG_FUNC_END("ret [%d]", ret);
15623         return ret;
15624
15625 }
15626
15627 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)
15628 {
15629
15630         EM_DEBUG_FUNC_BEGIN();
15631
15632         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);
15633
15634         if (account_id <= 0 || NULL == activity_id_list || NULL == activity_id_count || lowest_activity_type <= 0 || highest_activity_type <= 0) {
15635                 if (err_code != NULL)
15636                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15637                 return false;
15638         }
15639
15640         int ret = false;
15641         int error = EMAIL_ERROR_NONE;
15642         int i = 0, rc = -1, count = 0;
15643         char sql_query_string[1024] = {0x00, };
15644         int *activity_ids = NULL;
15645         int col_index = 0;
15646         char **result = NULL;
15647         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15648         EMSTORAGE_START_READ_TRANSACTION(transaction);
15649
15650         memset(sql_query_string, 0x00, sizeof(sql_query_string));
15651
15652         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);
15653
15654         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
15655
15656
15657         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15658         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15659                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15660
15661         col_index = 1;
15662
15663         EM_DEBUG_LOG(" Activity COUNT : %d ... ", count);
15664
15665         if (NULL == (activity_ids = (int *)em_malloc(sizeof(int) * count))) {
15666                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
15667                 error = EMAIL_ERROR_OUT_OF_MEMORY;
15668                 goto FINISH_OFF;
15669         }
15670
15671         for (i = 0; i < count; i++) {
15672                 activity_ids[i] = atoi(result[col_index]);
15673                 col_index++;
15674                 EM_DEBUG_LOG("activity_id %d", activity_ids[i]);
15675         }
15676
15677         ret = true;
15678
15679 FINISH_OFF:
15680
15681
15682         if (ret == true) {
15683                 *activity_id_count = count;
15684                 *activity_id_list = activity_ids;
15685         } else if (activity_ids != NULL) /* Prevent defect - 216566 */
15686                 EM_SAFE_FREE(activity_ids);
15687
15688
15689         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15690         if (err_code != NULL)
15691                 *err_code = error;
15692
15693         EM_DEBUG_FUNC_END("ret [%d]", ret);
15694         return ret;
15695 }
15696
15697 INTERNAL_FUNC int emstorage_free_activity_id_list(int *activity_id_list, int *error_code)
15698 {
15699         EM_DEBUG_FUNC_BEGIN();
15700
15701         int error = EMAIL_ERROR_NONE;
15702         int ret = false;
15703
15704         EM_DEBUG_LOG(" activity_id_list [%p]", activity_id_list);
15705
15706         if (NULL == activity_id_list) {
15707                 error = EMAIL_ERROR_INVALID_PARAM;
15708                 goto FINISH_OFF;
15709         } else {
15710                 EM_SAFE_FREE(activity_id_list);
15711         }
15712
15713
15714         ret = true;
15715
15716 FINISH_OFF:
15717
15718         if (NULL != error_code)
15719                 *error_code = error;
15720
15721         EM_DEBUG_FUNC_END("ret [%d]", ret);
15722         return ret;
15723 }
15724
15725 /**
15726  * emstorage_delete_local_activity - Deletes the Local acitivity Generated based on activity_type
15727  * or based on server mail id
15728  *
15729  */
15730 INTERNAL_FUNC int emstorage_delete_local_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15731 {
15732         EM_DEBUG_FUNC_BEGIN();
15733
15734
15735         EM_DEBUG_LOG(" local_activity[%p] ", local_activity);
15736
15737         if (!local_activity) {
15738                 EM_DEBUG_EXCEPTION(" local_activity[%p] ", local_activity);
15739                 if (err_code != NULL)
15740                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15741                 return false;
15742         }
15743
15744         int rc = -1, ret = false;                       /* Prevent_FIX  */
15745         int err = EMAIL_ERROR_NONE;
15746         int query_and = 0;
15747         int query_where = 0;
15748         char sql_query_string[8192] = { 0x00, };
15749         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15750         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15751
15752         memset(sql_query_string, 0x00, sizeof(sql_query_string));
15753
15754         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_local_activity_tbl ");
15755
15756         EM_DEBUG_LOG_SEC(">>> Query [ %s ] ", sql_query_string);
15757
15758         if (local_activity->account_id) {
15759                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15760                                 " WHERE account_id = %d ", local_activity->account_id);
15761                 query_and = 1;
15762                 query_where = 1;
15763         }
15764
15765         if (local_activity->server_mailid) {
15766                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15767                                 " %s %s server_mailid = '%s' ", query_where ? "" : "WHERE", query_and ? "AND" : "", local_activity->server_mailid);
15768                 query_and = 1;
15769                 query_where = 1;
15770         }
15771
15772
15773         if (local_activity->mail_id) {
15774                 EM_DEBUG_LOG(">>>> MAIL ID [ %d ] , ACTIVITY TYPE [%d ]", local_activity->mail_id, local_activity->activity_type);
15775
15776                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15777                                 " %s %s mail_id = %d  ", query_where ? "" : "WHERE", query_and ? "AND" : "", local_activity->mail_id);
15778
15779                 query_and = 1;
15780                 query_where = 1;
15781
15782         }
15783
15784         if (local_activity->activity_type > 0) {
15785                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15786                                 " %s %s activity_type = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "" , local_activity->activity_type);
15787         }
15788
15789         if (local_activity->activity_id > 0) {
15790                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15791                                 " %s %s activity_id = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "" , local_activity->activity_id);
15792
15793         }
15794
15795         EM_DEBUG_LOG_SEC(">>>>> Query [ %s ] ", sql_query_string);
15796         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15797         if (error != EMAIL_ERROR_NONE) {
15798                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
15799                 goto FINISH_OFF;
15800         }
15801
15802         rc = sqlite3_changes(local_db_handle);
15803         if (rc == 0) {
15804                 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
15805                 err = EMAIL_ERROR_MAILBOX_NOT_FOUND;
15806         }
15807
15808         ret = true;
15809
15810 FINISH_OFF:
15811         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15812
15813         if (hStmt != NULL) {
15814                 rc = sqlite3_finalize(hStmt);
15815                 if (rc != SQLITE_OK) {
15816                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15817                         error = EMAIL_ERROR_DB_FAILURE;
15818                 }
15819         }
15820
15821         if (err_code != NULL)
15822                 *err_code = err;
15823         EM_DEBUG_FUNC_END("ret [%d]", ret);
15824         return ret;
15825 }
15826
15827 /**
15828  *      emstorage_free_local_activity - Free the Local Activity data
15829  */
15830 INTERNAL_FUNC int emstorage_free_local_activity(emstorage_activity_tbl_t **local_activity_list, int count, int *err_code)
15831 {
15832         EM_DEBUG_FUNC_BEGIN();
15833
15834         EM_DEBUG_LOG(" local_activity_list[%p], count[%d], err_code[%p]", local_activity_list, count, err_code);
15835
15836         int ret = false;
15837         int error = EMAIL_ERROR_INVALID_PARAM;
15838
15839         if (count > 0) {
15840                 if (!local_activity_list || !*local_activity_list) {
15841                         EM_DEBUG_EXCEPTION(" local_activity_list[%p], count[%d]", local_activity_list, count);
15842
15843                         error = EMAIL_ERROR_INVALID_PARAM;
15844                         goto FINISH_OFF;
15845                 }
15846
15847                 emstorage_activity_tbl_t* p = *local_activity_list;
15848                 int i = 0;
15849                 if (p) {
15850                         for (; i < count; i++) {
15851                                 EM_SAFE_FREE(p[i].dest_mbox);
15852                                 EM_SAFE_FREE(p[i].src_mbox);
15853                                 EM_SAFE_FREE(p[i].server_mailid);
15854                         }
15855
15856                         EM_SAFE_FREE(p);
15857                         *local_activity_list = NULL;
15858                 }
15859         }
15860
15861         ret = true;
15862
15863 FINISH_OFF:
15864
15865         if (err_code != NULL)
15866                 *err_code = error;
15867
15868         EM_DEBUG_FUNC_END("ret [%d]", ret);
15869         return ret;
15870
15871 }
15872 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
15873
15874
15875 static int _get_key_value_string_for_list_filter_rule(email_list_filter_rule_t *input_list_filter_rule, char **output_key_value_string)
15876 {
15877         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_key_value_string [%p]", input_list_filter_rule, output_key_value_string);
15878
15879         int  ret = EMAIL_ERROR_NONE;
15880         char key_value_string[QUERY_SIZE] = { 0, };
15881         char *temp_key_value_1 = NULL;
15882         char *temp_key_value_2 = NULL;
15883
15884         if (input_list_filter_rule == NULL || output_key_value_string == NULL) {
15885                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15886                 return EMAIL_ERROR_INVALID_PARAM;
15887         }
15888
15889         switch (input_list_filter_rule->target_attribute) {
15890         case EMAIL_MAIL_ATTRIBUTE_MAIL_ID:
15891         case EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID:
15892         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID:
15893         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE:
15894         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_STATUS:
15895         case EMAIL_MAIL_ATTRIBUTE_REFERENCE_MAIL_ID:
15896         case EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS:
15897         case EMAIL_MAIL_ATTRIBUTE_MAIL_SIZE:
15898         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN:
15899         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML:
15900         case EMAIL_MAIL_ATTRIBUTE_FILE_SIZE:
15901         case EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD:
15902         case EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD:
15903         case EMAIL_MAIL_ATTRIBUTE_FLAGS_FLAGGED_FIELD:
15904         case EMAIL_MAIL_ATTRIBUTE_FLAGS_ANSWERED_FIELD:
15905         case EMAIL_MAIL_ATTRIBUTE_FLAGS_RECENT_FIELD:
15906         case EMAIL_MAIL_ATTRIBUTE_FLAGS_DRAFT_FIELD:
15907         case EMAIL_MAIL_ATTRIBUTE_FLAGS_FORWARDED_FIELD:
15908         case EMAIL_MAIL_ATTRIBUTE_DRM_STATUS:
15909         case EMAIL_MAIL_ATTRIBUTE_PRIORITY:
15910         case EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS:
15911         case EMAIL_MAIL_ATTRIBUTE_LOCK_STATUS:
15912         case EMAIL_MAIL_ATTRIBUTE_REPORT_STATUS:
15913         case EMAIL_MAIL_ATTRIBUTE_ATTACHMENT_COUNT:
15914         case EMAIL_MAIL_ATTRIBUTE_INLINE_CONTENT_COUNT:
15915         case EMAIL_MAIL_ATTRIBUTE_THREAD_ID:
15916         case EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT:
15917         case EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS:
15918         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_CLASS:
15919         case EMAIL_MAIL_ATTRIBUTE_DIGEST_TYPE:
15920         case EMAIL_MAIL_ATTRIBUTE_SMIME_TYPE:
15921         case EMAIL_MAIL_ATTRIBUTE_REMAINING_RESEND_TIMES:
15922         case EMAIL_MAIL_ATTRIBUTE_TAG_ID:
15923         case EMAIL_MAIL_ATTRIBUTE_EAS_DATA_LENGTH_TYPE:
15924                 SNPRINTF(key_value_string, QUERY_SIZE, "%d", input_list_filter_rule->key_value.integer_type_value);
15925                 break;
15926
15927         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
15928         case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
15929         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
15930         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
15931         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
15932         case EMAIL_MAIL_ATTRIBUTE_FROM:
15933         case EMAIL_MAIL_ATTRIBUTE_TO:
15934         case EMAIL_MAIL_ATTRIBUTE_CC:
15935         case EMAIL_MAIL_ATTRIBUTE_BCC:
15936         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
15937                 if (input_list_filter_rule->key_value.string_type_value == NULL) {
15938                         EM_DEBUG_EXCEPTION("Invalid string_type_value [%p]", input_list_filter_rule->key_value.string_type_value);
15939                         ret = EMAIL_ERROR_INVALID_PARAM;
15940                         goto FINISH_OFF;
15941                 }
15942
15943                 temp_key_value_1 = input_list_filter_rule->key_value.string_type_value;
15944
15945                 temp_key_value_2 = em_replace_all_string(temp_key_value_1, "_", "\\_");
15946                 temp_key_value_1 = em_replace_all_string(temp_key_value_2, "%", "\\%");
15947
15948                 if (input_list_filter_rule->rule_type == EMAIL_LIST_FILTER_RULE_INCLUDE)
15949                         SNPRINTF(key_value_string, QUERY_SIZE, "\'%%%s%%\'", temp_key_value_1);
15950                 else
15951                         SNPRINTF(key_value_string, QUERY_SIZE, "\'%s\'", temp_key_value_1);
15952                 break;
15953
15954         case EMAIL_MAIL_ATTRIBUTE_DATE_TIME:
15955         case EMAIL_MAIL_ATTRIBUTE_SCHEDULED_SENDING_TIME:
15956         case EMAIL_MAIL_ATTRIBUTE_REPLIED_TIME:
15957         case EMAIL_MAIL_ATTRIBUTE_FORWARDED_TIME:
15958                 SNPRINTF(key_value_string, QUERY_SIZE, "%d", (int)input_list_filter_rule->key_value.datetime_type_value);
15959                 break;
15960
15961         default:
15962                 ret = EMAIL_ERROR_INVALID_PARAM;
15963                 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
15964                 break;
15965         }
15966
15967         if (ret == EMAIL_ERROR_NONE && EM_SAFE_STRLEN(key_value_string) > 0)
15968                 *output_key_value_string = strdup(key_value_string);
15969
15970 FINISH_OFF:
15971
15972         EM_SAFE_FREE(temp_key_value_1);
15973         EM_SAFE_FREE(temp_key_value_2);
15974
15975         EM_DEBUG_FUNC_END("ret [%d]", ret);
15976         return ret;
15977 }
15978
15979 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15980 static int _get_cert_password_file_name(int index, char *cert_password_file_name)
15981 {
15982         EM_DEBUG_FUNC_BEGIN("index : [%d]", index);
15983
15984         if (index <= 0 || !cert_password_file_name) {
15985                 EM_DEBUG_EXCEPTION("Invalid parameter");
15986                 return EMAIL_ERROR_INVALID_PARAM;
15987         }
15988
15989         sprintf(cert_password_file_name, ".email_cert_%d", index);
15990
15991         EM_DEBUG_FUNC_END();
15992         return EMAIL_ERROR_NONE;
15993 }
15994 #endif
15995
15996 static int _make_filter_rule_string(email_list_filter_rule_t *input_list_filter_rule, char **output_string)
15997 {
15998         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
15999
16000         int   ret = EMAIL_ERROR_NONE;
16001         int   is_alpha = 0;
16002         int   length_field_name = 0;
16003         int   length_value = 0;
16004         char  result_rule_string[QUERY_SIZE] = { 0 , };
16005         char *mod_field_name_string = NULL;
16006         char *mod_value_string = NULL;
16007         char *temp_field_name_string = NULL;
16008         char *temp_key_value_string = NULL;
16009
16010         if (input_list_filter_rule == NULL || output_string == NULL) {
16011                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16012                 return  EMAIL_ERROR_INVALID_PARAM;
16013         }
16014
16015         temp_field_name_string = emcore_get_mail_field_name_by_attribute_type(input_list_filter_rule->target_attribute);
16016
16017         if (temp_field_name_string == NULL) {
16018                 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
16019                 return EMAIL_ERROR_INVALID_PARAM;
16020         }
16021
16022         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) {
16023                 EM_DEBUG_EXCEPTION("_get_key_value_string_for_list_filter_rule failed");
16024                 return EMAIL_ERROR_INVALID_PARAM;
16025         }
16026
16027         length_field_name = EM_SAFE_STRLEN(temp_field_name_string);
16028         length_value      = EM_SAFE_STRLEN(temp_key_value_string);
16029
16030         switch (input_list_filter_rule->target_attribute) {
16031         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
16032         case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
16033         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
16034         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
16035         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
16036         case EMAIL_MAIL_ATTRIBUTE_FROM:
16037         case EMAIL_MAIL_ATTRIBUTE_TO:
16038         case EMAIL_MAIL_ATTRIBUTE_CC:
16039         case EMAIL_MAIL_ATTRIBUTE_BCC:
16040         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
16041                 is_alpha = 1;
16042                 break;
16043         default:
16044                 is_alpha = 0;
16045                 break;
16046         }
16047
16048         if (is_alpha == 1 && input_list_filter_rule->case_sensitivity == false) {
16049                 length_field_name += strlen("UPPER() ");
16050                 length_value      += strlen("UPPER() ");
16051                 mod_field_name_string = em_malloc(sizeof(char) * length_field_name);
16052                 if (mod_field_name_string == NULL) {
16053                         EM_DEBUG_EXCEPTION("em_mallocfailed");
16054                         EM_SAFE_FREE(temp_field_name_string);
16055                         ret = EMAIL_ERROR_OUT_OF_MEMORY;
16056                         goto FINISH_OFF;
16057                 }
16058
16059                 mod_value_string = em_malloc(sizeof(char) * length_value);
16060                 if (mod_value_string == NULL) {
16061                         EM_DEBUG_EXCEPTION("em_mallocfailed");
16062                         EM_SAFE_FREE(temp_field_name_string);
16063                         ret = EMAIL_ERROR_OUT_OF_MEMORY;
16064                         goto FINISH_OFF;
16065                 }
16066
16067                 SNPRINTF(mod_field_name_string, length_field_name, "UPPER(%s)", temp_field_name_string);
16068                 SNPRINTF(mod_value_string,      length_value, "UPPER(%s)", temp_key_value_string);
16069                 EM_SAFE_FREE(temp_key_value_string);
16070         } else {
16071                 mod_field_name_string = strdup(temp_field_name_string);
16072                 mod_value_string      = temp_key_value_string;
16073         }
16074
16075         switch (input_list_filter_rule->rule_type) {
16076         case EMAIL_LIST_FILTER_RULE_EQUAL:
16077                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = %s ", mod_field_name_string, mod_value_string);
16078                 break;
16079         case EMAIL_LIST_FILTER_RULE_NOT_EQUAL:
16080                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s != %s ", mod_field_name_string, mod_value_string);
16081                 break;
16082         case EMAIL_LIST_FILTER_RULE_LESS_THAN:
16083                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s < %s ", mod_field_name_string, mod_value_string);
16084                 break;
16085         case EMAIL_LIST_FILTER_RULE_GREATER_THAN:
16086                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s > %s ", mod_field_name_string, mod_value_string);
16087                 break;
16088         case EMAIL_LIST_FILTER_RULE_LESS_THAN_OR_EQUAL:
16089                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s <= %s ", mod_field_name_string, mod_value_string);
16090                 break;
16091         case EMAIL_LIST_FILTER_RULE_GREATER_THAN_OR_EQUAL:
16092                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s >= %s ", mod_field_name_string, mod_value_string);
16093                 break;
16094         case EMAIL_LIST_FILTER_RULE_INCLUDE:
16095                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s LIKE %s ", mod_field_name_string, mod_value_string);
16096                 break;
16097         case EMAIL_LIST_FILTER_RULE_IN:
16098                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s IN (%s) ", mod_field_name_string, mod_value_string);
16099                 break;
16100         case EMAIL_LIST_FILTER_RULE_NOT_IN:
16101                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s NOT IN (%s) ", mod_field_name_string, mod_value_string);
16102                 break;
16103         default:
16104                 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16105                 ret = EMAIL_ERROR_INVALID_PARAM;
16106                 goto FINISH_OFF;
16107         }
16108
16109         *output_string = strdup(result_rule_string);
16110
16111 FINISH_OFF:
16112         EM_SAFE_FREE(mod_field_name_string);
16113         EM_SAFE_FREE(mod_value_string);
16114
16115         EM_DEBUG_FUNC_END("ret [%d]", ret);
16116         return ret;
16117 }
16118
16119 static int _make_filter_attach_rule_string(char *multi_user_name, email_list_filter_rule_attach_t *input_list_filter_rule, char **output_string)
16120 {
16121         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16122
16123         char *field_name_string = NULL;
16124         char  key_value_string[QUERY_SIZE] = {0,};
16125         char  result_rule_string[QUERY_SIZE] = {0,};
16126         int rc = -1;
16127         int count = 0;
16128         int query_size = 0;
16129         int cur_query = 0;
16130         int col_index = 0;
16131         int error = EMAIL_ERROR_NONE;
16132         char **result = NULL;
16133         char sql_query_string[QUERY_SIZE] = {0,};
16134         char *sql_query_string2 = NULL;
16135         sqlite3 *local_db_handle = NULL;
16136         int *mail_ids = NULL;
16137
16138         if (input_list_filter_rule == NULL || output_string == NULL) {
16139                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16140                 return  EMAIL_ERROR_INVALID_PARAM;
16141         }
16142
16143         field_name_string = EM_SAFE_STRDUP("attachment_name");
16144         SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16145
16146         switch (input_list_filter_rule->rule_type) {
16147
16148         case EMAIL_LIST_FILTER_RULE_INCLUDE:
16149                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16150                 break;
16151
16152         case EMAIL_LIST_FILTER_RULE_MATCH:
16153                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16154                 break;
16155
16156         default:
16157                 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16158                 error = EMAIL_ERROR_INVALID_PARAM;
16159                 goto FINISH_OFF;
16160         }
16161
16162         local_db_handle = emstorage_get_db_connection(multi_user_name);
16163
16164         EMSTORAGE_START_READ_TRANSACTION(true);
16165         SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_attachment_tbl %s", result_rule_string);
16166
16167         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16168         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16169                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16170
16171         col_index = 1;
16172
16173         if (!count) {
16174                 EM_DEBUG_LOG("No mail found...");
16175                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
16176                 *output_string = strdup("mail_id IN () ");
16177                 goto FINISH_OFF;
16178         }
16179
16180         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
16181         int i = 0;
16182         if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16183                 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16184                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16185                 goto FINISH_OFF;
16186         }
16187
16188         for (i = 0; i < count; i++) {
16189                 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16190                 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16191         }
16192
16193         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >>", count);
16194         sqlite3_free_table(result);
16195         EMSTORAGE_FINISH_READ_TRANSACTION(true);
16196
16197         //      sqlite3_db_release_memory(local_db_handle);
16198
16199
16200         query_size = (10 * count) + strlen("mail_id IN ()  ");
16201
16202         sql_query_string2 = em_malloc(query_size);
16203         if (sql_query_string2 == NULL) {
16204                 EM_DEBUG_EXCEPTION("em_mallocfailed");
16205                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16206                 goto FINISH_OFF;
16207         }
16208
16209         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN (");
16210         for (i = 0; i < count-1; i++)
16211                 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16212
16213         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d) ", mail_ids[count-1]);
16214
16215         *output_string = strdup(sql_query_string2);
16216 FINISH_OFF:
16217
16218         EM_SAFE_FREE(mail_ids); /* prevent */
16219         EM_SAFE_FREE(sql_query_string2);
16220         EM_SAFE_FREE(field_name_string);
16221         EM_DEBUG_FUNC_END("error [%d]", error);
16222         return error;
16223 }
16224
16225 static int _make_filter_fts_rule_string(char *multi_user_name, email_list_filter_rule_fts_t *input_list_filter_rule, char **output_string)
16226 {
16227         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16228         char *field_name_string = NULL;
16229         char key_value_string[QUERY_SIZE] = {0,};
16230         char  result_rule_string[QUERY_SIZE] = {0,};
16231         int rc = -1;
16232         int count = 0;
16233         int col_index = 0;
16234         int query_size = 0;
16235         int error = EMAIL_ERROR_NONE;
16236         char **result = NULL;
16237         char sql_query_string[QUERY_SIZE] = {0,};
16238         char *sql_query_string2 = NULL;
16239         sqlite3 *local_db_handle = NULL;
16240         int *mail_ids = NULL;
16241
16242         if (input_list_filter_rule == NULL || output_string == NULL) {
16243                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16244                 return  EMAIL_ERROR_INVALID_PARAM;
16245         }
16246
16247         field_name_string = EM_SAFE_STRDUP("body_text");
16248         SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16249
16250         switch (input_list_filter_rule->rule_type) {
16251
16252         case EMAIL_LIST_FILTER_RULE_INCLUDE:
16253                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16254                 break;
16255
16256         case EMAIL_LIST_FILTER_RULE_MATCH:
16257                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16258                 break;
16259
16260         default:
16261                 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16262                 error = EMAIL_ERROR_INVALID_PARAM;
16263                 goto FINISH_OFF;
16264         }
16265
16266         local_db_handle = emstorage_get_db_connection(multi_user_name);
16267
16268         EMSTORAGE_START_READ_TRANSACTION(true);
16269         SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_text_tbl %s", result_rule_string);
16270
16271         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16272         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16273                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16274
16275         col_index = 1;
16276
16277         if (!count) {
16278                 EM_DEBUG_LOG("No mail found...");
16279                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
16280                 *output_string = strdup("mail_id IN () ");
16281                 goto FINISH_OFF;
16282         }
16283
16284         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
16285         int i = 0;
16286
16287         if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16288                 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16289                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16290                 goto FINISH_OFF;
16291         }
16292
16293         for (i = 0; i < count; i++) {
16294                 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16295                 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16296         }
16297
16298         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >>", count);
16299         sqlite3_free_table(result);
16300         EMSTORAGE_FINISH_READ_TRANSACTION(true);
16301
16302         //      sqlite3_db_release_memory(local_db_handle);
16303
16304
16305         query_size = (10 * count) + strlen("mail_id IN ()  ");
16306         sql_query_string2 = em_malloc(query_size);
16307         if (sql_query_string2 == NULL) {
16308                 EM_DEBUG_EXCEPTION("em_mallocfailed");
16309                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16310                 goto FINISH_OFF;
16311         }
16312         int cur_query = 0;
16313         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN (");
16314         for (i = 0; i < count-1; i++)
16315                 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16316
16317         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d) ", mail_ids[count-1]);
16318
16319         *output_string = strdup(sql_query_string2);
16320
16321 FINISH_OFF:
16322         EM_SAFE_FREE(mail_ids); /* prevent */
16323         EM_SAFE_FREE(sql_query_string2);
16324         EM_SAFE_FREE(field_name_string);
16325         EM_DEBUG_FUNC_END("error [%d]", error);
16326         return error;
16327 }
16328
16329 static int _make_order_rule_string(char *multi_user_name, email_list_sorting_rule_t *input_sorting_rule, char **output_string)
16330 {
16331         EM_DEBUG_FUNC_BEGIN("input_sorting_rule [%p], output_string [%p]", input_sorting_rule, output_string);
16332
16333         char  result_rule_string[QUERY_SIZE] = { 0 , };
16334         int   ret = EMAIL_ERROR_NONE;
16335
16336         emstorage_account_tbl_t *account_tbl_array = NULL;
16337         int count = 0;
16338         int i = 0;
16339         char *result_str = NULL;
16340         char *tmp_str1 = NULL;
16341         char *tmp_str2 = NULL;
16342         char query_per_account[QUERY_SIZE] = { 0 , };
16343
16344         if (input_sorting_rule->force_boolean_check) {
16345                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = 0 ",
16346                         emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16347         } else {
16348                 EM_SAFE_STRNCPY(result_rule_string,
16349                         emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16350                         QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16351         }
16352
16353         switch (input_sorting_rule->sort_order) {
16354         case EMAIL_SORT_ORDER_ASCEND:
16355                 EM_SAFE_STRNCAT(result_rule_string, " ASC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16356                 break;
16357
16358         case EMAIL_SORT_ORDER_DESCEND:
16359                 EM_SAFE_STRNCAT(result_rule_string, " DESC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16360                 break;
16361
16362         case EMAIL_SORT_ORDER_NOCASE_ASCEND:
16363                 EM_SAFE_STRNCAT(result_rule_string, " COLLATE NOCASE ASC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16364                 break;
16365
16366         case EMAIL_SORT_ORDER_NOCASE_DESCEND:
16367                 EM_SAFE_STRNCAT(result_rule_string, " COLLATE NOCASE DESC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16368                 break;
16369
16370         case EMAIL_SORT_ORDER_TO_CCBCC:
16371                 memset(result_rule_string, 0, QUERY_SIZE);
16372                 if (input_sorting_rule->key_value.string_type_value)
16373                         sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16374                                         " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 ELSE 2 END ",
16375                                         input_sorting_rule->key_value.string_type_value);
16376                 break;
16377
16378         case EMAIL_SORT_ORDER_TO_CC_BCC:
16379                 memset(result_rule_string, 0, QUERY_SIZE);
16380                 if (input_sorting_rule->key_value.string_type_value)
16381                         sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16382                                         " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 WHEN full_address_cc LIKE \'%%%q%%\' THEN 2 ELSE 3 END ",
16383                                         input_sorting_rule->key_value.string_type_value, input_sorting_rule->key_value.string_type_value);
16384                 break;
16385
16386         case EMAIL_SORT_ORDER_TO_CCBCC_ALL:
16387                 if (!emstorage_get_account_list(multi_user_name, &count, &account_tbl_array, true, false, NULL)) {
16388                         EM_DEBUG_EXCEPTION("emstorage_get_account_list failed");
16389                         goto FINISH_OFF;
16390                 }
16391
16392                 if (!count) {
16393                         EM_DEBUG_LOG("No account exist");
16394                         ret = EMAIL_ERROR_INVALID_PARAM;
16395                         goto FINISH_OFF;
16396                 }
16397
16398                 for (i = 0; i < count; i++) {
16399                         if (i > 0 && result_str) {
16400                                 tmp_str2 = result_str;
16401                                 result_str = g_strconcat(tmp_str2, " OR ", NULL);
16402                                 EM_SAFE_FREE(tmp_str2);
16403                         }
16404
16405                         memset(query_per_account, 0, QUERY_SIZE);
16406                         snprintf(query_per_account, QUERY_SIZE,
16407                                         "(account_id = %d AND full_address_to LIKE \'%%%s%%\')",
16408                                         account_tbl_array[i].account_id, account_tbl_array[i].user_email_address);
16409
16410                         tmp_str1 = result_str;
16411                         if (tmp_str1)
16412                                 result_str = g_strconcat(tmp_str1, query_per_account, NULL);
16413                         else
16414                                 result_str = g_strdup(query_per_account);
16415                         EM_SAFE_FREE(tmp_str1);
16416                 }
16417
16418                 snprintf(result_rule_string, QUERY_SIZE,
16419                                 " CASE WHEN %s THEN 1 ELSE 2 END ", result_str);
16420
16421                 EM_SAFE_FREE(result_str);
16422                 if (account_tbl_array)
16423                         emstorage_free_account(&account_tbl_array, count, NULL);
16424                 break;
16425
16426         case EMAIL_SORT_ORDER_LOCALIZE_ASCEND:
16427                 memset(result_rule_string, 0, QUERY_SIZE);
16428                 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16429                                 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END ASC, %s COLLATE NOCASE ASC ",
16430                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16431                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16432                 break;
16433
16434         case EMAIL_SORT_ORDER_LOCALIZE_DESCEND:
16435                 memset(result_rule_string, 0, QUERY_SIZE);
16436                 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16437                                 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END DESC, %s COLLATE NOCASE DESC ",
16438                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16439                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16440                 break;
16441
16442         default:
16443                 EM_DEBUG_EXCEPTION("Invalid sort_order [%d]", input_sorting_rule->sort_order);
16444                 ret = EMAIL_ERROR_INVALID_PARAM;
16445                 goto FINISH_OFF;
16446         }
16447
16448         *output_string = strdup(result_rule_string);
16449
16450 FINISH_OFF:
16451         EM_DEBUG_FUNC_END("ret [%d]", ret);
16452         return ret;
16453 }
16454
16455 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)
16456 {
16457         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);
16458         int ret = EMAIL_ERROR_NONE;
16459         int i = 0;
16460         int string_offset = 0;
16461         int query_size = 0;
16462         int new_query_size = 0;
16463         char *conditional_clause_string = NULL;
16464         char *result_string_for_a_item = NULL;
16465
16466         if ((input_filter_count > 0 && !input_filter_list) || (input_sorting_rule_count > 0 && !input_sorting_rule_list) || output_conditional_clause == NULL) {
16467                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16468                 return EMAIL_ERROR_INVALID_PARAM;
16469         }
16470
16471         conditional_clause_string = em_malloc(QUERY_SIZE);
16472         if (conditional_clause_string == NULL) {
16473                 EM_DEBUG_EXCEPTION("Memory is full");
16474                 return EMAIL_ERROR_OUT_OF_MEMORY;
16475         }
16476
16477         if (input_filter_count > 0) {
16478                 query_size = QUERY_SIZE;
16479                 g_strlcpy(conditional_clause_string, " WHERE ", QUERY_SIZE);
16480
16481                 for (i = 0; i < input_filter_count; i++) {
16482                         switch (input_filter_list[i].list_filter_item_type) {
16483                         case EMAIL_LIST_FILTER_ITEM_RULE:
16484                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE", i);
16485                                 _make_filter_rule_string(&(input_filter_list[i].list_filter_item.rule), &result_string_for_a_item);
16486                                 break;
16487
16488                         case EMAIL_LIST_FILTER_ITEM_RULE_FTS:
16489                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_FTS", i);
16490                                 _make_filter_fts_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_fts), &result_string_for_a_item);
16491                                 break;
16492
16493                         case EMAIL_LIST_FILTER_ITEM_RULE_ATTACH:
16494                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_ATTACH", i);
16495                                 _make_filter_attach_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_attach), &result_string_for_a_item);
16496                                 break;
16497
16498                         case EMAIL_LIST_FILTER_ITEM_OPERATOR:
16499                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_OPERATOR", i);
16500                                 switch (input_filter_list[i].list_filter_item.operator_type) {
16501                                 case EMAIL_LIST_FILTER_OPERATOR_AND:
16502                                         result_string_for_a_item = strdup("AND ");
16503                                         break;
16504                                 case EMAIL_LIST_FILTER_OPERATOR_OR:
16505                                         result_string_for_a_item = strdup("OR ");
16506                                         break;
16507                                 case EMAIL_LIST_FILTER_OPERATOR_LEFT_PARENTHESIS:
16508                                         result_string_for_a_item = strdup(" (");
16509                                         break;
16510                                 case EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS:
16511                                         result_string_for_a_item = strdup(") ");
16512                                         break;
16513                                 }
16514                                 break;
16515
16516                         default:
16517                                 EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", input_filter_list[i].list_filter_item_type);
16518                                 ret = EMAIL_ERROR_INVALID_PARAM;
16519                                 goto FINISH_OFF;
16520                         }
16521
16522                         if (result_string_for_a_item == NULL) {
16523                                 EM_DEBUG_EXCEPTION("result_string_for_a_item is null");
16524                                 ret = EMAIL_ERROR_INVALID_PARAM;
16525                                 goto FINISH_OFF;
16526                         }
16527
16528                         if (strlen(conditional_clause_string) + EM_SAFE_STRLEN(result_string_for_a_item) >= query_size) { /* prevent 34364 */
16529                                 EM_DEBUG_LOG("QUERY is too long");
16530                                 new_query_size = EM_SAFE_STRLEN(result_string_for_a_item) + EM_SAFE_STRLEN(conditional_clause_string) + QUERY_SIZE;
16531                                 conditional_clause_string = realloc(conditional_clause_string, new_query_size);
16532                                 if (conditional_clause_string == NULL) {
16533                                         EM_DEBUG_EXCEPTION("realloc failed");
16534                                         ret = EMAIL_ERROR_OUT_OF_MEMORY;
16535                                         goto FINISH_OFF;
16536                                 }
16537
16538                                 query_size = new_query_size;
16539                         }
16540
16541                         EM_SAFE_STRNCAT(conditional_clause_string, result_string_for_a_item , QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16542                         EM_SAFE_FREE(result_string_for_a_item);
16543                 }
16544         }
16545
16546         if (input_sorting_rule_count > 0) {
16547                 EM_SAFE_STRNCAT(conditional_clause_string, "ORDER BY ", QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16548
16549                 for (i = 0; i < input_sorting_rule_count; i++) {
16550                         if ((ret = _make_order_rule_string(multi_user_name, &input_sorting_rule_list[i], &result_string_for_a_item)) != EMAIL_ERROR_NONE) {
16551                                 EM_DEBUG_EXCEPTION("_make_order_rule_string failed. [%d]", ret);
16552                                 goto FINISH_OFF;
16553                         }
16554                         if (i > 0)
16555                                 EM_SAFE_STRNCAT(conditional_clause_string, ", " , QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16556                         EM_SAFE_STRNCAT(conditional_clause_string, result_string_for_a_item , QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16557                         EM_SAFE_FREE(result_string_for_a_item);
16558                 }
16559         }
16560
16561         if (input_start_index != -1 && input_limit_count != -1) {
16562                 string_offset = strlen(conditional_clause_string);
16563                 SNPRINTF_OFFSET(conditional_clause_string, string_offset, query_size, " LIMIT %d, %d", input_start_index, input_limit_count);
16564         }
16565
16566         *output_conditional_clause = strdup(conditional_clause_string);
16567
16568 FINISH_OFF:
16569         EM_SAFE_FREE(result_string_for_a_item);
16570         EM_SAFE_FREE(conditional_clause_string);
16571
16572         EM_DEBUG_FUNC_END("ret [%d]", ret);
16573         return ret;
16574 }
16575
16576 INTERNAL_FUNC int emstorage_free_list_filter(email_list_filter_t **input_filter_list, int input_filter_count)
16577 {
16578         EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d]", input_filter_list, input_filter_count);
16579         int err = EMAIL_ERROR_NONE;
16580         int i = 0;
16581         email_list_filter_t *temp_filter_list = NULL;
16582
16583         EM_IF_NULL_RETURN_VALUE(input_filter_list, EMAIL_ERROR_INVALID_PARAM);
16584
16585         for (i = 0; i < input_filter_count; i++) {
16586                 temp_filter_list = (*input_filter_list) + i;
16587                 if (!temp_filter_list)
16588                         continue;
16589
16590                 if (temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE) {
16591                         switch (temp_filter_list->list_filter_item.rule.target_attribute) {
16592                         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
16593                         case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
16594                         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
16595                         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
16596                         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
16597                         case EMAIL_MAIL_ATTRIBUTE_FROM:
16598                         case EMAIL_MAIL_ATTRIBUTE_TO:
16599                         case EMAIL_MAIL_ATTRIBUTE_CC:
16600                         case EMAIL_MAIL_ATTRIBUTE_BCC:
16601                         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN:
16602                         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML:
16603                         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
16604                                 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule.key_value.string_type_value);
16605                                 break;
16606                         default:
16607                                 break;
16608                         }
16609                 } 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) {
16610                         EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_fts.key_value.string_type_value);
16611                 } 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) {
16612                         EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_attach.key_value.string_type_value);
16613                 }
16614         }
16615
16616         EM_SAFE_FREE(*input_filter_list);
16617
16618         EM_DEBUG_FUNC_END("err [%d]", err);
16619         return err;
16620 }
16621
16622 /* Tasks --------------------------------------------------------------------------*/
16623 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)
16624 {
16625         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);
16626         int ret = 0;
16627         int i = 0;
16628         int task_id = 0;
16629         int err = EMAIL_ERROR_NONE;
16630         int rc = -1;
16631         DB_STMT hStmt = NULL;
16632         char sql_query_string[QUERY_SIZE] = {0, };
16633         sqlite3 *local_db_handle = NULL;
16634         char *sql = "SELECT max(rowid) FROM mail_task_tbl;";
16635         char **result = NULL;
16636
16637         if (input_task_parameter == NULL || output_task_id == NULL) {
16638                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16639                 return EMAIL_ERROR_INVALID_PARAM;
16640         }
16641
16642         local_db_handle = emstorage_get_db_connection(multi_user_name);
16643         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, err);
16644
16645         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
16646         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16647                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
16648
16649         if (NULL == result[1])
16650                 task_id = 1;
16651         else
16652                 task_id = atoi(result[1])+1;
16653
16654         *output_task_id = task_id;
16655
16656         sqlite3_free_table(result);
16657         result = NULL;
16658
16659         SNPRINTF(sql_query_string, sizeof(sql_query_string),
16660                         "INSERT INTO mail_task_tbl VALUES "
16661                         "(        "
16662                         "    ? "  /*   task_id */
16663                         "  , ? "  /*   task_type */
16664                         "  , ? "  /*   task_status */
16665                         "  , ? "  /*   task_priority */
16666                         "  , ? "  /*   task_parameter_length */
16667                         "  , ? "  /*   task_parameter */
16668                         "  , ? "  /*   date_time */
16669                         ") ");
16670
16671         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16672         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16673                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16674
16675         EM_DEBUG_LOG_SEC(">>>> SQL STMT [%s] ", sql_query_string);
16676
16677
16678         _bind_stmt_field_data_int(hStmt, i++, task_id);
16679         _bind_stmt_field_data_int(hStmt, i++, input_task_type);
16680         _bind_stmt_field_data_int(hStmt, i++, EMAIL_TASK_STATUS_WAIT);
16681         _bind_stmt_field_data_int(hStmt, i++, input_task_priority);
16682         _bind_stmt_field_data_int(hStmt, i++, input_task_parameter_length);
16683         _bind_stmt_field_data_blob(hStmt, i++, input_task_parameter, input_task_parameter_length);
16684         _bind_stmt_field_data_int(hStmt, i++, time(NULL));
16685
16686         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16687
16688         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {err = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
16689                         ("sqlite3_step fail:%d", rc));
16690         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16691                         ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
16692
16693         ret = (err == EMAIL_ERROR_NONE);
16694
16695 FINISH_OFF:
16696         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, err);
16697
16698         if (hStmt != NULL) {
16699                 rc = sqlite3_finalize(hStmt);
16700                 if (rc != SQLITE_OK) {
16701                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16702                         err = EMAIL_ERROR_DB_FAILURE;
16703                 }
16704         }
16705
16706         EM_DEBUG_FUNC_END("err [%d]", err);
16707         return err;
16708 }
16709
16710 INTERNAL_FUNC int emstorage_delete_task(char *multi_user_name, int task_id, int transaction)
16711 {
16712         EM_DEBUG_FUNC_BEGIN("task_id[%d], transaction[%d]", task_id, transaction);
16713         int ret = false;
16714         int err = EMAIL_ERROR_NONE;
16715         char sql_query_string[QUERY_SIZE] = {0, };
16716
16717         sqlite3 *local_db_handle = NULL;
16718
16719         if (task_id < 0) {
16720                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16721                 return EMAIL_ERROR_INVALID_PARAM;
16722         }
16723
16724         local_db_handle = emstorage_get_db_connection(multi_user_name);
16725
16726         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
16727
16728         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_task_tbl WHERE task_id = %d", task_id);
16729         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16730         if (err != EMAIL_ERROR_NONE) {
16731                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16732                 goto FINISH_OFF;
16733         }
16734
16735         ret = true;
16736
16737 FINISH_OFF:
16738         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
16739
16740         EM_DEBUG_FUNC_END("err [%d]", err);
16741         return err;
16742 }
16743
16744 INTERNAL_FUNC int emstorage_update_task_status(char *multi_user_name, int task_id, email_task_status_type_t task_status, int transaction)
16745 {
16746         EM_DEBUG_FUNC_BEGIN("task_id[%d] task_status[%d] transaction[%d]", task_id, task_status, transaction);
16747         int ret = false;
16748         int err = EMAIL_ERROR_NONE;
16749         char sql_query_string[QUERY_SIZE] = {0, };
16750
16751         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16752         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
16753
16754         SNPRINTF(sql_query_string, sizeof(sql_query_string),
16755                         "UPDATE mail_task_tbl SET"
16756                         " task_status = %d"
16757                         " WHERE task_id = %d"
16758                         , task_status
16759                         , task_id);
16760         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16761         if (err != EMAIL_ERROR_NONE) {
16762                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16763                 goto FINISH_OFF;
16764         }
16765
16766         ret = true;
16767
16768 FINISH_OFF:
16769         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
16770
16771         EM_DEBUG_FUNC_END("err [%d]", err);
16772         return err;
16773 }
16774
16775 INTERNAL_FUNC int emstorage_query_task(char *multi_user_name,
16776                 const char *input_conditional_clause,
16777                 const char *input_ordering_clause,
16778                 email_task_t **output_task_list,
16779                 int *output_task_count)
16780 {
16781         EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_ordering_clause [%p], "
16782                         "output_task_list[%p], output_task_count[%d]",
16783                         input_conditional_clause, input_ordering_clause, output_task_list, output_task_count);
16784         int i = 0, count = 0, rc = -1;
16785         int cur_query = 0;
16786         int field_index = 0;
16787         int err = EMAIL_ERROR_NONE;
16788         email_task_t *task_item_from_tbl = NULL;
16789         char sql_query_string[QUERY_SIZE] = {0, };
16790         char *field_list = "task_id, task_type, task_status, task_priority, task_parameter_length, task_parameter ";
16791         char **result;
16792         sqlite3 *local_db_handle = NULL;
16793         DB_STMT hStmt = NULL;
16794
16795         EM_IF_NULL_RETURN_VALUE(input_conditional_clause, false);
16796         EM_IF_NULL_RETURN_VALUE(output_task_count, false);
16797
16798         local_db_handle = emstorage_get_db_connection(multi_user_name);
16799
16800         SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
16801                         "SELECT COUNT(*) FROM mail_task_tbl %s", input_conditional_clause);
16802         EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
16803
16804         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
16805         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16806                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16807
16808         count = atoi(result[1]);
16809         sqlite3_free_table(result);
16810
16811         EM_DEBUG_LOG("count = %d", rc);
16812
16813         if (count == 0) {
16814                 EM_DEBUG_EXCEPTION("no task found...");
16815                 err = EMAIL_ERROR_TASK_NOT_FOUND;
16816                 goto FINISH_OFF;
16817         }
16818
16819         SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
16820                         "SELECT %s FROM mail_task_tbl %s %s", field_list, input_conditional_clause, input_ordering_clause);
16821         EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
16822
16823         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16824
16825         EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
16826         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16827                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16828
16829         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16830         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16831                         ("sqlite3_step fail:%d", rc));
16832
16833         if (rc == SQLITE_DONE) {
16834                 EM_DEBUG_EXCEPTION("no task found...");
16835                 err = EMAIL_ERROR_TASK_NOT_FOUND;
16836                 count = 0;
16837                 goto FINISH_OFF;
16838         }
16839
16840         if (!(task_item_from_tbl = (email_task_t*)em_malloc(sizeof(email_task_t) * count))) {
16841                 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
16842                 err = EMAIL_ERROR_OUT_OF_MEMORY;
16843                 goto FINISH_OFF;
16844         }
16845
16846         for (i = 0; i < count; i++) {
16847                 /*  get recordset */
16848                 field_index = 0;
16849
16850                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_id), field_index++);
16851                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_type), field_index++);
16852                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_status), field_index++);
16853                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_priority), field_index++);
16854                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_parameter_length), field_index++);
16855                 _get_stmt_field_data_blob(hStmt, (void**)&(task_item_from_tbl[i].task_parameter), field_index++);
16856
16857                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16858                 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i,  rc);
16859                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16860                                 ("sqlite3_step fail:%d", rc));
16861         }
16862
16863 FINISH_OFF:
16864
16865         if (err == EMAIL_ERROR_NONE && output_task_list) {
16866                 *output_task_list = task_item_from_tbl;
16867                 *output_task_count = count;
16868         } else {
16869                 if (task_item_from_tbl) {
16870                         for (i = 0; i < count; i++)
16871                                 EM_SAFE_FREE(task_item_from_tbl[i].task_parameter);
16872
16873                         free(task_item_from_tbl);
16874                 }
16875         }
16876
16877         if (hStmt != NULL) {
16878                 rc = sqlite3_finalize(hStmt);
16879                 hStmt = NULL;
16880                 if (rc != SQLITE_OK) {
16881                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16882                         err = EMAIL_ERROR_DB_FAILURE;
16883                 }
16884         }
16885
16886         EM_DEBUG_FUNC_END("err [%d]", err);
16887         return err;
16888 }
16889
16890 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)
16891 {
16892         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);
16893         int err = EMAIL_ERROR_NONE;
16894
16895         if (message_id == NULL) {
16896                 EM_DEBUG_EXCEPTION("Invalid parameter");
16897                 err = EMAIL_ERROR_INVALID_PARAM;
16898                 return err;
16899         }
16900
16901         int rc = -1;
16902         int count = 0;
16903         int temp_mail_id = 0;
16904         int where_pararaph_length = 0;
16905         char *where_pararaph = NULL;
16906         char sql_query_string[QUERY_SIZE] = {0, };
16907         char **result = NULL;
16908         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16909
16910         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id from mail_tbl ");
16911
16912         where_pararaph_length = EM_SAFE_STRLEN(message_id) + 100;
16913         where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
16914         if (where_pararaph == NULL) {
16915                 EM_DEBUG_EXCEPTION("em_mallocfailed");
16916                 err = EMAIL_ERROR_OUT_OF_MEMORY;
16917                 goto FINISH_OFF;
16918         }
16919
16920         if (account_id != ALL_ACCOUNT)
16921                 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);
16922         else
16923                 sqlite3_snprintf(where_pararaph_length, where_pararaph, "WHERE mailbox_type = %d AND message_id like '%q'", input_mailbox_type, message_id);
16924
16925         if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
16926                 EM_SAFE_STRNCAT(sql_query_string, where_pararaph , QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
16927
16928         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
16929
16930         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16931         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16932                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16933
16934
16935         EM_DEBUG_LOG("Count of mails [%d]", count);
16936
16937         if (count) {
16938                 _get_table_field_data_int(result, &temp_mail_id, 1);
16939                 EM_DEBUG_LOG("Searched mail_id [%d]", temp_mail_id);
16940
16941                 memset(sql_query_string, 0x00, QUERY_SIZE);
16942                 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string, "UPDATE mail_tbl set server_mail_id = '%q'", server_uid);
16943
16944                 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE) {
16945                         EM_SAFE_STRNCAT(sql_query_string, where_pararaph,
16946                                         QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
16947                 }
16948
16949                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16950                 if (err != EMAIL_ERROR_NONE) {
16951                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16952                         goto FINISH_OFF;
16953                 }
16954
16955         } else {
16956                 err = EMAIL_ERROR_MAIL_NOT_FOUND;
16957         }
16958
16959 FINISH_OFF:
16960
16961         sqlite3_free_table(result);
16962         result = NULL;
16963
16964
16965         EM_SAFE_FREE(where_pararaph);
16966
16967         if (mail_id != NULL)
16968                 *mail_id = temp_mail_id;
16969
16970         EM_DEBUG_FUNC_END("err : [%d]", err);
16971         return err;
16972 }
16973 /* Tasks --------------------------------------------------------------------------*/
16974
16975 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
16976 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)
16977 {
16978         EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
16979
16980         if (!local_activity || !activity_id) {
16981                 EM_DEBUG_EXCEPTION("local_activity[%p], activity_id[%p]", local_activity, activity_id);
16982                 if (err_code != NULL)
16983                         *err_code = EMAIL_ERROR_INVALID_PARAM;
16984                 return false;
16985         }
16986
16987         int rc = -1;
16988         int ret = false;
16989         int error = EMAIL_ERROR_NONE;
16990         int i = 0;
16991
16992         char sql_query_string[QUERY_SIZE] = {0, };
16993         DB_STMT hStmt = NULL;
16994
16995         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16996         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
16997
16998         memset(sql_query_string, 0x00, sizeof(sql_query_string));
16999         SNPRINTF(sql_query_string, sizeof(sql_query_string),
17000                         "INSERT INTO mail_auto_download_activity_tbl VALUES "
17001                         "("
17002                         "? "  /* Activity ID */
17003                         ",?"  /* Status */
17004                         ",?"  /* Account ID */
17005                         ",?"  /* Local Mail ID */
17006                         ",?"  /* Server mail ID */
17007                         ",?"  /* Mailbox ID*/
17008                         ",?"  /* Multi USER NAME */
17009                         ") ");
17010
17011         char *sql = "SELECT max(rowid) FROM mail_auto_download_activity_tbl;";
17012         char **result = NULL;
17013
17014
17015         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
17016
17017         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17018                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
17019
17020         if (NULL == result[1]) rc = 1;
17021         else rc = atoi(result[1])+1;
17022         sqlite3_free_table(result);
17023         result = NULL;
17024
17025         *activity_id = local_activity->activity_id = rc;
17026
17027         EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [%d], MAIL ID [%d], SERVER MAIL ID [%lu]",
17028                         local_activity->activity_id, local_activity->mail_id, local_activity->server_mail_id);
17029
17030         if (local_activity->mailbox_id)
17031                 EM_DEBUG_LOG(" MAILBOX ID [%d]", local_activity->mailbox_id);
17032
17033
17034         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17035         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17036                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17037
17038
17039         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
17040         _bind_stmt_field_data_int(hStmt, i++, local_activity->status);
17041         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
17042         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
17043         _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
17044         _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
17045         _bind_stmt_field_data_string(hStmt, i++, (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
17046
17047
17048         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17049
17050         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
17051                         ("sqlite3_step fail:%d", rc));
17052         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17053                         ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
17054
17055         ret = true;
17056
17057 FINISH_OFF:
17058         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17059         if (hStmt != NULL) {
17060                 rc = sqlite3_finalize(hStmt);
17061                 hStmt = NULL;
17062                 if (rc != SQLITE_OK) {
17063                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17064                         error = EMAIL_ERROR_DB_FAILURE;
17065                 }
17066         }
17067
17068         if (err_code != NULL)
17069                 *err_code = error;
17070
17071         EM_DEBUG_FUNC_END("ret [%d]", ret);
17072         return ret;
17073 }
17074
17075
17076 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)
17077 {
17078         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);
17079
17080         if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
17081                 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);
17082
17083                 if (err_code != NULL)
17084                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17085                 return false;
17086         }
17087
17088         int rc = -1;
17089         int ret = false;
17090         int error = EMAIL_ERROR_NONE;
17091         char sql_query_string[QUERY_SIZE] = {0, };
17092         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17093
17094         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17095         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17096
17097         if (activity_id == 0)
17098                 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);
17099         else
17100                 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);
17101
17102         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17103         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17104         if (error != EMAIL_ERROR_NONE) {
17105                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17106                 goto FINISH_OFF;
17107         }
17108
17109         /*  validate activity existence */
17110         rc = sqlite3_changes(local_db_handle);
17111         if (rc == 0) {
17112                 EM_DEBUG_EXCEPTION("No matching activity found");
17113                 error = EMAIL_ERROR_DATA_NOT_FOUND;
17114                 ret = true;
17115                 goto FINISH_OFF;
17116         }
17117
17118         ret = true;
17119
17120 FINISH_OFF:
17121         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17122
17123         if (err_code != NULL)
17124                 *err_code = error;
17125
17126         EM_DEBUG_FUNC_END("ret [%d]", ret);
17127         return ret;
17128 }
17129
17130 INTERNAL_FUNC int emstorage_delete_all_auto_download_activity(char *multi_user_name, int account_id, int transaction, int *err_code)
17131 {
17132         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17133
17134         if (account_id < FIRST_ACCOUNT_ID) {
17135                 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17136                 if (err_code != NULL)
17137                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17138                 return false;
17139         }
17140
17141         int rc = -1;
17142         int ret = false;
17143         int error = EMAIL_ERROR_NONE;
17144         char sql_query_string[QUERY_SIZE] = {0, };
17145
17146         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17147
17148         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17149         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17150         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d", account_id);
17151
17152         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17153         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17154         if (error != EMAIL_ERROR_NONE) {
17155                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17156                 goto FINISH_OFF;
17157         }
17158
17159         rc = sqlite3_changes(local_db_handle);
17160         if (rc == 0) {
17161                 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17162                 error = EMAIL_ERROR_DATA_NOT_FOUND;
17163                 ret = true;
17164                 goto FINISH_OFF;
17165         }
17166
17167         ret = true;
17168
17169 FINISH_OFF:
17170         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17171
17172         if (err_code != NULL)
17173                 *err_code = error;
17174
17175         EM_DEBUG_FUNC_END("ret [%d]", ret);
17176         return ret;
17177 }
17178
17179
17180 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)
17181 {
17182         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17183
17184         if (account_id < FIRST_ACCOUNT_ID || mailbox_id < 0) {
17185                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17186                 if (err_code != NULL)
17187                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17188                 return false;
17189         }
17190
17191         int rc = -1;
17192         int ret = false;
17193         int error = EMAIL_ERROR_NONE;
17194         char sql_query_string[QUERY_SIZE] = {0, };
17195
17196         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17197
17198         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17199         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17200         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);
17201
17202         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17203         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17204         if (error != EMAIL_ERROR_NONE) {
17205                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17206                 goto FINISH_OFF;
17207         }
17208
17209         rc = sqlite3_changes(local_db_handle);
17210         if (rc == 0) {
17211                 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17212                 error = EMAIL_ERROR_DATA_NOT_FOUND;
17213                 ret = true;
17214                 goto FINISH_OFF;
17215         }
17216
17217         ret = true;
17218
17219 FINISH_OFF:
17220         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17221
17222         if (err_code != NULL)
17223                 *err_code = error;
17224
17225         EM_DEBUG_FUNC_END("ret [%d]", ret);
17226         return ret;
17227 }
17228
17229
17230 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)
17231 {
17232         EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
17233
17234         if (account_id < FIRST_ACCOUNT_ID || !event_start || input_mailbox_id <= 0 || !count) {
17235                 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);
17236
17237                 if (err_code != NULL)
17238                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17239                 return false;
17240         }
17241
17242         int rc = -1;
17243         int ret = false;
17244         char **result;
17245         int error = EMAIL_ERROR_NONE;
17246         int i = 0;
17247         DB_STMT hStmt = NULL;
17248         email_event_auto_download *event_list = NULL;
17249         char sql_query_string[QUERY_SIZE] = {0, };
17250
17251         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17252
17253         EMSTORAGE_START_READ_TRANSACTION(transaction);
17254
17255         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17256         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);
17257
17258
17259         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17260         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17261                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17262
17263         *count = atoi(result[1]);
17264         sqlite3_free_table(result);
17265
17266         EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
17267
17268         if (!*count) {
17269                 EM_DEBUG_LOG("No matched activity found in mail_auto_download_activity_tbl");
17270                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
17271                 ret = true;
17272                 goto FINISH_OFF;
17273         }
17274         EM_DEBUG_LOG("Activity Count = %d", *count);
17275
17276         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17277         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);
17278
17279         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17280
17281
17282         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17283
17284         EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
17285         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17286                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17287
17288
17289         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17290         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17291                         ("sqlite3_step fail:%d", rc));
17292
17293         if (!(event_list = (email_event_auto_download *)em_malloc(sizeof(email_event_auto_download)*(*count)))) {
17294                 EM_DEBUG_EXCEPTION("Malloc failed");
17295
17296                 error = EMAIL_ERROR_OUT_OF_MEMORY;
17297                 goto FINISH_OFF;
17298         }
17299
17300         for (i = 0; i < (*count); i++) {
17301                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17302                 _get_stmt_field_data_int(hStmt, &(event_list[i].status), STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17303                 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17304                 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17305                 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17306                 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17307
17308                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17309                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17310                                 ("sqlite3_step fail:%d", rc));
17311         }
17312
17313         ret = true;
17314
17315 FINISH_OFF:
17316
17317         if (true == ret)
17318                 *event_start = event_list;
17319         else {
17320                 EM_SAFE_FREE(event_list);
17321                 *event_start = NULL;
17322                 *count = 0;
17323         }
17324
17325         if (hStmt != NULL) {
17326                 rc = sqlite3_finalize(hStmt);
17327                 hStmt = NULL;
17328                 if (rc != SQLITE_OK) {
17329                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17330                         error = EMAIL_ERROR_DB_FAILURE;
17331                 }
17332         }
17333
17334         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17335         if (err_code != NULL)
17336                 *err_code = error;
17337
17338         EM_DEBUG_FUNC_END("ret [%d]", ret);
17339         return ret;
17340 }
17341
17342
17343 INTERNAL_FUNC int emstorage_get_auto_download_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
17344 {
17345         EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
17346
17347         if (!activity_count || !err_code) {
17348                 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
17349                 if (err_code != NULL)
17350                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17351                 return false;
17352         }
17353
17354         int rc = -1;
17355         int ret = false;
17356         int error = EMAIL_ERROR_NONE;
17357         DB_STMT hStmt = NULL;
17358         char sql_query_string[QUERY_SIZE] = {0, };
17359
17360         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17361
17362         EMSTORAGE_START_READ_TRANSACTION(transaction);
17363         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17364
17365         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl;");
17366
17367         EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
17368
17369
17370         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17371         EM_DEBUG_LOG_DEV("before sqlite3_prepare hStmt = %p", hStmt);
17372         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17373                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17374
17375
17376         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17377         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17378                         ("sqlite3_step fail:%d", rc));
17379
17380         _get_stmt_field_data_int(hStmt, activity_count, 0);
17381
17382         EM_DEBUG_LOG("counts of activities in activity table [%d]", *activity_count);
17383
17384         ret = true;
17385
17386 FINISH_OFF:
17387
17388         if (hStmt != NULL) {
17389                 rc = sqlite3_finalize(hStmt);
17390                 hStmt = NULL;
17391                 if (rc != SQLITE_OK) {
17392                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17393                         error = EMAIL_ERROR_DB_FAILURE;
17394                 }
17395         }
17396
17397         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17398         if (err_code != NULL)
17399                 *err_code = error;
17400
17401         EM_DEBUG_FUNC_END("ret [%d]", ret);
17402         return ret;
17403 }
17404
17405
17406 INTERNAL_FUNC int emstorage_get_auto_download_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
17407 {
17408         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
17409
17410         if (!account_list || !count) {
17411                 EM_DEBUG_EXCEPTION("account_list[%p], count[%p]", account_list, count);
17412                 if (err_code != NULL)
17413                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17414                 return false;
17415         }
17416
17417         int ret = false;
17418         int error = EMAIL_ERROR_NONE;
17419         char *sql = "SELECT count(distinct account_id) FROM mail_auto_download_activity_tbl";
17420         char **result;
17421         int i = 0, rc = -1;
17422         int *result_account_list = NULL;
17423         DB_STMT hStmt = NULL;
17424         char sql_query_string[QUERY_SIZE] = {0, };
17425         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17426
17427         EMSTORAGE_START_READ_TRANSACTION(transaction);
17428
17429         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
17430         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17431                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
17432
17433         *count = atoi(result[1]);
17434         sqlite3_free_table(result);
17435
17436         if (!*count) {
17437                 EM_DEBUG_EXCEPTION("no account found...");
17438                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17439                 ret = true;
17440                 goto FINISH_OFF;
17441         }
17442
17443         EM_DEBUG_LOG("Account count [%d]", *count);
17444
17445         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17446
17447         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_auto_download_activity_tbl");
17448
17449         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17450
17451
17452         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17453
17454         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
17455         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17456                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17457
17458
17459         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17460         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17461                         ("sqlite3_step fail:%d", rc));
17462
17463         if (NULL == (result_account_list = (int *)em_malloc(sizeof(int)*(*count)))) {
17464                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
17465                 error = EMAIL_ERROR_OUT_OF_MEMORY;
17466                 goto FINISH_OFF;
17467         }
17468
17469         for (i = 0; i < (*count); i++) {
17470                 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
17471
17472                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17473                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17474                                 ("sqlite3_step fail:%d", rc));
17475                 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
17476         }
17477
17478         ret = true;
17479
17480 FINISH_OFF:
17481
17482         if (ret == true)
17483                 *account_list = result_account_list;
17484         else
17485                 EM_SAFE_FREE(result_account_list);
17486
17487         if (hStmt != NULL) {
17488                 rc = sqlite3_finalize(hStmt);
17489                 hStmt = NULL;
17490                 if (rc != SQLITE_OK) {
17491                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17492                         error = EMAIL_ERROR_DB_FAILURE;
17493                 }
17494         }
17495
17496         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17497         if (err_code != NULL)
17498                 *err_code = error;
17499         EM_DEBUG_FUNC_END("ret [%d]", ret);
17500         return ret;
17501 }
17502
17503
17504 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)
17505 {
17506         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
17507
17508         if (account_id < FIRST_ACCOUNT_ID || !mailbox_list || !count) {
17509                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p]", account_id, mailbox_list, count);
17510                 if (err_code != NULL)
17511                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17512                 return false;
17513         }
17514
17515         int ret = false;
17516         int error = EMAIL_ERROR_NONE;
17517         char **result;
17518         int i = 0, rc = -1;
17519         int *mbox_list = NULL;
17520         DB_STMT hStmt = NULL;
17521         char sql_query_string[QUERY_SIZE] = {0, };
17522
17523         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17524
17525         EMSTORAGE_START_READ_TRANSACTION(transaction);
17526
17527         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17528         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);
17529
17530
17531         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17532         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17533                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17534
17535         *count = atoi(result[1]);
17536         sqlite3_free_table(result);
17537
17538         if (!*count) {
17539                 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
17540                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17541                 ret = true;
17542                 goto FINISH_OFF;
17543         }
17544         EM_DEBUG_LOG("Mailbox count = %d", *count);
17545
17546         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17547
17548         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);
17549
17550         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
17551
17552
17553         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17554
17555
17556         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17557                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17558
17559
17560         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17561         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17562                         ("sqlite3_step fail:%d", rc));
17563
17564         mbox_list = (int *)em_malloc(sizeof(int)*(*count)); /* prevent */
17565         if (mbox_list == NULL) {
17566                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
17567                 error = EMAIL_ERROR_OUT_OF_MEMORY;
17568                 goto FINISH_OFF;
17569         }
17570
17571         for (i = 0; i < (*count); i++) {
17572                 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
17573                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17574
17575                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17576                                 ("sqlite3_step fail:%d", rc));
17577                 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
17578         }
17579
17580         ret = true;
17581
17582 FINISH_OFF:
17583
17584         if (ret == true)
17585                 *mailbox_list = mbox_list;
17586         else
17587                 EM_SAFE_FREE(mbox_list);
17588
17589         if (hStmt != NULL) {
17590                 rc = sqlite3_finalize(hStmt);
17591                 hStmt = NULL;
17592                 if (rc != SQLITE_OK) {
17593                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17594                         error = EMAIL_ERROR_DB_FAILURE;
17595                 }
17596         }
17597
17598         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17599         if (err_code != NULL)
17600                 *err_code = error;
17601         EM_DEBUG_FUNC_END("ret [%d]", ret);
17602         return ret;
17603 }
17604
17605
17606 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)
17607 {
17608         EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
17609
17610         if (account_id < FIRST_ACCOUNT_ID || !activity_count || !err_code) {
17611                 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
17612                 if (err_code != NULL)
17613                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17614                 return false;
17615         }
17616
17617         int rc = -1;
17618         int ret = false;
17619         int error = EMAIL_ERROR_NONE;
17620         char sql_query_string[QUERY_SIZE] = {0, };
17621         DB_STMT hStmt = NULL;
17622
17623         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17624
17625         EMSTORAGE_START_READ_TRANSACTION(transaction);
17626         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17627         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);
17628
17629         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
17630
17631         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17632         EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
17633         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17634                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17635
17636         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17637         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17638                         ("sqlite3_step fail:%d", rc));
17639
17640         _get_stmt_field_data_int(hStmt, activity_count, 0);
17641
17642         EM_DEBUG_LOG("count of activities in activity table [%d]", *activity_count);
17643
17644         ret = true;
17645
17646 FINISH_OFF:
17647
17648         if (hStmt != NULL) {
17649                 rc = sqlite3_finalize(hStmt);
17650                 hStmt = NULL;
17651                 if (rc != SQLITE_OK) {
17652                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17653                         error = EMAIL_ERROR_DB_FAILURE;
17654                 }
17655         }
17656
17657         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17658
17659         if (err_code != NULL)
17660                 *err_code = error;
17661
17662         EM_DEBUG_FUNC_END("ret [%d]", ret);
17663         return ret;
17664 }
17665
17666
17667 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)
17668 {
17669         EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mailbox_id[%d]", old_server_uid, new_server_uid, mailbox_id);
17670
17671         int rc = -1, ret = false;
17672         int error = EMAIL_ERROR_NONE;
17673         char sql_query_string[QUERY_SIZE] = {0, };
17674         int transaction = true;
17675
17676         if (!old_server_uid || !new_server_uid || (!mailbox_name && mailbox_id < 0)) {
17677                 EM_DEBUG_EXCEPTION("Invalid parameters");
17678                 error = EMAIL_ERROR_INVALID_PARAM;
17679                 return false;
17680         }
17681
17682         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17683
17684         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17685         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17686
17687         if (mailbox_id > 0)
17688                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17689                                 "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);
17690         else if (mailbox_name)
17691                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17692                                 "UPDATE mail_auto_download_activity_tbl SET server_mail_id = %s WHERE server_mail_id = %s ", new_server_uid, old_server_uid);
17693
17694         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17695         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17696         if (error != EMAIL_ERROR_NONE) {
17697                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17698                 goto FINISH_OFF;
17699         }
17700
17701         rc = sqlite3_changes(local_db_handle);
17702         if (rc == 0)
17703                 EM_DEBUG_LOG("No matching found in mail_auto_download_activity_tbl");
17704
17705         ret = true;
17706
17707 FINISH_OFF:
17708
17709         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17710
17711         if (err_code != NULL)
17712                 *err_code = error;
17713
17714         EM_DEBUG_FUNC_END("ret [%d]", ret);
17715         return ret;
17716 }
17717
17718 #endif
17719
17720 #ifdef __FEATURE_UPDATE_DB_TABLE_SCHEMA__
17721
17722 typedef struct {
17723         char *column_name;
17724         char *column_type;
17725 } email_column_info_t;
17726
17727 static int get_column_information_from_table_callback(void *arg1, int argc, char **argv, char **input_column_name)
17728 {
17729         EM_DEBUG_FUNC_BEGIN("arg1[%p] argc[%d] argv[%p] column_name[%p]", arg1, argc, argv, input_column_name);
17730
17731         int i = 0;
17732         int validated = 0;
17733         char *column_name = NULL;
17734         char *column_type = NULL;
17735         GList *new_list = *((GList**)arg1);
17736         email_column_info_t *column_info_item = NULL;
17737
17738         for (i = 0; i < argc; ++i) {
17739                 /* EM_DEBUG_LOG("%s = %s", input_column_name[i], argv[i]); */
17740                 if (EM_SAFE_STRCMP(input_column_name[i], "name") == 0) {
17741                         if (column_name)
17742                                 EM_SAFE_FREE(column_name);
17743
17744                         column_name = EM_SAFE_STRDUP(argv[i]);
17745                         validated = 1;
17746                 } else if (EM_SAFE_STRCMP(input_column_name[i], "type") == 0) {
17747                         if (column_type)
17748                                 EM_SAFE_FREE(column_type);
17749
17750                         column_type = EM_SAFE_STRDUP(argv[i]);
17751                 }
17752         }
17753
17754         if (validated) {
17755                 EM_DEBUG_LOG("column_name[%s] column_type[%s]", column_name, column_type);
17756                 column_info_item = em_malloc(sizeof(email_column_info_t));
17757                 if (column_info_item == NULL) {
17758                         EM_DEBUG_EXCEPTION("em_mallocfailed");
17759                         goto FINISH_OFF;
17760                 }
17761
17762                 column_info_item->column_name = EM_SAFE_STRDUP(column_name);
17763                 column_info_item->column_type = EM_SAFE_STRDUP(column_type);
17764                 new_list = g_list_append(new_list, (gpointer)column_info_item);
17765                 *((GList**)arg1) = new_list;
17766         }
17767
17768 FINISH_OFF:
17769
17770         EM_SAFE_FREE(column_name);
17771         EM_SAFE_FREE(column_type);
17772
17773         EM_DEBUG_FUNC_END();
17774         return 0;
17775 }
17776
17777 static int emstorage_get_column_information_from_table(char *multi_user_name, const char *input_table_name, GList **output_column_info)
17778 {
17779         EM_DEBUG_FUNC_BEGIN("input_table_name[%p] output_column_info[%p]", input_table_name, output_column_info);
17780         int err = EMAIL_ERROR_NONE;
17781         int result_from_sqlite = 0;
17782         char *error_message_from_sqlite = NULL;
17783         char sql_query_string[QUERY_SIZE] = {0, };
17784         GList *new_list = NULL;
17785         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17786
17787         SNPRINTF(sql_query_string, QUERY_SIZE, "pragma table_info(%s);", input_table_name);
17788
17789         result_from_sqlite = sqlite3_exec(local_db_handle, sql_query_string, get_column_information_from_table_callback, &new_list, &error_message_from_sqlite);
17790
17791         if (result_from_sqlite != SQLITE_OK)
17792                 EM_DEBUG_EXCEPTION("sqlite3_exec returns [%d]", result_from_sqlite);
17793
17794         EM_DEBUG_LOG("new_list[%p] output_column_info[%p]", new_list, output_column_info);
17795
17796         if (new_list && output_column_info) {
17797                 EM_DEBUG_LOG("g_list_length[%d]", g_list_length(new_list));
17798                 *output_column_info = new_list;
17799         }
17800
17801         EM_DEBUG_FUNC_END("err [%d]", err);
17802         return err;
17803 }
17804
17805 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)
17806 {
17807         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);
17808         int error = EMAIL_ERROR_NONE;
17809         int rc = -1;
17810         sqlite3 *local_db_handle = NULL;
17811         char sql_query_string[QUERY_SIZE] = {0, };
17812
17813         if (input_full_query == NULL || input_source_table_name == NULL || input_new_table_name == NULL) {
17814                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17815                 error = EMAIL_ERROR_INVALID_PARAM;
17816                 goto FINISH_OFF;
17817         }
17818
17819         local_db_handle = emstorage_get_db_connection(multi_user_name);
17820
17821         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17822         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17823
17824         EM_DEBUG_LOG("[%s] will be replaced by [%s]", input_source_table_name, input_new_table_name);
17825
17826         EM_SAFE_STRNCPY(sql_query_string, input_full_query[input_query_index], sizeof(sql_query_string)-1); /*prevent 21984*/
17827         reg_replace(sql_query_string, input_source_table_name, input_new_table_name);
17828
17829         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17830 FINISH_OFF:
17831
17832         if (error == EMAIL_ERROR_NONE) {
17833                 EMSTORAGE_PROTECTED_FUNC_CALL(
17834                         sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17835         } else {
17836                 EMSTORAGE_PROTECTED_FUNC_CALL(
17837                         sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17838         }
17839
17840         EM_DEBUG_FUNC_END("error [%d]", error);
17841         return error;
17842 }
17843
17844 static int emstorage_add_column(char *multi_user_name, char *input_table_name, email_column_info_t *input_new_column)
17845 {
17846         EM_DEBUG_FUNC_BEGIN("input_table_name[%p] input_new_column[%p]", input_table_name, input_new_column);
17847         int error = EMAIL_ERROR_NONE;
17848         int rc = -1;
17849         sqlite3 *local_db_handle = NULL;
17850         char sql_query_string[QUERY_SIZE] = {0, };
17851
17852         if (input_table_name == NULL || input_new_column == NULL) {
17853                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17854                 error = EMAIL_ERROR_INVALID_PARAM;
17855                 goto FINISH_OFF;
17856         }
17857
17858         local_db_handle = emstorage_get_db_connection(multi_user_name);
17859
17860         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17861         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17862         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);
17863         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17864 FINISH_OFF:
17865
17866         if (error == EMAIL_ERROR_NONE) {
17867                 EMSTORAGE_PROTECTED_FUNC_CALL(
17868                         sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17869         } else {
17870                 EMSTORAGE_PROTECTED_FUNC_CALL(
17871                         sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17872         }
17873
17874         EM_DEBUG_FUNC_END("error [%d]", error);
17875         return error;
17876 }
17877
17878 static int emstorage_drop_table(char *multi_user_name, char *input_table_name)
17879 {
17880         EM_DEBUG_FUNC_BEGIN("input_table_name[%p]", input_table_name);
17881         int error = EMAIL_ERROR_NONE;
17882         int rc = -1;
17883         sqlite3 *local_db_handle = NULL;
17884         char sql_query_string[QUERY_SIZE] = {0, };
17885
17886         if (input_table_name == NULL) {
17887                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17888                 error = EMAIL_ERROR_INVALID_PARAM;
17889                 goto FINISH_OFF;
17890         }
17891
17892         local_db_handle = emstorage_get_db_connection(multi_user_name);
17893
17894         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17895         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17896         SNPRINTF(sql_query_string, QUERY_SIZE, "DROP TABLE %s;", input_table_name);
17897         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17898 FINISH_OFF:
17899
17900         if (error == EMAIL_ERROR_NONE) {
17901                 EMSTORAGE_PROTECTED_FUNC_CALL(
17902                         sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17903         } else {
17904                 EMSTORAGE_PROTECTED_FUNC_CALL(
17905                         sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17906         }
17907
17908         EM_DEBUG_FUNC_END("error [%d]", error);
17909         return error;
17910 }
17911
17912 gint glist_compare_column_name(gconstpointer old_column_info, gconstpointer new_column_info)
17913 {
17914         EM_DEBUG_FUNC_BEGIN("old_column_info[%p] new_column_info[%p]", old_column_info, new_column_info);
17915         email_column_info_t *left_one  = (email_column_info_t*)old_column_info;
17916         email_column_info_t *right_one = (email_column_info_t*)new_column_info;
17917
17918         if (old_column_info == NULL || new_column_info == NULL)
17919                 return -1;
17920
17921         return EM_SAFE_STRCMP((char*)left_one->column_name, (char*)right_one->column_name);
17922 }
17923
17924 INTERNAL_FUNC int emstorage_update_db_table_schema(char *multi_user_name)
17925 {
17926         EM_DEBUG_FUNC_BEGIN();
17927         int i = 0;
17928         int j = 0;
17929         int error = EMAIL_ERROR_NONE;
17930         int query_len = 0;
17931         email_column_info_t *new_column_info = NULL;
17932         email_column_info_t *p_column_info = NULL;
17933         char **create_table_query = NULL;
17934         GList *found_data = NULL;
17935         GList *column_list_of_old_table = NULL;
17936         GList *column_list_of_new_table = NULL;
17937         char table_names[CREATE_TABLE_MAX][2][50] = { { "mail_account_tbl", "mail_account_tbl_new" },
17938                 { "mail_box_tbl", "mail_box_tbl_new" },
17939                 { "mail_read_mail_uid_tbl", "mail_read_mail_uid_tbl_new" },
17940                 { "mail_rule_tbl", "mail_rule_tbl_new" },
17941                 { "mail_tbl", "mail_tbl_new" },
17942                 { "mail_attachment_tbl", "mail_attachment_tbl_new" },
17943 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
17944                 { "mail_partial_body_activity_tbl", "mail_partial_body_activity_tbl_new" },
17945 #else
17946                 { "", "" },
17947 #endif
17948                 { "mail_meeting_tbl", "mail_meeting_tbl_new" },
17949 #ifdef __FEATURE_LOCAL_ACTIVITY__
17950                 { "mail_local_activity_tbl", "mail_local_activity_tbl_new" },
17951 #else
17952                 { "", "" },
17953 #endif
17954                 { "mail_certificate_tbl", "mail_certificate_tbl_new" },
17955                 { "mail_task_tbl", "mail_task_tbl_new" },
17956 #ifdef __FEATURE_BODY_SEARCH__
17957                 { "mail_text_tbl", "mail_text_tbl_new" },
17958 #else
17959                 { "", "" },
17960 #endif
17961
17962 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
17963                 { "mail_auto_download_activity_tbl", "mail_auto_download_activity_tbl_new" }
17964 #else
17965                 { "", "" }
17966 #endif
17967
17968         };
17969
17970         error = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
17971
17972         if (error != EMAIL_ERROR_NONE) {
17973                 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
17974                 goto FINISH_OFF;
17975         }
17976
17977         if (query_len < CREATE_TABLE_MAX) {
17978                 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
17979                 error = EMAIL_ERROR_SYSTEM_FAILURE;
17980                 goto FINISH_OFF;
17981         }
17982
17983         for (i = CREATE_TABLE_MAIL_ACCOUNT_TBL; i < CREATE_TABLE_MAX; i++) {
17984                 EM_DEBUG_LOG("table [%s] new_table [%s]", table_names[i][0], table_names[i][1]);
17985                 if (EM_SAFE_STRLEN(table_names[i][0]) && EM_SAFE_STRLEN(table_names[i][1])) {
17986                         /* Check existing of _new table */
17987                         emstorage_drop_table(multi_user_name, table_names[i][1]);
17988                         error = emstorage_create_renamed_table(multi_user_name, create_table_query, i, table_names[i][0], table_names[i][1]);
17989                         if (error != EMAIL_ERROR_NONE) {
17990                                 EM_DEBUG_EXCEPTION("emstorage_create_renamed_table failed [%d]", error);
17991                                 goto FINISH_OFF;
17992                         }
17993
17994                         emstorage_get_column_information_from_table(multi_user_name, table_names[i][0], &column_list_of_old_table);
17995                         emstorage_get_column_information_from_table(multi_user_name, table_names[i][1], &column_list_of_new_table);
17996
17997                         /* Compare fields and add new field */
17998                         for (j = 0; j < g_list_length(column_list_of_new_table); j++) {
17999                                 new_column_info = (email_column_info_t*)g_list_nth_data(column_list_of_new_table, j);
18000                                 found_data = g_list_find_custom(column_list_of_old_table, (gconstpointer)new_column_info, glist_compare_column_name);
18001                                 if (found_data == NULL) {
18002                                         /* add new field*/
18003                                         emstorage_add_column(multi_user_name, table_names[i][0], new_column_info);
18004                                 }
18005                         }
18006
18007                         emstorage_drop_table(multi_user_name, table_names[i][1]);
18008                 } else {
18009                         EM_DEBUG_LOG("Skipped");
18010                 }
18011         }
18012
18013 FINISH_OFF:
18014         if (create_table_query) {
18015                 int i = 0;
18016                 for (i = 0; i < query_len; i++) {
18017                         if (create_table_query[i])
18018                                 EM_SAFE_FREE(create_table_query[i]);
18019                 }
18020                 EM_SAFE_FREE(create_table_query);
18021         }
18022
18023         found_data = g_list_first(column_list_of_old_table);
18024         while (found_data != NULL) {
18025                 p_column_info = (email_column_info_t *)found_data->data;
18026                 EM_SAFE_FREE(p_column_info->column_name);
18027                 EM_SAFE_FREE(p_column_info->column_type);
18028                 EM_SAFE_FREE(p_column_info);
18029
18030                 found_data = g_list_next(found_data);
18031         }
18032         g_list_free(column_list_of_old_table);
18033
18034         found_data = g_list_first(column_list_of_new_table);
18035         while (found_data != NULL) {
18036                 p_column_info = (email_column_info_t *)found_data->data;
18037                 EM_SAFE_FREE(p_column_info->column_name);
18038                 EM_SAFE_FREE(p_column_info->column_type);
18039                 EM_SAFE_FREE(p_column_info);
18040
18041                 found_data = g_list_next(found_data);
18042         }
18043         g_list_free(column_list_of_new_table);
18044
18045         EM_DEBUG_FUNC_END("error [%d]", error);
18046         return error;
18047 }
18048 #endif /* __FEATURE_UPDATE_DB_TABLE_SCHEMA__ */
18049
18050 /*EOF*/