Merge "svace issues" into tizen
[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                 if (mkdir(DATA_PATH, DIRECTORY_PERMISSION) != 0)
1613                         EM_DEBUG_EXCEPTION("Fail to create the directory \n");
1614                 if (mkdir(EMAIL_PATH, DIRECTORY_PERMISSION) != 0)
1615                         EM_DEBUG_EXCEPTION("Fail to create the directory \n");
1616                 if (mkdir(MAIL_HOME, DIRECTORY_PERMISSION) != 0)
1617                         EM_DEBUG_EXCEPTION("Fail to create the directory \n");
1618                 if (mkdir(MAIL_TEMP, DIRECTORY_PERMISSION) != 0)
1619                         EM_DEBUG_EXCEPTION("Fail to create the directory \n");
1620
1621                 _delete_temp_file(MAIL_TEMP);
1622         }
1623
1624         if (!emstorage_create_table(multi_user_name, EMAIL_CREATE_DB_NORMAL, &error)) {
1625                 EM_DEBUG_EXCEPTION(" emstorage_create_table failed - %d", error);
1626                 goto FINISH_OFF;
1627         }
1628
1629 FINISH_OFF:
1630
1631
1632         if (err_code != NULL)
1633                 *err_code = error;
1634
1635         return NULL;
1636 }
1637
1638 /*  pData : a parameter which is registered when busy handler is registerd */
1639 /*  count : retry count */
1640 static int _callback_sqlite_busy_handler(void *pData, int count)
1641 {
1642         if (10 - count > 0) {
1643                 struct timespec time = {
1644                         .tv_sec = 0,
1645                         .tv_nsec = (count + 1) * 100 * 1000 * 1000
1646                 };
1647                 EM_DEBUG_LOG("Busy handler called!!: PID[%d] / CNT [%d]", getpid(), count);
1648                 nanosleep(&time, NULL);
1649                 return 1;
1650         } else {
1651                 EM_DEBUG_EXCEPTION("Busy handler will be returned SQLITE_BUSY error PID[%d] / CNT[%d]", getpid(), count);
1652                 return 0;
1653         }
1654 }
1655
1656 static int _callback_collation_utf7_sort(void *data, int length_text_a, const void *text_a,
1657                 int length_text_b, const void *text_b)
1658 {
1659         EM_DEBUG_FUNC_BEGIN();
1660         int result = 0;
1661         char *converted_string_a = NULL;
1662         char *converted_string_b = NULL;
1663
1664         EM_DEBUG_LOG_DEV("text_a : [%s]", text_a);
1665         converted_string_a = emcore_convert_mutf7_to_utf8((char *)text_a);
1666         EM_DEBUG_LOG_DEV("Converted text_a : [%s]", converted_string_a);
1667
1668         EM_DEBUG_LOG_DEV("text_b : [%s]", text_b);
1669         converted_string_b = emcore_convert_mutf7_to_utf8((char *)text_b);
1670         EM_DEBUG_LOG_DEV("Converted text_b : [%s]", converted_string_b);
1671
1672         if (converted_string_a && converted_string_b)
1673                 result = strcmp(converted_string_a, converted_string_b);
1674
1675         EM_SAFE_FREE(converted_string_a);
1676         EM_SAFE_FREE(converted_string_b);
1677
1678         EM_DEBUG_FUNC_END();
1679         return result;
1680 }
1681
1682 static int _delete_all_files_and_directories(char *db_file_path, int *err_code)
1683 {
1684         EM_DEBUG_FUNC_BEGIN();
1685
1686         int error = EMAIL_ERROR_NONE;
1687         int ret = false;
1688
1689         if (!emstorage_delete_file(db_file_path, &error)) {
1690                 if (error != EMAIL_ERROR_FILE_NOT_FOUND) {
1691                         EM_DEBUG_EXCEPTION_SEC("remove failed - %s", EMAIL_SERVICE_DB_FILE_PATH);
1692                         goto FINISH_OFF;
1693                 }
1694         }
1695
1696         if (!emstorage_delete_dir((char *)MAIL_HOME, &error)) {
1697                 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed");
1698                 goto FINISH_OFF;
1699         }
1700
1701         ret = true;
1702
1703 FINISH_OFF:
1704         if (err_code)
1705                 *err_code = error;
1706         EM_DEBUG_FUNC_END();
1707         return ret;
1708 }
1709
1710 static int _recovery_from_malformed_db_file(char *db_file_path, int *err_code)
1711 {
1712         EM_DEBUG_FUNC_BEGIN();
1713
1714         int error = EMAIL_ERROR_NONE;
1715         int ret = false;
1716
1717         /* Delete all files and directories */
1718         if (!_delete_all_files_and_directories(db_file_path, &error)) {
1719                 EM_DEBUG_EXCEPTION("_delete_all_files_and_directories failed [%d]", error);
1720                 goto FINISH_OFF;
1721         }
1722
1723         /* Delete all accounts on MyAccount */
1724
1725         /* Delete all managed connection to DB */
1726         emstorage_reset_db_handle_list();
1727
1728         ret = true;
1729
1730 FINISH_OFF:
1731         if (err_code)
1732                 *err_code = error;
1733         EM_DEBUG_FUNC_END();
1734         return ret;
1735 }
1736
1737 int _xsystem(const char *argv[])
1738 {
1739         int status = 0;
1740         pid_t pid;
1741         pid = fork();
1742
1743         switch (pid) {
1744         case -1:
1745                 perror("fork failed");
1746                 return -1;
1747         case 0:
1748                 if (execvp(argv[0], (char *const *)argv) == -1) {
1749                         perror("execute init db script");
1750                         _exit(-1);
1751                 }
1752                 _exit(2);
1753         default:
1754                 /* parent */
1755                 break;
1756         }
1757
1758         if (waitpid(pid, &status, 0) == -1) {
1759                 perror("waitpid failed");
1760                 return -1;
1761         }
1762
1763         if (WIFSIGNALED(status)) {
1764                 perror("signal");
1765                 return -1;
1766         }
1767
1768         if (!WIFEXITED(status)) {
1769                 perror("should not happen");
1770                 return -1;
1771         }
1772
1773         return WEXITSTATUS(status);
1774 }
1775
1776 #define SCRIPT_INIT_DB "/usr/bin/email-service_init_db.sh"
1777
1778 INTERNAL_FUNC int emstorage_init_db(char *multi_user_name)
1779 {
1780         EM_DEBUG_FUNC_BEGIN();
1781         int err = EMAIL_ERROR_NONE;
1782         char *prefix_path = NULL;
1783         char *output_file_path = NULL;
1784         char temp_file_path[MAX_PATH] = {0};
1785         gid_t email_gid = -1;
1786         uid_t email_uid = -1;
1787         struct group *gr;
1788         struct group buf_group;
1789         struct passwd *pw;
1790         struct passwd buf_passwd;
1791         char buf_gr[MAX_GRP_BUF_SIZE];
1792         char buf_pw[MAX_GRP_BUF_SIZE];
1793         int result = 0;
1794
1795         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1796                 err = emcore_get_container_path(multi_user_name, &prefix_path);
1797                 if (err != EMAIL_ERROR_NONE) {
1798                         if (err != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1799                                 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", err);
1800                                 goto FINISH_OFF;
1801                         }
1802                 }
1803         } else {
1804                 prefix_path = strdup("");
1805         }
1806
1807         if (err == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1808                 err = emcore_get_canonicalize_path((char *)EMAIL_SERVICE_DB_FILE_PATH, &output_file_path);
1809                 if (err != EMAIL_ERROR_NONE) {
1810                         EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", err);
1811                         goto FINISH_OFF;
1812                 }
1813
1814                 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
1815         } else {
1816                 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
1817         }
1818
1819         EM_DEBUG_LOG("db file path : [%s]", temp_file_path);
1820
1821         if (!g_file_test(temp_file_path, G_FILE_TEST_EXISTS)) {
1822                 int ret = true;
1823                 const char *argv_script[] = {"/bin/sh", SCRIPT_INIT_DB, NULL};
1824                 ret = _xsystem(argv_script);
1825
1826                 if (ret == -1) {
1827                         EM_DEBUG_EXCEPTION("_xsystem failed");
1828                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1829                 }
1830
1831                 result = getgrnam_r(GID, &buf_group, buf_gr, sizeof(buf_gr), &gr);
1832                 if (result == 0 && NULL != gr)
1833                         email_gid = gr->gr_gid;
1834                 else
1835                         email_gid = GID_DEFAULT;
1836
1837
1838                 result = getpwnam_r(UID, &buf_passwd, buf_pw, sizeof(buf_pw), &pw);
1839                 if (result == 0 && NULL != pw)
1840                         email_uid = pw->pw_uid;
1841                 else
1842                         email_uid = UID_DEFAULT;
1843
1844
1845                 if (chmod(EMAIL_SERVICE_DB_FILE_PATH, 0660) != 0)
1846                         EM_DEBUG_EXCEPTION("Fail to change the permission \n");
1847                 if (chmod(EMAIL_SERVICE_DB_JOURNAL_FILE_PATH, 0660) != 0)
1848                         EM_DEBUG_EXCEPTION("Fail to change the permission \n");
1849                 if (chmod(EMAIL_SQL_PATH, 0660) != 0)
1850                         EM_DEBUG_EXCEPTION("Fail to change the permission \n");
1851
1852                 ret = chown(EMAIL_SERVICE_DB_FILE_PATH, email_uid, email_gid);
1853                 if (0 != ret)
1854                         EM_DEBUG_EXCEPTION("chown() Fail[errno=%d]", errno);
1855                 ret = chown(EMAIL_SERVICE_DB_JOURNAL_FILE_PATH, email_uid, email_gid);
1856                 if (0 != ret)
1857                         EM_DEBUG_EXCEPTION("chown() Fail[errno=%d]", errno);
1858                 ret = chown(EMAIL_SQL_PATH, email_uid, email_gid);
1859                 if (0 != ret)
1860                         EM_DEBUG_EXCEPTION("chown() Fail[errno=%d]", errno);
1861
1862         }
1863
1864 FINISH_OFF:
1865
1866         EM_SAFE_FREE(prefix_path);
1867         EM_SAFE_FREE(output_file_path);
1868
1869         EM_DEBUG_FUNC_END();
1870         return err;
1871 }
1872
1873 INTERNAL_FUNC int em_db_open(char *db_file_path, sqlite3 **sqlite_handle, int *err_code)
1874 {
1875         EM_DEBUG_FUNC_BEGIN();
1876         int rc = 0;
1877         int error = EMAIL_ERROR_NONE;
1878         int ret = false;
1879
1880         EM_DEBUG_LOG_DEV("*sqlite_handle[%p]", *sqlite_handle);
1881
1882         if (*sqlite_handle) { /*prevent 33351*/
1883                 EM_DEBUG_LOG_DEV(">>>>> DB Already Opened......");
1884                 if (err_code != NULL)
1885                         *err_code = error;
1886                 return true;
1887         }
1888
1889         EM_DEBUG_LOG("DB file path : [%s]", db_file_path);
1890
1891         /*  db open */
1892         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1893         if (SQLITE_OK != rc) {
1894                 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1895                 if (SQLITE_PERM == rc || SQLITE_CANTOPEN == rc)
1896                         error = EMAIL_ERROR_PERMISSION_DENIED;
1897                 else
1898                         error = EMAIL_ERROR_DB_FAILURE;
1899
1900                 sqlite3_close(*sqlite_handle);
1901                 *sqlite_handle = NULL;
1902
1903                 if (SQLITE_CORRUPT == rc) /* SQLITE_CORRUPT : The database disk image is malformed */ {/* Recovery DB file */
1904                         EM_DEBUG_LOG("The database disk image is malformed. Trying to remove and create database disk image and directories");
1905                         if (!_recovery_from_malformed_db_file(db_file_path, &error)) {
1906                                 EM_DEBUG_EXCEPTION("_recovery_from_malformed_db_file failed [%d]", error);
1907                                 goto FINISH_OFF;
1908                         }
1909
1910                         EM_DEBUG_LOG("Open DB again");
1911                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1912                         if (SQLITE_OK != rc) {
1913                                 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1914                                 if (SQLITE_PERM == rc)
1915                                         error = EMAIL_ERROR_PERMISSION_DENIED;
1916                                 else
1917                                         error = EMAIL_ERROR_DB_FAILURE;
1918
1919                                 sqlite3_close(*sqlite_handle);
1920                                 *sqlite_handle = NULL;
1921                                 goto FINISH_OFF; /*prevent 33351*/
1922                         }
1923                 } else
1924                         goto FINISH_OFF;
1925         }
1926
1927         /* register busy handler */
1928         EM_DEBUG_LOG_DEV(">>>>> Register DB Handle to busy handler: *sqlite_handle[%p]", *sqlite_handle);
1929         rc = sqlite3_busy_handler(*sqlite_handle, _callback_sqlite_busy_handler, NULL);  /*  Busy Handler registration, NULL is a parameter which will be passed to handler */
1930         if (SQLITE_OK != rc) {
1931                 EM_DEBUG_EXCEPTION("sqlite3_busy_handler fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1932                 error = EMAIL_ERROR_DB_FAILURE;
1933                 sqlite3_close(*sqlite_handle);
1934                 *sqlite_handle = NULL;
1935                 goto FINISH_OFF;
1936         }
1937
1938         /* Register collation callback function */
1939         rc = sqlite3_create_collation(*sqlite_handle, "CONVERTUTF8", SQLITE_UTF8, NULL, _callback_collation_utf7_sort);
1940         if (SQLITE_OK != rc) {
1941                 EM_DEBUG_EXCEPTION("sqlite3_create_collation failed : [%d][%s]", rc, sqlite3_errmsg(*sqlite_handle));
1942                 error = EMAIL_ERROR_DB_FAILURE;
1943                 sqlite3_close(*sqlite_handle);
1944                 *sqlite_handle = NULL;
1945                 goto FINISH_OFF;
1946         }
1947
1948         ret = true;
1949
1950 FINISH_OFF:
1951         if (err_code != NULL)
1952                 *err_code = error;
1953
1954         EM_DEBUG_FUNC_END("ret [%d]", ret);
1955         return ret;
1956 }
1957
1958 INTERNAL_FUNC sqlite3* emstorage_db_open(char *multi_user_name, int *err_code)
1959 {
1960         EM_DEBUG_FUNC_BEGIN();
1961
1962         sqlite3 *_db_handle = NULL;
1963
1964         int error = EMAIL_ERROR_NONE;
1965         char *prefix_path = NULL;
1966
1967         _db_handle = emstorage_get_db_handle(multi_user_name);
1968
1969         if (_db_handle == NULL) {
1970                 char *output_file_path = NULL;
1971                 char temp_file_path[MAX_PATH] = {0};
1972
1973                 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1974                         error = emcore_get_container_path(multi_user_name, &prefix_path);
1975                         if (error != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION && error != EMAIL_ERROR_NONE) {
1976                                 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", error);
1977                                 goto FINISH_OFF;
1978                         }
1979                 } else {
1980                         prefix_path = strdup("");
1981                 }
1982
1983                 if (error == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1984                         if ((error = emcore_get_canonicalize_path((char *)EMAIL_SERVICE_DB_FILE_PATH, &output_file_path)) != EMAIL_ERROR_NONE) {
1985                                 EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", error);
1986                                 goto FINISH_OFF;
1987                         }
1988
1989                         SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
1990                         EM_SAFE_FREE(output_file_path);
1991                 } else {
1992                         SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
1993                 }
1994
1995                 if (!em_db_open(temp_file_path, &_db_handle, &error)) {
1996                         EM_DEBUG_EXCEPTION("em_db_open failed[%d]", error);
1997                         goto FINISH_OFF;
1998                 }
1999
2000                 _initialize_shm_mutex(SHM_FILE_FOR_DB_LOCK, &shm_fd_for_db_lock, &mapped_for_db_lock);
2001
2002 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
2003                 _initialize_shm_mutex(SHM_FILE_FOR_MAIL_ID_LOCK, &shm_fd_for_generating_mail_id, &mapped_for_generating_mail_id);
2004 #endif /*__FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
2005
2006                 emstorage_set_db_handle(multi_user_name, _db_handle);
2007
2008                 emstorage_initialize_field_count();
2009         }
2010
2011 FINISH_OFF:
2012
2013         EM_SAFE_FREE(prefix_path);
2014
2015         if (err_code != NULL)
2016                 *err_code = error;
2017
2018         EM_DEBUG_FUNC_END("ret [%p]", _db_handle);
2019         return _db_handle;
2020 }
2021
2022 INTERNAL_FUNC int emstorage_db_close(char *multi_user_name, int *err_code)
2023 {
2024         EM_DEBUG_FUNC_BEGIN();
2025 #ifdef _MULTIPLE_DB_HANDLE
2026         sqlite3 *_db_handle = emstorage_get_db_handle(multi_user_name);
2027 #endif
2028
2029         int error = EMAIL_ERROR_NONE;
2030         int ret = false;
2031
2032         if (_db_handle) {
2033                 ret = sqlite3_close(_db_handle);
2034                 if (ret != SQLITE_OK) {
2035                         EM_DEBUG_EXCEPTION(" sqlite3_close fail - %d", ret);
2036                         error = EMAIL_ERROR_DB_FAILURE;
2037                         ret = false;
2038                         goto FINISH_OFF;
2039                 }
2040 #ifdef _MULTIPLE_DB_HANDLE
2041                 emstorage_remove_db_handle();
2042 #endif
2043                 _db_handle = NULL;
2044         }
2045
2046         ret = true;
2047
2048 FINISH_OFF:
2049         if (err_code != NULL)
2050                 *err_code = error;
2051
2052         EM_DEBUG_FUNC_END("ret [%d]", ret);
2053         return ret;
2054 }
2055
2056 INTERNAL_FUNC int emstorage_open(char *multi_user_name, int *err_code)
2057 {
2058         EM_DEBUG_FUNC_BEGIN();
2059
2060         int ret = false;
2061         int error = EMAIL_ERROR_NONE;
2062         int retValue;
2063         char *prefix_path = NULL;
2064         char buf[MAX_PATH] = {0};
2065
2066         if (EM_SAFE_STRLEN(multi_user_name) <= 0) {
2067                 SNPRINTF(buf, sizeof(buf), "%s", DB_PATH);
2068         } else {
2069                 error = emcore_get_container_path(multi_user_name, &prefix_path);
2070                 if (error != EMAIL_ERROR_NONE) {
2071                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
2072                         goto FINISH_OFF;
2073                 }
2074                 SNPRINTF(buf, sizeof(buf), "%s/%s", prefix_path, DB_PATH);
2075         }
2076
2077         if (!g_file_test(buf, G_FILE_TEST_EXISTS)) {
2078                 retValue = mkdir(buf, DIRECTORY_PERMISSION);
2079
2080                 EM_DEBUG_LOG("mkdir return- %d", retValue);
2081                 EM_DEBUG_LOG("emstorage_open - before sqlite3_open - pid = %d", getpid());
2082         }
2083
2084         if (emstorage_db_open(multi_user_name, &error) == NULL) {
2085                 EM_DEBUG_EXCEPTION("emstorage_db_open failed[%d]", error);
2086                 goto FINISH_OFF;
2087         }
2088
2089         if (_open_counter++ == 0)
2090                 _emstorage_open_once(multi_user_name, &error);
2091
2092         ret = true;
2093
2094 FINISH_OFF:
2095
2096         EM_SAFE_FREE(prefix_path);
2097
2098         if (err_code != NULL)
2099                 *err_code = error;
2100
2101         EM_DEBUG_FUNC_END("ret [%d]", ret);
2102         return ret;
2103 }
2104
2105 static int emstorage_get_field_count_from_create_table_query(char *input_create_table_query, int *output_field_count)
2106 {
2107         EM_DEBUG_FUNC_BEGIN("input_create_table_query[%d], output_field_count[%p]", input_create_table_query, output_field_count);
2108         int err = EMAIL_ERROR_NONE;
2109         int field_count = 0;
2110         char *pos = NULL;
2111
2112         if (input_create_table_query == NULL || output_field_count == NULL) {
2113                 err = EMAIL_ERROR_INVALID_PARAM;
2114                 goto FINISH_OFF;
2115         }
2116
2117         pos = input_create_table_query;
2118
2119         do {
2120                 field_count++;
2121                 if (pos == NULL || *pos == (char)0)
2122                         break;
2123                 pos += 1;
2124                 pos = strchr(pos, ',');
2125         } while (pos);
2126
2127         *output_field_count = field_count;
2128
2129         EM_DEBUG_LOG_DEV("field_count [%d]", field_count);
2130
2131 FINISH_OFF:
2132
2133         EM_DEBUG_FUNC_END("err [%d]", err);
2134         return err;
2135 }
2136
2137 INTERNAL_FUNC int emstorage_initialize_field_count()
2138 {
2139         EM_DEBUG_FUNC_BEGIN();
2140         int err = EMAIL_ERROR_NONE;
2141         int query_len = 0;
2142         char **create_table_query = NULL;
2143
2144         if (_field_count_of_table[CREATE_TABLE_MAIL_ACCOUNT_TBL] != 0) {
2145                 err = EMAIL_ERROR_ALREADY_INITIALIZED;
2146                 goto FINISH_OFF;
2147         }
2148
2149         err = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2150         if (err != EMAIL_ERROR_NONE) {
2151                 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", err);
2152                 goto FINISH_OFF;
2153         }
2154
2155         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]));
2156         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]));
2157         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]));
2158         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]));
2159         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_TBL]));
2160         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]));
2161         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]));
2162         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]));
2163 #ifdef __FEATURE_LOCAL_ACTIVITY__
2164         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]));
2165 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2166         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]));
2167         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]));
2168         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]));
2169 FINISH_OFF:
2170
2171         if (create_table_query) {
2172                 int i = 0;
2173                 for (i = 0; i < query_len; i++) {
2174                         if (create_table_query[i])
2175                                 EM_SAFE_FREE(create_table_query[i]);
2176                 }
2177                 EM_SAFE_FREE(create_table_query);
2178         }
2179
2180         EM_DEBUG_FUNC_END("err [%d]", err);
2181         return err;
2182 }
2183
2184 INTERNAL_FUNC int emstorage_create_table(char *multi_user_name, emstorage_create_db_type_t type, int *err_code)
2185 {
2186         EM_DEBUG_FUNC_BEGIN();
2187
2188         int error = EMAIL_ERROR_NONE;
2189         int rc = -1, ret = false;
2190         int query_len = 0;
2191         char sql_query_string[QUERY_SIZE] = {0, };
2192         char **create_table_query = NULL;
2193
2194         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2195
2196         error = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2197         if (error != EMAIL_ERROR_NONE) {
2198                 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
2199                 goto FINISH_OFF2;
2200         }
2201
2202         if (query_len < CREATE_TABLE_MAX) {
2203                 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
2204                 error = EMAIL_ERROR_SYSTEM_FAILURE;
2205                 goto FINISH_OFF2;
2206         }
2207
2208         EM_DEBUG_LOG("local_db_handle = %p.", local_db_handle);
2209
2210         char *sql;
2211         char **result = NULL;
2212
2213         /*  1. create mail_account_tbl */
2214         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_account_tbl';";
2215         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2216         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2217                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2218
2219         EM_DEBUG_LOG("emstorage_create_table - result[1] = %s %s", result[1], result[1]);
2220
2221         if (atoi(result[1]) < 1) {
2222                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2223                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2224                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2225
2226                 EM_DEBUG_LOG("CREATE TABLE mail_account_tbl");
2227                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2228                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2229                 if (error != EMAIL_ERROR_NONE) {
2230                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2231                         goto FINISH_OFF;
2232                 }
2233
2234                 /*  create mail_account_tbl unique index */
2235                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ACCOUNT_IDX]);
2236                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2237                 if (error != EMAIL_ERROR_NONE) {
2238                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2239                         goto FINISH_OFF;
2240                 }
2241
2242                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2243         } /*  mail_account_tbl */
2244         else if (type == EMAIL_CREATE_DB_CHECK) {
2245                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], NULL, NULL, NULL), rc);
2246                 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)));
2247         }
2248
2249         sqlite3_free_table(result);
2250         result = NULL;
2251
2252         /*  2. create mail_box_tbl */
2253         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_box_tbl';";
2254
2255         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2256         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2257                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2258
2259         if (atoi(result[1]) < 1) {
2260                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2261                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2262                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2263
2264                 EM_DEBUG_LOG("CREATE TABLE mail_box_tbl");
2265
2266                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_BOX_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2267                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2268                 if (error != EMAIL_ERROR_NONE) {
2269                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2270                         goto FINISH_OFF;
2271                 }
2272
2273                 /*  create mail_local_mailbox_tbl unique index */
2274                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_BOX_IDX]);
2275                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2276                 if (error != EMAIL_ERROR_NONE) {
2277                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2278                         goto FINISH_OFF;
2279                 }
2280
2281                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2282         } /*  mail_box_tbl */
2283         else if (type == EMAIL_CREATE_DB_CHECK) {
2284                 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_BOX_TBL], NULL, NULL, NULL);
2285                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2286                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_BOX_TBL], rc, sqlite3_errmsg(local_db_handle)));
2287         }
2288         sqlite3_free_table(result);
2289         result = NULL;
2290
2291         /*  3. create mail_read_mail_uid_tbl */
2292         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_read_mail_uid_tbl';";
2293         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2294         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2295                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2296
2297
2298         if (atoi(result[1]) < 1) {
2299                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2300                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2301                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2302
2303                 EM_DEBUG_LOG("CREATE TABLE mail_read_mail_uid_tbl");
2304
2305                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2306                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2307                 if (error != EMAIL_ERROR_NONE) {
2308                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2309                         goto FINISH_OFF;
2310                 }
2311
2312                 /*  create mail_read_mail_uid_tbl unique index */
2313                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_IDX]);
2314                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2315                 if (error != EMAIL_ERROR_NONE) {
2316                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2317                         goto FINISH_OFF;
2318                 }
2319
2320                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2321         } /*  mail_read_mail_uid_tbl */
2322         else if (type == EMAIL_CREATE_DB_CHECK) {
2323                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], NULL, NULL, NULL), rc);
2324                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2325                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], rc, sqlite3_errmsg(local_db_handle)));
2326         }
2327         sqlite3_free_table(result);
2328         result = NULL;
2329
2330         /*  4. create mail_rule_tbl */
2331         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_rule_tbl';";
2332
2333         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2334         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2335                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2336
2337         if (atoi(result[1]) < 1) {
2338                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2339                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2340                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2341
2342                 EM_DEBUG_LOG("CREATE TABLE mail_rule_tbl");
2343
2344                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_RULE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2345                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2346                 if (error != EMAIL_ERROR_NONE) {
2347                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2348                         goto FINISH_OFF;
2349                 }
2350
2351                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2352         } /*  mail_rule_tbl */
2353         else if (type == EMAIL_CREATE_DB_CHECK) {
2354                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_RULE_TBL], NULL, NULL, NULL), rc);
2355                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2356                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_RULE_TBL], rc, sqlite3_errmsg(local_db_handle)));
2357         }
2358         sqlite3_free_table(result);
2359         result = NULL;
2360
2361         /*  5. create mail_tbl */
2362         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_tbl';";
2363         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2364         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2365                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2366
2367         if (atoi(result[1]) < 1) {
2368                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2369                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2370                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2371                 EM_DEBUG_LOG("CREATE TABLE mail_tbl");
2372
2373                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2374                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2375                 if (error != EMAIL_ERROR_NONE) {
2376                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2377                         goto FINISH_OFF;
2378                 }
2379
2380                 /*  create mail_tbl unique index */
2381                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_IDX]);
2382                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2383                 if (error != EMAIL_ERROR_NONE) {
2384                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2385                         goto FINISH_OFF;
2386                 }
2387
2388                 /*  create mail_tbl index for date_time */
2389                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_DATETIME_IDX]);
2390                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2391                 if (error != EMAIL_ERROR_NONE) {
2392                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2393                         goto FINISH_OFF;
2394                 }
2395
2396                 /*  create mail_tbl index for thread_item_count */
2397                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_THREAD_IDX]);
2398                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2399                 if (error != EMAIL_ERROR_NONE) {
2400                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2401                         goto FINISH_OFF;
2402                 }
2403
2404                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2405                 /*  just one time call */
2406                 /*      EFTSInitFTSIndex(FTS_EMAIL_IDX); */
2407         } /* mail_tbl */
2408         else if (type == EMAIL_CREATE_DB_CHECK) {
2409                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TBL], NULL, NULL, NULL), rc);
2410                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2411                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TBL], rc, sqlite3_errmsg(local_db_handle)));
2412         }
2413         sqlite3_free_table(result);
2414         result = NULL;
2415
2416         /*  6. create mail_attachment_tbl */
2417         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_attachment_tbl';";
2418         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2419         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2420                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2421
2422         if (atoi(result[1]) < 1) {
2423                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2424                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2425                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2426
2427                 EM_DEBUG_LOG("CREATE TABLE mail_attachment_tbl");
2428
2429                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2430                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2431                 if (error != EMAIL_ERROR_NONE) {
2432                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2433                         goto FINISH_OFF;
2434                 }
2435
2436                 /*  create mail_attachment_tbl unique index */
2437                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_IDX]);
2438                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2439                 if (error != EMAIL_ERROR_NONE) {
2440                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2441                         goto FINISH_OFF;
2442                 }
2443
2444                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2445         } /*  mail_attachment_tbl */
2446         else if (type == EMAIL_CREATE_DB_CHECK) {
2447                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], NULL, NULL, NULL), rc);
2448                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2449                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], rc, sqlite3_errmsg(local_db_handle)));
2450         }
2451         sqlite3_free_table(result);
2452         result = NULL;
2453
2454 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
2455
2456         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_partial_body_activity_tbl';";
2457         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2458         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2459                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2460
2461         if (atoi(result[1]) < 1) {
2462
2463                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2464                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2465                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2466
2467                 EM_DEBUG_LOG("CREATE TABLE mail_partial_body_activity_tbl");
2468
2469                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2470                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2471                 if (error != EMAIL_ERROR_NONE) {
2472                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2473                         goto FINISH_OFF;
2474                 }
2475
2476                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2477         } /*  mail_rule_tbl */
2478         else if (type == EMAIL_CREATE_DB_CHECK) {
2479                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2480                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2481                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2482         }
2483         sqlite3_free_table(result);
2484         result = NULL;
2485
2486 #endif /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
2487
2488         /*  create mail_meeting_tbl */
2489         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_meeting_tbl';";
2490         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2491         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2492                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2493
2494         if (atoi(result[1]) < 1) {
2495                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2496                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2497                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2498
2499                 EM_DEBUG_LOG("CREATE TABLE mail_meeting_tbl");
2500
2501                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_MEETING_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
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                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_MEETING_IDX]);
2509                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2510                 if (error != EMAIL_ERROR_NONE) {
2511                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2512                         goto FINISH_OFF;
2513                 }
2514
2515                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2516         } /*  mail_contact_sync_tbl */
2517         else if (type == EMAIL_CREATE_DB_CHECK) {
2518                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], NULL, NULL, NULL), rc);
2519                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2520                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], rc, sqlite3_errmsg(local_db_handle)));
2521         }
2522         sqlite3_free_table(result);
2523         result = NULL;
2524
2525 #ifdef __FEATURE_LOCAL_ACTIVITY__
2526
2527         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_local_activity_tbl';";
2528         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2529         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2530                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2531
2532         if (atoi(result[1]) < 1) {
2533
2534                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2535                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2536                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2537
2538                 EM_DEBUG_LOG(" CREATE TABLE mail_local_activity_tbl");
2539
2540                 SNPRINTF(sql_query_string, sizeof(sql_query_string), create_table_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL]);
2541                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2542                 if (error != EMAIL_ERROR_NONE) {
2543                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2544                         goto FINISH_OFF;
2545                 }
2546
2547                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2548         } /*  mail_rule_tbl */
2549         else if (type == EMAIL_CREATE_DB_CHECK) {
2550                 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL);
2551                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2552                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2553                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2554         }
2555         sqlite3_free_table(result);
2556         result = NULL;
2557 #endif /*  __FEATURE_LOCAL_ACTIVITY__ */
2558         /*  create mail_certificate_tbl */
2559         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_certificate_tbl';";
2560         /*  rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL);   */
2561         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2562         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2563
2564         if (atoi(result[1]) < 1) {
2565                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2566                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2567
2568                 EM_DEBUG_LOG("CREATE TABLE mail_certificate_tbl");
2569
2570                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2571                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2572                 if (error != EMAIL_ERROR_NONE) {
2573                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2574                         goto FINISH_OFF;
2575                 }
2576
2577                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2578         } /*  mail_contact_sync_tbl */
2579         else if (type == EMAIL_CREATE_DB_CHECK) {
2580                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], NULL, NULL, NULL), rc);
2581                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], rc, sqlite3_errmsg(local_db_handle)));
2582         }
2583
2584         sqlite3_free_table(result);
2585         result = NULL;
2586
2587         /*  create mail_task_tbl */
2588         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_task_tbl';";
2589         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2590         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)));
2591
2592         if (atoi(result[1]) < 1) {
2593                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2594                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2595
2596                 EM_DEBUG_LOG("CREATE TABLE mail_task_tbl");
2597
2598                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TASK_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
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                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_TASK_IDX]);
2606                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2607                 if (error != EMAIL_ERROR_NONE) {
2608                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2609                         goto FINISH_OFF;
2610                 }
2611
2612                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2613         } /*  mail_task_tbl */
2614         else if (type == EMAIL_CREATE_DB_CHECK) {
2615                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TASK_TBL], NULL, NULL, NULL), rc);
2616                 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)));
2617         }
2618
2619         sqlite3_free_table(result);
2620         result = NULL;
2621
2622 #ifdef __FEATURE_BODY_SEARCH__
2623         /*  create mail_text_tbl */
2624         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_text_tbl';";
2625         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2626         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)));
2627
2628         if (atoi(result[1]) < 1) {
2629                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2630                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2631
2632                 EM_DEBUG_LOG("CREATE TABLE mail_text_tbl");
2633
2634                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TEXT_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2635                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2636                 if (error != EMAIL_ERROR_NONE) {
2637                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2638                         goto FINISH_OFF;
2639                 }
2640
2641                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2642         } /*  mail_text_tbl */
2643         else if (type == EMAIL_CREATE_DB_CHECK) {
2644                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TEXT_TBL], NULL, NULL, NULL), rc);
2645                 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)));
2646         }
2647
2648         sqlite3_free_table(result);
2649         result = NULL;
2650
2651 #endif
2652
2653 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
2654         /*  create mail_auto_download_activity_tbl */
2655         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_auto_download_activity_tbl';";
2656         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2657         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)));
2658
2659         if (atoi(result[1]) < 1) {
2660                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2661                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2662
2663                 EM_DEBUG_LOG("CREATE TABLE mail_auto_download_activity_tbl");
2664
2665                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], sizeof(sql_query_string)-1);
2666                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2667                 if (error != EMAIL_ERROR_NONE) {
2668                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2669                         goto FINISH_OFF;
2670                 }
2671
2672                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2673         } /*  mail_auto_download_activity_tbl */
2674         else if (type == EMAIL_CREATE_DB_CHECK) {
2675                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2676                 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)));
2677         }
2678
2679         sqlite3_free_table(result);
2680         result = NULL;
2681 #endif
2682
2683
2684         ret = true;
2685
2686 FINISH_OFF:
2687
2688         if (result) sqlite3_free_table(result);
2689
2690         if (ret == true) {
2691                 EMSTORAGE_PROTECTED_FUNC_CALL(
2692                         sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2693         } else {
2694                 EMSTORAGE_PROTECTED_FUNC_CALL(
2695                         sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
2696         }
2697
2698 FINISH_OFF2:
2699         if (create_table_query) {
2700                 int i = 0;
2701                 for (i = 0; i < query_len; i++) {
2702                         if (create_table_query[i])
2703                                 EM_SAFE_FREE(create_table_query[i]);
2704                 }
2705                 EM_SAFE_FREE(create_table_query);
2706         }
2707
2708         if (err_code != NULL)
2709                 *err_code = error;
2710
2711         EM_DEBUG_FUNC_END("ret [%d]", ret);
2712         return ret;
2713 }
2714
2715 /* Query series --------------------------------------------------------------*/
2716 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)
2717 {
2718         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);
2719         int rc = -1;
2720         int query_size = 0;
2721         int error = EMAIL_ERROR_NONE;
2722         DB_STMT hStmt = NULL;
2723         char *sql_query_string = NULL;
2724         char **result;
2725         sqlite3 *local_db_handle = NULL;
2726
2727         if (!input_conditional_clause || (!output_total_mail_count && !output_unseen_mail_count)) {
2728                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2729                 return EMAIL_ERROR_INVALID_PARAM;
2730         }
2731
2732         query_size = EM_SAFE_STRLEN(input_conditional_clause) + QUERY_SIZE;
2733         sql_query_string = em_malloc(query_size);
2734         if (sql_query_string == NULL) {
2735                 EM_DEBUG_EXCEPTION("em_mallocfailed");
2736                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2737                 goto FINISH_OFF;
2738         }
2739
2740         local_db_handle = emstorage_get_db_connection(multi_user_name);
2741
2742         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2743
2744         SNPRINTF(sql_query_string, query_size, "SELECT COUNT(*) FROM mail_tbl");
2745         EM_SAFE_STRNCAT(sql_query_string, (char*)input_conditional_clause, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
2746
2747         if (output_total_mail_count) {
2748                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
2749                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2750                                 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
2751
2752                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2753                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2754                                 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
2755                 _get_stmt_field_data_int(hStmt, output_total_mail_count, 0);
2756         }
2757
2758         if (output_unseen_mail_count) {
2759                 EM_SAFE_STRNCAT(sql_query_string, " AND flags_seen_field = 0 ", query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
2760
2761                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
2762                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2763                                 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2764
2765                 *output_unseen_mail_count = atoi(result[1]);
2766                 sqlite3_free_table(result);
2767         }
2768
2769 FINISH_OFF:
2770
2771         if (hStmt != NULL) {
2772                 rc = sqlite3_finalize(hStmt);
2773                 if (rc != SQLITE_OK) {
2774                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
2775                         error = EMAIL_ERROR_DB_FAILURE;
2776                 }
2777         }
2778
2779         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2780
2781         EM_SAFE_FREE(sql_query_string);
2782
2783         EM_DEBUG_FUNC_END("error [%d]", error);
2784         return error;
2785 }
2786
2787 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)
2788 {
2789         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);
2790
2791         int      i = 0;
2792         int      count = 0;
2793         int      rc = -1;
2794         int      cur_query = 0;
2795         int      col_index;
2796         int      query_size = 0;
2797         int      error = EMAIL_ERROR_NONE;
2798         int     *result_mail_id_list = NULL;
2799         char   **result = NULL;
2800         char     *sql_query_string = NULL;
2801         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2802
2803         EM_IF_NULL_RETURN_VALUE(input_conditional_clause, EMAIL_ERROR_INVALID_PARAM);
2804         EM_IF_NULL_RETURN_VALUE(output_mail_id_list, EMAIL_ERROR_INVALID_PARAM);
2805         EM_IF_NULL_RETURN_VALUE(output_mail_id_count, EMAIL_ERROR_INVALID_PARAM);
2806
2807         query_size = strlen(input_conditional_clause) + strlen("SELECT mail_id FROM mail_tbl ") + 10;  // 10 is extra space
2808         sql_query_string = em_malloc(query_size);
2809         if (sql_query_string == NULL) {
2810                 EM_DEBUG_EXCEPTION("em_mallocfailed");
2811                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2812                 goto FINISH_OFF;
2813         }
2814
2815         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2816
2817         /* Composing query */
2818         SNPRINTF_OFFSET(sql_query_string, cur_query, query_size, "SELECT mail_id FROM mail_tbl ");
2819         EM_SAFE_STRNCAT(sql_query_string, (char*)input_conditional_clause, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
2820
2821         EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
2822
2823         /* Performing query */
2824         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2825         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2826                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2827
2828         col_index = 1;
2829
2830         /*  to get mail list */
2831         if (count == 0) {
2832                 EM_DEBUG_LOG("No mail found...");
2833                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2834                 goto FINISH_OFF;
2835         }
2836
2837         EM_DEBUG_LOG("There are [%d] mails.", count);
2838
2839         if (!(result_mail_id_list = (int*)em_malloc(sizeof(int) * count))) {
2840                 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
2841                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2842                 goto FINISH_OFF;
2843         }
2844
2845         EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2846
2847         for (i = 0; i < count; i++)
2848                 _get_table_field_data_int(result, result_mail_id_list + i, col_index++);
2849
2850         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >> ", count);
2851
2852         *output_mail_id_list  = result_mail_id_list;
2853         *output_mail_id_count = count;
2854
2855 FINISH_OFF:
2856
2857         if (result)
2858                 sqlite3_free_table(result);
2859
2860         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2861
2862         EM_SAFE_FREE(sql_query_string);
2863
2864         if (error != EMAIL_ERROR_NONE)
2865                 EM_SAFE_FREE(result_mail_id_list);
2866
2867         EM_DEBUG_FUNC_END("error [%d]", error);
2868         return error;
2869 }
2870
2871 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)
2872 {
2873         EM_DEBUG_FUNC_BEGIN();
2874         EM_PROFILE_BEGIN(emstorage_query_mail_list_func);
2875
2876         int i = 0, count = 0, rc = -1, to_get_count = (result_mail_list) ? 0 : 1;
2877         int sql_query_string_length = 0;
2878         int local_inline_content_count = 0, local_attachment_count = 0;
2879         int cur_query = 0, base_count = 0, col_index;
2880         int ret = false, error = EMAIL_ERROR_NONE;
2881         char *date_time_string = NULL;
2882         char **result = NULL;
2883         char *field_mail_id = "mail_id";
2884         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 ";
2885         char *select_query_form = "SELECT %s FROM mail_tbl ";
2886         char *target_field = NULL;
2887         char *sql_query_string = NULL;
2888         email_mail_list_item_t *mail_list_item_from_tbl = NULL;
2889         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2890
2891         EM_IF_NULL_RETURN_VALUE(conditional_clause, false);
2892         EM_IF_NULL_RETURN_VALUE(result_count, false);
2893
2894         EMSTORAGE_START_READ_TRANSACTION(transaction);
2895
2896         /*  select clause */
2897         if (to_get_count) /*  count only */
2898                 target_field = field_mail_id;
2899         else /* mail list in plain form */
2900                 target_field = field_all;
2901
2902         sql_query_string_length = EM_SAFE_STRLEN(select_query_form) + EM_SAFE_STRLEN(target_field) + EM_SAFE_STRLEN(conditional_clause);
2903
2904         if (sql_query_string_length)
2905                 sql_query_string = em_malloc(sql_query_string_length);
2906
2907         if (sql_query_string == NULL) {
2908                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
2909                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2910                 goto FINISH_OFF;
2911         }
2912
2913         cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, sql_query_string_length, select_query_form, target_field);
2914
2915         strncat(sql_query_string, conditional_clause, sql_query_string_length - cur_query);
2916
2917         EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
2918
2919         /*  performing query */
2920         EM_PROFILE_BEGIN(emstorage_query_mail_list_performing_query);
2921         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2922         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2923                         ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2924         EM_PROFILE_END(emstorage_query_mail_list_performing_query);
2925
2926         if (!base_count)
2927                 base_count = ({
2928                                 int i = 0;
2929                                 char *tmp = NULL;
2930                                 for (tmp = field_all; tmp && *(tmp + 1); tmp = index(tmp + 1, ',')) i++ ;
2931                                 i;
2932                                 });
2933
2934         col_index = base_count;
2935
2936         EM_DEBUG_LOG_DEV("base_count [%d]", base_count);
2937
2938         if (to_get_count) {
2939                 /*  to get count */
2940                 if (!count) {
2941                         EM_DEBUG_LOG_DEV("No mail found...");
2942                         ret = false;
2943                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
2944                         goto FINISH_OFF;
2945                 }
2946                 EM_DEBUG_LOG_DEV("There are [%d] mails.", count);
2947         } else {
2948                 /*  to get mail list */
2949                 if (!count) {
2950                         EM_DEBUG_LOG_DEV("No mail found...");
2951                         ret = false;
2952                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
2953                         goto FINISH_OFF;
2954                 }
2955
2956                 EM_DEBUG_LOG_DEV("There are [%d] mails.", count);
2957                 if (!(mail_list_item_from_tbl = (email_mail_list_item_t*)em_malloc(sizeof(email_mail_list_item_t) * count))) {
2958                         EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
2959                         error = EMAIL_ERROR_OUT_OF_MEMORY;
2960                         goto FINISH_OFF;
2961                 }
2962
2963                 EM_PROFILE_BEGIN(emstorage_query_mail_list_loop);
2964                 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >> ");
2965                 for (i = 0; i < count; i++) {
2966                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_id), col_index++);
2967                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].account_id), col_index++);
2968                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mailbox_id), col_index++);
2969                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].mailbox_type), col_index++);
2970                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].full_address_from, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2971                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_sender, MAX_EMAIL_ADDRESS_LENGTH, 1, col_index++);
2972                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_recipient, STRING_LENGTH_FOR_DISPLAY,  1, col_index++);
2973                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].subject, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2974                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].body_download_status), col_index++);
2975                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_size), col_index++);
2976                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_seen_field), col_index++);
2977                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_deleted_field), col_index++);
2978                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_flagged_field), col_index++);
2979                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_answered_field), col_index++);
2980                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_recent_field), col_index++);
2981                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_draft_field), col_index++);
2982                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_forwarded_field), col_index++);
2983                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].DRM_status), col_index++);
2984                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].priority), col_index++);
2985                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].save_status), col_index++);
2986                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].lock_status), col_index++);
2987                         _get_table_field_data_int(result, &local_attachment_count, col_index++);
2988                         _get_table_field_data_int(result, &local_inline_content_count, col_index++);
2989                         _get_table_field_data_time_t(result, &(mail_list_item_from_tbl[i].date_time), col_index++);
2990                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].preview_text, MAX_PREVIEW_TEXT_LENGTH, 1, col_index++);
2991                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_id), col_index++);
2992                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_item_count), col_index++);
2993                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].meeting_request_status), col_index++);
2994                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].message_class), col_index++);
2995                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].smime_type), col_index++);
2996                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].scheduled_sending_time), col_index++);
2997                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].remaining_resend_times), col_index++);
2998                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].tag_id), col_index++);
2999                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].eas_data_length), col_index++);
3000                         _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++);
3001
3002                         mail_list_item_from_tbl[i].attachment_count = (local_attachment_count > 0) ? 1 : 0;
3003                 }
3004                 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >> ", count);
3005                 EM_PROFILE_END(emstorage_query_mail_list_loop);
3006         }
3007
3008         ret = true;
3009
3010 FINISH_OFF:
3011         EM_DEBUG_LOG("MAIL_COUNT [%d]", count);
3012
3013         if (result)
3014                 sqlite3_free_table(result);
3015
3016         if (to_get_count)
3017                 *result_count = count;
3018         else {
3019                 if (ret == true) {
3020                         if (result_mail_list)
3021                                 *result_mail_list = mail_list_item_from_tbl;
3022                         else
3023                                 EM_SAFE_FREE(mail_list_item_from_tbl);
3024                         *result_count = count;
3025                 } else
3026                         EM_SAFE_FREE(mail_list_item_from_tbl);
3027         }
3028
3029         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3030
3031         //      sqlite3_db_release_memory(local_db_handle);
3032
3033         EM_SAFE_FREE(sql_query_string);
3034         EM_SAFE_FREE(date_time_string);
3035
3036         if (err_code != NULL)
3037                 *err_code = error;
3038
3039         EM_PROFILE_END(emstorage_query_mail_list_func);
3040         EM_DEBUG_FUNC_END("ret [%d]", ret);
3041         return ret;
3042 }
3043
3044
3045 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)
3046 {
3047         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);
3048
3049         if (!conditional_clause || !result_mail_tbl || !result_count) {
3050                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: [%p] [%p] [%p]", conditional_clause, result_mail_tbl, result_mail_tbl);
3051                 if (err_code != NULL)
3052                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3053                 return false;
3054         }
3055
3056         int i, col_index = FIELD_COUNT_OF_MAIL_TBL, rc, ret = false, count;
3057         int error = EMAIL_ERROR_NONE;
3058         char **result = NULL, sql_query_string[QUERY_SIZE] = {0, };
3059         emstorage_mail_tbl_t* p_data_tbl = NULL;
3060         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3061
3062         EMSTORAGE_START_READ_TRANSACTION(transaction);
3063
3064         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl %s", conditional_clause);
3065
3066         EM_DEBUG_LOG_DEV("Query[%s]", sql_query_string);
3067
3068         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3069         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3070                         ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
3071
3072         if (!count) {
3073                 EM_DEBUG_LOG("No mail found...");
3074                 ret = false;
3075                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
3076                 goto FINISH_OFF;
3077         }
3078
3079         EM_DEBUG_LOG("There are [%d] mails.", count);
3080         if (!(p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * count))) {
3081                 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
3082                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3083                 goto FINISH_OFF;
3084         }
3085
3086         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >> ");
3087         for (i = 0; i < count; i++) {
3088                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
3089                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3090                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3091                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_type), col_index++);
3092                 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 1, col_index++);
3093                 _get_table_field_data_time_t (result, &(p_data_tbl[i].date_time), col_index++);
3094                 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
3095                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
3096                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
3097                 _get_table_field_data_string(result, &(p_data_tbl[i].message_id), 0, col_index++);
3098                 _get_table_field_data_int(result, &(p_data_tbl[i].reference_mail_id), col_index++);
3099                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_from), 1, col_index++);
3100                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_reply), 1, col_index++);
3101                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_to), 1, col_index++);
3102                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_cc), 1, col_index++);
3103                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_bcc), 1, col_index++);
3104                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_return), 1, col_index++);
3105                 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_sender), 1, col_index++);
3106                 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_recipient), 1, col_index++);
3107                 _get_table_field_data_string(result, &(p_data_tbl[i].alias_sender), 1, col_index++);
3108                 _get_table_field_data_string(result, &(p_data_tbl[i].alias_recipient), 1, col_index++);
3109                 _get_table_field_data_int(result, &(p_data_tbl[i].body_download_status), col_index++);
3110                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
3111                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
3112                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
3113                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_size), col_index++);
3114                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_seen_field), col_index++);
3115                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_deleted_field), col_index++);
3116                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_flagged_field), col_index++);
3117                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_answered_field), col_index++);
3118                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_recent_field), col_index++);
3119                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_draft_field), col_index++);
3120                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_forwarded_field), col_index++);
3121                 _get_table_field_data_int(result, &(p_data_tbl[i].DRM_status), col_index++);
3122                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].priority), col_index++);
3123                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
3124                 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
3125                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].report_status), col_index++);
3126                 _get_table_field_data_int(result, &(p_data_tbl[i].attachment_count), col_index++);
3127                 _get_table_field_data_int(result, &(p_data_tbl[i].inline_content_count), col_index++);
3128                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
3129                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
3130                 _get_table_field_data_string(result, &(p_data_tbl[i].preview_text), 1, col_index++);
3131                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].meeting_request_status), col_index++);
3132                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].message_class), col_index++);
3133                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].digest_type), col_index++);
3134                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].smime_type), col_index++);
3135                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].scheduled_sending_time), col_index++);
3136                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].remaining_resend_times), col_index++);
3137                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].tag_id), col_index++);
3138                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].replied_time), col_index++);
3139                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].forwarded_time), col_index++);
3140                 _get_table_field_data_string(result, &(p_data_tbl[i].default_charset), 0, col_index++);
3141                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3142                 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3143                 _get_table_field_data_string(result, &(p_data_tbl[i].user_name), 0, col_index++);
3144         }
3145
3146         ret = true;
3147
3148 FINISH_OFF:
3149         if (result)
3150                 sqlite3_free_table(result);
3151
3152         if (ret) {
3153                 *result_mail_tbl = p_data_tbl;
3154                 *result_count = count;
3155         } else {
3156                 *result_mail_tbl = NULL;
3157                 *result_count = 0;
3158         }
3159
3160         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3161
3162         //      sqlite3_db_release_memory(local_db_handle);
3163
3164         if (err_code != NULL)
3165                 *err_code = error;
3166
3167         EM_DEBUG_FUNC_END("ret [%d]", ret);
3168         return ret;
3169 }
3170
3171 #ifdef __FEATURE_BODY_SEARCH__
3172 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)
3173 {
3174         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);
3175
3176         if (!conditional_clause || !result_mail_text_tbl || !result_count) { /*prevent 50930*/
3177                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3178                 if (err_code != NULL)
3179                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3180                 return false;
3181         }
3182
3183         int i = 0;
3184         int rc = 0;
3185         int ret = false;
3186         int count = 0;
3187         int col_index = FIELD_COUNT_OF_MAIL_TEXT_TBL;
3188         int error = EMAIL_ERROR_NONE;
3189         char **result = NULL;
3190         char sql_query_string[QUERY_SIZE] = {0, };
3191         emstorage_mail_text_tbl_t* p_data_tbl = NULL;
3192         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3193
3194         EMSTORAGE_START_READ_TRANSACTION(transaction);
3195
3196         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_text_tbl %s", conditional_clause);
3197
3198         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
3199
3200         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3201         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3202                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3203
3204         if (!count) {
3205                 EM_DEBUG_LOG("No mail found...");
3206                 ret = false;
3207                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
3208                 goto FINISH_OFF;
3209         }
3210
3211         EM_DEBUG_LOG("There are [%d] mails.", count);
3212         if (!(p_data_tbl = (emstorage_mail_text_tbl_t *)em_malloc(sizeof(emstorage_mail_text_tbl_t) * count))) {
3213                 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_text_tbl_t failed...");
3214                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3215                 goto FINISH_OFF;
3216         }
3217
3218         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
3219         for (i = 0; i < count; i++) {
3220                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
3221                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3222                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3223                 _get_table_field_data_string(result, &(p_data_tbl[i].body_text), 0, col_index++);
3224         }
3225
3226         ret = true;
3227
3228 FINISH_OFF:
3229         if (result)
3230                 sqlite3_free_table(result);
3231
3232         if (ret == true) {
3233                 *result_mail_text_tbl = p_data_tbl;
3234                 *result_count = count;
3235         } else
3236                 EM_SAFE_FREE(p_data_tbl);
3237
3238         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3239
3240         //      sqlite3_db_release_memory(local_db_handle);
3241
3242         if (err_code != NULL)
3243                 *err_code = error;
3244
3245         EM_DEBUG_FUNC_END("ret [%d]", ret);
3246         return ret;
3247 }
3248 #endif
3249
3250 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)
3251 {
3252         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);
3253
3254         int i = 0;
3255         int rc;
3256         int count = 0;
3257         int col_index = 0;
3258         int error = EMAIL_ERROR_NONE;
3259         int read_count = 0;
3260         int total_count = 0;
3261         char **result;
3262         char sql_query_string[QUERY_SIZE] = {0, };
3263         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 ";
3264         emstorage_mailbox_tbl_t* p_data_tbl = NULL;
3265         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3266
3267         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
3268
3269         if (input_get_mail_count == 0) {        /* without mail count */
3270                 col_index = 15;
3271                 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);
3272         } else {        /* with read count and total count */
3273                 col_index = 17;
3274                 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);
3275         }
3276
3277         EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
3278
3279         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
3280         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3281                         ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string))
3282
3283                 EM_DEBUG_LOG_DEV("result count [%d]", count);
3284
3285         if (count == 0) {
3286                 EM_DEBUG_LOG_SEC("Can't find mailbox query[%s]", sql_query_string);
3287                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
3288                 goto FINISH_OFF;
3289         }
3290
3291         if ((p_data_tbl = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t) * count)) == NULL) {
3292                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
3293                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3294                 goto FINISH_OFF;
3295         }
3296
3297         for (i = 0; i < count; i++) {
3298                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3299                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3300                 _get_table_field_data_int(result, &(p_data_tbl[i].local_yn), col_index++);
3301                 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
3302                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].mailbox_type), col_index++);
3303                 _get_table_field_data_string(result, &(p_data_tbl[i].alias), 0, col_index++);
3304                 _get_table_field_data_int(result, &(p_data_tbl[i].deleted_flag), col_index++);
3305                 _get_table_field_data_int(result, &(p_data_tbl[i].modifiable_yn), col_index++);
3306                 _get_table_field_data_int(result, &(p_data_tbl[i].total_mail_count_on_server), col_index++);
3307                 _get_table_field_data_int(result, &(p_data_tbl[i].has_archived_mails), col_index++);
3308                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_slot_size), col_index++);
3309                 _get_table_field_data_int(result, &(p_data_tbl[i].no_select), col_index++);
3310                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].last_sync_time), col_index++);
3311                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3312                 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3313
3314                 if (input_get_mail_count == 1) {
3315                         _get_table_field_data_int(result, &(total_count), col_index++);
3316                         p_data_tbl[i].total_mail_count_on_local = total_count;
3317                         _get_table_field_data_int(result, &(read_count), col_index++);
3318                         p_data_tbl[i].unread_count = total_count - read_count;                  /*  return unread count, NOT  */
3319                 }
3320         }
3321
3322
3323 FINISH_OFF:
3324         if (result)
3325                 sqlite3_free_table(result);
3326
3327         if (error == EMAIL_ERROR_NONE) {
3328                 *output_mailbox_list = p_data_tbl;
3329                 *output_mailbox_count = count;
3330                 EM_DEBUG_LOG("Mailbox Count [%d]", count);
3331         }
3332
3333         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
3334
3335         //      sqlite3_db_release_memory(local_db_handle);
3336
3337         EM_DEBUG_FUNC_END("error [%d]", error);
3338         return error;
3339 }
3340
3341 /* Query series --------------------------------------------------------------*/
3342 INTERNAL_FUNC int emstorage_check_duplicated_account(char *multi_user_name, email_account_t* account, int transaction, int *err_code)
3343 {
3344         EM_DEBUG_FUNC_BEGIN();
3345         int rc = -1, ret = false;
3346         int error = EMAIL_ERROR_NONE;
3347         char **result;
3348         int count;
3349         char sql_query_string[QUERY_SIZE] = {0, };
3350         sqlite3 *local_db_handle = NULL;
3351
3352         if (account == NULL || account->user_email_address == NULL || account->incoming_server_user_name == NULL || account->incoming_server_address == NULL ||
3353                         account->outgoing_server_user_name == NULL || account->outgoing_server_address == NULL) {
3354                 if (err_code != NULL)
3355                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3356                 return false;
3357         }
3358
3359         local_db_handle = emstorage_get_db_connection(multi_user_name);
3360
3361         EMSTORAGE_START_READ_TRANSACTION(transaction);
3362
3363         SNPRINTF(sql_query_string, sizeof(sql_query_string),
3364                         "SELECT COUNT(*) FROM mail_account_tbl "
3365                         " WHERE "
3366                         " user_email_address = '%s' AND "
3367                         " incoming_server_user_name = '%s' AND "
3368                         " incoming_server_type = %d AND "
3369                         " incoming_server_address = '%s' AND "
3370                         " outgoing_server_user_name = '%s' AND "
3371                         " outgoing_server_type = %d AND "
3372                         " outgoing_server_address = '%s'; ",
3373                         account->user_email_address,
3374                         account->incoming_server_user_name, account->incoming_server_type, account->incoming_server_address,
3375                         account->outgoing_server_user_name, account->outgoing_server_type, account->outgoing_server_address
3376                         );
3377         EM_DEBUG_LOG_SEC("Query[%s]", sql_query_string);
3378         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
3379         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3380                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3381
3382         count = atoi(result[1]);
3383         sqlite3_free_table(result);
3384
3385         EM_DEBUG_LOG("Count of Duplicated Account Information: [%d]", count);
3386
3387         if (count == 0) {       /*  not duplicated account */
3388                 ret = true;
3389                 EM_DEBUG_LOG_SEC("NOT duplicated account: user_email_address[%s]", account->user_email_address);
3390         } else {        /*  duplicated account */
3391                 ret = false;
3392                 EM_DEBUG_LOG_SEC("The same account already exists. Duplicated account: user_email_address[%s]", account->user_email_address);
3393                 error = EMAIL_ERROR_ALREADY_EXISTS;
3394         }
3395
3396 FINISH_OFF:
3397
3398         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3399
3400         if (err_code != NULL)
3401                 *err_code = error;
3402
3403         EM_DEBUG_FUNC_END("ret [%d]", ret);
3404         return ret;
3405
3406 }
3407
3408 INTERNAL_FUNC int emstorage_get_account_count(char *multi_user_name, int *count, int transaction, int *err_code)
3409 {
3410         EM_DEBUG_FUNC_BEGIN();
3411
3412         if (!count) {
3413                 if (err_code != NULL)
3414                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3415                 return false;
3416         }
3417
3418         int rc = -1, ret = false;
3419         int error = EMAIL_ERROR_NONE;
3420
3421         DB_STMT hStmt = NULL;
3422         char sql_query_string[QUERY_SIZE] = {0, };
3423
3424
3425         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3426         EMSTORAGE_START_READ_TRANSACTION(transaction);
3427         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_account_tbl");
3428         EM_DEBUG_LOG_SEC("SQL STMT [%s]", sql_query_string);
3429         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string,
3430                                 EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3431         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
3432         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3433                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3434
3435         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3436         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3437                         ("sqlite3_step fail:%d", rc));
3438
3439         *count = sqlite3_column_int(hStmt, 0);
3440
3441         ret = true;
3442
3443 FINISH_OFF:
3444
3445         if (hStmt != NULL) {
3446                 rc = sqlite3_finalize(hStmt);
3447                 hStmt = NULL;
3448                 if (rc != SQLITE_OK) {
3449                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3450                         error = EMAIL_ERROR_DB_FAILURE;
3451                 }
3452         }
3453
3454         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3455
3456         if (err_code != NULL)
3457                 *err_code = error;
3458
3459         EM_DEBUG_FUNC_END("ret [%d]", ret);
3460         return ret;
3461 }
3462
3463 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)
3464 {
3465         EM_DEBUG_FUNC_BEGIN();
3466
3467         int i = 0, count = 0, rc = -1, ret = false;
3468         int field_index = 0;
3469         int sql_len = 0;
3470         int error = EMAIL_ERROR_NONE;
3471         emstorage_account_tbl_t *p_data_tbl = NULL;
3472         char sql_query_string[QUERY_SIZE] = {0, };
3473         char *sql = "SELECT count(*) FROM mail_account_tbl;";
3474         char **result;
3475         sqlite3 *local_db_handle = NULL;
3476         DB_STMT hStmt = NULL;
3477
3478         if (!select_num || !account_list) {
3479                 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, account_list);
3480                 if (err_code != NULL)
3481                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3482                 return false;
3483         }
3484
3485         local_db_handle = emstorage_get_db_connection(multi_user_name);
3486
3487         EMSTORAGE_START_READ_TRANSACTION(transaction);
3488
3489         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
3490         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3491                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
3492
3493         count = atoi(result[1]);
3494         sqlite3_free_table(result);
3495
3496         EM_DEBUG_LOG_DEV("count = %d", rc);
3497
3498         if (count <= 0) {
3499                 EM_DEBUG_LOG("no account found...");
3500                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3501                 ret = true;
3502                 goto FINISH_OFF;
3503         }
3504         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
3505         sql_len = EM_SAFE_STRLEN(sql_query_string);
3506
3507         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
3508                         "account_id, "
3509                         "account_name, "
3510                         "logo_icon_path, "
3511                         "user_data, "
3512                         "user_data_length, "
3513                         "account_svc_id, "
3514                         "sync_status, "
3515                         "sync_disabled, "
3516                         "default_mail_slot_size, "
3517                         "roaming_option, "
3518                         "color_label, "
3519                         "user_display_name, "
3520                         "user_email_address, "
3521                         "reply_to_address, "
3522                         "return_address, "
3523                         "incoming_server_type, "
3524                         "incoming_server_address, "
3525                         "incoming_server_port_number, "
3526                         "incoming_server_user_name, "
3527                         "incoming_server_password, "
3528                         "incoming_server_secure_connection, "
3529                         "incoming_server_authentication_method, "
3530                         "retrieval_mode, "
3531                         "keep_mails_on_pop_server_after_download, "
3532                         "check_interval, "
3533                         "auto_download_size, "
3534                         "peak_interval, "
3535                         "peak_days, "
3536                         "peak_start_time, "
3537                         "peak_end_time, "
3538                         "outgoing_server_type, "
3539                         "outgoing_server_address, "
3540                         "outgoing_server_port_number, "
3541                         "outgoing_server_user_name, "
3542                         "outgoing_server_password, "
3543                         "outgoing_server_secure_connection, "
3544                         "outgoing_server_need_authentication, "
3545                         "outgoing_server_use_same_authenticator, "
3546                         "priority, "
3547                         "keep_local_copy, "
3548                         "req_delivery_receipt, "
3549                         "req_read_receipt, "
3550                         "download_limit, "
3551                         "block_address, "
3552                         "block_subject, "
3553                         "display_name_from, "
3554                         "reply_with_body, "
3555                         "forward_with_files, "
3556                         "add_myname_card, "
3557                         "add_signature, "
3558                         "signature, "
3559                         "add_my_address_to_bcc, "
3560                         "notification_status, "
3561                         "vibrate_status, "
3562                         "display_content_status, "
3563                         "default_ringtone_status, "
3564                         "alert_ringtone_path, "
3565                         "auto_resend_times, "
3566                         "outgoing_server_size_limit, "
3567                         "wifi_auto_download, "
3568                         "pop_before_smtp, "
3569                         "incoming_server_requires_apop, "
3570                         "smime_type, "
3571                         "certificate_path, "
3572                         "cipher_type, "
3573                         "digest_type"
3574                         );
3575
3576         sql_len = EM_SAFE_STRLEN(sql_query_string);
3577
3578         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " FROM mail_account_tbl ORDER BY account_id");
3579
3580         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3581
3582         EM_DEBUG_LOG_DEV("After sqlite3_prepare_v2 hStmt = %p", hStmt);
3583         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3584                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3585
3586         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3587         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3588                         ("sqlite3_step fail:%d", rc));
3589         if (rc == SQLITE_DONE) {
3590                 EM_DEBUG_EXCEPTION("no account found...");
3591
3592                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3593                 count = 0;
3594                 ret = true;
3595                 goto FINISH_OFF;
3596         }
3597
3598         if (!(p_data_tbl = (emstorage_account_tbl_t*)malloc(sizeof(emstorage_account_tbl_t) * count))) {
3599                 EM_DEBUG_EXCEPTION("malloc failed...");
3600                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3601                 goto FINISH_OFF;
3602         }
3603         memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * count);
3604         for (i = 0; i < count; i++) {
3605                 /*  get recordset */
3606                 field_index = 0;
3607                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_id), field_index++);
3608                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].account_name), 0, field_index++);
3609                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].logo_icon_path), 0, field_index++);
3610                 _get_stmt_field_data_blob(hStmt, &(p_data_tbl[i].user_data), field_index++);
3611                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].user_data_length), field_index++);
3612                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_svc_id), field_index++);
3613                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_status), field_index++);
3614                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_disabled), field_index++);
3615                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].default_mail_slot_size), field_index++);
3616                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].roaming_option), field_index++);
3617                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].color_label), field_index++);
3618                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_display_name), 0, field_index++);
3619                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_email_address), 0, field_index++);
3620                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].reply_to_address), 0, field_index++);
3621                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].return_address), 0, field_index++);
3622                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].incoming_server_type), field_index++);
3623                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_address), 0, field_index++);
3624                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_port_number), field_index++);
3625                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_user_name), 0, field_index++);
3626                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_password), 0, field_index++);
3627                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_secure_connection), field_index++);
3628                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].incoming_server_authentication_method), field_index++);
3629                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].retrieval_mode), field_index++);
3630                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].keep_mails_on_pop_server_after_download), field_index++);
3631                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].check_interval), field_index++);
3632                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_download_size), field_index++);
3633                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_interval), field_index++);
3634                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_days), field_index++);
3635                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_start_time), field_index++);
3636                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_end_time), field_index++);
3637                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].outgoing_server_type), field_index++);
3638                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_address), 0, field_index++);
3639                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_port_number), field_index++);
3640                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_user_name), 0, field_index++);
3641                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_password), 0, field_index++);
3642                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_secure_connection), field_index++);
3643                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_need_authentication), field_index++);
3644                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_use_same_authenticator), field_index++);
3645                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.priority), field_index++);
3646                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.keep_local_copy), field_index++);
3647                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_delivery_receipt), field_index++);
3648                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_read_receipt), field_index++);
3649                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.download_limit), field_index++);
3650                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_address), field_index++);
3651                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_subject), field_index++);
3652                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.display_name_from), 0, field_index++);
3653                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.reply_with_body), field_index++);
3654                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.forward_with_files), field_index++);
3655                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_myname_card), field_index++);
3656                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_signature), field_index++);
3657                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.signature), 0, field_index++);
3658                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.add_my_address_to_bcc), field_index++);
3659                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.notification_status), field_index++);
3660                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.vibrate_status), field_index++);
3661                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.display_content_status), field_index++);
3662                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.default_ringtone_status), field_index++);
3663                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.alert_ringtone_path), 0, field_index++);
3664                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_resend_times), field_index++);
3665                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_size_limit), field_index++);
3666                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].wifi_auto_download), field_index++);
3667                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].pop_before_smtp), field_index++);
3668                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_requires_apop), field_index++);
3669                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].smime_type), field_index++);
3670                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].certificate_path), 0, field_index++);
3671                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].cipher_type), field_index++);
3672                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].digest_type), field_index++);
3673
3674                 /* EAS passwd is not accessible */
3675                 if (with_password == true && p_data_tbl[i].incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
3676                         /*  get password from the secure storage */
3677                         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3678                         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3679
3680                         EM_SAFE_FREE(p_data_tbl[i].incoming_server_password);
3681                         EM_SAFE_FREE(p_data_tbl[i].outgoing_server_password);
3682
3683                         /*  get password file name */
3684                         error = _get_password_file_name(multi_user_name, p_data_tbl[i].account_id,
3685                                         recv_password_file_name,
3686                                         send_password_file_name);
3687                         if (error != EMAIL_ERROR_NONE) {
3688                                 EM_DEBUG_EXCEPTION("_get_password_file_name error [%d]", error);
3689                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3690                                 goto FINISH_OFF;
3691                         }
3692
3693                         /*  read password from secure storage */
3694                         error = _read_password_from_secure_storage(recv_password_file_name,
3695                                         &(p_data_tbl[i].incoming_server_password));
3696                         if (error < 0) {
3697                                 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s] error [%d]",
3698                                                 recv_password_file_name, error);
3699                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3700                                 goto FINISH_OFF;
3701                         }
3702                         error = _read_password_from_secure_storage(send_password_file_name,
3703                                         &(p_data_tbl[i].outgoing_server_password));
3704                         if (error < 0) {
3705                                 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s]  error [%d]",
3706                                                 send_password_file_name, error);
3707                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3708                                 goto FINISH_OFF;
3709                         }
3710                 }
3711
3712                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3713                 EM_DEBUG_LOG_DEV("after sqlite3_step(), i = %d, rc = %d.", i,  rc);
3714                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3715                                 ("sqlite3_step fail:%d", rc));
3716         }
3717
3718         ret = true;
3719
3720 FINISH_OFF:
3721         if (ret == true) {
3722                 *account_list = p_data_tbl;
3723                 *select_num = count;
3724                 EM_DEBUG_LOG_DEV("COUNT : %d", count);
3725         } else if (p_data_tbl != NULL)
3726                 emstorage_free_account(&p_data_tbl, count, NULL);
3727
3728         if (hStmt != NULL) {
3729                 rc = sqlite3_finalize(hStmt);
3730                 hStmt = NULL;
3731                 if (rc != SQLITE_OK) {
3732                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3733                         error = EMAIL_ERROR_DB_FAILURE;
3734                 }
3735         }
3736
3737         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3738
3739         if (err_code != NULL)
3740                 *err_code = error;
3741
3742         EM_DEBUG_FUNC_END("ret [%d]", ret);
3743         return ret;
3744 }
3745
3746 INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(char *multi_user_name,
3747                 char *server_mail_id,
3748                 int mailbox_id,
3749                 emstorage_mail_tbl_t **mail,
3750                 int transaction,
3751                 int *err_code)
3752 {
3753         EM_DEBUG_FUNC_BEGIN_SEC("mailbox_id [%d], server_mail_id[%s], mail[%p], transaction[%d], err_code[%p]",
3754                         mailbox_id, server_mail_id, mail, transaction, err_code);
3755
3756         int ret = false, error = EMAIL_ERROR_NONE, result_count;
3757         char conditional_clause[QUERY_SIZE] = {0, };
3758         emstorage_mail_tbl_t* p_data_tbl = NULL;
3759
3760         if (!server_mail_id || !mail) {
3761                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3762                 error = EMAIL_ERROR_INVALID_PARAM;
3763                 goto FINISH_OFF;
3764         }
3765
3766         sqlite3_snprintf(sizeof(conditional_clause), conditional_clause, "WHERE server_mail_id = '%q'", server_mail_id);
3767
3768         if (mailbox_id > 0) {
3769                 SNPRINTF(conditional_clause + strlen(conditional_clause),
3770                                 QUERY_SIZE - strlen(conditional_clause),
3771                                 " AND mailbox_id = %d",
3772                                 mailbox_id);
3773         }
3774
3775         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
3776         if (!emstorage_query_mail_tbl(multi_user_name,
3777                                 conditional_clause,
3778                                 transaction,
3779                                 &p_data_tbl,
3780                                 &result_count,
3781                                 &error)) {
3782                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3783                 goto FINISH_OFF;
3784         }
3785
3786         ret = true;
3787
3788 FINISH_OFF:
3789
3790         if (ret == true)
3791                 *mail = p_data_tbl;
3792
3793         if (err_code != NULL)
3794                 *err_code = error;
3795
3796         EM_DEBUG_FUNC_END("ret [%d]", ret);
3797         return ret;
3798 }
3799
3800
3801 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)
3802 {
3803         int cur_clause = 0, conditional_clause_count = 0, i = 0;
3804
3805         if (account_id < ALL_ACCOUNT) {
3806                 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3807                 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3808         }
3809
3810         /*  where clause */
3811         if (account_id != ALL_ACCOUNT) {
3812                 cur_clause += (conditional_clause_count++ == 0) ?
3813                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE account_id = %d", account_id) :
3814                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND account_id = %d", account_id);
3815         }
3816
3817         if (mailbox_id > 0) {
3818                 cur_clause += (conditional_clause_count++ == 0) ?
3819                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_id = %d", mailbox_id) :
3820                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_id = %d", mailbox_id);
3821         }
3822
3823         if (thread_id > 0) {
3824                 cur_clause += (conditional_clause_count++ == 0) ?
3825                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_id = %d ", thread_id) :
3826                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_id = %d ", thread_id);
3827         } else if (thread_id == EMAIL_LIST_TYPE_THREAD) {
3828                 cur_clause += (conditional_clause_count++ == 0) ?
3829                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_item_count > 0") :
3830                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_item_count > 0");
3831         } else if (thread_id == EMAIL_LIST_TYPE_LOCAL) {
3832                 cur_clause += (conditional_clause_count++ == 0) ?
3833                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE server_mail_status = 0") :
3834                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND server_mail_status = 0");
3835         } else if (thread_id == EMAIL_LIST_TYPE_UNREAD) {
3836                 cur_clause += (conditional_clause_count++ == 0) ?
3837                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_seen_field == 0") :
3838                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_seen_field == 0");
3839         }
3840
3841         /*  EM_DEBUG_LOG("where clause added [%s]", conditional_clause_string); */
3842         if (addr_list && addr_list->address_count > 0) {
3843                 if (!addr_list->address_type) {
3844                         cur_clause += (conditional_clause_count++ == 0) ?
3845                                 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE email_address_sender IN(\"%s\"", addr_list->address_list[0]) :
3846                                 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND email_address_sender IN(\"%s\"", addr_list->address_list[0]);
3847
3848                         for (i = 1; i < addr_list->address_count; i++)
3849                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3850
3851                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3852                 } else {
3853                         cur_clause += (conditional_clause_count++ == 0) ?
3854                                 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]) :
3855                                 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]);
3856
3857                         for (i = 1; i < addr_list->address_count; i++)
3858                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3859
3860                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3861                 }
3862         }
3863
3864         if (input_except_delete_flagged_mails) {
3865                 cur_clause += (conditional_clause_count++ == 0) ?
3866                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_deleted_field = 0") :
3867                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_deleted_field = 0");
3868         }
3869
3870         if (search_value) {
3871                 switch (search_type) {
3872                 case EMAIL_SEARCH_FILTER_SUBJECT:
3873                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3874                                         " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3875                         break;
3876                 case EMAIL_SEARCH_FILTER_SENDER:
3877                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3878                                         " %s  ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3879                                         ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3880                         break;
3881                 case EMAIL_SEARCH_FILTER_RECIPIENT:
3882                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3883                                         " %s ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3884                                         "       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3885                                         "       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3886                                         ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value);
3887                         break;
3888                 case EMAIL_SEARCH_FILTER_ALL:
3889                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3890                                         " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\' "
3891                                         "       OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3892                                         "                       OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3893                                         "                       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3894                                         "                       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3895                                         "               ) "
3896                                         "       )"
3897                                         ")", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value, search_value, search_value);
3898                         break;
3899                 }
3900         }
3901
3902         /*  EM_DEBUG_LOG("where clause [%s]", conditional_clause_string); */
3903         static char sorting_str[][50] = {
3904                 " ORDER BY date_time DESC",                                              /* case EMAIL_SORT_DATETIME_HIGH: */
3905                 " ORDER BY date_time ASC",                                                /* case EMAIL_SORT_DATETIME_LOW: */
3906                 " ORDER BY full_address_from DESC, date_time DESC", /* case EMAIL_SORT_SENDER_HIGH: */
3907                 " ORDER BY full_address_from ASC, date_time DESC",  /* case EMAIL_SORT_SENDER_LOW: */
3908                 " ORDER BY full_address_to DESC, date_time DESC",   /* case EMAIL_SORT_RCPT_HIGH: */
3909                 " ORDER BY full_address_to ASC, date_time DESC",        /* case EMAIL_SORT_RCPT_LOW: */
3910                 " ORDER BY subject DESC, date_time DESC",                  /* case EMAIL_SORT_SUBJECT_HIGH: */
3911                 " ORDER BY subject ASC, date_time DESC",                        /* case EMAIL_SORT_SUBJECT_LOW: */
3912                 " ORDER BY priority DESC, date_time DESC",                /* case EMAIL_SORT_PRIORITY_HIGH: */
3913                 " ORDER BY priority ASC, date_time DESC",                  /* case EMAIL_SORT_PRIORITY_LOW: */
3914                 " ORDER BY attachment_count DESC, date_time DESC",  /* case EMAIL_SORT_ATTACHMENT_HIGH: */
3915                 " ORDER BY attachment_count ASC, date_time DESC",   /* case EMAIL_SORT_ATTACHMENT_LOW: */
3916                 " ORDER BY lock_status DESC, date_time DESC",      /* case EMAIL_SORT_FAVORITE_HIGH: */
3917                 " ORDER BY lock_status ASC, date_time DESC",            /* case EMAIL_SORT_FAVORITE_LOW: */
3918                 " ORDER BY mailbox_id DESC, date_time DESC",      /* case EMAIL_SORT_MAILBOX_ID_HIGH: */
3919                 " ORDER BY mailbox_id ASC, date_time DESC",             /* case EMAIL_SORT_MAILBOX_ID_LOW: */
3920                 " ORDER BY flags_flagged_field DESC, date_time DESC",  /* case EMAIL_SORT_FLAGGED_FLAG_HIGH: */
3921                 " ORDER BY flags_flagged_field ASC, date_time DESC",   /* case EMAIL_SORT_FLAGGED_FLAG_LOW: */
3922                 " ORDER BY flags_seen_field DESC, date_time DESC",     /* case EMAIL_SORT_SEEN_FLAG_HIGH: */
3923                 " ORDER BY flags_seen_field ASC, date_time DESC"       /* case EMAIL_SORT_SEEN_FLAG_LOW: */
3924         };
3925
3926         if (sorting < EMAIL_SORT_END)
3927                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " %s", sorting_str[sorting]);
3928         else
3929                 EM_DEBUG_LOG(" Invalid Sorting order ");
3930
3931         /*  limit clause */
3932         if (start_index != -1 && limit_count != -1)
3933                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " LIMIT %d, %d", start_index, limit_count);
3934
3935         return true;
3936 }
3937
3938
3939 /**
3940  *      emstorage_get_mail_list - Get the mail list information.
3941  *
3942  *
3943  */
3944 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)
3945 {
3946         EM_DEBUG_FUNC_BEGIN();
3947         EM_PROFILE_BEGIN(emstorage_get_mail_list_func);
3948
3949         int ret = false, error = EMAIL_ERROR_NONE;
3950         char conditional_clause_string[QUERY_SIZE] = { 0, };
3951
3952         if (account_id < ALL_ACCOUNT) {
3953                 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3954                 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3955         }
3956         EM_IF_NULL_RETURN_VALUE(result_count, false);
3957
3958         _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);
3959
3960         EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
3961
3962         if (!emstorage_query_mail_list(multi_user_name, conditional_clause_string, transaction, mail_list, result_count, &error)) {
3963                 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3964                 goto FINISH_OFF;
3965         }
3966
3967         ret = true;
3968
3969 FINISH_OFF:
3970         if (err_code != NULL)
3971                 *err_code = error;
3972
3973         EM_PROFILE_END(emstorage_get_mail_list_func);
3974         EM_DEBUG_FUNC_END("ret [%d]", ret);
3975         return ret;
3976 }
3977
3978
3979 /**
3980  *      emstorage_get_mails - Get the Mail list information based on mailbox_name name
3981  *
3982  *
3983  */
3984 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)
3985 {
3986         EM_DEBUG_FUNC_BEGIN();
3987         EM_PROFILE_BEGIN(emStorageGetMails);
3988
3989         int count = 0, ret = false, error = EMAIL_ERROR_NONE;
3990         emstorage_mail_tbl_t *p_data_tbl = NULL;
3991         char conditional_clause_string[QUERY_SIZE] = {0, };
3992
3993         EM_IF_NULL_RETURN_VALUE(mail_list, false);
3994         EM_IF_NULL_RETURN_VALUE(result_count, false);
3995
3996         if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3997                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3998                 if (err_code != NULL)
3999                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4000                 return false;
4001         }
4002
4003         _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);
4004
4005         EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
4006
4007         if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause_string, transaction, &p_data_tbl, &count,  &error)) {
4008                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
4009                 goto FINISH_OFF;
4010         }
4011
4012         ret = true;
4013
4014 FINISH_OFF:
4015
4016         if (ret == true) {
4017                 *mail_list = p_data_tbl;
4018                 *result_count = count;
4019                 EM_DEBUG_LOG("COUNT : %d", count);
4020         } else if (p_data_tbl != NULL)
4021                 emstorage_free_mail(&p_data_tbl, count, NULL);
4022
4023         if (err_code != NULL)
4024                 *err_code = error;
4025
4026         EM_PROFILE_END(emStorageGetMails);
4027         EM_DEBUG_FUNC_END("ret [%d]", ret);
4028         return ret;
4029 }
4030
4031
4032
4033 /**
4034  *      emstorage_get_searched_mail_list - Get the mail list information after filtering
4035  *
4036  *
4037  */
4038 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)
4039 {
4040         EM_DEBUG_FUNC_BEGIN();
4041
4042         int ret = false, error = EMAIL_ERROR_NONE;
4043         char conditional_clause[QUERY_SIZE] = {0, };
4044         char *temp_search_value = NULL;
4045         char *temp_search_value2 = NULL;
4046
4047         EM_IF_NULL_RETURN_VALUE(mail_list, false);
4048         EM_IF_NULL_RETURN_VALUE(result_count, false);
4049
4050         if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
4051                 EM_DEBUG_EXCEPTION("select_num[%p], Mail_list[%p]", result_count, mail_list);
4052                 error = EMAIL_ERROR_INVALID_PARAM;
4053                 goto FINISH_OFF;
4054         }
4055
4056         temp_search_value = em_replace_all_string((char*)search_value, "_", "\\_");
4057         temp_search_value2 = em_replace_all_string(temp_search_value, "%", "\\%");
4058
4059         _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);
4060
4061         EM_DEBUG_LOG("conditional_clause[%s]", conditional_clause);
4062
4063         if (!emstorage_query_mail_list(multi_user_name, conditional_clause, transaction, mail_list, result_count, &error)) {
4064                 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
4065                 goto FINISH_OFF;
4066         }
4067
4068         ret = true;
4069
4070 FINISH_OFF:
4071         EM_DEBUG_LOG("emstorage_get_searched_mail_list finish off");
4072
4073         if (err_code != NULL)
4074                 *err_code = error;
4075
4076         EM_SAFE_FREE(temp_search_value);
4077         EM_SAFE_FREE(temp_search_value2);
4078
4079         EM_DEBUG_FUNC_END("ret [%d]", ret);
4080         return ret;
4081 }
4082
4083
4084 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name)
4085 {
4086         EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
4087
4088         if (account_id <= 0 || !recv_password_file_name || !send_password_file_name) {
4089                 EM_DEBUG_EXCEPTION("Invalid parameter");
4090                 return EMAIL_ERROR_INVALID_PARAM;
4091         }
4092
4093         EM_DEBUG_LOG("MULTI_USER_NAME : [%s]", multi_user_name);
4094
4095         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
4096                 snprintf(recv_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_recv_%s", account_id, multi_user_name);
4097                 snprintf(send_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_send_%s", account_id, multi_user_name);
4098         } else {
4099                 snprintf(recv_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_recv", account_id);
4100                 snprintf(send_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_send", account_id);
4101         }
4102
4103         EM_DEBUG_FUNC_END();
4104         return EMAIL_ERROR_NONE;
4105 }
4106
4107 static int _read_password_from_secure_storage(char *file_name, char **password)
4108 {
4109         EM_DEBUG_FUNC_BEGIN_SEC("file_name[%s]", file_name);
4110
4111         if (!file_name || !password) {
4112                 EM_DEBUG_EXCEPTION("Invalid Parameter");
4113                 return EMAIL_ERROR_INVALID_PARAM;
4114         }
4115
4116         int ret = EMAIL_ERROR_NONE;
4117
4118         ret = emcore_get_password_in_key_manager(file_name, password);
4119         if (ret != EMAIL_ERROR_NONE) {
4120                 EM_DEBUG_EXCEPTION("emcore_get_password_in_key_manager failed : [%d]", ret);
4121                 goto FINISH_OFF;
4122         }
4123
4124 FINISH_OFF:
4125
4126         EM_DEBUG_FUNC_END("ret [%d]", ret);
4127         return ret;
4128 }
4129
4130 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)
4131 {
4132         EM_DEBUG_FUNC_BEGIN("account_id[%d], pulloption[%d], account[%p], transaction[%d], err_code[%p]", account_id, pulloption, account, transaction, err_code);
4133
4134         if (!account) {
4135                 EM_DEBUG_EXCEPTION("account_id[%d], account[%p]", account_id, account);
4136                 if (err_code != NULL)
4137                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4138                 return false;
4139         }
4140
4141         int ret = false;
4142         int error = EMAIL_ERROR_NONE;
4143         emstorage_account_tbl_t* p_data_tbl = NULL;
4144         DB_STMT hStmt = NULL;
4145         char sql_query_string[QUERY_SIZE] = {0, };
4146         int rc = -1;
4147         int sql_len = 0;
4148         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4149         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4150
4151         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4152
4153         EMSTORAGE_START_READ_TRANSACTION(transaction);
4154
4155         /*  Make query string */
4156         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
4157         sql_len = EM_SAFE_STRLEN(sql_query_string);
4158
4159         if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4160                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4161                                 "incoming_server_type,"
4162                                 "incoming_server_address,"
4163                                 "user_email_address,"
4164                                 "incoming_server_user_name,"
4165                                 "retrieval_mode,"
4166                                 "incoming_server_port_number,"
4167                                 "incoming_server_secure_connection,"
4168                                 "incoming_server_authentication_method,"
4169                                 "outgoing_server_type,"
4170                                 "outgoing_server_address,"
4171                                 "outgoing_server_port_number,"
4172                                 "outgoing_server_need_authentication,"
4173                                 "outgoing_server_secure_connection,"
4174                                 "outgoing_server_user_name,"
4175                                 "user_display_name,"
4176                                 "reply_to_address,"
4177                                 "return_address,"
4178                                 "account_id,"
4179                                 "keep_mails_on_pop_server_after_download,"
4180                                 "auto_download_size,"
4181                                 "peak_interval,"
4182                                 "peak_days,"
4183                                 "peak_start_time,"
4184                                 "peak_end_time,"
4185                                 "outgoing_server_use_same_authenticator,"
4186                                 "auto_resend_times,"
4187                                 "outgoing_server_size_limit,"
4188                                 "wifi_auto_download,"
4189                                 "pop_before_smtp,"
4190                                 "incoming_server_requires_apop,"
4191                                 "logo_icon_path,"
4192                                 "user_data,"
4193                                 "user_data_length,"
4194                                 "color_label,"
4195                                 "check_interval,"
4196                                 "sync_status,");
4197                 sql_len = EM_SAFE_STRLEN(sql_query_string);
4198         }
4199
4200         if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME) {
4201                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " account_name, ");
4202                 sql_len = EM_SAFE_STRLEN(sql_query_string);
4203         }
4204
4205         /*  get from secure storage, not from db */
4206         if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4207                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4208                                 "priority,"
4209                                 "keep_local_copy,"
4210                                 "req_delivery_receipt,"
4211                                 "req_read_receipt,"
4212                                 "download_limit,"
4213                                 "block_address,"
4214                                 "block_subject,"
4215                                 "display_name_from,"
4216                                 "reply_with_body,"
4217                                 "forward_with_files,"
4218                                 "add_myname_card,"
4219                                 "add_signature,"
4220                                 "signature,"
4221                                 "add_my_address_to_bcc,"
4222                                 "notification_status,"
4223                                 "vibrate_status,"
4224                                 "display_content_status,"
4225                                 "default_ringtone_status,"
4226                                 "alert_ringtone_path,"
4227                                 "account_svc_id,"
4228                                 "sync_disabled,"
4229                                 "default_mail_slot_size,"
4230                                 "roaming_option,"
4231                                 "smime_type,"
4232                                 "certificate_path,"
4233                                 "cipher_type,"
4234                                 "digest_type,");
4235
4236                 sql_len = EM_SAFE_STRLEN(sql_query_string);
4237         }
4238
4239         /*  dummy value, FROM WHERE clause */
4240         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "0 FROM mail_account_tbl WHERE account_id = %d", account_id);
4241
4242         /*  FROM clause */
4243         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
4244
4245         /*  execute a sql and count rows */
4246         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4247         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4248                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4249
4250         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4251         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4252                         ("sqlite3_step fail:%d", rc));
4253
4254         if (rc == SQLITE_DONE) {
4255                 EM_DEBUG_EXCEPTION("no matched account found...");
4256                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4257                 goto FINISH_OFF;
4258         }
4259
4260         /*  Assign query result to structure */
4261         if (!(p_data_tbl = (emstorage_account_tbl_t *)malloc(sizeof(emstorage_account_tbl_t) * 1))) {
4262                 EM_DEBUG_EXCEPTION("malloc failed...");
4263                 error = EMAIL_ERROR_OUT_OF_MEMORY;
4264                 goto FINISH_OFF;
4265         }
4266
4267         memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * 1);
4268         int col_index = 0;
4269
4270         if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4271                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_type), col_index++);
4272                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_address),  0, col_index++);
4273                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_email_address), 0, col_index++);
4274                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_user_name), 0, col_index++);
4275                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->retrieval_mode), col_index++);
4276                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_port_number), col_index++);
4277                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_secure_connection), col_index++);
4278                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_authentication_method), col_index++);
4279                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->outgoing_server_type), col_index++);
4280                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_address), 0, col_index++);
4281                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_port_number), col_index++);
4282                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_need_authentication), col_index++);
4283                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_secure_connection), col_index++);
4284                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_user_name), 0, col_index++);
4285                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_display_name), 0, col_index++);
4286                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->reply_to_address), 0, col_index++);
4287                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->return_address), 0, col_index++);
4288                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
4289                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->keep_mails_on_pop_server_after_download), col_index++);
4290                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_download_size), col_index++);
4291                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_interval), col_index++);
4292                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_days), col_index++);
4293                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_start_time), col_index++);
4294                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_end_time), col_index++);
4295                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_use_same_authenticator), col_index++);
4296                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_resend_times), col_index++);
4297                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_size_limit), col_index++);
4298                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->wifi_auto_download), col_index++);
4299                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->pop_before_smtp), col_index++);
4300                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_requires_apop), col_index++);
4301                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->logo_icon_path), 0, col_index++);
4302                 _get_stmt_field_data_blob(hStmt, &p_data_tbl->user_data, col_index++);
4303                 _get_stmt_field_data_int(hStmt, &p_data_tbl->user_data_length, col_index++);
4304                 _get_stmt_field_data_int(hStmt, &p_data_tbl->color_label, col_index++);
4305                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->check_interval), col_index++);
4306                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->sync_status), col_index++);
4307         }
4308
4309         if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME)
4310                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->account_name), 0, col_index++);
4311
4312         if (pulloption & EMAIL_ACC_GET_OPT_PASSWORD) {
4313                 /*  get password file name */
4314                 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) {
4315                         EM_DEBUG_EXCEPTION("_get_password_file_name failed [%d]", error);
4316                         goto FINISH_OFF;
4317                 }
4318
4319                 /*  read password from secure storage */
4320                 if ((error = _read_password_from_secure_storage(recv_password_file_name, &(p_data_tbl->incoming_server_password))) < 0) {
4321                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4322                         goto FINISH_OFF;
4323                 }
4324
4325                 EM_DEBUG_LOG_DEV("recv_password_file_name[%s], password[%s]", recv_password_file_name,  p_data_tbl->incoming_server_password);
4326
4327                 if (p_data_tbl->outgoing_server_use_same_authenticator == 0) {
4328                         if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl->outgoing_server_password))) < 0) {
4329                                 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4330                                 goto FINISH_OFF;
4331                         }
4332                         EM_DEBUG_LOG_DEV("send_password_file_name[%s], password[%s]", send_password_file_name,  p_data_tbl->outgoing_server_password);
4333                 }
4334         }
4335
4336         if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4337                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.priority), col_index++);
4338                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.keep_local_copy), col_index++);
4339                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_delivery_receipt), col_index++);
4340                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_read_receipt), col_index++);
4341                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.download_limit), col_index++);
4342                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_address), col_index++);
4343                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_subject), col_index++);
4344                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.display_name_from), 0, col_index++);
4345                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.reply_with_body), col_index++);
4346                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.forward_with_files), col_index++);
4347                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_myname_card), col_index++);
4348                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_signature), col_index++);
4349                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.signature), 0, col_index++);
4350                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.add_my_address_to_bcc), col_index++);
4351                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.notification_status), col_index++);
4352                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.vibrate_status), col_index++);
4353                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.display_content_status), col_index++);
4354                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.default_ringtone_status), col_index++);
4355                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.alert_ringtone_path), 0, col_index++);
4356                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_svc_id), col_index++);
4357                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->sync_disabled), col_index++);
4358                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->default_mail_slot_size), col_index++);
4359                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->roaming_option), col_index++);
4360                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->smime_type), col_index++);
4361                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->certificate_path), 0, col_index++);
4362                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->cipher_type), col_index++);
4363                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->digest_type), col_index++);
4364         }
4365
4366         ret = true;
4367
4368 FINISH_OFF:
4369         if (ret == true)
4370                 *account = p_data_tbl;
4371         else {
4372                 if (p_data_tbl)
4373                         emstorage_free_account((emstorage_account_tbl_t **)&p_data_tbl, 1, NULL);
4374         }
4375         if (hStmt != NULL) {
4376                 rc = sqlite3_finalize(hStmt);
4377                 if (rc != SQLITE_OK) {
4378                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4379                         error = EMAIL_ERROR_DB_FAILURE;
4380                 }
4381         }
4382
4383         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4384
4385         if (err_code != NULL)
4386                 *err_code = error;
4387
4388         EM_DEBUG_FUNC_END("ret [%d]", ret);
4389         return ret;
4390 }
4391
4392 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)
4393 {
4394         EM_DEBUG_FUNC_BEGIN("account_id[%d], password_length[%p], err_code[%p]", account_id, password_length, err_code);
4395
4396         if (account_id <= 0 || password_length == NULL) {
4397                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4398                 if (err_code != NULL)
4399                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4400                 return false;
4401         }
4402
4403         int ret = false;
4404         int error = EMAIL_ERROR_NONE;
4405         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4406         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4407         char *temp_password = NULL;
4408
4409
4410         /*  get password file name */
4411         if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4412                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4413                 goto FINISH_OFF;
4414         }
4415
4416         /*  read password from secure storage */
4417         if (password_type == EMAIL_GET_INCOMING_PASSWORD_LENGTH) {
4418                 if ((error = _read_password_from_secure_storage(recv_password_file_name, &temp_password)) < 0 || !temp_password) {
4419                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed...");
4420                         goto FINISH_OFF;
4421                 }
4422         } else if (password_type == EMAIL_GET_OUTGOING_PASSWORD_LENGTH) {
4423                 if ((error = _read_password_from_secure_storage(send_password_file_name, &temp_password)) < 0 || !temp_password) {
4424                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed...");
4425                         goto FINISH_OFF;
4426                 }
4427         } else {
4428                 EM_DEBUG_LOG("Invalid password type");
4429                 if (err_code != NULL)
4430                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4431                 goto FINISH_OFF;
4432         }
4433
4434         *password_length = EM_SAFE_STRLEN(temp_password);
4435
4436         EM_DEBUG_LOG_SEC("recv_password_file_name[%s], *password_length[%d]", recv_password_file_name,  *password_length);
4437
4438         ret = true;
4439
4440 FINISH_OFF:
4441         EM_SAFE_FREE(temp_password);
4442
4443         if (err_code != NULL)
4444                 *err_code = error;
4445
4446         EM_DEBUG_FUNC_END("ret [%d]", ret);
4447         return ret;
4448 }
4449
4450 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)
4451 {
4452         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);
4453
4454         int err = EMAIL_ERROR_NONE;
4455         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4456         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4457
4458         if (input_incoming_server_password == NULL && input_outgoing_server_password == NULL) {
4459                 EM_DEBUG_EXCEPTION_SEC("Invalid param");
4460                 err = EMAIL_ERROR_INVALID_PARAM;
4461                 goto FINISH_OFF;
4462         }
4463
4464         /*  get password file name */
4465         if ((err = _get_password_file_name(multi_user_name,
4466                                         input_account_id,
4467                                         recv_password_file_name,
4468                                         send_password_file_name)) != EMAIL_ERROR_NONE) {
4469                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4470                 goto FINISH_OFF;
4471         }
4472
4473         EM_DEBUG_LOG_SEC("recv_password_file_name [%s] input_incoming_server_password [%s]",
4474                         recv_password_file_name, input_incoming_server_password);
4475         EM_DEBUG_LOG_SEC("send_password_file_name [%s] input_outgoing_server_password [%s]",
4476                         send_password_file_name, input_outgoing_server_password);
4477
4478         if (input_incoming_server_password) {
4479                 err = emcore_remove_password_in_key_manager(recv_password_file_name);
4480                 if (err != EMAIL_ERROR_NONE) {
4481                         EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", recv_password_file_name);
4482                         goto FINISH_OFF;
4483                 }
4484
4485                 /*  save recv passwords to the secure storage */
4486                 err = emcore_add_password_in_key_manager(recv_password_file_name, input_incoming_server_password);
4487                 if (err != EMAIL_ERROR_NONE) {
4488                         EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4489                         goto FINISH_OFF;
4490                 }
4491         }
4492
4493         if (input_outgoing_server_password) {
4494                 err = emcore_remove_password_in_key_manager(send_password_file_name);
4495                 if (err != EMAIL_ERROR_NONE) {
4496                         EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", send_password_file_name);
4497                         goto FINISH_OFF;
4498                 }
4499
4500                 /*  save send passwords to the secure storage */
4501                 err = emcore_add_password_in_key_manager(send_password_file_name, input_outgoing_server_password);
4502                 if (err != EMAIL_ERROR_NONE) {
4503                         EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4504                         goto FINISH_OFF;
4505                 }
4506         }
4507 FINISH_OFF:
4508
4509         EM_DEBUG_FUNC_END("err [%d]", err);
4510         return err;
4511 }
4512
4513 INTERNAL_FUNC int emstorage_update_account(char *multi_user_name, int account_id, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4514 {
4515         EM_DEBUG_FUNC_BEGIN("account_id[%d], account[%p], transaction[%d], err_code[%p]", account_id, account_tbl, transaction, err_code);
4516
4517         if (account_id < FIRST_ACCOUNT_ID || !account_tbl) {
4518                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4519                 if (err_code != NULL)
4520                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4521                 return false;
4522         }
4523
4524         int error = EMAIL_ERROR_NONE;
4525         int rc, ret = false;
4526
4527         DB_STMT hStmt = NULL;
4528         char sql_query_string[QUERY_SIZE] = {0, };
4529
4530         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4531
4532         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4533
4534         SNPRINTF(sql_query_string, sizeof(sql_query_string),
4535                         "UPDATE mail_account_tbl SET"
4536                         "  account_name = ?"
4537                         ", logo_icon_path = ?"
4538                         ", user_data = ?"
4539                         ", user_data_length = ?"
4540                         ", account_svc_id = ?"
4541                         ", sync_status = ?"
4542                         ", sync_disabled = ?"
4543                         ", default_mail_slot_size = ?"
4544                         ", roaming_option = ?"
4545                         ", color_label = ?"
4546                         ", user_display_name = ?"
4547                         ", user_email_address = ?"
4548                         ", reply_to_address = ?"
4549                         ", return_address = ?"
4550                         ", incoming_server_type = ?"
4551                         ", incoming_server_address = ?"
4552                         ", incoming_server_port_number = ?"
4553                         ", incoming_server_user_name = ?"
4554                         ", incoming_server_secure_connection = ?"
4555                         ", incoming_server_authentication_method = ?"
4556                         ", retrieval_mode = ?"
4557                         ", keep_mails_on_pop_server_after_download = ?"
4558                         ", check_interval = ?"
4559                         ", auto_download_size = ?"
4560                         ", peak_interval = ?"
4561                         ", peak_days = ?"
4562                         ", peak_start_time = ?"
4563                         ", peak_end_time = ?"
4564                         ", outgoing_server_type = ?"
4565                         ", outgoing_server_address = ?"
4566                         ", outgoing_server_port_number = ?"
4567                         ", outgoing_server_user_name = ?"
4568                         ", outgoing_server_secure_connection = ?"
4569                         ", outgoing_server_need_authentication = ?"
4570                         ", outgoing_server_use_same_authenticator = ?"
4571                         ", priority = ?"
4572                         ", keep_local_copy = ?"
4573                         ", req_delivery_receipt = ?"
4574                         ", req_read_receipt = ?"
4575                         ", download_limit = ?"
4576                         ", block_address = ?"
4577                         ", block_subject = ?"
4578                         ", display_name_from = ?"
4579                         ", reply_with_body = ?"
4580                         ", forward_with_files = ?"
4581                         ", add_myname_card = ?"
4582                         ", add_signature = ?"
4583                         ", signature = ?"
4584                         ", add_my_address_to_bcc = ?"
4585                         ", notification_status = ?"
4586                         ", vibrate_status = ?"
4587                         ", display_content_status = ?"
4588                         ", default_ringtone_status = ?"
4589                         ", alert_ringtone_path = ?"
4590                         ", auto_resend_times = ?"
4591                         ", outgoing_server_size_limit = ?"
4592                         ", wifi_auto_download = ?"
4593                         ", pop_before_smtp = ?"
4594                         ", incoming_server_requires_apop = ?"
4595                         ", smime_type = ?"
4596                         ", certificate_path = ?"
4597                         ", cipher_type = ?"
4598                         ", digest_type = ?"
4599                         ", user_name = ?"
4600                         " WHERE account_id = ?");
4601
4602         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4603         EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
4604         EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
4605         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4606                         ("sqlite3_prepare fail:(%d) %s", rc, sqlite3_errmsg(local_db_handle)));
4607
4608         int i = 0;
4609
4610         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4611         _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4612         _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4613         _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4614         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4615         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4616         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4617         _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4618         _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
4619         _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
4620         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4621         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4622         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4623         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4624         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4625         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4626         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4627         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4628         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4629         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
4630         _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
4631         _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
4632         _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
4633         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
4634         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
4635         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
4636         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
4637         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
4638         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
4639         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4640         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
4641         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
4642         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
4643         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
4644         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
4645         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
4646         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
4647         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
4648         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
4649         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
4650         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
4651         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
4652         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4653         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
4654         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
4655         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
4656         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
4657         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
4658         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
4659         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
4660         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
4661         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
4662         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
4663         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4664         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
4665         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
4666         _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
4667         _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
4668         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
4669         _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
4670         _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4671         _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
4672         _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
4673         _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4674         _bind_stmt_field_data_int(hStmt, i++, account_id);
4675
4676         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4677         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
4678                         ("sqlite3_step fail:%d", rc));
4679         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4680                         ("sqlite3_step fail:%d", rc));
4681
4682         /*  validate account existence */
4683         rc = sqlite3_changes(local_db_handle);
4684         if (rc == 0) {
4685                 EM_DEBUG_EXCEPTION("no matched account found...");
4686                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4687                 goto FINISH_OFF;
4688         }
4689
4690         if (account_tbl->incoming_server_password || account_tbl->outgoing_server_password) {
4691                 if ((error = emstorage_update_account_password(multi_user_name, account_id, account_tbl->incoming_server_password, account_tbl->outgoing_server_password)) != EMAIL_ERROR_NONE) {
4692                         EM_DEBUG_EXCEPTION("emstorage_update_account_password failed [%d]", error);
4693                         goto FINISH_OFF;
4694                 }
4695         }
4696
4697         ret = true;
4698
4699 FINISH_OFF:
4700         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4701
4702         if (hStmt != NULL) {
4703                 rc = sqlite3_finalize(hStmt);
4704                 if (rc != SQLITE_OK) {
4705                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4706                         error = EMAIL_ERROR_DB_FAILURE;
4707                 }
4708         }
4709
4710         if (error == EMAIL_ERROR_NONE) {
4711                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_tbl->account_id, 0, NULL, 0))
4712                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_ACCOUNT_UPDATE] : Notification Failed >>> ");
4713         }
4714
4715         if (err_code != NULL)
4716                 *err_code = error;
4717
4718         EM_DEBUG_FUNC_END("ret [%d]", ret);
4719         return ret;
4720 }
4721
4722 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)
4723 {
4724         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], field_name[%s], value[%d], transaction[%d]", account_id, field_name, value, transaction);
4725         int error = EMAIL_ERROR_NONE;
4726         int result = 0;
4727         char sql_query_string[QUERY_SIZE] = {0, };
4728         sqlite3 *local_db_handle = NULL;
4729
4730         if (!account_id  || !field_name) {
4731                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4732                 return EMAIL_ERROR_INVALID_PARAM;
4733         }
4734
4735         local_db_handle = emstorage_get_db_connection(multi_user_name);
4736
4737         /* Write query string */
4738         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_account_tbl SET %s = %d WHERE account_id = %d", field_name, value, account_id);
4739
4740         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4741
4742         /* Execute query */
4743         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4744
4745         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4746         if (error != EMAIL_ERROR_NONE) {
4747                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4748                 goto FINISH_OFF;
4749         }
4750
4751         if (sqlite3_changes(local_db_handle) == 0)
4752                 EM_DEBUG_LOG("no mail matched...");
4753
4754
4755 FINISH_OFF:
4756         result = (error == EMAIL_ERROR_NONE) ? true : false;
4757         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, error);
4758
4759         if (error == EMAIL_ERROR_NONE) {
4760                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_id, 0, field_name, value))
4761                         EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_ACCOUNT_UPDATE [%s,%d]", field_name, value);
4762         }
4763         EM_DEBUG_FUNC_END("error [%d]", error);
4764         return error;
4765 }
4766
4767 INTERNAL_FUNC int emstorage_get_sync_status_of_account(char *multi_user_name, int account_id, int *result_sync_status, int *err_code)
4768 {
4769         EM_DEBUG_FUNC_BEGIN("account_id[%d], result_sync_status [%p], err_code[%p]", account_id, result_sync_status, err_code);
4770
4771         if (!result_sync_status) {
4772                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4773                 if (err_code != NULL)
4774                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4775                 return false;
4776         }
4777
4778         int error = EMAIL_ERROR_NONE, rc, ret = false, sync_status = 0, count, i, col_index;
4779         char sql_query_string[QUERY_SIZE] = {0, };
4780         char **result = NULL;
4781         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4782
4783         if (account_id)
4784                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl WHERE account_id = %d", account_id);
4785         else
4786                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl");
4787
4788         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
4789         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
4790                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4791
4792         if (!count) {
4793                 EM_DEBUG_EXCEPTION("no matched account found...");
4794                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4795                 goto FINISH_OFF;
4796         }
4797
4798         col_index = 1;
4799         *result_sync_status = 0;
4800
4801         for (i = 0; i < count; i++) {
4802                 _get_table_field_data_int(result, &sync_status, col_index++);
4803                 *result_sync_status |= sync_status;
4804         }
4805
4806         EM_DEBUG_LOG("sync_status [%d]", sync_status);
4807
4808         sqlite3_free_table(result);
4809
4810         ret = true;
4811
4812 FINISH_OFF:
4813
4814
4815         if (err_code != NULL)
4816                 *err_code = error;
4817
4818         EM_DEBUG_FUNC_END("ret [%d]", ret);
4819         return ret;
4820 }
4821
4822 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)
4823 {
4824         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);
4825
4826         int error = EMAIL_ERROR_NONE, rc, ret = false, set_value = sync_status, result_sync_status;
4827         char sql_query_string[QUERY_SIZE] = {0, };
4828         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4829
4830         if (set_operator != SET_TYPE_SET && account_id) {
4831                 if (!emstorage_get_sync_status_of_account(multi_user_name, account_id, &result_sync_status, &error)) {
4832                         EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", error);
4833                         if (err_code != NULL)
4834                                 *err_code = error;
4835                         return false;
4836                 }
4837                 switch (set_operator) {
4838                 case SET_TYPE_UNION:
4839                         set_value = result_sync_status | set_value;
4840                         break;
4841                 case SET_TYPE_MINUS:
4842                         set_value = result_sync_status & (~set_value);
4843                         break;
4844                 default:
4845                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_NOT_SUPPORTED [%d]", set_operator);
4846                         error = EMAIL_ERROR_NOT_SUPPORTED;
4847                         break;
4848                 }
4849                 EM_DEBUG_LOG("set_value [%d]", set_value);
4850         }
4851
4852         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4853
4854         if (account_id)
4855                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE account_id = %d", set_value, account_id);
4856         else
4857                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE incoming_server_type <> 5", set_value);
4858
4859         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4860
4861         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4862         if (error != EMAIL_ERROR_NONE) {
4863                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4864                 goto FINISH_OFF;
4865         }
4866
4867         rc = sqlite3_changes(local_db_handle);
4868
4869         if (rc == 0) {
4870                 EM_DEBUG_EXCEPTION("no matched account found...");
4871                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4872                 goto FINISH_OFF;
4873         }
4874
4875         ret = true;
4876
4877 FINISH_OFF:
4878         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4879
4880         if (error == EMAIL_ERROR_NONE) {
4881                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE_SYNC_STATUS, account_id, set_value, NULL, 0))
4882                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_UPDATE_SYNC_STATUS] : Notification failed");
4883         }
4884
4885         if (err_code != NULL)
4886                 *err_code = error;
4887
4888         EM_DEBUG_FUNC_END("ret [%d]", ret);
4889         return ret;
4890 }
4891
4892 INTERNAL_FUNC int emstorage_add_account(char *multi_user_name, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4893 {
4894         EM_DEBUG_FUNC_BEGIN("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4895
4896         if (!account_tbl) {
4897                 EM_DEBUG_EXCEPTION("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4898                 if (err_code != NULL)
4899                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4900                 return false;
4901         }
4902
4903         int row_count = 0;
4904         int i = 0;
4905         int rc = -1, ret = false;
4906         int error = EMAIL_ERROR_NONE;
4907         DB_STMT hStmt = NULL;
4908         char sql_query_string[QUERY_SIZE] = {0, };
4909         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4910         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4911
4912         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4913
4914         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4915
4916         char *sql = "SELECT rowid FROM mail_account_tbl;";
4917         char **result = NULL;
4918
4919         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, &row_count, NULL, NULL), rc);
4920         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
4921                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
4922
4923         if (NULL == result[1]) rc = 1;
4924         else {
4925                 for (i = 1; i <= row_count; i++) {
4926                         if (i != atoi(result[i]))
4927                                 break;
4928                 }
4929
4930                 rc = i;
4931         }
4932         sqlite3_free_table(result);
4933         result = NULL;
4934
4935         if (rc < 0 || rc > EMAIL_ACCOUNT_MAX) {
4936                 EM_DEBUG_EXCEPTION("OVERFLOWED THE MAX ACCOUNT");
4937                 error = EMAIL_ERROR_ACCOUNT_MAX_COUNT;
4938                 goto FINISH_OFF;
4939         }
4940
4941         account_tbl->account_id = rc;
4942
4943         if ((error = _get_password_file_name(multi_user_name, account_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4944                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4945                 goto FINISH_OFF;
4946         }
4947
4948         EM_DEBUG_LOG(" >>>> ACCOUNT_ID [ %d ] ", account_tbl->account_id);
4949         SNPRINTF(sql_query_string, sizeof(sql_query_string),
4950                         "INSERT INTO mail_account_tbl VALUES "
4951                         "(        "
4952                         "    ? "  /*   account_id */
4953                         "  , ? "  /*   account_name */
4954                         "  , ? "  /*   logo_icon_path */
4955                         "  , ? "  /*   user_data */
4956                         "  , ? "  /*   user_data_length */
4957                         "  , ? "  /*   account_svc_id */
4958                         "  , ? "  /*   sync_status */
4959                         "  , ? "  /*   sync_disabled */
4960                         "  , ? "  /*   default_mail_slot_size */
4961                         "  , ? "  /*   roaming_option */
4962                         "  , ? "  /*   color_label */
4963                         "  , ? "  /*   user_display_name */
4964                         "  , ? "  /*   user_email_address */
4965                         "  , ? "  /*   reply_to_address */
4966                         "  , ? "  /*   return_address */
4967                         "  , ? "  /*   incoming_server_type */
4968                         "  , ? "  /*   incoming_server_address */
4969                         "  , ? "  /*   incoming_server_port_number */
4970                         "  , ? "  /*   incoming_server_user_name */
4971                         "  , ? "  /*   incoming_server_password */
4972                         "  , ? "  /*   incoming_server_secure_connection */
4973                         "  , ? "  /*   incoming_server_authentication_method */
4974                         "  , ? "  /*   retrieval_mode */
4975                         "  , ? "  /*   keep_mails_on_pop_server_after_download */
4976                         "  , ? "  /*   check_interval */
4977                         "  , ? "  /*   auto_download_size */
4978                         "  , ? "  /*   peak_interval */
4979                         "  , ? "  /*   peak_days */
4980                         "  , ? "  /*   peak_start_time */
4981                         "  , ? "  /*   peak_end_time */
4982                         "  , ? "  /*   outgoing_server_type */
4983                         "  , ? "  /*   outgoing_server_address */
4984                         "  , ? "  /*   outgoing_server_port_number */
4985                         "  , ? "  /*   outgoing_server_user_name */
4986                         "  , ? "  /*   outgoing_server_password */
4987                         "  , ? "  /*   outgoing_server_secure_connection */
4988                         "  , ? "  /*   outgoing_server_need_authentication */
4989                         "  , ? "  /*   outgoing_server_use_same_authenticator */
4990                         "  , ? "  /*   priority */
4991                         "  , ? "  /*   keep_local_copy */
4992                         "  , ? "  /*   req_delivery_receipt */
4993                         "  , ? "  /*   req_read_receipt */
4994                         "  , ? "  /*   download_limit */
4995                         "  , ? "  /*   block_address */
4996                         "  , ? "  /*   block_subject */
4997                         "  , ? "  /*   display_name_from */
4998                         "  , ? "  /*   reply_with_body */
4999                         "  , ? "  /*   forward_with_files */
5000                         "  , ? "  /*   add_myname_card */
5001                         "  , ? "  /*   add_signature */
5002                         "  , ? "  /*   signature */
5003                         "  , ? "  /*   add_my_address_to_bcc */
5004                         "  , ? "  /*   auto_resend_times */
5005                         "  , ? "  /*   outgoing_server_size_limit */
5006                         "  , ? "  /*   wifi_auto_download */
5007                         "  , ? "  /*   pop_before_smtp */
5008                         "  , ? "  /*   incoming_server_requires_apop */
5009                         "  , ? "  /*   smime_type */
5010                         "  , ? "  /*   certificate_path */
5011                         "  , ? "  /*   cipher_type */
5012                         "  , ? "  /*   digest_type */
5013                         "  , ? "  /*   notification_status */
5014                         "  , ? "  /*   vibrate_status */
5015                         "  , ? "  /*   display_content_status */
5016                         "  , ? "  /*   default_ringtone_status */
5017                         "  , ? "  /*   alert_ringtone_path */
5018                         "  , ? "  /*   user_name */
5019                         ") ");
5020
5021         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5022         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5023                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5024
5025         i = 0;
5026
5027         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_id);
5028         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
5029         _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5030         _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
5031         _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
5032         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
5033         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
5034         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
5035         _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
5036         _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
5037         _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
5038         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
5039         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5040         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5041         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5042         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
5043         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5044         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
5045         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
5046         _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
5047         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
5048         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
5049         _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
5050         _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
5051         _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
5052         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
5053         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
5054         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
5055         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
5056         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
5057         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
5058         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5059         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
5060         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
5061         _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
5062         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
5063         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
5064         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
5065         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
5066         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
5067         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
5068         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
5069         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
5070         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
5071         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
5072         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
5073         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
5074         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
5075         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
5076         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
5077         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
5078         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
5079         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
5080         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
5081         _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
5082         _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
5083         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
5084         _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
5085         _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
5086         _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
5087         _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
5088         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
5089         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
5090         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
5091         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
5092         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5093         _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5094
5095         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5096
5097         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
5098                         ("sqlite3_step fail:%d", rc));
5099         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5100                         ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
5101
5102
5103         /*  save passwords to the secure storage */
5104         EM_DEBUG_LOG_SEC("save to the secure storage : recv_file[%s], send_file[%s]", recv_password_file_name, send_password_file_name);
5105         error = emcore_add_password_in_key_manager(recv_password_file_name, account_tbl->incoming_server_password);
5106         if (error != EMAIL_ERROR_NONE) {
5107                 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
5108                 goto FINISH_OFF;
5109         }
5110
5111         error = emcore_add_password_in_key_manager(send_password_file_name, account_tbl->outgoing_server_password);
5112         if (error != EMAIL_ERROR_NONE) {
5113                 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
5114                 goto FINISH_OFF;
5115         }
5116
5117         ret = true;
5118
5119 FINISH_OFF:
5120         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5121
5122         if (hStmt != NULL) {
5123                 rc = sqlite3_finalize(hStmt);
5124                 if (rc != SQLITE_OK) {
5125                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5126                         error = EMAIL_ERROR_DB_FAILURE;
5127                 }
5128         }
5129
5130         if (error == EMAIL_ERROR_NONE) {
5131                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_ADD, account_tbl->account_id, 0, NULL, 0))
5132                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_ADD] : Notification failed");
5133         }
5134
5135         if (err_code != NULL)
5136                 *err_code = error;
5137
5138         EM_DEBUG_FUNC_END("ret [%d]", ret);
5139         return ret;
5140 }
5141
5142 INTERNAL_FUNC int emstorage_delete_account(char *multi_user_name, int account_id, int transaction, int *err_code)
5143 {
5144         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
5145
5146         if (account_id < FIRST_ACCOUNT_ID) {
5147                 EM_DEBUG_EXCEPTION(" account_id[%d]", account_id);
5148
5149                 if (err_code != NULL)
5150                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5151                 return false;
5152         }
5153
5154         int rc = -1, ret = false;
5155         int error = EMAIL_ERROR_NONE;
5156
5157         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5158
5159         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5160
5161         /*  TODO : delete password files - file names can be obtained from db or a rule that makes a name */
5162         char sql_query_string[QUERY_SIZE] = {0, };
5163         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5164         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5165
5166         /*  get password file name */
5167         if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
5168                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
5169                 goto FINISH_OFF;
5170         }
5171
5172         /*  delete from db */
5173         memset(sql_query_string, 0x00, sizeof(sql_query_string));
5174         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_account_tbl WHERE account_id = %d", account_id);
5175         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5176         if (error != EMAIL_ERROR_NONE) {
5177                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5178                 goto FINISH_OFF;
5179         }
5180
5181         /*  validate account existence */
5182         rc = sqlite3_changes(local_db_handle);
5183         if (rc == 0) {
5184                 EM_DEBUG_EXCEPTION(" no matched account found...");
5185                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
5186                 goto FINISH_OFF;
5187         }
5188
5189         /*  delete from secure storage */
5190         error = emcore_remove_password_in_key_manager(recv_password_file_name);
5191         if (error != EMAIL_ERROR_NONE) {
5192                 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5193                 goto FINISH_OFF;
5194         }
5195
5196         error = emcore_remove_password_in_key_manager(send_password_file_name);
5197         if (error != EMAIL_ERROR_NONE) {
5198                 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5199                 goto FINISH_OFF;
5200         }
5201
5202         ret = true;
5203
5204 FINISH_OFF:
5205         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5206
5207         if (err_code != NULL)
5208                 *err_code = error;
5209
5210         EM_DEBUG_FUNC_END("ret [%d]", ret);
5211         return ret;
5212 }
5213
5214
5215 INTERNAL_FUNC int emstorage_free_account(emstorage_account_tbl_t** account_list, int count, int *err_code)
5216 {
5217         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%d], err_code[%p]", account_list, count, err_code);
5218
5219         int ret = false;
5220         int error = EMAIL_ERROR_NONE;
5221
5222         if (count > 0) {
5223                 if (!account_list || !*account_list) {
5224                         EM_DEBUG_EXCEPTION("account_list[%p], count[%d]", account_list, count);
5225                         error = EMAIL_ERROR_INVALID_PARAM;
5226                         goto FINISH_OFF;
5227                 }
5228
5229                 emstorage_account_tbl_t* p = *account_list;
5230                 int i = 0;
5231
5232                 for (; i < count; i++) {
5233                         EM_SAFE_FREE(p[i].account_name);
5234                         EM_SAFE_FREE(p[i].incoming_server_address);
5235                         EM_SAFE_FREE(p[i].user_email_address);
5236                         EM_SAFE_FREE(p[i].user_data);
5237                         EM_SAFE_FREE(p[i].incoming_server_user_name);
5238                         EM_SAFE_FREE(p[i].incoming_server_password);
5239                         EM_SAFE_FREE(p[i].outgoing_server_address);
5240                         EM_SAFE_FREE(p[i].outgoing_server_user_name);
5241                         EM_SAFE_FREE(p[i].outgoing_server_password);
5242                         EM_SAFE_FREE(p[i].user_display_name);
5243                         EM_SAFE_FREE(p[i].reply_to_address);
5244                         EM_SAFE_FREE(p[i].return_address);
5245                         EM_SAFE_FREE(p[i].logo_icon_path);
5246                         EM_SAFE_FREE(p[i].options.display_name_from);
5247                         EM_SAFE_FREE(p[i].options.signature);
5248                         EM_SAFE_FREE(p[i].options.alert_ringtone_path);
5249                         EM_SAFE_FREE(p[i].certificate_path);
5250                         EM_SAFE_FREE(p[i].user_name);
5251                 }
5252
5253                 EM_SAFE_FREE(p);
5254                 *account_list = NULL;
5255         }
5256
5257         ret = true;
5258
5259 FINISH_OFF:
5260         if (err_code != NULL)
5261                 *err_code = error;
5262
5263         EM_DEBUG_FUNC_END("ret [%d]", ret);
5264         return ret;
5265 }
5266
5267 INTERNAL_FUNC int emstorage_get_mailbox_count(char *multi_user_name, int account_id, int local_yn, int *count, int transaction, int *err_code)
5268 {
5269         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);
5270
5271         if ((account_id < FIRST_ACCOUNT_ID) || (count == NULL)) {
5272                 EM_DEBUG_EXCEPTION(" account_list[%d], local_yn[%d], count[%p]", account_id, local_yn, count);
5273
5274                 if (err_code != NULL)
5275                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5276                 return false;
5277         }
5278
5279         int rc = -1, ret = false;
5280         int error = EMAIL_ERROR_NONE;
5281         char sql_query_string[QUERY_SIZE] = {0, };
5282
5283         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5284         EMSTORAGE_START_READ_TRANSACTION(transaction);
5285
5286         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);
5287
5288         char **result;
5289
5290         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5291         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
5292                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5293
5294         *count = atoi(result[1]);
5295         sqlite3_free_table(result);
5296
5297
5298         ret = true;
5299
5300 FINISH_OFF:
5301
5302         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5303
5304         if (err_code != NULL)
5305                 *err_code = error;
5306
5307         EM_DEBUG_FUNC_END("ret [%d]", ret);
5308         return ret;
5309 }
5310
5311 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)
5312 {
5313         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);
5314
5315         if (!select_num || !mailbox_list) {
5316                 EM_DEBUG_EXCEPTION("Invalid parameters");
5317
5318                 if (err_code != NULL)
5319                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5320
5321                 return false;
5322         }
5323
5324         int ret = false;
5325         int error = EMAIL_ERROR_NONE;
5326         char conditional_clause_string[QUERY_SIZE] = {0, };
5327         char ordering_clause_string[QUERY_SIZE] = {0, };
5328
5329         if (account_id == ALL_ACCOUNT) {
5330                 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5331                         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);
5332         } else {
5333                 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d  ", account_id);
5334                 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5335                         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);
5336         }
5337
5338         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5339
5340         switch (sort_type) {
5341         case EMAIL_MAILBOX_SORT_BY_NAME_ASC:
5342                 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name ASC");
5343                 break;
5344
5345         case EMAIL_MAILBOX_SORT_BY_NAME_DSC:
5346                 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name DESC");
5347                 break;
5348
5349         case EMAIL_MAILBOX_SORT_BY_TYPE_ASC:
5350                 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type ASC");
5351                 break;
5352
5353         case EMAIL_MAILBOX_SORT_BY_TYPE_DSC:
5354                 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type DEC");
5355                 break;
5356         }
5357
5358         EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5359
5360         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5361                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5362                 goto FINISH_OFF;
5363         }
5364
5365         ret = true;
5366
5367 FINISH_OFF:
5368
5369         if (err_code != NULL)
5370                 *err_code = error;
5371
5372         EM_DEBUG_FUNC_END("ret [%d]", ret);
5373         return ret;
5374 }
5375
5376 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)
5377 {
5378         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);
5379
5380         int ret = false;
5381         int error = EMAIL_ERROR_NONE;
5382         int where_clause_count = 0;
5383         char conditional_clause_string[QUERY_SIZE] = {0, };
5384         char ordering_clause_string[QUERY_SIZE] = {0, };
5385
5386         if (!select_num || !mailbox_list) {
5387                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5388                 error = EMAIL_ERROR_INVALID_PARAM;
5389                 goto FINISH_OFF;
5390         }
5391
5392         if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL) {
5393                 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);
5394                 where_clause_count++;
5395         }
5396
5397         if (account_id > 0) {
5398                 if (where_clause_count == 0) {
5399                         SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " WHERE ");
5400                         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);
5401                 } else
5402                         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);
5403         }
5404
5405         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY CASE WHEN MBT.mailbox_name"
5406                         " GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\'"
5407                         " THEN 2 ELSE 1 END ASC,"
5408                         " MBT.mailbox_name COLLATE CONVERTUTF8 ASC ");
5409         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5410         EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5411
5412         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 1, 1, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5413                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5414                 goto FINISH_OFF;
5415         }
5416
5417         ret = true;
5418
5419 FINISH_OFF:
5420
5421         if (err_code != NULL)
5422                 *err_code = error;
5423
5424         EM_DEBUG_FUNC_END("ret [%d]", ret);
5425         return ret;
5426 }
5427
5428 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)
5429 {
5430         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);
5431
5432         int ret = false;
5433         int error = EMAIL_ERROR_NONE;
5434         char conditional_clause_string[QUERY_SIZE] = {0, };
5435
5436         if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list || !parent_mailbox_name) {
5437                 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);
5438                 if (err_code != NULL)
5439                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5440                 return false;
5441         }
5442
5443         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);
5444         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5445
5446         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) {
5447                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5448                 goto FINISH_OFF;
5449         }
5450
5451         ret = true;
5452
5453 FINISH_OFF:
5454
5455
5456         if (err_code != NULL)
5457                 *err_code = error;
5458
5459         EM_DEBUG_FUNC_END("ret [%d]", ret);
5460         return ret;
5461 }
5462
5463 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)
5464 {
5465         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);
5466         if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list) {
5467                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5468
5469                 if (err_code != NULL)
5470                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5471
5472                 return false;
5473         }
5474
5475         int ret = false;
5476         int error = EMAIL_ERROR_NONE;
5477         char conditional_clause_string[QUERY_SIZE] = {0, };
5478
5479         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND modifiable_yn = 0", account_id);
5480         EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
5481
5482         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) {
5483                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5484                 goto FINISH_OFF;
5485         }
5486
5487         ret = true;
5488
5489 FINISH_OFF:
5490
5491         if (err_code != NULL)
5492                 *err_code = error;
5493
5494         EM_DEBUG_FUNC_END("ret [%d]", ret);
5495         return ret;
5496 }
5497
5498 INTERNAL_FUNC int emstorage_stamp_last_sync_time_of_mailbox(char *multi_user_name, int input_mailbox_id, int input_transaction)
5499 {
5500         EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_transaction [%d]", input_mailbox_id, input_transaction);
5501
5502         int      result_code = false;
5503         int      error = EMAIL_ERROR_NONE;
5504         time_t   current_time = 0;
5505         char     sql_query_string[QUERY_SIZE] = {0, };
5506         sqlite3 *local_db_handle = NULL;
5507
5508         if (!input_mailbox_id) {
5509                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5510                 return EMAIL_ERROR_INVALID_PARAM;
5511         }
5512
5513         time(&current_time);
5514
5515         local_db_handle = emstorage_get_db_connection(multi_user_name);
5516         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
5517
5518         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5519                         "UPDATE mail_box_tbl SET"
5520                         " last_sync_time = %d"
5521                         " WHERE mailbox_id = %d"
5522                         , (int)current_time
5523                         , input_mailbox_id);
5524
5525         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
5526         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5527         if (error != EMAIL_ERROR_NONE) {
5528                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5529                 goto FINISH_OFF;
5530         }
5531
5532 FINISH_OFF:
5533
5534         if (error == EMAIL_ERROR_NONE)
5535                 result_code = true;
5536
5537         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, result_code, error);
5538
5539         EM_DEBUG_FUNC_END("error [%d]", error);
5540         return error;
5541 }
5542
5543 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)
5544 {
5545         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);
5546         EM_PROFILE_BEGIN(profile_emstorage_get_mailbox_by_name);
5547
5548         if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !result_mailbox) {
5549                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], mailbox_name[%s], result_mailbox[%p]", account_id, local_yn, mailbox_name, result_mailbox);
5550
5551                 if (err_code != NULL)
5552                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5553                 return false;
5554         }
5555
5556         int ret = false;
5557         int error = EMAIL_ERROR_NONE;
5558         int result_count = 0;
5559         char conditional_clause_string[QUERY_SIZE] = {0, };
5560
5561         if (strcmp(mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
5562                 if (!(*result_mailbox = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t)))) {
5563                         EM_DEBUG_EXCEPTION("malloc failed...");
5564                         error = EMAIL_ERROR_OUT_OF_MEMORY;
5565                         goto FINISH_OFF;
5566                 }
5567
5568                 (*result_mailbox)->mailbox_id                 = 0;
5569                 (*result_mailbox)->account_id                 = account_id;
5570                 (*result_mailbox)->local_yn                   = 1;
5571                 (*result_mailbox)->mailbox_name               = EM_SAFE_STRDUP(mailbox_name);
5572                 (*result_mailbox)->mailbox_type               = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
5573                 (*result_mailbox)->alias                      = EM_SAFE_STRDUP(mailbox_name);
5574                 (*result_mailbox)->deleted_flag               = 0;
5575                 (*result_mailbox)->modifiable_yn              = 1;
5576                 (*result_mailbox)->total_mail_count_on_server = 1;
5577                 (*result_mailbox)->has_archived_mails         = 0;
5578                 (*result_mailbox)->mail_slot_size             = 0x0FFFFFFF;
5579                 (*result_mailbox)->no_select                  = 0;
5580         } else {
5581                 if (local_yn == -1)
5582                         sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d AND mailbox_name = '%q'", account_id, mailbox_name);
5583                 else
5584                         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);
5585
5586                 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5587
5588                 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5589                         EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5590                         goto FINISH_OFF;
5591                 }
5592         }
5593
5594         ret = true;
5595
5596 FINISH_OFF:
5597
5598         if (err_code != NULL)
5599                 *err_code = error;
5600
5601         EM_PROFILE_END(profile_emstorage_get_mailbox_by_name);
5602         EM_DEBUG_FUNC_END("ret [%d]", ret);
5603         return ret;
5604 }
5605
5606 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)
5607 {
5608         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);
5609
5610         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_USER_DEFINED) || !output_mailbox) {
5611                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_type[%d], output_mailbox[%p]", account_id, mailbox_type, output_mailbox);
5612                 if (err_code != NULL)
5613                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5614                 return false;
5615         }
5616
5617         int ret = false;
5618         int error = EMAIL_ERROR_NONE;
5619         int result_count = 0;
5620         emstorage_mailbox_tbl_t *result_mailbox = NULL;
5621         char conditional_clause_string[QUERY_SIZE] = {0,};
5622
5623
5624         SNPRINTF(conditional_clause_string, QUERY_SIZE, "WHERE account_id = %d AND mailbox_type = %d ", account_id, mailbox_type);
5625
5626         EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5627
5628         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", true, false, &result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5629                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl error [%d]", error);
5630                 goto FINISH_OFF;
5631         }
5632
5633         ret = true;
5634
5635 FINISH_OFF:
5636         if (ret == true)
5637                 *output_mailbox = result_mailbox;
5638
5639         if (err_code != NULL)
5640                 *err_code = error;
5641
5642         EM_DEBUG_FUNC_END("ret [%d]", ret);
5643         return ret;
5644 }
5645
5646 INTERNAL_FUNC int emstorage_get_mailbox_by_id(char *multi_user_name, int input_mailbox_id, emstorage_mailbox_tbl_t** output_mailbox)
5647 {
5648         EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_mailbox[%p]", input_mailbox_id, output_mailbox);
5649
5650         if (input_mailbox_id <= 0 || !output_mailbox) {
5651                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: input_mailbox_id[%d] output_mailbox[%p]", input_mailbox_id, output_mailbox);
5652                 return  EMAIL_ERROR_INVALID_PARAM;
5653         }
5654
5655         int  ret = EMAIL_ERROR_NONE;
5656         int  result_count = 0;
5657         char conditional_clause_string[QUERY_SIZE] = {0, };
5658
5659         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE MBT.mailbox_id = %d", input_mailbox_id);
5660
5661         EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5662
5663         if ((ret = emstorage_query_mailbox_tbl(multi_user_name,
5664                                         conditional_clause_string,
5665                                         "", true, false, output_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5666                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", ret);
5667                 goto FINISH_OFF;
5668         }
5669
5670 FINISH_OFF:
5671
5672         EM_DEBUG_FUNC_END("ret [%d]", ret);
5673         return ret;
5674 }
5675
5676 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)
5677 {
5678         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);
5679
5680         if (account_id < 0 || !keyword || !result_mailbox) {
5681                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], keyword[%s], result_mailbox[%p]", account_id, keyword, result_mailbox);
5682
5683                 if (err_code != NULL)
5684                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5685                 return false;
5686         }
5687
5688         int ret = false;
5689         int error = EMAIL_ERROR_NONE;
5690         char conditional_clause_string[QUERY_SIZE] = {0, };
5691
5692         if (account_id == 0)
5693                 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5694                                 "WHERE alias LIKE \'%%%q%%\'", keyword);
5695         else if (account_id > 0)
5696                 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5697                                 "WHERE account_id = %d AND alias LIKE \'%%%q%%\'", account_id, keyword);
5698
5699         EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5700
5701         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, result_count)) != EMAIL_ERROR_NONE) {
5702                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5703                 goto FINISH_OFF;
5704         }
5705
5706         ret = true;
5707
5708 FINISH_OFF:
5709
5710         if (err_code != NULL)
5711                 *err_code = error;
5712
5713         EM_DEBUG_FUNC_END("ret [%d]", ret);
5714         return ret;
5715 }
5716
5717 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)
5718 {
5719         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);
5720         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_id) {
5721                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_id[%p]", account_id, mailbox_type, mailbox_id);
5722                 if (err_code != NULL)
5723                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5724                 return false;
5725         }
5726
5727         int rc, ret = false;
5728         int error = EMAIL_ERROR_NONE;
5729         emstorage_account_tbl_t* account = NULL;
5730         DB_STMT hStmt = NULL;
5731         char sql_query_string[QUERY_SIZE] = {0, };
5732
5733         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5734         EMSTORAGE_START_READ_TRANSACTION(transaction);
5735
5736         /*  Check whether the account exists. */
5737         if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
5738                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5739                 goto FINISH_OFF;
5740         }
5741
5742         if (account)
5743                 emstorage_free_account(&account, 1, NULL);
5744
5745         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);
5746
5747         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5748
5749
5750         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5751
5752         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5753                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5754
5755
5756         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5757         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5758                         ("sqlite3_step fail:%d", rc));
5759
5760         if (rc == SQLITE_DONE) {
5761                 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5762                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5763                 goto FINISH_OFF;
5764         }
5765
5766         _get_stmt_field_data_int(hStmt, mailbox_id, 0);
5767
5768         ret = true;
5769
5770 FINISH_OFF:
5771         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5772         if (hStmt != NULL) {
5773                 rc = sqlite3_finalize(hStmt);
5774                 if (rc != SQLITE_OK) {
5775                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5776                         error = EMAIL_ERROR_DB_FAILURE;
5777                 }
5778         }
5779
5780         if (err_code != NULL)
5781                 *err_code = error;
5782
5783         EM_DEBUG_FUNC_END("ret [%d]", ret);
5784         return ret;
5785 }
5786
5787 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)
5788 {
5789         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);
5790         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_name) {
5791                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
5792                 if (err_code != NULL)
5793                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5794                 return false;
5795         }
5796
5797         int rc, ret = false;
5798         int error = EMAIL_ERROR_NONE;
5799         emstorage_account_tbl_t* account = NULL;
5800         DB_STMT hStmt = NULL;
5801         char sql_query_string[QUERY_SIZE] = {0, };
5802
5803         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5804         EMSTORAGE_START_READ_TRANSACTION(transaction);
5805
5806         /*  Check whether the account exists. */
5807         if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
5808                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5809                 goto FINISH_OFF;
5810         }
5811
5812         if (account)
5813                 emstorage_free_account(&account, 1, NULL);
5814
5815         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);
5816
5817         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5818
5819
5820         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5821
5822         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5823                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5824
5825
5826         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5827         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5828                         ("sqlite3_step fail:%d", rc));
5829
5830         if (rc == SQLITE_DONE) {
5831                 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5832                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5833                 goto FINISH_OFF;
5834         }
5835
5836         _get_stmt_field_data_string(hStmt, mailbox_name, 0, 0);
5837
5838         ret = true;
5839
5840 FINISH_OFF:
5841         if (hStmt != NULL) {
5842                 rc = sqlite3_finalize(hStmt);
5843                 if (rc != SQLITE_OK) {
5844                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5845                         error = EMAIL_ERROR_DB_FAILURE;
5846                 }
5847         }
5848
5849         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5850
5851         if (err_code != NULL)
5852                 *err_code = error;
5853
5854         EM_DEBUG_FUNC_END("ret [%d]", ret);
5855         return ret;
5856 }
5857
5858 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)
5859 {
5860         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);
5861         int ret = false;
5862         int error = EMAIL_ERROR_NONE;
5863         char sql_query_string[QUERY_SIZE] = {0, };
5864         char *replaced_mailbox_name = NULL;
5865
5866         if (mailbox_name) {
5867                 if (strstr(mailbox_name, "'"))
5868                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5869                 else
5870                         replaced_mailbox_name = strdup(mailbox_name);
5871         }
5872
5873         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5874
5875         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5876
5877         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5878                         "UPDATE mail_box_tbl SET"
5879                         " modifiable_yn = %d"
5880                         " WHERE account_id = %d"
5881                         " AND local_yn = %d"
5882                         " AND mailbox_name = '%s'"
5883                         , modifiable_yn
5884                         , account_id
5885                         , local_yn
5886                         , replaced_mailbox_name);
5887
5888         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5889         if (error != EMAIL_ERROR_NONE) {
5890                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5891                 goto FINISH_OFF;
5892         }
5893
5894         ret = true;
5895
5896 FINISH_OFF:
5897         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5898
5899         EM_SAFE_FREE(replaced_mailbox_name);
5900
5901         if (err_code != NULL)
5902                 *err_code = error;
5903
5904         EM_DEBUG_FUNC_END("ret [%d]", ret);
5905         return ret;
5906
5907 }
5908
5909 INTERNAL_FUNC int emstorage_update_mailbox_total_count(char *multi_user_name,
5910                 int account_id,
5911                 int input_mailbox_id,
5912                 int total_count_on_server,
5913                 int transaction,
5914                 int *err_code)
5915 {
5916         EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], total_count_on_server[%d], "
5917                         "transaction[%d], err_code[%p]",
5918                         account_id, input_mailbox_id, total_count_on_server,  transaction, err_code);
5919
5920         int ret = false;
5921         int error = EMAIL_ERROR_NONE;
5922         char sql_query_string[QUERY_SIZE] = {0, };
5923
5924         if (account_id <= 0 || input_mailbox_id <= 0) {
5925                 EM_DEBUG_EXCEPTION("account_id[%d], input_mailbox_id[%d]", account_id, input_mailbox_id);
5926                 if (err_code != NULL)
5927                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5928                 EM_DEBUG_FUNC_END("ret [%d]", ret);
5929                 return false;
5930         }
5931
5932         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5933
5934         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5935
5936         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5937                         "UPDATE mail_box_tbl SET"
5938                         " total_mail_count_on_server = %d"
5939                         " WHERE account_id = %d"
5940                         " AND mailbox_id = %d"
5941                         , total_count_on_server
5942                         , account_id
5943                         , input_mailbox_id);
5944
5945         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
5946         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5947         if (error != EMAIL_ERROR_NONE) {
5948                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5949                 goto FINISH_OFF;
5950         }
5951
5952         ret = true;
5953
5954 FINISH_OFF:
5955
5956         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5957
5958         if (err_code != NULL)
5959                 *err_code = error;
5960
5961         EM_DEBUG_FUNC_END("ret [%d]", ret);
5962         return ret;
5963
5964 }
5965
5966 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)
5967 {
5968         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);
5969
5970         if (account_id < FIRST_ACCOUNT_ID || input_mailbox_id <= 0 || !result_mailbox) {
5971                 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);
5972
5973                 if (err_code != NULL)
5974                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5975                 EM_DEBUG_FUNC_END("ret [%d]", EMAIL_ERROR_INVALID_PARAM);
5976                 return false;
5977         }
5978
5979         int rc, ret = false;
5980         int error = EMAIL_ERROR_NONE;
5981         char sql_query_string[QUERY_SIZE] = {0, };
5982         DB_STMT hStmt = NULL;
5983         int i = 0;
5984
5985         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5986         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5987
5988         if (local_yn != -1) {
5989                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5990                                 "UPDATE mail_box_tbl SET"
5991                                 "  mailbox_id = ?"
5992                                 ", mailbox_name = ?"
5993                                 ", mailbox_type = ?"
5994                                 ", alias = ?"
5995                                 ", deleted_flag = ?"
5996                                 ", modifiable_yn= ?"
5997                                 ", mail_slot_size= ?"
5998                                 ", total_mail_count_on_server = ?"
5999                                 " WHERE account_id = %d"
6000                                 " AND local_yn = %d"
6001                                 " AND mailbox_id = '%d'"
6002                                 , account_id
6003                                 , local_yn
6004                                 , input_mailbox_id);
6005         } else {
6006                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6007                                 "UPDATE mail_box_tbl SET"
6008                                 "  mailbox_id = ?"
6009                                 ", mailbox_name = ?"
6010                                 ", mailbox_type = ?"
6011                                 ", alias = ?"
6012                                 ", deleted_flag = ?"
6013                                 ", modifiable_yn= ?"
6014                                 ", mail_slot_size= ?"
6015                                 ", total_mail_count_on_server = ?"
6016                                 " WHERE account_id = %d"
6017                                 " AND mailbox_id = '%d'"
6018                                 , account_id
6019                                 , input_mailbox_id);
6020         }
6021
6022
6023
6024         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6025         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6026                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6027
6028         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_id);
6029         _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);
6030         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_type);
6031         _bind_stmt_field_data_string(hStmt, i++, (char *)result_mailbox->alias ? result_mailbox->alias : "", 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
6032         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->deleted_flag);
6033         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->modifiable_yn);
6034         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mail_slot_size);
6035         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->total_mail_count_on_server);
6036
6037
6038         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6039         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
6040                         ("sqlite3_step fail:%d", rc));
6041         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6042                         ("sqlite3_step fail:%d", rc));
6043
6044         ret = true;
6045
6046 FINISH_OFF:
6047         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6048
6049         if (hStmt != NULL) {
6050                 rc = sqlite3_finalize(hStmt);
6051                 if (rc != SQLITE_OK) {
6052                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6053                         error = EMAIL_ERROR_DB_FAILURE;
6054                 }
6055         }
6056
6057         if (err_code != NULL)
6058                 *err_code = error;
6059
6060         EM_DEBUG_FUNC_END("ret [%d]", ret);
6061         return ret;
6062 }
6063
6064 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)
6065 {
6066         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);
6067
6068         if (account_id < FIRST_ACCOUNT_ID) {
6069                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
6070
6071                 if (err_code != NULL)
6072                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6073                 return false;
6074         }
6075
6076         int rc, ret = false;
6077         int error = EMAIL_ERROR_NONE;
6078         char sql_query_string[QUERY_SIZE] = {0, };
6079
6080         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6081         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6082
6083         EM_DEBUG_LOG("emstorage_update_mailbox_type");
6084
6085         DB_STMT hStmt_box_tbl = NULL;
6086         DB_STMT hStmt_mail_tbl = NULL;
6087         int i = 0;
6088
6089         /*  Update mail_box_tbl */
6090         if (local_yn != -1) {
6091                 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
6092                                 "UPDATE mail_box_tbl SET"
6093                                 " mailbox_type = ?"
6094                                 " WHERE account_id = %d"
6095                                 " AND local_yn = %d"
6096                                 " AND mailbox_id = '%d'"
6097                                 , account_id
6098                                 , local_yn
6099                                 , input_mailbox_id);
6100         } else {
6101                 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
6102                                 "UPDATE mail_box_tbl SET"
6103                                 " mailbox_type = ?"
6104                                 " WHERE account_id = %d"
6105                                 " AND mailbox_id = '%d'"
6106                                 , account_id
6107                                 , input_mailbox_id);
6108         }
6109
6110         EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6111
6112         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_box_tbl, NULL), rc);
6113
6114         if (SQLITE_OK != rc) {
6115                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6116                 error = EMAIL_ERROR_DB_FAILURE;
6117                 goto FINISH_OFF;
6118         }
6119
6120         _bind_stmt_field_data_int(hStmt_box_tbl, i++, new_mailbox_type);
6121
6122
6123         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_box_tbl), rc);
6124
6125         if (rc == SQLITE_FULL) {
6126                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6127                 error   = EMAIL_ERROR_MAIL_MEMORY_FULL;
6128                 goto FINISH_OFF;
6129         }
6130
6131         if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
6132                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6133                 error = EMAIL_ERROR_DB_FAILURE;
6134                 goto FINISH_OFF;
6135         }
6136
6137
6138         /*  Update mail_tbl */
6139         i = 0;
6140         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6141                         "UPDATE mail_tbl SET"
6142                         " mailbox_type = ?"
6143                         " WHERE account_id = %d"
6144                         " AND mailbox_id = '%d'"
6145                         , account_id
6146                         , input_mailbox_id);
6147
6148         EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
6149
6150         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_mail_tbl, NULL), rc);
6151         if (SQLITE_OK != rc) {
6152                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6153                 error = EMAIL_ERROR_DB_FAILURE;
6154                 goto FINISH_OFF;
6155         }
6156
6157         _bind_stmt_field_data_int(hStmt_mail_tbl, i++, new_mailbox_type);
6158
6159         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_mail_tbl), rc);
6160         if (rc == SQLITE_FULL) {
6161                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6162                 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
6163                 goto FINISH_OFF;
6164         }
6165
6166         if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
6167                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6168                 error = EMAIL_ERROR_DB_FAILURE;
6169                 goto FINISH_OFF;
6170         }
6171
6172         ret = true;
6173
6174 FINISH_OFF:
6175         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6176
6177         if (hStmt_box_tbl != NULL) {
6178                 rc = sqlite3_finalize(hStmt_box_tbl);
6179                 if (rc != SQLITE_OK) {
6180                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6181                         error = EMAIL_ERROR_DB_FAILURE;
6182                 }
6183         }
6184
6185         if (hStmt_mail_tbl != NULL) {
6186                 rc = sqlite3_finalize(hStmt_mail_tbl);
6187                 if (rc != SQLITE_OK) {
6188                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6189                         error = EMAIL_ERROR_DB_FAILURE;
6190                 }
6191         }
6192
6193         if (err_code != NULL)
6194                 *err_code = error;
6195
6196         EM_DEBUG_FUNC_END("ret [%d]", ret);
6197         return ret;
6198 }
6199
6200 INTERNAL_FUNC int emstorage_set_local_mailbox(char *multi_user_name, int input_mailbox_id, int input_is_local_mailbox, int transaction)
6201 {
6202         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);
6203
6204         int rc, ret = false;
6205         int error = EMAIL_ERROR_NONE;
6206         char sql_query_string[QUERY_SIZE] = {0, };
6207
6208         if (input_mailbox_id < 0) {
6209                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6210                 return EMAIL_ERROR_INVALID_PARAM;
6211         }
6212
6213         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6214
6215         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6216
6217         EM_DEBUG_LOG("emstorage_update_mailbox_type");
6218
6219         DB_STMT hStmt = NULL;
6220         int i = 0;
6221
6222         /*  Update mail_box_tbl */
6223         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6224                         "UPDATE mail_box_tbl SET"
6225                         " local_yn = ?"
6226                         " WHERE mailbox_id = %d"
6227                         , input_mailbox_id);
6228
6229         EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6230
6231         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6232         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6233                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6234
6235         _bind_stmt_field_data_int(hStmt, i++, input_is_local_mailbox);
6236
6237         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6238         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
6239                         ("sqlite3_step fail:%d", rc));
6240         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6241                         ("sqlite3_step fail:%d", rc));
6242         /*
6243            if (hStmt != NULL) {
6244            rc = sqlite3_finalize(hStmt);
6245            if (rc != SQLITE_OK) {
6246            EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6247            error = EMAIL_ERROR_DB_FAILURE;
6248            }
6249            hStmt = NULL;
6250            }
6251            */
6252         ret = true;
6253
6254 FINISH_OFF:
6255         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6256
6257         if (hStmt != NULL) {
6258                 rc = sqlite3_finalize(hStmt);
6259                 if (rc != SQLITE_OK) {
6260                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6261                         error = EMAIL_ERROR_DB_FAILURE;
6262                 }
6263         }
6264
6265         EM_DEBUG_FUNC_END("error [%d]", error);
6266         return error;
6267 }
6268
6269 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)
6270 {
6271         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);
6272         int i = 0;
6273         int err = EMAIL_ERROR_NONE;
6274         int result = false;
6275         int cur_mailbox_id_string = 0;
6276         int mailbox_id_string_buffer_length = 0;
6277         char  sql_query_string[QUERY_SIZE] = {0, };
6278         char *mailbox_id_string_buffer = NULL;
6279         char *parameter_string = NULL;
6280         sqlite3 *local_db_handle = NULL;
6281
6282         if (input_mailbox_id_array == NULL || input_mailbox_id_count == 0 || input_field_name == NULL) {
6283                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6284                 return EMAIL_ERROR_INVALID_PARAM;
6285         }
6286
6287         local_db_handle = emstorage_get_db_connection(multi_user_name);
6288
6289         /* Generating mail id list string */
6290         mailbox_id_string_buffer_length = MAILBOX_ID_STRING_LENGTH * input_mailbox_id_count;
6291
6292         mailbox_id_string_buffer = em_malloc(mailbox_id_string_buffer_length);
6293
6294         if (!mailbox_id_string_buffer) {
6295                 EM_DEBUG_EXCEPTION("em_mallocfailed");
6296                 err = EMAIL_ERROR_OUT_OF_MEMORY;
6297                 goto FINISH_OFF;
6298         }
6299
6300         for (i = 0; i < input_mailbox_id_count; i++)
6301                 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]);
6302
6303         if (EM_SAFE_STRLEN(mailbox_id_string_buffer) > 1)
6304                 mailbox_id_string_buffer[EM_SAFE_STRLEN(mailbox_id_string_buffer) - 1] = NULL_CHAR;
6305
6306         /* Generating notification parameter string */
6307         parameter_string = em_malloc(mailbox_id_string_buffer_length + EM_SAFE_STRLEN(input_field_name) + 2);
6308
6309         if (!parameter_string) {
6310                 EM_DEBUG_EXCEPTION("em_mallocfailed");
6311                 err = EMAIL_ERROR_OUT_OF_MEMORY;
6312                 goto FINISH_OFF;
6313         }
6314
6315         SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", input_field_name, 0x01, mailbox_id_string_buffer);
6316
6317         /* Write query string */
6318         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);
6319
6320         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
6321
6322         /* Execute query */
6323         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
6324         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6325         if (err != EMAIL_ERROR_NONE) {
6326                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
6327                 goto FINISH_OFF;
6328         }
6329
6330         if (sqlite3_changes(local_db_handle) == 0)
6331                 EM_DEBUG_LOG("no mail matched...");
6332
6333         result = true;
6334
6335 FINISH_OFF:
6336         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, err);
6337
6338         if (err == EMAIL_ERROR_NONE && parameter_string) {
6339                 if (!emcore_notify_storage_event(NOTI_MAILBOX_FIELD_UPDATE, input_account_id, 0, parameter_string, input_value))
6340                         EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_MAILBOX_FIELD_UPDATE [%s,%d]",
6341                                         input_field_name, input_value);
6342         }
6343
6344         EM_SAFE_FREE(mailbox_id_string_buffer);
6345         EM_SAFE_FREE(parameter_string);
6346
6347         EM_DEBUG_FUNC_END("err [%d]", err);
6348         return err;
6349 }
6350
6351 INTERNAL_FUNC int emstorage_add_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox_tbl, int transaction, int *err_code)
6352 {
6353         EM_DEBUG_FUNC_BEGIN("mailbox_tbl[%p], transaction[%d], err_code[%p]", mailbox_tbl, transaction, err_code);
6354
6355         if (!mailbox_tbl) {
6356                 if (err_code != NULL)
6357                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6358                 return false;
6359         }
6360
6361         int rc, ret = false;
6362         int error = EMAIL_ERROR_NONE;
6363         DB_STMT hStmt = NULL;
6364         char sql_query_string[QUERY_SIZE] = {0,};
6365         char **result = NULL;
6366         time_t current_time;
6367         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6368         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6369
6370         EM_SAFE_STRNCPY(sql_query_string, "SELECT max(rowid) FROM mail_box_tbl;", sizeof(sql_query_string));
6371
6372         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6373         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6374                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6375
6376         time(&current_time);
6377
6378         if (NULL == result[1])
6379                 rc = 1;
6380         else
6381                 rc = atoi(result[1]) + 1;
6382         sqlite3_free_table(result);
6383
6384         memset(sql_query_string, 0, sizeof(char) * QUERY_SIZE);
6385
6386         mailbox_tbl->mailbox_id = rc;
6387
6388         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6389                         "INSERT INTO mail_box_tbl VALUES "
6390                         "(?"    /* mailbox_id */
6391                         ", ?"    /* account_id */
6392                         ", ?"    /* local_yn */
6393                         ", ?"    /* mailbox_name */
6394                         ", ?"    /* mailbox_type */
6395                         ", ?"    /* alias */
6396                         ", ?"    /* deleted_flag */
6397                         ", ?"    /* modifiable_yn */
6398                         ", ?"    /* total_mail_count_on_server */
6399                         ", ?"    /* has_archived_mails */
6400                         ", ?"    /* mail_slot_size */
6401                         ", ?"    /* no_select */
6402                         ", ?"    /* last_sync_time */
6403                         ", ?"    /* eas_data_length */
6404                         ", ?"    /* eas_data */
6405                         ")");
6406
6407
6408         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6409         EM_DEBUG_LOG_DEV("After sqlite3_prepare hStmt = %p", hStmt);
6410         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6411                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6412
6413         int col_index = 0;
6414
6415         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_id);
6416         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->account_id);
6417         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->local_yn);
6418         _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
6419         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_type);
6420         _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->alias, 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
6421         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->deleted_flag);
6422         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->modifiable_yn);
6423         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->total_mail_count_on_server);
6424         _bind_stmt_field_data_int(hStmt, col_index++, 0);
6425         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mail_slot_size);
6426         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->no_select);
6427         _bind_stmt_field_data_int(hStmt, col_index++, current_time);
6428         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->eas_data_length);
6429         _bind_stmt_field_data_blob(hStmt, col_index++, (void*)mailbox_tbl->eas_data, mailbox_tbl->eas_data_length);
6430
6431
6432         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6433         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6434                         ("sqlite3_step fail:%dn", rc));
6435
6436         ret = true;
6437
6438 FINISH_OFF:
6439         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6440         if (hStmt != NULL) {
6441                 rc = sqlite3_finalize(hStmt);
6442                 if (rc != SQLITE_OK) {
6443                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6444                         error = EMAIL_ERROR_DB_FAILURE;
6445                 }
6446         }
6447
6448         if (error == EMAIL_ERROR_NONE) {
6449                 if (!emcore_notify_storage_event(NOTI_MAILBOX_ADD, mailbox_tbl->account_id, mailbox_tbl->mailbox_id,
6450                                         mailbox_tbl->mailbox_name, mailbox_tbl->mailbox_type))
6451                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6452         }
6453
6454         if (err_code != NULL)
6455                 *err_code = error;
6456
6457         EM_DEBUG_FUNC_END("ret [%d]", ret);
6458         return ret;
6459 }
6460
6461 INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(char *multi_user_name, int account_id, int modifiable_yn, int transaction, int *err_code)
6462 {
6463         EM_DEBUG_FUNC_BEGIN("account_id[%d], modifiable_yn[%d], err_code[%p]", account_id, modifiable_yn, err_code);
6464
6465         if (account_id < FIRST_ACCOUNT_ID) {
6466
6467                 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
6468
6469                 if (err_code != NULL)
6470                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6471                 return false;
6472         }
6473         int rc, ret = false;
6474         int error = EMAIL_ERROR_NONE;
6475         char sql_query_string[QUERY_SIZE] = {0,};
6476         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6477         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6478
6479
6480         SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET modifiable_yn = %d WHERE account_id = %d", modifiable_yn, account_id);
6481
6482         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6483         if (error != EMAIL_ERROR_NONE) {
6484                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6485                 goto FINISH_OFF;
6486         }
6487
6488         rc = sqlite3_changes(local_db_handle);
6489         if (rc == 0)
6490                 EM_DEBUG_EXCEPTION("All mailbox_name modifiable_yn set to 0 already");
6491
6492
6493         ret = true;
6494
6495 FINISH_OFF:
6496         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6497
6498         if (err_code != NULL)
6499                 *err_code = error;
6500         EM_DEBUG_FUNC_END("ret [%d]", ret);
6501         return ret;
6502
6503
6504 }
6505
6506 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)
6507 {
6508         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);
6509
6510         if (account_id < FIRST_ACCOUNT_ID) {
6511                 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
6512
6513                 if (err_code != NULL)
6514                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6515                 return false;
6516         }
6517
6518         int rc, ret = false;
6519         int error = EMAIL_ERROR_NONE;
6520         char sql_query_string[QUERY_SIZE] = {0, };
6521         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6522         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6523
6524         if (local_yn == -1)
6525                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_box_tbl WHERE account_id = %d ", account_id);
6526         else
6527                 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);
6528
6529         if (input_mailbox_id > 0) {             /* 0 means all mailbox */
6530                 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);
6531         }
6532
6533         EM_DEBUG_LOG_SEC("mailbox sql_query_string [%s]", sql_query_string);
6534         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6535         if (error != EMAIL_ERROR_NONE) {
6536                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6537                 goto FINISH_OFF;
6538         }
6539
6540         rc = sqlite3_changes(local_db_handle);
6541         if (rc == 0) {
6542                 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
6543                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
6544                 ret = true;
6545         }
6546         ret = true;
6547
6548 FINISH_OFF:
6549
6550         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6551
6552         if (error == EMAIL_ERROR_NONE) {
6553                 if (!emcore_notify_storage_event(NOTI_MAILBOX_DELETE, account_id, input_mailbox_id, NULL, 0))
6554                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6555         }
6556
6557         if (err_code != NULL)
6558                 *err_code = error;
6559
6560         EM_DEBUG_FUNC_END("ret [%d]", ret);
6561         return ret;
6562 }
6563
6564 INTERNAL_FUNC int emstorage_free_mailbox(emstorage_mailbox_tbl_t** mailbox_list, int count, int *err_code)
6565 {
6566         EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d], err_code[%p]", mailbox_list, count, err_code);
6567
6568         int ret = false;
6569         int error = EMAIL_ERROR_NONE;
6570
6571         if (count > 0) {
6572                 if (!mailbox_list || !*mailbox_list) {
6573                         EM_DEBUG_EXCEPTION(" mailbox_list[%p], count[%d]", mailbox_list, count);
6574
6575                         error = EMAIL_ERROR_INVALID_PARAM;
6576                         goto FINISH_OFF;
6577                 }
6578
6579                 emstorage_mailbox_tbl_t* p = *mailbox_list;
6580                 int i = 0;
6581
6582                 for (; i < count; i++) {
6583                         EM_SAFE_FREE(p[i].mailbox_name);
6584                         EM_SAFE_FREE(p[i].alias);
6585                         EM_SAFE_FREE(p[i].eas_data); /*valgrind*/
6586                 }
6587
6588                 EM_SAFE_FREE(p); *mailbox_list = NULL;
6589         }
6590
6591         ret = true;
6592
6593 FINISH_OFF:
6594         if (err_code != NULL)
6595                 *err_code = error;
6596
6597         EM_DEBUG_FUNC_END("ret [%d]", ret);
6598         return ret;
6599 }
6600
6601 INTERNAL_FUNC int emstorage_get_count_read_mail_uid(char *multi_user_name, int account_id, char *mailbox_name, int *count, int transaction, int *err_code)
6602 {
6603         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);
6604
6605         if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !count) {
6606                 EM_DEBUG_EXCEPTION("Invalid params");
6607
6608                 if (err_code != NULL)
6609                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6610                 return false;
6611         }
6612
6613         int rc = -1, ret = false;
6614         int error = EMAIL_ERROR_NONE;
6615         char sql_query_string[QUERY_SIZE] = {0, };
6616         char *replaced_mailbox_name = NULL;
6617
6618         if (strstr(mailbox_name, "'"))
6619                 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6620         else
6621                 replaced_mailbox_name = EM_SAFE_STRDUP(mailbox_name);
6622
6623         EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6624
6625
6626         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6627         EMSTORAGE_START_READ_TRANSACTION(transaction);
6628         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);
6629         EM_DEBUG_LOG_SEC(">>> SQL [ %s ] ", sql_query_string);
6630
6631         char **result;
6632
6633         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6634         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6635                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6636
6637         *count = atoi(result[1]);
6638         sqlite3_free_table(result);
6639
6640         ret = true;
6641
6642 FINISH_OFF:
6643         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6644
6645         EM_SAFE_FREE(replaced_mailbox_name);
6646
6647         if (err_code != NULL)
6648                 *err_code = error;
6649
6650         EM_DEBUG_FUNC_END("ret [%d]", ret);
6651         return ret;
6652 }
6653
6654 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)
6655 {
6656         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);
6657
6658         if (account_id < FIRST_ACCOUNT_ID || !uid || !exist) {
6659                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], uid[%p], exist[%p]", account_id, mailbox_name , uid, exist);
6660
6661                 if (err_code != NULL)
6662                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6663                 return false;
6664         }
6665
6666         int rc = -1, ret = false;
6667         int error = EMAIL_ERROR_NONE;
6668         char sql_query_string[QUERY_SIZE] = {0, };
6669         char *replaced_mailbox_name = NULL;
6670
6671         EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6672
6673         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6674         EMSTORAGE_START_READ_TRANSACTION(transaction);
6675
6676         if (mailbox_name) {
6677                 if (strstr(mailbox_name, "'"))
6678                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6679                 else
6680                         replaced_mailbox_name = strdup(mailbox_name);
6681
6682                 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);
6683         } else {
6684                 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);
6685         }
6686
6687         char **result = NULL;
6688
6689         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6690         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6691                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6692
6693         *exist = atoi(result[1]);
6694         sqlite3_free_table(result);
6695
6696         if (*exist > 0)
6697                 *exist = 1;
6698         else
6699                 *exist = 0;
6700
6701         ret = true;
6702
6703 FINISH_OFF:
6704         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6705
6706         EM_SAFE_FREE(replaced_mailbox_name);
6707
6708         if (err_code != NULL)
6709                 *err_code = error;
6710
6711         EM_DEBUG_FUNC_END("ret [%d]", ret);
6712         return ret;
6713 }
6714
6715 INTERNAL_FUNC int emstorage_get_downloaded_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code)
6716 {
6717         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], err_code[%p]", mail_id, mail, err_code);
6718
6719         if (!mail || mail_id <= 0) {
6720                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
6721                 if (err_code != NULL)
6722                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6723                 return false;
6724         }
6725
6726         int rc, ret = false;
6727         int error = EMAIL_ERROR_NONE;
6728         DB_STMT hStmt = NULL;
6729         char sql_query_string[QUERY_SIZE] = {0, };
6730
6731         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6732         EMSTORAGE_START_READ_TRANSACTION(transaction);
6733
6734         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE local_uid = %d", mail_id);
6735
6736         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6737         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6738
6739         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6740                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6741
6742
6743         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6744         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6745                         ("sqlite3_step fail:%d", rc));
6746
6747         *mail = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
6748         if (*mail == NULL) {
6749                 error = EMAIL_ERROR_OUT_OF_MEMORY;
6750                 EM_DEBUG_EXCEPTION("Memory allocation for mail failed.");
6751                 goto FINISH_OFF;
6752
6753         }
6754         memset(*mail, 0x00, sizeof(emstorage_mail_tbl_t));
6755
6756         _get_stmt_field_data_int(hStmt, &((*mail)->account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6757         _get_stmt_field_data_int(hStmt, &((*mail)->mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6758         _get_stmt_field_data_string(hStmt, &((*mail)->server_mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6759         _get_stmt_field_data_int(hStmt, &((*mail)->mail_id), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6760         _get_stmt_field_data_string(hStmt, &((*mail)->server_mail_id), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6761         _get_stmt_field_data_int(hStmt, &((*mail)->mail_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6762         _get_stmt_field_data_char(hStmt, &((*mail)->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6763
6764         (*mail)->server_mail_status = 1;
6765
6766         ret = true;
6767
6768 FINISH_OFF:
6769
6770         if (hStmt != NULL) {
6771                 rc = sqlite3_finalize(hStmt);
6772                 if (rc != SQLITE_OK) {
6773                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6774                         error = EMAIL_ERROR_DB_FAILURE;
6775                 }
6776         }
6777
6778         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6779
6780         if (err_code != NULL)
6781                 *err_code = error;
6782
6783         EM_DEBUG_FUNC_END("ret [%d]", ret);
6784         return ret;
6785 }
6786
6787 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)
6788 {
6789         EM_PROFILE_BEGIN(emStorageGetDownloadList);
6790         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);
6791         if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid || !count) {
6792                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_id[%d], read_mail_uid[%p], count[%p]", account_id, mailbox_id, read_mail_uid, count);
6793
6794                 if (err_code != NULL)
6795                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6796                 return false;
6797         }
6798
6799         int rc, ret = false;
6800         int error = EMAIL_ERROR_NONE;
6801
6802         DB_STMT hStmt = NULL;
6803         char sql_query_string[QUERY_SIZE] = {0, };
6804
6805         emstorage_read_mail_uid_tbl_t* p_data_tbl = NULL;
6806         int i = 0;
6807
6808         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6809         EMSTORAGE_START_READ_TRANSACTION(transaction);
6810
6811         if (mailbox_id)
6812                 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);
6813         else
6814                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
6815
6816         EM_DEBUG_LOG_SEC(" sql_query_string : %s", sql_query_string);
6817
6818
6819
6820         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6821         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6822         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6823                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6824
6825
6826         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6827         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6828                         ("sqlite3_step fail:%d", rc));
6829
6830         char **result;
6831         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL); */
6832         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL), rc);
6833         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6834                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6835
6836         sqlite3_free_table(result);
6837         if (*count == 0) {
6838                 EM_DEBUG_LOG("No mail found in mail_read_mail_uid_tbl");
6839                 ret = true;
6840                 goto FINISH_OFF;
6841         }
6842
6843
6844         if (!(p_data_tbl = (emstorage_read_mail_uid_tbl_t*)malloc(sizeof(emstorage_read_mail_uid_tbl_t) * *count))) {
6845                 EM_DEBUG_EXCEPTION(" malloc failed...");
6846                 error = EMAIL_ERROR_OUT_OF_MEMORY;
6847                 goto FINISH_OFF;
6848         }
6849
6850         memset(p_data_tbl, 0x00, sizeof(emstorage_read_mail_uid_tbl_t)*(*count));
6851
6852         for (i = 0; i < *count; ++i) {
6853                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6854                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6855                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6856                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].local_uid), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6857                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].server_uid), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6858                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rfc822_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6859                 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6860                 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_flagged_field), FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6861
6862                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6863                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6864                                 ("sqlite3_step fail:%d", rc));
6865         }
6866
6867         ret = true;
6868
6869 FINISH_OFF:
6870         if (ret == true)
6871                 *read_mail_uid = p_data_tbl;
6872         else if (p_data_tbl)
6873                 emstorage_free_read_mail_uid(&p_data_tbl, *count, NULL);
6874
6875         if (hStmt != NULL) {
6876                 rc = sqlite3_finalize(hStmt);
6877                 if (rc != SQLITE_OK) {
6878                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6879                         error = EMAIL_ERROR_DB_FAILURE;
6880                 }
6881         }
6882
6883         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6884
6885         if (err_code != NULL)
6886                 *err_code = error;
6887
6888         EM_PROFILE_END(emStorageGetDownloadList);
6889         EM_DEBUG_FUNC_END("ret [%d]", ret);
6890         return ret;
6891 }
6892
6893 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)
6894 {
6895         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);
6896
6897         if (account_id < FIRST_ACCOUNT_ID || !mail_size) {
6898                 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);
6899
6900                 if (err_code != NULL)
6901                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6902                 return false;
6903         }
6904
6905         int rc, ret = false;
6906         int error = EMAIL_ERROR_NONE;
6907         DB_STMT hStmt = NULL;
6908         char sql_query_string[QUERY_SIZE] = {0, };
6909         char *replaced_mailbox_name = NULL;
6910
6911         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6912         EMSTORAGE_START_READ_TRANSACTION(transaction);
6913
6914         if (mailbox_name) {
6915                 if (strstr(mailbox_name, "'"))
6916                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6917                 else
6918                         replaced_mailbox_name = strdup(mailbox_name);
6919
6920                 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6921
6922                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6923                                 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6924                                 "WHERE account_id = %d "
6925                                 "AND mailbox_id = '%s' "
6926                                 "AND local_uid = %d "
6927                                 "AND mailbox_name = '%s' "
6928                                 "AND server_uid = '%s'",
6929                                 account_id, mailbox_id, local_uid, replaced_mailbox_name, uid);
6930         } else {
6931                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6932                                 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6933                                 "WHERE account_id = %d "
6934                                 "AND mailbox_id = '%s' "
6935                                 "AND local_uid = %d "
6936                                 "AND server_uid = '%s'",
6937                                 account_id, mailbox_id, local_uid, uid);
6938         }
6939
6940
6941         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6942         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6943                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6944
6945
6946         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6947         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6948                         ("sqlite3_step fail:%d", rc));
6949
6950         if (rc == SQLITE_DONE) {
6951                 EM_DEBUG_LOG("no matched mail found....");
6952                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
6953                 goto FINISH_OFF;
6954         }
6955
6956         _get_stmt_field_data_int(hStmt, mail_size, 0);
6957
6958         ret = true;
6959
6960 FINISH_OFF:
6961         EM_SAFE_FREE(replaced_mailbox_name);
6962
6963         if (hStmt != NULL) {
6964                 rc = sqlite3_finalize(hStmt);
6965                 if (rc != SQLITE_OK) {
6966                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6967                         error = EMAIL_ERROR_DB_FAILURE;
6968                 }
6969         }
6970
6971         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6972
6973         if (err_code != NULL)
6974                 *err_code = error;
6975
6976         EM_DEBUG_FUNC_END("ret [%d]", ret);
6977         return ret;
6978 }
6979
6980 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)
6981 {
6982         EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], transaction[%d], err_code[%p]", read_mail_uid, transaction, err_code);
6983
6984         if (!read_mail_uid) {
6985                 EM_DEBUG_EXCEPTION("read_mail_uid[%p]", read_mail_uid);
6986                 if (err_code != NULL)
6987                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6988                 return false;
6989         }
6990
6991         int rc, rc2,  ret = false;
6992         int error = EMAIL_ERROR_NONE;
6993         DB_STMT hStmt = NULL;
6994         char sql_query_string[QUERY_SIZE] = {0, };
6995
6996         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6997         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6998
6999         char *sql = "SELECT max(rowid) FROM mail_read_mail_uid_tbl;";
7000         char **result = NULL;
7001
7002
7003         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7004         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7005                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7006
7007         if (NULL == result[1]) rc = 1;
7008         else rc = atoi(result[1])+1;
7009         sqlite3_free_table(result);
7010
7011         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7012                         "INSERT INTO mail_read_mail_uid_tbl VALUES "
7013                         "(?"  /* account_id */
7014                         ", ?"  /* mailbox_id */
7015                         ", ?"  /* mailbox_name */
7016                         ", ?"  /* local_uid */
7017                         ", ?"  /* server_uid */
7018                         ", ?"  /* rfc822_size */
7019                         ", ?"  /* sync_status */
7020                         ", ?"  /* flags_seen_field */
7021                         ", ?"  /* flags_flagged_field */
7022                         ", ?)");
7023
7024
7025         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
7026         if (rc2 != SQLITE_OK) {
7027                 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7028                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
7029
7030                 error = EMAIL_ERROR_DB_FAILURE;
7031                 goto FINISH_OFF;
7032         }
7033
7034         EM_DEBUG_LOG("account_id[%d] mailbox_id[%d] local_uid [%d]"
7035                         "server_uid[%s] rfc822_size[%d] rc[%d]",
7036                         read_mail_uid->account_id, read_mail_uid->mailbox_id, read_mail_uid->local_uid,
7037                         read_mail_uid->server_uid, read_mail_uid->rfc822_size, rc);
7038
7039         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->account_id);
7040         _bind_stmt_field_data_int(hStmt, LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->mailbox_id);
7041         _bind_stmt_field_data_int(hStmt, LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->local_uid);
7042         _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);
7043         _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);
7044         _bind_stmt_field_data_int(hStmt, RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->rfc822_size);
7045         _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_seen_field);
7046         _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_flagged_field);
7047         _bind_stmt_field_data_int(hStmt, IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, rc);
7048
7049
7050         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7051         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
7052                         ("sqlite3_step fail:%d", rc));
7053         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7054                         ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
7055
7056
7057         ret = true;
7058
7059 FINISH_OFF:
7060         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7061         if (hStmt != NULL) {
7062                 rc = sqlite3_finalize(hStmt);
7063                 if (rc != SQLITE_OK) {
7064                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7065                         error = EMAIL_ERROR_DB_FAILURE;
7066                 }
7067         }
7068
7069         if (err_code != NULL)
7070                 *err_code = error;
7071
7072         EM_DEBUG_FUNC_END("ret [%d]", ret);
7073         return ret;
7074 }
7075
7076 #ifdef __FEATURE_BODY_SEARCH__
7077 INTERNAL_FUNC int emstorage_add_mail_text(char *multi_user_name, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
7078 {
7079         EM_DEBUG_FUNC_BEGIN("mail_text[%p], transaction[%d], err_code[%p]", mail_text, transaction, err_code);
7080
7081         if (!mail_text) {
7082                 EM_DEBUG_EXCEPTION("mail_text[%p]", mail_text);
7083                 if (err_code != NULL)
7084                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7085                 return false;
7086         }
7087
7088         int rc, rc2,  ret = false;
7089         int error = EMAIL_ERROR_NONE;
7090         DB_STMT hStmt = NULL;
7091         char sql_query_string[QUERY_SIZE] = {0, };
7092
7093         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7094         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7095
7096         char *sql = "SELECT max(rowid) FROM mail_text_tbl;";
7097         char **result = NULL;
7098
7099         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7100         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7101                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7102         sqlite3_free_table(result);
7103
7104         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7105                         "INSERT INTO mail_text_tbl VALUES "
7106                         "(?"
7107                         ", ?"
7108                         ", ?"
7109                         ", ?)");
7110
7111         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
7112         if (rc2 != SQLITE_OK) {
7113                 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7114                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc2, sqlite3_errmsg(local_db_handle));
7115
7116                 error = EMAIL_ERROR_DB_FAILURE;
7117                 goto FINISH_OFF;
7118         }
7119
7120         EM_DEBUG_LOG("mail_id[%d] account_id[%d] mailbox_id[%d]", mail_text->mail_id,
7121                         mail_text->account_id, mail_text->mailbox_id);
7122         EM_DEBUG_LOG_DEV("body_text VALUE [%s] ", mail_text->body_text);
7123
7124         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mail_id);
7125         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->account_id);
7126         _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mailbox_id);
7127         _bind_stmt_field_data_string(hStmt, BODY_TEXT_IDX_IN_MAIL_TEXT_TBL, (char *)mail_text->body_text, 0, -1);
7128
7129         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7130         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
7131                         ("sqlite3_step fail:%d", rc));
7132         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7133                         ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
7134
7135         ret = true;
7136
7137 FINISH_OFF:
7138         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7139         if (hStmt != NULL) {
7140                 rc = sqlite3_finalize(hStmt);
7141                 if (rc != SQLITE_OK) {
7142                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7143                         error = EMAIL_ERROR_DB_FAILURE;
7144                 }
7145         }
7146
7147         if (err_code != NULL)
7148                 *err_code = error;
7149
7150         EM_DEBUG_FUNC_END("ret [%d]", ret);
7151         return ret;
7152 }
7153 #endif
7154
7155 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)
7156 {
7157         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);
7158
7159         if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid) {
7160                 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);
7161
7162                 if (err_code != NULL)
7163                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7164                 return false;
7165         }
7166
7167         int rc, ret = false;
7168         int error = EMAIL_ERROR_NONE;
7169         DB_STMT hStmt = NULL;
7170         char sql_query_string[QUERY_SIZE] = {0, };
7171
7172         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7173
7174         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7175
7176         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7177                         "UPDATE mail_read_mail_uid_tbl SET"
7178                         "  account_id = ?"
7179                         ", mailbox_id = ?"
7180                         ", mailbox_name = ?"
7181                         ", local_uid  = ?"
7182                         ", server_uid = ?"
7183                         ", rfc822_size = ?"
7184                         ", flags_seen_field  = ?"
7185                         ", flags_flagged_field  = ?"
7186                         " WHERE account_id = ?"
7187                         " AND mailbox_id  = ?"
7188                         " AND local_uid   = ?"
7189                         " AND mailbox_name= ?"
7190                         " AND server_uid = ?");
7191
7192
7193         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7194         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7195         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7196                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7197
7198
7199         int i = 0;
7200
7201         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->account_id);
7202         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->mailbox_id);
7203         _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7204         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->local_uid);
7205         _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->server_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7206         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->rfc822_size);
7207         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_seen_field);
7208         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_flagged_field);
7209         _bind_stmt_field_data_int(hStmt, i++, account_id);
7210         _bind_stmt_field_data_int(hStmt, i++, mailbox_id);
7211         _bind_stmt_field_data_int(hStmt, i++, local_uid);
7212         _bind_stmt_field_data_string(hStmt, i++, (char*)mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7213         _bind_stmt_field_data_string(hStmt, i++, (char*)uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7214
7215
7216         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7217         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7218                         ("sqlite3_step fail:%d", rc));
7219
7220         ret = true;
7221
7222 FINISH_OFF:
7223         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7224         if (hStmt != NULL) {
7225                 rc = sqlite3_finalize(hStmt);
7226                 if (rc != SQLITE_OK) {
7227                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7228                         error = EMAIL_ERROR_DB_FAILURE;
7229                 }
7230         }
7231
7232         if (err_code != NULL)
7233                 *err_code = error;
7234
7235         EM_DEBUG_FUNC_END("ret [%d]", ret);
7236         return ret;
7237 }
7238
7239 INTERNAL_FUNC int emstorage_remove_downloaded_mail(char *multi_user_name,
7240                 int account_id,
7241                 int mailbox_id,
7242                 char *mailbox_name,
7243                 char *uid,
7244                 int transaction,
7245                 int *err_code)
7246 {
7247         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], mailbox_id[%d], mailbox_name[%s], "
7248                         "uid[%s], transaction[%d], err_code[%p]",
7249                         account_id, mailbox_id, mailbox_name, uid, transaction, err_code);
7250
7251         if (account_id < FIRST_ACCOUNT_ID) {
7252                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_name[%s], uid[%s]", account_id, mailbox_name, uid);
7253
7254                 if (err_code != NULL)
7255                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7256                 return false;
7257         }
7258
7259         int ret = false;
7260         int error = EMAIL_ERROR_NONE;
7261         char sql_query_string[QUERY_SIZE] = {0, };
7262         char *replaced_mailbox_name = NULL;
7263         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7264
7265         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7266
7267         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7268                         "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d ", account_id);
7269
7270         if (mailbox_id > 0) {
7271                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7272                                 sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7273                                 "AND mailbox_id = %d ", mailbox_id);
7274         }
7275
7276         if (mailbox_name) {             /*  NULL means all mailbox_name */
7277                 if (strstr(mailbox_name, "'"))
7278                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
7279                 else
7280                         replaced_mailbox_name = strdup(mailbox_name);
7281
7282                 SNPRINTF(sql_query_string+EM_SAFE_STRLEN(sql_query_string),
7283                                 sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7284                                 "AND mailbox_name = '%s' ", replaced_mailbox_name);
7285         }
7286
7287         if (uid) {              /*  NULL means all mail */
7288                 sqlite3_snprintf(sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7289                                 sql_query_string + EM_SAFE_STRLEN(sql_query_string), "AND server_uid = '%q' ",
7290                                 uid);
7291         }
7292
7293         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7294         if (error != EMAIL_ERROR_NONE) {
7295                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7296                 goto FINISH_OFF;
7297         }
7298
7299         ret = true;
7300
7301 FINISH_OFF:
7302         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7303
7304         EM_SAFE_FREE(replaced_mailbox_name);
7305
7306         if (err_code != NULL)
7307                 *err_code = error;
7308
7309         EM_DEBUG_FUNC_END("ret [%d]", ret);
7310         return ret;
7311 }
7312
7313 INTERNAL_FUNC int emstorage_free_read_mail_uid(emstorage_read_mail_uid_tbl_t** read_mail_uid, int count, int *err_code)
7314 {
7315         EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], count[%d], err_code[%p]", read_mail_uid, count, err_code);
7316
7317         int ret = false;
7318         int error = EMAIL_ERROR_NONE;
7319
7320         if (count > 0) {
7321                 if (!read_mail_uid || !*read_mail_uid) {
7322                         EM_DEBUG_EXCEPTION(" read_mail_uid[%p], count[%d]", read_mail_uid, count);
7323
7324                         error = EMAIL_ERROR_INVALID_PARAM;
7325                         goto FINISH_OFF;
7326                 }
7327
7328                 emstorage_read_mail_uid_tbl_t* p = *read_mail_uid;
7329                 int i;
7330
7331                 for (i = 0; i < count; i++) {
7332                         EM_SAFE_FREE(p[i].mailbox_name);
7333                         EM_SAFE_FREE(p[i].server_uid);
7334                 }
7335
7336                 EM_SAFE_FREE(p); *read_mail_uid = NULL;
7337         }
7338
7339         ret = true;
7340
7341 FINISH_OFF:
7342         if (err_code != NULL)
7343                 *err_code = error;
7344
7345         EM_DEBUG_FUNC_END("ret [%d]", ret);
7346         return ret;
7347 }
7348
7349 INTERNAL_FUNC int emstorage_get_rule_count_by_account_id(char *multi_user_name, int account_id, int *count, int transaction, int *err_code)
7350 {
7351         EM_DEBUG_FUNC_BEGIN("account_id [%d], count[%p], transaction[%d], err_code[%p]", count, transaction, err_code);
7352
7353         if (!count) {
7354                 EM_DEBUG_EXCEPTION("count[%p]", count);
7355
7356                 if (err_code != NULL)
7357                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7358                 return false;
7359         }
7360
7361         int rc = -1, ret = false;
7362         int error =  EMAIL_ERROR_NONE;
7363         char sql_query_string[QUERY_SIZE] = {0, };
7364
7365         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7366         EMSTORAGE_START_READ_TRANSACTION(transaction);
7367
7368         if (account_id != ALL_ACCOUNT)
7369                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl where account_id = %d", account_id);
7370         else
7371                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl");
7372
7373         char **result;
7374
7375         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7376         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7377                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7378
7379         *count = atoi(result[1]);
7380         sqlite3_free_table(result);
7381
7382         ret = true;
7383
7384 FINISH_OFF:
7385         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7386
7387         if (err_code != NULL)
7388                 *err_code = error;
7389
7390         EM_DEBUG_FUNC_END("ret [%d]", ret);
7391         return ret;
7392 }
7393
7394 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)
7395 {
7396         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);
7397
7398         if (!select_num || !is_completed || !rule_list) {               /*  only global rule supported. */
7399                 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);
7400
7401                 if (err_code != NULL)
7402                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7403                 return false;
7404         }
7405
7406         int ret = false;
7407         int error = EMAIL_ERROR_NONE;
7408
7409         emstorage_rule_tbl_t* p_data_tbl = NULL;
7410         int i = 0, count = 0;
7411         DB_STMT hStmt = NULL;
7412         char sql_query_string[QUERY_SIZE] = {0, };
7413
7414         int rc;
7415
7416         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7417         EMSTORAGE_START_READ_TRANSACTION(transaction);
7418
7419         if (account_id != ALL_ACCOUNT) {
7420                 if (type)
7421                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d AND type = %d", account_id, type);
7422                 else
7423                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d ORDER BY type", account_id);
7424         } else {
7425                 if (type)
7426                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE type = %d", type);
7427                 else
7428                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl ORDER BY type");
7429         }
7430
7431         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7432         /*      EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt); */
7433         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7434                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7435
7436
7437         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7438         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7439                         ("sqlite3_step fail:%d", rc));
7440
7441         char **result;
7442         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL); */
7443         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
7444         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7445                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7446
7447         sqlite3_free_table(result);
7448
7449         if (count == 0) {
7450                 EM_DEBUG_LOG_DEV("No matching rule found...");
7451                 ret = true;
7452                 error = EMAIL_ERROR_FILTER_NOT_FOUND; /*there is no matched rule*/
7453                 goto FINISH_OFF;
7454         }
7455
7456
7457         if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t) * count))) {
7458                 EM_DEBUG_EXCEPTION(" malloc failed...");
7459
7460                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7461                 goto FINISH_OFF;
7462         }
7463
7464         memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t) * count);
7465
7466         for (i = 0; i < count; i++) {
7467                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7468                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7469                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7470                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].type), TYPE_IDX_IN_MAIL_RULE_TBL);
7471                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7472                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7473                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7474                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7475                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7476                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7477
7478                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7479                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7480                                 ("sqlite3_step fail:%d", rc));
7481         }
7482
7483         ret = true;
7484
7485 FINISH_OFF:
7486
7487         EM_DEBUG_LOG("[%d] rules found.", count);
7488
7489         if (ret == true) {
7490                 *rule_list = p_data_tbl;
7491                 *select_num = count;
7492         } else if (p_data_tbl != NULL)
7493                 emstorage_free_rule(&p_data_tbl, count, NULL); /* CID FIX */
7494
7495         if (hStmt != NULL) {
7496                 rc = sqlite3_finalize(hStmt);
7497                 if (rc != SQLITE_OK) {
7498                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7499                         error = EMAIL_ERROR_DB_FAILURE;
7500                 }
7501         }
7502
7503         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7504
7505         if (err_code != NULL)
7506                 *err_code = error;
7507
7508         EM_DEBUG_FUNC_END("ret [%d]", ret);
7509         return ret;
7510 }
7511
7512 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)
7513 {
7514         EM_DEBUG_FUNC_BEGIN("rule_id[%d], rule[%p], transaction[%d], err_code[%p]", rule_id, rule, transaction, err_code);
7515         int error = EMAIL_ERROR_NONE;
7516         int ret = false;
7517         DB_STMT hStmt = NULL;
7518
7519         if (rule_id <= 0) {
7520                 EM_DEBUG_EXCEPTION("Invalid parameter");
7521                 error = EMAIL_ERROR_INVALID_PARAM;
7522                 goto FINISH_OFF;
7523         }
7524
7525         if (!rule) {
7526                 EM_DEBUG_EXCEPTION("rule_id[%d], rule[%p]", rule_id, rule);
7527                 error = EMAIL_ERROR_INVALID_PARAM;
7528                 goto FINISH_OFF;
7529         }
7530
7531         emstorage_rule_tbl_t* p_data_tbl = NULL;
7532         int rc;
7533
7534         char sql_query_string[QUERY_SIZE] = {0, };
7535         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7536         EMSTORAGE_START_READ_TRANSACTION(transaction);
7537
7538         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7539
7540         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7541         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7542                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7543
7544
7545         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7546         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7547                         ("sqlite3_step fail:%d", rc));
7548
7549         if (rc == SQLITE_DONE) {
7550                 EM_DEBUG_EXCEPTION(" no matched rule found...");
7551                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7552                 goto FINISH_OFF;
7553         }
7554
7555         if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t)))) {
7556                 EM_DEBUG_EXCEPTION(" malloc failed...");
7557                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7558                 goto FINISH_OFF;
7559         }
7560
7561         memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t));
7562         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7563         _get_stmt_field_data_int(hStmt, &(p_data_tbl->rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7564         _get_stmt_field_data_string(hStmt, &(p_data_tbl->filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7565         _get_stmt_field_data_int(hStmt, &(p_data_tbl->type), TYPE_IDX_IN_MAIL_RULE_TBL);
7566         _get_stmt_field_data_string(hStmt, &(p_data_tbl->value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7567         _get_stmt_field_data_string(hStmt, &(p_data_tbl->value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7568         _get_stmt_field_data_int(hStmt, &(p_data_tbl->action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7569         _get_stmt_field_data_int(hStmt, &(p_data_tbl->target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7570         _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7571         _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7572
7573         ret = true;
7574
7575 FINISH_OFF:
7576
7577         if (ret == true)
7578                 *rule = p_data_tbl;
7579
7580         if (hStmt != NULL) {
7581                 rc = sqlite3_finalize(hStmt);
7582                 if (rc != SQLITE_OK) {
7583                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7584                         error = EMAIL_ERROR_DB_FAILURE;
7585                 }
7586         }
7587
7588         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7589
7590         if (err_code != NULL)
7591                 *err_code = error;
7592
7593         EM_DEBUG_FUNC_END("ret [%d]", ret);
7594         return ret;
7595 }
7596
7597 INTERNAL_FUNC int emstorage_change_rule(char *multi_user_name, int rule_id, emstorage_rule_tbl_t* new_rule, int transaction, int *err_code)
7598 {
7599         EM_DEBUG_FUNC_BEGIN("rule_id[%d], new_rule[%p], transaction[%d], err_code[%p]", rule_id, new_rule, transaction, err_code);
7600
7601         if (!new_rule) {                /*  only global rule supported. */
7602                 EM_DEBUG_EXCEPTION("rule_id[%d], new_rule[%p]", rule_id, new_rule);
7603
7604                 if (err_code != NULL)
7605                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7606                 return false;
7607         }
7608
7609         int rc, ret = false;
7610         int error = EMAIL_ERROR_NONE;
7611
7612         DB_STMT hStmt = NULL;
7613         char sql_query_string[QUERY_SIZE] = {0, };
7614         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7615         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7616
7617         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7618                         "UPDATE mail_rule_tbl SET"
7619                         "  filter_name = ?"
7620                         ", type = ?"
7621                         ", value = ?"
7622                         ", value2 = ?"
7623                         ", action_type = ?"
7624                         ", target_mailbox_id = ?"
7625                         ", flag1 = ?"
7626                         ", flag2 = ?"
7627                         ", account_id = ?"
7628                         ", rule_id = ?"
7629                         " WHERE rule_id = %d"
7630                         , rule_id);
7631
7632
7633         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7634         EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
7635         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7636                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7637
7638         int i = 0;
7639
7640         _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->filter_name, 0, FILTER_NAME_LEN_IN_MAIL_RULE_TBL);
7641         _bind_stmt_field_data_int(hStmt, i++, new_rule->type);
7642         _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7643         _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7644         _bind_stmt_field_data_int(hStmt, i++, new_rule->action_type);
7645         _bind_stmt_field_data_int(hStmt, i++, new_rule->target_mailbox_id);
7646         _bind_stmt_field_data_int(hStmt, i++, new_rule->flag1);
7647         _bind_stmt_field_data_int(hStmt, i++, new_rule->flag2);
7648         _bind_stmt_field_data_int(hStmt, i++, new_rule->account_id);
7649         _bind_stmt_field_data_int(hStmt, i++, rule_id);
7650
7651
7652         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7653         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7654                         ("sqlite3_step fail:%d", rc));
7655
7656         ret = true;
7657
7658 FINISH_OFF:
7659         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7660         if (hStmt != NULL) {
7661                 rc = sqlite3_finalize(hStmt);
7662                 if (rc != SQLITE_OK) {
7663                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7664                         error = EMAIL_ERROR_DB_FAILURE;
7665                 }
7666         }
7667
7668         if (err_code != NULL)
7669                 *err_code = error;
7670
7671         EM_DEBUG_FUNC_END("ret [%d]", ret);
7672         return ret;
7673 }
7674
7675 INTERNAL_FUNC int emstorage_find_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7676 {
7677         EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7678
7679         if (!rule) {
7680                 EM_DEBUG_LOG("rule is NULL");
7681                 if (err_code != NULL)
7682                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7683                 return false;
7684         }
7685
7686         DB_STMT hStmt = NULL;
7687         char sql_query_string[QUERY_SIZE] = {0,};
7688         int error = EMAIL_ERROR_NONE;
7689         int rc = 0;
7690         int ret = false;
7691
7692         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7693         EMSTORAGE_START_READ_TRANSACTION(transaction);
7694
7695         switch (rule->action_type) {
7696         case EMAIL_FILTER_MOVE:
7697                 if (rule->type == EMAIL_PRIORITY_SENDER) {
7698                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7699                                         "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7700                                         rule->action_type, rule->type, rule->value2);
7701                 } else {
7702                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7703                                         "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);
7704                 }
7705                 break;
7706         case EMAIL_FILTER_BLOCK:
7707                 if (rule->type == EMAIL_FILTER_FROM)
7708                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7709                                         "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7710                                         rule->action_type, rule->type, rule->value2);
7711                 else if (rule->type == EMAIL_FILTER_SUBJECT)
7712                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7713                                         "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value) = UPPER(\'%q\')",
7714                                         rule->action_type, rule->type, rule->value);
7715                 else if (rule->type == (EMAIL_FILTER_SUBJECT | EMAIL_FILTER_FROM))
7716                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7717                                         "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\'))",
7718                                         rule->action_type, EMAIL_FILTER_SUBJECT, rule->value, EMAIL_FILTER_FROM, rule->value2);
7719                 break;
7720
7721         default:
7722                 EM_DEBUG_EXCEPTION("Invalid parameter : rule->action_type[%d]", rule->action_type);
7723                 error = EMAIL_ERROR_INVALID_PARAM;
7724                 goto FINISH_OFF;
7725                 break;
7726         }
7727
7728         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7729         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7730                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7731
7732         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7733         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7734                         ("sqlite3_step fail:%d", rc));
7735
7736         if (rc == SQLITE_DONE) {
7737                 EM_DEBUG_EXCEPTION(" no matched rule found...");
7738                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7739         }
7740
7741         ret = true;
7742
7743 FINISH_OFF:
7744
7745         if (hStmt != NULL) {
7746                 rc = sqlite3_finalize(hStmt);
7747                 if (rc != SQLITE_OK) {
7748                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7749                         error = EMAIL_ERROR_DB_FAILURE;
7750                 }
7751         }
7752
7753         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7754
7755         if (err_code)
7756                 *err_code = error;
7757
7758         EM_DEBUG_FUNC_END("ret [%d]", ret);
7759         return ret;
7760 }
7761
7762 INTERNAL_FUNC int emstorage_add_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7763 {
7764         EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7765
7766         if (!rule) {    /*  only global rule supported. */
7767                 EM_DEBUG_LOG("rule is NULL");
7768                 if (err_code != NULL)
7769                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7770                 return false;
7771         }
7772
7773         int rc, rc_2, ret = false;
7774         int error = EMAIL_ERROR_NONE;
7775         DB_STMT hStmt = NULL;
7776         char sql_query_string[QUERY_SIZE] = {0, };
7777
7778         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7779
7780         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7781
7782         char *sql;
7783         char **result;
7784         sql = "SELECT max(rowid) FROM mail_rule_tbl;";
7785
7786         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7787         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7788                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7789
7790         if (NULL == result[1])
7791                 rc = 1;
7792         else
7793                 rc = atoi(result[1])+1;
7794
7795         sqlite3_free_table(result);
7796
7797         rule->rule_id = rc;
7798
7799         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7800                         "INSERT INTO mail_rule_tbl VALUES "
7801                         "(?"            /*  account id */
7802                         ", ?"           /*  rule_id */
7803                         ", ?"           /*  filter_name */
7804                         ", ?"           /*  type */
7805                         ", ?"           /*  value */
7806                         ", ?"           /*  value2 */
7807                         ", ?"           /*  action_type */
7808                         ", ?"           /*  target_mailbox_id */
7809                         ", ?"           /*  flag1 */
7810                         ", ?)");        /*  flag2 */
7811
7812         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc_2);
7813         if (rc_2 != SQLITE_OK) {
7814                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc_2, sqlite3_errmsg(local_db_handle));
7815                 error = EMAIL_ERROR_DB_FAILURE;
7816                 goto FINISH_OFF;
7817         }
7818
7819         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL, rule->account_id);
7820         _bind_stmt_field_data_int(hStmt, RULE_ID_IDX_IN_MAIL_RULE_TBL, rule->rule_id);
7821         _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);
7822         _bind_stmt_field_data_int(hStmt, TYPE_IDX_IN_MAIL_RULE_TBL, rule->type);
7823         _bind_stmt_field_data_string(hStmt, VALUE_IDX_IN_MAIL_RULE_TBL, (char*)rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7824         _bind_stmt_field_data_string(hStmt, VALUE2_IDX_IN_MAIL_RULE_TBL, (char*)rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7825         _bind_stmt_field_data_int(hStmt, ACTION_TYPE_IDX_IN_MAIL_RULE_TBL, rule->action_type);
7826         _bind_stmt_field_data_int(hStmt, TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL, rule->target_mailbox_id);
7827         _bind_stmt_field_data_int(hStmt, FLAG1_IDX_IN_MAIL_RULE_TBL, rule->flag1);
7828         _bind_stmt_field_data_int(hStmt, FLAG2_IDX_IN_MAIL_RULE_TBL, rule->flag2);
7829
7830         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7831         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7832                         ("sqlite3_step fail:%d", rc));
7833
7834         ret = true;
7835
7836 FINISH_OFF:
7837         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7838         if (hStmt != NULL) {
7839                 rc = sqlite3_finalize(hStmt);
7840                 if (rc != SQLITE_OK) {
7841                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7842                         error = EMAIL_ERROR_DB_FAILURE;
7843                 }
7844         }
7845
7846         if (err_code != NULL)
7847                 *err_code = error;
7848
7849         EM_DEBUG_FUNC_END("ret [%d]", ret);
7850         return ret;
7851 }
7852
7853 INTERNAL_FUNC int emstorage_delete_rule(char *multi_user_name, int rule_id, int transaction, int *err_code)
7854 {
7855         EM_DEBUG_FUNC_BEGIN("rule_id[%d], transaction[%d], err_code[%p]", rule_id, transaction, err_code);
7856
7857         if (rule_id <= 0) {             /*  only global rule supported. */
7858                 EM_DEBUG_EXCEPTION("rule_id[%d]", rule_id);
7859
7860                 if (err_code != NULL)
7861                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7862                 return false;
7863         }
7864
7865         int rc, ret = false;
7866         int error = EMAIL_ERROR_NONE;
7867         char sql_query_string[QUERY_SIZE] = {0, };
7868         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7869         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7870
7871         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7872         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7873         if (error != EMAIL_ERROR_NONE) {
7874                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7875                 goto FINISH_OFF;
7876         }
7877
7878         rc = sqlite3_changes(local_db_handle);
7879         if (rc == 0) {
7880                 EM_DEBUG_EXCEPTION(" no matched rule found...");
7881
7882                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7883                 goto FINISH_OFF;
7884         }
7885
7886         ret = true;
7887
7888 FINISH_OFF:
7889         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7890
7891         if (err_code != NULL)
7892                 *err_code = error;
7893
7894         EM_DEBUG_FUNC_END("ret [%d]", ret);
7895         return ret;
7896 }
7897
7898 INTERNAL_FUNC int emstorage_free_rule(emstorage_rule_tbl_t** rule_list, int count, int *err_code)
7899 {
7900         EM_DEBUG_FUNC_BEGIN("rule_list[%p], conut[%d], err_code[%p]", rule_list, count, err_code);
7901
7902         int ret = false;
7903         int error = EMAIL_ERROR_NONE;
7904
7905         if (count > 0) {
7906                 if (!rule_list || !*rule_list) {
7907                         EM_DEBUG_EXCEPTION(" rule_list[%p], conut[%d]", rule_list, count);
7908
7909                         error = EMAIL_ERROR_INVALID_PARAM;
7910                         goto FINISH_OFF;
7911                 }
7912
7913                 emstorage_rule_tbl_t* p = *rule_list;
7914                 int i = 0;
7915
7916                 for (; i < count; i++)
7917                         EM_SAFE_FREE(p[i].value);
7918
7919                 EM_SAFE_FREE(p); *rule_list = NULL;
7920         }
7921
7922 FINISH_OFF:
7923         if (err_code != NULL)
7924                 *err_code = error;
7925
7926         EM_DEBUG_FUNC_END("ret [%d]", ret);
7927         return ret;
7928 }
7929
7930 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)
7931 {
7932         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);
7933
7934         if (!total && !unseen) {
7935                 EM_DEBUG_EXCEPTION(" accoun_id[%d], mailbox_id[%d], total[%p], unseen[%p]", account_id, mailbox_id, total, unseen);
7936                 if (err_code != NULL)
7937                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7938                 return false;
7939         }
7940
7941         int rc = -1, ret = false;
7942         int error = EMAIL_ERROR_NONE;
7943         DB_STMT hStmt = NULL;
7944         char sql_query_string[QUERY_SIZE] = {0, };
7945         char *replaced_mailbox_name = NULL;
7946
7947         memset(&sql_query_string, 0x00, sizeof(sql_query_string));
7948         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7949         EMSTORAGE_START_READ_TRANSACTION(transaction);
7950
7951         if (total) {
7952                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl");
7953
7954                 if (account_id != ALL_ACCOUNT) {
7955                         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);
7956                         if (mailbox_id)
7957                                 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);
7958                 } else if (mailbox_id)
7959                         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);
7960
7961 #ifdef USE_GET_RECORD_COUNT_API
7962                 char **result;
7963
7964                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7965                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF2; },
7966                                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7967
7968                 *total = atoi(result[1]);
7969                 sqlite3_free_table(result);
7970 #else
7971
7972                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7973                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF2; },
7974                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7975
7976                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7977                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF2; },
7978                                 ("sqlite3_step fail:%d", rc));
7979                 _get_stmt_field_data_int(hStmt, total, 0);
7980 #endif          /*  USE_GET_RECORD_COUNT_API */
7981         }
7982
7983         if (unseen) {
7984                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl WHERE flags_seen_field = 0");               /*  fSEEN = 0x01 */
7985
7986                 if (account_id != ALL_ACCOUNT) {
7987                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7988                                 sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND account_id = %d", account_id);
7989                 }
7990
7991                 if (mailbox_id) {
7992                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7993                                 sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_id = %d", mailbox_id);
7994                 } else
7995                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7996                                 sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_type NOT IN (3, 5)");
7997
7998                 char **result;
7999                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
8000                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc,
8001                                 { error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
8002                                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8003
8004                 *unseen = atoi(result[1]);
8005                 sqlite3_free_table(result);
8006
8007         }
8008 FINISH_OFF:
8009         ret = true;
8010
8011 FINISH_OFF2:
8012
8013 #ifndef USE_PREPARED_QUERY_
8014         if (hStmt != NULL) {
8015                 rc = sqlite3_finalize(hStmt);
8016                 if (rc != SQLITE_OK) {
8017                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8018                         error = EMAIL_ERROR_DB_FAILURE;
8019                 }
8020         }
8021 #endif
8022
8023         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8024
8025         EM_SAFE_FREE(replaced_mailbox_name);
8026
8027         if (err_code != NULL)
8028                 *err_code = error;
8029
8030         EM_DEBUG_FUNC_END("ret [%d]", ret);
8031         return ret;
8032 }
8033
8034 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)
8035 {
8036         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
8037
8038         if (mail_id <= 0 || !mail) {
8039                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
8040                 if (err_code != NULL)
8041                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8042                 return false;
8043         }
8044
8045         int col_index = 0;
8046         emstorage_mail_tbl_t* p_data_tbl = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
8047
8048         if (p_data_tbl == NULL) {
8049                 EM_DEBUG_EXCEPTION("malloc failed...");
8050                 if (err_code != NULL)
8051                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8052                 return false;
8053         }
8054
8055         memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8056         DB_STMT hStmt = NULL;
8057         char sql_query_string[QUERY_SIZE] = {0, };
8058
8059         int rc, ret = false;
8060         int error = EMAIL_ERROR_NONE;
8061
8062         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8063         EMSTORAGE_START_READ_TRANSACTION(transaction);
8064
8065         switch (type) {
8066         case RETRIEVE_SUMMARY:
8067                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8068                                 "SELECT account_id, "
8069                                 "mail_id, "
8070                                 "mailbox_id, "
8071                                 "server_mail_status, "
8072                                 "server_mailbox_name, "
8073                                 "server_mail_id, "
8074                                 "file_path_plain, "
8075                                 "file_path_html,"
8076                                 "file_path_mime_entity, "
8077                                 "flags_seen_field, "
8078                                 "save_status, "
8079                                 "lock_status, "
8080                                 "thread_id, "
8081                                 "thread_item_count "
8082                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8083                 break;
8084
8085         case RETRIEVE_FIELDS_FOR_DELETE:
8086                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8087                                 "SELECT account_id, "
8088                                 "mail_id, "
8089                                 "server_mail_status, "
8090                                 "server_mailbox_name, "
8091                                 "server_mail_id "
8092                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8093                 break;
8094
8095         case RETRIEVE_ACCOUNT:
8096                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8097                                 "SELECT account_id "
8098                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8099                 break;
8100
8101         case RETRIEVE_FLAG:
8102                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8103                                 "SELECT account_id, "
8104                                 "flags_seen_field, "
8105                                 "thread_id, "
8106                                 "mailbox_id "
8107                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8108                 break;
8109
8110         default:
8111                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8112                 error = EMAIL_ERROR_INVALID_PARAM;
8113                 goto FINISH_OFF;
8114         }
8115
8116         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8117
8118         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8119         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8120                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8121
8122
8123         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8124         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8125                         ("sqlite3_step fail:%d", rc));
8126
8127         if (rc == SQLITE_DONE) {
8128                 EM_DEBUG_LOG("no matched mail found...");
8129                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8130                 goto FINISH_OFF;
8131         }
8132         switch (type) {
8133         case RETRIEVE_SUMMARY:
8134                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8135                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8136                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8137                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8138                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8139                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8140                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, col_index++);
8141                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, col_index++);
8142                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, col_index++);
8143                 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8144                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), col_index++);
8145                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), col_index++);
8146                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8147                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_item_count), col_index++);
8148                 break;
8149
8150         case RETRIEVE_FIELDS_FOR_DELETE:
8151                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8152                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8153                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8154                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8155                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8156                 break;
8157
8158         case RETRIEVE_ACCOUNT:
8159                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8160                 break;
8161
8162         case RETRIEVE_FLAG:
8163                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8164                 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8165                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8166                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8167                 break;
8168         }
8169
8170         ret = true;
8171
8172 FINISH_OFF:
8173         if (ret == true)
8174                 *mail = p_data_tbl;
8175         else if (p_data_tbl != NULL)
8176                 emstorage_free_mail(&p_data_tbl,  1, NULL);
8177
8178         if (hStmt != NULL) {
8179                 rc = sqlite3_finalize(hStmt);
8180                 if (rc != SQLITE_OK) {
8181                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8182                         error = EMAIL_ERROR_DB_FAILURE;
8183                 }
8184         }
8185
8186
8187         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8188
8189         if (err_code != NULL)
8190                 *err_code = error;
8191
8192         EM_DEBUG_FUNC_END("ret [%d]", ret);
8193         return ret;
8194 }
8195
8196 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)
8197 {
8198         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);
8199
8200         int ret = false;
8201         int error = EMAIL_ERROR_NONE;
8202         int query_string_length = 0;
8203         int i = 0, item_count = 0, rc = -1, field_count, col_index, cur_sql_query_string = 0;
8204         char **result = NULL;
8205         char *sql_query_string = NULL;
8206         emstorage_mail_tbl_t* p_data_tbl = NULL;
8207         sqlite3 *local_db_handle = NULL;
8208
8209         if (number_of_mails <= 0 || !mail_ids) {
8210                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8211                 if (err_code != NULL)
8212                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8213                 return false;
8214         }
8215
8216         p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * number_of_mails);
8217
8218         query_string_length = (sizeof(char) * 8 * number_of_mails) + 512;
8219         sql_query_string = (char*)em_malloc(query_string_length);
8220
8221         if (p_data_tbl == NULL || sql_query_string == NULL) {
8222                 EM_DEBUG_EXCEPTION("malloc failed...");
8223
8224                 EM_SAFE_FREE(p_data_tbl);
8225                 EM_SAFE_FREE(sql_query_string);
8226
8227                 if (err_code != NULL)
8228                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8229                 return false;
8230         }
8231
8232         local_db_handle = emstorage_get_db_connection(multi_user_name);
8233
8234         EMSTORAGE_START_READ_TRANSACTION(transaction);
8235
8236         switch (type) {
8237         case RETRIEVE_SUMMARY:
8238                 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8239                                 "SELECT account_id, "
8240                                 "mail_id, "
8241                                 "mailbox_id, "
8242                                 "server_mail_status, "
8243                                 "server_mailbox_name, "
8244                                 "server_mail_id, "
8245                                 "file_path_plain, "
8246                                 "file_path_html, "
8247                                 "file_path_mime_entity, "
8248                                 "subject, "
8249                                 "flags_seen_field, "
8250                                 "save_status, "
8251                                 "lock_status, "
8252                                 "thread_id, "
8253                                 "thread_item_count "
8254                                 "FROM mail_tbl WHERE mail_id in (");
8255                 field_count = 15;
8256                 break;
8257
8258         case RETRIEVE_FIELDS_FOR_DELETE:
8259                 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8260                                 "SELECT account_id, "
8261                                 "mail_id, "
8262                                 "server_mail_status, "
8263                                 "server_mailbox_name, "
8264                                 "server_mail_id "
8265                                 "FROM mail_tbl WHERE mail_id in (");
8266                 field_count = 5;
8267                 break;
8268
8269         case RETRIEVE_ACCOUNT:
8270                 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8271                                 "SELECT account_id FROM mail_tbl WHERE mail_id in (");
8272                 field_count = 1;
8273                 break;
8274
8275         case RETRIEVE_FLAG:
8276                 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8277                                 "SELECT account_id, "
8278                                 "mail_id, "
8279                                 "mailbox_id, "
8280                                 "flags_seen_field, "
8281                                 "thread_id "
8282                                 "FROM mail_tbl WHERE mail_id in (");
8283                 field_count = 5;
8284                 break;
8285
8286         default:
8287                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8288                 error = EMAIL_ERROR_INVALID_PARAM;
8289                 goto FINISH_OFF;
8290         }
8291
8292         for (i = 0; i < number_of_mails; i++)
8293                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_string_length, "%d,", mail_ids[i]);
8294         sql_query_string[EM_SAFE_STRLEN(sql_query_string) - 1] = ')';
8295
8296         EM_DEBUG_LOG_SEC("Query [%s], Length [%zu]", sql_query_string, EM_SAFE_STRLEN(sql_query_string));
8297
8298         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &item_count, 0, NULL), rc);
8299         if (SQLITE_OK != rc && -1 != rc) {
8300                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
8301                 error = EMAIL_ERROR_DB_FAILURE;
8302                 goto FINISH_OFF;
8303         }
8304         EM_DEBUG_LOG("item_count [%d]", item_count);
8305
8306         if (number_of_mails != item_count) {
8307                 EM_DEBUG_EXCEPTION("Can't find all emails");
8308                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8309                 goto FINISH_OFF;
8310         }
8311
8312         col_index = field_count;
8313
8314         for (i = 0; i < item_count; i++)        {
8315                 switch (type) {
8316                 case RETRIEVE_SUMMARY:
8317                         _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8318                         _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8319                         _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8320                         _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8321                         _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8322                         _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8323                         _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
8324                         _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
8325                         _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
8326                         _get_table_field_data_string(result, &(p_data_tbl[i].subject), 0, col_index++);
8327                         _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8328                         _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
8329                         _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
8330                         _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8331                         _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
8332                         break;
8333
8334                 case RETRIEVE_FIELDS_FOR_DELETE:
8335                         _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8336                         _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8337                         _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8338                         _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8339                         _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8340                         break;
8341
8342                 case RETRIEVE_ACCOUNT:
8343                         _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8344                         break;
8345
8346                 case RETRIEVE_FLAG:
8347                         _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8348                         _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8349                         _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8350                         _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8351                         _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8352                         break;
8353                 }
8354         }
8355
8356         ret = true;
8357
8358 FINISH_OFF:
8359         if (ret == true)
8360                 *mail = p_data_tbl;
8361         else
8362                 emstorage_free_mail(&p_data_tbl, number_of_mails, NULL);
8363
8364         if (result)
8365                 sqlite3_free_table(result);
8366
8367         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8368
8369         EM_SAFE_FREE(sql_query_string);
8370
8371         if (err_code != NULL)
8372                 *err_code = error;
8373
8374         EM_DEBUG_FUNC_END("ret [%d]", ret);
8375         return ret;
8376 }
8377
8378 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)
8379 {
8380         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8381
8382         if (mail_id <= 0 || !mail) {
8383                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
8384                 if (err_code != NULL)
8385                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8386                 return false;
8387         }
8388
8389         int ret = false, error = EMAIL_ERROR_NONE, count;
8390         char conditional_clause[QUERY_SIZE] = {0, };
8391         emstorage_mail_tbl_t* p_data_tbl = NULL;
8392
8393         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8394         EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8395
8396         if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8397                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8398                 goto FINISH_OFF;
8399         }
8400
8401         ret = true;
8402
8403 FINISH_OFF:
8404         if (ret == true)
8405                 *mail = p_data_tbl;
8406         else if (p_data_tbl != NULL)
8407                 emstorage_free_mail(&p_data_tbl, 1, &error);
8408
8409         if (err_code != NULL)
8410                 *err_code = error;
8411
8412         EM_DEBUG_FUNC_END("ret [%d]", ret);
8413         return ret;
8414 }
8415
8416 #ifdef __FEATURE_BODY_SEARCH__
8417 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)
8418 {
8419         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
8420
8421         if (mail_id <= 0 || !mail_text) {
8422                 EM_DEBUG_EXCEPTION("mail_id[%d], mail_text[%p]", mail_id, mail_text);
8423                 if (err_code != NULL)
8424                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8425                 return false;
8426         }
8427
8428         int ret = false;
8429         int error = EMAIL_ERROR_NONE;
8430         int count = 0;
8431         char conditional_clause[QUERY_SIZE] = {0, };
8432         emstorage_mail_text_tbl_t *p_data_tbl = NULL;
8433
8434         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8435         EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8436
8437         if (!emstorage_query_mail_text_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8438                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8439                 goto FINISH_OFF;
8440         }
8441
8442         ret = true;
8443
8444 FINISH_OFF:
8445         if (ret == true)
8446                 *mail_text = p_data_tbl;
8447         else if (p_data_tbl != NULL)
8448                 emstorage_free_mail_text(&p_data_tbl, 1, &error);
8449
8450         if (err_code != NULL)
8451                 *err_code = error;
8452
8453         EM_DEBUG_FUNC_END("ret [%d]", ret);
8454         return ret;
8455 }
8456 #endif
8457
8458 INTERNAL_FUNC int emstorage_mail_search_start(char *multi_user_name,
8459                 emstorage_search_filter_t *search,
8460                 int account_id,
8461                 int mailbox_id,
8462                 int sorting,
8463                 DB_STMT *search_handle,
8464                 int *searched,
8465                 int transaction,
8466                 int *err_code)
8467 {
8468         EM_DEBUG_FUNC_BEGIN("search[%p], account_id[%d], mailbox_id[%d], sorting[%d], "
8469                         "search_handle[%p], searched[%p], transaction[%d], err_code[%p]",
8470                         search, account_id, mailbox_id, sorting, search_handle,
8471                         searched, transaction, err_code);
8472
8473         if (!search_handle || !searched) {
8474                 if (err_code != NULL)
8475                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8476                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8477                 EM_DEBUG_FUNC_END("false");
8478                 return false;
8479         }
8480
8481         emstorage_search_filter_t* p = search;
8482         int error = EMAIL_ERROR_NONE;
8483         DB_STMT hStmt = NULL;
8484         char sql_query_string[QUERY_SIZE] = {0, };
8485         int rc, ret = false;
8486         int and = false, mail_count = 0;
8487
8488         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8489         EMSTORAGE_START_READ_TRANSACTION(transaction);
8490
8491         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl");
8492
8493         if (account_id != ALL_ACCOUNT) {
8494                 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);
8495                 and = true;
8496         }
8497
8498         if (mailbox_id) {
8499                 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);
8500                 and = true;
8501         }
8502
8503         while (p) {
8504                 if (p->key_type) {
8505                         if (!strncmp(p->key_type, "subject", strlen("subject"))) {
8506                                 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);
8507                                 and = true;
8508                         } else if (!strncmp(p->key_type, "full_address_from", strlen("full_address_from"))) {
8509                                 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);
8510                                 and = true;
8511                         } else if (!strncmp(p->key_type, "full_address_to", strlen("full_address_to"))) {
8512                                 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);
8513                                 and = true;
8514                         } else if (!strncmp(p->key_type, "email_address", strlen("email_address"))) {
8515                                 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);
8516                                 and = true;
8517                         }
8518                         p = p->next;
8519                 }
8520         }
8521
8522         if (sorting)
8523                 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");
8524
8525         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
8526
8527
8528         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8529         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8530                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8531
8532
8533         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8534         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8535                         ("sqlite3_step fail:%d", rc));
8536
8537         char **result;
8538
8539         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &mail_count, NULL, NULL), rc);
8540         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
8541                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8542
8543         sqlite3_free_table(result);
8544
8545         ret = true;
8546
8547 FINISH_OFF:
8548         if (ret == true) {
8549                 *search_handle = hStmt;
8550                 *searched = mail_count;
8551                 EM_DEBUG_LOG("mail_count [%d]", mail_count);
8552         } else {
8553                 if (hStmt != NULL) {
8554                         rc = sqlite3_finalize(hStmt);
8555                         if (rc != SQLITE_OK) {
8556                                 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8557                                 error = EMAIL_ERROR_DB_FAILURE;
8558                         }
8559                 }
8560
8561                 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8562         }
8563
8564         if (err_code != NULL)
8565                 *err_code = error;
8566
8567         EM_DEBUG_FUNC_END("ret [%d]", ret);
8568         return ret;
8569 }
8570
8571 INTERNAL_FUNC int emstorage_mail_search_result(DB_STMT search_handle, emstorage_mail_field_type_t type, void** data, int transaction, int *err_code)
8572 {
8573         EM_DEBUG_FUNC_BEGIN("search_handle[%d], type[%d], data[%p], transaction[%d], err_code[%p]", search_handle, type, data, transaction, err_code);
8574
8575         if (search_handle == 0 || !data) {
8576                 EM_DEBUG_EXCEPTION("No Search_handle type[%d], data[%p]", type, data);
8577
8578                 if (err_code != NULL)
8579                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8580                 return false;
8581         }
8582
8583         emstorage_mail_tbl_t* p_data_tbl = NULL;
8584         DB_STMT hStmt = search_handle;
8585         int rc, ret = false;
8586         int error = EMAIL_ERROR_NONE;
8587
8588         switch (type) {
8589         case RETRIEVE_ID:
8590                 _get_stmt_field_data_int(hStmt, (int *)data, MAIL_ID_IDX_IN_MAIL_TBL);
8591                 break;
8592
8593         case RETRIEVE_ENVELOPE:
8594         case RETRIEVE_ALL:
8595                 if (!(p_data_tbl = em_malloc(sizeof(emstorage_mail_tbl_t)))) {
8596                         EM_DEBUG_EXCEPTION(" em_mallocfailed...");
8597                         error = EMAIL_ERROR_OUT_OF_MEMORY;
8598                         goto FINISH_OFF;
8599                 }
8600
8601                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8602                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8603                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_size), MAIL_SIZE_IDX_IN_MAIL_TBL);
8604                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8605                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_from), 1, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL);
8606                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_to), 1, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL);
8607                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject), 1, SUBJECT_IDX_IN_MAIL_TBL);
8608                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->body_download_status), BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL);
8609                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL);
8610                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8611                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8612                 _get_stmt_field_data_time_t(hStmt, &(p_data_tbl->date_time), DATETIME_IDX_IN_MAIL_TBL);
8613                 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL);
8614                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->DRM_status), DRM_STATUS_IDX_IN_MAIL_TBL);
8615                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->priority), PRIORITY_IDX_IN_MAIL_TBL);
8616                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), SAVE_STATUS_IDX_IN_MAIL_TBL);
8617                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), LOCK_STATUS_IDX_IN_MAIL_TBL);
8618                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->report_status), REPORT_STATUS_IDX_IN_MAIL_TBL);
8619                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8620                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->meeting_request_status), MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL);
8621                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->message_class), MESSAGE_CLASS_IDX_IN_MAIL_TBL);
8622                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->digest_type), DIGEST_TYPE_IDX_IN_MAIL_TBL);
8623                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->smime_type), SMIME_TYPE_IDX_IN_MAIL_TBL);
8624                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->scheduled_sending_time), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8625                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->remaining_resend_times), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8626                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->tag_id), TAG_ID_IDX_IN_MAIL_TBL);
8627                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->eas_data_length), EAS_DATA_LENGTH_IDX_IN_MAIL_TBL);
8628                 _get_stmt_field_data_blob(hStmt, (void**)&(p_data_tbl->eas_data), EAS_DATA_IDX_IN_MAIL_TBL);
8629
8630                 if (type == RETRIEVE_ALL) {
8631                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8632                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8633                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_reply), 1, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL);
8634                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_cc), 1, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL);
8635                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_bcc), 1, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL);
8636                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_return), 1, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL);
8637                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->message_id), 0, MESSAGE_ID_IDX_IN_MAIL_TBL);
8638                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8639                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8640                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_count), ATTACHMENT_COUNT_IDX_IN_MAIL_TBL);
8641                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8642                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->replied_time), REPLIED_TIME_IDX_IN_MAIL_TBL);
8643                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->forwarded_time), FORWARDED_TIME_IDX_IN_MAIL_TBL);
8644                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->default_charset), 0, DEFAULT_CHARSET_IDX_IN_MAIL_TBL);
8645                 }
8646
8647                 if (p_data_tbl->body_download_status) {
8648                         struct stat buf;
8649
8650                         if (p_data_tbl->file_path_html) {
8651                                 if (stat(p_data_tbl->file_path_html, &buf) == -1)
8652                                         p_data_tbl->body_download_status = 0;
8653                         } else if (p_data_tbl->file_path_plain) {
8654                                 if (stat(p_data_tbl->file_path_plain, &buf) == -1)
8655                                         p_data_tbl->body_download_status = 0;
8656                         } else
8657                                 p_data_tbl->body_download_status = 0;
8658                 }
8659
8660                 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8661                 break;
8662
8663         case RETRIEVE_SUMMARY:
8664                 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8665                         EM_DEBUG_EXCEPTION(" malloc failed...");
8666
8667                         error = EMAIL_ERROR_OUT_OF_MEMORY;
8668                         goto FINISH_OFF;
8669                 }
8670
8671                 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8672
8673                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8674                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8675                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8676                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8677                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8678
8679                 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8680                 break;
8681
8682         case RETRIEVE_ADDRESS:
8683                 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8684                         EM_DEBUG_EXCEPTION(" malloc failed...");
8685                         error = EMAIL_ERROR_OUT_OF_MEMORY;
8686                         goto FINISH_OFF;
8687                 }
8688
8689                 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8690                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8691                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8692                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8693                 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8694                 break;
8695
8696         default:
8697                 break;
8698         }
8699
8700         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8701         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8702                         ("sqlite3_step fail:%d", rc));
8703
8704         ret = true;
8705
8706 FINISH_OFF:
8707
8708         if (err_code != NULL)
8709                 *err_code = error;
8710
8711         if (ret == false && p_data_tbl)
8712                 emstorage_free_mail(&p_data_tbl, 1, NULL);
8713
8714         EM_DEBUG_FUNC_END("ret [%d]", ret);
8715         return ret;
8716 }
8717
8718 INTERNAL_FUNC int emstorage_mail_search_end(DB_STMT search_handle, int transaction, int *err_code)
8719 {
8720         EM_DEBUG_FUNC_BEGIN("search_handle[%d], transaction[%d], err_code[%p]", search_handle, transaction, err_code);
8721
8722         int error = EMAIL_ERROR_NONE;
8723         int rc, ret = false;
8724
8725         if (search_handle == 0) {
8726                 EM_DEBUG_EXCEPTION("No search_handle");
8727                 error = EMAIL_ERROR_INVALID_PARAM;
8728                 goto FINISH_OFF;
8729         }
8730
8731         DB_STMT hStmt = search_handle;
8732
8733         rc = sqlite3_finalize(hStmt);
8734         if (rc != SQLITE_OK) {
8735                 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8736                 error = EMAIL_ERROR_DB_FAILURE;
8737         }
8738
8739         ret = true;
8740
8741 FINISH_OFF:
8742         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8743
8744         if (err_code != NULL)
8745                 *err_code = error;
8746
8747         EM_DEBUG_FUNC_END("ret [%d]", ret);
8748         return ret;
8749 }
8750
8751 INTERNAL_FUNC int emstorage_change_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t *mail, int transaction, int *err_code)
8752 {
8753         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8754
8755         if (mail_id <= 0 || !mail) {
8756                 EM_DEBUG_EXCEPTION(" mail_id[%d], mail[%p]", mail_id, mail);
8757
8758                 if (err_code != NULL)
8759                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8760                 return false;
8761         }
8762
8763         DB_STMT hStmt = NULL;
8764         char sql_query_string[QUERY_SIZE] = {0, };
8765         int rc = -1;
8766         int ret = false;
8767         int error = EMAIL_ERROR_NONE;
8768         int i = 0;
8769         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8770         char mailbox_id_param_string[10] = {0,};
8771
8772         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
8773
8774         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8775                         "UPDATE mail_tbl SET"
8776                         "  mail_id = ?"
8777                         ", account_id = ?"
8778                         ", mailbox_id = ?"
8779                         ", mail_size = ?"
8780                         ", server_mail_status = ?"
8781                         ", server_mailbox_name = ?"
8782                         ", server_mail_id = ?"
8783                         ", reference_mail_id = ?"
8784                         ", full_address_from = ?"
8785                         ", full_address_reply = ?"  /* 10 */
8786                         ", full_address_to = ?"
8787                         ", full_address_cc = ?"
8788                         ", full_address_bcc = ?"
8789                         ", full_address_return = ?"
8790                         ", subject = ?"
8791                         ", body_download_status = ?"
8792                         ", file_path_plain = ?"
8793                         ", file_path_html = ?"
8794                         ", file_path_mime_entity = ?"
8795                         ", date_time = ?"
8796                         ", flags_seen_field      = ?"
8797                         ", flags_deleted_field   = ?"
8798                         ", flags_flagged_field   = ?"
8799                         ", flags_answered_field  = ?"
8800                         ", flags_recent_field    = ?"
8801                         ", flags_draft_field     = ?"
8802                         ", flags_forwarded_field = ?"
8803                         ", DRM_status = ?"
8804                         ", priority = ?"
8805                         ", save_status = ?"
8806                         ", lock_status = ?"
8807                         ", message_id = ?"
8808                         ", report_status = ?"
8809                         ", preview_text = ?"
8810                         ", smime_type = ?"
8811                         ", scheduled_sending_time = ?"
8812                         ", remaining_resend_times = ?"
8813                         ", tag_id = ?"
8814                         ", replied_time = ?"
8815                         ", forwarded_time = ?"
8816                         ", default_charset = ?"
8817                         ", eas_data_length = ?"
8818                         ", eas_data = ?"
8819                         " WHERE mail_id = %d AND account_id != 0 "
8820                         , mail_id);
8821
8822
8823         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8824         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8825                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8826
8827         _bind_stmt_field_data_int(hStmt, i++, mail->mail_id);
8828         _bind_stmt_field_data_int(hStmt, i++, mail->account_id);
8829         _bind_stmt_field_data_int(hStmt, i++, mail->mailbox_id);
8830         _bind_stmt_field_data_int(hStmt, i++, mail->mail_size);
8831         _bind_stmt_field_data_int(hStmt, i++, mail->server_mail_status);
8832         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mailbox_name, 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
8833         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mail_id, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
8834         _bind_stmt_field_data_int(hStmt, i++, mail->reference_mail_id);
8835         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
8836         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
8837         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
8838         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
8839         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
8840         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
8841         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8842         _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8843         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8844         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8845         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
8846         _bind_stmt_field_data_int(hStmt, i++, mail->date_time);
8847         _bind_stmt_field_data_char(hStmt, i++, mail->flags_seen_field);
8848         _bind_stmt_field_data_char(hStmt, i++, mail->flags_deleted_field);
8849         _bind_stmt_field_data_char(hStmt, i++, mail->flags_flagged_field);
8850         _bind_stmt_field_data_char(hStmt, i++, mail->flags_answered_field);
8851         _bind_stmt_field_data_char(hStmt, i++, mail->flags_recent_field);
8852         _bind_stmt_field_data_char(hStmt, i++, mail->flags_draft_field);
8853         _bind_stmt_field_data_char(hStmt, i++, mail->flags_forwarded_field);
8854         _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
8855         _bind_stmt_field_data_int(hStmt, i++, mail->priority);
8856         _bind_stmt_field_data_int(hStmt, i++, mail->save_status);
8857         _bind_stmt_field_data_int(hStmt, i++, mail->lock_status);
8858         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->message_id, 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
8859         _bind_stmt_field_data_int(hStmt, i++, mail->report_status);
8860         _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8861         _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
8862         _bind_stmt_field_data_int(hStmt, i++, mail->scheduled_sending_time);
8863         _bind_stmt_field_data_int(hStmt, i++, mail->remaining_resend_times);
8864         _bind_stmt_field_data_int(hStmt, i++, mail->tag_id);
8865         _bind_stmt_field_data_int(hStmt, i++, mail->replied_time);
8866         _bind_stmt_field_data_int(hStmt, i++, mail->forwarded_time);
8867         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
8868         _bind_stmt_field_data_int(hStmt, i++, mail->eas_data_length);
8869         _bind_stmt_field_data_blob(hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
8870
8871         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8872         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8873                         ("sqlite3_step fail:%d", rc));
8874
8875         rc = sqlite3_changes(local_db_handle);
8876         if (rc == 0) {
8877                 EM_DEBUG_LOG(" no matched mail found...");
8878                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8879                 goto FINISH_OFF;
8880         }
8881
8882         ret = true;
8883
8884 FINISH_OFF:
8885         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
8886         if (hStmt != NULL) {
8887                 rc = sqlite3_finalize(hStmt);
8888                 if (rc != SQLITE_OK) {
8889                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8890                         error = EMAIL_ERROR_DB_FAILURE;
8891                 }
8892         }
8893
8894
8895         if (error == EMAIL_ERROR_NONE && mail) {
8896                 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
8897                 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail->mail_id, mailbox_id_param_string, 0))
8898                         EM_DEBUG_EXCEPTION("emcore_notify_storage_eventfailed [NOTI_MAIL_UPDATE]");
8899         }
8900
8901         if (err_code != NULL)
8902                 *err_code = error;
8903
8904         EM_DEBUG_FUNC_END("ret [%d]", ret);
8905         return ret;
8906 }
8907
8908 /**
8909  *  emstorage_clean_save_status(int save_status, int  *err_code) - set the all mail status to the set value
8910  *
8911  *
8912  **/
8913 INTERNAL_FUNC int emstorage_clean_save_status(char *multi_user_name, int save_status, int  *err_code)
8914 {
8915         EM_DEBUG_FUNC_BEGIN("save_status[%d], err_code[%p]", save_status, err_code);
8916
8917         EM_IF_NULL_RETURN_VALUE(err_code, false);
8918
8919         int ret = false;
8920         int error = EMAIL_ERROR_NONE;
8921         int rc = 0;
8922         char sql_query_string[QUERY_SIZE] = {0, };
8923         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8924
8925         memset(sql_query_string, 0x00, sizeof(sql_query_string));
8926         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);
8927
8928         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8929         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
8930         if (error != EMAIL_ERROR_NONE) {
8931                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
8932                 goto FINISH_OFF;
8933         }
8934
8935         rc = sqlite3_changes(local_db_handle);
8936         if (rc == 0) {
8937                 EM_DEBUG_LOG(" No Matched Mail Exists ");
8938                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8939         }
8940
8941         ret = true;
8942
8943 FINISH_OFF:
8944
8945         if (err_code != NULL)
8946                 *err_code = error;
8947
8948         EM_DEBUG_FUNC_END("ret [%d]", ret);
8949         return ret;
8950 }
8951
8952 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)
8953 {
8954         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);
8955         int i = 0;
8956         int error = EMAIL_ERROR_NONE;
8957         int ret = false;
8958         int query_size = 0;
8959         int cur_mail_id_string = 0;
8960         int mail_id_string_buffer_length = 0;
8961         int parameter_string_length = 0;
8962         char  *sql_query_string = NULL;
8963         char *mail_id_string_buffer = NULL;
8964         char *parameter_string = NULL;
8965         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8966         email_mail_attribute_type target_mail_attribute_type = 0;
8967
8968         if (!mail_ids  || !field_name || account_id == 0) {
8969                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8970                 if (err_code != NULL)
8971                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8972                 return false;
8973         }
8974
8975         if ((error = emcore_get_attribute_type_by_mail_field_name(field_name, &target_mail_attribute_type)) != EMAIL_ERROR_NONE) {
8976                 EM_DEBUG_EXCEPTION("emstorageemcore_get_attribute_type_by_mail_field_name failed [%d]", error);
8977                 if (err_code != NULL)
8978                         *err_code = error;
8979                 return false;
8980         }
8981
8982         /* Generating mail id list string */
8983         mail_id_string_buffer_length = MAIL_ID_STRING_LENGTH * mail_ids_count;
8984
8985         mail_id_string_buffer = em_malloc(mail_id_string_buffer_length);
8986
8987         if (!mail_id_string_buffer) {
8988                 EM_DEBUG_EXCEPTION("em_mallocfailed");
8989                 if (err_code != NULL)
8990                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8991                 return false;
8992         }
8993
8994         for (i = 0; i < mail_ids_count; i++)
8995                 cur_mail_id_string += SNPRINTF_OFFSET(mail_id_string_buffer, cur_mail_id_string, mail_id_string_buffer_length, "%d,", mail_ids[i]);
8996
8997         if (EM_SAFE_STRLEN(mail_id_string_buffer) > 1)
8998                 mail_id_string_buffer[EM_SAFE_STRLEN(mail_id_string_buffer) - 1] = NULL_CHAR;
8999
9000         /* Generating notification parameter string */
9001         parameter_string_length = mail_id_string_buffer_length + EM_SAFE_STRLEN(field_name) + 2;
9002         parameter_string = em_malloc(parameter_string_length);
9003
9004         if (!parameter_string) {
9005                 EM_DEBUG_EXCEPTION("em_mallocfailed");
9006                 if (err_code != NULL)
9007                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
9008                 EM_SAFE_FREE(mail_id_string_buffer);
9009                 return false;
9010         }
9011
9012         SNPRINTF(parameter_string, parameter_string_length, "%s%c%s", field_name, 0x01, mail_id_string_buffer);
9013         query_size = EM_SAFE_STRLEN(mail_id_string_buffer) + EM_SAFE_STRLEN(field_name) + 250;
9014
9015         sql_query_string = em_malloc(query_size);
9016         if (sql_query_string == NULL) {
9017                 EM_DEBUG_EXCEPTION("em_mallocfailed");
9018                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9019                 goto FINISH_OFF;
9020         }
9021         /* Write query string */
9022         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);
9023
9024         EM_DEBUG_LOG_DEV("sql_query_string [%s]", sql_query_string);
9025
9026         /* Execute query */
9027         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9028         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9029         if (error != EMAIL_ERROR_NONE) {
9030                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9031                 goto FINISH_OFF;
9032         }
9033
9034         if (sqlite3_changes(local_db_handle) == 0)
9035                 EM_DEBUG_LOG("no mail matched...");
9036
9037         ret = true;
9038
9039 FINISH_OFF:
9040         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9041
9042         if (error == EMAIL_ERROR_NONE && parameter_string) {
9043                 if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, target_mail_attribute_type, parameter_string, value))
9044                         EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_MAIL_FIELD_UPDATE [%s,%d]", field_name, value);
9045         }
9046
9047         EM_SAFE_FREE(mail_id_string_buffer);
9048         EM_SAFE_FREE(parameter_string);
9049         EM_SAFE_FREE(sql_query_string);
9050
9051
9052         if (err_code != NULL)
9053                 *err_code = error;
9054
9055         EM_DEBUG_FUNC_END("error [%d]", error);
9056         return ret;
9057 }
9058
9059 #ifdef __FEATURE_BODY_SEARCH__
9060 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)
9061 {
9062         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
9063
9064         if (mail_id <= 0 || !mail_text) {
9065                 EM_DEBUG_EXCEPTION(" mail_id[%d], mail_text[%p]", mail_id, mail_text);
9066                 if (err_code != NULL)
9067                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9068                 return false;
9069         }
9070
9071         int ret = false;
9072         int error = EMAIL_ERROR_NONE;
9073         DB_STMT hStmt = NULL;
9074         char sql_query_string[QUERY_SIZE] = {0, };
9075
9076         int i = 0;
9077         int rc = 0;
9078
9079         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9080         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9081
9082         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9083                         "UPDATE mail_text_tbl SET"
9084                         " body_text = ?"
9085                         " WHERE mail_id = %d AND account_id != 0"
9086                         , mail_id);
9087         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9088
9089         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9090         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9091                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9092
9093         i = 0;
9094         _bind_stmt_field_data_string(hStmt, i++, (char *)mail_text->body_text, 0, -1);
9095
9096         if (hStmt != NULL) {
9097                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9098                 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9099                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9100
9101                 rc = sqlite3_changes(local_db_handle);
9102                 if (rc == 0) {
9103                         EM_DEBUG_LOG(" no matched mail found...");
9104                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
9105                         goto FINISH_OFF;
9106                 }
9107         }
9108
9109         ret = true;
9110
9111 FINISH_OFF:
9112         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9113
9114         if (hStmt != NULL) {
9115                 rc = sqlite3_finalize(hStmt);
9116                 if (rc != SQLITE_OK) {
9117                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9118                         error = EMAIL_ERROR_DB_FAILURE;
9119                 }
9120                 hStmt = NULL;
9121         }
9122
9123         if (err_code != NULL)
9124                 *err_code = error;
9125
9126         EM_DEBUG_FUNC_END("ret [%d]", ret);
9127         return ret;
9128 }
9129 #endif
9130
9131 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)
9132 {
9133         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
9134
9135         int ret = false;
9136         int error = EMAIL_ERROR_NONE;
9137         int move_flag = 0;
9138         int rc = 0;
9139         int i = 0;
9140         int mailbox_id = 0;
9141         DB_STMT hStmt = NULL;
9142         char sql_query_string[QUERY_SIZE] = {0, };
9143         char mailbox_id_param_string[10] = {0,};
9144         sqlite3 *local_db_handle = NULL;
9145
9146         if (mail_id <= 0 || !mail) {
9147                 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], mail[%p]", mail_id, type, mail);
9148                 if (err_code != NULL)
9149                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9150                 return false;
9151         }
9152
9153         local_db_handle = emstorage_get_db_connection(multi_user_name);
9154
9155         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9156
9157         switch (type) {
9158         case APPEND_BODY:
9159                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9160                                 "UPDATE mail_tbl SET"
9161                                 "  body_download_status = ?"
9162                                 ", file_path_plain = ?"
9163                                 ", file_path_html = ?"
9164                                 ", file_path_mime_entity = ?"
9165                                 ", flags_seen_field      = ?"
9166                                 ", flags_deleted_field   = ?"
9167                                 ", flags_flagged_field   = ?"
9168                                 ", flags_answered_field  = ?"
9169                                 ", flags_recent_field    = ?"
9170                                 ", flags_draft_field     = ?"
9171                                 ", flags_forwarded_field = ?"
9172                                 ", DRM_status = ?"
9173                                 ", attachment_count = ?"
9174                                 ", preview_text= ?"
9175                                 ", meeting_request_status = ? "
9176                                 ", message_class = ? "
9177                                 ", digest_type = ? "
9178                                 ", smime_type = ? "
9179                                 " WHERE mail_id = %d AND account_id != 0"
9180                                 , mail_id);
9181
9182
9183                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9184                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9185                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9186                 i = 0;
9187
9188                 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9189                 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9190                 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9191                 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9192                 _bind_stmt_field_data_int(hStmt, i++, mail->flags_seen_field);
9193                 _bind_stmt_field_data_int(hStmt, i++, mail->flags_deleted_field);
9194                 _bind_stmt_field_data_int(hStmt, i++, mail->flags_flagged_field);
9195                 _bind_stmt_field_data_int(hStmt, i++, mail->flags_answered_field);
9196                 _bind_stmt_field_data_int(hStmt, i++, mail->flags_recent_field);
9197                 _bind_stmt_field_data_int(hStmt, i++, mail->flags_draft_field);
9198                 _bind_stmt_field_data_int(hStmt, i++, mail->flags_forwarded_field);
9199                 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9200                 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9201                 _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9202                 _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9203                 _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9204                 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9205                 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9206                 break;
9207
9208         case UPDATE_MAILBOX: {
9209                                                          int err;
9210                                                          emstorage_mailbox_tbl_t *mailbox_tbl;
9211
9212                                                          if ((err = emstorage_get_mailbox_by_id(multi_user_name, mail->mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
9213                                                                  EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_id failed [%d]", err);
9214                                                                  goto FINISH_OFF;
9215                                                          }
9216
9217                                                          SNPRINTF(sql_query_string, sizeof(sql_query_string),
9218                                                                          "UPDATE mail_tbl SET"
9219                                                                          " mailbox_id = '%d'"
9220                                                                          ",mailbox_type = '%d'"
9221                                                                          " WHERE mail_id = %d AND account_id != 0"
9222                                                                          , mailbox_tbl->mailbox_id
9223                                                                          , mailbox_tbl->mailbox_type
9224                                                                          , mail_id);
9225                                                          move_flag = 1;
9226
9227
9228                                                          EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9229                                                          EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9230                                                                          ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9231
9232                                                          i = 0;
9233                                                          _bind_stmt_field_data_string(hStmt, i++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
9234
9235                                                          emstorage_free_mailbox(&mailbox_tbl, 1, NULL); /*prevent 26251*/
9236
9237                                                  }
9238                                                  break;
9239
9240         case UPDATE_FLAG:
9241                                                  SNPRINTF(sql_query_string, sizeof(sql_query_string),
9242                                                                  "UPDATE mail_tbl SET"
9243                                                                  " flags_seen_field      = %d"
9244                                                                  ",flags_deleted_field   = %d"
9245                                                                  ",flags_flagged_field   = %d"
9246                                                                  ",flags_answered_field  = %d"
9247                                                                  ",flags_recent_field    = %d"
9248                                                                  ",flags_draft_field     = %d"
9249                                                                  ",flags_forwarded_field = %d"
9250                                                                  "  WHERE mail_id = %d AND account_id != 0"
9251                                                                  , mail->flags_seen_field
9252                                                                  , mail->flags_deleted_field
9253                                                                  , mail->flags_flagged_field
9254                                                                  , mail->flags_answered_field
9255                                                                  , mail->flags_recent_field
9256                                                                  , mail->flags_draft_field
9257                                                                  , mail->flags_forwarded_field
9258                                                                  , mail_id);
9259                                                  EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9260
9261
9262                                                  EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9263                                                  EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9264                                                                  ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9265
9266                                                  break;
9267
9268         case UPDATE_EXTRA_FLAG:
9269                                                  SNPRINTF(sql_query_string, sizeof(sql_query_string),
9270                                                                  "UPDATE mail_tbl SET"
9271                                                                  "  priority = %d"
9272                                                                  ", save_status = %d"
9273                                                                  ", lock_status = %d"
9274                                                                  ", report_status = %d"
9275                                                                  ", DRM_status = %d"
9276                                                                  " WHERE mail_id = %d AND account_id != 0"
9277                                                                  , mail->priority
9278                                                                  , mail->save_status
9279                                                                  , mail->lock_status
9280                                                                  , mail->report_status
9281                                                                  , mail->DRM_status
9282                                                                  , mail_id);
9283                                                  EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9284
9285
9286                                                  EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9287                                                  EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9288                                                                  ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9289                                                  break;
9290
9291         case UPDATE_STICKY_EXTRA_FLAG:
9292                                                  SNPRINTF(sql_query_string, sizeof(sql_query_string),
9293                                                                  "UPDATE mail_tbl SET"
9294                                                                  "  lock_status = %d"
9295                                                                  "  WHERE mail_id = %d AND account_id != 0"
9296                                                                  , mail->lock_status
9297                                                                  , mail_id);
9298                                                  EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9299
9300
9301                                                  EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9302                                                  EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9303                                                                  ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9304                                                  break;
9305
9306         case UPDATE_MAIL:
9307                                                  SNPRINTF(sql_query_string, sizeof(sql_query_string),
9308                                                                  "UPDATE mail_tbl SET"
9309                                                                  "  full_address_from = ?"
9310                                                                  ", full_address_reply = ?"
9311                                                                  ", full_address_to = ?"
9312                                                                  ", full_address_cc = ?"
9313                                                                  ", full_address_bcc = ?"
9314                                                                  ", full_address_return = ?"
9315                                                                  ", subject = ?"
9316                                                                  ", file_path_plain = ?"
9317                                                                  ", date_time = ?"
9318                                                                  ", flags_seen_field = ?"
9319                                                                  ", flags_deleted_field = ?"
9320                                                                  ", flags_flagged_field = ?"
9321                                                                  ", flags_answered_field = ?"
9322                                                                  ", flags_recent_field = ?"
9323                                                                  ", flags_draft_field = ?"
9324                                                                  ", flags_forwarded_field = ?"
9325                                                                  ", priority = ?"
9326                                                                  ", save_status = ?"
9327                                                                  ", lock_status = ?"
9328                                                                  ", report_status = ?"
9329                                                                  ", DRM_status = ?"
9330                                                                  ", file_path_html = ?"
9331                                                                  ", file_path_mime_entity = ?"
9332                                                                  ", mail_size = ?"
9333                                                                  ", preview_text = ?"
9334                                                                  ", body_download_status = ?"
9335                                                                  ", attachment_count = ?"
9336                                                                  ", inline_content_count = ?"
9337                                                                  ", meeting_request_status = ?"
9338                                                                  ", message_class = ?"
9339                                                                  ", digest_type = ?"
9340                                                                  ", smime_type = ?"
9341                                                                  ", scheduled_sending_time = ?"
9342                                                                  ", remaining_resend_times = ?"
9343                                                                  ", tag_id = ?"
9344                                                                  ", replied_time = ?"
9345                                                                  ", forwarded_time = ?"
9346                                                                  ", default_charset = ?"
9347                                                                  ", eas_data_length = ?"
9348                                                                  ", eas_data = ?"
9349                                                                  " WHERE mail_id = %d AND account_id != 0"
9350                                                                  , mail_id);
9351
9352
9353                                                  EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9354                                                  EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
9355                                                  EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9356                                                                  ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9357                                                  i = 0;
9358                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
9359                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
9360                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
9361                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
9362                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
9363                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
9364                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9365                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9366                                                  _bind_stmt_field_data_time_t(hStmt, i++, mail->date_time);
9367                                                  _bind_stmt_field_data_char(hStmt, i++, mail->flags_seen_field);
9368                                                  _bind_stmt_field_data_char(hStmt, i++, mail->flags_deleted_field);
9369                                                  _bind_stmt_field_data_char(hStmt, i++, mail->flags_flagged_field);
9370                                                  _bind_stmt_field_data_char(hStmt, i++, mail->flags_answered_field);
9371                                                  _bind_stmt_field_data_char(hStmt, i++, mail->flags_recent_field);
9372                                                  _bind_stmt_field_data_char(hStmt, i++, mail->flags_draft_field);
9373                                                  _bind_stmt_field_data_char(hStmt, i++, mail->flags_forwarded_field);
9374                                                  _bind_stmt_field_data_int(hStmt, i++, mail->priority);
9375                                                  _bind_stmt_field_data_int(hStmt, i++, mail->save_status);
9376                                                  _bind_stmt_field_data_int(hStmt, i++, mail->lock_status);
9377                                                  _bind_stmt_field_data_int(hStmt, i++, mail->report_status);
9378                                                  _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9379                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9380                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9381                                                  _bind_stmt_field_data_int(hStmt, i++, mail->mail_size);
9382                                                  _bind_stmt_field_data_nstring(hStmt, i++, (char*)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9383                                                  _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9384                                                  _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9385                                                  _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9386                                                  _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9387                                                  _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9388                                                  _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9389                                                  _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9390                                                  _bind_stmt_field_data_int(hStmt, i++, mail->scheduled_sending_time);
9391                                                  _bind_stmt_field_data_int(hStmt, i++, mail->remaining_resend_times);
9392                                                  _bind_stmt_field_data_int(hStmt, i++, mail->tag_id);
9393                                                  _bind_stmt_field_data_int(hStmt, i++, mail->replied_time);
9394                                                  _bind_stmt_field_data_int(hStmt, i++, mail->forwarded_time);
9395                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
9396                                                  _bind_stmt_field_data_int(hStmt, i++, mail->eas_data_length);
9397                                                  _bind_stmt_field_data_blob(hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
9398                                                  break;
9399
9400         case UPDATE_DATETIME: {
9401                                                           SNPRINTF(sql_query_string, sizeof(sql_query_string),
9402                                                                           "UPDATE mail_tbl SET"
9403                                                                           " date_time = '%ld'"
9404                                                                           " WHERE mail_id = %d AND account_id != 0"
9405                                                                           , mail->date_time
9406                                                                           , mail_id);
9407
9408                                                           EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9409                                                           EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9410                                                                           ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9411                                                           break;
9412                                                   }
9413
9414         case UPDATE_FROM_CONTACT_INFO:
9415                                                   EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_FROM_CONTACT_INFO");
9416                                                   SNPRINTF(sql_query_string, sizeof(sql_query_string),
9417                                                                   "UPDATE mail_tbl SET"
9418                                                                   " email_address_sender = ?,"
9419                                                                   " WHERE mail_id = %d",
9420                                                                   mail_id);
9421
9422                                                   hStmt = NULL;
9423
9424                                                   EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9425                                                   EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9426                                                                   ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9427                                                   i = 0;
9428                                                   _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9429                                                   break;
9430
9431         case UPDATE_TO_CONTACT_INFO:
9432                                                   EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_TO_CONTACT_INFO");
9433                                                   SNPRINTF(sql_query_string, sizeof(sql_query_string),
9434                                                                   "UPDATE mail_tbl SET"
9435                                                                   " email_address_recipient = ?,"
9436                                                                   " WHERE mail_id = %d",
9437                                                                   mail_id);
9438
9439                                                   hStmt = NULL;
9440
9441                                                   EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9442                                                   EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9443                                                                   ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9444                                                   i = 0;
9445                                                   _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9446                                                   break;
9447
9448         case UPDATE_ALL_CONTACT_INFO:
9449                                                   EM_DEBUG_LOG("emstorage_change_mail_field - mail change type is UPDATE_ALL_CONTACT_INFO");
9450                                                   SNPRINTF(sql_query_string, sizeof(sql_query_string),
9451                                                                   "UPDATE mail_tbl SET"
9452                                                                   " email_address_sender = ?,"
9453                                                                   " email_address_recipient = ?,"
9454                                                                   " WHERE mail_id = %d",
9455                                                                   mail_id);
9456
9457                                                   hStmt = NULL;
9458
9459                                                   EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9460                                                   EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9461                                                                   ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9462                                                   i = 0;
9463                                                   _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9464                                                   _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9465                                                   break;
9466
9467
9468 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
9469         case UPDATE_PARTIAL_BODY_DOWNLOAD:
9470
9471                                                   SNPRINTF(sql_query_string, sizeof(sql_query_string),
9472                                                                   "UPDATE mail_tbl SET"
9473                                                                   "  body_download_status = ?"
9474                                                                   ", file_path_plain = ?"
9475                                                                   ", file_path_html = ?"
9476                                                                   ", file_path_mime_entity = ?"
9477                                                                   ", attachment_count = ?"
9478                                                                   ", inline_content_count = ?"
9479                                                                   ", preview_text = ?"
9480                                                                   ", digest_type = ?"
9481                                                                   ", smime_type = ?"
9482                                                                   " WHERE mail_id = %d"
9483                                                                   , mail_id);
9484
9485
9486                                                   EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9487                                                   EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9488                                                                   ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9489                                                   i = 0;
9490
9491                                                   _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9492                                                   _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9493                                                   _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html,  0, TEXT_2_LEN_IN_MAIL_TBL);
9494                                                   _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9495                                                   _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9496                                                   _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9497                                                   _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text,    0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9498                                                   _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9499                                                   _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9500
9501                                                   break;
9502
9503 #endif
9504         case UPDATE_FILE_PATH:
9505                                                   SNPRINTF(sql_query_string, sizeof(sql_query_string),
9506                                                                   "UPDATE mail_tbl SET"
9507                                                                   ", file_path_plain = ?"
9508                                                                   ", file_path_html = ?"
9509                                                                   ", file_path_mime_entity = ?"
9510                                                                   " WHERE mail_id = %d"
9511                                                                   , mail_id);
9512
9513
9514                                                   EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9515                                                   EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
9516                                                   EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9517                                                                   ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9518                                                   i = 0;
9519                                                   _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9520                                                   _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9521                                                   _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9522                                                   break;
9523
9524         default:
9525                                                   EM_DEBUG_LOG(" type[%d]", type);
9526
9527                                                   error = EMAIL_ERROR_INVALID_PARAM;
9528                                                   goto FINISH_OFF;
9529         }
9530
9531         if (hStmt != NULL) {
9532
9533                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9534                 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
9535                                 ("sqlite3_step fail:%d", rc));
9536                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9537                                 ("sqlite3_step fail:%d", rc));
9538                 rc = sqlite3_changes(local_db_handle);
9539                 if (rc == 0) {
9540                         EM_DEBUG_LOG(" no matched mail found...");
9541                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
9542                         goto FINISH_OFF;
9543                 }
9544         }
9545
9546         if (mail->account_id == 0) {
9547                 emstorage_mail_tbl_t* mail_for_account_tbl = NULL;
9548                 if (!emstorage_get_mail_field_by_id(multi_user_name, mail_id, RETRIEVE_ACCOUNT, &mail_for_account_tbl, true, &error) || !mail_for_account_tbl) {
9549                         EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id error [%d]", error);
9550                         goto FINISH_OFF;
9551                 }
9552                 mail->account_id = mail_for_account_tbl->account_id;
9553                 if (mail_for_account_tbl)
9554                         emstorage_free_mail(&mail_for_account_tbl, 1, NULL);
9555         }
9556
9557         ret = true;
9558
9559 FINISH_OFF:
9560         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9561
9562         if (hStmt != NULL) {
9563                 rc = sqlite3_finalize(hStmt);
9564                 if (rc != SQLITE_OK) {
9565                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9566                         error = EMAIL_ERROR_DB_FAILURE;
9567                 }
9568                 hStmt = NULL;
9569         }
9570
9571         if (error == EMAIL_ERROR_NONE &&  move_flag != 1 && transaction) {
9572                 if (!emstorage_get_mailbox_id_by_mailbox_type(multi_user_name, mail->account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &mailbox_id, false, &error))
9573                         EM_DEBUG_EXCEPTION("emstorage_get_mailbox_id_by_mailbox_type error [%d]", error);
9574
9575                 if (mail->mailbox_id == mailbox_id) {
9576                         SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
9577                         if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, mailbox_id_param_string, type))
9578                                 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventerror [ NOTI_MAIL_UPDATE ] >>>> ");
9579                 } else {
9580                         /* h.gahlaut@samsung.com: Jan 10, 2011 Publishing noti to refresh outbox when email sending status changes */
9581                         if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, NULL, type))
9582                                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventerror [ NOTI_MAIL_UPDATE ]");
9583                 }
9584         }
9585
9586         if (err_code != NULL)
9587                 *err_code = error;
9588
9589         EM_DEBUG_FUNC_END("ret [%d]", ret);
9590         return ret;
9591 }
9592
9593 INTERNAL_FUNC int emstorage_increase_mail_id(char *multi_user_name, int *mail_id, int transaction, int *err_code)
9594 {
9595         EM_DEBUG_FUNC_BEGIN("mail_id[%p], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9596
9597         int rc, ret = false;
9598         int error = EMAIL_ERROR_NONE;
9599         int latest_mail_id = 0;
9600         sqlite3 *local_db_handle = NULL;
9601         char *sql = "SELECT MAX(mail_id) FROM mail_tbl;";
9602         char **result = NULL;
9603
9604 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9605         _timedlock_shm_mutex(mapped_for_generating_mail_id, 2);
9606 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9607
9608         ret = vconf_get_int(VCONF_KEY_LATEST_MAIL_ID, &latest_mail_id);
9609         if (ret < 0 || latest_mail_id == 0) {
9610                 EM_DEBUG_LOG("vconf_get_int() failed [%d] or latest_mail_id is zero", ret);
9611
9612                 local_db_handle = emstorage_get_db_connection(multi_user_name);
9613
9614                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9615                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
9616                                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9617                 if (NULL == result[1])
9618                         rc = 1;
9619                 else
9620                         rc = atoi(result[1]) + 1;
9621
9622                 sqlite3_free_table(result);
9623                 latest_mail_id = rc;
9624         }
9625
9626         latest_mail_id++;
9627
9628         ret = vconf_set_int(VCONF_KEY_LATEST_MAIL_ID, latest_mail_id);
9629
9630         if (mail_id)
9631                 *mail_id = latest_mail_id;
9632
9633         ret = true;
9634
9635 FINISH_OFF:
9636
9637 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9638                 _unlockshm_mutex(mapped_for_generating_mail_id);
9639 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9640
9641         if (err_code != NULL)
9642                 *err_code = error;
9643
9644         EM_DEBUG_FUNC_END("ret [%d]", ret);
9645         return ret;
9646 }
9647
9648 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)
9649 {
9650         EM_PROFILE_BEGIN(profile_emstorage_add_mail);
9651         EM_DEBUG_FUNC_BEGIN("mail_tbl_data[%p], get_id[%d], transaction[%d], err_code[%p]", mail_tbl_data, get_id, transaction, err_code);
9652
9653         if (!mail_tbl_data) {
9654                 EM_DEBUG_EXCEPTION("mail_tbl_data[%p], get_id[%d]", mail_tbl_data, get_id);
9655                 if (err_code != NULL)
9656                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9657                 return false;
9658         }
9659
9660         int rc, ret = false;
9661         int error = EMAIL_ERROR_NONE;
9662         char sql_query_string[QUERY_SIZE] = {0, };
9663         DB_STMT hStmt = NULL;
9664
9665         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9666
9667         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9668
9669         if (get_id) {
9670                 /*  increase unique id */
9671                 char *sql = "SELECT max(rowid) FROM mail_tbl;";
9672                 char **result;
9673
9674                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9675                 /*              EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
9676                                 ("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle))); */
9677                 if (rc != SQLITE_OK) {
9678                         EM_DEBUG_EXCEPTION("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle));
9679                         error = EMAIL_ERROR_DB_FAILURE;
9680                         sqlite3_free_table(result);
9681                         goto FINISH_OFF;
9682                 }
9683
9684                 if (NULL == result[1])
9685                         rc = 1;
9686                 else
9687                         rc = atoi(result[1])+1;
9688
9689                 sqlite3_free_table(result);
9690
9691                 mail_tbl_data->mail_id   = rc;
9692                 mail_tbl_data->thread_id = rc;
9693         }
9694
9695         if (mail_tbl_data->date_time == 0)
9696                 mail_tbl_data->date_time = time(NULL);
9697
9698         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9699                         "INSERT INTO mail_tbl VALUES "
9700                         "(?" /*  mail_id */
9701                         ", ?" /*  account_id */
9702                         ", ?" /*  mailbox_id */
9703                         ", ?" /*  mailbox_type */
9704                         ", ?" /*  subject */
9705
9706                         ", ?" /*  date_time */
9707                         ", ?" /*  server_mail_status */
9708                         ", ?" /*  server_mailbox_name */
9709                         ", ?" /*  server_mail_id */
9710                         ", ?" /*  message_id */
9711
9712                         ", ?" /*  reference_mail_id */
9713                         ", ?" /*  full_address_from */
9714                         ", ?" /*  full_address_reply */
9715                         ", ?" /*  full_address_to */
9716                         ", ?" /*  full_address_cc */
9717
9718                         ", ?" /*  full_address_bcc */
9719                         ", ?" /*  full_address_return */
9720                         ", ?" /*  email_address_sender */
9721                         ", ?" /*  email_address_recipient */
9722                         ", ?" /*  alias_sender */
9723
9724                         ", ?" /*  alias_recipient */
9725                         ", ?" /*  body_download_status */
9726                         ", ?" /*  file_path_plain */
9727                         ", ?" /*  file_path_html */
9728                         ", ?" /*  file_path_mime_entity */
9729
9730                         ", ?" /*  mail_size */
9731                         ", ?" /*  flags_seen_field */
9732                         ", ?" /*  flags_deleted_field */
9733                         ", ?" /*  flags_flagged_field */
9734                         ", ?" /*  flags_answered_field */
9735
9736                         ", ?" /*  flags_recent_field */
9737                         ", ?" /*  flags_draft_field */
9738                         ", ?" /*  flags_forwarded_field */
9739                         ", ?" /*  DRM_status */
9740                         ", ?" /*  priority */
9741
9742                         ", ?" /*  save_status */
9743                         ", ?" /*  lock_status */
9744                         ", ?" /*  report_status */
9745                         ", ?" /*  attachment_count */
9746                         ", ?" /*  inline_content_count */
9747
9748                         ", ?" /*  thread_id */
9749                         ", ?" /*  thread_item_count */
9750                         ", ?" /*  preview_text */
9751                         ", ?" /*  meeting_request_status */
9752                         ", ?" /*  message_class */
9753
9754                         ", ?" /*  digest_type */
9755                         ", ?" /*  smime_type */
9756                         ", ?" /*  scheduled_sending_time */
9757                         ", ?" /*  remaining_resend_times */
9758                         ", ?" /*  tag_id */
9759
9760                         ", ?" /*  replied_time */
9761                         ", ?" /*  forwarded_time */
9762                         ", ?" /*  default charset */
9763                         ", ?" /*  eas_data_length */
9764                         ", ?" /*  eas_data */
9765                         ", ?" /*  user_name */
9766                         ")");
9767
9768         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle,
9769                                 sql_query_string,
9770                                 EM_SAFE_STRLEN(sql_query_string),
9771                                 &hStmt,
9772                                 NULL),
9773                         rc);
9774         if (rc != SQLITE_OK) {
9775                 EM_DEBUG_EXCEPTION("sqlite3_prepare error [%d] [%s] SQL(%s) ",
9776                                 rc, sql_query_string, sqlite3_errmsg(local_db_handle));
9777                 error = EMAIL_ERROR_DB_FAILURE;
9778                 goto FINISH_OFF;
9779         }
9780
9781         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mail_id);
9782         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, mail_tbl_data->account_id);
9783         _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_id);
9784         _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_type);
9785         _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9786
9787         _bind_stmt_field_data_int(hStmt, DATETIME_IDX_IN_MAIL_TBL, mail_tbl_data->date_time);
9788         _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->server_mail_status);
9789         _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);
9790         _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);
9791         _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);
9792
9793         _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, mail_tbl_data->reference_mail_id);
9794         _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);
9795         _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);
9796         _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);
9797         _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);
9798
9799         _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);
9800         _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);
9801         _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);
9802         _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);
9803         _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);
9804
9805         _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);
9806         _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->body_download_status);
9807         _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);
9808         _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);
9809         _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);
9810
9811         _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, mail_tbl_data->mail_size);
9812         _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_seen_field);
9813         _bind_stmt_field_data_int(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_deleted_field);
9814         _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_flagged_field);
9815         _bind_stmt_field_data_int(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_answered_field);
9816
9817         _bind_stmt_field_data_int(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_recent_field);
9818         _bind_stmt_field_data_int(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_draft_field);
9819         _bind_stmt_field_data_int(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_forwarded_field);
9820         _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->DRM_status);
9821         _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, mail_tbl_data->priority);
9822
9823         _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->save_status);
9824         _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->lock_status);
9825         _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->report_status);
9826         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->attachment_count);
9827         _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->inline_content_count);
9828
9829         _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, mail_tbl_data->thread_id);
9830         _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->thread_item_count);
9831         _bind_stmt_field_data_nstring(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9832         _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->meeting_request_status);
9833         _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, mail_tbl_data->message_class);
9834
9835         _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->digest_type);
9836         _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->smime_type);
9837         _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->scheduled_sending_time);
9838         _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, mail_tbl_data->remaining_resend_times);
9839         _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, mail_tbl_data->tag_id);
9840
9841         _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->replied_time);
9842         _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->forwarded_time);
9843         _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);
9844         _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, mail_tbl_data->eas_data_length);
9845         _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, (void*)mail_tbl_data->eas_data, mail_tbl_data->eas_data_length);
9846         _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);
9847
9848         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9849         if (rc == SQLITE_FULL) {
9850                 EM_DEBUG_EXCEPTION("sqlite3_step error [%d]", rc);
9851                 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
9852                 goto FINISH_OFF;
9853         }
9854         if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
9855                 EM_DEBUG_EXCEPTION("sqlite3_step error [%d]", rc);
9856                 error = EMAIL_ERROR_DB_FAILURE;
9857                 goto FINISH_OFF;
9858         }
9859         ret = true;
9860
9861 FINISH_OFF:
9862         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9863
9864         if (hStmt != NULL) {
9865                 rc = sqlite3_finalize(hStmt);
9866                 if (rc != SQLITE_OK) {
9867                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9868                         error = EMAIL_ERROR_DB_FAILURE;
9869                 }
9870         }
9871
9872         if (err_code != NULL)
9873                 *err_code = error;
9874
9875         EM_PROFILE_END(profile_emstorage_add_mail);
9876         EM_DEBUG_FUNC_END("ret [%d]", ret);
9877         return ret;
9878 }
9879
9880 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)
9881 {
9882         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);
9883
9884         int ret = false, i, cur_conditional_clause = 0;
9885         int error = EMAIL_ERROR_NONE;
9886         int target_account_id;
9887         int conditional_clause_len = 0;
9888         char *sql_query_string = NULL, *conditional_clause = NULL;
9889         emstorage_mailbox_tbl_t *result_mailbox = NULL;
9890         email_mailbox_type_e target_mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED;
9891         char* target_mailbox_name = NULL;
9892
9893         if (!mail_ids || input_mailbox_id <= 0) {
9894                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9895                 if (err_code != NULL)
9896                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9897                 return false;
9898         }
9899
9900         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9901
9902         if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &result_mailbox)) != EMAIL_ERROR_NONE || !result_mailbox) {
9903                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", error);
9904                 if (err_code != NULL)
9905                         *err_code = error;
9906                 return false;
9907         }
9908
9909         if (result_mailbox->mailbox_name) {
9910                 if (strstr(result_mailbox->mailbox_name, "'"))
9911                         target_mailbox_name = em_replace_all_string(result_mailbox->mailbox_name, "'", "''");
9912                 else
9913                         target_mailbox_name = strdup(result_mailbox->mailbox_name);
9914         }
9915
9916         target_mailbox_type = result_mailbox->mailbox_type;
9917         target_account_id   = result_mailbox->account_id;
9918         emstorage_free_mailbox(&result_mailbox, 1, NULL);
9919
9920         conditional_clause_len =  (sizeof(char) * 8 * number_of_mails) + 512;
9921         conditional_clause = em_malloc(conditional_clause_len);
9922         if (conditional_clause == NULL) {
9923                 EM_DEBUG_EXCEPTION("em_mallocfailed");
9924                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9925                 goto FINISH_OFF;
9926         }
9927         cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE mail_id in (");
9928
9929         for (i = 0; i < number_of_mails; i++)
9930                 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
9931
9932         /* prevent 34415 */
9933         char *last_comma = rindex(conditional_clause, ',');
9934         if (last_comma) *last_comma = ')'; /* replace , with) */
9935
9936         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9937
9938         /* Updating a mail_tbl */
9939
9940         sql_query_string = em_malloc(conditional_clause_len);
9941         if (sql_query_string == NULL) {
9942                 EM_DEBUG_EXCEPTION("em_mallocfailed");
9943                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9944                 goto FINISH_OFF;
9945         }
9946         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);
9947         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9948
9949         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9950         if (error != EMAIL_ERROR_NONE) {
9951                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9952                 goto FINISH_OFF;
9953         }
9954
9955         /* Updating a mail_attachment_tbl */
9956         memset(sql_query_string, 0x00, conditional_clause_len);
9957         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);
9958         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9959         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9960         if (error != EMAIL_ERROR_NONE) {
9961                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9962                 goto FINISH_OFF;
9963         }
9964
9965         /* Updating a mail_meeting_tbl */
9966         memset(sql_query_string, 0x00, conditional_clause_len);
9967         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);
9968         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9969         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9970         if (error != EMAIL_ERROR_NONE) {
9971                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9972                 goto FINISH_OFF;
9973         }
9974
9975 #ifdef __FEATURE_BODY_SEARCH__
9976         /* Updating mail_text_tbl */
9977         memset(sql_query_string, 0x00, conditional_clause_len);
9978         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);
9979         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9980         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9981         if (error != EMAIL_ERROR_NONE) {
9982                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9983                 goto FINISH_OFF;
9984         }
9985 #endif
9986
9987         /* Updating a mail_read_mail_uid_tbl */
9988         memset(conditional_clause, 0x00, conditional_clause_len);
9989         cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE local_uid in (");
9990
9991         for (i = 0; i < number_of_mails; i++)
9992                 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
9993
9994         /* prevent 34415 */
9995         last_comma = rindex(conditional_clause, ',');
9996         if (last_comma) *last_comma = ')'; /* replace , with) */
9997
9998         memset(sql_query_string, 0x00, conditional_clause_len);
9999         SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_read_mail_uid_tbl SET mailbox_name = ? mailbox_id = ? account_id = ? %s", conditional_clause);
10000
10001         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10002         error = emstorage_exec_query_by_prepare_v2_stmt(local_db_handle, sql_query_string, target_mailbox_name, input_mailbox_id, target_account_id);
10003         if (error != EMAIL_ERROR_NONE) {
10004                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2_stmt failed:[%d]", error);
10005                 goto FINISH_OFF;
10006         }
10007
10008         ret = true;
10009
10010 FINISH_OFF:
10011         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10012
10013         EM_SAFE_FREE(target_mailbox_name);
10014         EM_SAFE_FREE(conditional_clause);
10015         EM_SAFE_FREE(sql_query_string);
10016
10017         if (err_code != NULL)
10018                 *err_code = error;
10019
10020         EM_DEBUG_FUNC_END("ret [%d]", ret);
10021         return ret;
10022 }
10023
10024 INTERNAL_FUNC int emstorage_delete_mail(char *multi_user_name, int mail_id, int from_server, int transaction, int *err_code)
10025 {
10026         EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
10027
10028         if (!mail_id) {
10029                 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
10030                 if (err_code != NULL)
10031                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10032                 return false;
10033         }
10034
10035         int ret = false;
10036         int error = EMAIL_ERROR_NONE;
10037         char sql_query_string[QUERY_SIZE] = {0, };
10038         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10039         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10040
10041         memset(sql_query_string, 0x00, sizeof(sql_query_string));
10042
10043         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id = %d ", mail_id);
10044         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10045         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10046         if (error != EMAIL_ERROR_NONE) {
10047                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10048                 goto FINISH_OFF;
10049         }
10050
10051         ret = true;
10052
10053 FINISH_OFF:
10054         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10055
10056         if (err_code != NULL)
10057                 *err_code = error;
10058
10059         EM_DEBUG_FUNC_END("ret [%d]", ret);
10060         return ret;
10061 }
10062
10063 INTERNAL_FUNC int emstorage_delete_multiple_mails(char *multi_user_name, int mail_ids[], int number_of_mails, int transaction, int *err_code)
10064 {
10065         EM_DEBUG_FUNC_BEGIN("mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", mail_ids, number_of_mails, transaction, err_code);
10066
10067         int ret = false, i, cur_sql_query_string = 0;
10068         int error = EMAIL_ERROR_NONE;
10069         int query_size = 0;
10070         char *sql_query_string = NULL;
10071
10072         if (!mail_ids) {
10073                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10074                 if (err_code != NULL)
10075                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10076                 return false;
10077         }
10078
10079         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10080         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10081
10082         query_size = (sizeof(char) * 8 * number_of_mails) + 512;
10083         sql_query_string =  em_malloc(query_size);
10084         if (sql_query_string == NULL) {
10085                 EM_DEBUG_EXCEPTION("em_mallocfailed");
10086                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10087                 goto FINISH_OFF;
10088         }
10089
10090         cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_tbl WHERE mail_id in (");
10091
10092         for (i = 0; i < number_of_mails; i++)
10093                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
10094
10095         /* prevent 34414 */
10096         char *last_comma = rindex(sql_query_string, ',');
10097         if (last_comma != NULL) *last_comma = ')'; /* replace , with) */
10098
10099         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10100         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10101         if (error != EMAIL_ERROR_NONE) {
10102                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10103                 goto FINISH_OFF;
10104         }
10105
10106 #ifdef __FEATURE_BODY_SEARCH__
10107         /* delete mail_text from mail_text_tbl */
10108         cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_text_tbl WHERE mail_id in (");
10109
10110         for (i = 0; i < number_of_mails; i++)
10111                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
10112
10113         last_comma = rindex(sql_query_string, ',');
10114         *last_comma = ')'; /* replace , with) */
10115
10116         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10117         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10118         if (error != EMAIL_ERROR_NONE) {
10119                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10120                 goto FINISH_OFF;
10121         }
10122 #endif
10123
10124         ret = true;
10125
10126 FINISH_OFF:
10127         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10128
10129         EM_SAFE_FREE(sql_query_string);
10130         if (err_code != NULL)
10131                 *err_code = error;
10132
10133         EM_DEBUG_FUNC_END("ret [%d]", ret);
10134         return ret;
10135 }
10136
10137 INTERNAL_FUNC int emstorage_delete_mail_by_account(char *multi_user_name, int account_id, int transaction, int *err_code)
10138 {
10139         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
10140
10141         if (account_id < FIRST_ACCOUNT_ID) {
10142                 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
10143
10144                 if (err_code != NULL)
10145                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10146                 return false;
10147         }
10148
10149         int rc, ret = false;
10150         int error = EMAIL_ERROR_NONE;
10151         char sql_query_string[QUERY_SIZE] = {0, };
10152
10153         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10154         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10155
10156         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d", account_id);
10157         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10158         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10159         if (error != EMAIL_ERROR_NONE) {
10160                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10161                 goto FINISH_OFF;
10162         }
10163
10164         rc = sqlite3_changes(local_db_handle);
10165         if (rc == 0) {
10166                 EM_DEBUG_LOG(" no mail found...");
10167                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10168         }
10169
10170         /* Delete all mails  mail_read_mail_uid_tbl table based on account id */
10171         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
10172         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10173         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10174         if (error != EMAIL_ERROR_NONE) {
10175                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10176                 goto FINISH_OFF;
10177         }
10178
10179         rc = sqlite3_changes(local_db_handle);
10180         if (rc == 0) {
10181                 EM_DEBUG_LOG("No mail found...");
10182                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10183         }
10184
10185 #ifdef __FEATURE_BODY_SEARCH__
10186         /* Delete all mail_text in mail_text_tbl table based on account id */
10187         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE account_id = %d", account_id);
10188         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10189         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10190         if (error != EMAIL_ERROR_NONE) {
10191                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10192                 goto FINISH_OFF;
10193         }
10194
10195         rc = sqlite3_changes(local_db_handle);
10196         if (rc == 0) {
10197                 EM_DEBUG_LOG("No mail found...");
10198                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10199         }
10200 #endif
10201
10202         ret = true;
10203
10204 FINISH_OFF:
10205         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10206
10207         if (error == EMAIL_ERROR_NONE) {
10208                 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_WITH_ACCOUNT, account_id, 0 , NULL, 0))
10209                         EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAIL_DELETE_ALL ]");
10210         }
10211
10212         if (err_code != NULL)
10213                 *err_code = error;
10214
10215         EM_DEBUG_FUNC_END("ret [%d]", ret);
10216         return ret;
10217 }
10218
10219 INTERNAL_FUNC int emstorage_delete_mail_by_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox, int transaction, int *err_code)
10220 {
10221         EM_DEBUG_FUNC_BEGIN("mailbox[%p], transaction[%d], err_code[%p]", mailbox, transaction, err_code);
10222
10223         if (mailbox == NULL) {
10224                 EM_DEBUG_EXCEPTION("mailbox [%p]", mailbox);
10225                 if (err_code != NULL)
10226                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10227                 return false;
10228         }
10229
10230         int ret = false;
10231         int error = EMAIL_ERROR_NONE;
10232         char sql_query_string[QUERY_SIZE] = {0, };
10233         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10234         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10235
10236         if (strcmp(mailbox->mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
10237                 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);
10238                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10239                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10240                 if (error != EMAIL_ERROR_NONE) {
10241                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10242                         goto FINISH_OFF;
10243                 }
10244
10245                 /* Delete Mails from mail_read_mail_uid_tbl */
10246                 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);
10247                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10248                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10249                 if (error != EMAIL_ERROR_NONE) {
10250                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10251                         goto FINISH_OFF;
10252                 }
10253         } else {
10254                 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);
10255                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10256                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10257                 if (error != EMAIL_ERROR_NONE) {
10258                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10259                         goto FINISH_OFF;
10260                 }
10261
10262                 /* Delete Mails from mail_read_mail_uid_tbl */
10263                 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);
10264                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10265                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10266                 if (error != EMAIL_ERROR_NONE) {
10267                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10268                         goto FINISH_OFF;
10269                 }
10270
10271 #ifdef __FEATURE_BODY_SEARCH__
10272                 /* Delete Mails from mail_text_tbl */
10273                 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);
10274                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10275                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10276                 if (error != EMAIL_ERROR_NONE) {
10277                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10278                         goto FINISH_OFF;
10279                 }
10280 #endif
10281         }
10282         ret = true;
10283
10284 FINISH_OFF:
10285         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10286
10287         if (error == EMAIL_ERROR_NONE) {
10288                 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_ALL, mailbox->account_id, mailbox->mailbox_id , mailbox->mailbox_name, 0))
10289                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventFailed [ NOTI_MAIL_DELETE_ALL ] >>>> ");
10290         }
10291
10292         if (err_code != NULL)
10293                 *err_code = error;
10294
10295         EM_DEBUG_FUNC_END("ret [%d]", ret);
10296         return ret;
10297 }
10298
10299 INTERNAL_FUNC int emstorage_free_mail(emstorage_mail_tbl_t** mail_list, int count, int *err_code)
10300 {
10301         EM_DEBUG_FUNC_BEGIN("mail_list[%p], count[%d], err_code[%p]", mail_list, count, err_code);
10302
10303         if (count > 0) {
10304                 if ((mail_list == NULL) || (*mail_list == NULL)) {
10305                         EM_DEBUG_EXCEPTION("mail_ilst[%p], count[%d]", mail_list, count);
10306
10307                         if (err_code)
10308                                 *err_code = EMAIL_ERROR_INVALID_PARAM;
10309                         return false;
10310                 }
10311
10312                 emstorage_mail_tbl_t* p = *mail_list;
10313                 int i = 0;
10314
10315                 for (; i < count; i++, p++) {
10316                         EM_SAFE_FREE(p->server_mailbox_name);
10317                         EM_SAFE_FREE(p->server_mail_id);
10318                         EM_SAFE_FREE(p->full_address_from);
10319                         EM_SAFE_FREE(p->full_address_reply);
10320                         EM_SAFE_FREE(p->full_address_to);
10321                         EM_SAFE_FREE(p->full_address_cc);
10322                         EM_SAFE_FREE(p->full_address_bcc);
10323                         EM_SAFE_FREE(p->full_address_return);
10324                         EM_SAFE_FREE(p->subject);
10325                         EM_SAFE_FREE(p->file_path_plain);
10326                         EM_SAFE_FREE(p->file_path_html);
10327                         EM_SAFE_FREE(p->file_path_mime_entity);
10328                         EM_SAFE_FREE(p->message_id);
10329                         EM_SAFE_FREE(p->email_address_sender);
10330                         EM_SAFE_FREE(p->email_address_recipient);
10331                         EM_SAFE_FREE(p->preview_text);
10332                         EM_SAFE_FREE(p->alias_sender);
10333                         EM_SAFE_FREE(p->alias_recipient);
10334                         EM_SAFE_FREE(p->default_charset);
10335                         EM_SAFE_FREE(p->pgp_password);
10336                         EM_SAFE_FREE(p->eas_data);
10337                         EM_SAFE_FREE(p->user_name);
10338                 }
10339                 EM_SAFE_FREE(*mail_list);
10340         }
10341
10342         if (err_code != NULL)
10343                 *err_code = EMAIL_ERROR_NONE;
10344
10345         EM_DEBUG_FUNC_END();
10346         return true;
10347 }
10348
10349 #ifdef __FEATURE_BODY_SEARCH__
10350 INTERNAL_FUNC void emstorage_free_mail_text(emstorage_mail_text_tbl_t** mail_text_list, int count, int *err_code)
10351 {
10352         EM_DEBUG_FUNC_BEGIN("mail_text_list[%p], count[%d], err_code[%p]", mail_text_list, count, err_code);
10353
10354         if (count > 0) {
10355                 if ((mail_text_list == NULL) || (*mail_text_list == NULL)) {
10356                         EM_DEBUG_LOG("Nothing to free: mail_text_list[%p]", mail_text_list);
10357                         return;
10358                 }
10359
10360                 emstorage_mail_text_tbl_t *p = *mail_text_list;
10361                 int i = 0;
10362
10363                 for (; i < count; i++, p++)
10364                         EM_SAFE_FREE(p->body_text);
10365
10366                 EM_SAFE_FREE(*mail_text_list);
10367         }
10368
10369         EM_DEBUG_FUNC_END();
10370 }
10371 #endif
10372
10373 INTERNAL_FUNC int emstorage_get_attachment_count(char *multi_user_name, int mail_id, int *count, int transaction, int *err_code)
10374 {
10375         EM_DEBUG_FUNC_BEGIN("mail_id[%d], count[%p], transaction[%d], err_code[%p]", mail_id, count, transaction, err_code);
10376
10377         if (mail_id <= 0 || !count) {
10378                 EM_DEBUG_EXCEPTION("mail_id[%d], count[%p]", mail_id, count);
10379                 if (err_code != NULL)
10380                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10381                 return false;
10382         }
10383
10384         int rc = -1, ret = false;
10385         int error = EMAIL_ERROR_NONE;
10386         char sql_query_string[QUERY_SIZE] = {0, };
10387
10388         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10389         EMSTORAGE_START_READ_TRANSACTION(transaction);
10390
10391         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
10392
10393         char **result;
10394
10395         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10396         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10397                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10398
10399         *count = atoi(result[1]);
10400         sqlite3_free_table(result);
10401
10402         ret = true;
10403
10404 FINISH_OFF:
10405         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10406
10407         if (err_code != NULL)
10408                 *err_code = error;
10409
10410         EM_DEBUG_FUNC_END("ret [%d]", ret);
10411         return ret;
10412 }
10413
10414 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)
10415 {
10416         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);
10417
10418         if (input_mail_id <= 0 || !output_attachment_list || !output_attachment_count) {
10419                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10420                 return EMAIL_ERROR_INVALID_PARAM;
10421         }
10422
10423         int                         error = EMAIL_ERROR_NONE;
10424         int                         i = 0;
10425         int                         rc = -1;
10426         char                      **result = NULL;
10427         char                        sql_query_string[QUERY_SIZE] = {0, };
10428         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10429         DB_STMT hStmt = NULL;
10430         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10431
10432         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
10433         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", input_mail_id);
10434         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10435         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10436                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10437
10438         *output_attachment_count = atoi(result[1]);
10439         sqlite3_free_table(result);
10440
10441         if (*output_attachment_count == 0) {
10442                 error = EMAIL_ERROR_NONE;
10443                 goto FINISH_OFF;
10444         }
10445
10446         p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * (*output_attachment_count));
10447
10448         if (!p_data_tbl) {
10449                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
10450                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10451                 goto FINISH_OFF;
10452         }
10453
10454         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE mail_id = %d ORDER BY attachment_id", input_mail_id);
10455         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
10456
10457         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10458         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)));
10459
10460         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10461         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },  ("sqlite3_step fail:%d", rc));
10462
10463         if (rc == SQLITE_DONE) {
10464                 EM_DEBUG_EXCEPTION("no matched attachment found...");
10465                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10466                 goto FINISH_OFF;
10467         }
10468         for (i = 0; i < *output_attachment_count; i++) {
10469                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10470                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10471                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10472                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10473                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10474                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10475                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10476                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10477                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10478                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10479                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10480                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10481                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10482                 EM_DEBUG_LOG("attachment[%d].attachment_id : %d", i, p_data_tbl[i].attachment_id);
10483                 EM_DEBUG_LOG("attachment_mime_type : %s", p_data_tbl[i].attachment_mime_type);
10484                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10485                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
10486         }
10487
10488 FINISH_OFF:
10489
10490         if (error == EMAIL_ERROR_NONE)
10491                 *output_attachment_list = p_data_tbl;
10492         else if (p_data_tbl != NULL)
10493                 emstorage_free_attachment(&p_data_tbl, *output_attachment_count, NULL);
10494
10495         if (hStmt) {
10496                 rc = sqlite3_finalize(hStmt);
10497                 if (rc != SQLITE_OK) {
10498                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10499                         error = EMAIL_ERROR_DB_FAILURE;
10500                         if (*output_attachment_list)
10501                                 emstorage_free_attachment(output_attachment_list, *output_attachment_count, NULL); /* prevent */
10502                 }
10503         }
10504
10505         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
10506
10507         EM_DEBUG_FUNC_END("error [%d]", error);
10508         return error;
10509 }
10510
10511 INTERNAL_FUNC int emstorage_get_attachment(char *multi_user_name, int attachment_id, emstorage_attachment_tbl_t **attachment, int transaction, int *err_code)
10512 {
10513         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], transaction[%d], err_code[%p]", attachment_id, attachment, transaction, err_code);
10514
10515         if (attachment_id <= 0 || !attachment) {
10516                 EM_DEBUG_EXCEPTION("attachment_id[%d], attachment[%p]", attachment_id, attachment);
10517                 if (err_code != NULL)
10518                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10519                 return false;
10520         }
10521
10522         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10523         int rc, ret = false;
10524         int error = EMAIL_ERROR_NONE;
10525         char sql_query_string[QUERY_SIZE] = {0, };
10526
10527         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10528         EMSTORAGE_START_READ_TRANSACTION(transaction);
10529
10530         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_id = %d",  attachment_id);
10531
10532         sqlite3_stmt* hStmt = NULL;
10533
10534         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10535         EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
10536
10537         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10538                         ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10539
10540
10541         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10542         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10543                         ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10544
10545         if (rc == SQLITE_DONE) {
10546                 EM_DEBUG_LOG("no matched attachment found...");
10547                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10548                 goto FINISH_OFF;
10549         }
10550
10551         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10552                 EM_DEBUG_EXCEPTION("malloc failed...");
10553                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10554                 goto FINISH_OFF;
10555         }
10556
10557         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10558         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10559         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10560         _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10561         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10562         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10563         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10564         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10565         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10566         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10567         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10568         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10569         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10570
10571 #ifdef __ATTACHMENT_OPTI__
10572         _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10573         _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10574 #endif
10575
10576         ret = true;
10577
10578 FINISH_OFF:
10579         if (ret == true)
10580                 *attachment = p_data_tbl;
10581
10582         if (hStmt != NULL) {
10583                 rc = sqlite3_finalize(hStmt);
10584                 if (rc != SQLITE_OK) {
10585                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10586                         error = EMAIL_ERROR_DB_FAILURE;
10587                 }
10588         }
10589
10590         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10591
10592         if (err_code != NULL)
10593                 *err_code = error;
10594
10595         EM_DEBUG_FUNC_END("ret [%d]", ret);
10596         return ret;
10597 }
10598
10599 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)
10600 {
10601         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);
10602
10603         if (mail_id <= 0 || nth <= 0 || !attachment_tbl) {
10604                 EM_DEBUG_EXCEPTION(" mail_id[%d], nth[%d], attachment[%p]", mail_id, nth, attachment_tbl);
10605
10606                 if (err_code != NULL)
10607                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10608                 return false;
10609         }
10610
10611         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10612         char *p = NULL;
10613         int rc, ret = false;
10614         int error = EMAIL_ERROR_NONE;
10615         char sql_query_string[QUERY_SIZE] = {0, };
10616
10617         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10618         EMSTORAGE_START_READ_TRANSACTION(transaction);
10619
10620         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));
10621         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10622
10623         DB_STMT hStmt = NULL;
10624
10625         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10626         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10627                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10628
10629
10630         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10631         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10632                         ("sqlite3_step fail:%d", rc));
10633
10634         if (rc == SQLITE_DONE) {
10635                 EM_DEBUG_EXCEPTION("no matched attachment found: mail_id[%d] nth[%d]", mail_id, nth);
10636                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10637                 goto FINISH_OFF;
10638         }
10639
10640         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10641                 EM_DEBUG_EXCEPTION(" malloc failed...");
10642                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10643                 goto FINISH_OFF;
10644         }
10645
10646         p_data_tbl->attachment_id = sqlite3_column_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10647         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10648                 p_data_tbl->attachment_name = cpy_str(p);
10649         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10650                 p_data_tbl->attachment_path = cpy_str(p);
10651         if ((p = (char *)sqlite3_column_text(hStmt, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10652                 p_data_tbl->content_id = cpy_str(p);
10653         p_data_tbl->attachment_size = sqlite3_column_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10654         p_data_tbl->mail_id = sqlite3_column_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10655         p_data_tbl->account_id = sqlite3_column_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10656         p_data_tbl->mailbox_id = sqlite3_column_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10657         p_data_tbl->attachment_save_status = sqlite3_column_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10658         p_data_tbl->attachment_drm_type = sqlite3_column_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10659         p_data_tbl->attachment_drm_method = sqlite3_column_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10660         p_data_tbl->attachment_inline_content_status = sqlite3_column_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10661         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10662                 p_data_tbl->attachment_mime_type = cpy_str(p);
10663 #ifdef __ATTACHMENT_OPTI__
10664         p_data_tbl->encoding = sqlite3_column_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10665         if ((p = (char *)sqlite3_column_text(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10666                 p_data_tbl->section = cpy_str(p);
10667 #endif
10668         ret = true;
10669
10670 FINISH_OFF:
10671         if (ret == true)
10672                 *attachment_tbl = p_data_tbl;
10673
10674         if (hStmt != NULL) {
10675                 rc = sqlite3_finalize(hStmt);
10676                 if (rc != SQLITE_OK) {
10677                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10678                         error = EMAIL_ERROR_DB_FAILURE;
10679                 }
10680         }
10681
10682         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10683
10684         if (err_code != NULL)
10685                 *err_code = error;
10686
10687         EM_DEBUG_FUNC_END("ret [%d]", ret);
10688         return ret;
10689 }
10690
10691 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)
10692 {
10693         EM_DEBUG_FUNC_BEGIN("attachment_path[%p], attachment[%p], transaction[%d], err_code[%p]", attachment_path, attachment, transaction, err_code);
10694
10695         if (attachment_path == NULL || !attachment) {
10696                 EM_DEBUG_EXCEPTION("attachment_path[%p], attachment[%p]", attachment_path, attachment);
10697                 if (err_code != NULL)
10698                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10699                 return false;
10700         }
10701
10702         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10703         int rc, ret = false;
10704         int error = EMAIL_ERROR_NONE;
10705         char sql_query_string[QUERY_SIZE] = {0, };
10706
10707         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10708         EMSTORAGE_START_READ_TRANSACTION(transaction);
10709
10710         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_path = '%s'", attachment_path);
10711
10712         sqlite3_stmt* hStmt = NULL;
10713
10714         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10715         EM_DEBUG_LOG_DEV("before sqlite3_prepare hStmt = %p", hStmt);
10716
10717         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10718                         ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10719
10720
10721         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10722         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10723                         ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10724
10725         if (rc == SQLITE_DONE) {
10726                 EM_DEBUG_LOG("no matched attachment found...");
10727                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10728                 goto FINISH_OFF;
10729         }
10730
10731         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10732                 EM_DEBUG_EXCEPTION("malloc failed...");
10733                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10734                 goto FINISH_OFF;
10735         }
10736
10737         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10738         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10739         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10740         _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10741         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10742         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10743         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10744         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10745         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10746         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10747         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10748         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10749         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10750
10751 #ifdef __ATTACHMENT_OPTI__
10752         _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10753         _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10754 #endif
10755
10756         ret = true;
10757
10758 FINISH_OFF:
10759         if (ret == true)
10760                 *attachment = p_data_tbl;
10761
10762         if (hStmt != NULL) {
10763                 rc = sqlite3_finalize(hStmt);
10764                 if (rc != SQLITE_OK) {
10765                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10766                         error = EMAIL_ERROR_DB_FAILURE;
10767                 }
10768         }
10769
10770         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10771
10772         if (err_code != NULL)
10773                 *err_code = error;
10774
10775         EM_DEBUG_FUNC_END("ret [%d]", ret);
10776         return ret;
10777 }
10778
10779 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)
10780 {
10781         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], attachment[%p], transaction[%d], err_code[%p]", mail_id, type, attachment, transaction, err_code);
10782
10783         if (mail_id <= 0 || !attachment) {
10784                 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], attachment[%p]", mail_id, type, attachment);
10785                 if (err_code != NULL)
10786                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10787                 return false;;
10788         }
10789
10790         int rc, ret = false;
10791         int error = EMAIL_ERROR_NONE;
10792         DB_STMT hStmt = NULL;
10793         char sql_query_string[QUERY_SIZE] = {0, };
10794
10795         int i = 0;
10796
10797         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10798
10799         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10800
10801         switch (type) {
10802         case UPDATE_MAILBOX:
10803                 EM_DEBUG_LOG("UPDATE_MAILBOX");
10804                 if (!attachment->mailbox_id) {
10805                         EM_DEBUG_EXCEPTION(" attachment->mailbox_id[%d]", attachment->mailbox_id);
10806                         error = EMAIL_ERROR_INVALID_PARAM;
10807                         goto FINISH_OFF;
10808                 }
10809                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10810                                 "UPDATE mail_attachment_tbl SET mailbox_id = ? WHERE mail_id = %d", mail_id);
10811
10812                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10813                 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10814                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10815                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10816
10817                 _bind_stmt_field_data_int(hStmt, i++, attachment->mailbox_id);
10818                 break;
10819
10820         case UPDATE_SAVENAME:
10821                 EM_DEBUG_LOG("UPDATE_SAVENAME");
10822                 if (!attachment->attachment_path) {
10823                         EM_DEBUG_EXCEPTION(" attachment->attachment_path[%p]", attachment->attachment_path);
10824                         error = EMAIL_ERROR_INVALID_PARAM;
10825                         goto FINISH_OFF;
10826                 }
10827
10828                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10829                                 "UPDATE mail_attachment_tbl SET"
10830                                 "  attachment_size = ?"
10831                                 ", attachment_save_status = ?"
10832                                 ", attachment_path = ?"
10833                                 " WHERE mail_id = %d"
10834                                 " AND attachment_id = %d"
10835                                 , attachment->mail_id
10836                                 , attachment->attachment_id);
10837
10838
10839                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10840                 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10841                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10842                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10843
10844                 _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_size);
10845                 _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_save_status);
10846                 _bind_stmt_field_data_string(hStmt, i++, (char *)attachment->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10847                 break;
10848
10849         default:
10850                 EM_DEBUG_LOG("type[%d]", type);
10851                 error = EMAIL_ERROR_INVALID_PARAM;
10852                 goto FINISH_OFF;
10853         }
10854         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10855
10856         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10857         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10858                         ("sqlite3_step fail:%d", rc));
10859         ret = true;
10860
10861 FINISH_OFF:
10862
10863         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10864
10865         if (hStmt != NULL) {
10866                 rc = sqlite3_finalize(hStmt);
10867                 if (rc != SQLITE_OK) {
10868                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10869                         error = EMAIL_ERROR_DB_FAILURE;
10870                 }
10871         }
10872
10873         if (err_code != NULL)
10874                 *err_code = error;
10875         EM_DEBUG_FUNC_END("ret [%d]", ret);
10876         return ret;
10877 }
10878
10879 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)
10880 {
10881         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);
10882
10883         int rc = 0;
10884         int ret = false;
10885         int error = EMAIL_ERROR_NONE;
10886         int field_idx = 0;
10887         int account_id = 0;
10888         char sql_query_string[QUERY_SIZE] = {0, };
10889         char *replaced_mailbox_name = NULL;
10890         char *replaced_alias = NULL;
10891         sqlite3 *local_db_handle = NULL;
10892         DB_STMT hStmt = NULL;
10893         emstorage_mailbox_tbl_t *old_mailbox_data = NULL;
10894
10895         if (input_mailbox_id <= 0 || !input_new_mailbox_name || !input_new_mailbox_alias) {
10896                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10897                 return EMAIL_ERROR_INVALID_PARAM;
10898         }
10899
10900         if (strstr(input_new_mailbox_name, "'"))
10901                 replaced_mailbox_name = em_replace_all_string(input_new_mailbox_name, "'", "''");
10902         else
10903                 replaced_mailbox_name = strdup(input_new_mailbox_name);
10904
10905         if (strstr(input_new_mailbox_alias, "'"))
10906                 replaced_alias = em_replace_all_string(input_new_mailbox_alias, "'", "''");
10907         else
10908                 replaced_alias = strdup(input_new_mailbox_alias);
10909
10910         local_db_handle = emstorage_get_db_connection(multi_user_name);
10911
10912         if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
10913                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", error);
10914                 EM_SAFE_FREE(replaced_mailbox_name);
10915                 EM_SAFE_FREE(replaced_alias);
10916                 return error;
10917         }
10918
10919         if (old_mailbox_data == NULL) {
10920                 EM_DEBUG_LOG("old_mailbox_data is NULL");
10921                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
10922                 goto FINISH_OFF;
10923         }
10924
10925         account_id = old_mailbox_data->account_id;
10926
10927         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
10928
10929         if (input_eas_data && input_eas_data_length > 0) {
10930                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10931                                 "UPDATE mail_box_tbl SET"
10932                                 " mailbox_name = ?"
10933                                 ",alias = ?"
10934                                 ",eas_data = ?"
10935                                 ",eas_data_length = ?"
10936                                 " WHERE mailbox_id = ?");
10937
10938                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10939
10940                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10941                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10942
10943                 _bind_stmt_field_data_string(hStmt, field_idx++, input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10944                 _bind_stmt_field_data_string(hStmt, field_idx++, input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10945                 _bind_stmt_field_data_blob(hStmt, field_idx++, input_eas_data, input_eas_data_length);
10946                 _bind_stmt_field_data_int(hStmt, field_idx++, input_eas_data_length);
10947                 _bind_stmt_field_data_int(hStmt, field_idx++, input_mailbox_id);
10948         } else {
10949                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10950                                 "UPDATE mail_box_tbl SET"
10951                                 " mailbox_name = ?"
10952                                 ",alias = ?"
10953                                 " WHERE mailbox_id = ?");
10954
10955                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10956
10957                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10958                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10959
10960                 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10961                 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10962                 _bind_stmt_field_data_int(hStmt, field_idx++ , input_mailbox_id);
10963         }
10964
10965
10966         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10967         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
10968                         ("sqlite3_step fail:%d", rc));
10969         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10970                         ("sqlite3_step fail:%d", rc));
10971
10972         if (sqlite3_changes(local_db_handle) == 0)
10973                 EM_DEBUG_LOG("no mail_meeting_tbl matched...");
10974
10975         ret = true;
10976
10977 FINISH_OFF:
10978         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, error);
10979         if (error == EMAIL_ERROR_NONE) {
10980                 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME, account_id, input_mailbox_id, input_new_mailbox_name, 0))
10981                         EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAILBOX_RENAME ] >>>> ");
10982         } else {
10983                 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME_FAIL, account_id, input_mailbox_id, input_new_mailbox_name, error))
10984                         EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAILBOX_RENAME_FAIL ] >>>> ");
10985         }
10986
10987         EM_SAFE_FREE(replaced_mailbox_name);
10988         EM_SAFE_FREE(replaced_alias);
10989
10990         if (old_mailbox_data)
10991                 emstorage_free_mailbox(&old_mailbox_data, 1, NULL);
10992
10993         if (hStmt != NULL) {
10994                 rc = sqlite3_finalize(hStmt);
10995                 if (rc != SQLITE_OK) {
10996                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10997                         error = EMAIL_ERROR_DB_FAILURE;
10998                 }
10999         }
11000
11001         EM_DEBUG_FUNC_END("error [%d]", error);
11002         return error;
11003 }
11004
11005 INTERNAL_FUNC int emstorage_get_new_attachment_no(char *multi_user_name, int *attachment_no, int *err_code)
11006 {
11007         EM_DEBUG_FUNC_BEGIN("attachment_no [%p], err_code[%p]", attachment_no, err_code);
11008         int rc, ret = false;
11009         int error = EMAIL_ERROR_NONE;
11010         char *sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
11011         char **result;
11012
11013         if (!attachment_no) {
11014                 EM_DEBUG_EXCEPTION("Invalid attachment");
11015                 if (err_code != NULL)
11016                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11017                 return false;
11018         }
11019
11020         *attachment_no = -1;
11021
11022         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11023
11024
11025         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
11026         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
11027                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
11028
11029         if (NULL == result[1])
11030                 rc = 1;
11031         else
11032                 rc = atoi(result[1])+1;
11033
11034         sqlite3_free_table(result);
11035
11036         *attachment_no = rc;
11037         EM_DEBUG_LOG("attachment_no [%d]", *attachment_no);
11038         ret = true;
11039
11040 FINISH_OFF:
11041
11042         if (err_code != NULL)
11043                 *err_code = error;
11044
11045         EM_DEBUG_FUNC_END("ret [%d]", ret);
11046         return ret;
11047 }
11048
11049 INTERNAL_FUNC int emstorage_add_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int iscopy, int transaction, int *err_code)
11050 {
11051         EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], iscopy[%d], transaction[%d], err_code[%p]", attachment_tbl, iscopy, transaction, err_code);
11052
11053         char *sql = NULL;
11054         char **result;
11055         int rc, ret = false;
11056         int error = EMAIL_ERROR_NONE;
11057         char sql_query_string[QUERY_SIZE] = {0, };
11058
11059         DB_STMT hStmt = NULL;
11060         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11061
11062         if (!attachment_tbl) {
11063                 EM_DEBUG_EXCEPTION("attachment_tbl[%p], iscopy[%d]", attachment_tbl, iscopy);
11064                 if (err_code != NULL)
11065                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11066                 return false;
11067         }
11068
11069         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11070
11071         sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
11072
11073         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
11074         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
11075                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
11076
11077         if (NULL == result[1]) rc = 1;
11078         else rc = atoi(result[1]) + 1;
11079         sqlite3_free_table(result);
11080
11081         attachment_tbl->attachment_id = rc;
11082
11083         SNPRINTF(sql_query_string, sizeof(sql_query_string),
11084                         "INSERT INTO mail_attachment_tbl VALUES "
11085                         "(?"    /* attachment_id */
11086                         ", ?"   /* attachment_name */
11087                         ", ?"   /* attachment_path */
11088                         ", ?"   /* content_id */
11089                         ", ?"   /* attachment_size */
11090                         ", ?"   /* mail_id */
11091                         ", ?"   /* account_id */
11092                         ", ?"   /* mailbox_id */
11093                         ", ?"   /* attachment_save_status */
11094                         ", ?"   /* attachment_drm_type */
11095                         ", ?"   /* attachment_drm_method */
11096                         ", ?"   /* attachment_inline_content_status */
11097                         ", ?"   /* attachment_mime_type */
11098 #ifdef __ATTACHMENT_OPTI__
11099                         ", ?"
11100                         ", ?"
11101 #endif
11102                         ")");
11103
11104
11105         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11106         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11107                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11108
11109         _bind_stmt_field_data_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_id);
11110         _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);
11111         _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);
11112         _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);
11113         _bind_stmt_field_data_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_size);
11114         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mail_id);
11115         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->account_id);
11116         _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mailbox_id);
11117         _bind_stmt_field_data_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_save_status);
11118         _bind_stmt_field_data_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_type);
11119         _bind_stmt_field_data_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_method);
11120         _bind_stmt_field_data_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_inline_content_status);
11121         _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);
11122 #ifdef __ATTACHMENT_OPTI__
11123         _bind_stmt_field_data_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->encoding);
11124         _bind_stmt_field_data_string(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->section, 0, ATTACHMENT_LEN_IN_MAIL_ATTACHMENT_TBL);
11125 #endif
11126
11127
11128         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11129         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11130                         ("sqlite3_step fail:%d", rc));
11131         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11132                         ("sqlite3_step fail:%d", rc));
11133
11134         rc = sqlite3_changes(local_db_handle);
11135         if (rc == 0) {
11136                 EM_DEBUG_LOG(" no matched mail found...");
11137                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11138                 goto FINISH_OFF;
11139         }
11140
11141         ret = true;
11142
11143 FINISH_OFF:
11144         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11145
11146         if (hStmt != NULL) {
11147                 rc = sqlite3_finalize(hStmt);
11148                 if (rc != SQLITE_OK) {
11149                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11150                         error = EMAIL_ERROR_DB_FAILURE;
11151                 }
11152         }
11153
11154         if (err_code != NULL)
11155                 *err_code = error;
11156
11157         EM_DEBUG_FUNC_END("ret [%d]", ret);
11158         return ret;
11159 }
11160
11161 INTERNAL_FUNC int emstorage_update_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int transaction, int *err_code)
11162 {
11163         EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], transaction[%d], err_code[%p]", attachment_tbl, transaction, err_code);
11164
11165         int rc, ret = false, field_idx = 0;
11166         int error = EMAIL_ERROR_NONE;
11167         DB_STMT hStmt = NULL;
11168         char sql_query_string[QUERY_SIZE] = {0, };
11169
11170         if (!attachment_tbl) {
11171                 EM_DEBUG_EXCEPTION(" attachment_tbl[%p] ", attachment_tbl);
11172                 if (err_code)
11173                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11174                 return false;
11175         }
11176
11177         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11178
11179         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11180
11181         SNPRINTF(sql_query_string, sizeof(sql_query_string),
11182                         "UPDATE mail_attachment_tbl SET  "
11183                         "  attachment_name = ?"
11184                         ", attachment_path =  ?"
11185                         ", content_id = ?"
11186                         ", attachment_size = ?"
11187                         ", mail_id = ?"
11188                         ", account_id = ?"
11189                         ", mailbox_id = ?"
11190                         ", attachment_save_status = ?"
11191                         ", attachment_drm_type = ?"
11192                         ", attachment_drm_method = ?"
11193                         ", attachment_inline_content_status = ? "
11194                         ", attachment_mime_type = ? "
11195                         " WHERE attachment_id = ?;");
11196
11197
11198         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11199
11200         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11201                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11202
11203         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
11204         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
11205         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->content_id, 0, CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL);
11206         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_size);
11207         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->mail_id);
11208         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->account_id);
11209         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->mailbox_id);
11210         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_save_status);
11211         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_drm_type);
11212         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_drm_method);
11213         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_inline_content_status);
11214         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_mime_type, 0, ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL);
11215         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_id);
11216
11217
11218         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11219         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11220                         ("sqlite3_step fail:%d", rc));
11221         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11222                         ("sqlite3_step fail:%d", rc));
11223
11224         rc = sqlite3_changes(local_db_handle);
11225         if (rc == 0) {
11226                 EM_DEBUG_LOG(" no matched mail found...");
11227                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11228                 goto FINISH_OFF;
11229         }
11230
11231         ret = true;
11232
11233 FINISH_OFF:
11234         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11235         if (hStmt != NULL) {
11236                 rc = sqlite3_finalize(hStmt);
11237                 if (rc != SQLITE_OK) {
11238                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11239                         error = EMAIL_ERROR_DB_FAILURE;
11240                 }
11241         }
11242
11243         if (err_code != NULL)
11244                 *err_code = error;
11245
11246         EM_DEBUG_FUNC_END("ret [%d]", ret);
11247         return ret;
11248 }
11249
11250 INTERNAL_FUNC int emstorage_delete_attachment_on_db(char *multi_user_name, int attachment_id, int transaction, int *err_code)
11251 {
11252         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], transaction[%d], err_code[%p]", attachment_id, transaction, err_code);
11253
11254         if (attachment_id < 0) {
11255                 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
11256                 if (err_code != NULL)
11257                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11258                 return false;
11259         }
11260
11261         int ret = false;
11262         int error = EMAIL_ERROR_NONE;
11263         char sql_query_string[QUERY_SIZE] = {0, };
11264         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11265
11266         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11267
11268         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
11269
11270         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11271         if (error != EMAIL_ERROR_NONE) {
11272                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11273                 goto FINISH_OFF;
11274         }
11275
11276         ret = true;
11277
11278 FINISH_OFF:
11279         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11280
11281         if (err_code)
11282                 *err_code = error;
11283
11284         EM_DEBUG_FUNC_END("ret [%d]", ret);
11285         return ret;
11286 }
11287
11288 INTERNAL_FUNC int emstorage_delete_all_attachments_of_mail(char *multi_user_name, int mail_id, int transaction, int *err_code)
11289 {
11290         EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
11291         int ret = false;
11292         int error = EMAIL_ERROR_NONE;
11293         char sql_query_string[QUERY_SIZE] = {0, };
11294         sqlite3 *local_db_handle = NULL;
11295
11296         if (mail_id <= 0) {
11297                 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
11298                 if (err_code != NULL)
11299                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11300                 return false;
11301         }
11302
11303         local_db_handle = emstorage_get_db_connection(multi_user_name);
11304
11305         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11306
11307         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
11308         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11309         if (error != EMAIL_ERROR_NONE) {
11310                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11311                 goto FINISH_OFF;
11312         }
11313
11314         ret = true;
11315
11316 FINISH_OFF:
11317         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11318
11319         if (err_code)
11320                 *err_code = error;
11321
11322         EM_DEBUG_FUNC_END("ret [%d]", ret);
11323         return ret;
11324 }
11325
11326 INTERNAL_FUNC int emstorage_delete_attachment_all_on_db(char *multi_user_name, int account_id, char *mailbox, int transaction, int *err_code)
11327 {
11328         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox, transaction, err_code);
11329
11330         int error = EMAIL_ERROR_NONE;
11331         int ret = false;
11332         char sql_query_string[QUERY_SIZE] = {0, };
11333         char *replaced_mailbox = NULL;
11334         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11335
11336         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11337
11338         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl");
11339
11340         if (account_id != ALL_ACCOUNT) /*  '0' means all account */
11341                 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);
11342
11343         if (mailbox) /*  NULL means all mailbox_name */ {
11344                 if (strstr(mailbox, "'"))
11345                         replaced_mailbox = em_replace_all_string(mailbox, "'", "''");
11346                 else
11347                         replaced_mailbox = strdup(mailbox);
11348
11349                 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);
11350                 EM_SAFE_FREE(replaced_mailbox); /*prevent 49434*/
11351         }
11352
11353         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11354         if (error != EMAIL_ERROR_NONE) {
11355                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11356                 goto FINISH_OFF;
11357         }
11358
11359         ret = true;
11360
11361 FINISH_OFF:
11362         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11363
11364         if (err_code != NULL)
11365                 *err_code = error;
11366
11367         EM_DEBUG_FUNC_END("ret [%d]", ret);
11368         return ret;
11369 }
11370
11371 INTERNAL_FUNC int emstorage_free_attachment(emstorage_attachment_tbl_t** attachment_tbl_list, int count, int *err_code)
11372 {
11373         EM_DEBUG_FUNC_BEGIN("attachment_tbl_list[%p], count[%d], err_code[%p]", attachment_tbl_list, count, err_code);
11374
11375         if (count > 0) {
11376                 if ((attachment_tbl_list == NULL) || (*attachment_tbl_list == NULL)) {
11377                         EM_DEBUG_LOG("Nothing to free: attachment_tbl_list[%p], count[%d]", attachment_tbl_list, count);
11378                         if (err_code != NULL)
11379                                 *err_code = EMAIL_ERROR_INVALID_PARAM;
11380                         return false;
11381                 }
11382
11383                 emstorage_attachment_tbl_t* p = *attachment_tbl_list;
11384                 int i;
11385
11386                 for (i = 0; i < count; i++) {
11387                         EM_SAFE_FREE(p[i].attachment_name);
11388                         EM_SAFE_FREE(p[i].attachment_path);
11389                         EM_SAFE_FREE(p[i].content_id);
11390                         EM_SAFE_FREE(p[i].attachment_mime_type);
11391 #ifdef __ATTACHMENT_OPTI__
11392                         EM_SAFE_FREE(p[i].section);
11393 #endif
11394                 }
11395
11396                 EM_SAFE_FREE(p);
11397                 *attachment_tbl_list = NULL;
11398         }
11399
11400         if (err_code != NULL)
11401                 *err_code = EMAIL_ERROR_NONE;
11402         EM_DEBUG_FUNC_END();
11403         return true;
11404 }
11405
11406 INTERNAL_FUNC int emstorage_begin_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11407 {
11408         EM_DEBUG_FUNC_BEGIN();
11409         EM_PROFILE_BEGIN(emStorageBeginTransaction);
11410         int ret = true;
11411
11412         _timedlock_shm_mutex(mapped_for_db_lock, 2);
11413
11414         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11415
11416         int rc;
11417         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN immediate;", NULL, NULL, NULL), rc);
11418         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11419                         ("SQL(BEGIN) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11420
11421         if (ret == false) {
11422                 if (err_code != NULL) *err_code = EMAIL_ERROR_DB_FAILURE;
11423         }
11424
11425         EM_PROFILE_END(emStorageBeginTransaction);
11426         EM_DEBUG_FUNC_END("ret [%d]", ret);
11427         return ret;
11428 }
11429
11430 INTERNAL_FUNC int emstorage_commit_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11431 {
11432         EM_DEBUG_FUNC_BEGIN();
11433         int ret = true;
11434         int rc;
11435
11436         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11437
11438         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
11439         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; }, ("SQL(END) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11440
11441         if (ret == false && err_code != NULL)
11442                 *err_code = EMAIL_ERROR_DB_FAILURE;
11443
11444         _unlockshm_mutex(mapped_for_db_lock);
11445
11446         EM_DEBUG_FUNC_END("ret [%d]", ret);
11447         return ret;
11448 }
11449
11450 INTERNAL_FUNC int emstorage_rollback_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11451 {
11452         EM_DEBUG_FUNC_BEGIN();
11453         int ret = true;
11454         int rc;
11455         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11456
11457         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "ROLLBACK;", NULL, NULL, NULL), rc);
11458         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11459                         ("SQL(ROLLBACK) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11460
11461         if (ret == false && err_code != NULL)
11462                 *err_code = EMAIL_ERROR_DB_FAILURE;
11463
11464         _unlockshm_mutex(mapped_for_db_lock);
11465
11466         EM_DEBUG_FUNC_END("ret [%d]", ret);
11467         return ret;
11468 }
11469
11470 INTERNAL_FUNC int emstorage_is_mailbox_full(char *multi_user_name, int account_id, email_mailbox_t *mailbox, int *result, int *err_code)
11471 {
11472         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], result[%p], err_code[%p]", account_id, mailbox, result, err_code);
11473
11474         if (account_id < FIRST_ACCOUNT_ID || !mailbox || !result) {
11475                 if (mailbox)
11476                         EM_DEBUG_EXCEPTION("Invalid Parameter. accoun_id[%d], mailbox[%p]", account_id, mailbox);
11477
11478                 if (err_code != NULL)
11479                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11480
11481                 return false;
11482         }
11483
11484         int ret = false;
11485         int error = EMAIL_ERROR_NONE;
11486         int mail_count = 0;
11487
11488         if (!emstorage_get_mail_count(multi_user_name, account_id, mailbox->mailbox_id, &mail_count, NULL, true, &error)) {
11489                 EM_DEBUG_EXCEPTION("emstorage_get_mail_count failed [%d]", error);
11490                 goto FINISH_OFF;
11491         }
11492
11493         if (mailbox) {
11494                 EM_DEBUG_LOG("mail_count[%d] mail_slot_size[%d]", mail_count, mailbox->mail_slot_size);
11495                 if (mail_count >= mailbox->mail_slot_size)
11496                         *result = true;
11497                 else
11498                         *result = false;
11499
11500                 ret = true;
11501         }
11502
11503         ret = true;
11504 FINISH_OFF:
11505
11506         if (err_code != NULL)
11507                 *err_code = error;
11508
11509         EM_DEBUG_FUNC_END("ret [%d]", ret);
11510         return ret;
11511 }
11512
11513 INTERNAL_FUNC int emstorage_clear_mail_data(char *multi_user_name, int transaction, int *err_code)
11514 {
11515         EM_DEBUG_FUNC_BEGIN("transaction[%d], err_code[%p]", transaction, err_code);
11516
11517         int ret = false;
11518         int error = EMAIL_ERROR_NONE;
11519         char sql_query_string[QUERY_SIZE] = {0, };
11520         const email_db_object_t* tables = _g_db_tables;
11521         const email_db_object_t* indexes = _g_db_indexes;
11522
11523         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11524         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11525
11526         if (!emstorage_delete_dir((char *)MAIL_HOME, &error)) {
11527                 EM_DEBUG_EXCEPTION(" emstorage_delete_dir failed - %d", error);
11528
11529                 goto FINISH_OFF;
11530         }
11531
11532         if (mkdir(MAIL_HOME, DIRECTORY_PERMISSION) != 0)
11533                 EM_DEBUG_EXCEPTION("Fail to create the directory \n");
11534         if (mkdir(MAIL_TEMP, DIRECTORY_PERMISSION) != 0)
11535                 EM_DEBUG_EXCEPTION("Fail to create the directory \n");
11536         if (chmod(MAIL_TEMP, 0777) != 0)
11537                 EM_DEBUG_EXCEPTION("Fail to change the permission \n");
11538
11539         /*  first clear index. */
11540         while (indexes->object_name) {
11541                 if (indexes->data_flag) {
11542                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP index %s", indexes->object_name);
11543                         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11544                         if (error != EMAIL_ERROR_NONE) {
11545                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11546                                 goto FINISH_OFF;
11547                         }
11548                 }
11549                 indexes++;
11550         }
11551
11552         while (tables->object_name) {
11553                 if (tables->data_flag) {
11554                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP table %s", tables->object_name);
11555                         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11556                         if (error != EMAIL_ERROR_NONE) {
11557                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11558                                 goto FINISH_OFF;
11559                         }
11560                 }
11561
11562                 tables++;
11563         }
11564         ret = true;
11565
11566 FINISH_OFF:
11567         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11568
11569         if (err_code != NULL)
11570                 *err_code = error;
11571
11572         EM_DEBUG_FUNC_END("ret [%d]", ret);
11573         return ret;
11574 }
11575 /*======================= DB File Utils =============================================*/
11576 #include <dirent.h>
11577 #include <sys/types.h>
11578 #define  DIR_SEPERATOR "/"
11579
11580 INTERNAL_FUNC char *emstorage_make_directory_path_from_file_path(char *file_name)
11581 {
11582         EM_DEBUG_FUNC_BEGIN("Filename [ %p ]", file_name);
11583         char delims[] = "/";
11584         char *result = NULL;
11585         gchar **token = NULL;
11586
11587         token = g_strsplit_set(file_name, delims, 1);
11588
11589         if (token && token[0]) {
11590                 EM_DEBUG_LOG_SEC(">>>> Directory_name [ %s ]", token[0]);
11591                 result = EM_SAFE_STRDUP(token[0]);
11592         } else
11593                 EM_DEBUG_LOG(">>>> No Need to create Directory");
11594
11595         g_strfreev(token);
11596
11597         return result;
11598 }
11599
11600 INTERNAL_FUNC int emstorage_get_save_name(char *multi_user_name, int account_id, int mail_id, int atch_id,
11601                 char *fname, char *move_buf, char *path_buf, int maxlen, int *err_code)
11602 {
11603         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);
11604         EM_PROFILE_BEGIN(profile_emstorage_get_save_name);
11605
11606         int ret = false;
11607         int error = EMAIL_ERROR_NONE;
11608         char *dir_name = NULL;
11609         char create_dir[1024] = {0};
11610         char *temp_file = NULL;
11611         char *prefix_path = NULL;
11612         char *modified_fname = NULL;
11613
11614         if (!move_buf || !path_buf || account_id < FIRST_ACCOUNT_ID || mail_id < 0 || atch_id < 0) {
11615                 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);
11616                 error = EMAIL_ERROR_INVALID_PARAM;
11617                 goto FINISH_OFF;
11618         }
11619
11620         snprintf(path_buf, 512, "%s", MAIL_HOME);
11621         snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, account_id);
11622
11623         if (mail_id > 0)
11624                 snprintf(path_buf+EM_SAFE_STRLEN(path_buf),   512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, mail_id);
11625
11626         if (atch_id > 0)
11627                 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, atch_id);
11628
11629         if (fname) {
11630                 temp_file = EM_SAFE_STRDUP(fname);
11631                 if (temp_file && strstr(temp_file, "/"))
11632                         dir_name = emstorage_make_directory_path_from_file_path(temp_file);
11633         }
11634
11635         if (dir_name) {
11636                 snprintf(create_dir, sizeof(create_dir), "%s%s%s", path_buf, DIR_SEPERATOR, dir_name);
11637                 EM_DEBUG_LOG(">>>>> DIR PATH [%s]", create_dir);
11638                 ret = mkdir(create_dir, DIRECTORY_PERMISSION);
11639                 EM_DEBUG_LOG("mkdir result = [%d]", ret);
11640                 EM_SAFE_FREE(dir_name);
11641         }
11642
11643         if (fname) {
11644                 EM_DEBUG_LOG_DEV(">>>>> fname [%s]", fname);
11645
11646                 /* Did not allow the slash */
11647                 modified_fname = reg_replace_new(fname, "/", "_");
11648                 EM_DEBUG_LOG_SEC("modified_fname : [%s]", modified_fname);
11649
11650                 if (modified_fname == NULL) modified_fname = g_strdup(fname);
11651
11652                 if (EM_SAFE_STRLEN(modified_fname) + EM_SAFE_STRLEN(path_buf) + strlen(DIR_SEPERATOR) > maxlen - 1) {
11653                         char *modified_name = NULL;
11654                         int remain_len  = (maxlen - 1) - EM_SAFE_STRLEN(path_buf) - strlen(DIR_SEPERATOR);
11655
11656                         if (remain_len <= 0) {
11657                                 error = EMAIL_ERROR_MAX_EXCEEDED;
11658                                 goto FINISH_OFF;
11659                         }
11660
11661                         if (remain_len > MAX_FILENAME)
11662                                 remain_len = MAX_FILENAME;
11663
11664                         modified_name = em_shrink_filename(modified_fname, remain_len);
11665
11666                         if (!modified_name) {
11667                                 error = EMAIL_ERROR_MAX_EXCEEDED;
11668                                 goto FINISH_OFF;
11669                         }
11670
11671                         snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),
11672                                 "%s%s", DIR_SEPERATOR, modified_name);
11673                         EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11674                         EM_SAFE_FREE(modified_name);
11675                 } else {
11676                         if (EM_SAFE_STRLEN(modified_fname) > MAX_FILENAME - 1) {
11677                                 char *modified_name = NULL;
11678
11679                                 modified_name = em_shrink_filename(modified_fname, MAX_FILENAME);
11680                                 if (!modified_name) {
11681                                         error = EMAIL_ERROR_MAX_EXCEEDED;
11682                                         goto FINISH_OFF;
11683                                 }
11684
11685                                 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),
11686                                         "%s%s", DIR_SEPERATOR, modified_name);
11687                                 EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11688                                 EM_SAFE_FREE(modified_name);
11689                         } else {
11690                                 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),
11691                                         "%s%s", DIR_SEPERATOR, modified_fname);
11692                         }
11693                 }
11694         }
11695
11696         EM_DEBUG_LOG_SEC(">>>>> path_buf [%s]", path_buf);
11697
11698         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11699                 error = emcore_get_container_path(multi_user_name, &prefix_path);
11700                 if (error != EMAIL_ERROR_NONE) {
11701                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11702                         goto FINISH_OFF;
11703                 }
11704                 snprintf(move_buf, 512, "%s/%s", prefix_path, path_buf);
11705                 EM_DEBUG_LOG_SEC("move_buf : [%s]", move_buf);
11706         } else {
11707                 snprintf(move_buf, 512, "%s", path_buf);
11708                 EM_DEBUG_LOG_SEC("move_buf : [%s]", move_buf);
11709         }
11710
11711         ret = true;
11712
11713 FINISH_OFF:
11714
11715         EM_SAFE_FREE(temp_file);
11716         EM_SAFE_FREE(prefix_path);
11717         EM_SAFE_FREE(modified_fname);
11718
11719         if (err_code != NULL)
11720                 *err_code = error;
11721
11722         EM_PROFILE_END(profile_emstorage_get_save_name);
11723         EM_DEBUG_FUNC_END("ret [%d]", ret);
11724         return ret;
11725 }
11726
11727 /*
11728    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)
11729    {
11730    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);
11731
11732    if (!name_buf || account_id < FIRST_ACCOUNT_ID) {
11733    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);
11734    if (err_code != NULL)
11735  *err_code = EMAIL_ERROR_INVALID_PARAM;
11736  return false;
11737  }
11738
11739  sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
11740
11741  if (mail_id > 0)
11742  sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, mail_id);
11743  else
11744  goto FINISH_OFF;
11745
11746  if (atch_id > 0)
11747  sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, atch_id);
11748  else
11749  goto FINISH_OFF;
11750
11751 FINISH_OFF:
11752 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), ".DELE");
11753
11754 EM_DEBUG_FUNC_END();
11755 return true;
11756 }
11757 */
11758
11759 INTERNAL_FUNC int emstorage_create_dir(char *multi_user_name, int account_id, int mail_id, int atch_id, int *err_code)
11760 {
11761         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], err_code[%p]", account_id, mail_id, atch_id, err_code);
11762         EM_PROFILE_BEGIN(profile_emcore_save_create_dir);
11763         int ret = false;
11764         int error = EMAIL_ERROR_NONE;
11765         char buf[512];
11766         struct stat sbuf;
11767         char *prefix_path = NULL;
11768         char errno_buf[ERRNO_BUF_SIZE] = {0};
11769
11770         memset(buf, 0x00, sizeof(buf));
11771
11772         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11773                 error = emcore_get_container_path(multi_user_name, &prefix_path);
11774                 if (error != EMAIL_ERROR_NONE) {
11775                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11776                         goto FINISH_OFF;
11777                 }
11778         } else {
11779                 prefix_path = strdup("");
11780         }
11781
11782         if (account_id >= FIRST_ACCOUNT_ID) {
11783                 SNPRINTF(buf, sizeof(buf), "%s%s%s%s%d", prefix_path,
11784                                 DIR_SEPERATOR,
11785                                 MAIL_HOME,
11786                                 DIR_SEPERATOR,
11787                                 account_id);
11788
11789                 if (stat(buf, &sbuf) == 0) {
11790                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11791                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11792                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11793                                 goto FINISH_OFF;
11794                         }
11795                 } else {
11796                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11797                                 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11798                                 EM_DEBUG_EXCEPTION("mkdir failed: %s", EM_STRERROR(errno_buf));
11799                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11800                                 if (errno == 28)
11801                                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11802                                 goto FINISH_OFF;
11803                         }
11804
11805                         if (account_id == EML_FOLDER)
11806                                 chmod(buf, 0777);
11807                 }
11808         }
11809
11810         if (mail_id > 0) {
11811                 int space_left_in_buffer = sizeof(buf) - EM_SAFE_STRLEN(buf);
11812
11813                 if (account_id < FIRST_ACCOUNT_ID) {
11814                         EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11815                         error = EMAIL_ERROR_INVALID_PARAM;
11816                         goto FINISH_OFF;
11817                 }
11818
11819                 if (space_left_in_buffer + 10 > sizeof(buf)) {
11820                         EM_DEBUG_EXCEPTION("Buffer overflowed");
11821                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11822                         goto FINISH_OFF;
11823                 }
11824
11825                 SNPRINTF(buf+EM_SAFE_STRLEN(buf), space_left_in_buffer, "%s%d", DIR_SEPERATOR, mail_id);
11826
11827                 if (stat(buf, &sbuf) == 0) {
11828                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11829                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11830                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11831                                 goto FINISH_OFF;
11832                         }
11833                 } else {
11834                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11835                                 EM_DEBUG_EXCEPTION("mkdir failed [%s]", buf);
11836                                 EM_DEBUG_EXCEPTION("mkdir failed [%d][%s]", errno, EM_STRERROR(errno_buf));
11837                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11838                                 if (errno == 28)
11839                                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11840                                 goto FINISH_OFF;
11841                         }
11842
11843                         if (account_id == EML_FOLDER)
11844                                 chmod(buf, 0777);
11845                 }
11846         }
11847
11848         if (atch_id > 0) {
11849                 if (account_id < FIRST_ACCOUNT_ID || mail_id <= 0) {
11850                         EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11851                         error = EMAIL_ERROR_INVALID_PARAM;
11852                         goto FINISH_OFF;
11853                 }
11854
11855                 SNPRINTF(buf+EM_SAFE_STRLEN(buf), sizeof(buf)-(EM_SAFE_STRLEN(buf)+1), "%s%d", DIR_SEPERATOR, atch_id);
11856
11857                 if (stat(buf, &sbuf) == 0) {
11858                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11859                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11860                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11861                                 goto FINISH_OFF;
11862                         }
11863                 } else {
11864                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11865                                 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11866                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11867                                 if (errno == 28)
11868                                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11869                                 goto FINISH_OFF;
11870                         }
11871
11872                         if (account_id == EML_FOLDER)
11873                                 if (chmod(buf, 0777) != 0)
11874                                         EM_DEBUG_EXCEPTION("Fail to change the permission \n");
11875                 }
11876         }
11877
11878         ret = true;
11879
11880 FINISH_OFF:
11881
11882         EM_SAFE_FREE(prefix_path);
11883
11884         if (err_code != NULL)
11885                 *err_code = error;
11886
11887         EM_PROFILE_END(profile_emcore_save_create_dir);
11888         EM_DEBUG_FUNC_END("ret [%d]", ret);
11889         return ret;
11890 }
11891
11892 INTERNAL_FUNC int emstorage_copy_file(char *src_file, char *dst_file, int sync_status, int *err_code)
11893 {
11894         EM_DEBUG_FUNC_BEGIN_SEC("src_file[%s], dst_file[%s], err_code[%p]", src_file, dst_file, err_code);
11895         EM_DEBUG_LOG("Using the fsync function");
11896         int ret = false;
11897         int error = EMAIL_ERROR_NONE;
11898         struct stat st_buf;
11899
11900         int fp_src = 0;
11901         int fp_dst = 0;
11902         int nread = 0;
11903         char buf[FILE_MAX_BUFFER_SIZE] = {0};
11904         char errno_buf[ERRNO_BUF_SIZE] = {0};
11905
11906         if (!src_file || !dst_file) {
11907                 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
11908
11909                 error = EMAIL_ERROR_INVALID_PARAM;
11910                 goto FINISH_OFF;
11911         }
11912
11913         if (stat(src_file, &st_buf) < 0) {
11914                 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", src_file);
11915
11916                 error = EMAIL_ERROR_SYSTEM_FAILURE;             /* EMAIL_ERROR_INVALID_PATH; */
11917                 goto FINISH_OFF;
11918         }
11919
11920         error = em_open(src_file, O_RDONLY, 0, &fp_src);
11921         if (error != EMAIL_ERROR_NONE) {
11922                 EM_DEBUG_EXCEPTION(">>>> Source Fail em_open %s Failed: %d", src_file, error);
11923                 goto FINISH_OFF;
11924         }
11925
11926         error = em_open(dst_file, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, &fp_dst); /*prevent 24474*/
11927         if (error != EMAIL_ERROR_NONE) {
11928                 EM_DEBUG_EXCEPTION(">>>> Destination Fail em_open %s:  %d", dst_file, error);
11929                 goto FINISH_OFF;
11930         }
11931
11932         memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
11933
11934         while ((nread = read(fp_src, buf, FILE_MAX_BUFFER_SIZE)) > 0) {
11935                 if (nread > 0 && nread <= FILE_MAX_BUFFER_SIZE) {
11936                         EM_DEBUG_LOG("Nread Value [%d]", nread);
11937                         char *buf_ptr;
11938                         ssize_t byte_written = 0;
11939                         size_t remain_byte = nread;
11940                         buf_ptr = buf;
11941                         errno = 0;
11942
11943                         while (remain_byte > 0 && buf_ptr && errno == 0) {
11944                                 byte_written = write(fp_dst, buf_ptr, remain_byte);
11945
11946                                 if (byte_written < 0) {
11947                                         /* interrupted by a signal */
11948                                         if (errno == EINTR) {
11949                                                 errno = 0;
11950                                                 continue;
11951                                         }
11952
11953                                         EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
11954                                         error = EMAIL_ERROR_UNKNOWN;
11955                                         goto FINISH_OFF;
11956                                 }
11957                                 EM_DEBUG_LOG("NWRITTEN [%zu]", byte_written);
11958                                 remain_byte -= byte_written;
11959                                 buf_ptr += byte_written;
11960                         }
11961                 }
11962
11963                 memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
11964         }
11965
11966         ret = true;
11967
11968 FINISH_OFF:
11969         EM_SAFE_CLOSE(fp_src);
11970
11971         if (fp_dst >= 0) { /*prevent 24474*/
11972                 if (sync_status) {
11973                         EM_DEBUG_LOG("Before fsync");
11974                         fsync(fp_dst);
11975                 }
11976                 close(fp_dst);
11977         }
11978
11979         if (nread < 0 || error == EMAIL_ERROR_UNKNOWN)
11980                 remove(dst_file);
11981
11982         if (err_code != NULL)
11983                 *err_code = error;
11984         EM_DEBUG_FUNC_END("ret [%d]", ret);
11985         return ret;
11986 }
11987 /* create Directory if user has deleted [deepam.p@samsung.com] */
11988 INTERNAL_FUNC void emstorage_create_dir_if_delete()
11989 {
11990         EM_DEBUG_FUNC_BEGIN();
11991
11992         if (mkdir(DATA_PATH, DIRECTORY_PERMISSION) != 0)
11993                 EM_DEBUG_EXCEPTION("Fail to create the directory \n");
11994         if (mkdir(EMAIL_PATH, DIRECTORY_PERMISSION) != 0)
11995                 EM_DEBUG_EXCEPTION("Fail to create the directory \n");
11996         if (mkdir(MAIL_HOME, DIRECTORY_PERMISSION) != 0)
11997                 EM_DEBUG_EXCEPTION("Fail to create the directory \n");
11998         if (mkdir(MAIL_TEMP, DIRECTORY_PERMISSION) != 0)
11999                 EM_DEBUG_EXCEPTION("Fail to create the directory \n");
12000         if (chmod(MAIL_TEMP, 0777) != 0)
12001                 EM_DEBUG_EXCEPTION("Fail to change the permission \n");
12002
12003         EM_DEBUG_FUNC_END();
12004 }
12005
12006 static int _get_temp_file_name(char **filename, int *err_code)
12007 {
12008         EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
12009
12010         int ret = false;
12011         int error = EMAIL_ERROR_NONE;
12012
12013         if (filename == NULL) {
12014                 EM_DEBUG_EXCEPTION(" filename[%p]", filename);
12015                 error = EMAIL_ERROR_INVALID_PARAM;
12016                 goto FINISH_OFF;
12017         }
12018
12019         char tempname[512] = {0x00, };
12020         struct timeval tv;
12021
12022         gettimeofday(&tv, NULL);
12023         srand(tv.tv_usec);
12024         unsigned int seed = time(NULL);
12025         SNPRINTF(tempname, sizeof(tempname), "%s%c%d", MAIL_TEMP, '/', rand_r(&seed));
12026
12027         char *p = EM_SAFE_STRDUP(tempname);
12028         if (p == NULL) {
12029                 EM_DEBUG_EXCEPTION(" strdup failed...");
12030                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12031                 goto FINISH_OFF;
12032         }
12033
12034         *filename = p;
12035
12036         ret = true;
12037
12038 FINISH_OFF:
12039         if (err_code != NULL)
12040                 *err_code = error;
12041
12042         EM_DEBUG_FUNC_END("ret [%d]", ret);
12043         return ret;
12044 }
12045
12046 INTERNAL_FUNC int emstorage_add_content_type(char *file_path, char *char_set, int *err_code)
12047 {
12048         EM_DEBUG_FUNC_BEGIN("File path [ %p ]  Character Set [ %p ] err_code [ %p]", file_path, char_set, err_code);
12049
12050         EM_IF_NULL_RETURN_VALUE(file_path, false);
12051         EM_IF_NULL_RETURN_VALUE(char_set, false);
12052         EM_IF_NULL_RETURN_VALUE(err_code, false);
12053
12054         char *buf =  NULL;
12055         char *buf1 = NULL;
12056         struct stat st_buf;
12057         int buf_size = 0;
12058         char *low_char_set = NULL;
12059         char *match_str = NULL;
12060         int nwritten = 0;
12061         int ret = false;
12062         int error = EMAIL_ERROR_NONE;
12063         int data_count_to_written = 0;
12064         char *temp_file_name = NULL;
12065         int err = 0;
12066         FILE* fp_src = NULL;
12067         FILE* fp_dest = NULL;
12068         int nread = 0;
12069
12070
12071         if (stat(file_path, &st_buf) < 0) {
12072                 EM_DEBUG_EXCEPTION_SEC(" stat(\"%s\") failed...", file_path);
12073                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12074                 goto FINISH_OFF;
12075         }
12076
12077         buf_size =  st_buf.st_size;
12078         EM_DEBUG_LOG(">>>> File Size [ %d ] ", buf_size);
12079         buf = (char *)calloc(1, buf_size+1);
12080
12081         if (!buf) {
12082                 EM_DEBUG_LOG(">>> Memory cannot be allocated ");
12083                 goto FINISH_OFF;
12084         }
12085
12086         error = em_fopen(file_path, "rb", &fp_src);
12087         if (error != EMAIL_ERROR_NONE || fp_src == NULL) {
12088                 EM_DEBUG_EXCEPTION_SEC(" file_path fopen failed - %s [%d]", file_path, error);
12089                 goto FINISH_OFF;
12090         }
12091
12092         if ((nread = fread(buf, 1, buf_size, fp_src)) > 0) {
12093                 if (nread > 0 && nread <= buf_size) {
12094                         EM_DEBUG_LOG(">>>> Nread Value [ %d ] ", nread);
12095
12096                         /**
12097                          *   1.Add check for whether content type is there.
12098                          *   2. If not based on the character set, Append it in File
12099                          **/
12100
12101                         low_char_set = calloc(1, EM_SAFE_STRLEN(char_set) + strlen(" \" /></head>") +1); /*prevent 34359*/
12102                         if (low_char_set == NULL) {
12103                                 EM_DEBUG_EXCEPTION("calloc failed");
12104                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12105                                 goto FINISH_OFF;
12106                         }
12107
12108                         strncat(low_char_set, char_set, sizeof(low_char_set)-EM_SAFE_STRLEN(low_char_set)-1);
12109                         EM_DEBUG_LOG(">>>> CHAR SET [ %s ] ", low_char_set);
12110                         strncat(low_char_set, " \" /></head>", sizeof(low_char_set)-EM_SAFE_STRLEN(low_char_set)-1); /*prevent 34359*/
12111                         EM_DEBUG_LOG(">>> CHARSET [ %s ] ", low_char_set);
12112                         buf[nread] = '\0';
12113
12114                         match_str = strstr(buf, CONTENT_TYPE_DATA);
12115
12116                         if (match_str == NULL) {
12117                                 EM_DEBUG_LOG(">>>>emstorage_add_content_type 3 ");
12118                                 if (fp_src != NULL) {
12119                                         fclose(fp_src);
12120                                         fp_src = NULL;
12121                                 }
12122                                 data_count_to_written = EM_SAFE_STRLEN(low_char_set)+strlen(CONTENT_DATA)+1; /*prevent 34359*/
12123                                 buf1 = (char *)calloc(1, data_count_to_written);
12124
12125                                 if (buf1) {
12126                                         strncat(buf1, CONTENT_DATA, sizeof(buf1)-EM_SAFE_STRLEN(buf1)-1); /*prevent 34359*/
12127                                         EM_DEBUG_LOG(">>>>> BUF 1 [ %s ] ", buf1);
12128                                         strncat(buf1, low_char_set, sizeof(buf1)-EM_SAFE_STRLEN(buf1)-1);
12129                                         EM_DEBUG_LOG(">>>> HTML TAG DATA  [ %s ] ", buf1);
12130
12131                                         /* 1. Create a temporary file name */
12132                                         if (!_get_temp_file_name(&temp_file_name, &err)) {
12133                                                 EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed - %d", err);
12134                                                 if (err_code != NULL) *err_code = err;
12135                                                 goto FINISH_OFF;
12136                                         }
12137                                         EM_DEBUG_LOG_SEC(">>>>>>> TEMP APPEND FILE PATH [ %s ] ", temp_file_name);
12138
12139                                         /* Open the Temp file in Append mode */
12140                                         error = em_fopen(temp_file_name, "ab", &fp_dest);
12141                                         if (error != EMAIL_ERROR_NONE) {
12142                                                 EM_DEBUG_EXCEPTION_SEC(" fopen failed - %s [%d]", temp_file_name, error);
12143                                                 goto FINISH_OFF;
12144                                         }
12145
12146                                         /* 2. write the Latest data */
12147                                         nwritten = fwrite(buf1, data_count_to_written-1, 1, fp_dest);
12148
12149                                         if (nwritten > 0) {
12150                                                 EM_DEBUG_LOG(" Latest Data  : [%d ] bytes written ", nwritten);
12151                                                 nwritten = 0;
12152                                                 /* 3. Append old data */
12153                                                 nwritten = fwrite(buf, nread-1, 1, fp_dest);
12154
12155                                                 if (nwritten <= 0) {
12156                                                         EM_DEBUG_EXCEPTION(" Error Occured while writing Old data : [%d ] bytes written ", nwritten);
12157                                                         error = EMAIL_ERROR_SYSTEM_FAILURE;
12158                                                         goto FINISH_OFF;
12159                                                 } else {
12160                                                         EM_DEBUG_LOG(">>>> OLD data appended [ %d ] ", nwritten);
12161
12162                                                         if (!emstorage_move_file(temp_file_name, file_path, false, &err)) {
12163                                                                 EM_DEBUG_EXCEPTION(" emstorage_move_file failed - %d", err);
12164                                                                 goto FINISH_OFF;
12165                                                         }
12166                                                 }
12167
12168                                         } else {
12169                                                 EM_DEBUG_EXCEPTION(" Error Occured while writing New data : [%d ] bytes written ", nwritten);
12170                                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12171                                                 goto FINISH_OFF;
12172                                         }
12173                                 }
12174                         }
12175                 }
12176         }
12177
12178         ret = true;
12179 FINISH_OFF:
12180
12181         EM_SAFE_FREE(buf);
12182         EM_SAFE_FREE(buf1);
12183         EM_SAFE_FREE(low_char_set);
12184         EM_SAFE_FREE(temp_file_name);
12185
12186         if (fp_src != NULL) {
12187                 fclose(fp_src);
12188                 fp_src = NULL;
12189         }
12190
12191         if (fp_dest != NULL) {
12192                 fclose(fp_dest);
12193                 fp_dest = NULL;
12194         }
12195
12196         if (err_code)
12197                 *err_code = error;
12198
12199         EM_DEBUG_FUNC_END("ret [%d]", ret);
12200         return ret;
12201
12202 }
12203
12204 INTERNAL_FUNC int emstorage_move_file(char *src_file, char *dst_file, int sync_status, int *err_code)
12205 {
12206         EM_DEBUG_FUNC_BEGIN("src_file[%p], dst_file[%p], err_code[%p]", src_file, dst_file, err_code);
12207
12208         int ret = false;
12209         int error = EMAIL_ERROR_NONE;
12210         char errno_buf[ERRNO_BUF_SIZE] = {0};
12211
12212         if (src_file == NULL || dst_file == NULL) {
12213                 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
12214                 error = EMAIL_ERROR_INVALID_PARAM;
12215                 goto FINISH_OFF;
12216         }
12217
12218         EM_DEBUG_LOG_SEC("src_file[%s], dst_file[%s]", src_file, dst_file);
12219
12220         if (strcmp(src_file, dst_file) != 0) {
12221                 EM_DEBUG_LOG("oldpath and newpath are not on the same mounted file system.");
12222                 if (!emstorage_copy_file(src_file, dst_file, sync_status, &error)) {
12223                         EM_DEBUG_EXCEPTION("emstorage_copy_file failed - %d", error);
12224
12225                         struct stat temp_file_stat;
12226                         if (stat(src_file, &temp_file_stat) < 0)
12227                                 EM_DEBUG_EXCEPTION("no src file found [%s] : %s", src_file, EM_STRERROR(errno_buf));
12228
12229                         if (stat(dst_file, &temp_file_stat) < 0)
12230                                 EM_DEBUG_EXCEPTION("no dst file found [%s] : %s", dst_file, EM_STRERROR(errno_buf));
12231
12232                         error = EMAIL_ERROR_FILE_NOT_FOUND;
12233                         goto FINISH_OFF;
12234
12235
12236                 }
12237                 remove(src_file);
12238                 EM_DEBUG_LOG("src[%s] removed", src_file);
12239         } else {
12240                 EM_DEBUG_LOG("src[%s] = dst[%s]", src_file, dst_file);
12241         }
12242
12243         ret = true;
12244
12245 FINISH_OFF:
12246         if (err_code != NULL)
12247                 *err_code = error;
12248
12249         EM_DEBUG_FUNC_END("ret [%d]", ret);
12250         return ret;
12251 }
12252
12253 INTERNAL_FUNC int emstorage_delete_file(char *src_file, int *err_code)
12254 {
12255         EM_DEBUG_FUNC_BEGIN("src_file[%p], err_code[%p]", src_file, err_code);
12256
12257         int ret = false;
12258         int error = EMAIL_ERROR_NONE;
12259
12260         if (src_file == NULL) {
12261                 EM_DEBUG_EXCEPTION(" src_file[%p]", src_file);
12262
12263                 error = EMAIL_ERROR_INVALID_PARAM;
12264                 goto FINISH_OFF;
12265         }
12266
12267         if (remove(src_file) != 0) {
12268                 if (errno != ENOENT) {
12269                         EM_DEBUG_EXCEPTION(" remove failed - %d", errno);
12270
12271                         error = EMAIL_ERROR_SYSTEM_FAILURE;
12272                         goto FINISH_OFF;
12273                 } else {
12274                         EM_DEBUG_EXCEPTION(" no file found...");
12275
12276                         error = EMAIL_ERROR_FILE_NOT_FOUND;
12277                 }
12278         }
12279
12280         ret = true;
12281
12282 FINISH_OFF:
12283         if (err_code != NULL)
12284                 *err_code = error;
12285
12286         EM_DEBUG_FUNC_END("ret [%d]", ret);
12287         return ret;
12288 }
12289
12290
12291 INTERNAL_FUNC int emstorage_delete_dir(char *src_dir, int *err_code)
12292 {
12293         EM_DEBUG_FUNC_BEGIN("src_dir[%p], err_code[%p]", src_dir, err_code);
12294
12295         if (src_dir == NULL) {
12296                 EM_DEBUG_EXCEPTION("src_dir[%p]", src_dir);
12297
12298                 if (err_code != NULL)
12299                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12300                 return false;
12301         }
12302
12303         int error = EMAIL_ERROR_NONE;
12304
12305         DIR *dirp;
12306         struct dirent *dp;
12307         struct stat sbuf;
12308         char buf[512];
12309
12310         dirp = opendir(src_dir);
12311
12312         if (dirp == NULL) {
12313                 if (errno == ENOENT) {
12314                         EM_DEBUG_EXCEPTION("directory[%s] does not exist...", src_dir);
12315                         if (err_code != NULL)
12316                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12317                         return true;
12318                 } else {
12319                         EM_DEBUG_EXCEPTION("opendir failed [%s] [%d]", src_dir, errno);
12320                         if (err_code != NULL)
12321                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12322                         return false;
12323                 }
12324         }
12325
12326         while ((dp = readdir(dirp))) {
12327                 if (strncmp(dp->d_name, ".", 1) == 0 || strncmp(dp->d_name, "..", 2) == 0) /* prevent 34360 */
12328                         continue;
12329
12330                 SNPRINTF(buf, sizeof(buf), "%s/%s", src_dir, dp->d_name);
12331
12332                 if (lstat(buf, &sbuf) == 0 || stat(buf, &sbuf) == 0) {
12333                         /*  check directory */
12334                         if ((sbuf.st_mode & S_IFMT) == S_IFDIR) {       /*  directory */
12335                                 /*  recursive call */
12336                                 if (!emstorage_delete_dir(buf, &error)) {
12337                                         closedir(dirp);
12338                                         if (err_code != NULL)
12339                                                 *err_code = error;
12340                                         return false;
12341                                 }
12342                         } else {        /*  file */
12343                                 if (remove(buf) < 0) {
12344                                         EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", buf, errno);
12345                                         closedir(dirp);
12346                                         if (err_code != NULL)
12347                                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12348                                         return false;
12349                                 }
12350                         }
12351                 } else
12352                         EM_DEBUG_EXCEPTION("content does not exist...");
12353         }
12354
12355         closedir(dirp);
12356
12357         EM_DEBUG_LOG_DEV("remove direcotory [%s]", src_dir);
12358
12359         /* EM_DEBUG_FUNC_BEGIN(); */
12360
12361         if (remove(src_dir) < 0) {
12362                 EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", src_dir, errno);
12363                 if (err_code != NULL)
12364                         *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12365                 return false;
12366         }
12367
12368         if (err_code != NULL)
12369                 *err_code = error;
12370
12371         return true;
12372 }
12373
12374 /* faizan.h@samsung.com */
12375 INTERNAL_FUNC int emstorage_update_server_uid(char *multi_user_name,
12376                 int mail_id,
12377                 char *old_server_uid,
12378                 char *new_server_uid,
12379                 int *err_code)
12380 {
12381         EM_DEBUG_FUNC_BEGIN("new_server_uid[%s], old_server_uid[%s]", new_server_uid, old_server_uid);
12382         int ret = false;
12383         int error = EMAIL_ERROR_NONE;
12384         int transaction = true;
12385         int temp_strlen = 0;
12386         int and_operation = 0;
12387         char sql_query_string[QUERY_SIZE] = {0, };
12388         char conditional_clause_string[QUERY_SIZE] = {0};
12389
12390         if ((mail_id <= 0 || !old_server_uid) && !new_server_uid) {
12391                 EM_DEBUG_EXCEPTION("Invalid parameters");
12392                 if (err_code)
12393                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12394                 return false;
12395         }
12396
12397         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12398         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12399
12400         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE ");
12401
12402         if (mail_id > 0) {
12403                 temp_strlen = strlen(conditional_clause_string);
12404                 SNPRINTF(conditional_clause_string + temp_strlen, sizeof(conditional_clause_string) - temp_strlen,
12405                                 "mail_id = %d ", mail_id);
12406                 and_operation = 1;
12407         }
12408
12409         if (old_server_uid) {
12410                 temp_strlen = strlen(conditional_clause_string);
12411                 if (!and_operation) {
12412                         sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
12413                                         "server_mail_id = '%q'", old_server_uid);
12414                 } else {
12415                         sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
12416                                         "and server_mail_id = '%q'", old_server_uid);
12417                 }
12418         }
12419
12420         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
12421                         "UPDATE mail_tbl SET server_mail_id = '%q' %s", new_server_uid, conditional_clause_string);
12422
12423         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12424         if (error != EMAIL_ERROR_NONE) {
12425                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12426                 goto FINISH_OFF;
12427         }
12428
12429         ret = true;
12430
12431 FINISH_OFF:
12432
12433         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12434
12435         if (err_code != NULL)
12436                 *err_code = error;
12437
12438         EM_DEBUG_FUNC_END("ret [%d]", ret);
12439         return ret;
12440
12441 }
12442
12443 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)
12444 {
12445         EM_DEBUG_FUNC_BEGIN_SEC("mail_id[%d], new_server_uid[%s], mbox_name[%s]", mail_id, new_server_uid, mbox_name);
12446
12447         int ret = false;
12448         int error = EMAIL_ERROR_NONE;
12449         char sql_query_string[QUERY_SIZE] = {0, };
12450         int transaction = true;
12451
12452         if (!mail_id || !new_server_uid || !mbox_name) {
12453                 EM_DEBUG_EXCEPTION("Invalid parameters");
12454                 if (err_code != NULL)
12455                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12456                 return false;
12457         }
12458
12459         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12460         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12461
12462
12463         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12464                         "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);
12465
12466         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12467         if (error != EMAIL_ERROR_NONE) {
12468                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12469                 goto FINISH_OFF;
12470         }
12471
12472         ret     = true;
12473
12474 FINISH_OFF:
12475
12476         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12477
12478         if (err_code != NULL)
12479                 *err_code = error;
12480
12481         EM_DEBUG_FUNC_END("ret [%d]", ret);
12482         return ret;
12483
12484 }
12485
12486 INTERNAL_FUNC int emstorage_update_save_status(char *multi_user_name, int account_id, int *err_code)
12487 {
12488         EM_DEBUG_FUNC_BEGIN();
12489
12490         int ret = false;
12491         int transaction = true;
12492         int error = EMAIL_ERROR_NONE;
12493         char sql_query_string[QUERY_SIZE] = {0,};
12494
12495         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12496
12497         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12498
12499         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12500
12501         if (account_id <= ALL_ACCOUNT)
12502                 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);
12503         else
12504                 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);
12505
12506         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12507         if (error != EMAIL_ERROR_NONE) {
12508                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12509                 goto FINISH_OFF;
12510         }
12511
12512         ret = true;
12513
12514 FINISH_OFF:
12515         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12516
12517         if (err_code != NULL)
12518                 *err_code = error;
12519
12520         EM_DEBUG_FUNC_END("ret [%d]", ret);
12521         return ret;
12522
12523 }
12524
12525 int emstorage_get_unread_mailid(char *multi_user_name, int account_id, int vip_mode, int **mail_ids, int *mail_number, int *err_code)
12526 {
12527         EM_DEBUG_FUNC_BEGIN();
12528
12529         if ((!mail_ids) || (account_id <= 0 &&  account_id != -1)) {
12530                 EM_DEBUG_EXCEPTION(" mail_id[%p], account_id[%d] ", mail_ids, account_id);
12531                 if (err_code != NULL)
12532                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12533                 return false;
12534         }
12535
12536         int ret = false;
12537         int rc = -1;
12538         int error = EMAIL_ERROR_NONE;
12539         int count = 0;
12540         int i = 0;
12541         int col_index = 0;
12542         int *p_mail_ids = NULL;
12543         int transaction = false;
12544         char **result = NULL;
12545         char sql_query_string[QUERY_SIZE] = {0, };
12546         char temp_query_string[QUERY_SIZE] = {0,};
12547         char sql_select_query_string[QUERY_SIZE] = {0, };
12548
12549         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12550         EMSTORAGE_START_READ_TRANSACTION(transaction);
12551
12552         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12553
12554         if (account_id == -1) {
12555                 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string),
12556                         "SELECT mail_id FROM mail_tbl WHERE flags_seen_field = 0 AND (save_status = %d or save_status = %d)",
12557                         EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12558         } else {
12559                 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string),
12560                         "SELECT mail_id FROM mail_tbl WHERE account_id = %d AND flags_seen_field = 0 AND (save_status = %d or save_status = %d)",
12561                         account_id, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12562         }
12563
12564         if (vip_mode) {
12565                 SNPRINTF(temp_query_string, sizeof(temp_query_string),
12566                         "%s AND tag_id < 0", sql_select_query_string);
12567         } else {
12568                 SNPRINTF(temp_query_string, sizeof(temp_query_string), "%s", sql_select_query_string);
12569         }
12570
12571         SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s ORDER BY date_time ASC", temp_query_string);
12572
12573         EM_DEBUG_LOG_SEC("query: [%s]", sql_query_string);
12574
12575         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
12576         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
12577                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12578
12579         EM_DEBUG_LOG("Count : %d", count);
12580
12581         if (count == 0) {
12582                 EM_DEBUG_EXCEPTION("no Mails found...");
12583                 ret = false;
12584                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
12585                 goto FINISH_OFF;
12586         }
12587
12588         p_mail_ids = em_malloc(count * sizeof(int));
12589         if (p_mail_ids == NULL) {
12590                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
12591                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12592                 goto FINISH_OFF;
12593         }
12594
12595         col_index = 1;
12596
12597         for (i = 0; i < count; i++)
12598                 _get_table_field_data_int(result, &(p_mail_ids[i]), col_index++);
12599
12600         ret = true;
12601
12602 FINISH_OFF:
12603
12604         if (result)
12605                 sqlite3_free_table(result);
12606
12607         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12608
12609         //      sqlite3_db_release_memory(local_db_handle);
12610
12611         if (ret == true) {
12612                 if (mail_ids != NULL)
12613                         *mail_ids = p_mail_ids;
12614
12615                 if (mail_number != NULL)
12616                         *mail_number = count;
12617         } else {
12618                 EM_SAFE_FREE(p_mail_ids);
12619         }
12620
12621         if (err_code != NULL)
12622                 *err_code = error;
12623
12624         EM_DEBUG_FUNC_END("ret [%d]", ret);
12625         return ret;
12626 }
12627
12628 int setting_system_command(const char *command)
12629
12630 {
12631         int pid = 0, status = 0;
12632         char *const environ[] = { NULL };
12633
12634         if (command == 0)
12635                 return 1;
12636
12637         pid = fork();
12638
12639         if (pid == -1)
12640                 return -1;
12641
12642         if (pid == 0) {
12643                 char *argv[4];
12644
12645                 argv[0] = "sh";
12646                 argv[1] = "-c";
12647                 argv[2] = (char *)command;
12648                 argv[3] = 0;
12649
12650                 execve("/bin/sh", argv, environ);
12651                 abort();
12652         }
12653
12654         do {
12655                 if (waitpid(pid, &status, 0) == -1) {
12656                         if (errno != EINTR)
12657                                 return -1;
12658                 } else {
12659                         return status;
12660                 }
12661         } while (1);
12662
12663         return 0;
12664 }
12665
12666
12667 INTERNAL_FUNC int emstorage_mail_get_total_diskspace_usage(unsigned long *total_usage,  int transaction, int *err_code)
12668 {
12669         EM_DEBUG_FUNC_BEGIN("total_usage[%p],  transaction[%d], err_code[%p]", total_usage, transaction, err_code);
12670
12671         if (!total_usage) {
12672                 EM_DEBUG_EXCEPTION("total_usage[%p]", total_usage);
12673
12674                 if (err_code != NULL)
12675                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12676                 return false;
12677         }
12678
12679         int   ret = false;
12680         int   error = EMAIL_ERROR_NONE;
12681         char  syscmd[256] = {0, };
12682         char  line[256] = {0, };
12683         char *line_from_file = NULL;
12684         FILE *fp = NULL;
12685         unsigned long total_diskusage = 0;
12686
12687         SNPRINTF(syscmd, sizeof(syscmd), "touch %s", SETTING_MEMORY_TEMP_FILE_PATH);
12688         if (setting_system_command(syscmd) == -1) {
12689                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
12690
12691                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12692                 goto FINISH_OFF;
12693         }
12694
12695         SNPRINTF(syscmd, sizeof(syscmd), "du -hsk %s > %s", EMAIL_PATH, SETTING_MEMORY_TEMP_FILE_PATH);
12696         EM_DEBUG_LOG(" cmd : %s", syscmd);
12697         if (setting_system_command(syscmd) == -1) {
12698                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : Setting > Memory] System Command [%s] is failed", syscmd);
12699
12700                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12701                 goto FINISH_OFF;
12702         }
12703
12704         error = em_fopen(SETTING_MEMORY_TEMP_FILE_PATH, "r", &fp);
12705         if (error != EMAIL_ERROR_NONE) {
12706                 perror(SETTING_MEMORY_TEMP_FILE_PATH);
12707                 goto FINISH_OFF;
12708         }
12709
12710         line_from_file = fgets(line, sizeof(line), fp);
12711
12712         if (line_from_file == NULL) {
12713                 EM_DEBUG_EXCEPTION("fgets failed");
12714                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12715                 goto FINISH_OFF;
12716         }
12717         total_diskusage = strtoul(line, NULL, 10);
12718
12719         memset(syscmd, 0, sizeof(syscmd));
12720         SNPRINTF(syscmd, sizeof(syscmd), "rm -f %s", SETTING_MEMORY_TEMP_FILE_PATH);
12721         if (setting_system_command(syscmd) == -1) {
12722                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage :  [Setting > Memory] System Command [%s] is failed", syscmd);
12723                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12724                 goto FINISH_OFF;
12725         }
12726
12727         EM_DEBUG_LOG("[Setting > Memory] @@@@@ Size of Directory [%s] is %ld KB", EMAIL_PATH, total_diskusage);
12728
12729         ret = true;
12730
12731 FINISH_OFF:
12732         if (err_code != NULL)
12733                 *err_code = error;
12734
12735         if (ret)
12736                 *total_usage = total_diskusage;
12737         else
12738                 *total_usage = 0;
12739
12740         if (fp) fclose(fp); /* prevent 32730 */
12741
12742         EM_DEBUG_FUNC_END("ret [%d]", ret);
12743         return ret;
12744 }
12745 #define MAILHOME_UTF8    tzplatform_mkpath(TZ_USER_DATA, "email/.email_data/7/348/UTF-8")
12746
12747 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)
12748 {
12749         DB_STMT hStmt = NULL;
12750         int ret = false;
12751         int error = EMAIL_ERROR_NONE;
12752         int rc = 0;
12753         char sql_query_string[QUERY_SIZE] = {0, };
12754
12755         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12756                         "INSERT INTO mail_tbl VALUES "
12757                         "(?" /*  mail_id */
12758                         ", ?" /*  account_id */
12759                         ", ?" /*  mail_size */
12760                         ", ?" /*  server_mail_status */
12761                         ", ?" /*  server_mailbox_name */
12762                         ", ?" /*  server_mail_id */
12763                         ", ?" /*  reference_mail_id */
12764                         ", ?" /*  full_address_from */
12765                         ", ?" /*  full_address_reply */
12766                         ", ?" /*  full_address_to */
12767                         ", ?" /*  full_address_cc */
12768                         ", ?" /*  full_address_bcc */
12769                         ", ?" /*  full_address_return */
12770                         ", ?" /*  subject */
12771                         ", ?" /*  body_download_status */
12772                         ", ?" /*  file_path_plain */
12773                         ", ?" /*  file_path_html */
12774                         ", ?" /*  date_time */
12775                         ", ?" /*  flags_seen_field */
12776                         ", ?" /*  flags_deleted_field */
12777                         ", ?" /*  flags_flagged_field */
12778                         ", ?" /*  flags_answered_field */
12779                         ", ?" /*  flags_recent_field */
12780                         ", ?" /*  flags_draft_field */
12781                         ", ?" /*  flags_forwarded_field */
12782                         ", ?" /*  DRM_status */
12783                         ", ?" /*  priority */
12784                         ", ?" /*  save_status */
12785                         ", ?" /*  lock_status */
12786                         ", ?" /*  message_id */
12787                         ", ?" /*  report_status */
12788                         ", ?" /*  email_address_sender */
12789                         ", ?" /*  email_address_recipient */
12790                         ", ?" /*  attachment_count */
12791                         ", ?" /*  inline_content_count */
12792                         ", ?" /*  preview_text */
12793                         ", ?" /*  thread_id */
12794                         ", ?" /*  mailbox_type */
12795                         ", ?" /*  alias_sender */
12796                         ", ?" /*  alias_recipient */
12797                         ", ?" /*  thread_item_count */
12798                         ", ?" /*  meeting_request_status */
12799                         ", ?" /*  message_class */
12800                         ", ?" /*  digest_type */
12801                         ", ?" /*  smime_type */
12802                         ", ?" /*  scheduled_sending_time */
12803                         ", ?" /*  remaining_resend_times */
12804                         ", ?" /*  tag_id   */
12805                         ", ?" /*  replied_time */
12806                         ", ?" /*  forwarded_time */
12807                         ", ?" /*  default_charset */
12808                         ", ?" /*  eas_data_length */
12809                         ", ?" /*  eas_data */
12810                         ")");
12811
12812         int transaction = true;
12813         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12814
12815         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12816
12817
12818         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12819         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
12820                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12821
12822         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_id);
12823         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, account_id);
12824         _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, EMAIL_MAILBOX_TYPE_OUTBOX);
12825         _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, "save test - long", 1, SUBJECT_LEN_IN_MAIL_TBL);
12826         _bind_stmt_field_data_string(hStmt, DATETIME_IDX_IN_MAIL_TBL, "20100316052908", 0, DATETIME_LEN_IN_MAIL_TBL);
12827         _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, 0);
12828         _bind_stmt_field_data_string(hStmt, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL, "", 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
12829         _bind_stmt_field_data_string(hStmt, SERVER_MAIL_ID_IDX_IN_MAIL_TBL, "", 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
12830         _bind_stmt_field_data_string(hStmt, MESSAGE_ID_IDX_IN_MAIL_TBL, "", 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
12831         _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, 0);
12832         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL, "<test08@streaming.s3glab.net>", 1, FROM_LEN_IN_MAIL_TBL);
12833         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL, "", 1, REPLY_TO_LEN_IN_MAIL_TBL);
12834         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL, full_address_to, 1, TO_LEN_IN_MAIL_TBL);
12835         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL, full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
12836         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL, full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
12837         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL, "", 1, RETURN_PATH_LEN_IN_MAIL_TBL);
12838         _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);
12839         _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);
12840         _bind_stmt_field_data_string(hStmt, ALIAS_SENDER_IDX_IN_MAIL_TBL, "send_alias", 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12841         _bind_stmt_field_data_string(hStmt, ALIAS_RECIPIENT_IDX_IN_MAIL_TBL, "recipient_alias", 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12842         _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, 1);
12843         _bind_stmt_field_data_string(hStmt, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL, (char *)MAILHOME_UTF8, 0, TEXT_1_LEN_IN_MAIL_TBL);
12844         _bind_stmt_field_data_string(hStmt, FILE_PATH_HTML_IDX_IN_MAIL_TBL, "", 0, TEXT_2_LEN_IN_MAIL_TBL);
12845         _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, 4);
12846         _bind_stmt_field_data_char(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, 0);
12847         _bind_stmt_field_data_char(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, 0);
12848         _bind_stmt_field_data_char(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, 0);
12849         _bind_stmt_field_data_char(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, 0);
12850         _bind_stmt_field_data_char(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, 0);
12851         _bind_stmt_field_data_char(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, 0);
12852         _bind_stmt_field_data_char(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, 0);
12853         _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, 0);
12854         _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, 0);
12855         _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, 0);
12856         _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, 0);
12857         _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, 0);
12858         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12859         _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, 0);
12860         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12861         _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, 0);
12862         _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, 0);
12863         _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, "preview body", 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
12864         _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, 0);
12865         _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, 0);
12866         _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, 0);
12867         _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, 0);
12868         _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, 0);
12869         _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, 0);
12870         _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, 0);
12871         _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, 0);
12872         _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, 0);
12873         _bind_stmt_field_data_string(hStmt, DEFAULT_CHARSET_IDX_IN_MAIL_TBL, "UTF-8", 0, TEXT_2_LEN_IN_MAIL_TBL);
12874         _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, 0);
12875         _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, NULL, 0);
12876
12877         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12878         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
12879                         ("sqlite3_step fail:%d", rc));
12880         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
12881                         ("sqlite3_step fail:%d", rc));
12882         ret = true;
12883
12884 FINISH_OFF:
12885         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12886         if (hStmt != NULL) {
12887                 rc = sqlite3_finalize(hStmt);
12888                 if (rc != SQLITE_OK) {
12889                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
12890                         error = EMAIL_ERROR_DB_FAILURE;
12891                 }
12892         }
12893
12894         if (err_code != NULL)
12895                 *err_code = error;
12896
12897         EM_DEBUG_FUNC_END("ret [%d]", ret);
12898         return ret;
12899 }
12900
12901
12902 INTERNAL_FUNC int emstorage_get_max_mail_count()
12903 {
12904         return EMAIL_MAIL_MAX_COUNT;
12905 }
12906
12907 #define STRIPPED_SUBJECT_BUFFER_SIZE 4086
12908
12909 INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(char *multi_user_name,
12910                 emstorage_mail_tbl_t *mail_tbl,
12911                 int *thread_id,
12912                 int *result_latest_mail_id_in_thread,
12913                 int *thread_item_count)
12914 {
12915         EM_DEBUG_FUNC_BEGIN("mail_tbl [%p], thread_id [%p], "
12916                         "result_latest_mail_id_in_thread [%p], thread_item_count [%p]",
12917                         mail_tbl, thread_id, result_latest_mail_id_in_thread, thread_item_count);
12918         EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
12919
12920         int rc = 0, query_size = 0, query_size_account = 0;
12921         int i = 0;
12922         int search_thread = false;
12923         int account_id = 0;
12924         int err_code = EMAIL_ERROR_NONE;
12925         int count = 0, result_thread_id = -1, latest_mail_id_in_thread = -1;
12926         time_t latest_date_time = 0;
12927         char *subject = NULL;
12928         char *p_subject = NULL;
12929         char *sql_query_string = NULL, *sql_account = NULL;
12930         int col_index = 4;
12931         int temp_thread_id = -1;
12932         char *sql_format = "SELECT mail_id, thread_id, date_time, subject "
12933                 "FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
12934         char *sql_format_account = " AND account_id = %d ";
12935         char *sql_format_order_by = " ORDER BY thread_id, date_time DESC ";
12936         char **result = NULL;
12937         char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
12938         char stripped_subject2[STRIPPED_SUBJECT_BUFFER_SIZE];
12939
12940         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12941
12942         EM_DEBUG_LOG("subject: [%p], mail_id: [%d]", subject, mail_tbl->mail_id);
12943
12944         EM_IF_NULL_RETURN_VALUE(mail_tbl, EMAIL_ERROR_INVALID_PARAM);
12945         EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
12946         EM_IF_NULL_RETURN_VALUE(result_latest_mail_id_in_thread, EMAIL_ERROR_INVALID_PARAM);
12947         EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
12948
12949         account_id   = mail_tbl->account_id;
12950         subject      = mail_tbl->subject;
12951
12952         EM_DEBUG_LOG_SEC("subject: [%s]", subject);
12953
12954         if (EM_SAFE_STRLEN(subject) == 0 && mail_tbl->mail_id != 0) {
12955                 result_thread_id = mail_tbl->mail_id;
12956                 count = 1;
12957                 goto FINISH_OFF;
12958         }
12959
12960         if (em_find_pos_stripped_subject_for_thread_view(subject,
12961                                 stripped_subject,
12962                                 STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE)      {
12963                 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
12964                 err_code =  EMAIL_ERROR_UNKNOWN;
12965                 result_thread_id = -1;
12966                 goto FINISH_OFF;
12967         }
12968
12969         EM_DEBUG_LOG_SEC("stripped_subject: [%s]", stripped_subject);
12970
12971         if (EM_SAFE_STRLEN(stripped_subject) == 0) {
12972                 result_thread_id = -1;
12973                 goto FINISH_OFF;
12974         }
12975
12976         EM_DEBUG_LOG_SEC("em_find_pos_stripped_subject_for_thread_view returns[len = %zu] = %s",
12977                         EM_SAFE_STRLEN(stripped_subject), stripped_subject);
12978
12979         if (account_id > 0) {
12980                 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
12981                 sql_account = malloc(query_size_account);
12982                 if (sql_account == NULL) {
12983                         EM_DEBUG_EXCEPTION("malloc for sql_account  is failed %d", query_size_account);
12984                         err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
12985                         goto FINISH_OFF;
12986                 }
12987                 snprintf(sql_account, query_size_account, sql_format_account, account_id);
12988         }
12989
12990         /* prevent 34362 */
12991         query_size = strlen(sql_format) + strlen(stripped_subject)*2 + 50 + query_size_account + strlen(sql_format_order_by); /*  + query_size_mailbox; */
12992         sql_query_string = malloc(query_size);
12993
12994         if (sql_query_string == NULL) {
12995                 EM_DEBUG_EXCEPTION("malloc for sql  is failed %d", query_size);
12996                 err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
12997                 goto FINISH_OFF;
12998         }
12999
13000         sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mail_tbl->mailbox_id);
13001
13002         if (account_id > 0)
13003                 EM_SAFE_STRNCAT(sql_query_string, sql_account, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13004
13005         EM_SAFE_STRNCAT(sql_query_string, sql_format_order_by, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13006         EM_SAFE_STRNCAT(sql_query_string, ";", query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13007
13008         EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
13009
13010         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL),
13011                         rc);
13012
13013         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13014                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13015
13016         EM_DEBUG_LOG("Result rows count : %d", count);
13017
13018         if (count == 0)
13019                 result_thread_id = -1;
13020         else {
13021                 for (i = 0; i < count; i++) {
13022                         EM_SAFE_FREE(p_subject);
13023
13024                         _get_table_field_data_int(result, &latest_mail_id_in_thread, col_index++);
13025                         _get_table_field_data_int(result, &result_thread_id, col_index++);
13026                         _get_table_field_data_time_t(result, &latest_date_time, col_index++);
13027                         _get_table_field_data_string(result, &p_subject, 0, col_index++);
13028
13029                         if (temp_thread_id == result_thread_id)
13030                                 continue;
13031
13032                         temp_thread_id = result_thread_id;
13033
13034                         if (em_find_pos_stripped_subject_for_thread_view(p_subject,
13035                                                 stripped_subject2,
13036                                                 STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE)      {
13037                                 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
13038                                 err_code = EMAIL_ERROR_UNKNOWN;
13039                                 result_thread_id = -1;
13040                                 goto FINISH_OFF;
13041                         }
13042
13043                         if (g_strcmp0(stripped_subject2, stripped_subject) == 0) {
13044                                 if (latest_date_time < mail_tbl->date_time)
13045                                         *result_latest_mail_id_in_thread = latest_mail_id_in_thread;
13046                                 else
13047                                         *result_latest_mail_id_in_thread = mail_tbl->mail_id;
13048
13049                                 search_thread = true;
13050                         }
13051
13052                         if (search_thread) {
13053                                 EM_DEBUG_LOG("latest_mail_id_in_thread [%d], mail_id [%d]",
13054                                                 latest_mail_id_in_thread, mail_tbl->mail_id);
13055                                 break;
13056                         } else {
13057                                 result_thread_id = -1;
13058                                 count = 0;
13059                         }
13060                 }
13061
13062         }
13063
13064 FINISH_OFF:
13065
13066         *thread_id = result_thread_id;
13067         *thread_item_count = count;
13068
13069         EM_DEBUG_LOG("Result thread id : %d", *thread_id);
13070         EM_DEBUG_LOG("Result count : %d", *thread_item_count);
13071         EM_DEBUG_LOG("err_code : %d", err_code);
13072
13073         EM_SAFE_FREE(sql_account);
13074         EM_SAFE_FREE(sql_query_string);
13075         EM_SAFE_FREE(p_subject);
13076
13077         sqlite3_free_table(result);
13078
13079         EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
13080
13081         return err_code;
13082 }
13083
13084 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)
13085 {
13086         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);
13087         EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
13088
13089         int rc = 0;
13090         int query_size = 0;
13091         int query_size_account = 0;
13092         int err_code = EMAIL_ERROR_NONE;
13093         int count = 0;
13094         int result_thread_id = -1;
13095         char *sql_query_string = NULL;
13096         char *sql_account = NULL;
13097         char *sql_format = "SELECT thread_id FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
13098         char *sql_format_account = " AND account_id = %d ";
13099         char *sql_format_order_by = " ORDER BY date_time DESC ";
13100         char **result = NULL;
13101         char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
13102         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13103
13104         EM_IF_NULL_RETURN_VALUE(mail_subject, EMAIL_ERROR_INVALID_PARAM);
13105         EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
13106         EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
13107
13108         EM_DEBUG_LOG_SEC("subject: [%s]", mail_subject);
13109
13110         if (em_find_pos_stripped_subject_for_thread_view(mail_subject, stripped_subject, STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE)     {
13111                 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view  is failed");
13112                 err_code =  EMAIL_ERROR_UNKNOWN;
13113                 result_thread_id = -1;
13114                 goto FINISH_OFF;
13115         }
13116
13117         if (EM_SAFE_STRLEN(stripped_subject) == 0) {
13118                 result_thread_id = -1;
13119                 goto FINISH_OFF;
13120         }
13121
13122         EM_DEBUG_LOG("em_find_pos_stripped_subject_for_thread_view returns[len = %zu] = %s", EM_SAFE_STRLEN(stripped_subject), stripped_subject);
13123
13124         if (account_id > 0) {
13125                 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
13126                 sql_account = malloc(query_size_account);
13127                 if (sql_account == NULL) {
13128                         EM_DEBUG_EXCEPTION("malloc for sql_account  is failed %d", query_size_account);
13129                         err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
13130                         goto FINISH_OFF;
13131                 }
13132                 snprintf(sql_account, query_size_account, sql_format_account, account_id);
13133         }
13134
13135         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; */
13136         sql_query_string = malloc(query_size);
13137
13138         if (sql_query_string == NULL) {
13139                 EM_DEBUG_EXCEPTION("malloc for sql  is failed %d", query_size);
13140                 err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
13141                 goto FINISH_OFF;
13142         }
13143
13144         sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mailbox_id);
13145
13146         if (account_id > 0)
13147                 EM_SAFE_STRNCAT(sql_query_string, sql_account, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13148         EM_SAFE_STRNCAT(sql_query_string, sql_format_order_by, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13149         EM_SAFE_STRNCAT(sql_query_string, ";", query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13150
13151         EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
13152
13153         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
13154
13155         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13156                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13157
13158         EM_DEBUG_LOG("Result rows count : %d", count);
13159
13160         if (count == 0)
13161                 result_thread_id = -1;
13162         else
13163                 _get_table_field_data_int(result, &result_thread_id, 1);
13164
13165 FINISH_OFF:
13166         *thread_id = result_thread_id;
13167         *thread_item_count = count;
13168
13169         EM_DEBUG_LOG("Result thread id : %d", *thread_id);
13170         EM_DEBUG_LOG("Result count : %d", *thread_item_count);
13171         EM_DEBUG_LOG("err_code : %d", err_code);
13172
13173         EM_SAFE_FREE(sql_account);
13174         EM_SAFE_FREE(sql_query_string);
13175
13176         sqlite3_free_table(result);
13177
13178         EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
13179
13180         return err_code;
13181 }
13182
13183 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)
13184 {
13185         EM_DEBUG_FUNC_BEGIN();
13186
13187         int count = 0, ret = false;
13188         int error = EMAIL_ERROR_NONE;
13189         emstorage_mail_tbl_t *p_data_tbl = NULL;
13190         char conditional_clause[QUERY_SIZE] = {0, };
13191
13192         EM_IF_NULL_RETURN_VALUE(mail_tbl, false);
13193
13194         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE thread_id = %d AND thread_item_count > 0", thread_id);
13195         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
13196
13197         if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
13198                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
13199                 goto FINISH_OFF;
13200         }
13201
13202         if (p_data_tbl)
13203                 EM_DEBUG_LOG("thread_id : %d, thread_item_count : %d", p_data_tbl[0].thread_id, p_data_tbl[0].thread_item_count);
13204
13205         ret = true;
13206
13207 FINISH_OFF:
13208         if (ret == true)
13209                 *mail_tbl = p_data_tbl;
13210         else if (p_data_tbl != NULL)
13211                 emstorage_free_mail(&p_data_tbl, 1, NULL);
13212
13213         if (err_code != NULL)
13214                 *err_code = error;
13215
13216         EM_DEBUG_FUNC_END("ret [%d]", ret);
13217         return ret;
13218 }
13219
13220 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)
13221 {
13222         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]"
13223                         , account_id , mailbox_id , search_type , search_value , sorting , sender_list, sender_count, err_code);
13224
13225         if ((!sender_list) || (!sender_count)) {
13226                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
13227                 if (err_code != NULL)
13228                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13229                 return false;
13230         }
13231
13232         int rc = -1, ret = false;
13233         int error = EMAIL_ERROR_NONE;
13234         int count = 0;
13235         int i, col_index = 0;
13236         int read_count = 0;
13237         email_sender_list_t *p_sender_list = NULL;
13238         char sql_query_string[QUERY_SIZE] = {0, };
13239         char **result = NULL;
13240         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13241
13242         SNPRINTF(sql_query_string, sizeof(sql_query_string),
13243                         "SELECT email_address_sender, alias_sender, COUNT(email_address_sender), SUM(flags_seen_field = 1) "
13244                         "FROM mail_tbl ");
13245
13246         /*  mailbox_id */
13247         if (mailbox_id)
13248                 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);
13249         else    /*  NULL  means all mailbox_name. but except for trash(3), spambox(5), all emails(for GMail, 7) */
13250                 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) ");
13251
13252         /*  account id */
13253         /*  '0' (ALL_ACCOUNT) means all account */
13254         if (account_id > ALL_ACCOUNT)
13255                 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);
13256
13257         if (search_value) {
13258                 switch (search_type) {
13259                 case EMAIL_SEARCH_FILTER_SUBJECT:
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%%%%\')) ", search_value);
13262                         break;
13263                 case EMAIL_SEARCH_FILTER_SENDER:
13264                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13265                                         " AND  ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13266                                         ") ", search_value);
13267                         break;
13268                 case EMAIL_SEARCH_FILTER_RECIPIENT:
13269                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13270                                         " AND ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13271                                         "       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13272                                         "       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13273                                         ") ", search_value, search_value, search_value);
13274                         break;
13275                 case EMAIL_SEARCH_FILTER_ALL:
13276                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13277                                         " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') "
13278                                         "       OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13279                                         "                       OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13280                                         "                       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13281                                         "                       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13282                                         "               ) "
13283                                         "       )"
13284                                         ")", search_value, search_value, search_value, search_value, search_value);
13285                         break;
13286                 }
13287         }
13288
13289
13290         /*  sorting option is not available now. The order of sender list is ascending order by display name */
13291         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
13292                         "GROUP BY email_address_sender "
13293                         "ORDER BY UPPER(alias_sender) ");
13294
13295         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
13296
13297         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
13298         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13299                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13300
13301         EM_DEBUG_LOG("Count of Sender [%d]", count);
13302
13303         if (!(p_sender_list = (email_sender_list_t*)em_malloc(sizeof(email_sender_list_t) * count))) {
13304                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
13305                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13306                 goto FINISH_OFF;
13307         }
13308
13309         col_index = 4;
13310
13311         EM_DEBUG_LOG(">>>> DATA ASSIGN START >>");
13312         for (i = 0; i < count; i++) {
13313                 _get_table_field_data_string(result, &(p_sender_list[i].address), 1, col_index++);
13314                 _get_table_field_data_string(result, &(p_sender_list[i].display_name), 1, col_index++);
13315                 _get_table_field_data_int(result, &(p_sender_list[i].total_count), col_index++);
13316                 _get_table_field_data_int(result, &(read_count), col_index++);
13317                 p_sender_list[i].unread_count = p_sender_list[i].total_count - read_count;              /*  unread count = total - read          */
13318         }
13319         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >>", count);
13320
13321         sqlite3_free_table(result);
13322         result = NULL;
13323
13324         ret = true;
13325
13326 FINISH_OFF:
13327         if (ret == true) {
13328                 *sender_list = p_sender_list;
13329                 *sender_count = count;
13330                 EM_DEBUG_LOG(">>>> COUNT : %d >>", count);
13331         }
13332
13333
13334         if (err_code != NULL)
13335                 *err_code = error;
13336
13337         EM_DEBUG_FUNC_END("ret [%d]", ret);
13338         return ret;
13339 }
13340
13341 INTERNAL_FUNC int emstorage_free_sender_list(email_sender_list_t **sender_list, int count)
13342 {
13343         EM_DEBUG_FUNC_BEGIN("sender_list[%p], count[%d]", sender_list, count);
13344
13345         int err = EMAIL_ERROR_NONE;
13346
13347         if (count > 0) {
13348                 if (!sender_list || !*sender_list) {
13349                         EM_DEBUG_EXCEPTION("sender_list[%p], count[%d]", sender_list, count);
13350                         err = EMAIL_ERROR_INVALID_PARAM;
13351                         return err;
13352                 }
13353
13354                 email_sender_list_t* p = *sender_list;
13355                 int i = 0;
13356
13357                 for (; i < count; i++) {
13358                         EM_SAFE_FREE(p[i].address);
13359                         EM_SAFE_FREE(p[i].display_name);
13360                 }
13361
13362                 EM_SAFE_FREE(p);
13363                 *sender_list = NULL;
13364         }
13365
13366         return err;
13367 }
13368
13369
13370 INTERNAL_FUNC int emstorage_free_address_info_list(email_address_info_list_t **address_info_list)
13371 {
13372         EM_DEBUG_FUNC_BEGIN("address_info_list[%p]", address_info_list);
13373
13374         int err = EMAIL_ERROR_NONE;
13375         email_address_info_t *p_address_info = NULL;
13376         GList *list = NULL;
13377         GList *node = NULL;
13378         int i = 0;
13379
13380         if (!address_info_list || !*address_info_list) {
13381                 EM_DEBUG_EXCEPTION("address_info_list[%p]", address_info_list);
13382                 err = EMAIL_ERROR_INVALID_PARAM;
13383                 return err;
13384         }
13385
13386         /*  delete GLists */
13387         for (i = EMAIL_ADDRESS_TYPE_FROM; i <= EMAIL_ADDRESS_TYPE_BCC; i++) {
13388                 switch (i) {
13389                 case EMAIL_ADDRESS_TYPE_FROM:
13390                         list = (*address_info_list)->from;
13391                         break;
13392                 case EMAIL_ADDRESS_TYPE_TO:
13393                         list = (*address_info_list)->to;
13394                         break;
13395                 case EMAIL_ADDRESS_TYPE_CC:
13396                         list = (*address_info_list)->cc;
13397                         break;
13398                 case EMAIL_ADDRESS_TYPE_BCC:
13399                         list = (*address_info_list)->bcc;
13400                         break;
13401                 }
13402
13403                 /*  delete dynamic-allocated memory for each item */
13404                 node = g_list_first(list);
13405                 while (node != NULL) {
13406                         p_address_info = (email_address_info_t*)node->data;
13407                         EM_SAFE_FREE(p_address_info->address);
13408                         EM_SAFE_FREE(p_address_info->display_name);
13409                         EM_SAFE_FREE(node->data);
13410
13411                         node = g_list_next(node);
13412                 }
13413                 g_list_free(list);
13414         }
13415
13416         EM_SAFE_FREE(*address_info_list);
13417         *address_info_list = NULL;
13418
13419         EM_DEBUG_FUNC_END("err [%d]", err);
13420         return err;
13421 }
13422 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
13423
13424 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)
13425 {
13426         EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
13427
13428         if (!local_activity || !activity_id) {
13429                 EM_DEBUG_EXCEPTION("local_activity[%p], transaction[%d], activity_id[%p], err_code[%p]", local_activity, transaction, activity_id, err_code);
13430                 if (err_code != NULL)
13431                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13432                 return false;
13433         }
13434
13435         int rc = -1;
13436         int ret = false;
13437         int error = EMAIL_ERROR_NONE;
13438         int i = 0;
13439
13440         char sql_query_string[QUERY_SIZE] = {0, };
13441         DB_STMT hStmt = NULL;
13442         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13443
13444         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13445         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13446         SNPRINTF(sql_query_string, sizeof(sql_query_string),
13447                         "INSERT INTO mail_partial_body_activity_tbl VALUES "
13448                         "("
13449                         "? "  /* Account ID */
13450                         ",?"  /* Local Mail ID */
13451                         ",?"  /* Server mail ID */
13452                         ",?"  /* Activity ID */
13453                         ",?"  /* Activity type*/
13454                         ",?"  /* Mailbox ID*/
13455                         ",?"  /* Mailbox name*/
13456                         ",?"  /* Multi User Name */
13457                         ") ");
13458
13459         char *sql = "SELECT max(rowid) FROM mail_partial_body_activity_tbl;";
13460         char **result = NULL;
13461
13462
13463         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13464
13465         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13466                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13467
13468         if (NULL == result[1]) rc = 1;
13469         else rc = atoi(result[1])+1;
13470         sqlite3_free_table(result);
13471         result = NULL;
13472
13473         *activity_id = local_activity->activity_id = rc;
13474
13475         EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [ %d ], MAIL ID [ %d ], ACTIVITY TYPE [ %d ], SERVER MAIL ID [ %lu ]", \
13476                         local_activity->activity_id, local_activity->mail_id, local_activity->activity_type, local_activity->server_mail_id);
13477
13478         if (local_activity->mailbox_id)
13479                 EM_DEBUG_LOG(" MAILBOX ID [ %d ]", local_activity->mailbox_id);
13480
13481
13482         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13483         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13484                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13485
13486
13487         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
13488         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
13489         _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
13490         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
13491         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
13492         _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
13493         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->mailbox_name, 0, 3999);
13494         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
13495
13496
13497         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13498
13499         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
13500                         ("sqlite3_step fail:%d", rc));
13501         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13502                         ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
13503
13504         ret = true;
13505
13506 FINISH_OFF:
13507         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13508         if (hStmt != NULL) {
13509                 rc = sqlite3_finalize(hStmt);
13510                 hStmt = NULL;
13511                 if (rc != SQLITE_OK) {
13512                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13513                         error = EMAIL_ERROR_DB_FAILURE;
13514                 }
13515         }
13516
13517         if (err_code != NULL)
13518                 *err_code = error;
13519
13520         EM_DEBUG_FUNC_END("ret [%d]", ret);
13521         return ret;
13522 }
13523
13524 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)
13525 {
13526         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13527
13528         if (account_id < FIRST_ACCOUNT_ID || NULL == mailbox_list || *mailbox_list == NULL || NULL == count) {
13529                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13530                 if (err_code != NULL)
13531                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13532                 return false;
13533         }
13534
13535         int ret = false;
13536         int error = EMAIL_ERROR_NONE;
13537         char **result;
13538         int i = 0, rc = -1;
13539         int *mbox_list = NULL;
13540         DB_STMT hStmt = NULL;
13541         char sql_query_string[QUERY_SIZE] = {0, };
13542
13543         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13544         EMSTORAGE_START_READ_TRANSACTION(transaction);
13545
13546         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13547         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);
13548
13549
13550         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13551         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13552                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13553
13554         *count = atoi(result[1]);
13555         sqlite3_free_table(result);
13556
13557         if (*count <= 0) {
13558                 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
13559                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13560                 ret = true;
13561                 goto FINISH_OFF;
13562         }
13563         EM_DEBUG_LOG("Mailbox count = %d", *count);
13564
13565         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13566
13567         /* 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); */
13568         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);
13569
13570         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13571
13572
13573         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13574
13575
13576         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13577                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13578
13579
13580         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13581         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13582                         ("sqlite3_step fail:%d", rc));
13583
13584         mbox_list = (int *)em_malloc(sizeof(int) * (*count));
13585         if (NULL == mbox_list) {
13586                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
13587                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13588                 goto FINISH_OFF;
13589         }
13590
13591         memset(mbox_list, 0x00, sizeof(int) * (*count));
13592
13593         for (i = 0; i < (*count); i++) {
13594                 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
13595                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13596                 /* EM_DEBUG_LOG("In emstorage_get_pdb_mailbox_list() loop, After sqlite3_step(), , i = %d, rc = %d.", i,  rc); */
13597                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13598                                 ("sqlite3_step fail:%d", rc));
13599                 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
13600         }
13601
13602         ret = true;
13603
13604 FINISH_OFF:
13605         if (ret == true)
13606                 *mailbox_list = mbox_list;
13607         else
13608                 EM_SAFE_FREE(mbox_list);
13609
13610         if (hStmt != NULL) {
13611                 rc = sqlite3_finalize(hStmt);
13612                 hStmt = NULL;
13613                 if (rc != SQLITE_OK) {
13614                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13615                         error = EMAIL_ERROR_DB_FAILURE;
13616                 }
13617         }
13618
13619         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13620         if (err_code != NULL)
13621                 *err_code = error;
13622         EM_DEBUG_FUNC_END("ret [%d]", ret);
13623         return ret;
13624 }
13625
13626 INTERNAL_FUNC int emstorage_get_pbd_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
13627 {
13628         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13629
13630         if (NULL == account_list || NULL == count) {
13631                 EM_DEBUG_EXCEPTION("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13632                 if (err_code != NULL)
13633                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13634                 return false;
13635         }
13636
13637         int ret = false;
13638         int error = EMAIL_ERROR_NONE;
13639         char *sql = "SELECT count(distinct account_id) FROM mail_partial_body_activity_tbl";
13640         char **result;
13641         int i = 0, rc = -1;
13642         int *result_account_list = NULL;
13643         DB_STMT hStmt = NULL;
13644         char sql_query_string[QUERY_SIZE] = {0, };
13645
13646         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13647
13648         EMSTORAGE_START_READ_TRANSACTION(transaction);
13649
13650         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13651         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13652                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13653
13654         *count = atoi(result[1]);
13655         sqlite3_free_table(result);
13656
13657         if (*count <= 0) {
13658                 EM_DEBUG_EXCEPTION("no account found...");
13659                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13660                 ret = true;
13661                 goto FINISH_OFF;
13662         }
13663
13664         EM_DEBUG_LOG("Account count [%d]", *count);
13665
13666         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13667
13668         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_partial_body_activity_tbl");
13669
13670         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13671
13672
13673         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13674
13675         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
13676         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13677                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13678
13679
13680         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13681         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13682                         ("sqlite3_step fail:%d", rc));
13683
13684         if (NULL == (result_account_list = (int *)em_malloc(sizeof(int) * (*count)))) {
13685                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
13686                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13687                 goto FINISH_OFF;
13688         }
13689
13690         memset(result_account_list, 0x00, sizeof(int) * (*count));
13691
13692         for (i = 0; i < (*count); i++) {
13693                 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
13694
13695                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13696                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13697                                 ("sqlite3_step fail:%d", rc));
13698                 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
13699         }
13700
13701         ret = true;
13702
13703 FINISH_OFF:
13704         if (ret == true)
13705                 *account_list = result_account_list;
13706         else
13707                 EM_SAFE_FREE(result_account_list);
13708
13709         if (hStmt != NULL) {
13710                 rc = sqlite3_finalize(hStmt);
13711                 hStmt = NULL;
13712                 if (rc != SQLITE_OK) {
13713                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13714                         error = EMAIL_ERROR_DB_FAILURE;
13715                 }
13716         }
13717
13718         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13719         if (err_code != NULL)
13720                 *err_code = error;
13721         EM_DEBUG_FUNC_END("ret [%d]", ret);
13722         return ret;
13723 }
13724
13725 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)
13726 {
13727         EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
13728
13729         if (account_id < FIRST_ACCOUNT_ID || NULL == event_start || 0 == input_mailbox_id || NULL == count) {
13730                 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);
13731
13732                 if (err_code != NULL)
13733                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13734                 return false;
13735         }
13736
13737         int rc = -1;
13738         int ret = false;
13739         char **result;
13740         int error = EMAIL_ERROR_NONE;
13741         int i = 0;
13742         DB_STMT hStmt = NULL;
13743         email_event_partial_body_thd* event_list = NULL;
13744         char sql_query_string[QUERY_SIZE] = {0, };
13745
13746         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13747
13748         EMSTORAGE_START_READ_TRANSACTION(transaction);
13749
13750         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13751         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);
13752
13753
13754         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13755         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13756                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13757
13758         *count = atoi(result[1]);
13759         sqlite3_free_table(result);
13760
13761         EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
13762
13763         if (*count <= 0) {
13764                 EM_DEBUG_LOG("No matched activity found in mail_partial_body_activity_tbl");
13765                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
13766                 ret = true;
13767                 goto FINISH_OFF;
13768         }
13769         EM_DEBUG_LOG("Activity Count = %d", *count);
13770
13771         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13772         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);
13773
13774         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13775
13776
13777         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13778
13779         EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
13780         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13781                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13782
13783
13784         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13785         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13786                         ("sqlite3_step fail:%d", rc));
13787
13788         if (!(event_list = (email_event_partial_body_thd*)em_malloc(sizeof(email_event_partial_body_thd) * (*count)))) {
13789                 EM_DEBUG_EXCEPTION("Malloc failed");
13790
13791                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13792                 goto FINISH_OFF;
13793         }
13794         memset(event_list, 0x00, sizeof(email_event_partial_body_thd) * (*count));
13795
13796         for (i = 0; i < (*count); i++) {
13797                 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_IDX_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13798                 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13799                 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13800                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13801                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_type), ACTIVITY_TYPE_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13802                 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13803                 _get_stmt_field_data_string(hStmt, &(event_list[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13804                 _get_stmt_field_data_string(hStmt, &(event_list[i].multi_user_name), 0, MULTI_USER_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13805
13806                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13807                 /* EM_DEBUG_LOG("In emstorage_get_pbd_activity_data() loop, After sqlite3_step(), , i = %d, rc = %d.", i,  rc); */
13808                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13809                                 ("sqlite3_step fail:%d", rc));
13810
13811                 event_list[i].event_type = 0;
13812         }
13813
13814         ret = true;
13815
13816 FINISH_OFF:
13817         if (true == ret)
13818                 *event_start = event_list;
13819         else {
13820                 for (i = 0; i < (*count); i++)
13821                         emcore_free_partial_body_thd_event(event_list, NULL);
13822                 EM_SAFE_FREE(event_list); /*prevent 54559*/
13823                 *event_start = NULL;
13824                 *count = 0;
13825         }
13826
13827         if (hStmt != NULL) {
13828                 rc = sqlite3_finalize(hStmt);
13829                 hStmt = NULL;
13830                 if (rc != SQLITE_OK) {
13831                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13832                         error = EMAIL_ERROR_DB_FAILURE;
13833                 }
13834         }
13835
13836         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13837         if (err_code != NULL)
13838                 *err_code = error;
13839
13840         EM_DEBUG_FUNC_END("ret [%d]", ret);
13841         return ret;
13842 }
13843
13844 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)
13845 {
13846         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);
13847
13848
13849         if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
13850                 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);
13851
13852                 if (err_code != NULL)
13853                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13854                 return false;
13855         }
13856
13857         int rc = -1;
13858         int ret = false;
13859         int error = EMAIL_ERROR_NONE;
13860         char sql_query_string[QUERY_SIZE] = {0, };
13861         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13862
13863         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13864         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13865
13866         if (activity_id == 0)
13867                 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);
13868         else
13869                 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);
13870
13871         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13872         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
13873         if (error != EMAIL_ERROR_NONE) {
13874                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
13875                 goto FINISH_OFF;
13876         }
13877
13878         /*  validate activity existence */
13879         rc = sqlite3_changes(local_db_handle);
13880         if (rc == 0) {
13881                 EM_DEBUG_EXCEPTION("No matching activity found");
13882                 error = EMAIL_ERROR_DATA_NOT_FOUND;
13883                 ret = true;
13884                 goto FINISH_OFF;
13885         }
13886
13887         ret = true;
13888
13889 FINISH_OFF:
13890         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13891
13892         if (err_code != NULL)
13893                 *err_code = error;
13894
13895         EM_DEBUG_FUNC_END("ret [%d]", ret);
13896         return ret;
13897 }
13898
13899 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)
13900 {
13901         EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
13902
13903         if (account_id < FIRST_ACCOUNT_ID || NULL == activity_count || NULL == err_code) {
13904                 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
13905                 if (err_code != NULL)
13906                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13907                 return false;
13908         }
13909         int rc = -1;
13910         int ret = false;
13911         int error = EMAIL_ERROR_NONE;
13912         char sql_query_string[QUERY_SIZE] = {0, };
13913
13914         DB_STMT hStmt = NULL;
13915
13916         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13917
13918         EMSTORAGE_START_READ_TRANSACTION(transaction);
13919         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13920
13921         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);
13922
13923         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13924
13925
13926         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13927         EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
13928         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13929                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13930
13931
13932         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13933         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13934                         ("sqlite3_step fail:%d", rc));
13935
13936         _get_stmt_field_data_int(hStmt, activity_count, 0);
13937
13938         EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
13939
13940         ret = true;
13941
13942 FINISH_OFF:
13943
13944         if (hStmt != NULL) {
13945                 rc = sqlite3_finalize(hStmt);
13946                 hStmt = NULL;
13947                 if (rc != SQLITE_OK) {
13948                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13949                         error = EMAIL_ERROR_DB_FAILURE;
13950                 }
13951         }
13952
13953         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13954
13955         if (err_code != NULL)
13956                 *err_code = error;
13957
13958         EM_DEBUG_FUNC_END("ret [%d]", ret);
13959         return ret;
13960 }
13961
13962 INTERNAL_FUNC int emstorage_get_pbd_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
13963 {
13964         EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
13965
13966         if (NULL == activity_count || NULL == err_code) {
13967                 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
13968                 if (err_code != NULL)
13969                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13970                 return false;
13971         }
13972         int rc = -1;
13973         int ret = false;
13974         int error = EMAIL_ERROR_NONE;
13975         DB_STMT hStmt = NULL;
13976         char sql_query_string[QUERY_SIZE] = {0, };
13977
13978         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13979
13980         EMSTORAGE_START_READ_TRANSACTION(transaction);
13981         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13982
13983         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl;");
13984
13985         EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
13986
13987         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13988         EM_DEBUG_LOG_DEV("  before sqlite3_prepare hStmt = %p", hStmt);
13989         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13990                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13991
13992         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13993         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13994                         ("sqlite3_step fail:%d", rc));
13995
13996         _get_stmt_field_data_int(hStmt, activity_count, 0);
13997
13998         EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
13999
14000         ret = true;
14001
14002 FINISH_OFF:
14003
14004
14005         if (hStmt != NULL) {
14006                 rc = sqlite3_finalize(hStmt);
14007                 hStmt = NULL;
14008                 if (rc != SQLITE_OK) {
14009                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14010                         error = EMAIL_ERROR_DB_FAILURE;
14011                 }
14012         }
14013
14014         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14015         if (err_code != NULL)
14016                 *err_code = error;
14017
14018         EM_DEBUG_FUNC_END("ret [%d]", ret);
14019         return ret;
14020 }
14021
14022 INTERNAL_FUNC int emstorage_delete_full_pbd_activity_data(char *multi_user_name, int account_id, int transaction, int *err_code)
14023 {
14024         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
14025         if (account_id < FIRST_ACCOUNT_ID) {
14026                 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
14027                 if (err_code != NULL)
14028                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14029                 return false;
14030         }
14031
14032         int rc = -1;
14033         int ret = false;
14034         int error = EMAIL_ERROR_NONE;
14035         char sql_query_string[QUERY_SIZE] = {0, };
14036
14037         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14038
14039         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14040         memset(sql_query_string, 0x00, sizeof(sql_query_string));
14041         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d", account_id);
14042
14043         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14044         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14045         if (error != EMAIL_ERROR_NONE) {
14046                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14047                 goto FINISH_OFF;
14048         }
14049
14050         rc = sqlite3_changes(local_db_handle);
14051         if (rc == 0) {
14052                 EM_DEBUG_EXCEPTION("No matching activities found in mail_partial_body_activity_tbl");
14053                 error = EMAIL_ERROR_DATA_NOT_FOUND;
14054                 ret = true;
14055                 goto FINISH_OFF;
14056         }
14057
14058         ret = true;
14059
14060 FINISH_OFF:
14061
14062         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14063
14064         if (err_code != NULL)
14065                 *err_code = error;
14066
14067         EM_DEBUG_FUNC_END("ret [%d]", ret);
14068         return ret;
14069 }
14070
14071 /*Himanshu[h.gahlaut]-> Added below API to update mail_partial_body_activity_tbl
14072   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*/
14073
14074 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)
14075 {
14076         EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
14077
14078         int rc = -1, ret = false;
14079         int error = EMAIL_ERROR_NONE;
14080         char sql_query_string[QUERY_SIZE] = {0, };
14081
14082         int transaction = true;
14083
14084         if (!old_server_uid || !new_server_uid || !mbox_name) {
14085                 EM_DEBUG_EXCEPTION("Invalid parameters");
14086                 error = EMAIL_ERROR_INVALID_PARAM;
14087                 return false;
14088         }
14089
14090         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14091
14092         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14093         memset(sql_query_string, 0x00, sizeof(sql_query_string));
14094         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14095                         "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);
14096
14097         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14098         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14099         if (error != EMAIL_ERROR_NONE) {
14100                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14101                 goto FINISH_OFF;
14102         }
14103
14104         rc = sqlite3_changes(local_db_handle);
14105         if (rc == 0)
14106                 EM_DEBUG_LOG("No matching found in mail_partial_body_activity_tbl");
14107
14108         ret = true;
14109
14110 FINISH_OFF:
14111         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14112
14113         if (err_code != NULL)
14114                 *err_code = error;
14115
14116         EM_DEBUG_FUNC_END("ret [%d]", ret);
14117         return ret;
14118 }
14119
14120
14121 INTERNAL_FUNC int emstorage_create_file(char *data_string, size_t file_size, char *dst_file_name, int *err_code)
14122 {
14123         EM_DEBUG_FUNC_BEGIN_SEC("file_size[%d] , dst_file_name[%s], err_code[%p]", file_size, dst_file_name, err_code);
14124
14125         int ret = false;
14126         int error = EMAIL_ERROR_NONE;
14127         FILE* fp_dst = NULL;
14128         char errno_buf[ERRNO_BUF_SIZE] = {0};
14129
14130         if (!data_string || !dst_file_name) {
14131                 EM_DEBUG_LOG("data_string[%p], dst_file_name[%p]", data_string, dst_file_name);
14132                 error = EMAIL_ERROR_INVALID_PARAM;
14133                 goto FINISH_OFF;
14134         }
14135
14136         error = em_fopen(dst_file_name, "w", &fp_dst);
14137         if (error != EMAIL_ERROR_NONE) {
14138                 EM_DEBUG_EXCEPTION_SEC("em_fopen failed - %s: %d", dst_file_name, error);
14139                 goto FINISH_OFF;
14140         }
14141
14142         if (fwrite(data_string, 1, file_size, fp_dst) == 0) {
14143                 EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
14144                 error = EMAIL_ERROR_UNKNOWN;
14145                 goto FINISH_OFF;
14146         }
14147
14148         ret = true;
14149
14150 FINISH_OFF:
14151
14152         if (fp_dst != NULL)
14153                 fclose(fp_dst);
14154
14155         if (err_code != NULL)
14156                 *err_code = error;
14157
14158         EM_DEBUG_FUNC_END("ret [%d]", ret);
14159         return ret;
14160 }
14161
14162 #endif /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
14163
14164
14165
14166 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
14167 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)
14168 {
14169         EM_DEBUG_FUNC_BEGIN();
14170         int rc = -1;
14171         int ret = false;
14172         int error = EMAIL_ERROR_NONE;
14173         char sql_query_string[QUERY_SIZE] = {0, };
14174
14175         int transaction = true;
14176
14177         if (!old_server_uid || !new_server_uid || !mbox_name) {
14178                 EM_DEBUG_EXCEPTION("Invalid parameters");
14179                 if (err_code != NULL)
14180                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14181                 return false;
14182         }
14183
14184         EM_DEBUG_LOG_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
14185
14186         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14187         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14188
14189
14190         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14191                         "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);
14192
14193         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
14194         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14195         if (error != EMAIL_ERROR_NONE) {
14196                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14197                 goto FINISH_OFF;
14198         }
14199
14200         rc = sqlite3_changes(local_db_handle);
14201         if (rc == 0) {
14202                 EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
14203                 error = EMAIL_ERROR_DATA_NOT_FOUND;
14204                 goto FINISH_OFF;
14205         }
14206
14207         ret = true;
14208
14209 FINISH_OFF:
14210         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14211
14212         if (err_code != NULL)
14213                 *err_code = error;
14214
14215         EM_DEBUG_FUNC_END("ret [%d]", ret);
14216         return ret;
14217
14218 }
14219
14220
14221 /**
14222  * @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);
14223  * Prepare an array of mail_id and corresponding server mail id.
14224  *
14225  * @author                                      h.gahlaut@samsung.com
14226  * @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)
14227  *                                                      where 88 is the length of fixed keywords including ending null character in the QUERY to be formed
14228  * @param[out] idset                    Returns the array of mail_id and corresponding server_mail_id sorted by server_mail_ids
14229  * @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
14230  * @param[out] err_code         Returns the error code.
14231  * @remarks                                     An Example of Query to be exexuted in this API:
14232  *                                                      SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (12, 13, 56, 78);
14233  * @return This function returns true on success or false on failure.
14234  */
14235 INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *multi_user_name,
14236                 char *mail_ids,
14237                 email_id_set_t** idset,
14238                 int *id_set_count,
14239                 int *err_code)
14240 {
14241         EM_DEBUG_FUNC_BEGIN();
14242         EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds);
14243
14244         int error = EMAIL_ERROR_NONE;
14245         int ret = false;
14246         email_id_set_t* p_id_set = NULL;
14247         int count = 0;
14248         const int buf_size = QUERY_SIZE;
14249         char sql_query_string[QUERY_SIZE] = {0, };
14250         int space_left_in_query_buffer = buf_size;
14251         int i = 0;
14252         int rc = -1;
14253         char *server_mail_id = NULL;
14254         char **result = NULL;
14255         int col_index = 0;
14256
14257
14258         if (NULL == mail_ids || NULL == idset || NULL == id_set_count) {
14259                 EM_DEBUG_EXCEPTION("Invalid Parameters mail_ids[%p] idset[%p] id_set_count [%p]",
14260                                 mail_ids, idset, id_set_count);
14261                 if (err_code != NULL)
14262                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14263                 return false;
14264         }
14265
14266         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14267
14268         SNPRINTF(sql_query_string, space_left_in_query_buffer,
14269                         "SELECT local_uid, server_uid FROM mail_read_mail_uid_tbl WHERE local_uid in (%s) ORDER BY server_uid",
14270                         mail_ids);
14271
14272         EM_DEBUG_LOG_SEC("SQL Query formed [%s] ", sql_query_string);
14273
14274         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14275         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14276         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result);
14277                         goto FINISH_OFF; },     ("SQL(%s) sqlite3_get_table fail:%d -%s",
14278                                 sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14279
14280         EM_DEBUG_LOG(" Count of mails [%d ]", count);
14281
14282         if (count <= 0) {
14283                 EM_DEBUG_EXCEPTION("Can't find proper mail");
14284                 error = EMAIL_ERROR_DATA_NOT_FOUND;
14285                 goto FINISH_OFF;
14286         }
14287
14288         if (NULL == (p_id_set = (email_id_set_t*)em_malloc(sizeof(email_id_set_t) * count))) {
14289                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
14290                 error = EMAIL_ERROR_OUT_OF_MEMORY;
14291                 goto FINISH_OFF;
14292         }
14293
14294         col_index = 2;
14295
14296         EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds_Loop);
14297         EM_DEBUG_LOG(">>>> DATA ASSIGN START");
14298         for (i = 0; i < count; i++) {
14299                 _get_table_field_data_int(result, &(p_id_set[i].mail_id), col_index++);
14300                 _get_table_field_data_string(result, &server_mail_id, 1, col_index++);
14301                 if (server_mail_id) {
14302                         p_id_set[i].server_mail_id = strtoul(server_mail_id, NULL, 10);
14303                         EM_SAFE_FREE(server_mail_id);
14304                 }
14305         }
14306         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d]", count);
14307         EM_PROFILE_END(EmStorageGetIdSetFromMailIds_Loop);
14308
14309         sqlite3_free_table(result);
14310         result = NULL;
14311
14312         ret = true;
14313
14314 FINISH_OFF:
14315
14316         if (ret == true) {
14317                 *idset = p_id_set;
14318                 *id_set_count = count;
14319                 EM_DEBUG_LOG(" idset[%p] id_set_count [%d]", *idset, *id_set_count);
14320         } else
14321                 EM_SAFE_FREE(p_id_set);
14322
14323
14324         if (err_code != NULL)
14325                 *err_code = error;
14326
14327         EM_PROFILE_END(EmStorageGetIdSetFromMailIds);
14328
14329         EM_DEBUG_FUNC_END("ret [%d]", ret);
14330         return ret;
14331 }
14332
14333
14334
14335 #endif
14336
14337 INTERNAL_FUNC int emstorage_delete_triggers_from_lucene(char *multi_user_name)
14338 {
14339         EM_DEBUG_FUNC_BEGIN();
14340         int ret = true, transaction = true;
14341         int error = EMAIL_ERROR_NONE;
14342         char sql_query_string[QUERY_SIZE] = {0, };
14343         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14344         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14345
14346         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerDelete;");
14347         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14348         if (error != EMAIL_ERROR_NONE) {
14349                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14350                 goto FINISH_OFF;
14351         }
14352
14353         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerInsert;");
14354         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14355         if (error != EMAIL_ERROR_NONE) {
14356                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14357                 goto FINISH_OFF;
14358         }
14359
14360         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerUpdate;");
14361         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14362         if (error != EMAIL_ERROR_NONE) {
14363                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14364                 goto FINISH_OFF;
14365         }
14366
14367         ret = true;
14368
14369 FINISH_OFF:
14370         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14371
14372         EM_DEBUG_FUNC_END("ret [%d]", ret);
14373         return ret;
14374 }
14375
14376 INTERNAL_FUNC int emstorage_update_tag_id(char *multi_user_name, int old_filter_id, int new_filter_id, int *err_code)
14377 {
14378         EM_DEBUG_FUNC_BEGIN("new_filter_id[%d], old_filter_id[%d]", new_filter_id, old_filter_id);
14379         int ret = false;
14380         int error = EMAIL_ERROR_NONE;
14381         char sql_query_string[QUERY_SIZE] = {0, };
14382         int transaction = true;
14383
14384         if (old_filter_id < 0 || new_filter_id < 0) {
14385                 EM_DEBUG_EXCEPTION("Invalid parameters");
14386                 if (err_code != NULL)
14387                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14388                 return false;
14389         }
14390
14391         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14392
14393         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14394
14395         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14396                         "UPDATE mail_tbl SET tag_id=%d WHERE tag_id=%d ", new_filter_id, old_filter_id);
14397
14398         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14399         if (error != EMAIL_ERROR_NONE) {
14400                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14401                 goto FINISH_OFF;
14402         }
14403
14404         ret = true;
14405
14406 FINISH_OFF:
14407         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14408
14409         if (err_code != NULL)
14410                 *err_code = error;
14411
14412         EM_DEBUG_FUNC_END("ret [%d]", ret);
14413         return ret;
14414 }
14415
14416 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)
14417 {
14418         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);
14419
14420         if ((account_id < 0) || (dest_mailbox_id < 0) || (!rule)) {
14421                 EM_DEBUG_EXCEPTION("Invalid Parameter");
14422
14423                 if (err_code != NULL)
14424                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14425                 return false;
14426         }
14427
14428         int rc = -1, ret = false, error = EMAIL_ERROR_NONE;
14429         int count = 0, col_index = 0, i = 0, where_pararaph_length = 0, *mail_list = NULL;
14430         int tag_id = rule->rule_id;
14431         char **result = NULL, *where_pararaph = NULL;
14432         char sql_query_string[QUERY_SIZE] = {0, };
14433         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14434
14435         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl ");
14436
14437         where_pararaph_length = EM_SAFE_STRLEN(rule->value) + 2 * (EM_SAFE_STRLEN(rule->value2)) + 100;
14438         where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
14439         if (where_pararaph == NULL) {
14440                 EM_DEBUG_EXCEPTION("malloc failed for where_pararaph.");
14441                 error = EMAIL_ERROR_OUT_OF_MEMORY;
14442                 goto FINISH_OFF;
14443         }
14444
14445         if (account_id != ALL_ACCOUNT)
14446                 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (0)", account_id);
14447         else
14448                 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_type NOT in (0)");
14449
14450         if (rule->type & EMAIL_FILTER_SUBJECT) {
14451                 if (rule->flag2 == RULE_TYPE_INCLUDES)
14452                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject like \'%%%q%%\'", rule->value);
14453                 else /*  RULE_TYPE_EXACTLY */
14454                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject = \'%q\'", rule->value);
14455         }
14456
14457         if (rule->type & EMAIL_FILTER_FROM) {
14458                 if (rule->flag2 == RULE_TYPE_INCLUDES)
14459                         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);
14460 #ifdef __FEATURE_COMPARE_DOMAIN__
14461                 else if (rule->flag2 == RULE_TYPE_COMPARE_DOMAIN)
14462                         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);
14463 #endif /*__FEATURE_COMPARE_DOMAIN__ */
14464                 else /*  RULE_TYPE_EXACTLY */
14465                         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);
14466         }
14467
14468         if (rule->type == EMAIL_PRIORITY_SENDER) {
14469                 if (rule->flag2 == RULE_TYPE_INCLUDES)
14470                         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);
14471                 else /*  RULE_TYPE_EXACTLY */
14472                         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);
14473
14474                 tag_id = PRIORITY_SENDER_TAG_ID;
14475         }
14476
14477         /* prevent 34361 */
14478         if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14479                 EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14480         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14481
14482         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14483         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14484         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
14485                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14486
14487         EM_DEBUG_LOG("Count of mails [%d]", count);
14488
14489         if (count) {
14490                 mail_list = malloc(sizeof(int) * count);
14491                 if (mail_list == NULL) {
14492                         EM_DEBUG_EXCEPTION("malloc failed for mail_list.");
14493                         error = EMAIL_ERROR_OUT_OF_MEMORY;
14494                         goto FINISH_OFF;
14495                 }
14496
14497                 col_index = 1;
14498
14499                 for (i = 0; i < count; i++)
14500                         _get_table_field_data_int(result, &(mail_list[i]), col_index++);
14501
14502                 memset(sql_query_string, 0x00, QUERY_SIZE);
14503                 if (reset) {
14504                         switch (rule->action_type) {
14505                         case EMAIL_FILTER_MOVE:
14506                                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = 0 ");
14507                                 break;
14508                         case EMAIL_FILTER_BLOCK:
14509                         default:
14510                                 EM_DEBUG_LOG("Not support : action_type[%d]", rule->action_type);
14511                                 ret = true;
14512                                 goto FINISH_OFF;
14513                         }
14514                 } else {
14515                         switch (rule->action_type) {
14516                         case EMAIL_FILTER_MOVE:
14517                                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = %d ", tag_id);
14518                                 break;
14519                         case EMAIL_FILTER_BLOCK:
14520                                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET mailbox_id = %d, mailbox_type = %d ", dest_mailbox_id, dest_mailbox_type);
14521                                 break;
14522                         default:
14523                                 EM_DEBUG_LOG("Not support");
14524                                 ret = true;
14525                                 goto FINISH_OFF;
14526                         }
14527                 }
14528                 /* prevent 34361 */
14529                 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14530                         EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14531
14532                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14533                 if (error != EMAIL_ERROR_NONE) {
14534                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14535                         goto FINISH_OFF;
14536                 }
14537
14538 #ifdef __FEATURE_BODY_SEARCH__
14539                 /* Updating mail_text_tbl */
14540                 if (rule->action_type == EMAIL_FILTER_BLOCK) {
14541                         memset(sql_query_string, 0x00, QUERY_SIZE);
14542                         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_text_tbl SET mailbox_id = %d ", dest_mailbox_id);
14543                         if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14544                                 EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14545
14546                         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14547                         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14548                         if (error != EMAIL_ERROR_NONE) {
14549                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14550                                 goto FINISH_OFF;
14551                         }
14552                 }
14553 #endif
14554         }
14555
14556         ret = true;
14557
14558 FINISH_OFF:
14559
14560         if (ret) {
14561                 if (filtered_mail_id_list)
14562                         *filtered_mail_id_list = mail_list;
14563
14564                 if (count_of_mails)
14565                         *count_of_mails = count;
14566         } else
14567                 EM_SAFE_FREE(mail_list);
14568
14569         sqlite3_free_table(result);
14570         result = NULL;
14571
14572
14573         EM_SAFE_FREE(where_pararaph);
14574
14575         if (err_code != NULL)
14576                 *err_code = error;
14577
14578         EM_DEBUG_FUNC_END("ret [%d]", ret);
14579         return ret;
14580 }
14581
14582 #define EMAIL_SLOT_UNIT 25
14583 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)
14584 {
14585         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);
14586         int ret = false, err = EMAIL_ERROR_NONE;
14587         int where_pararaph_length = 0;
14588         char *where_pararaph = NULL;
14589         char sql_query_string[QUERY_SIZE] = {0, };
14590         int and = 0;
14591         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14592
14593         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
14594
14595         if (new_slot_size > 0)
14596                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = %d ", new_slot_size);
14597         else if (new_slot_size == 0)
14598                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", EMAIL_SLOT_UNIT);
14599         else
14600                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", new_slot_size * -1);
14601
14602
14603         if (mailbox_id)
14604                 where_pararaph_length = 80;
14605         else
14606                 where_pararaph_length = 50;
14607
14608         if (new_slot_size == 0)
14609                 where_pararaph_length += 70;
14610
14611         where_pararaph = malloc(sizeof(char) * where_pararaph_length);
14612         if (where_pararaph == NULL) {
14613                 EM_DEBUG_EXCEPTION("Memory allocation failed for where_pararaph");
14614                 err = EMAIL_ERROR_OUT_OF_MEMORY;
14615                 goto FINISH_OFF;
14616         }
14617         memset(where_pararaph, 0x00, where_pararaph_length);
14618
14619         if (account_id > ALL_ACCOUNT) {
14620                 and = 1;
14621                 if (mailbox_id)
14622                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_id = %d ", mailbox_id);
14623                 else
14624                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d ", account_id);
14625         }
14626
14627         if (new_slot_size == 0)
14628                 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"));
14629
14630         if (strlen(sql_query_string) + EM_SAFE_STRLEN(where_pararaph) < QUERY_SIZE) /* prevent 34363 */
14631                 EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14632         else {
14633                 EM_DEBUG_EXCEPTION("Query buffer overflowed !!!");
14634                 err = EMAIL_ERROR_OUT_OF_MEMORY;
14635                 goto FINISH_OFF;
14636         }
14637
14638         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14639         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14640         if (err != EMAIL_ERROR_NONE) {
14641                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
14642                 goto FINISH_OFF;
14643         }
14644
14645         ret = true;
14646
14647 FINISH_OFF:
14648         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
14649
14650         EM_SAFE_FREE(where_pararaph);
14651
14652         if (err_code != NULL)
14653                 *err_code = err;
14654
14655         EM_DEBUG_FUNC_END("ret [%d]", ret);
14656         return ret;
14657 }
14658
14659 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)
14660 {
14661         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);
14662
14663         if (!meeting_req || meeting_req->mail_id <= 0) {
14664                 if (meeting_req)
14665                         EM_DEBUG_EXCEPTION("mail_id[%d]", meeting_req->mail_id);
14666
14667                 if (err_code != NULL)
14668                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14669
14670                 return false;
14671         }
14672
14673         int rc = -1;
14674         int ret = false;
14675         int error = EMAIL_ERROR_NONE;
14676         DB_STMT hStmt = NULL;
14677         char sql_query_string[QUERY_SIZE] = {0, };
14678         int col_index = 0;
14679         time_t temp_unix_time = 0;
14680
14681         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14682         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14683
14684         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14685                         "INSERT INTO mail_meeting_tbl VALUES "
14686                         "(?"            /*  mail_id */
14687                         ", ?"           /*  account_id */
14688                         ", ?"           /*  mailbox_id */
14689                         ", ?"           /*  meeting_response */
14690                         ", ?"           /*  start_time */
14691                         ", ?"           /*  end_time */
14692                         ", ?"           /*  location */
14693                         ", ?"           /*  global_object_id */
14694                         ", ?"           /*  offset */
14695                         ", ?"           /*  standard_name */
14696                         ", ?"           /*  standard_time_start_date */
14697                         ", ?"           /*  standard_biad */
14698                         ", ?"           /*  daylight_name */
14699                         ", ?"           /*  daylight_time_start_date */
14700                         ", ?"           /*  daylight_bias */
14701                         ")");
14702
14703         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14704         if (rc != SQLITE_OK) {
14705                 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
14706                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
14707
14708                 error = EMAIL_ERROR_DB_FAILURE;
14709                 goto FINISH_OFF;
14710         }
14711
14712         col_index = 0;
14713         /*
14714            EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14715            EM_DEBUG_LOG_SEC(">>>>> account_id[%d]", account_id);
14716            EM_DEBUG_LOG_SEC(">>>>> mailbox_name[%s]", mailbox_name);
14717            EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14718            EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14719            EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14720            EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14721            EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14722            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14723            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14724            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14725            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14726            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14727            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14728            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14729            */
14730         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->mail_id);
14731         _bind_stmt_field_data_int(hStmt, col_index++, account_id);
14732         _bind_stmt_field_data_int(hStmt, col_index++, input_mailbox_id);
14733         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14734
14735         temp_unix_time = timegm(&(meeting_req->start_time));
14736         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14737         temp_unix_time = timegm(&(meeting_req->end_time));
14738         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14739
14740         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->location, 0, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14741         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->global_object_id, 0, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14742
14743         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14744         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.standard_name, 0, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14745         temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14746         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14747         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14748
14749         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.daylight_name, 0, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14750         temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14751         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14752         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
14753
14754
14755         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14756         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
14757                         ("sqlite3_step fail:%d", rc));
14758         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14759                         ("sqlite3_step fail:%d", rc));
14760
14761         ret = true;
14762
14763 FINISH_OFF:
14764         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14765         if (hStmt != NULL) {
14766                 rc = sqlite3_finalize(hStmt);
14767                 if (rc != SQLITE_OK) {
14768                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14769                         error = EMAIL_ERROR_DB_FAILURE;
14770                 }
14771         }
14772
14773         if (err_code != NULL)
14774                 *err_code = error;
14775
14776         EM_DEBUG_FUNC_END("ret [%d]", ret);
14777         return ret;
14778 }
14779
14780 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)
14781 {
14782         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);
14783
14784         int i = 0;
14785         int col_index = 0;
14786         int rc;
14787         int count = 0;
14788         int dummy = 0;
14789         int err = EMAIL_ERROR_NONE;
14790         char **result = NULL;
14791         char sql_query_string[QUERY_SIZE] = {0, };
14792         email_meeting_request_t* p_temp_meeting_req = NULL;
14793         sqlite3 *local_db_handle = NULL;
14794         time_t temp_unix_time;
14795
14796         if (conditional_clause == NULL || output_meeting_req == NULL || output_result_count == NULL) {
14797                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14798                 err = EMAIL_ERROR_INVALID_PARAM;
14799                 goto FINISH_OFF;
14800         }
14801
14802         col_index = _field_count_of_table[CREATE_TABLE_MAIL_MEETING_TBL];
14803         EM_DEBUG_LOG("col_index [%d]", col_index);
14804
14805         local_db_handle = emstorage_get_db_connection(multi_user_name);
14806
14807         EMSTORAGE_START_READ_TRANSACTION(transaction);
14808
14809         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_meeting_tbl %s", conditional_clause);
14810
14811         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14812
14813         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14814         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
14815                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14816
14817         if (!count) {
14818                 EM_DEBUG_EXCEPTION("No meeting_request found...");
14819                 err = EMAIL_ERROR_DATA_NOT_FOUND;
14820                 goto FINISH_OFF;
14821         }
14822
14823         EM_DEBUG_LOG("There are [%d] meeting requests.", count);
14824         if (!(p_temp_meeting_req = (email_meeting_request_t*)em_malloc(sizeof(email_meeting_request_t) * count))) {
14825                 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
14826                 err = EMAIL_ERROR_OUT_OF_MEMORY;
14827                 goto FINISH_OFF;
14828         }
14829
14830         EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
14831
14832         for (i = 0; i < count; i++) {
14833                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].mail_id), col_index++);
14834                 _get_table_field_data_int(result, &dummy, col_index++); /* account_id. but why should this field exist in DB table? */
14835                 _get_table_field_data_int(result, &dummy, col_index++); /* mailbox_id */
14836                 _get_table_field_data_int(result, (int*)&(p_temp_meeting_req[i].meeting_response), col_index++);
14837                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* start time */
14838                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].start_time));
14839                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* end time */
14840                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].end_time));
14841                 _get_table_field_data_string(result, &p_temp_meeting_req[i].location, 1, col_index++);
14842                 _get_table_field_data_string(result, &p_temp_meeting_req[i].global_object_id, 1, col_index++);
14843                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.offset_from_GMT), col_index++);
14844                 _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++);
14845                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14846                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.standard_time_start_date));
14847                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.standard_bias), col_index++);
14848                 _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++);
14849                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14850                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.daylight_time_start_date));
14851                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.daylight_bias), col_index++);
14852         }
14853
14854
14855 FINISH_OFF:
14856         if (result)
14857                 sqlite3_free_table(result);
14858
14859         if (err == EMAIL_ERROR_NONE) {
14860                 if (p_temp_meeting_req)
14861                         *output_meeting_req = p_temp_meeting_req;
14862                 *output_result_count = count;
14863         } else
14864                 EM_SAFE_FREE(p_temp_meeting_req);
14865
14866         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14867
14868         //      sqlite3_db_release_memory(local_db_handle);
14869
14870         EM_DEBUG_FUNC_END("err [%d]", err);
14871         return err;
14872 }
14873
14874 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)
14875 {
14876         EM_DEBUG_FUNC_BEGIN();
14877
14878         int count = 0;
14879         int ret = false;
14880         int error = EMAIL_ERROR_NONE;
14881         char conditional_clause[QUERY_SIZE] = {0, };
14882
14883         EM_IF_NULL_RETURN_VALUE(meeting_req, false);
14884
14885
14886         SNPRINTF(conditional_clause, QUERY_SIZE, " WHERE mail_id = %d", mail_id);
14887         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
14888
14889         if ((error = emstorage_query_meeting_request(multi_user_name, conditional_clause, meeting_req, &count, transaction)) != EMAIL_ERROR_NONE) {
14890                 EM_DEBUG_EXCEPTION("emstorage_query_meeting_request failed. [%d]", error);
14891                 goto FINISH_OFF;
14892         }
14893
14894         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->mail_id[%d]", (*meeting_req)->mail_id);
14895         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->meeting_response[%d]", (*meeting_req)->meeting_response);
14896         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->start_time[%s]", asctime(&((*meeting_req)->start_time)));
14897         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->end_time[%s]", asctime(&((*meeting_req)->end_time)));
14898         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->location[%s]", (*meeting_req)->location);
14899         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->global_object_id[%s]", (*meeting_req)->global_object_id);
14900         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.offset_from_GMT[%d]", (*meeting_req)->time_zone.offset_from_GMT);
14901         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_name[%s]", (*meeting_req)->time_zone.standard_name);
14902         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.standard_time_start_date)));
14903         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_bias[%d]", (*meeting_req)->time_zone.standard_bias);
14904         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_name[%s]", (*meeting_req)->time_zone.daylight_name);
14905         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.daylight_time_start_date)));
14906         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_bias[%d]", (*meeting_req)->time_zone.daylight_bias);
14907         ret = true;
14908
14909 FINISH_OFF:
14910
14911         if (err_code != NULL)
14912                 *err_code = error;
14913
14914         EM_DEBUG_FUNC_END("ret [%d]", ret);
14915         return ret;
14916 }
14917
14918 INTERNAL_FUNC int emstorage_update_meeting_request(char *multi_user_name, email_meeting_request_t* meeting_req, int transaction, int *err_code)
14919 {
14920         EM_DEBUG_FUNC_BEGIN("meeting_req[%p], transaction[%d], err_code[%p]", meeting_req, transaction, err_code);
14921
14922         int ret = false;
14923         int error = EMAIL_ERROR_NONE;
14924         int rc;
14925         DB_STMT hStmt = NULL;
14926         char sql_query_string[QUERY_SIZE] = {0, };
14927         time_t temp_unix_time = 0;
14928
14929         if (!meeting_req) {
14930                 EM_DEBUG_EXCEPTION("Invalid Parameter!");
14931                 if (err_code != NULL)
14932                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14933                 return false;
14934         }
14935
14936         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14937         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14938
14939         memset(sql_query_string, 0x00, sizeof(sql_query_string));
14940         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14941                         "UPDATE mail_meeting_tbl "
14942                         "SET "
14943                         /* "  account_id = ?, "         //  not update here, this can be changed when move or copy */
14944                         /* "  mailbox_name = ?, "               //  not update here, this can be changed when move or copy */
14945                         "  meeting_response = ?, "
14946                         "  start_time = ?, "
14947                         "  end_time = ?, "
14948                         "  location = ?, "
14949                         "  global_object_id = ?, "
14950                         "  offset = ?, "
14951                         "  standard_name = ?, "
14952                         "  standard_time_start_date = ?, "
14953                         "  standard_bias = ?, "
14954                         "  daylight_name = ?, "
14955                         "  daylight_time_start_date = ?, "
14956                         "  daylight_bias = ? "
14957                         "WHERE mail_id = %d",
14958                         meeting_req->mail_id);
14959
14960         EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
14961
14962         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14963         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14964                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14965         /*
14966            EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14967            EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14968            EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14969            EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14970            EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14971            EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14972            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14973            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14974            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14975            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14976            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14977            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14978            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14979            */
14980         int col_index = 0;
14981
14982         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14983         temp_unix_time = timegm(&(meeting_req->start_time));
14984         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14985         temp_unix_time = timegm(&(meeting_req->end_time));
14986         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14987         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->location, 1, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14988         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->global_object_id, 1, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14989         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14990         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.standard_name, 1, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14991         temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14992         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14993         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14994         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.daylight_name, 1, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14995         temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14996         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14997         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
14998
14999
15000         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15001         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15002                         ("sqlite3_step fail:%d", rc));
15003         ret = true;
15004
15005
15006 FINISH_OFF:
15007         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15008
15009         if (hStmt != NULL) {
15010                 rc = sqlite3_finalize(hStmt);
15011                 if (rc != SQLITE_OK) {
15012                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15013                         error = EMAIL_ERROR_DB_FAILURE;
15014                 }
15015         }
15016
15017         if (err_code != NULL)
15018                 *err_code = error;
15019
15020         EM_DEBUG_FUNC_END("ret [%d]", ret);
15021         return ret;
15022 }
15023
15024 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)
15025 {
15026         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);
15027
15028         if (account_id < ALL_ACCOUNT || mail_id < 0) {
15029                 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d]", account_id, mail_id);
15030
15031                 if (err_code != NULL)
15032                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15033                 return false;
15034         }
15035
15036         int ret = false;
15037         int error = EMAIL_ERROR_NONE;
15038         int and = false;
15039         char sql_query_string[QUERY_SIZE] = {0, };
15040
15041         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15042         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15043
15044         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_meeting_tbl ");
15045
15046         if (account_id != ALL_ACCOUNT) {                /*  NOT '0' means a specific account. '0' means all account */
15047                 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);
15048                 and = true;
15049         }
15050         if (mail_id > 0) {
15051                 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);
15052                 and = true;
15053         }
15054         if (input_mailbox_id > 0) {             /*  0 means all mailbox_id */
15055                 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);
15056         }
15057
15058         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15059         if (error != EMAIL_ERROR_NONE) {
15060                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
15061                 goto FINISH_OFF;
15062         }
15063
15064         ret = true;
15065
15066 FINISH_OFF:
15067         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15068
15069         if (err_code)
15070                 *err_code = error;
15071
15072         EM_DEBUG_FUNC_END("ret [%d]", ret);
15073         return ret;
15074 }
15075
15076
15077 INTERNAL_FUNC void emstorage_free_meeting_request(email_meeting_request_t *meeting_req)
15078 {
15079         EM_DEBUG_FUNC_BEGIN();
15080
15081         if (!meeting_req) return;
15082
15083         EM_SAFE_FREE(meeting_req->location);
15084         EM_SAFE_FREE(meeting_req->global_object_id);
15085
15086         EM_DEBUG_FUNC_END();
15087 }
15088
15089 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)
15090 {
15091         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);
15092         EM_PROFILE_BEGIN(profile_emstorage_get_overflowed_mail_id_list);
15093         char sql_query_string[QUERY_SIZE] = {0, };
15094         char **result = NULL;
15095         int rc = -1, ret = false;
15096         int error = EMAIL_ERROR_NONE;
15097         int counter = 0, col_index = 0;
15098         int result_mail_id_count = 0;
15099         int *result_mail_id_list = NULL;
15100
15101         if (input_mailbox_id <= 0 || !mail_id_list || !mail_id_count || account_id < 1) {
15102                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15103                 if (err_code)
15104                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15105                 return false;
15106         }
15107
15108         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);
15109
15110         EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
15111
15112         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15113         EMSTORAGE_START_READ_TRANSACTION(transaction);
15114
15115         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_mail_id_count, 0, NULL), rc);
15116         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15117                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15118
15119         if (!result_mail_id_count) {
15120                 EM_DEBUG_LOG("No mail found...");
15121                 ret = false;
15122                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
15123                 goto FINISH_OFF;
15124         }
15125
15126         EM_DEBUG_LOG("There are [%d] overflowed mails in mailbox_id [%d]", result_mail_id_count, input_mailbox_id);
15127
15128         if (!(result_mail_id_list = (int *)malloc(sizeof(int) * result_mail_id_count))) {
15129                 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
15130                 error = EMAIL_ERROR_OUT_OF_MEMORY;
15131                 sqlite3_free_table(result);
15132                 goto FINISH_OFF;
15133         }
15134
15135         memset(result_mail_id_list, 0x00, sizeof(int) * result_mail_id_count);
15136
15137         col_index = 1;
15138
15139         for (counter = 0; counter < result_mail_id_count; counter++)
15140                 _get_table_field_data_int(result, result_mail_id_list + counter, col_index++);
15141
15142         ret = true;
15143
15144 FINISH_OFF:
15145         EM_DEBUG_LOG("finish off [%d]", ret);
15146
15147         if (result)
15148                 sqlite3_free_table(result);
15149
15150         if (ret == true) {
15151                 *mail_id_list = result_mail_id_list;
15152                 *mail_id_count = result_mail_id_count;
15153         } else
15154                 EM_SAFE_FREE(result_mail_id_list);
15155
15156         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15157
15158         if (err_code != NULL)
15159                 *err_code = error;
15160
15161         EM_PROFILE_END(profile_emstorage_get_overflowed_mail_id_list);
15162         EM_DEBUG_FUNC_END("ret [%d]", ret);
15163         return ret;
15164 }
15165
15166 INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(char *multi_user_name, int mail_id, int *thread_id, int *err_code)
15167 {
15168         EM_DEBUG_FUNC_BEGIN("mail_id[%d], thread_id[%p], err_code[%p]", mail_id, thread_id, err_code);
15169
15170         int rc = -1, ret = false;
15171         int err = EMAIL_ERROR_NONE;
15172         char sql_query_string[QUERY_SIZE] = {0, };
15173         char **result;
15174         int result_count = 0;
15175
15176         if (mail_id == 0 || thread_id == NULL) {
15177                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15178                 if (err_code)
15179                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15180                 return false;
15181         }
15182
15183         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15184
15185         memset(sql_query_string, 0, QUERY_SIZE);
15186         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT thread_id FROM mail_tbl WHERE mail_id = %d", mail_id);
15187
15188         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15189         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15190         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15191                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15192
15193         if (!result_count) {
15194                 EM_DEBUG_LOG("No mail found...");
15195                 ret = false;
15196                 err = EMAIL_ERROR_MAIL_NOT_FOUND;
15197                 /* sqlite3_free_table(result); */
15198                 goto FINISH_OFF;
15199         }
15200
15201         _get_table_field_data_int(result, thread_id, 1);
15202
15203         sqlite3_free_table(result);
15204
15205         ret = true;
15206
15207 FINISH_OFF:
15208
15209         if (err_code != NULL)
15210                 *err_code = err;
15211
15212         EM_DEBUG_FUNC_END("ret [%d]", ret);
15213         return ret;
15214 }
15215
15216 INTERNAL_FUNC int emstorage_update_latest_thread_mail(char *multi_user_name,
15217                 int account_id,
15218                 int mailbox_id,
15219                 int mailbox_type,
15220                 int thread_id,
15221                 int *updated_thread_id,
15222                 int latest_mail_id,
15223                 int thread_item_count,
15224                 int noti_type,
15225                 int transaction,
15226                 int *err_code)
15227 {
15228         EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id [%d], thread_id[%d], updated_thread_id[%p], "
15229                         "latest_mail_id [%d], thread_item_count[%d], err_code[%p]",
15230                         account_id, mailbox_id, thread_id, updated_thread_id,
15231                         latest_mail_id, thread_item_count, err_code);
15232
15233         int rc = -1, ret = false;
15234         int err = EMAIL_ERROR_NONE;
15235         char sql_query_string[QUERY_SIZE] = {0, };
15236         char **result = NULL;
15237         int result_count = 0;
15238
15239         if (thread_id == 0) {
15240                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15241                 if (err_code)
15242                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15243                 return false;
15244         }
15245
15246         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15247
15248         if (thread_item_count == 0 && latest_mail_id == 0) {
15249                 memset(sql_query_string, 0, QUERY_SIZE);
15250                 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);
15251
15252                 /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15253                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15254                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15255                                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15256                 EM_DEBUG_LOG("result_count[%d]", result_count);
15257                 if (result_count == 0) {
15258                         EM_DEBUG_LOG("No mail found...");
15259                         ret = false;
15260                         if (err_code)
15261                                 *err_code =  EMAIL_ERROR_MAIL_NOT_FOUND;
15262                         sqlite3_free_table(result);
15263                         return false;
15264                 }
15265
15266                 _get_table_field_data_int(result, &latest_mail_id, 2);
15267                 _get_table_field_data_int(result, &thread_item_count, 3);
15268
15269                 EM_DEBUG_LOG("latest_mail_id[%d]", latest_mail_id);
15270                 EM_DEBUG_LOG("thread_item_count[%d]", thread_item_count);
15271
15272                 sqlite3_free_table(result);
15273         }
15274
15275         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15276
15277         if (thread_item_count < 0) {
15278                 memset(sql_query_string, 0, QUERY_SIZE);
15279                 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);
15280                 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15281                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15282                 if (err != EMAIL_ERROR_NONE) {
15283                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15284                         goto FINISH_OFF;
15285                 }
15286         } else if (thread_id != latest_mail_id) {
15287                 /* Initialize the thread id */
15288                 memset(sql_query_string, 0, QUERY_SIZE);
15289                 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);
15290                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15291                 if (err != EMAIL_ERROR_NONE) {
15292                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15293                         goto FINISH_OFF;
15294                 }
15295
15296                 /* update the thread item count */
15297                 memset(sql_query_string, 0, QUERY_SIZE);
15298                 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);
15299                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15300                 if (err != EMAIL_ERROR_NONE) {
15301                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15302                         goto FINISH_OFF;
15303                 }
15304         } else {
15305                 memset(sql_query_string, 0, QUERY_SIZE);
15306                 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);
15307                 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15308                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15309                 if (err != EMAIL_ERROR_NONE) {
15310                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15311                         goto FINISH_OFF;
15312                 }
15313         }
15314         ret = true;
15315
15316 FINISH_OFF:
15317         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15318
15319         if (thread_id != latest_mail_id) {
15320                 if (err == EMAIL_ERROR_NONE) {
15321                         EM_DEBUG_LOG("noti_type[%d]", noti_type);
15322
15323                         if (latest_mail_id > 0 && thread_id > 0 && noti_type > 0) {
15324                                 char mailbox_id_str[25] = {0,};
15325                                 snprintf(mailbox_id_str, sizeof(mailbox_id_str), "%d", mailbox_id);
15326                                 if (!emcore_notify_storage_event(noti_type, thread_id, latest_mail_id, mailbox_id_str, account_id))
15327                                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventfailed [NOTI_THREAD_ID_CHANGED] >>>> ");
15328
15329                                 if (updated_thread_id) *updated_thread_id = latest_mail_id;
15330                         }
15331                 }
15332         } else if (thread_item_count >= 0) {
15333                 if (err == EMAIL_ERROR_NONE) {
15334                         char parameter_string[500] = {0,};
15335                         SNPRINTF(parameter_string, sizeof(parameter_string), "%s%c%d", "thread_item_count", 0x01, latest_mail_id);
15336                         if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT, parameter_string, thread_item_count))
15337                                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventfailed [NOTI_MAIL_FIELD_UPDATE] >>>> ");
15338                 }
15339         }
15340
15341         if (err_code != NULL)
15342                 *err_code = err;
15343
15344         EM_DEBUG_FUNC_END("ret [%d]", ret);
15345         return ret;
15346 }
15347
15348 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)
15349 {
15350         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);
15351
15352         int ret = false;
15353         int err = EMAIL_ERROR_NONE;
15354         char sql_query_string[QUERY_SIZE] = {0, };
15355
15356         if (thread_id == 0) {
15357                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15358                 if (err_code)
15359                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15360                 return false;
15361         }
15362
15363         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15364
15365         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15366
15367         memset(sql_query_string, 0, QUERY_SIZE);
15368         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);
15369         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15370         if (err != EMAIL_ERROR_NONE) {
15371                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15372                 goto FINISH_OFF;
15373         }
15374
15375         ret = true;
15376
15377 FINISH_OFF:
15378         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15379
15380         if (err_code != NULL)
15381                 *err_code = err;
15382
15383         EM_DEBUG_FUNC_END("ret [%d]", ret);
15384         return ret;
15385 }
15386
15387 #ifdef __FEATURE_LOCAL_ACTIVITY__
15388 /**
15389  * emstorage_add_activity - Add Email Local activity during OFFLINE mode
15390  *
15391  */
15392 INTERNAL_FUNC int emstorage_add_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15393 {
15394         EM_DEBUG_FUNC_BEGIN();
15395
15396         EM_DEBUG_LOG(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15397
15398         int rc = -1, ret = false;
15399         int error = EMAIL_ERROR_NONE;
15400         DB_STMT hStmt = NULL;
15401         char sql_query_string[8192] = { 0x00, };
15402         int i = 0;
15403
15404         if (!local_activity) {
15405                 EM_DEBUG_EXCEPTION(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15406                 if (err_code != NULL)
15407                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15408                 return false;
15409         }
15410
15411         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15412
15413         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15414
15415         memset(sql_query_string, 0x00 , sizeof(sql_query_string));
15416         SNPRINTF(sql_query_string, sizeof(sql_query_string), "INSERT INTO mail_local_activity_tbl VALUES (?, ?, ?, ?, ?, ?, ?)");
15417
15418         EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ] ", local_activity->activity_id);
15419         EM_DEBUG_LOG(">>>>> MAIL ID [ %d ] ", local_activity->mail_id);
15420         EM_DEBUG_LOG(">>>>> ACCOUNT ID [ %d ] ", local_activity->account_id);
15421         EM_DEBUG_LOG(">>>>> ACTIVITY TYPE [ %d ] ", local_activity->activity_type);
15422         EM_DEBUG_LOG_SEC(">>>>> SERVER MAIL ID [ %s ] ", local_activity->server_mailid);
15423         EM_DEBUG_LOG(">>>>> SOURCE MAILBOX [ %s ] ", local_activity->src_mbox);
15424         EM_DEBUG_LOG(">>>>> DEST MAILBOX   [ %s ] ", local_activity->dest_mbox);
15425
15426         EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15427
15428
15429         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15430         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15431                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15432
15433         EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15434
15435         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
15436         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
15437         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
15438         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
15439         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->server_mailid, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
15440         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->src_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15441         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->dest_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15442
15443
15444         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15445
15446         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
15447                         ("sqlite3_step fail:%d", rc));
15448         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15449                         ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
15450
15451         ret = true;
15452
15453 FINISH_OFF:
15454         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15455
15456         if (hStmt != NULL) {
15457                 rc = sqlite3_finalize(hStmt);
15458                 if (rc != SQLITE_OK) {
15459                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15460                         error = EMAIL_ERROR_DB_FAILURE;
15461                 }
15462         }
15463
15464         if (err_code != NULL)
15465                 *err_code = error;
15466
15467         EM_DEBUG_FUNC_END("ret [%d]", ret);
15468         return ret;
15469 }
15470
15471 /**
15472  *      emstorage_get_activity - Get the Local activity Information
15473  *
15474  *
15475  */
15476 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)
15477 {
15478         EM_DEBUG_FUNC_BEGIN();
15479
15480         int i = 0, count = 0, rc = -1, ret = false;
15481         int error = EMAIL_ERROR_NONE;
15482         emstorage_activity_tbl_t *p_activity_tbl = NULL;
15483         char sql_query_string[1024] = {0x00, };
15484         char **result = NULL;
15485         int col_index ;
15486
15487         EM_IF_NULL_RETURN_VALUE(activity_list, false);
15488         EM_IF_NULL_RETURN_VALUE(select_num, false);
15489
15490
15491         if (!select_num || !activity_list || account_id <= 0 || activityid < 0) {
15492                 EM_DEBUG_LOG(" select_num[%p], activity_list[%p] account_id [%d] activityid [%d] ", select_num, activity_list, account_id, activityid);
15493                 if (err_code)
15494                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15495                 return false;
15496         }
15497
15498         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15499
15500         EMSTORAGE_START_READ_TRANSACTION(transaction);
15501
15502         memset(sql_query_string, 0x00, sizeof(sql_query_string));
15503
15504         if (activityid == ALL_ACTIVITIES) {
15505                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
15506                         "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d order by activity_id", account_id);
15507         } else {
15508                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
15509                         "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d AND activity_id = %d ", account_id, activityid);
15510         }
15511
15512         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
15513
15514
15515
15516         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15517         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15518                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15519
15520         col_index = 7;
15521
15522         if (!(p_activity_tbl = (emstorage_activity_tbl_t*)em_malloc(sizeof(emstorage_activity_tbl_t) * count))) {
15523                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
15524                 error = EMAIL_ERROR_OUT_OF_MEMORY;
15525                 goto FINISH_OFF;
15526         }
15527
15528
15529         for (i = 0; i < count; i++) {
15530                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15531                 if (result[col_index])
15532                         p_activity_tbl[i].activity_id = atoi(result[col_index++]);
15533
15534                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15535                 if (result[col_index])
15536                         p_activity_tbl[i].account_id = atoi(result[col_index++]);
15537
15538                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15539                 if (result[col_index])
15540                         p_activity_tbl[i].mail_id = atoi(result[col_index++]);
15541
15542                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15543                 if (result[col_index])
15544                         p_activity_tbl[i].activity_type = atoi(result[col_index++]);
15545
15546
15547                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15548                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15549                         p_activity_tbl[i].server_mailid = EM_SAFE_STRDUP(result[col_index++]);
15550                 else
15551                         col_index++;
15552
15553                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15554                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15555                         p_activity_tbl[i].src_mbox = EM_SAFE_STRDUP(result[col_index++]);
15556                 else
15557                         col_index++;
15558
15559                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15560                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15561                         p_activity_tbl[i].dest_mbox = EM_SAFE_STRDUP(result[col_index++]);
15562                 else
15563                         col_index++;
15564
15565         }
15566
15567         if (result)
15568                 sqlite3_free_table(result);
15569
15570         ret = true;
15571
15572 FINISH_OFF:
15573
15574         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15575
15576         if (ret == true) {
15577                 *activity_list = p_activity_tbl;
15578                 *select_num = count;
15579                 EM_DEBUG_LOG(">>>> COUNT : %d >> ", count);
15580         } else if (p_activity_tbl != NULL) {
15581                 emstorage_free_local_activity(&p_activity_tbl, count, NULL);
15582         }
15583
15584
15585         if (err_code != NULL)
15586                 *err_code = error;
15587
15588         EM_DEBUG_FUNC_END("ret [%d]", ret);
15589         return ret;
15590 }
15591
15592
15593 INTERNAL_FUNC int emstorage_get_next_activity_id(int *activity_id, int *err_code)
15594 {
15595
15596         EM_DEBUG_FUNC_BEGIN();
15597
15598         int ret = false;
15599         int err = EMAIL_ERROR_NONE;
15600         int rc = -1;
15601         char *sql = NULL;
15602         char **result = NULL;
15603
15604         if (NULL == activity_id) {
15605                 EM_DEBUG_EXCEPTION(" activity_id[%p]", activity_id);
15606                 if (err_code)
15607                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15608                 return false;
15609         }
15610
15611         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15612
15613         /*  increase unique id */
15614
15615         sql = "SELECT max(rowid) FROM mail_local_activity_tbl;";
15616
15617         /*  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); */
15618         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15619                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
15620
15621         if (NULL == result[1])
15622                 rc = 1;
15623         else
15624                 rc = atoi(result[1])+1;
15625
15626         *activity_id = rc;
15627
15628         if (result)
15629                 sqlite3_free_table(result);
15630
15631         ret = true;
15632
15633 FINISH_OFF:
15634
15635         if (err_code)
15636                 *err_code = err;
15637
15638         EM_DEBUG_FUNC_END("ret [%d]", ret);
15639         return ret;
15640
15641 }
15642
15643 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)
15644 {
15645
15646         EM_DEBUG_FUNC_BEGIN();
15647
15648         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);
15649
15650         if (account_id <= 0 || NULL == activity_id_list || NULL == activity_id_count || lowest_activity_type <= 0 || highest_activity_type <= 0) {
15651                 if (err_code != NULL)
15652                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15653                 return false;
15654         }
15655
15656         int ret = false;
15657         int error = EMAIL_ERROR_NONE;
15658         int i = 0, rc = -1, count = 0;
15659         char sql_query_string[1024] = {0x00, };
15660         int *activity_ids = NULL;
15661         int col_index = 0;
15662         char **result = NULL;
15663         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15664         EMSTORAGE_START_READ_TRANSACTION(transaction);
15665
15666         memset(sql_query_string, 0x00, sizeof(sql_query_string));
15667
15668         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);
15669
15670         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
15671
15672
15673         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15674         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15675                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15676
15677         col_index = 1;
15678
15679         EM_DEBUG_LOG(" Activity COUNT : %d ... ", count);
15680
15681         if (NULL == (activity_ids = (int *)em_malloc(sizeof(int) * count))) {
15682                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
15683                 error = EMAIL_ERROR_OUT_OF_MEMORY;
15684                 goto FINISH_OFF;
15685         }
15686
15687         for (i = 0; i < count; i++) {
15688                 activity_ids[i] = atoi(result[col_index]);
15689                 col_index++;
15690                 EM_DEBUG_LOG("activity_id %d", activity_ids[i]);
15691         }
15692
15693         ret = true;
15694
15695 FINISH_OFF:
15696
15697
15698         if (ret == true) {
15699                 *activity_id_count = count;
15700                 *activity_id_list = activity_ids;
15701         } else if (activity_ids != NULL) /* Prevent defect - 216566 */
15702                 EM_SAFE_FREE(activity_ids);
15703
15704
15705         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15706         if (err_code != NULL)
15707                 *err_code = error;
15708
15709         EM_DEBUG_FUNC_END("ret [%d]", ret);
15710         return ret;
15711 }
15712
15713 INTERNAL_FUNC int emstorage_free_activity_id_list(int *activity_id_list, int *error_code)
15714 {
15715         EM_DEBUG_FUNC_BEGIN();
15716
15717         int error = EMAIL_ERROR_NONE;
15718         int ret = false;
15719
15720         EM_DEBUG_LOG(" activity_id_list [%p]", activity_id_list);
15721
15722         if (NULL == activity_id_list) {
15723                 error = EMAIL_ERROR_INVALID_PARAM;
15724                 goto FINISH_OFF;
15725         } else {
15726                 EM_SAFE_FREE(activity_id_list);
15727         }
15728
15729
15730         ret = true;
15731
15732 FINISH_OFF:
15733
15734         if (NULL != error_code)
15735                 *error_code = error;
15736
15737         EM_DEBUG_FUNC_END("ret [%d]", ret);
15738         return ret;
15739 }
15740
15741 /**
15742  * emstorage_delete_local_activity - Deletes the Local acitivity Generated based on activity_type
15743  * or based on server mail id
15744  *
15745  */
15746 INTERNAL_FUNC int emstorage_delete_local_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15747 {
15748         EM_DEBUG_FUNC_BEGIN();
15749
15750
15751         EM_DEBUG_LOG(" local_activity[%p] ", local_activity);
15752
15753         if (!local_activity) {
15754                 EM_DEBUG_EXCEPTION(" local_activity[%p] ", local_activity);
15755                 if (err_code != NULL)
15756                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15757                 return false;
15758         }
15759
15760         int rc = -1, ret = false;                       /* Prevent_FIX  */
15761         int err = EMAIL_ERROR_NONE;
15762         int query_and = 0;
15763         int query_where = 0;
15764         char sql_query_string[8192] = { 0x00, };
15765         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15766         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15767
15768         memset(sql_query_string, 0x00, sizeof(sql_query_string));
15769
15770         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_local_activity_tbl ");
15771
15772         EM_DEBUG_LOG_SEC(">>> Query [ %s ] ", sql_query_string);
15773
15774         if (local_activity->account_id) {
15775                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15776                                 " WHERE account_id = %d ", local_activity->account_id);
15777                 query_and = 1;
15778                 query_where = 1;
15779         }
15780
15781         if (local_activity->server_mailid) {
15782                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15783                                 " %s %s server_mailid = '%s' ", query_where ? "" : "WHERE", query_and ? "AND" : "", local_activity->server_mailid);
15784                 query_and = 1;
15785                 query_where = 1;
15786         }
15787
15788
15789         if (local_activity->mail_id) {
15790                 EM_DEBUG_LOG(">>>> MAIL ID [ %d ] , ACTIVITY TYPE [%d ]", local_activity->mail_id, local_activity->activity_type);
15791
15792                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15793                                 " %s %s mail_id = %d  ", query_where ? "" : "WHERE", query_and ? "AND" : "", local_activity->mail_id);
15794
15795                 query_and = 1;
15796                 query_where = 1;
15797
15798         }
15799
15800         if (local_activity->activity_type > 0) {
15801                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15802                                 " %s %s activity_type = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "" , local_activity->activity_type);
15803         }
15804
15805         if (local_activity->activity_id > 0) {
15806                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15807                                 " %s %s activity_id = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "" , local_activity->activity_id);
15808
15809         }
15810
15811         EM_DEBUG_LOG_SEC(">>>>> Query [ %s ] ", sql_query_string);
15812         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15813         if (error != EMAIL_ERROR_NONE) {
15814                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
15815                 goto FINISH_OFF;
15816         }
15817
15818         rc = sqlite3_changes(local_db_handle);
15819         if (rc == 0) {
15820                 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
15821                 err = EMAIL_ERROR_MAILBOX_NOT_FOUND;
15822         }
15823
15824         ret = true;
15825
15826 FINISH_OFF:
15827         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15828
15829         if (hStmt != NULL) {
15830                 rc = sqlite3_finalize(hStmt);
15831                 if (rc != SQLITE_OK) {
15832                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15833                         error = EMAIL_ERROR_DB_FAILURE;
15834                 }
15835         }
15836
15837         if (err_code != NULL)
15838                 *err_code = err;
15839         EM_DEBUG_FUNC_END("ret [%d]", ret);
15840         return ret;
15841 }
15842
15843 /**
15844  *      emstorage_free_local_activity - Free the Local Activity data
15845  */
15846 INTERNAL_FUNC int emstorage_free_local_activity(emstorage_activity_tbl_t **local_activity_list, int count, int *err_code)
15847 {
15848         EM_DEBUG_FUNC_BEGIN();
15849
15850         EM_DEBUG_LOG(" local_activity_list[%p], count[%d], err_code[%p]", local_activity_list, count, err_code);
15851
15852         int ret = false;
15853         int error = EMAIL_ERROR_INVALID_PARAM;
15854
15855         if (count > 0) {
15856                 if (!local_activity_list || !*local_activity_list) {
15857                         EM_DEBUG_EXCEPTION(" local_activity_list[%p], count[%d]", local_activity_list, count);
15858
15859                         error = EMAIL_ERROR_INVALID_PARAM;
15860                         goto FINISH_OFF;
15861                 }
15862
15863                 emstorage_activity_tbl_t* p = *local_activity_list;
15864                 int i = 0;
15865                 if (p) {
15866                         for (; i < count; i++) {
15867                                 EM_SAFE_FREE(p[i].dest_mbox);
15868                                 EM_SAFE_FREE(p[i].src_mbox);
15869                                 EM_SAFE_FREE(p[i].server_mailid);
15870                         }
15871
15872                         EM_SAFE_FREE(p);
15873                         *local_activity_list = NULL;
15874                 }
15875         }
15876
15877         ret = true;
15878
15879 FINISH_OFF:
15880
15881         if (err_code != NULL)
15882                 *err_code = error;
15883
15884         EM_DEBUG_FUNC_END("ret [%d]", ret);
15885         return ret;
15886
15887 }
15888 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
15889
15890
15891 static int _get_key_value_string_for_list_filter_rule(email_list_filter_rule_t *input_list_filter_rule, char **output_key_value_string)
15892 {
15893         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_key_value_string [%p]", input_list_filter_rule, output_key_value_string);
15894
15895         int  ret = EMAIL_ERROR_NONE;
15896         char key_value_string[QUERY_SIZE] = { 0, };
15897         char *temp_key_value_1 = NULL;
15898         char *temp_key_value_2 = NULL;
15899
15900         if (input_list_filter_rule == NULL || output_key_value_string == NULL) {
15901                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15902                 return EMAIL_ERROR_INVALID_PARAM;
15903         }
15904
15905         switch (input_list_filter_rule->target_attribute) {
15906         case EMAIL_MAIL_ATTRIBUTE_MAIL_ID:
15907         case EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID:
15908         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID:
15909         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE:
15910         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_STATUS:
15911         case EMAIL_MAIL_ATTRIBUTE_REFERENCE_MAIL_ID:
15912         case EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS:
15913         case EMAIL_MAIL_ATTRIBUTE_MAIL_SIZE:
15914         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN:
15915         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML:
15916         case EMAIL_MAIL_ATTRIBUTE_FILE_SIZE:
15917         case EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD:
15918         case EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD:
15919         case EMAIL_MAIL_ATTRIBUTE_FLAGS_FLAGGED_FIELD:
15920         case EMAIL_MAIL_ATTRIBUTE_FLAGS_ANSWERED_FIELD:
15921         case EMAIL_MAIL_ATTRIBUTE_FLAGS_RECENT_FIELD:
15922         case EMAIL_MAIL_ATTRIBUTE_FLAGS_DRAFT_FIELD:
15923         case EMAIL_MAIL_ATTRIBUTE_FLAGS_FORWARDED_FIELD:
15924         case EMAIL_MAIL_ATTRIBUTE_DRM_STATUS:
15925         case EMAIL_MAIL_ATTRIBUTE_PRIORITY:
15926         case EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS:
15927         case EMAIL_MAIL_ATTRIBUTE_LOCK_STATUS:
15928         case EMAIL_MAIL_ATTRIBUTE_REPORT_STATUS:
15929         case EMAIL_MAIL_ATTRIBUTE_ATTACHMENT_COUNT:
15930         case EMAIL_MAIL_ATTRIBUTE_INLINE_CONTENT_COUNT:
15931         case EMAIL_MAIL_ATTRIBUTE_THREAD_ID:
15932         case EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT:
15933         case EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS:
15934         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_CLASS:
15935         case EMAIL_MAIL_ATTRIBUTE_DIGEST_TYPE:
15936         case EMAIL_MAIL_ATTRIBUTE_SMIME_TYPE:
15937         case EMAIL_MAIL_ATTRIBUTE_REMAINING_RESEND_TIMES:
15938         case EMAIL_MAIL_ATTRIBUTE_TAG_ID:
15939         case EMAIL_MAIL_ATTRIBUTE_EAS_DATA_LENGTH_TYPE:
15940                 SNPRINTF(key_value_string, QUERY_SIZE, "%d", input_list_filter_rule->key_value.integer_type_value);
15941                 break;
15942
15943         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
15944         case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
15945         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
15946         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
15947         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
15948         case EMAIL_MAIL_ATTRIBUTE_FROM:
15949         case EMAIL_MAIL_ATTRIBUTE_TO:
15950         case EMAIL_MAIL_ATTRIBUTE_CC:
15951         case EMAIL_MAIL_ATTRIBUTE_BCC:
15952         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
15953                 if (input_list_filter_rule->key_value.string_type_value == NULL) {
15954                         EM_DEBUG_EXCEPTION("Invalid string_type_value [%p]", input_list_filter_rule->key_value.string_type_value);
15955                         ret = EMAIL_ERROR_INVALID_PARAM;
15956                         goto FINISH_OFF;
15957                 }
15958
15959                 temp_key_value_1 = input_list_filter_rule->key_value.string_type_value;
15960
15961                 temp_key_value_2 = em_replace_all_string(temp_key_value_1, "_", "\\_");
15962                 temp_key_value_1 = em_replace_all_string(temp_key_value_2, "%", "\\%");
15963
15964                 if (input_list_filter_rule->rule_type == EMAIL_LIST_FILTER_RULE_INCLUDE)
15965                         SNPRINTF(key_value_string, QUERY_SIZE, "\'%%%s%%\'", temp_key_value_1);
15966                 else
15967                         SNPRINTF(key_value_string, QUERY_SIZE, "\'%s\'", temp_key_value_1);
15968                 break;
15969
15970         case EMAIL_MAIL_ATTRIBUTE_DATE_TIME:
15971         case EMAIL_MAIL_ATTRIBUTE_SCHEDULED_SENDING_TIME:
15972         case EMAIL_MAIL_ATTRIBUTE_REPLIED_TIME:
15973         case EMAIL_MAIL_ATTRIBUTE_FORWARDED_TIME:
15974                 SNPRINTF(key_value_string, QUERY_SIZE, "%d", (int)input_list_filter_rule->key_value.datetime_type_value);
15975                 break;
15976
15977         default:
15978                 ret = EMAIL_ERROR_INVALID_PARAM;
15979                 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
15980                 break;
15981         }
15982
15983         if (ret == EMAIL_ERROR_NONE && EM_SAFE_STRLEN(key_value_string) > 0)
15984                 *output_key_value_string = strdup(key_value_string);
15985
15986 FINISH_OFF:
15987
15988         EM_SAFE_FREE(temp_key_value_1);
15989         EM_SAFE_FREE(temp_key_value_2);
15990
15991         EM_DEBUG_FUNC_END("ret [%d]", ret);
15992         return ret;
15993 }
15994
15995 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15996 static int _get_cert_password_file_name(int index, char *cert_password_file_name)
15997 {
15998         EM_DEBUG_FUNC_BEGIN("index : [%d]", index);
15999
16000         if (index <= 0 || !cert_password_file_name) {
16001                 EM_DEBUG_EXCEPTION("Invalid parameter");
16002                 return EMAIL_ERROR_INVALID_PARAM;
16003         }
16004
16005         sprintf(cert_password_file_name, ".email_cert_%d", index);
16006
16007         EM_DEBUG_FUNC_END();
16008         return EMAIL_ERROR_NONE;
16009 }
16010 #endif
16011
16012 static int _make_filter_rule_string(email_list_filter_rule_t *input_list_filter_rule, char **output_string)
16013 {
16014         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16015
16016         int   ret = EMAIL_ERROR_NONE;
16017         int   is_alpha = 0;
16018         int   length_field_name = 0;
16019         int   length_value = 0;
16020         char  result_rule_string[QUERY_SIZE] = { 0 , };
16021         char *mod_field_name_string = NULL;
16022         char *mod_value_string = NULL;
16023         char *temp_field_name_string = NULL;
16024         char *temp_key_value_string = NULL;
16025
16026         if (input_list_filter_rule == NULL || output_string == NULL) {
16027                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16028                 return  EMAIL_ERROR_INVALID_PARAM;
16029         }
16030
16031         temp_field_name_string = emcore_get_mail_field_name_by_attribute_type(input_list_filter_rule->target_attribute);
16032
16033         if (temp_field_name_string == NULL) {
16034                 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
16035                 return EMAIL_ERROR_INVALID_PARAM;
16036         }
16037
16038         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) {
16039                 EM_DEBUG_EXCEPTION("_get_key_value_string_for_list_filter_rule failed");
16040                 return EMAIL_ERROR_INVALID_PARAM;
16041         }
16042
16043         length_field_name = EM_SAFE_STRLEN(temp_field_name_string);
16044         length_value      = EM_SAFE_STRLEN(temp_key_value_string);
16045
16046         switch (input_list_filter_rule->target_attribute) {
16047         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
16048         case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
16049         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
16050         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
16051         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
16052         case EMAIL_MAIL_ATTRIBUTE_FROM:
16053         case EMAIL_MAIL_ATTRIBUTE_TO:
16054         case EMAIL_MAIL_ATTRIBUTE_CC:
16055         case EMAIL_MAIL_ATTRIBUTE_BCC:
16056         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
16057                 is_alpha = 1;
16058                 break;
16059         default:
16060                 is_alpha = 0;
16061                 break;
16062         }
16063
16064         if (is_alpha == 1 && input_list_filter_rule->case_sensitivity == false) {
16065                 length_field_name += strlen("UPPER() ");
16066                 length_value      += strlen("UPPER() ");
16067                 mod_field_name_string = em_malloc(sizeof(char) * length_field_name);
16068                 if (mod_field_name_string == NULL) {
16069                         EM_DEBUG_EXCEPTION("em_mallocfailed");
16070                         EM_SAFE_FREE(temp_field_name_string);
16071                         ret = EMAIL_ERROR_OUT_OF_MEMORY;
16072                         goto FINISH_OFF;
16073                 }
16074
16075                 mod_value_string = em_malloc(sizeof(char) * length_value);
16076                 if (mod_value_string == NULL) {
16077                         EM_DEBUG_EXCEPTION("em_mallocfailed");
16078                         EM_SAFE_FREE(temp_field_name_string);
16079                         ret = EMAIL_ERROR_OUT_OF_MEMORY;
16080                         goto FINISH_OFF;
16081                 }
16082
16083                 SNPRINTF(mod_field_name_string, length_field_name, "UPPER(%s)", temp_field_name_string);
16084                 SNPRINTF(mod_value_string,      length_value, "UPPER(%s)", temp_key_value_string);
16085                 EM_SAFE_FREE(temp_key_value_string);
16086         } else {
16087                 mod_field_name_string = strdup(temp_field_name_string);
16088                 mod_value_string      = temp_key_value_string;
16089         }
16090
16091         switch (input_list_filter_rule->rule_type) {
16092         case EMAIL_LIST_FILTER_RULE_EQUAL:
16093                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = %s ", mod_field_name_string, mod_value_string);
16094                 break;
16095         case EMAIL_LIST_FILTER_RULE_NOT_EQUAL:
16096                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s != %s ", mod_field_name_string, mod_value_string);
16097                 break;
16098         case EMAIL_LIST_FILTER_RULE_LESS_THAN:
16099                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s < %s ", mod_field_name_string, mod_value_string);
16100                 break;
16101         case EMAIL_LIST_FILTER_RULE_GREATER_THAN:
16102                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s > %s ", mod_field_name_string, mod_value_string);
16103                 break;
16104         case EMAIL_LIST_FILTER_RULE_LESS_THAN_OR_EQUAL:
16105                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s <= %s ", mod_field_name_string, mod_value_string);
16106                 break;
16107         case EMAIL_LIST_FILTER_RULE_GREATER_THAN_OR_EQUAL:
16108                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s >= %s ", mod_field_name_string, mod_value_string);
16109                 break;
16110         case EMAIL_LIST_FILTER_RULE_INCLUDE:
16111                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s LIKE %s ", mod_field_name_string, mod_value_string);
16112                 break;
16113         case EMAIL_LIST_FILTER_RULE_IN:
16114                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s IN (%s) ", mod_field_name_string, mod_value_string);
16115                 break;
16116         case EMAIL_LIST_FILTER_RULE_NOT_IN:
16117                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s NOT IN (%s) ", mod_field_name_string, mod_value_string);
16118                 break;
16119         default:
16120                 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16121                 ret = EMAIL_ERROR_INVALID_PARAM;
16122                 goto FINISH_OFF;
16123         }
16124
16125         *output_string = strdup(result_rule_string);
16126
16127 FINISH_OFF:
16128         EM_SAFE_FREE(mod_field_name_string);
16129         EM_SAFE_FREE(mod_value_string);
16130
16131         EM_DEBUG_FUNC_END("ret [%d]", ret);
16132         return ret;
16133 }
16134
16135 static int _make_filter_attach_rule_string(char *multi_user_name, email_list_filter_rule_attach_t *input_list_filter_rule, char **output_string)
16136 {
16137         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16138
16139         char *field_name_string = NULL;
16140         char  key_value_string[QUERY_SIZE] = {0,};
16141         char  result_rule_string[QUERY_SIZE] = {0,};
16142         int rc = -1;
16143         int count = 0;
16144         int query_size = 0;
16145         int cur_query = 0;
16146         int col_index = 0;
16147         int error = EMAIL_ERROR_NONE;
16148         char **result = NULL;
16149         char sql_query_string[QUERY_SIZE] = {0,};
16150         char *sql_query_string2 = NULL;
16151         sqlite3 *local_db_handle = NULL;
16152         int *mail_ids = NULL;
16153
16154         if (input_list_filter_rule == NULL || output_string == NULL) {
16155                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16156                 return  EMAIL_ERROR_INVALID_PARAM;
16157         }
16158
16159         field_name_string = EM_SAFE_STRDUP("attachment_name");
16160         SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16161
16162         switch (input_list_filter_rule->rule_type) {
16163
16164         case EMAIL_LIST_FILTER_RULE_INCLUDE:
16165                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16166                 break;
16167
16168         case EMAIL_LIST_FILTER_RULE_MATCH:
16169                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16170                 break;
16171
16172         default:
16173                 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16174                 error = EMAIL_ERROR_INVALID_PARAM;
16175                 goto FINISH_OFF;
16176         }
16177
16178         local_db_handle = emstorage_get_db_connection(multi_user_name);
16179
16180         EMSTORAGE_START_READ_TRANSACTION(true);
16181         SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_attachment_tbl %s", result_rule_string);
16182
16183         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16184         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16185                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16186
16187         col_index = 1;
16188
16189         if (!count) {
16190                 EM_DEBUG_LOG("No mail found...");
16191                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
16192                 *output_string = strdup("mail_id IN () ");
16193                 goto FINISH_OFF;
16194         }
16195
16196         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
16197         int i = 0;
16198         if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16199                 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16200                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16201                 goto FINISH_OFF;
16202         }
16203
16204         for (i = 0; i < count; i++) {
16205                 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16206                 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16207         }
16208
16209         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >>", count);
16210         sqlite3_free_table(result);
16211         EMSTORAGE_FINISH_READ_TRANSACTION(true);
16212
16213         //      sqlite3_db_release_memory(local_db_handle);
16214
16215
16216         query_size = (10 * count) + strlen("mail_id IN ()  ");
16217
16218         sql_query_string2 = em_malloc(query_size);
16219         if (sql_query_string2 == NULL) {
16220                 EM_DEBUG_EXCEPTION("em_mallocfailed");
16221                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16222                 goto FINISH_OFF;
16223         }
16224
16225         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN (");
16226         for (i = 0; i < count-1; i++)
16227                 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16228
16229         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d) ", mail_ids[count-1]);
16230
16231         *output_string = strdup(sql_query_string2);
16232 FINISH_OFF:
16233
16234         EM_SAFE_FREE(mail_ids); /* prevent */
16235         EM_SAFE_FREE(sql_query_string2);
16236         EM_SAFE_FREE(field_name_string);
16237         EM_DEBUG_FUNC_END("error [%d]", error);
16238         return error;
16239 }
16240
16241 static int _make_filter_fts_rule_string(char *multi_user_name, email_list_filter_rule_fts_t *input_list_filter_rule, char **output_string)
16242 {
16243         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16244         char *field_name_string = NULL;
16245         char key_value_string[QUERY_SIZE] = {0,};
16246         char  result_rule_string[QUERY_SIZE] = {0,};
16247         int rc = -1;
16248         int count = 0;
16249         int col_index = 0;
16250         int query_size = 0;
16251         int error = EMAIL_ERROR_NONE;
16252         char **result = NULL;
16253         char sql_query_string[QUERY_SIZE] = {0,};
16254         char *sql_query_string2 = NULL;
16255         sqlite3 *local_db_handle = NULL;
16256         int *mail_ids = NULL;
16257
16258         if (input_list_filter_rule == NULL || output_string == NULL) {
16259                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16260                 return  EMAIL_ERROR_INVALID_PARAM;
16261         }
16262
16263         field_name_string = EM_SAFE_STRDUP("body_text");
16264         SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16265
16266         switch (input_list_filter_rule->rule_type) {
16267
16268         case EMAIL_LIST_FILTER_RULE_INCLUDE:
16269                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16270                 break;
16271
16272         case EMAIL_LIST_FILTER_RULE_MATCH:
16273                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16274                 break;
16275
16276         default:
16277                 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16278                 error = EMAIL_ERROR_INVALID_PARAM;
16279                 goto FINISH_OFF;
16280         }
16281
16282         local_db_handle = emstorage_get_db_connection(multi_user_name);
16283
16284         EMSTORAGE_START_READ_TRANSACTION(true);
16285         SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_text_tbl %s", result_rule_string);
16286
16287         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16288         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16289                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16290
16291         col_index = 1;
16292
16293         if (!count) {
16294                 EM_DEBUG_LOG("No mail found...");
16295                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
16296                 *output_string = strdup("mail_id IN () ");
16297                 goto FINISH_OFF;
16298         }
16299
16300         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
16301         int i = 0;
16302
16303         if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16304                 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16305                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16306                 goto FINISH_OFF;
16307         }
16308
16309         for (i = 0; i < count; i++) {
16310                 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16311                 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16312         }
16313
16314         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >>", count);
16315         sqlite3_free_table(result);
16316         EMSTORAGE_FINISH_READ_TRANSACTION(true);
16317
16318         //      sqlite3_db_release_memory(local_db_handle);
16319
16320
16321         query_size = (10 * count) + strlen("mail_id IN ()  ");
16322         sql_query_string2 = em_malloc(query_size);
16323         if (sql_query_string2 == NULL) {
16324                 EM_DEBUG_EXCEPTION("em_mallocfailed");
16325                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16326                 goto FINISH_OFF;
16327         }
16328         int cur_query = 0;
16329         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN (");
16330         for (i = 0; i < count-1; i++)
16331                 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16332
16333         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d) ", mail_ids[count-1]);
16334
16335         *output_string = strdup(sql_query_string2);
16336
16337 FINISH_OFF:
16338         EM_SAFE_FREE(mail_ids); /* prevent */
16339         EM_SAFE_FREE(sql_query_string2);
16340         EM_SAFE_FREE(field_name_string);
16341         EM_DEBUG_FUNC_END("error [%d]", error);
16342         return error;
16343 }
16344
16345 static int _make_order_rule_string(char *multi_user_name, email_list_sorting_rule_t *input_sorting_rule, char **output_string)
16346 {
16347         EM_DEBUG_FUNC_BEGIN("input_sorting_rule [%p], output_string [%p]", input_sorting_rule, output_string);
16348
16349         char  result_rule_string[QUERY_SIZE] = { 0 , };
16350         int   ret = EMAIL_ERROR_NONE;
16351
16352         emstorage_account_tbl_t *account_tbl_array = NULL;
16353         int count = 0;
16354         int i = 0;
16355         char *result_str = NULL;
16356         char *tmp_str1 = NULL;
16357         char *tmp_str2 = NULL;
16358         char query_per_account[QUERY_SIZE] = { 0 , };
16359
16360         if (input_sorting_rule->force_boolean_check) {
16361                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = 0 ",
16362                         emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16363         } else {
16364                 EM_SAFE_STRNCPY(result_rule_string,
16365                         emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16366                         QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16367         }
16368
16369         switch (input_sorting_rule->sort_order) {
16370         case EMAIL_SORT_ORDER_ASCEND:
16371                 EM_SAFE_STRNCAT(result_rule_string, " ASC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16372                 break;
16373
16374         case EMAIL_SORT_ORDER_DESCEND:
16375                 EM_SAFE_STRNCAT(result_rule_string, " DESC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16376                 break;
16377
16378         case EMAIL_SORT_ORDER_NOCASE_ASCEND:
16379                 EM_SAFE_STRNCAT(result_rule_string, " COLLATE NOCASE ASC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16380                 break;
16381
16382         case EMAIL_SORT_ORDER_NOCASE_DESCEND:
16383                 EM_SAFE_STRNCAT(result_rule_string, " COLLATE NOCASE DESC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16384                 break;
16385
16386         case EMAIL_SORT_ORDER_TO_CCBCC:
16387                 memset(result_rule_string, 0, QUERY_SIZE);
16388                 if (input_sorting_rule->key_value.string_type_value)
16389                         sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16390                                         " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 ELSE 2 END ",
16391                                         input_sorting_rule->key_value.string_type_value);
16392                 break;
16393
16394         case EMAIL_SORT_ORDER_TO_CC_BCC:
16395                 memset(result_rule_string, 0, QUERY_SIZE);
16396                 if (input_sorting_rule->key_value.string_type_value)
16397                         sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16398                                         " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 WHEN full_address_cc LIKE \'%%%q%%\' THEN 2 ELSE 3 END ",
16399                                         input_sorting_rule->key_value.string_type_value, input_sorting_rule->key_value.string_type_value);
16400                 break;
16401
16402         case EMAIL_SORT_ORDER_TO_CCBCC_ALL:
16403                 if (!emstorage_get_account_list(multi_user_name, &count, &account_tbl_array, true, false, NULL)) {
16404                         EM_DEBUG_EXCEPTION("emstorage_get_account_list failed");
16405                         goto FINISH_OFF;
16406                 }
16407
16408                 if (!count) {
16409                         EM_DEBUG_LOG("No account exist");
16410                         ret = EMAIL_ERROR_INVALID_PARAM;
16411                         goto FINISH_OFF;
16412                 }
16413
16414                 for (i = 0; i < count; i++) {
16415                         if (i > 0 && result_str) {
16416                                 tmp_str2 = result_str;
16417                                 result_str = g_strconcat(tmp_str2, " OR ", NULL);
16418                                 EM_SAFE_FREE(tmp_str2);
16419                         }
16420
16421                         memset(query_per_account, 0, QUERY_SIZE);
16422                         snprintf(query_per_account, QUERY_SIZE,
16423                                         "(account_id = %d AND full_address_to LIKE \'%%%s%%\')",
16424                                         account_tbl_array[i].account_id, account_tbl_array[i].user_email_address);
16425
16426                         tmp_str1 = result_str;
16427                         if (tmp_str1)
16428                                 result_str = g_strconcat(tmp_str1, query_per_account, NULL);
16429                         else
16430                                 result_str = g_strdup(query_per_account);
16431                         EM_SAFE_FREE(tmp_str1);
16432                 }
16433
16434                 snprintf(result_rule_string, QUERY_SIZE,
16435                                 " CASE WHEN %s THEN 1 ELSE 2 END ", result_str);
16436
16437                 EM_SAFE_FREE(result_str);
16438                 if (account_tbl_array)
16439                         emstorage_free_account(&account_tbl_array, count, NULL);
16440                 break;
16441
16442         case EMAIL_SORT_ORDER_LOCALIZE_ASCEND:
16443                 memset(result_rule_string, 0, QUERY_SIZE);
16444                 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16445                                 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END ASC, %s COLLATE NOCASE ASC ",
16446                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16447                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16448                 break;
16449
16450         case EMAIL_SORT_ORDER_LOCALIZE_DESCEND:
16451                 memset(result_rule_string, 0, QUERY_SIZE);
16452                 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16453                                 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END DESC, %s COLLATE NOCASE DESC ",
16454                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16455                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16456                 break;
16457
16458         default:
16459                 EM_DEBUG_EXCEPTION("Invalid sort_order [%d]", input_sorting_rule->sort_order);
16460                 ret = EMAIL_ERROR_INVALID_PARAM;
16461                 goto FINISH_OFF;
16462         }
16463
16464         *output_string = strdup(result_rule_string);
16465
16466 FINISH_OFF:
16467         EM_DEBUG_FUNC_END("ret [%d]", ret);
16468         return ret;
16469 }
16470
16471 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)
16472 {
16473         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);
16474         int ret = EMAIL_ERROR_NONE;
16475         int i = 0;
16476         int string_offset = 0;
16477         int query_size = 0;
16478         int new_query_size = 0;
16479         char *conditional_clause_string = NULL;
16480         char *result_string_for_a_item = NULL;
16481
16482         if ((input_filter_count > 0 && !input_filter_list) || (input_sorting_rule_count > 0 && !input_sorting_rule_list) || output_conditional_clause == NULL) {
16483                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16484                 return EMAIL_ERROR_INVALID_PARAM;
16485         }
16486
16487         conditional_clause_string = em_malloc(QUERY_SIZE);
16488         if (conditional_clause_string == NULL) {
16489                 EM_DEBUG_EXCEPTION("Memory is full");
16490                 return EMAIL_ERROR_OUT_OF_MEMORY;
16491         }
16492
16493         if (input_filter_count > 0) {
16494                 query_size = QUERY_SIZE;
16495                 g_strlcpy(conditional_clause_string, " WHERE ", QUERY_SIZE);
16496
16497                 for (i = 0; i < input_filter_count; i++) {
16498                         switch (input_filter_list[i].list_filter_item_type) {
16499                         case EMAIL_LIST_FILTER_ITEM_RULE:
16500                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE", i);
16501                                 _make_filter_rule_string(&(input_filter_list[i].list_filter_item.rule), &result_string_for_a_item);
16502                                 break;
16503
16504                         case EMAIL_LIST_FILTER_ITEM_RULE_FTS:
16505                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_FTS", i);
16506                                 _make_filter_fts_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_fts), &result_string_for_a_item);
16507                                 break;
16508
16509                         case EMAIL_LIST_FILTER_ITEM_RULE_ATTACH:
16510                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_ATTACH", i);
16511                                 _make_filter_attach_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_attach), &result_string_for_a_item);
16512                                 break;
16513
16514                         case EMAIL_LIST_FILTER_ITEM_OPERATOR:
16515                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_OPERATOR", i);
16516                                 switch (input_filter_list[i].list_filter_item.operator_type) {
16517                                 case EMAIL_LIST_FILTER_OPERATOR_AND:
16518                                         result_string_for_a_item = strdup("AND ");
16519                                         break;
16520                                 case EMAIL_LIST_FILTER_OPERATOR_OR:
16521                                         result_string_for_a_item = strdup("OR ");
16522                                         break;
16523                                 case EMAIL_LIST_FILTER_OPERATOR_LEFT_PARENTHESIS:
16524                                         result_string_for_a_item = strdup(" (");
16525                                         break;
16526                                 case EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS:
16527                                         result_string_for_a_item = strdup(") ");
16528                                         break;
16529                                 }
16530                                 break;
16531
16532                         default:
16533                                 EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", input_filter_list[i].list_filter_item_type);
16534                                 ret = EMAIL_ERROR_INVALID_PARAM;
16535                                 goto FINISH_OFF;
16536                         }
16537
16538                         if (result_string_for_a_item == NULL) {
16539                                 EM_DEBUG_EXCEPTION("result_string_for_a_item is null");
16540                                 ret = EMAIL_ERROR_INVALID_PARAM;
16541                                 goto FINISH_OFF;
16542                         }
16543
16544                         if (strlen(conditional_clause_string) + EM_SAFE_STRLEN(result_string_for_a_item) >= query_size) { /* prevent 34364 */
16545                                 EM_DEBUG_LOG("QUERY is too long");
16546                                 new_query_size = EM_SAFE_STRLEN(result_string_for_a_item) + EM_SAFE_STRLEN(conditional_clause_string) + QUERY_SIZE;
16547                                 conditional_clause_string = realloc(conditional_clause_string, new_query_size);
16548                                 if (conditional_clause_string == NULL) {
16549                                         EM_DEBUG_EXCEPTION("realloc failed");
16550                                         ret = EMAIL_ERROR_OUT_OF_MEMORY;
16551                                         goto FINISH_OFF;
16552                                 }
16553
16554                                 query_size = new_query_size;
16555                         }
16556
16557                         EM_SAFE_STRNCAT(conditional_clause_string, result_string_for_a_item , QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16558                         EM_SAFE_FREE(result_string_for_a_item);
16559                 }
16560         }
16561
16562         if (input_sorting_rule_count > 0) {
16563                 EM_SAFE_STRNCAT(conditional_clause_string, "ORDER BY ", QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16564
16565                 for (i = 0; i < input_sorting_rule_count; i++) {
16566                         if ((ret = _make_order_rule_string(multi_user_name, &input_sorting_rule_list[i], &result_string_for_a_item)) != EMAIL_ERROR_NONE) {
16567                                 EM_DEBUG_EXCEPTION("_make_order_rule_string failed. [%d]", ret);
16568                                 goto FINISH_OFF;
16569                         }
16570                         if (i > 0)
16571                                 EM_SAFE_STRNCAT(conditional_clause_string, ", " , QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16572                         EM_SAFE_STRNCAT(conditional_clause_string, result_string_for_a_item , QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16573                         EM_SAFE_FREE(result_string_for_a_item);
16574                 }
16575         }
16576
16577         if (input_start_index != -1 && input_limit_count != -1) {
16578                 string_offset = strlen(conditional_clause_string);
16579                 SNPRINTF_OFFSET(conditional_clause_string, string_offset, query_size, " LIMIT %d, %d", input_start_index, input_limit_count);
16580         }
16581
16582         *output_conditional_clause = strdup(conditional_clause_string);
16583
16584 FINISH_OFF:
16585         EM_SAFE_FREE(result_string_for_a_item);
16586         EM_SAFE_FREE(conditional_clause_string);
16587
16588         EM_DEBUG_FUNC_END("ret [%d]", ret);
16589         return ret;
16590 }
16591
16592 INTERNAL_FUNC int emstorage_free_list_filter(email_list_filter_t **input_filter_list, int input_filter_count)
16593 {
16594         EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d]", input_filter_list, input_filter_count);
16595         int err = EMAIL_ERROR_NONE;
16596         int i = 0;
16597         email_list_filter_t *temp_filter_list = NULL;
16598
16599         EM_IF_NULL_RETURN_VALUE(input_filter_list, EMAIL_ERROR_INVALID_PARAM);
16600
16601         for (i = 0; i < input_filter_count; i++) {
16602                 temp_filter_list = (*input_filter_list) + i;
16603                 if (!temp_filter_list)
16604                         continue;
16605
16606                 if (temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE) {
16607                         switch (temp_filter_list->list_filter_item.rule.target_attribute) {
16608                         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
16609                         case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
16610                         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
16611                         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
16612                         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
16613                         case EMAIL_MAIL_ATTRIBUTE_FROM:
16614                         case EMAIL_MAIL_ATTRIBUTE_TO:
16615                         case EMAIL_MAIL_ATTRIBUTE_CC:
16616                         case EMAIL_MAIL_ATTRIBUTE_BCC:
16617                         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN:
16618                         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML:
16619                         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
16620                                 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule.key_value.string_type_value);
16621                                 break;
16622                         default:
16623                                 break;
16624                         }
16625                 } 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) {
16626                         EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_fts.key_value.string_type_value);
16627                 } 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) {
16628                         EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_attach.key_value.string_type_value);
16629                 }
16630         }
16631
16632         EM_SAFE_FREE(*input_filter_list);
16633
16634         EM_DEBUG_FUNC_END("err [%d]", err);
16635         return err;
16636 }
16637
16638 /* Tasks --------------------------------------------------------------------------*/
16639 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)
16640 {
16641         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);
16642         int ret = 0;
16643         int i = 0;
16644         int task_id = 0;
16645         int err = EMAIL_ERROR_NONE;
16646         int rc = -1;
16647         DB_STMT hStmt = NULL;
16648         char sql_query_string[QUERY_SIZE] = {0, };
16649         sqlite3 *local_db_handle = NULL;
16650         char *sql = "SELECT max(rowid) FROM mail_task_tbl;";
16651         char **result = NULL;
16652
16653         if (input_task_parameter == NULL || output_task_id == NULL) {
16654                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16655                 return EMAIL_ERROR_INVALID_PARAM;
16656         }
16657
16658         local_db_handle = emstorage_get_db_connection(multi_user_name);
16659         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, err);
16660
16661         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
16662         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16663                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
16664
16665         if (NULL == result[1])
16666                 task_id = 1;
16667         else
16668                 task_id = atoi(result[1])+1;
16669
16670         *output_task_id = task_id;
16671
16672         sqlite3_free_table(result);
16673         result = NULL;
16674
16675         SNPRINTF(sql_query_string, sizeof(sql_query_string),
16676                         "INSERT INTO mail_task_tbl VALUES "
16677                         "(        "
16678                         "    ? "  /*   task_id */
16679                         "  , ? "  /*   task_type */
16680                         "  , ? "  /*   task_status */
16681                         "  , ? "  /*   task_priority */
16682                         "  , ? "  /*   task_parameter_length */
16683                         "  , ? "  /*   task_parameter */
16684                         "  , ? "  /*   date_time */
16685                         ") ");
16686
16687         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16688         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16689                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16690
16691         EM_DEBUG_LOG_SEC(">>>> SQL STMT [%s] ", sql_query_string);
16692
16693
16694         _bind_stmt_field_data_int(hStmt, i++, task_id);
16695         _bind_stmt_field_data_int(hStmt, i++, input_task_type);
16696         _bind_stmt_field_data_int(hStmt, i++, EMAIL_TASK_STATUS_WAIT);
16697         _bind_stmt_field_data_int(hStmt, i++, input_task_priority);
16698         _bind_stmt_field_data_int(hStmt, i++, input_task_parameter_length);
16699         _bind_stmt_field_data_blob(hStmt, i++, input_task_parameter, input_task_parameter_length);
16700         _bind_stmt_field_data_int(hStmt, i++, time(NULL));
16701
16702         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16703
16704         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {err = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
16705                         ("sqlite3_step fail:%d", rc));
16706         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16707                         ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
16708
16709         ret = (err == EMAIL_ERROR_NONE);
16710
16711 FINISH_OFF:
16712         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, err);
16713
16714         if (hStmt != NULL) {
16715                 rc = sqlite3_finalize(hStmt);
16716                 if (rc != SQLITE_OK) {
16717                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16718                         err = EMAIL_ERROR_DB_FAILURE;
16719                 }
16720         }
16721
16722         EM_DEBUG_FUNC_END("err [%d]", err);
16723         return err;
16724 }
16725
16726 INTERNAL_FUNC int emstorage_delete_task(char *multi_user_name, int task_id, int transaction)
16727 {
16728         EM_DEBUG_FUNC_BEGIN("task_id[%d], transaction[%d]", task_id, transaction);
16729         int ret = false;
16730         int err = EMAIL_ERROR_NONE;
16731         char sql_query_string[QUERY_SIZE] = {0, };
16732
16733         sqlite3 *local_db_handle = NULL;
16734
16735         if (task_id < 0) {
16736                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16737                 return EMAIL_ERROR_INVALID_PARAM;
16738         }
16739
16740         local_db_handle = emstorage_get_db_connection(multi_user_name);
16741
16742         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
16743
16744         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_task_tbl WHERE task_id = %d", task_id);
16745         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16746         if (err != EMAIL_ERROR_NONE) {
16747                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16748                 goto FINISH_OFF;
16749         }
16750
16751         ret = true;
16752
16753 FINISH_OFF:
16754         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
16755
16756         EM_DEBUG_FUNC_END("err [%d]", err);
16757         return err;
16758 }
16759
16760 INTERNAL_FUNC int emstorage_update_task_status(char *multi_user_name, int task_id, email_task_status_type_t task_status, int transaction)
16761 {
16762         EM_DEBUG_FUNC_BEGIN("task_id[%d] task_status[%d] transaction[%d]", task_id, task_status, transaction);
16763         int ret = false;
16764         int err = EMAIL_ERROR_NONE;
16765         char sql_query_string[QUERY_SIZE] = {0, };
16766
16767         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16768         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
16769
16770         SNPRINTF(sql_query_string, sizeof(sql_query_string),
16771                         "UPDATE mail_task_tbl SET"
16772                         " task_status = %d"
16773                         " WHERE task_id = %d"
16774                         , task_status
16775                         , task_id);
16776         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16777         if (err != EMAIL_ERROR_NONE) {
16778                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16779                 goto FINISH_OFF;
16780         }
16781
16782         ret = true;
16783
16784 FINISH_OFF:
16785         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
16786
16787         EM_DEBUG_FUNC_END("err [%d]", err);
16788         return err;
16789 }
16790
16791 INTERNAL_FUNC int emstorage_query_task(char *multi_user_name,
16792                 const char *input_conditional_clause,
16793                 const char *input_ordering_clause,
16794                 email_task_t **output_task_list,
16795                 int *output_task_count)
16796 {
16797         EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_ordering_clause [%p], "
16798                         "output_task_list[%p], output_task_count[%d]",
16799                         input_conditional_clause, input_ordering_clause, output_task_list, output_task_count);
16800         int i = 0, count = 0, rc = -1;
16801         int cur_query = 0;
16802         int field_index = 0;
16803         int err = EMAIL_ERROR_NONE;
16804         email_task_t *task_item_from_tbl = NULL;
16805         char sql_query_string[QUERY_SIZE] = {0, };
16806         char *field_list = "task_id, task_type, task_status, task_priority, task_parameter_length, task_parameter ";
16807         char **result;
16808         sqlite3 *local_db_handle = NULL;
16809         DB_STMT hStmt = NULL;
16810
16811         EM_IF_NULL_RETURN_VALUE(input_conditional_clause, false);
16812         EM_IF_NULL_RETURN_VALUE(output_task_count, false);
16813
16814         local_db_handle = emstorage_get_db_connection(multi_user_name);
16815
16816         SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
16817                         "SELECT COUNT(*) FROM mail_task_tbl %s", input_conditional_clause);
16818         EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
16819
16820         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
16821         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16822                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16823
16824         count = atoi(result[1]);
16825         sqlite3_free_table(result);
16826
16827         EM_DEBUG_LOG("count = %d", rc);
16828
16829         if (count == 0) {
16830                 EM_DEBUG_EXCEPTION("no task found...");
16831                 err = EMAIL_ERROR_TASK_NOT_FOUND;
16832                 goto FINISH_OFF;
16833         }
16834
16835         SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
16836                         "SELECT %s FROM mail_task_tbl %s %s", field_list, input_conditional_clause, input_ordering_clause);
16837         EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
16838
16839         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16840
16841         EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
16842         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16843                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16844
16845         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16846         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16847                         ("sqlite3_step fail:%d", rc));
16848
16849         if (rc == SQLITE_DONE) {
16850                 EM_DEBUG_EXCEPTION("no task found...");
16851                 err = EMAIL_ERROR_TASK_NOT_FOUND;
16852                 count = 0;
16853                 goto FINISH_OFF;
16854         }
16855
16856         if (!(task_item_from_tbl = (email_task_t*)em_malloc(sizeof(email_task_t) * count))) {
16857                 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
16858                 err = EMAIL_ERROR_OUT_OF_MEMORY;
16859                 goto FINISH_OFF;
16860         }
16861
16862         for (i = 0; i < count; i++) {
16863                 /*  get recordset */
16864                 field_index = 0;
16865
16866                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_id), field_index++);
16867                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_type), field_index++);
16868                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_status), field_index++);
16869                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_priority), field_index++);
16870                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_parameter_length), field_index++);
16871                 _get_stmt_field_data_blob(hStmt, (void**)&(task_item_from_tbl[i].task_parameter), field_index++);
16872
16873                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16874                 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i,  rc);
16875                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16876                                 ("sqlite3_step fail:%d", rc));
16877         }
16878
16879 FINISH_OFF:
16880
16881         if (err == EMAIL_ERROR_NONE && output_task_list) {
16882                 *output_task_list = task_item_from_tbl;
16883                 *output_task_count = count;
16884         } else {
16885                 if (task_item_from_tbl) {
16886                         for (i = 0; i < count; i++)
16887                                 EM_SAFE_FREE(task_item_from_tbl[i].task_parameter);
16888
16889                         free(task_item_from_tbl);
16890                 }
16891         }
16892
16893         if (hStmt != NULL) {
16894                 rc = sqlite3_finalize(hStmt);
16895                 hStmt = NULL;
16896                 if (rc != SQLITE_OK) {
16897                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16898                         err = EMAIL_ERROR_DB_FAILURE;
16899                 }
16900         }
16901
16902         EM_DEBUG_FUNC_END("err [%d]", err);
16903         return err;
16904 }
16905
16906 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)
16907 {
16908         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);
16909         int err = EMAIL_ERROR_NONE;
16910
16911         if (message_id == NULL) {
16912                 EM_DEBUG_EXCEPTION("Invalid parameter");
16913                 err = EMAIL_ERROR_INVALID_PARAM;
16914                 return err;
16915         }
16916
16917         int rc = -1;
16918         int count = 0;
16919         int temp_mail_id = 0;
16920         int where_pararaph_length = 0;
16921         char *where_pararaph = NULL;
16922         char sql_query_string[QUERY_SIZE] = {0, };
16923         char **result = NULL;
16924         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16925
16926         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id from mail_tbl ");
16927
16928         where_pararaph_length = EM_SAFE_STRLEN(message_id) + 100;
16929         where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
16930         if (where_pararaph == NULL) {
16931                 EM_DEBUG_EXCEPTION("em_mallocfailed");
16932                 err = EMAIL_ERROR_OUT_OF_MEMORY;
16933                 goto FINISH_OFF;
16934         }
16935
16936         if (account_id != ALL_ACCOUNT)
16937                 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);
16938         else
16939                 sqlite3_snprintf(where_pararaph_length, where_pararaph, "WHERE mailbox_type = %d AND message_id like '%q'", input_mailbox_type, message_id);
16940
16941         if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
16942                 EM_SAFE_STRNCAT(sql_query_string, where_pararaph , QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
16943
16944         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
16945
16946         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16947         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16948                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16949
16950
16951         EM_DEBUG_LOG("Count of mails [%d]", count);
16952
16953         if (count) {
16954                 _get_table_field_data_int(result, &temp_mail_id, 1);
16955                 EM_DEBUG_LOG("Searched mail_id [%d]", temp_mail_id);
16956
16957                 memset(sql_query_string, 0x00, QUERY_SIZE);
16958                 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string, "UPDATE mail_tbl set server_mail_id = '%q'", server_uid);
16959
16960                 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE) {
16961                         EM_SAFE_STRNCAT(sql_query_string, where_pararaph,
16962                                         QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
16963                 }
16964
16965                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16966                 if (err != EMAIL_ERROR_NONE) {
16967                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16968                         goto FINISH_OFF;
16969                 }
16970
16971         } else {
16972                 err = EMAIL_ERROR_MAIL_NOT_FOUND;
16973         }
16974
16975 FINISH_OFF:
16976
16977         sqlite3_free_table(result);
16978         result = NULL;
16979
16980
16981         EM_SAFE_FREE(where_pararaph);
16982
16983         if (mail_id != NULL)
16984                 *mail_id = temp_mail_id;
16985
16986         EM_DEBUG_FUNC_END("err : [%d]", err);
16987         return err;
16988 }
16989 /* Tasks --------------------------------------------------------------------------*/
16990
16991 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
16992 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)
16993 {
16994         EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
16995
16996         if (!local_activity || !activity_id) {
16997                 EM_DEBUG_EXCEPTION("local_activity[%p], activity_id[%p]", local_activity, activity_id);
16998                 if (err_code != NULL)
16999                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17000                 return false;
17001         }
17002
17003         int rc = -1;
17004         int ret = false;
17005         int error = EMAIL_ERROR_NONE;
17006         int i = 0;
17007
17008         char sql_query_string[QUERY_SIZE] = {0, };
17009         DB_STMT hStmt = NULL;
17010
17011         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17012         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17013
17014         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17015         SNPRINTF(sql_query_string, sizeof(sql_query_string),
17016                         "INSERT INTO mail_auto_download_activity_tbl VALUES "
17017                         "("
17018                         "? "  /* Activity ID */
17019                         ",?"  /* Status */
17020                         ",?"  /* Account ID */
17021                         ",?"  /* Local Mail ID */
17022                         ",?"  /* Server mail ID */
17023                         ",?"  /* Mailbox ID*/
17024                         ",?"  /* Multi USER NAME */
17025                         ") ");
17026
17027         char *sql = "SELECT max(rowid) FROM mail_auto_download_activity_tbl;";
17028         char **result = NULL;
17029
17030
17031         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
17032
17033         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17034                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
17035
17036         if (NULL == result[1]) rc = 1;
17037         else rc = atoi(result[1])+1;
17038         sqlite3_free_table(result);
17039         result = NULL;
17040
17041         *activity_id = local_activity->activity_id = rc;
17042
17043         EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [%d], MAIL ID [%d], SERVER MAIL ID [%lu]",
17044                         local_activity->activity_id, local_activity->mail_id, local_activity->server_mail_id);
17045
17046         if (local_activity->mailbox_id)
17047                 EM_DEBUG_LOG(" MAILBOX ID [%d]", local_activity->mailbox_id);
17048
17049
17050         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17051         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17052                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17053
17054
17055         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
17056         _bind_stmt_field_data_int(hStmt, i++, local_activity->status);
17057         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
17058         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
17059         _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
17060         _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
17061         _bind_stmt_field_data_string(hStmt, i++, (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
17062
17063
17064         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17065
17066         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
17067                         ("sqlite3_step fail:%d", rc));
17068         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17069                         ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
17070
17071         ret = true;
17072
17073 FINISH_OFF:
17074         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17075         if (hStmt != NULL) {
17076                 rc = sqlite3_finalize(hStmt);
17077                 hStmt = NULL;
17078                 if (rc != SQLITE_OK) {
17079                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17080                         error = EMAIL_ERROR_DB_FAILURE;
17081                 }
17082         }
17083
17084         if (err_code != NULL)
17085                 *err_code = error;
17086
17087         EM_DEBUG_FUNC_END("ret [%d]", ret);
17088         return ret;
17089 }
17090
17091
17092 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)
17093 {
17094         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);
17095
17096         if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
17097                 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);
17098
17099                 if (err_code != NULL)
17100                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17101                 return false;
17102         }
17103
17104         int rc = -1;
17105         int ret = false;
17106         int error = EMAIL_ERROR_NONE;
17107         char sql_query_string[QUERY_SIZE] = {0, };
17108         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17109
17110         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17111         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17112
17113         if (activity_id == 0)
17114                 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);
17115         else
17116                 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);
17117
17118         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17119         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17120         if (error != EMAIL_ERROR_NONE) {
17121                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17122                 goto FINISH_OFF;
17123         }
17124
17125         /*  validate activity existence */
17126         rc = sqlite3_changes(local_db_handle);
17127         if (rc == 0) {
17128                 EM_DEBUG_EXCEPTION("No matching activity found");
17129                 error = EMAIL_ERROR_DATA_NOT_FOUND;
17130                 ret = true;
17131                 goto FINISH_OFF;
17132         }
17133
17134         ret = true;
17135
17136 FINISH_OFF:
17137         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17138
17139         if (err_code != NULL)
17140                 *err_code = error;
17141
17142         EM_DEBUG_FUNC_END("ret [%d]", ret);
17143         return ret;
17144 }
17145
17146 INTERNAL_FUNC int emstorage_delete_all_auto_download_activity(char *multi_user_name, int account_id, int transaction, int *err_code)
17147 {
17148         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17149
17150         if (account_id < FIRST_ACCOUNT_ID) {
17151                 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17152                 if (err_code != NULL)
17153                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17154                 return false;
17155         }
17156
17157         int rc = -1;
17158         int ret = false;
17159         int error = EMAIL_ERROR_NONE;
17160         char sql_query_string[QUERY_SIZE] = {0, };
17161
17162         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17163
17164         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17165         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17166         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d", account_id);
17167
17168         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17169         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17170         if (error != EMAIL_ERROR_NONE) {
17171                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17172                 goto FINISH_OFF;
17173         }
17174
17175         rc = sqlite3_changes(local_db_handle);
17176         if (rc == 0) {
17177                 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17178                 error = EMAIL_ERROR_DATA_NOT_FOUND;
17179                 ret = true;
17180                 goto FINISH_OFF;
17181         }
17182
17183         ret = true;
17184
17185 FINISH_OFF:
17186         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17187
17188         if (err_code != NULL)
17189                 *err_code = error;
17190
17191         EM_DEBUG_FUNC_END("ret [%d]", ret);
17192         return ret;
17193 }
17194
17195
17196 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)
17197 {
17198         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17199
17200         if (account_id < FIRST_ACCOUNT_ID || mailbox_id < 0) {
17201                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17202                 if (err_code != NULL)
17203                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17204                 return false;
17205         }
17206
17207         int rc = -1;
17208         int ret = false;
17209         int error = EMAIL_ERROR_NONE;
17210         char sql_query_string[QUERY_SIZE] = {0, };
17211
17212         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17213
17214         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17215         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17216         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);
17217
17218         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17219         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17220         if (error != EMAIL_ERROR_NONE) {
17221                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17222                 goto FINISH_OFF;
17223         }
17224
17225         rc = sqlite3_changes(local_db_handle);
17226         if (rc == 0) {
17227                 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17228                 error = EMAIL_ERROR_DATA_NOT_FOUND;
17229                 ret = true;
17230                 goto FINISH_OFF;
17231         }
17232
17233         ret = true;
17234
17235 FINISH_OFF:
17236         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17237
17238         if (err_code != NULL)
17239                 *err_code = error;
17240
17241         EM_DEBUG_FUNC_END("ret [%d]", ret);
17242         return ret;
17243 }
17244
17245
17246 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)
17247 {
17248         EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
17249
17250         if (account_id < FIRST_ACCOUNT_ID || !event_start || input_mailbox_id <= 0 || !count) {
17251                 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);
17252
17253                 if (err_code != NULL)
17254                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17255                 return false;
17256         }
17257
17258         int rc = -1;
17259         int ret = false;
17260         char **result;
17261         int error = EMAIL_ERROR_NONE;
17262         int i = 0;
17263         DB_STMT hStmt = NULL;
17264         email_event_auto_download *event_list = NULL;
17265         char sql_query_string[QUERY_SIZE] = {0, };
17266
17267         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17268
17269         EMSTORAGE_START_READ_TRANSACTION(transaction);
17270
17271         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17272         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);
17273
17274
17275         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17276         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17277                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17278
17279         *count = atoi(result[1]);
17280         sqlite3_free_table(result);
17281
17282         EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
17283
17284         if (!*count) {
17285                 EM_DEBUG_LOG("No matched activity found in mail_auto_download_activity_tbl");
17286                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
17287                 ret = true;
17288                 goto FINISH_OFF;
17289         }
17290         EM_DEBUG_LOG("Activity Count = %d", *count);
17291
17292         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17293         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);
17294
17295         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17296
17297
17298         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17299
17300         EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
17301         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17302                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17303
17304
17305         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17306         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17307                         ("sqlite3_step fail:%d", rc));
17308
17309         if (!(event_list = (email_event_auto_download *)em_malloc(sizeof(email_event_auto_download)*(*count)))) {
17310                 EM_DEBUG_EXCEPTION("Malloc failed");
17311
17312                 error = EMAIL_ERROR_OUT_OF_MEMORY;
17313                 goto FINISH_OFF;
17314         }
17315
17316         for (i = 0; i < (*count); i++) {
17317                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17318                 _get_stmt_field_data_int(hStmt, &(event_list[i].status), STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17319                 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17320                 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17321                 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17322                 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17323
17324                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17325                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17326                                 ("sqlite3_step fail:%d", rc));
17327         }
17328
17329         ret = true;
17330
17331 FINISH_OFF:
17332
17333         if (true == ret)
17334                 *event_start = event_list;
17335         else {
17336                 EM_SAFE_FREE(event_list);
17337                 *event_start = NULL;
17338                 *count = 0;
17339         }
17340
17341         if (hStmt != NULL) {
17342                 rc = sqlite3_finalize(hStmt);
17343                 hStmt = NULL;
17344                 if (rc != SQLITE_OK) {
17345                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17346                         error = EMAIL_ERROR_DB_FAILURE;
17347                 }
17348         }
17349
17350         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17351         if (err_code != NULL)
17352                 *err_code = error;
17353
17354         EM_DEBUG_FUNC_END("ret [%d]", ret);
17355         return ret;
17356 }
17357
17358
17359 INTERNAL_FUNC int emstorage_get_auto_download_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
17360 {
17361         EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
17362
17363         if (!activity_count || !err_code) {
17364                 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
17365                 if (err_code != NULL)
17366                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17367                 return false;
17368         }
17369
17370         int rc = -1;
17371         int ret = false;
17372         int error = EMAIL_ERROR_NONE;
17373         DB_STMT hStmt = NULL;
17374         char sql_query_string[QUERY_SIZE] = {0, };
17375
17376         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17377
17378         EMSTORAGE_START_READ_TRANSACTION(transaction);
17379         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17380
17381         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl;");
17382
17383         EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
17384
17385
17386         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17387         EM_DEBUG_LOG_DEV("before sqlite3_prepare hStmt = %p", hStmt);
17388         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17389                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17390
17391
17392         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17393         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17394                         ("sqlite3_step fail:%d", rc));
17395
17396         _get_stmt_field_data_int(hStmt, activity_count, 0);
17397
17398         EM_DEBUG_LOG("counts of activities in activity table [%d]", *activity_count);
17399
17400         ret = true;
17401
17402 FINISH_OFF:
17403
17404         if (hStmt != NULL) {
17405                 rc = sqlite3_finalize(hStmt);
17406                 hStmt = NULL;
17407                 if (rc != SQLITE_OK) {
17408                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17409                         error = EMAIL_ERROR_DB_FAILURE;
17410                 }
17411         }
17412
17413         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17414         if (err_code != NULL)
17415                 *err_code = error;
17416
17417         EM_DEBUG_FUNC_END("ret [%d]", ret);
17418         return ret;
17419 }
17420
17421
17422 INTERNAL_FUNC int emstorage_get_auto_download_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
17423 {
17424         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
17425
17426         if (!account_list || !count) {
17427                 EM_DEBUG_EXCEPTION("account_list[%p], count[%p]", account_list, count);
17428                 if (err_code != NULL)
17429                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17430                 return false;
17431         }
17432
17433         int ret = false;
17434         int error = EMAIL_ERROR_NONE;
17435         char *sql = "SELECT count(distinct account_id) FROM mail_auto_download_activity_tbl";
17436         char **result;
17437         int i = 0, rc = -1;
17438         int *result_account_list = NULL;
17439         DB_STMT hStmt = NULL;
17440         char sql_query_string[QUERY_SIZE] = {0, };
17441         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17442
17443         EMSTORAGE_START_READ_TRANSACTION(transaction);
17444
17445         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
17446         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17447                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
17448
17449         *count = atoi(result[1]);
17450         sqlite3_free_table(result);
17451
17452         if (!*count) {
17453                 EM_DEBUG_EXCEPTION("no account found...");
17454                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17455                 ret = true;
17456                 goto FINISH_OFF;
17457         }
17458
17459         EM_DEBUG_LOG("Account count [%d]", *count);
17460
17461         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17462
17463         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_auto_download_activity_tbl");
17464
17465         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17466
17467
17468         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17469
17470         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
17471         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17472                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17473
17474
17475         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17476         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17477                         ("sqlite3_step fail:%d", rc));
17478
17479         if (NULL == (result_account_list = (int *)em_malloc(sizeof(int)*(*count)))) {
17480                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
17481                 error = EMAIL_ERROR_OUT_OF_MEMORY;
17482                 goto FINISH_OFF;
17483         }
17484
17485         for (i = 0; i < (*count); i++) {
17486                 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
17487
17488                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17489                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17490                                 ("sqlite3_step fail:%d", rc));
17491                 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
17492         }
17493
17494         ret = true;
17495
17496 FINISH_OFF:
17497
17498         if (ret == true)
17499                 *account_list = result_account_list;
17500         else
17501                 EM_SAFE_FREE(result_account_list);
17502
17503         if (hStmt != NULL) {
17504                 rc = sqlite3_finalize(hStmt);
17505                 hStmt = NULL;
17506                 if (rc != SQLITE_OK) {
17507                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17508                         error = EMAIL_ERROR_DB_FAILURE;
17509                 }
17510         }
17511
17512         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17513         if (err_code != NULL)
17514                 *err_code = error;
17515         EM_DEBUG_FUNC_END("ret [%d]", ret);
17516         return ret;
17517 }
17518
17519
17520 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)
17521 {
17522         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
17523
17524         if (account_id < FIRST_ACCOUNT_ID || !mailbox_list || !count) {
17525                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p]", account_id, mailbox_list, count);
17526                 if (err_code != NULL)
17527                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17528                 return false;
17529         }
17530
17531         int ret = false;
17532         int error = EMAIL_ERROR_NONE;
17533         char **result;
17534         int i = 0, rc = -1;
17535         int *mbox_list = NULL;
17536         DB_STMT hStmt = NULL;
17537         char sql_query_string[QUERY_SIZE] = {0, };
17538
17539         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17540
17541         EMSTORAGE_START_READ_TRANSACTION(transaction);
17542
17543         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17544         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);
17545
17546
17547         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17548         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17549                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17550
17551         *count = atoi(result[1]);
17552         sqlite3_free_table(result);
17553
17554         if (!*count) {
17555                 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
17556                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17557                 ret = true;
17558                 goto FINISH_OFF;
17559         }
17560         EM_DEBUG_LOG("Mailbox count = %d", *count);
17561
17562         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17563
17564         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);
17565
17566         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
17567
17568
17569         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17570
17571
17572         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17573                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17574
17575
17576         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17577         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17578                         ("sqlite3_step fail:%d", rc));
17579
17580         mbox_list = (int *)em_malloc(sizeof(int)*(*count)); /* prevent */
17581         if (mbox_list == NULL) {
17582                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
17583                 error = EMAIL_ERROR_OUT_OF_MEMORY;
17584                 goto FINISH_OFF;
17585         }
17586
17587         for (i = 0; i < (*count); i++) {
17588                 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
17589                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17590
17591                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17592                                 ("sqlite3_step fail:%d", rc));
17593                 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
17594         }
17595
17596         ret = true;
17597
17598 FINISH_OFF:
17599
17600         if (ret == true)
17601                 *mailbox_list = mbox_list;
17602         else
17603                 EM_SAFE_FREE(mbox_list);
17604
17605         if (hStmt != NULL) {
17606                 rc = sqlite3_finalize(hStmt);
17607                 hStmt = NULL;
17608                 if (rc != SQLITE_OK) {
17609                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17610                         error = EMAIL_ERROR_DB_FAILURE;
17611                 }
17612         }
17613
17614         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17615         if (err_code != NULL)
17616                 *err_code = error;
17617         EM_DEBUG_FUNC_END("ret [%d]", ret);
17618         return ret;
17619 }
17620
17621
17622 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)
17623 {
17624         EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
17625
17626         if (account_id < FIRST_ACCOUNT_ID || !activity_count || !err_code) {
17627                 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
17628                 if (err_code != NULL)
17629                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17630                 return false;
17631         }
17632
17633         int rc = -1;
17634         int ret = false;
17635         int error = EMAIL_ERROR_NONE;
17636         char sql_query_string[QUERY_SIZE] = {0, };
17637         DB_STMT hStmt = NULL;
17638
17639         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17640
17641         EMSTORAGE_START_READ_TRANSACTION(transaction);
17642         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17643         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);
17644
17645         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
17646
17647         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17648         EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
17649         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17650                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17651
17652         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17653         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17654                         ("sqlite3_step fail:%d", rc));
17655
17656         _get_stmt_field_data_int(hStmt, activity_count, 0);
17657
17658         EM_DEBUG_LOG("count of activities in activity table [%d]", *activity_count);
17659
17660         ret = true;
17661
17662 FINISH_OFF:
17663
17664         if (hStmt != NULL) {
17665                 rc = sqlite3_finalize(hStmt);
17666                 hStmt = NULL;
17667                 if (rc != SQLITE_OK) {
17668                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17669                         error = EMAIL_ERROR_DB_FAILURE;
17670                 }
17671         }
17672
17673         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17674
17675         if (err_code != NULL)
17676                 *err_code = error;
17677
17678         EM_DEBUG_FUNC_END("ret [%d]", ret);
17679         return ret;
17680 }
17681
17682
17683 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)
17684 {
17685         EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mailbox_id[%d]", old_server_uid, new_server_uid, mailbox_id);
17686
17687         int rc = -1, ret = false;
17688         int error = EMAIL_ERROR_NONE;
17689         char sql_query_string[QUERY_SIZE] = {0, };
17690         int transaction = true;
17691
17692         if (!old_server_uid || !new_server_uid || (!mailbox_name && mailbox_id < 0)) {
17693                 EM_DEBUG_EXCEPTION("Invalid parameters");
17694                 error = EMAIL_ERROR_INVALID_PARAM;
17695                 return false;
17696         }
17697
17698         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17699
17700         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17701         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17702
17703         if (mailbox_id > 0)
17704                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17705                                 "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);
17706         else if (mailbox_name)
17707                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17708                                 "UPDATE mail_auto_download_activity_tbl SET server_mail_id = %s WHERE server_mail_id = %s ", new_server_uid, old_server_uid);
17709
17710         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17711         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17712         if (error != EMAIL_ERROR_NONE) {
17713                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17714                 goto FINISH_OFF;
17715         }
17716
17717         rc = sqlite3_changes(local_db_handle);
17718         if (rc == 0)
17719                 EM_DEBUG_LOG("No matching found in mail_auto_download_activity_tbl");
17720
17721         ret = true;
17722
17723 FINISH_OFF:
17724
17725         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17726
17727         if (err_code != NULL)
17728                 *err_code = error;
17729
17730         EM_DEBUG_FUNC_END("ret [%d]", ret);
17731         return ret;
17732 }
17733
17734 #endif
17735
17736 #ifdef __FEATURE_UPDATE_DB_TABLE_SCHEMA__
17737
17738 typedef struct {
17739         char *column_name;
17740         char *column_type;
17741 } email_column_info_t;
17742
17743 static int get_column_information_from_table_callback(void *arg1, int argc, char **argv, char **input_column_name)
17744 {
17745         EM_DEBUG_FUNC_BEGIN("arg1[%p] argc[%d] argv[%p] column_name[%p]", arg1, argc, argv, input_column_name);
17746
17747         int i = 0;
17748         int validated = 0;
17749         char *column_name = NULL;
17750         char *column_type = NULL;
17751         GList *new_list = *((GList**)arg1);
17752         email_column_info_t *column_info_item = NULL;
17753
17754         for (i = 0; i < argc; ++i) {
17755                 /* EM_DEBUG_LOG("%s = %s", input_column_name[i], argv[i]); */
17756                 if (EM_SAFE_STRCMP(input_column_name[i], "name") == 0) {
17757                         if (column_name)
17758                                 EM_SAFE_FREE(column_name);
17759
17760                         column_name = EM_SAFE_STRDUP(argv[i]);
17761                         validated = 1;
17762                 } else if (EM_SAFE_STRCMP(input_column_name[i], "type") == 0) {
17763                         if (column_type)
17764                                 EM_SAFE_FREE(column_type);
17765
17766                         column_type = EM_SAFE_STRDUP(argv[i]);
17767                 }
17768         }
17769
17770         if (validated) {
17771                 EM_DEBUG_LOG("column_name[%s] column_type[%s]", column_name, column_type);
17772                 column_info_item = em_malloc(sizeof(email_column_info_t));
17773                 if (column_info_item == NULL) {
17774                         EM_DEBUG_EXCEPTION("em_mallocfailed");
17775                         goto FINISH_OFF;
17776                 }
17777
17778                 column_info_item->column_name = EM_SAFE_STRDUP(column_name);
17779                 column_info_item->column_type = EM_SAFE_STRDUP(column_type);
17780                 new_list = g_list_append(new_list, (gpointer)column_info_item);
17781                 *((GList**)arg1) = new_list;
17782         }
17783
17784 FINISH_OFF:
17785
17786         EM_SAFE_FREE(column_name);
17787         EM_SAFE_FREE(column_type);
17788
17789         EM_DEBUG_FUNC_END();
17790         return 0;
17791 }
17792
17793 static int emstorage_get_column_information_from_table(char *multi_user_name, const char *input_table_name, GList **output_column_info)
17794 {
17795         EM_DEBUG_FUNC_BEGIN("input_table_name[%p] output_column_info[%p]", input_table_name, output_column_info);
17796         int err = EMAIL_ERROR_NONE;
17797         int result_from_sqlite = 0;
17798         char *error_message_from_sqlite = NULL;
17799         char sql_query_string[QUERY_SIZE] = {0, };
17800         GList *new_list = NULL;
17801         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17802
17803         SNPRINTF(sql_query_string, QUERY_SIZE, "pragma table_info(%s);", input_table_name);
17804
17805         result_from_sqlite = sqlite3_exec(local_db_handle, sql_query_string, get_column_information_from_table_callback, &new_list, &error_message_from_sqlite);
17806
17807         if (result_from_sqlite != SQLITE_OK)
17808                 EM_DEBUG_EXCEPTION("sqlite3_exec returns [%d]", result_from_sqlite);
17809
17810         EM_DEBUG_LOG("new_list[%p] output_column_info[%p]", new_list, output_column_info);
17811
17812         if (new_list && output_column_info) {
17813                 EM_DEBUG_LOG("g_list_length[%d]", g_list_length(new_list));
17814                 *output_column_info = new_list;
17815         }
17816
17817         EM_DEBUG_FUNC_END("err [%d]", err);
17818         return err;
17819 }
17820
17821 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)
17822 {
17823         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);
17824         int error = EMAIL_ERROR_NONE;
17825         int rc = -1;
17826         sqlite3 *local_db_handle = NULL;
17827         char sql_query_string[QUERY_SIZE] = {0, };
17828         
17829         local_db_handle = emstorage_get_db_connection(multi_user_name);
17830
17831         if (input_full_query == NULL || input_source_table_name == NULL || input_new_table_name == NULL) {
17832                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17833                 error = EMAIL_ERROR_INVALID_PARAM;
17834                 goto FINISH_OFF;
17835         }
17836
17837         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17838         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17839
17840         EM_DEBUG_LOG("[%s] will be replaced by [%s]", input_source_table_name, input_new_table_name);
17841
17842         EM_SAFE_STRNCPY(sql_query_string, input_full_query[input_query_index], sizeof(sql_query_string)-1); /*prevent 21984*/
17843         reg_replace(sql_query_string, input_source_table_name, input_new_table_name);
17844
17845         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17846 FINISH_OFF:
17847
17848         if (error == EMAIL_ERROR_NONE) {
17849                 EMSTORAGE_PROTECTED_FUNC_CALL(
17850                         sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17851         } else {
17852                 EMSTORAGE_PROTECTED_FUNC_CALL(
17853                         sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17854         }
17855
17856         EM_DEBUG_FUNC_END("error [%d]", error);
17857         return error;
17858 }
17859
17860 static int emstorage_add_column(char *multi_user_name, char *input_table_name, email_column_info_t *input_new_column)
17861 {
17862         EM_DEBUG_FUNC_BEGIN("input_table_name[%p] input_new_column[%p]", input_table_name, input_new_column);
17863         int error = EMAIL_ERROR_NONE;
17864         int rc = -1;
17865         sqlite3 *local_db_handle = NULL;
17866         char sql_query_string[QUERY_SIZE] = {0, };
17867
17868         local_db_handle = emstorage_get_db_connection(multi_user_name);
17869
17870         if (input_table_name == NULL || input_new_column == NULL) {
17871                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17872                 error = EMAIL_ERROR_INVALID_PARAM;
17873                 goto FINISH_OFF;
17874         }
17875
17876         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17877         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17878         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);
17879         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17880 FINISH_OFF:
17881
17882         if (error == EMAIL_ERROR_NONE) {
17883                 EMSTORAGE_PROTECTED_FUNC_CALL(
17884                         sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17885         } else {
17886                 EMSTORAGE_PROTECTED_FUNC_CALL(
17887                         sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17888         }
17889
17890         EM_DEBUG_FUNC_END("error [%d]", error);
17891         return error;
17892 }
17893
17894 static int emstorage_drop_table(char *multi_user_name, char *input_table_name)
17895 {
17896         EM_DEBUG_FUNC_BEGIN("input_table_name[%p]", input_table_name);
17897         int error = EMAIL_ERROR_NONE;
17898         int rc = -1;
17899         sqlite3 *local_db_handle = NULL;
17900         char sql_query_string[QUERY_SIZE] = {0, };
17901
17902         local_db_handle = emstorage_get_db_connection(multi_user_name);
17903
17904         if (input_table_name == NULL) {
17905                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17906                 error = EMAIL_ERROR_INVALID_PARAM;
17907                 goto FINISH_OFF;
17908         }
17909
17910         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17911         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17912         SNPRINTF(sql_query_string, QUERY_SIZE, "DROP TABLE %s;", input_table_name);
17913         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17914 FINISH_OFF:
17915
17916         if (error == EMAIL_ERROR_NONE) {
17917                 EMSTORAGE_PROTECTED_FUNC_CALL(
17918                         sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17919         } else {
17920                 EMSTORAGE_PROTECTED_FUNC_CALL(
17921                         sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17922         }
17923
17924         EM_DEBUG_FUNC_END("error [%d]", error);
17925         return error;
17926 }
17927
17928 gint glist_compare_column_name(gconstpointer old_column_info, gconstpointer new_column_info)
17929 {
17930         EM_DEBUG_FUNC_BEGIN("old_column_info[%p] new_column_info[%p]", old_column_info, new_column_info);
17931         email_column_info_t *left_one  = (email_column_info_t*)old_column_info;
17932         email_column_info_t *right_one = (email_column_info_t*)new_column_info;
17933
17934         if (old_column_info == NULL || new_column_info == NULL)
17935                 return -1;
17936
17937         return EM_SAFE_STRCMP((char*)left_one->column_name, (char*)right_one->column_name);
17938 }
17939
17940 INTERNAL_FUNC int emstorage_update_db_table_schema(char *multi_user_name)
17941 {
17942         EM_DEBUG_FUNC_BEGIN();
17943         int i = 0;
17944         int j = 0;
17945         int error = EMAIL_ERROR_NONE;
17946         int query_len = 0;
17947         email_column_info_t *new_column_info = NULL;
17948         email_column_info_t *p_column_info = NULL;
17949         char **create_table_query = NULL;
17950         GList *found_data = NULL;
17951         GList *column_list_of_old_table = NULL;
17952         GList *column_list_of_new_table = NULL;
17953         char table_names[CREATE_TABLE_MAX][2][50] = { { "mail_account_tbl", "mail_account_tbl_new" },
17954                 { "mail_box_tbl", "mail_box_tbl_new" },
17955                 { "mail_read_mail_uid_tbl", "mail_read_mail_uid_tbl_new" },
17956                 { "mail_rule_tbl", "mail_rule_tbl_new" },
17957                 { "mail_tbl", "mail_tbl_new" },
17958                 { "mail_attachment_tbl", "mail_attachment_tbl_new" },
17959 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
17960                 { "mail_partial_body_activity_tbl", "mail_partial_body_activity_tbl_new" },
17961 #else
17962                 { "", "" },
17963 #endif
17964                 { "mail_meeting_tbl", "mail_meeting_tbl_new" },
17965 #ifdef __FEATURE_LOCAL_ACTIVITY__
17966                 { "mail_local_activity_tbl", "mail_local_activity_tbl_new" },
17967 #else
17968                 { "", "" },
17969 #endif
17970                 { "mail_certificate_tbl", "mail_certificate_tbl_new" },
17971                 { "mail_task_tbl", "mail_task_tbl_new" },
17972 #ifdef __FEATURE_BODY_SEARCH__
17973                 { "mail_text_tbl", "mail_text_tbl_new" },
17974 #else
17975                 { "", "" },
17976 #endif
17977
17978 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
17979                 { "mail_auto_download_activity_tbl", "mail_auto_download_activity_tbl_new" }
17980 #else
17981                 { "", "" }
17982 #endif
17983
17984         };
17985
17986         error = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
17987
17988         if (error != EMAIL_ERROR_NONE) {
17989                 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
17990                 goto FINISH_OFF;
17991         }
17992
17993         if (query_len < CREATE_TABLE_MAX) {
17994                 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
17995                 error = EMAIL_ERROR_SYSTEM_FAILURE;
17996                 goto FINISH_OFF;
17997         }
17998
17999         for (i = CREATE_TABLE_MAIL_ACCOUNT_TBL; i < CREATE_TABLE_MAX; i++) {
18000                 EM_DEBUG_LOG("table [%s] new_table [%s]", table_names[i][0], table_names[i][1]);
18001                 if (EM_SAFE_STRLEN(table_names[i][0]) && EM_SAFE_STRLEN(table_names[i][1])) {
18002                         /* Check existing of _new table */
18003                         emstorage_drop_table(multi_user_name, table_names[i][1]);
18004                         error = emstorage_create_renamed_table(multi_user_name, create_table_query, i, table_names[i][0], table_names[i][1]);
18005                         if (error != EMAIL_ERROR_NONE) {
18006                                 EM_DEBUG_EXCEPTION("emstorage_create_renamed_table failed [%d]", error);
18007                                 goto FINISH_OFF;
18008                         }
18009
18010                         emstorage_get_column_information_from_table(multi_user_name, table_names[i][0], &column_list_of_old_table);
18011                         emstorage_get_column_information_from_table(multi_user_name, table_names[i][1], &column_list_of_new_table);
18012
18013                         /* Compare fields and add new field */
18014                         for (j = 0; j < g_list_length(column_list_of_new_table); j++) {
18015                                 new_column_info = (email_column_info_t*)g_list_nth_data(column_list_of_new_table, j);
18016                                 found_data = g_list_find_custom(column_list_of_old_table, (gconstpointer)new_column_info, glist_compare_column_name);
18017                                 if (found_data == NULL) {
18018                                         /* add new field*/
18019                                         emstorage_add_column(multi_user_name, table_names[i][0], new_column_info);
18020                                 }
18021                         }
18022
18023                         emstorage_drop_table(multi_user_name, table_names[i][1]);
18024                 } else {
18025                         EM_DEBUG_LOG("Skipped");
18026                 }
18027         }
18028
18029 FINISH_OFF:
18030         if (create_table_query) {
18031                 int i = 0;
18032                 for (i = 0; i < query_len; i++) {
18033                         if (create_table_query[i])
18034                                 EM_SAFE_FREE(create_table_query[i]);
18035                 }
18036                 EM_SAFE_FREE(create_table_query);
18037         }
18038
18039         found_data = g_list_first(column_list_of_old_table);
18040         while (found_data != NULL) {
18041                 p_column_info = (email_column_info_t *)found_data->data;
18042                 EM_SAFE_FREE(p_column_info->column_name);
18043                 EM_SAFE_FREE(p_column_info->column_type);
18044                 EM_SAFE_FREE(p_column_info);
18045
18046                 found_data = g_list_next(found_data);
18047         }
18048         g_list_free(column_list_of_old_table);
18049
18050         found_data = g_list_first(column_list_of_new_table);
18051         while (found_data != NULL) {
18052                 p_column_info = (email_column_info_t *)found_data->data;
18053                 EM_SAFE_FREE(p_column_info->column_name);
18054                 EM_SAFE_FREE(p_column_info->column_type);
18055                 EM_SAFE_FREE(p_column_info);
18056
18057                 found_data = g_list_next(found_data);
18058         }
18059         g_list_free(column_list_of_new_table);
18060
18061         EM_DEBUG_FUNC_END("error [%d]", error);
18062         return error;
18063 }
18064 #endif /* __FEATURE_UPDATE_DB_TABLE_SCHEMA__ */
18065
18066 /*EOF*/