fixed build warning
[platform/core/messaging/email-service.git] / email-core / email-storage / email-storage.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21
22
23 /******************************************************************************
24  * File: email-storage.c
25  * Desc: email-service Storage Library on Sqlite3
26  *
27  * Auth:
28  *
29  * History:
30  *      2007.03.02 : created
31  *****************************************************************************/
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <stdbool.h>
37 #include <time.h>
38 #include <sys/types.h>
39 #include <sys/time.h>
40 #include <sys/wait.h>
41 #include <glib.h>
42 #include <sys/stat.h>
43 #include <unistd.h>
44 #include <errno.h>
45 #include <dirent.h>
46 #include <vconf.h>
47 #include <sys/mman.h>
48 #include <fcntl.h>
49 #include <tzplatform_config.h>
50
51 #define __USE_UNIX98
52 #define __USE_GNU
53 #include <pthread.h>
54
55 #include "email-internal-types.h"
56 #include "email-convert.h"
57 #include "email-core-utils.h"
58 #include "email-utilities.h"
59 #include "email-storage.h"
60 #include "email-debug-log.h"
61 #include "email-types.h"
62 #include "email-convert.h"
63 #include "email-core-signal.h"
64 #include "email-core-event.h"
65 #include "email-core-container.h"
66 #include "email-core-gmime.h"
67 #include "email-core-key-manager.h"
68
69 #define SETTING_MEMORY_TEMP_FILE_PATH "/tmp/email_tmp_file"
70 #define CONTENT_DATA                  "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset="
71 #define CONTENT_TYPE_DATA             "<meta http-equiv=\"Content-Type\" content=\"text/html; charset="
72
73 #define FLAG_SEEN       0x01
74 #define FLAG_DELETED    0x02
75 #define FLAG_FLAGGED    0x04
76 #define FLAG_ANSWERED   0x08
77 #define FLAG_OLD        0x10
78 #define FLAG_DRAFT      0x20
79
80 #undef close
81
82 #define ISSUE_ORGANIZATION_LEN_IN_MAIL_CERTIFICATE_TBL  256
83 #define EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL       128
84 #define SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL      1027
85 #define FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL           256
86
87 #define ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL            50
88 #define RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL   50
89 #define EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL              128
90 #define USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL               50
91 #define PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL                50
92 #define SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL     50
93 #define SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL            50
94 #define SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL        50
95 #define DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL            30
96 #define REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL           128
97 #define RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL             128
98 #define LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL          256
99 #define DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL       256
100 #define SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL               256
101 #define MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL                128
102 #define ALIAS_LEN_IN_MAIL_BOX_TBL                       128
103 #define LOCAL_MBOX_LEN_IN_MAIL_READ_MAIL_UID_TBL        128
104 #define MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL      128
105 #define S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL             128
106 #define DATA2_LEN_IN_MAIL_READ_MAIL_UID_TBL             256
107 #define FILTER_NAME_LEN_IN_MAIL_RULE_TBL                256
108 #define VALUE_LEN_IN_MAIL_RULE_TBL                      256
109 #define VALUE2_LEN_IN_MAIL_RULE_TBL                     256
110 #define DEST_MAILBOX_LEN_IN_MAIL_RULE_TBL               128
111 #define MAILBOX_NAME_LEN_IN_MAIL_ATTACHMENT_TBL         128
112 #define ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL      256
113 #define ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL      256
114 #define CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL           256
115 #define ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL 128
116 #define MAILBOX_LEN_IN_MAIL_TBL                         128
117 #define SERVER_MAILBOX_LEN_IN_MAIL_TBL                  128
118 #define SERVER_MAIL_ID_LEN_IN_MAIL_TBL                  128
119 #define FROM_LEN_IN_MAIL_TBL                            256
120 #define SENDER_LEN_IN_MAIL_TBL                          256
121 #define REPLY_TO_LEN_IN_MAIL_TBL                        256
122 #define TO_LEN_IN_MAIL_TBL                              3999
123 #define CC_LEN_IN_MAIL_TBL                              3999
124 #define BCC_LEN_IN_MAIL_TBL                             3999
125 #define RETURN_PATH_LEN_IN_MAIL_TBL                     3999
126 #define SUBJECT_LEN_IN_MAIL_TBL                         1027
127 #define THREAD_TOPIC_LEN_IN_MAIL_TBL                    256
128 #define TEXT_1_LEN_IN_MAIL_TBL                          256
129 #define TEXT_2_LEN_IN_MAIL_TBL                          256
130 #define MIME_ENTITY_LEN_IN_MAIL_TBL                     256
131 #define DATETIME_LEN_IN_MAIL_TBL                        128
132 #define MESSAGE_ID_LEN_IN_MAIL_TBL                      256
133 #define FROM_CONTACT_NAME_LEN_IN_MAIL_TBL               3999
134 #define FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL              3999
135 #define TO_CONTACT_NAME_LEN_IN_MAIL_TBL                 3999
136 #define TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL                3999
137 #define MAILBOX_LEN_IN_MAIL_MEETING_TBL                 128
138 #define LOCATION_LEN_IN_MAIL_MEETING_TBL                1024
139 #define GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL        512
140 #define STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL           32
141 #define DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL           32
142 #define PREVIEWBODY_LEN_IN_MAIL_TBL                     512
143 #define CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL        256
144
145 /*  this define is used for query to change data (delete, insert, update) */
146 #define EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction_flag, error_code) \
147         do { \
148                 if (transaction_flag) { \
149                         if (emstorage_begin_transaction(multi_user_name, NULL, NULL, &error_code) == false) { \
150                                 EM_DEBUG_EXCEPTION("emstorage_begin_transaction error [%d]", error_code); \
151                                 goto FINISH_OFF; \
152                         } \
153                 } \
154         } while (0)
155
156 /*  this define is used for query to change data (delete, insert, update) */
157 #define EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction_flag, result_code, error_code) \
158         do { \
159                 if (transaction_flag) { \
160                         if (result_code == true) { \
161                                 if (emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL) == false) { \
162                                         EM_DEBUG_EXCEPTION("emstorage_commit_transaction error"); \
163                                         error_code = EMAIL_ERROR_DB_FAILURE; \
164                                         result_code = false; \
165                                 } \
166                         } \
167                         else { \
168                                 if (emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL) == false) { \
169                                         EM_DEBUG_EXCEPTION("emstorage_rollback_transaction error"); \
170                                         error_code = EMAIL_ERROR_DB_FAILURE; \
171                                 } \
172                         } \
173                 } \
174         } while (0)
175
176 /*  this define is used for query to read (select) */
177 #define EMSTORAGE_START_READ_TRANSACTION(transaction_flag) \
178         if (transaction_flag) { \
179                 /*_timedlock_shm_mutex(mapped_for_db_lock, 2);*/\
180         }
181
182 /*  this define is used for query to read (select) */
183 #define EMSTORAGE_FINISH_READ_TRANSACTION(transaction_flag) \
184         if (transaction_flag) { \
185                 /*_unlockshm_mutex(mapped_for_db_lock);*/\
186         }
187
188 /*  for safety DB operation */
189 static pthread_mutex_t _db_handle_lock = PTHREAD_MUTEX_INITIALIZER;
190
191
192 #define _MULTIPLE_DB_HANDLE
193
194 #ifdef _MULTIPLE_DB_HANDLE
195
196 typedef struct {
197     char *user_name;
198         pthread_t       thread_id;
199         sqlite3 *db_handle;
200 } db_handle_t;
201
202 #define MAX_DB_CLIENT 100
203
204 /* static int _db_handle_count = 0; */
205 db_handle_t _db_handle_list[MAX_DB_CLIENT] = {{NULL, 0, 0}, };
206
207
208 sqlite3 *emstorage_get_db_handle(char *multi_user_name)
209 {
210         EM_DEBUG_FUNC_BEGIN();
211         int i;
212         pthread_t current_thread_id = THREAD_SELF();
213         sqlite3 *result_db_handle = NULL;
214
215         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
216                 ENTER_CRITICAL_SECTION(_db_handle_lock);
217                 for (i = 0; i < MAX_DB_CLIENT; i++) {
218                         if (EM_SAFE_STRCASECMP(_db_handle_list[i].user_name, multi_user_name) != 0)
219                                 continue;
220
221                         if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
222                                 EM_DEBUG_LOG_DEV("found db handle at [%d]", i);
223                                 result_db_handle = _db_handle_list[i].db_handle;
224                                 break;
225                         }
226                 }
227                 LEAVE_CRITICAL_SECTION(_db_handle_lock);
228         } else {
229                 ENTER_CRITICAL_SECTION(_db_handle_lock);
230                 for (i = 0; i < MAX_DB_CLIENT; i++) {
231                         if (EM_SAFE_STRLEN(_db_handle_list[i].user_name) > 0)
232                                 continue;
233
234                         if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
235                                 EM_DEBUG_LOG_DEV("found db handle at [%d]", i);
236                                 result_db_handle = _db_handle_list[i].db_handle;
237                                 break;
238                         }
239                 }
240                 LEAVE_CRITICAL_SECTION(_db_handle_lock);
241         }
242
243         if (!result_db_handle)
244                 EM_DEBUG_LOG("no db_handle for [%d] found", current_thread_id);
245
246         EM_DEBUG_FUNC_END();
247         return result_db_handle;
248 }
249
250 int emstorage_set_db_handle(char *multi_user_name, sqlite3 *db_handle)
251 {
252         EM_DEBUG_FUNC_BEGIN();
253         int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
254         pthread_t current_thread_id = THREAD_SELF();
255
256         ENTER_CRITICAL_SECTION(_db_handle_lock);
257         for (i = 0; i < MAX_DB_CLIENT; i++)     {
258                 if (_db_handle_list[i].thread_id == 0) {
259                         _db_handle_list[i].thread_id = current_thread_id;
260                         _db_handle_list[i].db_handle = db_handle;
261             /* Only distinguished container and host  */
262             _db_handle_list[i].user_name = EM_SAFE_STRDUP(multi_user_name);
263                         EM_DEBUG_LOG("current_thread_id [%d], index [%d]", current_thread_id, i);
264                         error_code =  EMAIL_ERROR_NONE;
265                         break;
266                 }
267         }
268         LEAVE_CRITICAL_SECTION(_db_handle_lock);
269
270         if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
271                 EM_DEBUG_EXCEPTION("Exceeded the limitation of db client. Can't find empty slot in _db_handle_list.");
272
273         EM_DEBUG_FUNC_END("error_code [%d]", error_code);
274         return error_code;
275 }
276
277 int emstorage_remove_db_handle()
278 {
279         EM_DEBUG_FUNC_BEGIN();
280         int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
281         ENTER_CRITICAL_SECTION(_db_handle_lock);
282         for (i = 0; i < MAX_DB_CLIENT; i++) {
283                 if (_db_handle_list[i].thread_id == THREAD_SELF()) {
284                         _db_handle_list[i].thread_id = 0;
285                         _db_handle_list[i].db_handle = NULL;
286             EM_SAFE_FREE(_db_handle_list[i].user_name);
287
288                         EM_DEBUG_LOG("index [%d]", i);
289                         error_code = EMAIL_ERROR_NONE;
290                         break;
291                 }
292         }
293         LEAVE_CRITICAL_SECTION(_db_handle_lock);
294
295         if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
296                 EM_DEBUG_EXCEPTION("Can't find proper thread_id");
297
298         EM_DEBUG_FUNC_END("error_code [%d]", error_code);
299         return error_code;
300 }
301
302 int emstorage_reset_db_handle_list()
303 {
304         EM_DEBUG_FUNC_BEGIN();
305         int i;
306
307         ENTER_CRITICAL_SECTION(_db_handle_lock);
308         for (i = 0; i < MAX_DB_CLIENT; i++) {
309                 _db_handle_list[i].thread_id = 0;
310                 _db_handle_list[i].db_handle = NULL;
311         EM_SAFE_FREE(_db_handle_list[i].user_name);
312         }
313         LEAVE_CRITICAL_SECTION(_db_handle_lock)
314
315         EM_DEBUG_FUNC_END();
316         return EMAIL_ERROR_NONE;
317 }
318
319
320 sqlite3 *emstorage_get_db_connection(char *multi_user_name)
321 {
322         return emstorage_db_open(multi_user_name, NULL);
323 }
324
325 #else   /*  _MULTIPLE_DB_HANDLE */
326
327 sqlite3 *_db_handle = NULL;
328
329 sqlite3 *emstorage_get_db_connection()
330 {
331         if (NULL == _db_handle)
332                 emstorage_db_open(NULL);
333         return _db_handle;
334 }
335 #endif  /*  _MULTIPLE_DB_HANDLE */
336
337 /* ------------------------------------------------------------------------------ */
338 /*  Mutex using shared memory */
339 typedef struct {
340         pthread_mutex_t mutex;
341         int data;
342 } mmapped_t;
343
344 mmapped_t       *mapped_for_db_lock = NULL;
345 int              shm_fd_for_db_lock = 0;
346
347 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
348 mmapped_t       *mapped_for_generating_mail_id = NULL;
349 int              shm_fd_for_generating_mail_id = 0;
350 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
351
352 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__
353 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
354         do {\
355                 _timedlock_shm_mutex(mapped_for_db_lock, 2);\
356                 return_value = function_call;\
357                 _unlockshm_mutex(mapped_for_db_lock);\
358         } while (0)
359
360 #else /*  __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
361 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
362         {  return_value = function_call; }
363 #endif /*  __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
364
365 static int emstorage_exec_query_by_prepare_v2(sqlite3 *local_db_handle, char *query_string)
366 {
367         EM_DEBUG_FUNC_BEGIN("local_db_handle[%p] query_string[%p]", local_db_handle, query_string);
368         int error = EMAIL_ERROR_NONE;
369         int rc = 0;
370         DB_STMT db_statement = NULL;
371
372         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, query_string, EM_SAFE_STRLEN(query_string), &db_statement, NULL), rc);
373         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
374                 ("sqlite3_prepare failed [%d] [%s]", rc, query_string));
375
376         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(db_statement), rc);
377         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
378         ("sqlite3_step failed [%d] [%s]", rc, query_string));
379
380 FINISH_OFF:
381
382         if (db_statement != NULL) {
383                 rc = sqlite3_finalize(db_statement);
384                 if (rc != SQLITE_OK) {
385                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
386                         error = EMAIL_ERROR_DB_FAILURE;
387                 }
388         }
389
390         return error;
391 }
392
393 INTERNAL_FUNC int emstorage_shm_file_init(const char *shm_file_name)
394 {
395         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
396         char errno_buf[ERRNO_BUF_SIZE] = {0};
397
398         if (!shm_file_name) {
399                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
400                 return EMAIL_ERROR_INVALID_PARAM;
401         }
402
403         int fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); /*  note: permission is not working */
404         if (fd < 0) {
405                 EM_DEBUG_EXCEPTION("shm_open errno [%d] [%s]", errno, EM_STRERROR(errno_buf));
406                 return EMAIL_ERROR_SYSTEM_FAILURE;
407         }
408
409         fchmod(fd, 0666);
410         EM_DEBUG_LOG("** Create SHM FILE **");
411         if (ftruncate(fd, sizeof(mmapped_t)) != 0) {
412                 EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
413                 return EMAIL_ERROR_SYSTEM_FAILURE;
414         }
415
416         mmapped_t *m = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
417         if (m == MAP_FAILED) {
418                 EM_DEBUG_EXCEPTION("mmap errno [%d]", errno);
419                 EM_SAFE_CLOSE(fd);
420                 return EMAIL_ERROR_SYSTEM_FAILURE;
421         }
422
423         m->data = 0;
424
425         pthread_mutexattr_t mattr;
426         pthread_mutexattr_init(&mattr);
427         pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
428         pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
429         pthread_mutex_init(&(m->mutex), &mattr);
430         pthread_mutexattr_destroy(&mattr);
431
432     pthread_mutex_destroy(&(m->mutex));
433     munmap(m, sizeof(mmapped_t));
434
435         EM_SAFE_CLOSE(fd);
436         EM_DEBUG_FUNC_END();
437         return EMAIL_ERROR_NONE;
438 }
439
440 int emstorage_shm_file_destroy(const char *shm_file_name)
441 {
442         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
443         char errno_buf[ERRNO_BUF_SIZE] = {0};
444
445         if (!shm_file_name) {
446                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
447                 return EMAIL_ERROR_INVALID_PARAM;
448         }
449
450         if (shm_unlink(shm_file_name) != 0)
451                 EM_DEBUG_EXCEPTION("shm_unlink failed: %s", EM_STRERROR(errno_buf));
452         EM_DEBUG_FUNC_END();
453         return EMAIL_ERROR_NONE;
454 }
455
456 int _initialize_shm_mutex(const char *shm_file_name, int *param_shm_fd, mmapped_t **param_mapped)
457 {
458         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p] param_shm_fd [%p], param_mapped [%p]", shm_file_name, param_shm_fd, param_mapped);
459         char errno_buf[ERRNO_BUF_SIZE] = {0};
460
461         if (!shm_file_name || !param_shm_fd || !param_mapped) {
462                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
463                 return EMAIL_ERROR_INVALID_PARAM;
464         }
465
466         if (!(*param_mapped)) {
467                 EM_DEBUG_LOG("** mapping begin **");
468                 if (!(*param_shm_fd)) { /*  open shm_file_name at first. Otherwise, the num of files in /proc/pid/fd will be increasing  */
469                         *param_shm_fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
470                         if ((*param_shm_fd) == -1) {
471                                 EM_DEBUG_EXCEPTION("FAIL: shm_open(): %s", EM_STRERROR(errno_buf));
472                                 return EMAIL_ERROR_SYSTEM_FAILURE;
473                         }
474                 }
475
476         fchmod((*param_shm_fd), 0666);
477         EM_DEBUG_LOG("** Create SHM FILE **");
478         if (ftruncate((*param_shm_fd), sizeof(mmapped_t)) != 0) {
479             EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
480             return EMAIL_ERROR_SYSTEM_FAILURE;
481         }
482
483                 mmapped_t *tmp = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ|PROT_WRITE, MAP_SHARED, (*param_shm_fd), 0);
484                 if (tmp == MAP_FAILED) {
485                         EM_DEBUG_EXCEPTION("mmap failed: %s", EM_STRERROR(errno_buf));
486                         return EMAIL_ERROR_SYSTEM_FAILURE;
487                 }
488
489         tmp->data = 0;
490
491         pthread_mutexattr_t mattr;
492         pthread_mutexattr_init(&mattr);
493         pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
494         pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
495         pthread_mutex_init(&(tmp->mutex), &mattr);
496         pthread_mutexattr_destroy(&mattr);
497
498                 *param_mapped = tmp;
499         }
500
501         EM_DEBUG_FUNC_END();
502         return EMAIL_ERROR_NONE;
503 }
504
505 int _timedlock_shm_mutex(mmapped_t *param_mapped, int sec)
506 {
507         EM_DEBUG_FUNC_BEGIN("param_mapped [%p], sec [%d]", param_mapped, sec);
508
509         if (!param_mapped) {
510                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
511                 return EMAIL_ERROR_INVALID_PARAM;
512         }
513
514         struct timespec abs_time;
515         clock_gettime(CLOCK_REALTIME, &abs_time);
516         abs_time.tv_sec += sec;
517         char errno_buf[ERRNO_BUF_SIZE] = {0};
518
519         int err = pthread_mutex_timedlock(&(param_mapped->mutex), &abs_time);
520
521         if (err == EOWNERDEAD) {
522                 err = pthread_mutex_consistent(&(param_mapped->mutex));
523                 EM_DEBUG_EXCEPTION("Previous owner is dead with lock. Fix mutex : %s", EM_STRERROR(errno_buf));
524         } else if (err != 0) {
525                 EM_DEBUG_EXCEPTION("ERROR : %s", EM_STRERROR(errno_buf));
526                 return err;
527         }
528
529         EM_DEBUG_FUNC_END();
530         return EMAIL_ERROR_NONE;
531 }
532
533 void _unlockshm_mutex(mmapped_t *param_mapped)
534 {
535         EM_DEBUG_FUNC_BEGIN();
536         pthread_mutex_unlock(&(param_mapped->mutex));
537         EM_DEBUG_FUNC_END();
538 }
539 /* ------------------------------------------------------------------------------ */
540
541
542 static int _open_counter = 0;
543
544 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name);
545 static int _read_password_from_secure_storage(char *file_name, char **password);
546
547 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
548 static int _get_cert_password_file_name(int index, char *cert_password_file_name);
549 #endif
550
551 typedef struct {
552         const char *object_name;
553         unsigned int data_flag;
554 } email_db_object_t;
555
556 static const email_db_object_t _g_db_tables[] = {
557         { "mail_read_mail_uid_tbl", 1},
558         { "mail_tbl", 1},
559         { "mail_attachment_tbl", 1},
560         { NULL,  0},
561 };
562
563 static const email_db_object_t _g_db_indexes[] = {
564         { "mail_read_mail_uid_idx1", 1},
565         { "mail_idx1", 1},
566         { "mail_attachment_idx1", 1},
567         { NULL,  1},
568 };
569
570 enum {
571         CREATE_TABLE_MAIL_ACCOUNT_TBL,
572         CREATE_TABLE_MAIL_BOX_TBL,
573         CREATE_TABLE_MAIL_READ_MAIL_UID_TBL,
574         CREATE_TABLE_MAIL_RULE_TBL,
575         CREATE_TABLE_MAIL_TBL,
576         CREATE_TABLE_MAIL_ATTACHMENT_TBL,
577 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
578         CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL,
579 #else
580         CREATE_TABLE_DUMMY_INDEX1,
581 #endif
582         CREATE_TABLE_MAIL_MEETING_TBL,
583 #ifdef __FEATURE_LOCAL_ACTIVITY__
584         CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL,
585 #else
586         CREATE_TABLE_DUMMY_INDEX2,
587 #endif
588         CREATE_TABLE_MAIL_CERTIFICATE_TBL,
589         CREATE_TABLE_MAIL_TASK_TBL,
590 #ifdef __FEATURE_BODY_SEARCH__
591         CREATE_TABLE_MAIL_TEXT_TBL,
592 #else
593         CREATE_TABLE_DUMMY_INDEX3,
594 #endif
595
596 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
597         CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
598 #else
599         CREATE_TABLE_DUMMY_INDEX4,
600 #endif
601
602         /*CREATE INDEX*/
603         CREATE_TABLE_MAIL_ACCOUNT_IDX,
604         CREATE_TABLE_MAIL_BOX_IDX,
605         CREATE_TABLE_MAIL_READ_MAIL_UID_IDX,
606         CREATE_TABLE_MAIL_IDX,
607         CREATE_TABLE_MAIL_ATTACHMENT_IDX,
608         CREATE_TABLE_MAIL_MEETING_IDX,
609         CREATE_TABLE_MAIL_TASK_IDX,
610         CREATE_TABLE_MAIL_DATETIME_IDX,
611         CREATE_TABLE_MAIL_THREAD_IDX,
612         CREATE_TABLE_MAX,
613 };
614
615 enum {
616         DATA1_IDX_IN_MAIL_ACTIVITY_TBL = 0,
617         TRANSTYPE_IDX_IN_MAIL_ACTIVITY_TBL,
618         FLAG_IDX_IN_MAIL_ACTIVITY_TBL,
619 };
620
621 enum {
622         CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL = 0,
623         ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
624         ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
625         ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
626         EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
627         EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
628         EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
629         ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL,
630         EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL,
631         SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL,
632         FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL,
633 };
634
635 enum {
636         TO_RECIPIENT = 0,
637         CC_RECIPIENT,
638         BCC_RECIPIENT,
639 };
640 enum {
641         ACCOUNT_ID_IDX_IN_MAIL_BOX_TBL = 0,
642         LOCAL_YN_IDX_IN_MAIL_BOX_TBL,
643         MAILBOX_NAME_IDX_IN_MAIL_BOX_TBL,
644         MAILBOX_TYPE_IDX_IN_MAIL_BOX_TBL,
645         ALIAS_IDX_IN_MAIL_BOX_TBL,
646         SYNC_WITH_SERVER_YN_IDX_IN_MAIL_BOX_TBL,
647         MODIFIABLE_YN_IDX_IN_MAIL_BOX_TBL,
648         TOTAL_MAIL_COUNT_ON_SERVER_IDX_IN_MAIL_BOX_TBL,
649         ARCHIVE_IDX_IN_MAIL_BOX_TBL,
650         MAIL_SLOT_SIZE_IDX_IN_MAIL_BOX_TBL,
651 };
652
653 enum {
654         ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL = 0,
655         LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
656         MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL,
657         LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
658         SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
659         RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL,
660         SYNC_STATUS_IDX_IN_MAIL_READ_MAIL_UID_TBL,
661         FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL,
662         FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL,
663         IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL,          /* unused */
664 };
665
666 #ifdef __FEATURE_BODY_SEARCH__
667 enum {
668         MAIL_ID_IDX_IN_MAIL_TEXT_TBL = 0,
669         ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL,
670         MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL,
671         BODY_TEXT_IDX_IN_MAIL_TEXT_TBL,
672         FIELD_COUNT_OF_MAIL_TEXT_TBL,
673 };
674 #endif
675
676 enum {
677         ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL = 0,
678         RULE_ID_IDX_IN_MAIL_RULE_TBL,
679         FILTER_NAME_IDX_IN_MAIL_RULE_TBL,
680         TYPE_IDX_IN_MAIL_RULE_TBL,
681         VALUE_IDX_IN_MAIL_RULE_TBL,
682         VALUE2_IDX_IN_MAIL_RULE_TBL,
683         ACTION_TYPE_IDX_IN_MAIL_RULE_TBL,
684         TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL,
685         FLAG1_IDX_IN_MAIL_RULE_TBL,
686         FLAG2_IDX_IN_MAIL_RULE_TBL,
687 };
688
689 enum {
690         MAIL_ID_IDX_IN_MAIL_TBL = 0,
691         ACCOUNT_ID_IDX_IN_MAIL_TBL,
692         MAILBOX_ID_IDX_IN_MAIL_TBL,
693         MAILBOX_TYPE_IDX_IN_MAIL_TBL,
694         SUBJECT_IDX_IN_MAIL_TBL,
695         DATETIME_IDX_IN_MAIL_TBL,
696         SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL,
697         SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL,
698         SERVER_MAIL_ID_IDX_IN_MAIL_TBL,
699         MESSAGE_ID_IDX_IN_MAIL_TBL,
700         REFERENCE_ID_IDX_IN_MAIL_TBL,
701         FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL,
702         FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL,
703         FULL_ADDRESS_TO_IDX_IN_MAIL_TBL,
704         FULL_ADDRESS_CC_IDX_IN_MAIL_TBL,
705         FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL,
706         FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL,
707         EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL,
708         EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL,
709         ALIAS_SENDER_IDX_IN_MAIL_TBL,
710         ALIAS_RECIPIENT_IDX_IN_MAIL_TBL,
711         BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL,
712         FILE_PATH_PLAIN_IDX_IN_MAIL_TBL,
713         FILE_PATH_HTML_IDX_IN_MAIL_TBL,
714         FILE_PATH_MIME_ENTITY_IDX_IN_MAIL_TBL,
715         MAIL_SIZE_IDX_IN_MAIL_TBL,
716         FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL,
717         FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL,
718         FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL,
719         FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL,
720         FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL,
721         FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL,
722         FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL,
723         DRM_STATUS_IDX_IN_MAIL_TBL,
724         PRIORITY_IDX_IN_MAIL_TBL,
725         SAVE_STATUS_IDX_IN_MAIL_TBL,
726         LOCK_STATUS_IDX_IN_MAIL_TBL,
727         REPORT_STATUS_IDX_IN_MAIL_TBL,
728         ATTACHMENT_COUNT_IDX_IN_MAIL_TBL,
729         INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL,
730         THREAD_ID_IDX_IN_MAIL_TBL,
731         THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL,
732         PREVIEW_TEXT_IDX_IN_MAIL_TBL,
733         MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL,
734         MESSAGE_CLASS_IDX_IN_MAIL_TBL,
735         DIGEST_TYPE_IDX_IN_MAIL_TBL,
736         SMIME_TYPE_IDX_IN_MAIL_TBL,
737         SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL,
738         REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL,
739         TAG_ID_IDX_IN_MAIL_TBL,
740         REPLIED_TIME_IDX_IN_MAIL_TBL,
741         FORWARDED_TIME_IDX_IN_MAIL_TBL,
742         DEFAULT_CHARSET_IDX_IN_MAIL_TBL,
743         EAS_DATA_LENGTH_IDX_IN_MAIL_TBL,
744         EAS_DATA_IDX_IN_MAIL_TBL,
745         USER_NAME_IDX_IN_MAIL_TBL,
746         FIELD_COUNT_OF_MAIL_TBL,  /* End of mail_tbl */
747 };
748
749 enum {
750         ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL = 0,
751         ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL,
752         ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL,
753         CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
754         ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL,
755         MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
756         ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
757         MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
758         ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
759         ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
760         ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL,
761         ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
762         ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
763 #ifdef __ATTACHMENT_OPTI__
764         ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL,
765         SECTION_IDX_IN_MAIL_ATTACHMENT_TBL,
766 #endif
767 };
768
769 enum {
770         IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL = 0,
771 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
772         MAIL_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
773         ACCOUNT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
774         ADDRESS_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
775         ADDRESS_IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL,
776 #endif
777         ADDRESS_IDX_IN_MAIL_CONTACT_SYNC_TBL,
778         CONTACT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
779         STORAGE_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
780         CONTACT_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
781 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
782         DISPLAY_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
783         FLAG1_IDX_IN_MAIL_CONTACT_SYNC_TBL,
784 #endif
785 };
786
787 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
788 enum {
789         ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL = 0,
790         STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
791         ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
792         MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
793         SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
794         MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
795     MULTI_USER_NAME_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
796 };
797 #endif
798
799 /* sowmya.kr 03032010, changes for get list of mails for given addr list */
800 typedef struct _em_mail_id_list {
801         int mail_id;
802         struct _em_mail_id_list *next;
803 } em_mail_id_list;
804
805 static char *g_test_query[] = {
806                 /*  1. select mail_account_tbl */
807                 "SELECT"
808                 " account_name, "
809                 " incoming_server_type, "
810                 " incoming_server_address, "
811                 " user_email_address, "
812                 " incoming_server_user_name, "
813                 " incoming_server_password, "
814                 " retrieval_mode, "
815                 " incoming_server_port_number, "
816                 " incoming_server_secure_connection, "
817                 " incoming_server_authentication_method,"
818                 " outgoing_server_type, "
819                 " outgoing_server_address, "
820                 " outgoing_server_port_number, "
821                 " outgoing_server_need_authentication, "
822                 " outgoing_server_secure_connection, "
823                 " outgoing_server_user_name, "
824                 " outgoing_server_password, "
825                 " display_name, "
826                 " reply_to_addr, "
827                 " return_addr, "
828                 " account_id, "
829                 " keep_mails_on_pop_server_after_download, "
830                 " auto_resend_times, "
831                 " outgoing_server_size_limit, "
832                 " wifi_auto_download, "
833                 " pop_before_smtp, "
834                 " incoming_server_requires_apop,"
835                 " logo_icon_path, "
836                 " is_preset_account, "
837                 " check_interval, "
838                 " priority, "
839                 " keep_local_copy, "
840                 " req_delivery_receipt, "
841                 " req_read_receipt, "
842                 " download_limit, "
843                 " block_address, "
844                 " block_subject, "
845                 " display_name_from, "
846                 " reply_with_body, "
847                 " forward_with_files, "
848                 " add_myname_card, "
849                 " add_signature, "
850                 " signature"
851                 ", add_my_address_to_bcc"
852                 ", notification_status "
853                 ", vibrate_status "
854                 ", display_content_status "
855                 ", default_ringtone_status "
856                 ", alert_ringtone_path "
857                 ", account_svc_id "
858                 ", index_color "
859                 ", sync_status "
860                 ", sync_disabled "
861                 ", smime_type"
862                 ", certificate_path"
863                 ", cipher_type"
864                 ", digest_type"
865                 " FROM mail_account_tbl",
866                 /*  2. select mail_box_tbl */
867                 "SELECT "
868                 "   mailbox_id, "
869                 "   account_id, "
870                 "   local_yn,  "
871                 "   mailbox_name,  "
872                 "   mailbox_type,   "
873                 "   alias,  "
874                 "   deleted_flag,  "
875                 "   modifiable_yn,  "
876                 "   total_mail_count_on_server,  "
877                 "   has_archived_mails, "
878                 "   mail_slot_size, "
879                 "   no_select, "
880                 "   last_sync_time "
881                 " FROM mail_box_tbl ",
882                 /*  3. select mail_read_mail_uid_tbl */
883                 "SELECT  "
884                 "   account_id,  "
885                 "   mailbox_id,  "
886                 "   mailbox_name,  "
887                 "   local_uid,  "
888                 "   server_uid,  "
889                 "   rfc822_size ,  "
890                 "   sync_status,  "
891                 "   flags_seen_field,  "
892                 "   idx_num "
893                 " FROM mail_read_mail_uid_tbl ",
894                 /*  4. select mail_rule_tbl */
895                 "SELECT "
896                 "   account_id, "
897                 "   rule_id, "
898                 "   filter_name, "
899                 "   type, "
900                 "   value, "
901                 "   value2, "
902                 "   action_type, "
903                 "   target_mailbox_id,  "
904                 "   flag1, "
905                 "   flag2 "
906                 " FROM mail_rule_tbl    ",
907                 /*  5. select mail_tbl */
908                 "SELECT"
909                 "       mail_id, "
910                 "       account_id, "
911                 "       mailbox_id, "
912                 "       mailbox_name, "
913                 "   mailbox_type, "
914                 "   subject, "
915                 "       date_time, "
916                 "       server_mail_status, "
917                 "       server_mailbox_name, "
918                 "       server_mail_id, "
919                 "   message_id, "
920                 "       reference_mail_id, "
921                 "   full_address_from, "
922                 "   full_address_reply, "
923                 "   full_address_to, "
924                 "   full_address_cc, "
925                 "   full_address_bcc, "
926                 "   full_address_return, "
927                 "   email_address_sender, "
928                 "   email_address_recipient, "
929                 "   alias_sender, "
930                 "   alias_recipient, "
931                 "       body_download_status, "
932                 "       file_path_plain, "
933                 "       file_path_html, "
934                 "   file_path_mime_entity, "
935                 "       mail_size, "
936                 "   flags_seen_field     ,"
937                 "   flags_deleted_field  ,"
938                 "   flags_flagged_field  ,"
939                 "   flags_answered_field ,"
940                 "   flags_recent_field   ,"
941                 "   flags_draft_field    ,"
942                 "   flags_forwarded_field,"
943                 "       DRM_status, "
944                 "       priority, "
945                 "       save_status, "
946                 "       lock_status, "
947                 "       report_status, "
948                 "   attachment_count, "
949                 "       inline_content_count, "
950                 "       thread_id, "
951                 "       thread_item_count, "
952                 "   preview_text, "
953                 "       meeting_request_status, "
954                 "   message_class, "
955                 "   digest_type, "
956                 "   smime_type "
957                 " FROM mail_tbl",
958                 /*  6. select mail_attachment_tbl */
959                 "SELECT "
960                 "       attachment_id, "
961                 "       attachment_name, "
962                 "       attachment_path, "
963                 "       attachment_size, "
964                 "       mail_id,  "
965                 "       account_id, "
966                 "       mailbox_id, "
967                 "       attachment_save_status,  "
968                 "       attachment_drm_type,  "
969                 "       attachment_drm_method,  "
970                 "       attachment_inline_content_status,  "
971                 "       attachment_mime_type  "
972                 " FROM mail_attachment_tbl ",
973
974 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
975                 "SELECT  "
976                 "   account_id, "
977                 "   mail_id, "
978                 "   server_mail_id, "
979                 "   activity_id, "
980                 "   activity_type, "
981                 "   mailbox_id, "
982                 "   mailbox_name "
983                 "   multi_user_name "
984                 " FROM mail_partial_body_activity_tbl ",
985 #endif
986
987                 "SELECT  "
988                 "   mail_id, "
989                 "   account_id, "
990                 "   mailbox_id, "
991                 "   meeting_response, "
992                 "   start_time, "
993                 "   end_time, "
994                 "   location, "
995                 "   global_object_id, "
996                 "   offset, "
997                 "   standard_name, "
998                 "   standard_time_start_date, "
999                 "   standard_bias, "
1000                 "   daylight_name, "
1001                 "   daylight_time_start_date, "
1002                 "   daylight_bias "
1003                 " FROM mail_meeting_tbl ",
1004
1005 #ifdef __FEATURE_LOCAL_ACTIVITY__
1006                 "SELECT "
1007                 "       activity_id, "
1008                 "   account_id, "
1009                 "   mail_id, "
1010                 "   activity_type, "
1011                 "   server_mailid, "
1012                 "   src_mbox , "
1013                 "       dest_mbox "
1014                 " FROM mail_local_activity_tbl  ",
1015 #endif
1016                 "SELECT "
1017                 "       certificate_id, "
1018                 "   issue_year, "
1019                 "   issue_month, "
1020                 "   issue_day, "
1021                 "   expiration_year, "
1022                 "   expiration_month, "
1023                 "   expiration_day, "
1024                 "   issue_organization_name, "
1025                 "   email_address, "
1026                 "   subject_str, "
1027                 "   filepath, "
1028                 "   password "
1029                 " FROM mail_certificate_tbl     ",
1030                 "SELECT "
1031                 "       task_id, "
1032                 "   task_type, "
1033                 "   task_status, "
1034                 "   task_priority, "
1035                 "   task_parameter_length, "
1036                 "   task_parameter , "
1037                 "       date_time "
1038                 " FROM mail_task_tbl    ",
1039 #ifdef __FEATURE_BODY_SEARCH__
1040                 "SELECT "
1041                 "       mail_id, "
1042                 "   account_id, "
1043                 "   mailbox_id, "
1044                 "   body_text "
1045                 " FROM mail_text_tbl    ",
1046 #endif
1047 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
1048                 "SELECT  "
1049                 "   activity_id, "
1050                 "   status, "
1051                 "   account_id, "
1052                 "   mail_id, "
1053                 "   server_mail_id, "
1054                 "   mailbox_id, "
1055                 "   multi_user_name, "
1056                 " FROM mail_auto_download_activity_tbl ",
1057 #endif
1058                 NULL,
1059 };
1060
1061 int _field_count_of_table[CREATE_TABLE_MAX] = { 0, };
1062
1063 static int _get_table_field_data_char(char  **table, char *buf, int index)
1064 {
1065         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1066                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1067                 return false;
1068         }
1069
1070         if (table[index] != NULL) {
1071                 *buf = (char)atoi(table[index]);
1072                 return true;
1073         }
1074
1075         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
1076
1077         *buf = 0;
1078         return false;
1079 }
1080
1081 static int _get_table_field_data_int(char  **table, int *buf, int index)
1082 {
1083         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1084                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1085                 return false;
1086         }
1087
1088         if (table[index] != NULL) {
1089                 *buf = atoi(table[index]);
1090                 return true;
1091         }
1092
1093         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
1094
1095         *buf = 0;
1096         return false;
1097 }
1098
1099 static int _get_table_field_data_time_t(char  **table, time_t *buf, int index)
1100 {
1101         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1102                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1103                 return false;
1104         }
1105
1106         if (table[index] != NULL) {
1107                 *buf = (time_t)atol(table[index]);
1108                 return true;
1109         }
1110
1111         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
1112
1113         *buf = 0;
1114         return false;
1115 }
1116
1117 static int _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
1118 {
1119         int ret = false;
1120
1121         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1122                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1123                 return false;
1124         }
1125
1126         char *pTemp = table[index];
1127         int sLen = 0;
1128         if (pTemp == NULL)
1129                 *buf = NULL;
1130         else {
1131                 sLen = EM_SAFE_STRLEN(pTemp);
1132                 if (sLen) {
1133                         *buf = (char *) em_malloc(sLen + 1);
1134                         if (*buf == NULL) {
1135                                 EM_DEBUG_EXCEPTION("malloc is failed");
1136                                 goto FINISH_OFF;
1137                         }
1138                         strncpy(*buf, pTemp, sLen);
1139                 } else
1140                         *buf = NULL;
1141         }
1142 #ifdef _PRINT_STORAGE_LOG_
1143         if (*buf)
1144                 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", *buf, index);
1145         else
1146                 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1147 #endif
1148         ret = true;
1149 FINISH_OFF:
1150
1151         return ret;
1152 }
1153
1154 static int _get_table_field_data_string_without_allocation(char **table, char *buf, int buffer_size, int ucs2, int index)
1155 {
1156         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1157                 EM_DEBUG_EXCEPTION(" table[%p], buf[%p], index[%d]", table, buf, index);
1158                         return false;
1159         }
1160
1161         char *pTemp = table[index];
1162
1163         if (pTemp == NULL)
1164                 buf = NULL;
1165         else {
1166                 memset(buf, 0, buffer_size);
1167                 strncpy(buf, pTemp, buffer_size - 1);
1168         }
1169 #ifdef _PRINT_STORAGE_LOG_
1170         if (buf)
1171                 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", buf, index);
1172         else
1173                 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1174 #endif
1175
1176         return true;
1177 }
1178
1179 static int _get_table_field_data_blob(char **table, void **buffer, int buffer_size, int index)
1180 {
1181         if ((table == NULL) || (buffer == NULL) || (index < 0)) {
1182                 EM_DEBUG_EXCEPTION(" table[%p], buffer[%p], buffer_size [%d], index[%d]", table, buffer, buffer_size, index);
1183                 return false;
1184         }
1185
1186         char *temp_buffer = table[index];
1187
1188         if (temp_buffer == NULL)
1189                 buffer = NULL;
1190         else {
1191                 *buffer = malloc(buffer_size);
1192                 if (*buffer == NULL) {
1193                         EM_DEBUG_EXCEPTION("allocation failed.");
1194                         return false;
1195                 }
1196                 memset(*buffer, 0, buffer_size);
1197                 memcpy(*buffer, temp_buffer, buffer_size);
1198         }
1199 #ifdef _PRINT_STORAGE_LOG_
1200         if (buf)
1201                 EM_DEBUG_LOG("_get_table_field_data_string - buffer[%s], index[%d]", buffer, index);
1202         else
1203                 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1204 #endif
1205
1206         return true;
1207 }
1208
1209 static int _get_stmt_field_data_char(DB_STMT hStmt, char *buf, int index)
1210 {
1211         if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1212                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1213                 return false;
1214         }
1215
1216         if (sqlite3_column_text(hStmt, index) != NULL) {
1217                 *buf = (char)sqlite3_column_int(hStmt, index);
1218 #ifdef _PRINT_STORAGE_LOG_
1219                         EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1220 #endif
1221                 return true;
1222         }
1223
1224         EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1225
1226         return false;
1227 }
1228
1229 static int _get_stmt_field_data_int(DB_STMT hStmt, int *buf, int index)
1230 {
1231         if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1232                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1233                 return false;
1234         }
1235
1236         if (sqlite3_column_text(hStmt, index) != NULL) {
1237                 *buf = sqlite3_column_int(hStmt, index);
1238 #ifdef _PRINT_STORAGE_LOG_
1239                         EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1240 #endif
1241                 return true;
1242         }
1243
1244         EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1245
1246         return false;
1247 }
1248
1249 static int _get_stmt_field_data_time_t(DB_STMT hStmt, time_t *buf, int index)
1250 {
1251         if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1252                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1253                 return false;
1254         }
1255
1256         if (sqlite3_column_text(hStmt, index) != NULL) {
1257                 *buf = (time_t)sqlite3_column_int(hStmt, index);
1258 #ifdef _PRINT_STORAGE_LOG_
1259                 EM_DEBUG_LOG("_get_stmt_field_data_time_t [%d]", *buf);
1260 #endif
1261                 return true;
1262         }
1263
1264         EM_DEBUG_LOG("_get_stmt_field_data_time_t fail. index [%d]", index);
1265         return false;
1266 }
1267
1268 static int _get_stmt_field_data_string(DB_STMT hStmt, char **buf, int ucs2, int index)
1269 {
1270         if (!hStmt || !buf || (index < 0)) { /*prevent 39619*/
1271                 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1272                 return false;
1273         }
1274
1275         int sLen = 0;
1276         sLen = sqlite3_column_bytes(hStmt, index);
1277
1278 #ifdef _PRINT_STORAGE_LOG_
1279                 EM_DEBUG_LOG("_get_stmt_field_data_string sqlite3_column_bytes sLen[%d]", sLen);
1280 #endif
1281
1282         if (sLen > 0) {
1283                 *buf = (char *) em_malloc(sLen + 1);
1284                 if (*buf == NULL) {
1285                         EM_DEBUG_EXCEPTION("em_mallocfailed");
1286                         return false;
1287                 }
1288
1289                 strncpy(*buf, (char *)sqlite3_column_text(hStmt, index), sLen);
1290         } else
1291                 *buf = NULL;
1292
1293 #ifdef _PRINT_STORAGE_LOG_
1294         if (*buf)
1295                 EM_DEBUG_LOG("buf[%s], index[%d]", *buf, index);
1296         else
1297                 EM_DEBUG_LOG("_get_stmt_field_data_string - No string got");
1298 #endif
1299
1300         return false;
1301 }
1302
1303 static void _get_stmt_field_data_blob(DB_STMT hStmt, void **buf, int index)
1304 {
1305         if (!hStmt || !buf || (index < 0)) { /*prevent 39618*/
1306                 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1307                 return;
1308         }
1309
1310         int sLen = 0;
1311         sLen = sqlite3_column_bytes(hStmt, index);
1312
1313 #ifdef _PRINT_STORAGE_LOG_
1314         EM_DEBUG_LOG("_get_stmt_field_data_blob sqlite3_column_bytes sLen[%d]", sLen);
1315 #endif
1316
1317         if (sLen > 0) {
1318                 *buf = (char *) em_malloc(sLen);
1319                 if (*buf == NULL) {
1320                         EM_DEBUG_EXCEPTION("em_mallocfailed");
1321                         return;
1322                 }
1323
1324                 memcpy(*buf, (void *)sqlite3_column_blob(hStmt, index), sLen);
1325         } else
1326                 *buf = NULL;
1327
1328 }
1329
1330 static int _bind_stmt_field_data_char(DB_STMT hStmt, int index, char value)
1331 {
1332         if ((hStmt == NULL) || (index < 0)) {
1333                 EM_DEBUG_EXCEPTION("index[%d]", index);
1334                 return false;
1335         }
1336
1337         int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1338
1339         if (ret != SQLITE_OK) {
1340                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1341                 return false;
1342         }
1343
1344         return true;
1345 }
1346
1347 static int _bind_stmt_field_data_int(DB_STMT hStmt, int index, int value)
1348 {
1349         if ((hStmt == NULL) || (index < 0)) {
1350                 EM_DEBUG_EXCEPTION("index[%d]", index);
1351                 return false;
1352         }
1353
1354         int ret = sqlite3_bind_int(hStmt, index+1, value);
1355
1356         if (ret != SQLITE_OK) {
1357                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1358                 return false;
1359         }
1360
1361         return true;
1362 }
1363
1364 static int _bind_stmt_field_data_time_t(DB_STMT hStmt, int index, time_t value)
1365 {
1366         if ((hStmt == NULL) || (index < 0)) {
1367                 EM_DEBUG_EXCEPTION("index[%d]", index);
1368                 return false;
1369         }
1370
1371         int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1372
1373         if (ret != SQLITE_OK) {
1374                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1375                 return false;
1376         }
1377
1378         return true;
1379 }
1380
1381 static int _bind_stmt_field_data_string(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1382 {
1383         if ((hStmt == NULL) || (index < 0)) {
1384                 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1385                 return false;
1386         }
1387
1388 #ifdef _PRINT_STORAGE_LOG_
1389         EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1390 #endif
1391
1392         int ret = 0;
1393         if (value != NULL)
1394                 ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1395         else
1396                 ret = sqlite3_bind_text(hStmt, index+1, "", -1, NULL);
1397
1398         if (ret != SQLITE_OK) {
1399                 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1400                 return false;
1401         }
1402         return true;
1403 }
1404
1405 /* destroy function for sqlite3_bind_text */
1406 void _bind_stmt_free_string(void* arg)
1407 {
1408         EM_DEBUG_FUNC_BEGIN();
1409         char* p = (char*) arg;
1410         EM_SAFE_FREE(p);
1411         EM_DEBUG_FUNC_END();
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, _bind_stmt_free_string);
1437                 }
1438         } else
1439                 ret = sqlite3_bind_text(hStmt, index+1, "", -1, NULL);
1440
1441         if (ret != SQLITE_OK) {
1442                 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1443                 return false;
1444         }
1445         return true;
1446 }
1447
1448
1449 static int _bind_stmt_field_data_blob(DB_STMT hStmt, int index, void *blob, int blob_size)
1450 {
1451         if ((hStmt == NULL) || (index < 0)) {
1452                 EM_DEBUG_EXCEPTION("index[%d], blob_size[%d]", index, blob_size);
1453                 return false;
1454         }
1455
1456 #ifdef _PRINT_STORAGE_LOG_
1457         EM_DEBUG_LOG("hStmt = %p, index = %d, blob_size = %d, blob = [%p]", hStmt, index, blob_size, blob);
1458 #endif
1459
1460         int ret = 0;
1461         if (blob_size > 0)
1462                 ret = sqlite3_bind_blob(hStmt, index+1, blob, blob_size, SQLITE_STATIC);
1463         else
1464                 ret = sqlite3_bind_null(hStmt, index+1);
1465
1466         if (ret != SQLITE_OK) {
1467                 EM_DEBUG_EXCEPTION("sqlite3_bind_blob fail [%d]", ret);
1468                 return false;
1469         }
1470         return true;
1471 }
1472
1473
1474 static int _delete_temp_file(const char *path)
1475 {
1476         EM_DEBUG_FUNC_BEGIN("path[%p]", path);
1477
1478         DIR *dp = NULL;
1479         struct dirent *entry = NULL;
1480
1481         char buf[1024] = {0x00, };
1482
1483         if ((dp = opendir(path)) == NULL) {
1484                 EM_DEBUG_EXCEPTION("opendir(\"%s\") failed...", path);
1485                 return false;
1486         }
1487
1488         while ((entry = readdir(dp)) != NULL) {
1489                 SNPRINTF(buf, sizeof(buf), "%s/%s", path, entry->d_name);
1490                 remove(buf);
1491         }
1492
1493         closedir(dp);
1494         EM_DEBUG_FUNC_END();
1495         return true;
1496 }
1497
1498 char *cpy_str(char *src)
1499 {
1500         char *p = NULL;
1501
1502         if (src) {
1503                 if (!(p = em_malloc((int)EM_SAFE_STRLEN(src) + 1))) {
1504                         EM_DEBUG_EXCEPTION("mailoc failed...");
1505                         return NULL;
1506                 }
1507                 strncpy(p, src, EM_SAFE_STRLEN(src));
1508         }
1509
1510         return p;
1511 }
1512
1513 static void _emstorage_close_once(void)
1514 {
1515         EM_DEBUG_FUNC_BEGIN();
1516
1517         EM_DEBUG_FUNC_END();
1518 }
1519
1520 INTERNAL_FUNC int emstorage_close(int *err_code)
1521 {
1522         EM_DEBUG_FUNC_BEGIN();
1523
1524         int ret = false;
1525         int error = EMAIL_ERROR_NONE;
1526
1527         if (!emstorage_db_close(NULL, &error))
1528
1529         if (--_open_counter == 0)
1530                 _emstorage_close_once();
1531
1532         ret = true;
1533
1534         if (err_code != NULL)
1535                 *err_code = error;
1536
1537         EM_DEBUG_FUNC_END("ret [%d]", ret);
1538         return ret;
1539 }
1540
1541 static void *_emstorage_open_once(char *multi_user_name, int *err_code)
1542 {
1543         EM_DEBUG_FUNC_BEGIN();
1544
1545         int error = EMAIL_ERROR_NONE;
1546
1547     if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1548         char buf[MAX_PATH] = {0};
1549                 char *prefix_path = NULL;
1550
1551                 error = emcore_get_container_path(multi_user_name, &prefix_path);
1552                 if (error != EMAIL_ERROR_NONE) {
1553                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
1554                         goto FINISH_OFF;
1555                 }
1556
1557         memset(buf, 0x00, sizeof(buf));
1558         SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, EMAILPATH);
1559         mkdir(buf, DIRECTORY_PERMISSION);
1560
1561         memset(buf, 0x00, sizeof(buf));
1562         SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAILHOME);
1563         mkdir(buf, DIRECTORY_PERMISSION);
1564
1565         memset(buf, 0x00, sizeof(buf));
1566         SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAILTEMP);
1567         mkdir(buf, DIRECTORY_PERMISSION);
1568
1569         _delete_temp_file(buf);
1570                 EM_SAFE_FREE(prefix_path);
1571     } else {
1572         mkdir(DATA_PATH, DIRECTORY_PERMISSION);
1573         mkdir(EMAILPATH, DIRECTORY_PERMISSION);
1574         mkdir(MAILHOME, DIRECTORY_PERMISSION);
1575         mkdir(MAILTEMP, DIRECTORY_PERMISSION);
1576
1577         _delete_temp_file(MAILTEMP);
1578     }
1579
1580         if (!emstorage_create_table(multi_user_name, EMAIL_CREATE_DB_NORMAL, &error)) {
1581                 EM_DEBUG_EXCEPTION(" emstorage_create_table failed - %d", error);
1582                 goto FINISH_OFF;
1583         }
1584
1585 FINISH_OFF:
1586
1587
1588         if (err_code != NULL)
1589                 *err_code = error;
1590
1591         return NULL;
1592 }
1593
1594  /*  pData : a parameter which is registered when busy handler is registerd */
1595  /*  count : retry count */
1596 static int _callback_sqlite_busy_handler(void *pData, int count)
1597 {
1598         if (10 - count > 0) {
1599                 struct timespec time = {
1600                         .tv_sec = 0,
1601                         .tv_nsec = (count + 1) * 100 * 1000 * 1000
1602                 };
1603                 EM_DEBUG_LOG("Busy handler called!!: PID[%d] / CNT [%d]", getpid(), count);
1604                 nanosleep(&time, NULL);
1605                 return 1;
1606         } else {
1607                 EM_DEBUG_EXCEPTION("Busy handler will be returned SQLITE_BUSY error PID[%d] / CNT[%d]", getpid(), count);
1608                 return 0;
1609         }
1610 }
1611
1612 static int _callback_collation_utf7_sort(void *data, int length_text_a, const void *text_a,
1613                                                                                                          int length_text_b, const void *text_b)
1614 {
1615     EM_DEBUG_FUNC_BEGIN();
1616     int result = 0;
1617     char *converted_string_a = NULL;
1618     char *converted_string_b = NULL;
1619
1620         EM_DEBUG_LOG_DEV("text_a : [%s]", text_a);
1621         converted_string_a = emcore_convert_mutf7_to_utf8((char *)text_a);
1622         EM_DEBUG_LOG_DEV("Converted text_a : [%s]", converted_string_a);
1623
1624         EM_DEBUG_LOG_DEV("text_b : [%s]", text_b);
1625         converted_string_b = emcore_convert_mutf7_to_utf8((char *)text_b);
1626         EM_DEBUG_LOG_DEV("Converted text_b : [%s]", converted_string_b);
1627
1628         if (converted_string_a && converted_string_b)
1629                 result = strcmp(converted_string_a, converted_string_b);
1630
1631         EM_SAFE_FREE(converted_string_a);
1632         EM_SAFE_FREE(converted_string_b);
1633
1634         EM_DEBUG_FUNC_END();
1635         return result;
1636 }
1637
1638 static int _delete_all_files_and_directories(char *db_file_path, int *err_code)
1639 {
1640         EM_DEBUG_FUNC_BEGIN();
1641
1642         int error = EMAIL_ERROR_NONE;
1643         int ret = false;
1644
1645         if (!emstorage_delete_file(db_file_path, &error)) {
1646                 if (error != EMAIL_ERROR_FILE_NOT_FOUND) {
1647                         EM_DEBUG_EXCEPTION_SEC("remove failed - %s", EMAIL_SERVICE_DB_FILE_PATH);
1648                         goto FINISH_OFF;
1649                 }
1650         }
1651
1652         if (!emstorage_delete_dir((char *)MAILHOME, &error)) {
1653                 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed");
1654                 goto FINISH_OFF;
1655         }
1656
1657         ret = true;
1658
1659 FINISH_OFF:
1660         if (err_code)
1661                 *err_code = error;
1662         EM_DEBUG_FUNC_END();
1663         return ret;
1664 }
1665
1666 static int _recovery_from_malformed_db_file(char *db_file_path, int *err_code)
1667 {
1668         EM_DEBUG_FUNC_BEGIN();
1669
1670         int error = EMAIL_ERROR_NONE;
1671         int ret = false;
1672
1673         /* Delete all files and directories */
1674         if (!_delete_all_files_and_directories(db_file_path, &error)) {
1675                 EM_DEBUG_EXCEPTION("_delete_all_files_and_directories failed [%d]", error);
1676                 goto FINISH_OFF;
1677         }
1678
1679         /* Delete all accounts on MyAccount */
1680
1681         /* Delete all managed connection to DB */
1682         emstorage_reset_db_handle_list();
1683
1684         ret = true;
1685
1686 FINISH_OFF:
1687         if (err_code)
1688                 *err_code = error;
1689         EM_DEBUG_FUNC_END();
1690         return ret;
1691 }
1692
1693 int _xsystem(const char *argv[])
1694 {
1695         int status = 0;
1696         pid_t pid;
1697         pid = fork();
1698
1699         switch (pid) {
1700                 case -1:
1701                         perror("fork failed");
1702                         return -1;
1703                 case 0:
1704                         if (execvp(argv[0], (char *const *)argv) == -1) {
1705                                 perror("execute init db script");
1706                                 _exit(-1);
1707                         }
1708
1709                         _exit(2);
1710                 default:
1711                         /* parent */
1712                         break;
1713         }
1714
1715         if (waitpid(pid, &status, 0) == -1) {
1716                 perror("waitpid failed");
1717                 return -1;
1718         }
1719
1720         if (WIFSIGNALED(status)) {
1721                 perror("signal");
1722                 return -1;
1723         }
1724
1725         if (!WIFEXITED(status)) {
1726                 perror("should not happen");
1727                 return -1;
1728         }
1729
1730         return WEXITSTATUS(status);
1731 }
1732
1733 #define SCRIPT_INIT_DB "/usr/bin/email-service_init_db.sh"
1734
1735 INTERNAL_FUNC int emstorage_init_db(char *multi_user_name)
1736 {
1737         EM_DEBUG_FUNC_BEGIN();
1738         int err = EMAIL_ERROR_NONE;
1739         char *prefix_path = NULL;
1740         char *output_file_path = NULL;
1741         char temp_file_path[MAX_PATH] = {0};
1742
1743         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1744                 err = emcore_get_container_path(multi_user_name, &prefix_path);
1745                 if (err != EMAIL_ERROR_NONE) {
1746                         if (err != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1747                                 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", err);
1748                                 goto FINISH_OFF;
1749                         }
1750                 }
1751         } else {
1752                 prefix_path = strdup("");
1753         }
1754
1755         if (err == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1756                 err = emcore_get_canonicalize_path((char *)EMAIL_SERVICE_DB_FILE_PATH, &output_file_path);
1757                 if (err != EMAIL_ERROR_NONE) {
1758                         EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", err);
1759                         goto FINISH_OFF;
1760                 }
1761
1762                 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
1763         } else {
1764                 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
1765         }
1766
1767         EM_DEBUG_LOG("db file path : [%s]", temp_file_path);
1768
1769         if (!g_file_test(temp_file_path, G_FILE_TEST_EXISTS)) {
1770                 int ret = true;
1771                 const char *argv_script[] = {"/bin/sh", SCRIPT_INIT_DB, NULL};
1772                 ret = _xsystem(argv_script);
1773
1774                 if (ret == -1) {
1775                         EM_DEBUG_EXCEPTION("_xsystem failed");
1776                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1777                 }
1778         }
1779
1780 FINISH_OFF:
1781
1782         EM_SAFE_FREE(prefix_path);
1783         EM_SAFE_FREE(output_file_path);
1784
1785         EM_DEBUG_FUNC_END();
1786         return err;
1787 }
1788
1789 INTERNAL_FUNC int em_db_open(char *db_file_path, sqlite3 **sqlite_handle, int *err_code)
1790 {
1791         EM_DEBUG_FUNC_BEGIN();
1792         int rc = 0;
1793         int error = EMAIL_ERROR_NONE;
1794         int ret = false;
1795
1796         EM_DEBUG_LOG_DEV("*sqlite_handle[%p]", *sqlite_handle);
1797
1798         if (*sqlite_handle) { /*prevent 33351*/
1799                 EM_DEBUG_LOG_DEV(">>>>> DB Already Opened......");
1800                 if (err_code != NULL)
1801                         *err_code = error;
1802                 return true;
1803         }
1804
1805     EM_DEBUG_LOG("DB file path : [%s]", db_file_path);
1806
1807         /*  db open */
1808         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1809         if (SQLITE_OK != rc) {
1810                 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1811                 if (SQLITE_PERM == rc || SQLITE_CANTOPEN == rc) {
1812                         error = EMAIL_ERROR_PERMISSION_DENIED;
1813                 } else {
1814                         error = EMAIL_ERROR_DB_FAILURE;
1815                 }
1816                 sqlite3_close(*sqlite_handle);
1817                 *sqlite_handle = NULL;
1818
1819                 if (SQLITE_CORRUPT == rc) /* SQLITE_CORRUPT : The database disk image is malformed */ {/* Recovery DB file */
1820                         EM_DEBUG_LOG("The database disk image is malformed. Trying to remove and create database disk image and directories");
1821                         if (!_recovery_from_malformed_db_file(db_file_path, &error)) {
1822                                 EM_DEBUG_EXCEPTION("_recovery_from_malformed_db_file failed [%d]", error);
1823                                 goto FINISH_OFF;
1824                         }
1825
1826                         EM_DEBUG_LOG("Open DB again");
1827                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1828                         if (SQLITE_OK != rc) {
1829                                 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1830                                 if (SQLITE_PERM == rc) {
1831                                         error = EMAIL_ERROR_PERMISSION_DENIED;
1832                                 } else {
1833                                         error = EMAIL_ERROR_DB_FAILURE;
1834                                 }
1835                                 sqlite3_close(*sqlite_handle);
1836                                 *sqlite_handle = NULL;
1837                                 goto FINISH_OFF; /*prevent 33351*/
1838                         }
1839                 } else
1840                         goto FINISH_OFF;
1841         }
1842
1843         /* register busy handler */
1844         EM_DEBUG_LOG_DEV(">>>>> Register DB Handle to busy handler: *sqlite_handle[%p]", *sqlite_handle);
1845         rc = sqlite3_busy_handler(*sqlite_handle, _callback_sqlite_busy_handler, NULL);  /*  Busy Handler registration, NULL is a parameter which will be passed to handler */
1846         if (SQLITE_OK != rc) {
1847                 EM_DEBUG_EXCEPTION("sqlite3_busy_handler fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1848                 error = EMAIL_ERROR_DB_FAILURE;
1849                 sqlite3_close(*sqlite_handle);
1850                 *sqlite_handle = NULL;
1851                 goto FINISH_OFF;
1852         }
1853
1854         /* Register collation callback function */
1855         rc = sqlite3_create_collation(*sqlite_handle, "CONVERTUTF8", SQLITE_UTF8, NULL, _callback_collation_utf7_sort);
1856         if (SQLITE_OK != rc) {
1857                 EM_DEBUG_EXCEPTION("sqlite3_create_collation failed : [%d][%s]", rc, sqlite3_errmsg(*sqlite_handle));
1858                 error = EMAIL_ERROR_DB_FAILURE;
1859                 sqlite3_close(*sqlite_handle);
1860                 *sqlite_handle = NULL;
1861                 goto FINISH_OFF;
1862         }
1863
1864         ret = true;
1865
1866 FINISH_OFF:
1867         if (err_code != NULL)
1868                 *err_code = error;
1869
1870         EM_DEBUG_FUNC_END("ret [%d]", ret);
1871         return ret;
1872 }
1873
1874 INTERNAL_FUNC sqlite3* emstorage_db_open(char *multi_user_name, int *err_code)
1875 {
1876         EM_DEBUG_FUNC_BEGIN();
1877
1878         sqlite3 *_db_handle = NULL;
1879
1880         int error = EMAIL_ERROR_NONE;
1881         char *prefix_path = NULL;
1882
1883         _db_handle = emstorage_get_db_handle(multi_user_name);
1884
1885         if (_db_handle == NULL) {
1886                 char *output_file_path = NULL;
1887         char temp_file_path[MAX_PATH] = {0};
1888
1889         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1890                         error = emcore_get_container_path(multi_user_name, &prefix_path);
1891                         if (error != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION && error != EMAIL_ERROR_NONE) {
1892                                 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", error);
1893                                 goto FINISH_OFF;
1894                         }
1895         } else {
1896             prefix_path = strdup("");
1897         }
1898
1899                 if (error == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1900                         if ((error = emcore_get_canonicalize_path((char *)EMAIL_SERVICE_DB_FILE_PATH, &output_file_path)) != EMAIL_ERROR_NONE) {
1901                                 EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", error);
1902                                 goto FINISH_OFF;
1903                         }
1904
1905                         SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
1906                         EM_SAFE_FREE(output_file_path);
1907                 } else {
1908                         SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
1909                 }
1910
1911                 if (!em_db_open(temp_file_path, &_db_handle, &error)) {
1912                         EM_DEBUG_EXCEPTION("em_db_open failed[%d]", error);
1913                         goto FINISH_OFF;
1914                 }
1915
1916                 _initialize_shm_mutex(SHM_FILE_FOR_DB_LOCK, &shm_fd_for_db_lock, &mapped_for_db_lock);
1917
1918 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
1919                 _initialize_shm_mutex(SHM_FILE_FOR_MAIL_ID_LOCK, &shm_fd_for_generating_mail_id, &mapped_for_generating_mail_id);
1920 #endif /*__FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
1921
1922                 emstorage_set_db_handle(multi_user_name, _db_handle);
1923
1924                 emstorage_initialize_field_count();
1925         }
1926
1927 FINISH_OFF:
1928
1929         EM_SAFE_FREE(prefix_path);
1930
1931         if (err_code != NULL)
1932                 *err_code = error;
1933
1934         EM_DEBUG_FUNC_END("ret [%p]", _db_handle);
1935         return _db_handle;
1936 }
1937
1938 INTERNAL_FUNC int emstorage_db_close(char *multi_user_name, int *err_code)
1939 {
1940         EM_DEBUG_FUNC_BEGIN();
1941 #ifdef _MULTIPLE_DB_HANDLE
1942         sqlite3 *_db_handle = emstorage_get_db_handle(multi_user_name);
1943 #endif
1944
1945         int error = EMAIL_ERROR_NONE;
1946         int ret = false;
1947
1948         if (_db_handle) {
1949                 ret = sqlite3_close(_db_handle);
1950                 if (ret != SQLITE_OK) {
1951                         EM_DEBUG_EXCEPTION(" sqlite3_close fail - %d", ret);
1952                         error = EMAIL_ERROR_DB_FAILURE;
1953                         ret = false;
1954                         goto FINISH_OFF;
1955                 }
1956 #ifdef _MULTIPLE_DB_HANDLE
1957                 emstorage_remove_db_handle();
1958 #endif
1959                 _db_handle = NULL;
1960         }
1961
1962         ret = true;
1963
1964 FINISH_OFF:
1965         if (err_code != NULL)
1966                 *err_code = error;
1967
1968         EM_DEBUG_FUNC_END("ret [%d]", ret);
1969         return ret;
1970 }
1971
1972 INTERNAL_FUNC int emstorage_open(char *multi_user_name, int *err_code)
1973 {
1974         EM_DEBUG_FUNC_BEGIN();
1975
1976     int ret = false;
1977     int error = EMAIL_ERROR_NONE;
1978     int retValue;
1979         char *prefix_path = NULL;
1980     char buf[MAX_PATH] = {0};
1981
1982     if (EM_SAFE_STRLEN(multi_user_name) <= 0) {
1983         SNPRINTF(buf, sizeof(buf), "%s", DB_PATH);
1984     } else {
1985                 error = emcore_get_container_path(multi_user_name, &prefix_path);
1986                 if (error != EMAIL_ERROR_NONE) {
1987                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
1988                         goto FINISH_OFF;
1989                 }
1990         SNPRINTF(buf, sizeof(buf), "%s/%s", prefix_path, DB_PATH);
1991     }
1992
1993         if (!g_file_test(buf, G_FILE_TEST_EXISTS)) {
1994             retValue = mkdir(buf, DIRECTORY_PERMISSION);
1995
1996                 EM_DEBUG_LOG("mkdir return- %d", retValue);
1997             EM_DEBUG_LOG("emstorage_open - before sqlite3_open - pid = %d", getpid());
1998         }
1999
2000     if (emstorage_db_open(multi_user_name, &error) == NULL) {
2001         EM_DEBUG_EXCEPTION("emstorage_db_open failed[%d]", error);
2002         goto FINISH_OFF;
2003     }
2004
2005     if (_open_counter++ == 0) {
2006         _emstorage_open_once(multi_user_name, &error);
2007     }
2008
2009     ret = true;
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 [%d]", ret);
2019     return ret;
2020 }
2021
2022 static int emstorage_get_field_count_from_create_table_query(char *input_create_table_query, int *output_field_count)
2023 {
2024         EM_DEBUG_FUNC_BEGIN("input_create_table_query[%d], output_field_count[%p]", input_create_table_query, output_field_count);
2025         int err = EMAIL_ERROR_NONE;
2026         int field_count = 0;
2027         char *pos = NULL;
2028
2029         if (input_create_table_query == NULL || output_field_count == NULL) {
2030                 err = EMAIL_ERROR_INVALID_PARAM;
2031                 goto FINISH_OFF;
2032         }
2033
2034         pos = input_create_table_query;
2035
2036         do {
2037                 field_count++;
2038                 if (pos == NULL || *pos == (char)0)
2039                         break;
2040                 pos += 1;
2041                 pos = strchr(pos, ',');
2042         } while (pos);
2043
2044         *output_field_count = field_count;
2045
2046         EM_DEBUG_LOG_DEV("field_count [%d]", field_count);
2047
2048 FINISH_OFF:
2049
2050         EM_DEBUG_FUNC_END("err [%d]", err);
2051         return err;
2052 }
2053
2054 INTERNAL_FUNC int emstorage_initialize_field_count()
2055 {
2056         EM_DEBUG_FUNC_BEGIN();
2057         int err = EMAIL_ERROR_NONE;
2058         int query_len = 0;
2059         char **create_table_query = NULL;
2060
2061         if (_field_count_of_table[CREATE_TABLE_MAIL_ACCOUNT_TBL] != 0) {
2062                 err = EMAIL_ERROR_ALREADY_INITIALIZED;
2063                 goto FINISH_OFF;
2064         }
2065
2066         err = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2067         if (err != EMAIL_ERROR_NONE) {
2068                 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", err);
2069                 goto FINISH_OFF;
2070         }
2071
2072         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]));
2073         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]));
2074         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]));
2075         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]));
2076         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_TBL]));
2077         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]));
2078         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]));
2079         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]));
2080 #ifdef __FEATURE_LOCAL_ACTIVITY__
2081         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]));
2082 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2083         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]));
2084         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]));
2085         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]));
2086 FINISH_OFF:
2087
2088         if (create_table_query) {
2089                 int i = 0;
2090                 for (i = 0; i < query_len; i++) {
2091                         if (create_table_query[i]) {
2092                                 EM_SAFE_FREE(create_table_query[i]);
2093                         }
2094                 }
2095                 EM_SAFE_FREE(create_table_query);
2096         }
2097
2098         EM_DEBUG_FUNC_END("err [%d]", err);
2099         return err;
2100 }
2101
2102 INTERNAL_FUNC int emstorage_create_table(char *multi_user_name, emstorage_create_db_type_t type, int *err_code)
2103 {
2104         EM_DEBUG_FUNC_BEGIN();
2105
2106         int error = EMAIL_ERROR_NONE;
2107         int rc = -1, ret = false;
2108         int query_len = 0;
2109         char sql_query_string[QUERY_SIZE] = {0, };
2110         char **create_table_query = NULL;
2111
2112         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2113
2114         error = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2115         if (error != EMAIL_ERROR_NONE) {
2116                 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
2117                 goto FINISH_OFF2;
2118         }
2119
2120         if (query_len < CREATE_TABLE_MAX) {
2121                 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
2122                 error = EMAIL_ERROR_SYSTEM_FAILURE;
2123                 goto FINISH_OFF2;
2124         }
2125
2126         EM_DEBUG_LOG("local_db_handle = %p.", local_db_handle);
2127
2128         char *sql;
2129         char **result = NULL;
2130
2131         /*  1. create mail_account_tbl */
2132         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_account_tbl';";
2133         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2134         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2135                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2136
2137         EM_DEBUG_LOG("emstorage_create_table - result[1] = %s %c", result[1], result[1]);
2138
2139         if (atoi(result[1]) < 1) {
2140                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2141                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2142                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2143
2144                 EM_DEBUG_LOG("CREATE TABLE mail_account_tbl");
2145                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2146                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2147                 if (error != EMAIL_ERROR_NONE) {
2148                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2149                         goto FINISH_OFF;
2150                 }
2151
2152                 /*  create mail_account_tbl unique index */
2153                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ACCOUNT_IDX]);
2154                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2155                 if (error != EMAIL_ERROR_NONE) {
2156                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2157                         goto FINISH_OFF;
2158                 }
2159
2160                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2161         } /*  mail_account_tbl */
2162         else if (type == EMAIL_CREATE_DB_CHECK) {
2163                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], NULL, NULL, NULL), rc);
2164                 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)));
2165         }
2166
2167         sqlite3_free_table(result);
2168         result = NULL;
2169
2170         /*  2. create mail_box_tbl */
2171         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_box_tbl';";
2172
2173         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2174         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2175                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2176
2177         if (atoi(result[1]) < 1) {
2178                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2179                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2180                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2181
2182                 EM_DEBUG_LOG("CREATE TABLE mail_box_tbl");
2183
2184                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_BOX_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2185                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2186                 if (error != EMAIL_ERROR_NONE) {
2187                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2188                         goto FINISH_OFF;
2189                 }
2190
2191                 /*  create mail_local_mailbox_tbl unique index */
2192                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_BOX_IDX]);
2193                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2194                 if (error != EMAIL_ERROR_NONE) {
2195                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2196                         goto FINISH_OFF;
2197                 }
2198
2199                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2200         } /*  mail_box_tbl */
2201         else if (type == EMAIL_CREATE_DB_CHECK) {
2202                 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_BOX_TBL], NULL, NULL, NULL);
2203                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2204                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_BOX_TBL], rc, sqlite3_errmsg(local_db_handle)));
2205         }
2206         sqlite3_free_table(result);
2207         result = NULL;
2208
2209         /*  3. create mail_read_mail_uid_tbl */
2210         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_read_mail_uid_tbl';";
2211         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2212         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2213                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2214
2215
2216         if (atoi(result[1]) < 1) {
2217                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2218                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2219                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2220
2221                 EM_DEBUG_LOG("CREATE TABLE mail_read_mail_uid_tbl");
2222
2223                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2224                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2225                 if (error != EMAIL_ERROR_NONE) {
2226                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2227                         goto FINISH_OFF;
2228                 }
2229
2230                 /*  create mail_read_mail_uid_tbl unique index */
2231                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_IDX]);
2232                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2233                 if (error != EMAIL_ERROR_NONE) {
2234                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2235                         goto FINISH_OFF;
2236                 }
2237
2238                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2239         } /*  mail_read_mail_uid_tbl */
2240         else if (type == EMAIL_CREATE_DB_CHECK) {
2241                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], NULL, NULL, NULL), rc);
2242                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2243                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], rc, sqlite3_errmsg(local_db_handle)));
2244         }
2245         sqlite3_free_table(result);
2246         result = NULL;
2247
2248         /*  4. create mail_rule_tbl */
2249         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_rule_tbl';";
2250
2251         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2252         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2253                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2254
2255         if (atoi(result[1]) < 1) {
2256                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2257                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2258                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2259
2260                 EM_DEBUG_LOG("CREATE TABLE mail_rule_tbl");
2261
2262                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_RULE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2263                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2264                 if (error != EMAIL_ERROR_NONE) {
2265                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2266                         goto FINISH_OFF;
2267                 }
2268
2269                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2270         } /*  mail_rule_tbl */
2271         else if (type == EMAIL_CREATE_DB_CHECK) {
2272                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_RULE_TBL], NULL, NULL, NULL), rc);
2273                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2274                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_RULE_TBL], rc, sqlite3_errmsg(local_db_handle)));
2275         }
2276         sqlite3_free_table(result);
2277         result = NULL;
2278
2279         /*  5. create mail_tbl */
2280         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_tbl';";
2281         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2282         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2283                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2284
2285         if (atoi(result[1]) < 1) {
2286                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2287                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2288                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2289                 EM_DEBUG_LOG("CREATE TABLE mail_tbl");
2290
2291                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2292                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2293                 if (error != EMAIL_ERROR_NONE) {
2294                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2295                         goto FINISH_OFF;
2296                 }
2297
2298                 /*  create mail_tbl unique index */
2299                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_IDX]);
2300                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2301                 if (error != EMAIL_ERROR_NONE) {
2302                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2303                         goto FINISH_OFF;
2304                 }
2305
2306                 /*  create mail_tbl index for date_time */
2307                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_DATETIME_IDX]);
2308                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2309                 if (error != EMAIL_ERROR_NONE) {
2310                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2311                         goto FINISH_OFF;
2312                 }
2313
2314                 /*  create mail_tbl index for thread_item_count */
2315                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_THREAD_IDX]);
2316                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2317                 if (error != EMAIL_ERROR_NONE) {
2318                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2319                         goto FINISH_OFF;
2320                 }
2321
2322                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2323                 /*  just one time call */
2324 /*              EFTSInitFTSIndex(FTS_EMAIL_IDX); */
2325         } /*  mail_tbl */
2326         else if (type == EMAIL_CREATE_DB_CHECK) {
2327                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TBL], NULL, NULL, NULL), rc);
2328                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2329                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TBL], rc, sqlite3_errmsg(local_db_handle)));
2330         }
2331         sqlite3_free_table(result);
2332         result = NULL;
2333
2334         /*  6. create mail_attachment_tbl */
2335         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_attachment_tbl';";
2336         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2337         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2338                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2339
2340         if (atoi(result[1]) < 1) {
2341                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2342                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2343                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2344
2345                 EM_DEBUG_LOG("CREATE TABLE mail_attachment_tbl");
2346
2347                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2348                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2349                 if (error != EMAIL_ERROR_NONE) {
2350                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2351                         goto FINISH_OFF;
2352                 }
2353
2354                 /*  create mail_attachment_tbl unique index */
2355                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_IDX]);
2356                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2357                 if (error != EMAIL_ERROR_NONE) {
2358                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2359                         goto FINISH_OFF;
2360                 }
2361
2362                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2363         } /*  mail_attachment_tbl */
2364         else if (type == EMAIL_CREATE_DB_CHECK) {
2365                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], NULL, NULL, NULL), rc);
2366                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2367                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], rc, sqlite3_errmsg(local_db_handle)));
2368         }
2369         sqlite3_free_table(result);
2370         result = NULL;
2371
2372 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
2373
2374         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_partial_body_activity_tbl';";
2375         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2376         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2377                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2378
2379         if (atoi(result[1]) < 1) {
2380
2381                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2382                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2383                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2384
2385                 EM_DEBUG_LOG("CREATE TABLE mail_partial_body_activity_tbl");
2386
2387                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2388                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2389                 if (error != EMAIL_ERROR_NONE) {
2390                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2391                         goto FINISH_OFF;
2392                 }
2393
2394                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2395         } /*  mail_rule_tbl */
2396         else if (type == EMAIL_CREATE_DB_CHECK) {
2397                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2398                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2399                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2400         }
2401         sqlite3_free_table(result);
2402         result = NULL;
2403
2404 #endif /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
2405
2406         /*  create mail_meeting_tbl */
2407         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_meeting_tbl';";
2408         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2409         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2410                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2411
2412         if (atoi(result[1]) < 1) {
2413                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2414                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2415                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2416
2417                 EM_DEBUG_LOG("CREATE TABLE mail_meeting_tbl");
2418
2419                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_MEETING_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2420                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2421                 if (error != EMAIL_ERROR_NONE) {
2422                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2423                         goto FINISH_OFF;
2424                 }
2425
2426                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_MEETING_IDX]);
2427                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2428                 if (error != EMAIL_ERROR_NONE) {
2429                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2430                         goto FINISH_OFF;
2431                 }
2432
2433                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2434         } /*  mail_contact_sync_tbl */
2435         else if (type == EMAIL_CREATE_DB_CHECK) {
2436                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], NULL, NULL, NULL), rc);
2437                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2438                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], rc, sqlite3_errmsg(local_db_handle)));
2439         }
2440         sqlite3_free_table(result);
2441         result = NULL;
2442
2443 #ifdef __FEATURE_LOCAL_ACTIVITY__
2444
2445                 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_local_activity_tbl';";
2446                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2447                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2448                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2449
2450                 if (atoi(result[1]) < 1) {
2451
2452                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2453                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2454                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2455
2456                         EM_DEBUG_LOG(" CREATE TABLE mail_local_activity_tbl");
2457
2458                         SNPRINTF(sql_query_string, sizeof(sql_query_string), create_table_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL]);
2459                         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2460                         if (error != EMAIL_ERROR_NONE) {
2461                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2462                                 goto FINISH_OFF;
2463                         }
2464
2465                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2466                 } /*  mail_rule_tbl */
2467                 else if (type == EMAIL_CREATE_DB_CHECK) {
2468                         rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL);
2469                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2470                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2471                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2472                 }
2473                 sqlite3_free_table(result);
2474                 result = NULL;
2475 #endif /*  __FEATURE_LOCAL_ACTIVITY__ */
2476         /*  create mail_certificate_tbl */
2477         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_certificate_tbl';";
2478         /*  rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL);   */
2479         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2480         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)));
2481
2482         if (atoi(result[1]) < 1) {
2483                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2484                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2485
2486                 EM_DEBUG_LOG("CREATE TABLE mail_certificate_tbl");
2487
2488                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2489                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2490                 if (error != EMAIL_ERROR_NONE) {
2491                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2492                         goto FINISH_OFF;
2493                 }
2494
2495                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2496         } /*  mail_contact_sync_tbl */
2497         else if (type == EMAIL_CREATE_DB_CHECK) {
2498                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], NULL, NULL, NULL), rc);
2499                 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)));
2500         }
2501
2502         sqlite3_free_table(result);
2503         result = NULL;
2504
2505         /*  create mail_task_tbl */
2506         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_task_tbl';";
2507         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2508         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)));
2509
2510         if (atoi(result[1]) < 1) {
2511                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2512                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2513
2514                 EM_DEBUG_LOG("CREATE TABLE mail_task_tbl");
2515
2516                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TASK_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2517                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2518                 if (error != EMAIL_ERROR_NONE) {
2519                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2520                         goto FINISH_OFF;
2521                 }
2522
2523                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_TASK_IDX]);
2524                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2525                 if (error != EMAIL_ERROR_NONE) {
2526                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2527                         goto FINISH_OFF;
2528                 }
2529
2530                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2531         } /*  mail_task_tbl */
2532         else if (type == EMAIL_CREATE_DB_CHECK) {
2533                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TASK_TBL], NULL, NULL, NULL), rc);
2534                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TASK_TBL], rc, sqlite3_errmsg(local_db_handle)));
2535         }
2536
2537         sqlite3_free_table(result);
2538         result = NULL;
2539
2540 #ifdef __FEATURE_BODY_SEARCH__
2541         /*  create mail_text_tbl */
2542         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_text_tbl';";
2543         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2544         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)));
2545
2546         if (atoi(result[1]) < 1) {
2547                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2548                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2549
2550                 EM_DEBUG_LOG("CREATE TABLE mail_text_tbl");
2551
2552                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TEXT_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2553                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2554                 if (error != EMAIL_ERROR_NONE) {
2555                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2556                         goto FINISH_OFF;
2557                 }
2558
2559                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2560         } /*  mail_text_tbl */
2561         else if (type == EMAIL_CREATE_DB_CHECK) {
2562                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TEXT_TBL], NULL, NULL, NULL), rc);
2563                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TEXT_TBL], rc, sqlite3_errmsg(local_db_handle)));
2564         }
2565
2566         sqlite3_free_table(result);
2567         result = NULL;
2568
2569 #endif
2570
2571 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
2572         /*  create mail_auto_download_activity_tbl */
2573         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_auto_download_activity_tbl';";
2574         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2575         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)));
2576
2577         if (atoi(result[1]) < 1) {
2578                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2579                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2580
2581                 EM_DEBUG_LOG("CREATE TABLE mail_auto_download_activity_tbl");
2582
2583                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], sizeof(sql_query_string)-1);
2584                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2585                 if (error != EMAIL_ERROR_NONE) {
2586                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2587                         goto FINISH_OFF;
2588                 }
2589
2590                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2591         } /*  mail_auto_download_activity_tbl */
2592         else if (type == EMAIL_CREATE_DB_CHECK) {
2593                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2594                 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)));
2595         }
2596
2597         sqlite3_free_table(result);
2598         result = NULL;
2599 #endif
2600
2601
2602         ret = true;
2603
2604 FINISH_OFF:
2605
2606         if (result) sqlite3_free_table(result);
2607
2608         if (ret == true) {
2609                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2610         } else {
2611                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
2612         }
2613
2614 FINISH_OFF2:
2615         if (create_table_query) {
2616                 int i = 0;
2617                 for (i = 0; i < query_len; i++) {
2618                         if (create_table_query[i]) {
2619                                 EM_SAFE_FREE(create_table_query[i]);
2620                         }
2621                 }
2622                 EM_SAFE_FREE(create_table_query);
2623         }
2624
2625         if (err_code != NULL)
2626                 *err_code = error;
2627
2628         EM_DEBUG_FUNC_END("ret [%d]", ret);
2629         return ret;
2630 }
2631
2632 /* Query series --------------------------------------------------------------*/
2633 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)
2634 {
2635         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);
2636         int rc = -1;
2637         int query_size = 0;
2638         int error = EMAIL_ERROR_NONE;
2639         DB_STMT hStmt = NULL;
2640         char *sql_query_string = NULL;
2641         char **result;
2642         sqlite3 *local_db_handle = NULL;
2643
2644         if (!input_conditional_clause || (!output_total_mail_count && !output_unseen_mail_count)) {
2645                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2646                 return EMAIL_ERROR_INVALID_PARAM;
2647         }
2648
2649         query_size = EM_SAFE_STRLEN(input_conditional_clause) + QUERY_SIZE;
2650         sql_query_string = em_malloc(query_size);
2651         if (sql_query_string == NULL) {
2652                 EM_DEBUG_EXCEPTION("em_mallocfailed");
2653                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2654                 goto FINISH_OFF;
2655         }
2656
2657         local_db_handle = emstorage_get_db_connection(multi_user_name);
2658
2659         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2660
2661         SNPRINTF(sql_query_string, query_size, "SELECT COUNT(*) FROM mail_tbl");
2662         EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
2663
2664         if (output_total_mail_count) {
2665                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
2666                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2667                         ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
2668
2669                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2670                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2671                         ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
2672                 _get_stmt_field_data_int(hStmt, output_total_mail_count, 0);
2673         }
2674
2675         if (output_unseen_mail_count) {
2676                 EM_SAFE_STRCAT(sql_query_string, " AND flags_seen_field = 0 ");
2677
2678                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
2679                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2680                         ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2681
2682                 *output_unseen_mail_count = atoi(result[1]);
2683                 sqlite3_free_table(result);
2684         }
2685
2686 FINISH_OFF:
2687
2688         if (hStmt != NULL) {
2689                 rc = sqlite3_finalize(hStmt);
2690                 if (rc != SQLITE_OK) {
2691                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
2692                         error = EMAIL_ERROR_DB_FAILURE;
2693                 }
2694         }
2695
2696         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2697
2698         EM_SAFE_FREE(sql_query_string);
2699
2700         EM_DEBUG_FUNC_END("error [%d]", error);
2701         return error;
2702 }
2703
2704 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)
2705 {
2706         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);
2707
2708         int      i = 0;
2709         int      count = 0;
2710         int      rc = -1;
2711         int      cur_query = 0;
2712         int      col_index;
2713         int      query_size = 0;
2714         int      error = EMAIL_ERROR_NONE;
2715         int     *result_mail_id_list = NULL;
2716         char   **result = NULL;
2717         char     *sql_query_string = NULL;
2718         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2719
2720         EM_IF_NULL_RETURN_VALUE(input_conditional_clause, EMAIL_ERROR_INVALID_PARAM);
2721         EM_IF_NULL_RETURN_VALUE(output_mail_id_list, EMAIL_ERROR_INVALID_PARAM);
2722         EM_IF_NULL_RETURN_VALUE(output_mail_id_count, EMAIL_ERROR_INVALID_PARAM);
2723
2724         query_size = strlen(input_conditional_clause) + strlen("SELECT mail_id FROM mail_tbl ") + 10;  // 10 is extra space
2725         sql_query_string = em_malloc(query_size);
2726         if (sql_query_string == NULL) {
2727                 EM_DEBUG_EXCEPTION("em_mallocfailed");
2728                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2729                 goto FINISH_OFF;
2730         }
2731
2732         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2733
2734         /* Composing query */
2735         SNPRINTF_OFFSET(sql_query_string, cur_query, query_size, "SELECT mail_id FROM mail_tbl ");
2736         EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
2737
2738         EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
2739
2740         /* Performing query */
2741         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2742         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2743                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2744
2745         col_index = 1;
2746
2747         /*  to get mail list */
2748         if (count == 0) {
2749                 EM_DEBUG_LOG("No mail found...");
2750                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2751                 goto FINISH_OFF;
2752         }
2753
2754         EM_DEBUG_LOG("There are [%d] mails.", count);
2755
2756         if (!(result_mail_id_list = (int*)em_malloc(sizeof(int) * count))) {
2757                 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
2758                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2759                 goto FINISH_OFF;
2760         }
2761
2762         EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2763
2764         for (i = 0; i < count; i++)
2765                 _get_table_field_data_int(result, result_mail_id_list + i, col_index++);
2766
2767         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >> ", count);
2768
2769         *output_mail_id_list  = result_mail_id_list;
2770         *output_mail_id_count = count;
2771
2772 FINISH_OFF:
2773
2774         if (result)
2775                 sqlite3_free_table(result);
2776
2777         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2778
2779         EM_SAFE_FREE(sql_query_string);
2780
2781         if (error != EMAIL_ERROR_NONE)
2782                 EM_SAFE_FREE(result_mail_id_list);
2783
2784         EM_DEBUG_FUNC_END("error [%d]", error);
2785         return error;
2786 }
2787
2788 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)
2789 {
2790         EM_DEBUG_FUNC_BEGIN();
2791         EM_PROFILE_BEGIN(emstorage_query_mail_list_func);
2792
2793         int i = 0, count = 0, rc = -1, to_get_count = (result_mail_list) ? 0 : 1;
2794         int sql_query_string_length = 0;
2795         int local_inline_content_count = 0, local_attachment_count = 0;
2796         int cur_query = 0, base_count = 0, col_index;
2797         int ret = false, error = EMAIL_ERROR_NONE;
2798         char *date_time_string = NULL;
2799         char **result = NULL;
2800         char *field_mail_id = "mail_id";
2801         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 ";
2802         char *select_query_form = "SELECT %s FROM mail_tbl ";
2803         char *target_field = NULL;
2804         char *sql_query_string = NULL;
2805         email_mail_list_item_t *mail_list_item_from_tbl = NULL;
2806         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2807
2808         EM_IF_NULL_RETURN_VALUE(conditional_clause, false);
2809         EM_IF_NULL_RETURN_VALUE(result_count, false);
2810
2811         EMSTORAGE_START_READ_TRANSACTION(transaction);
2812
2813         /*  select clause */
2814         if (to_get_count) /*  count only */
2815                 target_field = field_mail_id;
2816         else /* mail list in plain form */
2817                 target_field = field_all;
2818
2819         sql_query_string_length = EM_SAFE_STRLEN(select_query_form) + EM_SAFE_STRLEN(target_field) + EM_SAFE_STRLEN(conditional_clause);
2820
2821         if (sql_query_string_length)
2822                 sql_query_string = em_malloc(sql_query_string_length);
2823
2824         if (sql_query_string == NULL) {
2825                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
2826                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2827                 goto FINISH_OFF;
2828         }
2829
2830         cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, sql_query_string_length, select_query_form, target_field);
2831
2832         strncat(sql_query_string, conditional_clause, sql_query_string_length - cur_query);
2833
2834         EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
2835
2836         /*  performing query */
2837         EM_PROFILE_BEGIN(emstorage_query_mail_list_performing_query);
2838         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2839         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2840                 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2841         EM_PROFILE_END(emstorage_query_mail_list_performing_query);
2842
2843         if (!base_count)
2844                 base_count = ({
2845                                 int i = 0;
2846                                 char *tmp = NULL;
2847                                 for (tmp = field_all; tmp && *(tmp + 1); tmp = index(tmp + 1, ',')) i++ ;
2848                                 i;
2849                                 });
2850
2851         col_index = base_count;
2852
2853         EM_DEBUG_LOG_DEV("base_count [%d]", base_count);
2854
2855         if (to_get_count) {
2856                 /*  to get count */
2857                 if (!count) {
2858                         EM_DEBUG_LOG_DEV("No mail found...");
2859                         ret = false;
2860                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
2861                         goto FINISH_OFF;
2862                 }
2863                 EM_DEBUG_LOG_DEV("There are [%d] mails.", count);
2864         } else {
2865                 /*  to get mail list */
2866                 if (!count) {
2867                         EM_DEBUG_LOG_DEV("No mail found...");
2868                         ret = false;
2869                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
2870                         goto FINISH_OFF;
2871                 }
2872
2873                 EM_DEBUG_LOG_DEV("There are [%d] mails.", count);
2874                 if (!(mail_list_item_from_tbl = (email_mail_list_item_t*)em_malloc(sizeof(email_mail_list_item_t) * count))) {
2875                         EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
2876                         error = EMAIL_ERROR_OUT_OF_MEMORY;
2877                         goto FINISH_OFF;
2878                 }
2879
2880                 EM_PROFILE_BEGIN(emstorage_query_mail_list_loop);
2881                 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >> ");
2882                 for (i = 0; i < count; i++) {
2883                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_id), col_index++);
2884                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].account_id), col_index++);
2885                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mailbox_id), col_index++);
2886                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].mailbox_type), col_index++);
2887                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].full_address_from, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2888                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_sender, MAX_EMAIL_ADDRESS_LENGTH, 1, col_index++);
2889                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_recipient, STRING_LENGTH_FOR_DISPLAY,  1, col_index++);
2890                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].subject, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2891                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].body_download_status), col_index++);
2892                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_size), col_index++);
2893                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_seen_field), col_index++);
2894                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_deleted_field), col_index++);
2895                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_flagged_field), col_index++);
2896                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_answered_field), col_index++);
2897                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_recent_field), col_index++);
2898                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_draft_field), col_index++);
2899                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_forwarded_field), col_index++);
2900                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].DRM_status), col_index++);
2901                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].priority), col_index++);
2902                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].save_status), col_index++);
2903                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].lock_status), col_index++);
2904                         _get_table_field_data_int(result, &local_attachment_count, col_index++);
2905                         _get_table_field_data_int(result, &local_inline_content_count, col_index++);
2906                         _get_table_field_data_time_t(result, &(mail_list_item_from_tbl[i].date_time), col_index++);
2907                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].preview_text, MAX_PREVIEW_TEXT_LENGTH, 1, col_index++);
2908                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_id), col_index++);
2909                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_item_count), col_index++);
2910                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].meeting_request_status), col_index++);
2911                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].message_class), col_index++);
2912                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].smime_type), col_index++);
2913                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].scheduled_sending_time), col_index++);
2914                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].remaining_resend_times), col_index++);
2915                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].tag_id), col_index++);
2916                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].eas_data_length), col_index++);
2917                         _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++);
2918
2919                         mail_list_item_from_tbl[i].attachment_count = (local_attachment_count > 0) ? 1 : 0;
2920                 }
2921                 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >> ", count);
2922                 EM_PROFILE_END(emstorage_query_mail_list_loop);
2923         }
2924
2925         ret = true;
2926
2927 FINISH_OFF:
2928         EM_DEBUG_LOG("MAIL_COUNT [%d]", count);
2929
2930         if (result)
2931                 sqlite3_free_table(result);
2932
2933         if (to_get_count)
2934                 *result_count = count;
2935         else {
2936                 if (ret == true) {
2937                         if (result_mail_list)
2938                                 *result_mail_list = mail_list_item_from_tbl;
2939                         *result_count = count;
2940                 } else
2941                         EM_SAFE_FREE(mail_list_item_from_tbl);
2942         }
2943
2944         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2945
2946 //      sqlite3_db_release_memory(local_db_handle);
2947
2948         EM_SAFE_FREE(sql_query_string);
2949         EM_SAFE_FREE(date_time_string);
2950
2951         if (err_code != NULL)
2952                 *err_code = error;
2953
2954         EM_PROFILE_END(emstorage_query_mail_list_func);
2955         EM_DEBUG_FUNC_END("ret [%d]", ret);
2956         return ret;
2957 }
2958
2959
2960 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)
2961 {
2962         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);
2963
2964         if (!conditional_clause || !result_mail_tbl || !result_count) {
2965                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: [%p] [%p] [%p]", conditional_clause, result_mail_tbl, result_mail_tbl);
2966                 if (err_code != NULL)
2967                         *err_code = EMAIL_ERROR_INVALID_PARAM;
2968                 return false;
2969         }
2970
2971         int i, col_index = FIELD_COUNT_OF_MAIL_TBL, rc, ret = false, count;
2972         int error = EMAIL_ERROR_NONE;
2973         char **result = NULL, sql_query_string[QUERY_SIZE] = {0, };
2974         emstorage_mail_tbl_t* p_data_tbl = NULL;
2975         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2976
2977         EMSTORAGE_START_READ_TRANSACTION(transaction);
2978
2979         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl %s", conditional_clause);
2980
2981         EM_DEBUG_LOG_DEV("Query[%s]", sql_query_string);
2982
2983         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2984         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2985                 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2986
2987         if (!count) {
2988                 EM_DEBUG_LOG("No mail found...");
2989                 ret = false;
2990                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2991                 goto FINISH_OFF;
2992         }
2993
2994         EM_DEBUG_LOG("There are [%d] mails.", count);
2995         if (!(p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * count))) {
2996                 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
2997                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2998                 goto FINISH_OFF;
2999         }
3000
3001         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >> ");
3002         for (i = 0; i < count; i++) {
3003                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
3004                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3005                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3006                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_type), col_index++);
3007                 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 1, col_index++);
3008                 _get_table_field_data_time_t (result, &(p_data_tbl[i].date_time), col_index++);
3009                 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
3010                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
3011                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
3012                 _get_table_field_data_string(result, &(p_data_tbl[i].message_id), 0, col_index++);
3013                 _get_table_field_data_int(result, &(p_data_tbl[i].reference_mail_id), col_index++);
3014                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_from), 1, col_index++);
3015                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_reply), 1, col_index++);
3016                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_to), 1, col_index++);
3017                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_cc), 1, col_index++);
3018                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_bcc), 1, col_index++);
3019                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_return), 1, col_index++);
3020                 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_sender), 1, col_index++);
3021                 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_recipient), 1, col_index++);
3022                 _get_table_field_data_string(result, &(p_data_tbl[i].alias_sender), 1, col_index++);
3023                 _get_table_field_data_string(result, &(p_data_tbl[i].alias_recipient), 1, col_index++);
3024                 _get_table_field_data_int(result, &(p_data_tbl[i].body_download_status), col_index++);
3025                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
3026                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
3027                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
3028                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_size), col_index++);
3029                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_seen_field), col_index++);
3030                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_deleted_field), col_index++);
3031                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_flagged_field), col_index++);
3032                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_answered_field), col_index++);
3033                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_recent_field), col_index++);
3034                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_draft_field), col_index++);
3035                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_forwarded_field), col_index++);
3036                 _get_table_field_data_int(result, &(p_data_tbl[i].DRM_status), col_index++);
3037                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].priority), col_index++);
3038                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
3039                 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
3040                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].report_status), col_index++);
3041                 _get_table_field_data_int(result, &(p_data_tbl[i].attachment_count), col_index++);
3042                 _get_table_field_data_int(result, &(p_data_tbl[i].inline_content_count), col_index++);
3043                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
3044                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
3045                 _get_table_field_data_string(result, &(p_data_tbl[i].preview_text), 1, col_index++);
3046                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].meeting_request_status), col_index++);
3047                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].message_class), col_index++);
3048                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].digest_type), col_index++);
3049                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].smime_type), col_index++);
3050                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].scheduled_sending_time), col_index++);
3051                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].remaining_resend_times), col_index++);
3052                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].tag_id), col_index++);
3053                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].replied_time), col_index++);
3054                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].forwarded_time), col_index++);
3055                 _get_table_field_data_string(result, &(p_data_tbl[i].default_charset), 0, col_index++);
3056                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3057                 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3058                 _get_table_field_data_string(result, &(p_data_tbl[i].user_name), 0, col_index++);
3059         }
3060
3061         ret = true;
3062
3063 FINISH_OFF:
3064         if (result)
3065                 sqlite3_free_table(result);
3066
3067         if (ret) {
3068                 *result_mail_tbl = p_data_tbl;
3069                 *result_count = count;
3070         } else {
3071                 *result_mail_tbl = NULL;
3072                 *result_count = 0;
3073         }
3074
3075         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3076
3077 //      sqlite3_db_release_memory(local_db_handle);
3078
3079         if (err_code != NULL)
3080                 *err_code = error;
3081
3082         EM_DEBUG_FUNC_END("ret [%d]", ret);
3083         return ret;
3084 }
3085
3086 #ifdef __FEATURE_BODY_SEARCH__
3087 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)
3088 {
3089         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);
3090
3091         if (!conditional_clause || !result_mail_text_tbl || !result_count) { /*prevent 50930*/
3092                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3093                 if (err_code != NULL)
3094                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3095                 return false;
3096         }
3097
3098         int i = 0;
3099         int rc = 0;
3100         int ret = false;
3101         int count = 0;
3102         int col_index = FIELD_COUNT_OF_MAIL_TEXT_TBL;
3103         int error = EMAIL_ERROR_NONE;
3104         char **result = NULL;
3105         char sql_query_string[QUERY_SIZE] = {0, };
3106         emstorage_mail_text_tbl_t* p_data_tbl = NULL;
3107         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3108
3109         EMSTORAGE_START_READ_TRANSACTION(transaction);
3110
3111         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_text_tbl %s", conditional_clause);
3112
3113         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
3114
3115         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3116         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3117                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3118
3119         if (!count) {
3120                 EM_DEBUG_LOG("No mail found...");
3121                 ret = false;
3122                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
3123                 goto FINISH_OFF;
3124         }
3125
3126         EM_DEBUG_LOG("There are [%d] mails.", count);
3127         if (!(p_data_tbl = (emstorage_mail_text_tbl_t *)em_malloc(sizeof(emstorage_mail_text_tbl_t) * count))) {
3128                 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_text_tbl_t failed...");
3129                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3130                 goto FINISH_OFF;
3131         }
3132
3133         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
3134         for (i = 0; i < count; i++) {
3135                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
3136                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3137                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3138                 _get_table_field_data_string(result, &(p_data_tbl[i].body_text), 0, col_index++);
3139         }
3140
3141         ret = true;
3142
3143 FINISH_OFF:
3144         if (result)
3145                 sqlite3_free_table(result);
3146
3147         if (ret == true) {
3148                 *result_mail_text_tbl = p_data_tbl;
3149                 *result_count = count;
3150         } else
3151                 EM_SAFE_FREE(p_data_tbl);
3152
3153         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3154
3155 //      sqlite3_db_release_memory(local_db_handle);
3156
3157         if (err_code != NULL)
3158                 *err_code = error;
3159
3160         EM_DEBUG_FUNC_END("ret [%d]", ret);
3161         return ret;
3162 }
3163 #endif
3164
3165 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)
3166 {
3167         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);
3168
3169         int i = 0;
3170         int rc;
3171         int count = 0;
3172         int col_index = 0;
3173         int error = EMAIL_ERROR_NONE;
3174         int read_count = 0;
3175         int total_count = 0;
3176         char **result;
3177         char sql_query_string[QUERY_SIZE] = {0, };
3178         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 ";
3179         emstorage_mailbox_tbl_t* p_data_tbl = NULL;
3180         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3181
3182         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
3183
3184         if (input_get_mail_count == 0) {        /* without mail count */
3185                 col_index = 15;
3186                 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);
3187         } else {        /* with read count and total count */
3188                 col_index = 17;
3189                 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);
3190         }
3191
3192         EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
3193
3194         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
3195         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3196                 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string))
3197
3198         EM_DEBUG_LOG_DEV("result count [%d]", count);
3199
3200         if (count == 0) {
3201                 EM_DEBUG_LOG_SEC("Can't find mailbox query[%s]", sql_query_string);
3202                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
3203                 goto FINISH_OFF;
3204         }
3205
3206         if ((p_data_tbl = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t) * count)) == NULL) {
3207                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
3208                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3209                 goto FINISH_OFF;
3210         }
3211
3212         for (i = 0; i < count; i++) {
3213                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3214                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3215                 _get_table_field_data_int(result, &(p_data_tbl[i].local_yn), col_index++);
3216                 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
3217                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].mailbox_type), col_index++);
3218                 _get_table_field_data_string(result, &(p_data_tbl[i].alias), 0, col_index++);
3219                 _get_table_field_data_int(result, &(p_data_tbl[i].deleted_flag), col_index++);
3220                 _get_table_field_data_int(result, &(p_data_tbl[i].modifiable_yn), col_index++);
3221                 _get_table_field_data_int(result, &(p_data_tbl[i].total_mail_count_on_server), col_index++);
3222                 _get_table_field_data_int(result, &(p_data_tbl[i].has_archived_mails), col_index++);
3223                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_slot_size), col_index++);
3224                 _get_table_field_data_int(result, &(p_data_tbl[i].no_select), col_index++);
3225                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].last_sync_time), col_index++);
3226                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3227                 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3228
3229                 if (input_get_mail_count == 1) {
3230                         _get_table_field_data_int(result, &(total_count), col_index++);
3231                         p_data_tbl[i].total_mail_count_on_local = total_count;
3232                         _get_table_field_data_int(result, &(read_count), col_index++);
3233                         p_data_tbl[i].unread_count = total_count - read_count;                  /*  return unread count, NOT  */
3234                 }
3235         }
3236
3237
3238 FINISH_OFF:
3239         if (result)
3240                 sqlite3_free_table(result);
3241
3242         if (error == EMAIL_ERROR_NONE) {
3243                 *output_mailbox_list = p_data_tbl;
3244                 *output_mailbox_count = count;
3245                 EM_DEBUG_LOG("Mailbox Count [%d]", count);
3246         }
3247
3248         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
3249
3250 //      sqlite3_db_release_memory(local_db_handle);
3251
3252         EM_DEBUG_FUNC_END("error [%d]", error);
3253         return error;
3254 }
3255
3256 /* Query series --------------------------------------------------------------*/
3257 INTERNAL_FUNC int emstorage_check_duplicated_account(char *multi_user_name, email_account_t* account, int transaction, int *err_code)
3258 {
3259         EM_DEBUG_FUNC_BEGIN();
3260         int rc = -1, ret = false;
3261         int error = EMAIL_ERROR_NONE;
3262         char **result;
3263         int count;
3264         char sql_query_string[QUERY_SIZE] = {0, };
3265         sqlite3 *local_db_handle = NULL;
3266
3267         if (account == NULL || account->user_email_address == NULL || account->incoming_server_user_name == NULL || account->incoming_server_address == NULL ||
3268                 account->outgoing_server_user_name == NULL || account->outgoing_server_address == NULL) {
3269                 if (err_code != NULL)
3270                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3271                 return false;
3272         }
3273
3274         local_db_handle = emstorage_get_db_connection(multi_user_name);
3275
3276         EMSTORAGE_START_READ_TRANSACTION(transaction);
3277
3278         SNPRINTF(sql_query_string, sizeof(sql_query_string),
3279                 "SELECT COUNT(*) FROM mail_account_tbl "
3280                 " WHERE "
3281                 " user_email_address = '%s' AND "
3282                 " incoming_server_user_name = '%s' AND "
3283                 " incoming_server_type = %d AND "
3284                 " incoming_server_address = '%s' AND "
3285                 " outgoing_server_user_name = '%s' AND "
3286                 " outgoing_server_type = %d AND "
3287                 " outgoing_server_address = '%s'; ",
3288                 account->user_email_address,
3289                 account->incoming_server_user_name, account->incoming_server_type, account->incoming_server_address,
3290                 account->outgoing_server_user_name, account->outgoing_server_type, account->outgoing_server_address
3291         );
3292         EM_DEBUG_LOG_SEC("Query[%s]", sql_query_string);
3293         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
3294         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3295                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3296
3297         count = atoi(result[1]);
3298         sqlite3_free_table(result);
3299
3300         EM_DEBUG_LOG("Count of Duplicated Account Information: [%d]", count);
3301
3302         if (count == 0) {       /*  not duplicated account */
3303                 ret = true;
3304                 EM_DEBUG_LOG_SEC("NOT duplicated account: user_email_address[%s]", account->user_email_address);
3305         } else {        /*  duplicated account */
3306                 ret = false;
3307                 EM_DEBUG_LOG_SEC("The same account already exists. Duplicated account: user_email_address[%s]", account->user_email_address);
3308                 error = EMAIL_ERROR_ALREADY_EXISTS;
3309         }
3310
3311 FINISH_OFF:
3312
3313         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3314
3315         if (err_code != NULL)
3316                 *err_code = error;
3317
3318         EM_DEBUG_FUNC_END("ret [%d]", ret);
3319         return ret;
3320
3321 }
3322
3323 INTERNAL_FUNC int emstorage_get_account_count(char *multi_user_name, int *count, int transaction, int *err_code)
3324 {
3325         EM_DEBUG_FUNC_BEGIN();
3326
3327         if (!count) {
3328                 if (err_code != NULL)
3329                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3330                 return false;
3331         }
3332
3333         int rc = -1, ret = false;
3334         int error = EMAIL_ERROR_NONE;
3335
3336         DB_STMT hStmt = NULL;
3337         char sql_query_string[QUERY_SIZE] = {0, };
3338
3339
3340         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3341         EMSTORAGE_START_READ_TRANSACTION(transaction);
3342         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_account_tbl");
3343         EM_DEBUG_LOG_SEC("SQL STMT [%s]", sql_query_string);
3344         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string,
3345                                                           EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3346         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
3347         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3348                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3349
3350         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3351         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3352                 ("sqlite3_step fail:%d", rc));
3353
3354         *count = sqlite3_column_int(hStmt, 0);
3355
3356         ret = true;
3357
3358 FINISH_OFF:
3359
3360         if (hStmt != NULL) {
3361                 rc = sqlite3_finalize(hStmt);
3362                 hStmt = NULL;
3363                 if (rc != SQLITE_OK) {
3364                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3365                         error = EMAIL_ERROR_DB_FAILURE;
3366                 }
3367         }
3368
3369         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3370
3371         if (err_code != NULL)
3372                 *err_code = error;
3373
3374         EM_DEBUG_FUNC_END("ret [%d]", ret);
3375         return ret;
3376 }
3377
3378 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)
3379 {
3380         EM_DEBUG_FUNC_BEGIN();
3381
3382         int i = 0, count = 0, rc = -1, ret = false;
3383         int field_index = 0;
3384         int sql_len = 0;
3385         int error = EMAIL_ERROR_NONE;
3386         emstorage_account_tbl_t *p_data_tbl = NULL;
3387         char sql_query_string[QUERY_SIZE] = {0, };
3388         char *sql = "SELECT count(*) FROM mail_account_tbl;";
3389         char **result;
3390         sqlite3 *local_db_handle = NULL;
3391         DB_STMT hStmt = NULL;
3392
3393         if (!select_num || !account_list) {
3394                 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, account_list);
3395                 if (err_code != NULL)
3396                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3397                 return false;
3398         }
3399
3400         local_db_handle = emstorage_get_db_connection(multi_user_name);
3401
3402         EMSTORAGE_START_READ_TRANSACTION(transaction);
3403
3404         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
3405         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3406                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
3407
3408         count = atoi(result[1]);
3409         sqlite3_free_table(result);
3410
3411         EM_DEBUG_LOG_DEV("count = %d", rc);
3412
3413         if (count == 0) {
3414                 EM_DEBUG_LOG("no account found...");
3415                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3416                 ret = true;
3417                 goto FINISH_OFF;
3418         }
3419         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
3420         sql_len = EM_SAFE_STRLEN(sql_query_string);
3421
3422         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
3423                 "account_id, "
3424                 "account_name, "
3425                 "logo_icon_path, "
3426                 "user_data, "
3427                 "user_data_length, "
3428                 "account_svc_id, "
3429                 "sync_status, "
3430                 "sync_disabled, "
3431                 "default_mail_slot_size, "
3432                 "roaming_option, "
3433                 "color_label, "
3434                 "user_display_name, "
3435                 "user_email_address, "
3436                 "reply_to_address, "
3437                 "return_address, "
3438                 "incoming_server_type, "
3439                 "incoming_server_address, "
3440                 "incoming_server_port_number, "
3441                 "incoming_server_user_name, "
3442                 "incoming_server_password, "
3443                 "incoming_server_secure_connection, "
3444                 "incoming_server_authentication_method, "
3445                 "retrieval_mode, "
3446                 "keep_mails_on_pop_server_after_download, "
3447                 "check_interval, "
3448                 "auto_download_size, "
3449                 "peak_interval, "
3450                 "peak_days, "
3451                 "peak_start_time, "
3452                 "peak_end_time, "
3453                 "outgoing_server_type, "
3454                 "outgoing_server_address, "
3455                 "outgoing_server_port_number, "
3456                 "outgoing_server_user_name, "
3457                 "outgoing_server_password, "
3458                 "outgoing_server_secure_connection, "
3459                 "outgoing_server_need_authentication, "
3460                 "outgoing_server_use_same_authenticator, "
3461                 "priority, "
3462                 "keep_local_copy, "
3463                 "req_delivery_receipt, "
3464                 "req_read_receipt, "
3465                 "download_limit, "
3466                 "block_address, "
3467                 "block_subject, "
3468                 "display_name_from, "
3469                 "reply_with_body, "
3470                 "forward_with_files, "
3471                 "add_myname_card, "
3472                 "add_signature, "
3473                 "signature, "
3474                 "add_my_address_to_bcc, "
3475                 "notification_status, "
3476                 "vibrate_status, "
3477                 "display_content_status, "
3478                 "default_ringtone_status, "
3479                 "alert_ringtone_path, "
3480                 "auto_resend_times, "
3481                 "outgoing_server_size_limit, "
3482                 "wifi_auto_download, "
3483                 "pop_before_smtp, "
3484                 "incoming_server_requires_apop, "
3485                 "smime_type, "
3486                 "certificate_path, "
3487                 "cipher_type, "
3488                 "digest_type"
3489         );
3490
3491         sql_len = EM_SAFE_STRLEN(sql_query_string);
3492
3493         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " FROM mail_account_tbl ORDER BY account_id");
3494
3495         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3496
3497         EM_DEBUG_LOG_DEV("After sqlite3_prepare_v2 hStmt = %p", hStmt);
3498         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3499                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3500
3501         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3502         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3503                 ("sqlite3_step fail:%d", rc));
3504         if (rc == SQLITE_DONE) {
3505                 EM_DEBUG_EXCEPTION("no account found...");
3506
3507                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3508                 count = 0;
3509                 ret = true;
3510                 goto FINISH_OFF;
3511         }
3512
3513         if (!(p_data_tbl = (emstorage_account_tbl_t*)malloc(sizeof(emstorage_account_tbl_t) * count))) {
3514                 EM_DEBUG_EXCEPTION("malloc failed...");
3515                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3516                 goto FINISH_OFF;
3517         }
3518         memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * count);
3519         for (i = 0; i < count; i++) {
3520                 /*  get recordset */
3521                 field_index = 0;
3522                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_id), field_index++);
3523                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].account_name), 0, field_index++);
3524                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].logo_icon_path), 0, field_index++);
3525                 _get_stmt_field_data_blob(hStmt, &(p_data_tbl[i].user_data), field_index++);
3526                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].user_data_length), field_index++);
3527                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_svc_id), field_index++);
3528                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_status), field_index++);
3529                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_disabled), field_index++);
3530                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].default_mail_slot_size), field_index++);
3531                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].roaming_option), field_index++);
3532                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].color_label), field_index++);
3533                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_display_name), 0, field_index++);
3534                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_email_address), 0, field_index++);
3535                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].reply_to_address), 0, field_index++);
3536                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].return_address), 0, field_index++);
3537                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].incoming_server_type), field_index++);
3538                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_address), 0, field_index++);
3539                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_port_number), field_index++);
3540                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_user_name), 0, field_index++);
3541                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_password), 0, field_index++);
3542                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_secure_connection), field_index++);
3543                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].incoming_server_authentication_method), field_index++);
3544                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].retrieval_mode), field_index++);
3545                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].keep_mails_on_pop_server_after_download), field_index++);
3546                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].check_interval), field_index++);
3547                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_download_size), field_index++);
3548                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_interval), field_index++);
3549                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_days), field_index++);
3550                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_start_time), field_index++);
3551                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_end_time), field_index++);
3552                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].outgoing_server_type), field_index++);
3553                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_address), 0, field_index++);
3554                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_port_number), field_index++);
3555                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_user_name), 0, field_index++);
3556                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_password), 0, field_index++);
3557                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_secure_connection), field_index++);
3558                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_need_authentication), field_index++);
3559                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_use_same_authenticator), field_index++);
3560                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.priority), field_index++);
3561                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.keep_local_copy), field_index++);
3562                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_delivery_receipt), field_index++);
3563                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_read_receipt), field_index++);
3564                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.download_limit), field_index++);
3565                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_address), field_index++);
3566                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_subject), field_index++);
3567                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.display_name_from), 0, field_index++);
3568                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.reply_with_body), field_index++);
3569                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.forward_with_files), field_index++);
3570                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_myname_card), field_index++);
3571                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_signature), field_index++);
3572                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.signature), 0, field_index++);
3573                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.add_my_address_to_bcc), field_index++);
3574                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.notification_status), field_index++);
3575                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.vibrate_status), field_index++);
3576                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.display_content_status), field_index++);
3577                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.default_ringtone_status), field_index++);
3578                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.alert_ringtone_path), 0, field_index++);
3579                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_resend_times), field_index++);
3580                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_size_limit), field_index++);
3581                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].wifi_auto_download), field_index++);
3582                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].pop_before_smtp), field_index++);
3583                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_requires_apop), field_index++);
3584                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].smime_type), field_index++);
3585                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].certificate_path), 0, field_index++);
3586                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].cipher_type), field_index++);
3587                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].digest_type), field_index++);
3588
3589                 /* EAS passwd is not accessible */
3590                 if (with_password == true && p_data_tbl[i].incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
3591                         /*  get password from the secure storage */
3592                         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3593                         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3594
3595                         EM_SAFE_FREE(p_data_tbl[i].incoming_server_password);
3596                         EM_SAFE_FREE(p_data_tbl[i].outgoing_server_password);
3597
3598                         /*  get password file name */
3599                         error = _get_password_file_name(multi_user_name, p_data_tbl[i].account_id,
3600                                                    recv_password_file_name,
3601                                                    send_password_file_name);
3602                         if (error != EMAIL_ERROR_NONE) {
3603                                 EM_DEBUG_EXCEPTION("_get_password_file_name error [%d]", error);
3604                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3605                                 goto FINISH_OFF;
3606                         }
3607
3608                         /*  read password from secure storage */
3609                         error = _read_password_from_secure_storage(recv_password_file_name,
3610                                           &(p_data_tbl[i].incoming_server_password));
3611                         if (error < 0) {
3612                                 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s] error [%d]",
3613                                                     recv_password_file_name, error);
3614                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3615                                 goto FINISH_OFF;
3616                         }
3617                         error = _read_password_from_secure_storage(send_password_file_name,
3618                                           &(p_data_tbl[i].outgoing_server_password));
3619                         if (error < 0) {
3620                                 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s]  error [%d]",
3621                                                      send_password_file_name, error);
3622                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3623                                 goto FINISH_OFF;
3624                         }
3625                 }
3626
3627                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3628                 EM_DEBUG_LOG_DEV("after sqlite3_step(), i = %d, rc = %d.", i,  rc);
3629                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3630                         ("sqlite3_step fail:%d", rc));
3631         }
3632
3633         ret = true;
3634
3635 FINISH_OFF:
3636         if (ret == true) {
3637                 *account_list = p_data_tbl;
3638                 *select_num = count;
3639                 EM_DEBUG_LOG_DEV("COUNT : %d", count);
3640         } else if (p_data_tbl != NULL)
3641                 emstorage_free_account(&p_data_tbl, count, NULL);
3642
3643         if (hStmt != NULL) {
3644                 rc = sqlite3_finalize(hStmt);
3645                 hStmt = NULL;
3646                 if (rc != SQLITE_OK) {
3647                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3648                         error = EMAIL_ERROR_DB_FAILURE;
3649                 }
3650         }
3651
3652         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3653
3654         if (err_code != NULL)
3655                 *err_code = error;
3656
3657         EM_DEBUG_FUNC_END("ret [%d]", ret);
3658         return ret;
3659 }
3660
3661 INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(char *multi_user_name,
3662                                                                                                                         char *server_mail_id,
3663                                                                                                                         int mailbox_id,
3664                                                                                                                         emstorage_mail_tbl_t **mail,
3665                                                                                                                         int transaction,
3666                                                                                                                         int *err_code)
3667 {
3668         EM_DEBUG_FUNC_BEGIN_SEC("mailbox_id [%d], server_mail_id[%s], mail[%p], transaction[%d], err_code[%p]",
3669                                                         mailbox_id, server_mail_id, mail, transaction, err_code);
3670
3671         int ret = false, error = EMAIL_ERROR_NONE, result_count;
3672         char conditional_clause[QUERY_SIZE] = {0, };
3673         emstorage_mail_tbl_t* p_data_tbl = NULL;
3674
3675         if (!server_mail_id || !mail) {
3676                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3677                 error = EMAIL_ERROR_INVALID_PARAM;
3678                 goto FINISH_OFF;
3679         }
3680
3681         sqlite3_snprintf(sizeof(conditional_clause), conditional_clause, "WHERE server_mail_id = '%q'", server_mail_id);
3682
3683         if (mailbox_id > 0) {
3684                 SNPRINTF(conditional_clause + strlen(conditional_clause),
3685                                         QUERY_SIZE - strlen(conditional_clause),
3686                                         " AND mailbox_id = %d",
3687                                         mailbox_id);
3688         }
3689
3690         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
3691         if (!emstorage_query_mail_tbl(multi_user_name,
3692                                                                         conditional_clause,
3693                                                                         transaction,
3694                                                                         &p_data_tbl,
3695                                                                         &result_count,
3696                                                                         &error)) {
3697                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3698                 goto FINISH_OFF;
3699         }
3700
3701         ret = true;
3702
3703 FINISH_OFF:
3704
3705         if (ret == true)
3706                 *mail = p_data_tbl;
3707
3708         if (err_code != NULL)
3709                 *err_code = error;
3710
3711         EM_DEBUG_FUNC_END("ret [%d]", ret);
3712         return ret;
3713 }
3714
3715
3716 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)
3717 {
3718         int cur_clause = 0, conditional_clause_count = 0, i = 0;
3719
3720         if (account_id < ALL_ACCOUNT) {
3721                 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3722                 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3723         }
3724
3725         /*  where clause */
3726         if (account_id != ALL_ACCOUNT) {
3727                 cur_clause += (conditional_clause_count++ == 0) ?
3728                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE account_id = %d", account_id) :
3729                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND account_id = %d", account_id);
3730         }
3731
3732         if (mailbox_id > 0) {
3733                 cur_clause += (conditional_clause_count++ == 0) ?
3734                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_id = %d", mailbox_id) :
3735                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_id = %d", mailbox_id);
3736         }
3737
3738         if (thread_id > 0) {
3739                 cur_clause += (conditional_clause_count++ == 0) ?
3740                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_id = %d ", thread_id) :
3741                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_id = %d ", thread_id);
3742         } else if (thread_id == EMAIL_LIST_TYPE_THREAD) {
3743                 cur_clause += (conditional_clause_count++ == 0) ?
3744                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_item_count > 0") :
3745                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_item_count > 0");
3746         } else if (thread_id == EMAIL_LIST_TYPE_LOCAL) {
3747                 cur_clause += (conditional_clause_count++ == 0) ?
3748                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE server_mail_status = 0") :
3749                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND server_mail_status = 0");
3750         } else if (thread_id == EMAIL_LIST_TYPE_UNREAD) {
3751                 cur_clause += (conditional_clause_count++ == 0) ?
3752                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_seen_field == 0") :
3753                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_seen_field == 0");
3754         }
3755
3756         /*  EM_DEBUG_LOG("where clause added [%s]", conditional_clause_string); */
3757         if (addr_list && addr_list->address_count > 0) {
3758                 if (!addr_list->address_type) {
3759                         cur_clause += (conditional_clause_count++ == 0) ?
3760                                 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE email_address_sender IN(\"%s\"", addr_list->address_list[0]) :
3761                                 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND email_address_sender IN(\"%s\"", addr_list->address_list[0]);
3762
3763                         for (i = 1; i < addr_list->address_count; i++)
3764                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3765
3766                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3767                 } else {
3768                         cur_clause += (conditional_clause_count++ == 0) ?
3769                                 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]) :
3770                                 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]);
3771
3772                         for (i = 1; i < addr_list->address_count; i++)
3773                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3774
3775                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3776                 }
3777         }
3778
3779         if (input_except_delete_flagged_mails) {
3780                 cur_clause += (conditional_clause_count++ == 0) ?
3781                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_deleted_field = 0") :
3782                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_deleted_field = 0");
3783         }
3784
3785         if (search_value) {
3786                 switch (search_type) {
3787                         case EMAIL_SEARCH_FILTER_SUBJECT:
3788                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3789                                         " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3790                                 break;
3791                         case EMAIL_SEARCH_FILTER_SENDER:
3792                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3793                                         " %s  ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3794                                         ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3795                                 break;
3796                         case EMAIL_SEARCH_FILTER_RECIPIENT:
3797                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3798                                         " %s ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3799                                         "       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3800                                         "       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3801                                         ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value);
3802                                 break;
3803                         case EMAIL_SEARCH_FILTER_ALL:
3804                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3805                                         " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\' "
3806                                         "       OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3807                                         "                       OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3808                                         "                       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3809                                         "                       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3810                                         "               ) "
3811                                         "       )"
3812                                         ")", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value, search_value, search_value);
3813                                 break;
3814                 }
3815         }
3816
3817         /*  EM_DEBUG_LOG("where clause [%s]", conditional_clause_string); */
3818         static char sorting_str[][50] = {
3819                          " ORDER BY date_time DESC",                                             /* case EMAIL_SORT_DATETIME_HIGH: */
3820                          " ORDER BY date_time ASC",                                               /* case EMAIL_SORT_DATETIME_LOW: */
3821                          " ORDER BY full_address_from DESC, date_time DESC", /* case EMAIL_SORT_SENDER_HIGH: */
3822                          " ORDER BY full_address_from ASC, date_time DESC",  /* case EMAIL_SORT_SENDER_LOW: */
3823                          " ORDER BY full_address_to DESC, date_time DESC",   /* case EMAIL_SORT_RCPT_HIGH: */
3824                          " ORDER BY full_address_to ASC, date_time DESC",       /* case EMAIL_SORT_RCPT_LOW: */
3825                          " ORDER BY subject DESC, date_time DESC",                 /* case EMAIL_SORT_SUBJECT_HIGH: */
3826                          " ORDER BY subject ASC, date_time DESC",                       /* case EMAIL_SORT_SUBJECT_LOW: */
3827                          " ORDER BY priority DESC, date_time DESC",               /* case EMAIL_SORT_PRIORITY_HIGH: */
3828                          " ORDER BY priority ASC, date_time DESC",                 /* case EMAIL_SORT_PRIORITY_LOW: */
3829                          " ORDER BY attachment_count DESC, date_time DESC",  /* case EMAIL_SORT_ATTACHMENT_HIGH: */
3830                          " ORDER BY attachment_count ASC, date_time DESC",   /* case EMAIL_SORT_ATTACHMENT_LOW: */
3831                          " ORDER BY lock_status DESC, date_time DESC",     /* case EMAIL_SORT_FAVORITE_HIGH: */
3832                          " ORDER BY lock_status ASC, date_time DESC",           /* case EMAIL_SORT_FAVORITE_LOW: */
3833                          " ORDER BY mailbox_id DESC, date_time DESC",     /* case EMAIL_SORT_MAILBOX_ID_HIGH: */
3834                          " ORDER BY mailbox_id ASC, date_time DESC",            /* case EMAIL_SORT_MAILBOX_ID_LOW: */
3835                          " ORDER BY flags_flagged_field DESC, date_time DESC",  /* case EMAIL_SORT_FLAGGED_FLAG_HIGH: */
3836                          " ORDER BY flags_flagged_field ASC, date_time DESC",   /* case EMAIL_SORT_FLAGGED_FLAG_LOW: */
3837                          " ORDER BY flags_seen_field DESC, date_time DESC",     /* case EMAIL_SORT_SEEN_FLAG_HIGH: */
3838                          " ORDER BY flags_seen_field ASC, date_time DESC"       /* case EMAIL_SORT_SEEN_FLAG_LOW: */
3839                          };
3840
3841         if (sorting < EMAIL_SORT_END && sorting >= 0)
3842                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " %s", sorting_str[sorting]);
3843         else
3844                 EM_DEBUG_LOG(" Invalid Sorting order ");
3845
3846         /*  limit clause */
3847         if (start_index != -1 && limit_count != -1)
3848                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " LIMIT %d, %d", start_index, limit_count);
3849
3850         return true;
3851 }
3852
3853
3854 /**
3855   *     emstorage_get_mail_list - Get the mail list information.
3856   *
3857   *
3858   */
3859 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)
3860 {
3861         EM_DEBUG_FUNC_BEGIN();
3862         EM_PROFILE_BEGIN(emstorage_get_mail_list_func);
3863
3864         int ret = false, error = EMAIL_ERROR_NONE;
3865         char conditional_clause_string[QUERY_SIZE] = { 0, };
3866
3867         if (account_id < ALL_ACCOUNT) {
3868                 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3869                 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3870         }
3871         EM_IF_NULL_RETURN_VALUE(result_count, false);
3872
3873         _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);
3874
3875         EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
3876
3877         if (!emstorage_query_mail_list(multi_user_name, conditional_clause_string, transaction, mail_list, result_count, &error)) {
3878                 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3879                 goto FINISH_OFF;
3880         }
3881
3882         ret = true;
3883
3884 FINISH_OFF:
3885         if (err_code != NULL)
3886                 *err_code = error;
3887
3888         EM_PROFILE_END(emstorage_get_mail_list_func);
3889         EM_DEBUG_FUNC_END("ret [%d]", ret);
3890         return ret;
3891 }
3892
3893
3894 /**
3895   *     emstorage_get_mails - Get the Mail list information based on mailbox_name name
3896   *
3897   *
3898   */
3899 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)
3900 {
3901         EM_DEBUG_FUNC_BEGIN();
3902         EM_PROFILE_BEGIN(emStorageGetMails);
3903
3904         int count = 0, ret = false, error = EMAIL_ERROR_NONE;
3905         emstorage_mail_tbl_t *p_data_tbl = NULL;
3906         char conditional_clause_string[QUERY_SIZE] = {0, };
3907
3908         EM_IF_NULL_RETURN_VALUE(mail_list, false);
3909         EM_IF_NULL_RETURN_VALUE(result_count, false);
3910
3911         if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3912                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3913                 if (err_code != NULL)
3914                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3915                 return false;
3916         }
3917
3918         _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);
3919
3920         EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
3921
3922         if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause_string, transaction, &p_data_tbl, &count,  &error)) {
3923                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3924                 goto FINISH_OFF;
3925         }
3926
3927         ret = true;
3928
3929 FINISH_OFF:
3930
3931         if (ret == true) {
3932                 *mail_list = p_data_tbl;
3933                 *result_count = count;
3934                 EM_DEBUG_LOG("COUNT : %d", count);
3935         } else if (p_data_tbl != NULL)
3936                 emstorage_free_mail(&p_data_tbl, count, NULL);
3937
3938         if (err_code != NULL)
3939                 *err_code = error;
3940
3941         EM_PROFILE_END(emStorageGetMails);
3942         EM_DEBUG_FUNC_END("ret [%d]", ret);
3943         return ret;
3944 }
3945
3946
3947
3948 /**
3949   *     emstorage_get_searched_mail_list - Get the mail list information after filtering
3950   *
3951   *
3952   */
3953 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)
3954 {
3955         EM_DEBUG_FUNC_BEGIN();
3956
3957         int ret = false, error = EMAIL_ERROR_NONE;
3958         char conditional_clause[QUERY_SIZE] = {0, };
3959         char *temp_search_value = NULL;
3960         char *temp_search_value2 = NULL;
3961
3962         EM_IF_NULL_RETURN_VALUE(mail_list, false);
3963         EM_IF_NULL_RETURN_VALUE(result_count, false);
3964
3965         if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3966                 EM_DEBUG_EXCEPTION("select_num[%p], Mail_list[%p]", result_count, mail_list);
3967                 error = EMAIL_ERROR_INVALID_PARAM;
3968                 goto FINISH_OFF;
3969         }
3970
3971         temp_search_value = em_replace_all_string((char*)search_value, "_", "\\_");
3972         temp_search_value2 = em_replace_all_string(temp_search_value, "%", "\\%");
3973
3974         _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);
3975
3976         EM_DEBUG_LOG("conditional_clause[%s]", conditional_clause);
3977
3978         if (!emstorage_query_mail_list(multi_user_name, conditional_clause, transaction, mail_list, result_count, &error)) {
3979                 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3980                 goto FINISH_OFF;
3981         }
3982
3983         ret = true;
3984
3985 FINISH_OFF:
3986         EM_DEBUG_LOG("emstorage_get_searched_mail_list finish off");
3987
3988         if (err_code != NULL)
3989                 *err_code = error;
3990
3991         EM_SAFE_FREE(temp_search_value);
3992         EM_SAFE_FREE(temp_search_value2);
3993
3994         EM_DEBUG_FUNC_END("ret [%d]", ret);
3995         return ret;
3996 }
3997
3998
3999 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name)
4000 {
4001         EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
4002
4003         if (account_id <= 0 || !recv_password_file_name || !send_password_file_name) {
4004                 EM_DEBUG_EXCEPTION("Invalid parameter");
4005                 return EMAIL_ERROR_INVALID_PARAM;
4006         }
4007
4008     EM_DEBUG_LOG("MULTI_USER_NAME : [%s]", multi_user_name);
4009
4010         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
4011                 snprintf(recv_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_recv_%s", account_id, multi_user_name);
4012                 snprintf(send_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_send_%s", account_id, multi_user_name);
4013         } else {
4014                 snprintf(recv_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_recv", account_id);
4015                 snprintf(send_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_send", account_id);
4016         }
4017
4018         EM_DEBUG_FUNC_END();
4019         return EMAIL_ERROR_NONE;
4020 }
4021
4022 static int _read_password_from_secure_storage(char *file_name, char **password)
4023 {
4024         EM_DEBUG_FUNC_BEGIN_SEC("file_name[%s]", file_name);
4025
4026         if (!file_name || !password) {
4027                 EM_DEBUG_EXCEPTION("Invalid Parameter");
4028                 return EMAIL_ERROR_INVALID_PARAM;
4029         }
4030
4031         int ret = EMAIL_ERROR_NONE;
4032
4033         ret = emcore_get_password_in_key_manager(file_name, password);
4034         if (ret != EMAIL_ERROR_NONE) {
4035                 EM_DEBUG_EXCEPTION("emcore_get_password_in_key_manager failed : [%d]", ret);
4036                 goto FINISH_OFF;
4037         }
4038
4039 FINISH_OFF:
4040
4041         EM_DEBUG_FUNC_END("ret [%d]", ret);
4042         return ret;
4043 }
4044
4045 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)
4046 {
4047         EM_DEBUG_FUNC_BEGIN("account_id[%d], pulloption[%d], account[%p], transaction[%d], err_code[%p]", account_id, pulloption, account, transaction, err_code);
4048
4049         if (!account) {
4050                 EM_DEBUG_EXCEPTION("account_id[%d], account[%p]", account_id, account);
4051                 if (err_code != NULL)
4052                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4053                 return false;
4054         }
4055
4056         int ret = false;
4057         int error = EMAIL_ERROR_NONE;
4058         emstorage_account_tbl_t* p_data_tbl = NULL;
4059         DB_STMT hStmt = NULL;
4060         char sql_query_string[QUERY_SIZE] = {0, };
4061         int rc = -1;
4062         int sql_len = 0;
4063         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4064         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4065
4066         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4067
4068         EMSTORAGE_START_READ_TRANSACTION(transaction);
4069
4070         /*  Make query string */
4071         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
4072         sql_len = EM_SAFE_STRLEN(sql_query_string);
4073
4074         if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4075                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4076                         "incoming_server_type,"
4077                         "incoming_server_address,"
4078                         "user_email_address,"
4079                         "incoming_server_user_name,"
4080                         "retrieval_mode,"
4081                         "incoming_server_port_number,"
4082                         "incoming_server_secure_connection,"
4083                         "incoming_server_authentication_method,"
4084                         "outgoing_server_type,"
4085                         "outgoing_server_address,"
4086                         "outgoing_server_port_number,"
4087                         "outgoing_server_need_authentication,"
4088                         "outgoing_server_secure_connection,"
4089                         "outgoing_server_user_name,"
4090                         "user_display_name,"
4091                         "reply_to_address,"
4092                         "return_address,"
4093                         "account_id,"
4094                         "keep_mails_on_pop_server_after_download,"
4095                         "auto_download_size,"
4096                         "peak_interval,"
4097                         "peak_days,"
4098                         "peak_start_time,"
4099                         "peak_end_time,"
4100                         "outgoing_server_use_same_authenticator,"
4101                         "auto_resend_times,"
4102                         "outgoing_server_size_limit,"
4103                         "wifi_auto_download,"
4104                         "pop_before_smtp,"
4105                         "incoming_server_requires_apop,"
4106                         "logo_icon_path,"
4107                         "user_data,"
4108                         "user_data_length,"
4109                         "color_label,"
4110                         "check_interval,"
4111                         "sync_status,");
4112                 sql_len = EM_SAFE_STRLEN(sql_query_string);
4113         }
4114
4115         if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME) {
4116                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " account_name, ");
4117                 sql_len = EM_SAFE_STRLEN(sql_query_string);
4118         }
4119
4120         /*  get from secure storage, not from db */
4121         if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4122                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4123                         "priority,"
4124                         "keep_local_copy,"
4125                         "req_delivery_receipt,"
4126                         "req_read_receipt,"
4127                         "download_limit,"
4128                         "block_address,"
4129                         "block_subject,"
4130                         "display_name_from,"
4131                         "reply_with_body,"
4132                         "forward_with_files,"
4133                         "add_myname_card,"
4134                         "add_signature,"
4135                         "signature,"
4136                         "add_my_address_to_bcc,"
4137                         "notification_status,"
4138                         "vibrate_status,"
4139                         "display_content_status,"
4140                         "default_ringtone_status,"
4141                         "alert_ringtone_path,"
4142                         "account_svc_id,"
4143                         "sync_disabled,"
4144                         "default_mail_slot_size,"
4145                         "roaming_option,"
4146                         "smime_type,"
4147                         "certificate_path,"
4148                         "cipher_type,"
4149                         "digest_type,");
4150
4151                 sql_len = EM_SAFE_STRLEN(sql_query_string);
4152         }
4153
4154         /*  dummy value, FROM WHERE clause */
4155         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "0 FROM mail_account_tbl WHERE account_id = %d", account_id);
4156
4157         /*  FROM clause */
4158         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
4159
4160         /*  execute a sql and count rows */
4161         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4162         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4163                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4164
4165         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4166         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4167                 ("sqlite3_step fail:%d", rc));
4168
4169         if (rc == SQLITE_DONE) {
4170                 EM_DEBUG_EXCEPTION("no matched account found...");
4171                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4172                 goto FINISH_OFF;
4173         }
4174
4175         /*  Assign query result to structure */
4176         if (!(p_data_tbl = (emstorage_account_tbl_t *)malloc(sizeof(emstorage_account_tbl_t) * 1))) {
4177                 EM_DEBUG_EXCEPTION("malloc failed...");
4178                 error = EMAIL_ERROR_OUT_OF_MEMORY;
4179                 goto FINISH_OFF;
4180         }
4181
4182         memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * 1);
4183         int col_index = 0;
4184
4185         if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4186                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_type), col_index++);
4187                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_address),  0, col_index++);
4188                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_email_address), 0, col_index++);
4189                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_user_name), 0, col_index++);
4190                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->retrieval_mode), col_index++);
4191                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_port_number), col_index++);
4192                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_secure_connection), col_index++);
4193                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_authentication_method), col_index++);
4194                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->outgoing_server_type), col_index++);
4195                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_address), 0, col_index++);
4196                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_port_number), col_index++);
4197                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_need_authentication), col_index++);
4198                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_secure_connection), col_index++);
4199                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_user_name), 0, col_index++);
4200                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_display_name), 0, col_index++);
4201                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->reply_to_address), 0, col_index++);
4202                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->return_address), 0, col_index++);
4203                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
4204                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->keep_mails_on_pop_server_after_download), col_index++);
4205                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_download_size), col_index++);
4206                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_interval), col_index++);
4207                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_days), col_index++);
4208                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_start_time), col_index++);
4209                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_end_time), col_index++);
4210                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_use_same_authenticator), col_index++);
4211                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_resend_times), col_index++);
4212                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_size_limit), col_index++);
4213                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->wifi_auto_download), col_index++);
4214                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->pop_before_smtp), col_index++);
4215                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_requires_apop), col_index++);
4216                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->logo_icon_path), 0, col_index++);
4217                 _get_stmt_field_data_blob(hStmt, &p_data_tbl->user_data, col_index++);
4218                 _get_stmt_field_data_int(hStmt, &p_data_tbl->user_data_length, col_index++);
4219                 _get_stmt_field_data_int(hStmt, &p_data_tbl->color_label, col_index++);
4220                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->check_interval), col_index++);
4221                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->sync_status), col_index++);
4222         }
4223
4224         if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME)
4225                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->account_name), 0, col_index++);
4226
4227         if (pulloption & EMAIL_ACC_GET_OPT_PASSWORD) {
4228                 /*  get password file name */
4229                 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) {
4230                         EM_DEBUG_EXCEPTION("_get_password_file_name failed [%d]", error);
4231                         goto FINISH_OFF;
4232                 }
4233
4234                 /*  read password from secure storage */
4235                 if ((error = _read_password_from_secure_storage(recv_password_file_name, &(p_data_tbl->incoming_server_password))) < 0) {
4236                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4237                         goto FINISH_OFF;
4238                 }
4239
4240                 EM_DEBUG_LOG_DEV("recv_password_file_name[%s], password[%s]", recv_password_file_name,  p_data_tbl->incoming_server_password);
4241
4242                 if (p_data_tbl->outgoing_server_use_same_authenticator == 0) {
4243                         if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl->outgoing_server_password))) < 0) {
4244                                 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4245                                 goto FINISH_OFF;
4246                         }
4247                         EM_DEBUG_LOG_DEV("send_password_file_name[%s], password[%s]", send_password_file_name,  p_data_tbl->outgoing_server_password);
4248                 }
4249         }
4250
4251         if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4252                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.priority), col_index++);
4253                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.keep_local_copy), col_index++);
4254                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_delivery_receipt), col_index++);
4255                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_read_receipt), col_index++);
4256                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.download_limit), col_index++);
4257                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_address), col_index++);
4258                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_subject), col_index++);
4259                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.display_name_from), 0, col_index++);
4260                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.reply_with_body), col_index++);
4261                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.forward_with_files), col_index++);
4262                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_myname_card), col_index++);
4263                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_signature), col_index++);
4264                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.signature), 0, col_index++);
4265                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.add_my_address_to_bcc), col_index++);
4266                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.notification_status), col_index++);
4267                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.vibrate_status), col_index++);
4268                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.display_content_status), col_index++);
4269                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.default_ringtone_status), col_index++);
4270                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.alert_ringtone_path), 0, col_index++);
4271                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_svc_id), col_index++);
4272                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->sync_disabled), col_index++);
4273                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->default_mail_slot_size), col_index++);
4274                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->roaming_option), col_index++);
4275                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->smime_type), col_index++);
4276                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->certificate_path), 0, col_index++);
4277                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->cipher_type), col_index++);
4278                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->digest_type), col_index++);
4279         }
4280
4281         ret = true;
4282
4283 FINISH_OFF:
4284         if (ret == true)
4285                 *account = p_data_tbl;
4286         else {
4287                 if (p_data_tbl)
4288                         emstorage_free_account((emstorage_account_tbl_t **)&p_data_tbl, 1, NULL);
4289         }
4290         if (hStmt != NULL) {
4291                 rc = sqlite3_finalize(hStmt);
4292                 if (rc != SQLITE_OK) {
4293                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4294                         error = EMAIL_ERROR_DB_FAILURE;
4295                 }
4296         }
4297
4298         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4299
4300         if (err_code != NULL)
4301                 *err_code = error;
4302
4303         EM_DEBUG_FUNC_END("ret [%d]", ret);
4304         return ret;
4305 }
4306
4307 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)
4308 {
4309         EM_DEBUG_FUNC_BEGIN("account_id[%d], password_length[%p], err_code[%p]", account_id, password_length, err_code);
4310
4311         if (account_id <= 0 || password_length == NULL) {
4312                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4313                 if (err_code != NULL)
4314                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4315                 return false;
4316         }
4317
4318         int ret = false;
4319         int error = EMAIL_ERROR_NONE;
4320         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4321         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4322         char *temp_password = NULL;
4323
4324
4325         /*  get password file name */
4326         if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4327                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4328                 goto FINISH_OFF;
4329         }
4330
4331         /*  read password from secure storage */
4332         if (password_type == EMAIL_GET_INCOMING_PASSWORD_LENGTH) {
4333                 if ((error = _read_password_from_secure_storage(recv_password_file_name, &temp_password)) < 0 || !temp_password) {
4334                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed...");
4335                         goto FINISH_OFF;
4336                 }
4337         } else if (password_type == EMAIL_GET_OUTGOING_PASSWORD_LENGTH) {
4338                 if ((error = _read_password_from_secure_storage(send_password_file_name, &temp_password)) < 0 || !temp_password) {
4339                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed...");
4340                         goto FINISH_OFF;
4341                 }
4342         } else {
4343                 EM_DEBUG_LOG("Invalid password type");
4344                 if (err_code != NULL)
4345                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4346                 goto FINISH_OFF;
4347         }
4348
4349         *password_length = EM_SAFE_STRLEN(temp_password);
4350
4351         EM_DEBUG_LOG_SEC("recv_password_file_name[%s], *password_length[%d]", recv_password_file_name,  *password_length);
4352
4353         ret = true;
4354
4355 FINISH_OFF:
4356         EM_SAFE_FREE(temp_password);
4357
4358         if (err_code != NULL)
4359                 *err_code = error;
4360
4361         EM_DEBUG_FUNC_END("ret [%d]", ret);
4362         return ret;
4363 }
4364
4365 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)
4366 {
4367         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);
4368
4369         int err = EMAIL_ERROR_NONE;
4370         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4371         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4372
4373         if (input_incoming_server_password == NULL && input_outgoing_server_password == NULL) {
4374                 EM_DEBUG_EXCEPTION_SEC("Invalid param");
4375                 err = EMAIL_ERROR_INVALID_PARAM;
4376                 goto FINISH_OFF;
4377         }
4378
4379         /*  get password file name */
4380         if ((err = _get_password_file_name(multi_user_name,
4381                                                                                 input_account_id,
4382                                                                                 recv_password_file_name,
4383                                                                                 send_password_file_name)) != EMAIL_ERROR_NONE) {
4384                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4385                 goto FINISH_OFF;
4386         }
4387
4388         EM_DEBUG_LOG_SEC("recv_password_file_name [%s] input_incoming_server_password [%s]",
4389                                                 recv_password_file_name, input_incoming_server_password);
4390         EM_DEBUG_LOG_SEC("send_password_file_name [%s] input_outgoing_server_password [%s]",
4391                                                 send_password_file_name, input_outgoing_server_password);
4392
4393         if (input_incoming_server_password) {
4394                 err = emcore_remove_password_in_key_manager(recv_password_file_name);
4395                 if (err != EMAIL_ERROR_NONE) {
4396                         EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", recv_password_file_name);
4397                         goto FINISH_OFF;
4398                 }
4399
4400                 /*  save recv passwords to the secure storage */
4401                 err = emcore_add_password_in_key_manager(recv_password_file_name, input_incoming_server_password);
4402                 if (err != EMAIL_ERROR_NONE) {
4403                         EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4404                         goto FINISH_OFF;
4405                 }
4406         }
4407
4408         if (input_outgoing_server_password) {
4409                 err = emcore_remove_password_in_key_manager(send_password_file_name);
4410                 if (err != EMAIL_ERROR_NONE) {
4411                         EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", send_password_file_name);
4412                         goto FINISH_OFF;
4413                 }
4414
4415                 /*  save send passwords to the secure storage */
4416                 err = emcore_add_password_in_key_manager(send_password_file_name, input_outgoing_server_password);
4417                 if (err != EMAIL_ERROR_NONE) {
4418                         EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4419                         goto FINISH_OFF;
4420                 }
4421         }
4422 FINISH_OFF:
4423
4424         EM_DEBUG_FUNC_END("err [%d]", err);
4425         return err;
4426 }
4427
4428 INTERNAL_FUNC int emstorage_update_account(char *multi_user_name, int account_id, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4429 {
4430         EM_DEBUG_FUNC_BEGIN("account_id[%d], account[%p], transaction[%d], err_code[%p]", account_id, account_tbl, transaction, err_code);
4431
4432         if (account_id < FIRST_ACCOUNT_ID || !account_tbl) {
4433                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4434                 if (err_code != NULL)
4435                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4436                 return false;
4437         }
4438
4439         int error = EMAIL_ERROR_NONE;
4440         int rc, ret = false;
4441
4442         DB_STMT hStmt = NULL;
4443         char sql_query_string[QUERY_SIZE] = {0, };
4444
4445         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4446
4447         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4448
4449         SNPRINTF(sql_query_string, sizeof(sql_query_string),
4450                 "UPDATE mail_account_tbl SET"
4451                 "  account_name = ?"
4452                 ", logo_icon_path = ?"
4453                 ", user_data = ?"
4454                 ", user_data_length = ?"
4455                 ", account_svc_id = ?"
4456                 ", sync_status = ?"
4457                 ", sync_disabled = ?"
4458                 ", default_mail_slot_size = ?"
4459                 ", roaming_option = ?"
4460                 ", color_label = ?"
4461                 ", user_display_name = ?"
4462                 ", user_email_address = ?"
4463                 ", reply_to_address = ?"
4464                 ", return_address = ?"
4465                 ", incoming_server_type = ?"
4466                 ", incoming_server_address = ?"
4467                 ", incoming_server_port_number = ?"
4468                 ", incoming_server_user_name = ?"
4469                 ", incoming_server_secure_connection = ?"
4470                 ", incoming_server_authentication_method = ?"
4471                 ", retrieval_mode = ?"
4472                 ", keep_mails_on_pop_server_after_download = ?"
4473                 ", check_interval = ?"
4474                 ", auto_download_size = ?"
4475                 ", peak_interval = ?"
4476                 ", peak_days = ?"
4477                 ", peak_start_time = ?"
4478                 ", peak_end_time = ?"
4479                 ", outgoing_server_type = ?"
4480                 ", outgoing_server_address = ?"
4481                 ", outgoing_server_port_number = ?"
4482                 ", outgoing_server_user_name = ?"
4483                 ", outgoing_server_secure_connection = ?"
4484                 ", outgoing_server_need_authentication = ?"
4485                 ", outgoing_server_use_same_authenticator = ?"
4486                 ", priority = ?"
4487                 ", keep_local_copy = ?"
4488                 ", req_delivery_receipt = ?"
4489                 ", req_read_receipt = ?"
4490                 ", download_limit = ?"
4491                 ", block_address = ?"
4492                 ", block_subject = ?"
4493                 ", display_name_from = ?"
4494                 ", reply_with_body = ?"
4495                 ", forward_with_files = ?"
4496                 ", add_myname_card = ?"
4497                 ", add_signature = ?"
4498                 ", signature = ?"
4499                 ", add_my_address_to_bcc = ?"
4500                 ", notification_status = ?"
4501                 ", vibrate_status = ?"
4502                 ", display_content_status = ?"
4503                 ", default_ringtone_status = ?"
4504                 ", alert_ringtone_path = ?"
4505                 ", auto_resend_times = ?"
4506                 ", outgoing_server_size_limit = ?"
4507                 ", wifi_auto_download = ?"
4508                 ", pop_before_smtp = ?"
4509                 ", incoming_server_requires_apop = ?"
4510                 ", smime_type = ?"
4511                 ", certificate_path = ?"
4512                 ", cipher_type = ?"
4513                 ", digest_type = ?"
4514                 ", user_name = ?"
4515                 " WHERE account_id = ?");
4516
4517         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4518         EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
4519         EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
4520         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4521                 ("sqlite3_prepare fail:(%d) %s", rc, sqlite3_errmsg(local_db_handle)));
4522
4523         int i = 0;
4524
4525         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4526         _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4527         _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4528         _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4529         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4530         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4531         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4532         _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4533         _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
4534         _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
4535         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4536         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4537         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4538         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4539         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4540         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4541         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4542         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4543         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4544         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
4545         _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
4546         _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
4547         _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
4548         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
4549         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
4550         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
4551         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
4552         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
4553         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
4554         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4555         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
4556         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
4557         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
4558         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
4559         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
4560         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
4561         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
4562         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
4563         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
4564         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
4565         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
4566         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
4567         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4568         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
4569         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
4570         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
4571         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
4572         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
4573         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
4574         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
4575         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
4576         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
4577         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
4578         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4579         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
4580         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
4581         _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
4582         _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
4583         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
4584         _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
4585         _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4586         _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
4587         _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
4588         _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4589         _bind_stmt_field_data_int(hStmt, i++, account_id);
4590
4591         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4592         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
4593                 ("sqlite3_step fail:%d", rc));
4594         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4595                 ("sqlite3_step fail:%d", rc));
4596
4597         /*  validate account existence */
4598         rc = sqlite3_changes(local_db_handle);
4599         if (rc == 0) {
4600                 EM_DEBUG_EXCEPTION("no matched account found...");
4601                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4602                 goto FINISH_OFF;
4603         }
4604
4605         if (account_tbl->incoming_server_password || account_tbl->outgoing_server_password) {
4606                 if ((error = emstorage_update_account_password(multi_user_name, account_id, account_tbl->incoming_server_password, account_tbl->outgoing_server_password)) != EMAIL_ERROR_NONE) {
4607                         EM_DEBUG_EXCEPTION("emstorage_update_account_password failed [%d]", error);
4608                         goto FINISH_OFF;
4609                 }
4610         }
4611
4612         ret = true;
4613
4614 FINISH_OFF:
4615         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4616
4617         if (hStmt != NULL) {
4618                 rc = sqlite3_finalize(hStmt);
4619                 if (rc != SQLITE_OK) {
4620                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4621                         error = EMAIL_ERROR_DB_FAILURE;
4622                 }
4623         }
4624
4625         if (error == EMAIL_ERROR_NONE) {
4626                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_tbl->account_id, 0, NULL, 0))
4627                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_ACCOUNT_UPDATE] : Notification Failed >>> ");
4628         }
4629
4630         if (err_code != NULL)
4631                 *err_code = error;
4632
4633         EM_DEBUG_FUNC_END("ret [%d]", ret);
4634         return ret;
4635 }
4636
4637 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)
4638 {
4639         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], field_name[%s], value[%d], transaction[%d]", account_id, field_name, value, transaction);
4640         int error = EMAIL_ERROR_NONE;
4641         int result = 0;
4642         char sql_query_string[QUERY_SIZE] = {0, };
4643         sqlite3 *local_db_handle = NULL;
4644
4645         if (!account_id  || !field_name) {
4646                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4647                 return EMAIL_ERROR_INVALID_PARAM;
4648         }
4649
4650         local_db_handle = emstorage_get_db_connection(multi_user_name);
4651
4652         /* Write query string */
4653         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_account_tbl SET %s = %d WHERE account_id = %d", field_name, value, account_id);
4654
4655         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4656
4657         /* Execute query */
4658         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4659
4660         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4661         if (error != EMAIL_ERROR_NONE) {
4662                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4663                 goto FINISH_OFF;
4664         }
4665
4666         if (sqlite3_changes(local_db_handle) == 0)
4667                 EM_DEBUG_LOG("no mail matched...");
4668
4669
4670 FINISH_OFF:
4671         result = (error == EMAIL_ERROR_NONE) ? true : false;
4672         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, error);
4673
4674         if (error == EMAIL_ERROR_NONE) {
4675                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_id, 0, field_name, value))
4676                         EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_ACCOUNT_UPDATE [%s,%d]", field_name, value);
4677         }
4678         EM_DEBUG_FUNC_END("error [%d]", error);
4679         return error;
4680 }
4681
4682 INTERNAL_FUNC int emstorage_get_sync_status_of_account(char *multi_user_name, int account_id, int *result_sync_status, int *err_code)
4683 {
4684         EM_DEBUG_FUNC_BEGIN("account_id[%d], result_sync_status [%p], err_code[%p]", account_id, result_sync_status, err_code);
4685
4686         if (!result_sync_status) {
4687                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4688                 if (err_code != NULL)
4689                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4690                 return false;
4691         }
4692
4693         int error = EMAIL_ERROR_NONE, rc, ret = false, sync_status, count, i, col_index;
4694         char sql_query_string[QUERY_SIZE] = {0, };
4695         char **result = NULL;
4696         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4697
4698         if (account_id)
4699                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl WHERE account_id = %d", account_id);
4700         else
4701                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl");
4702
4703         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
4704         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
4705                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4706
4707         if (!count) {
4708                 EM_DEBUG_EXCEPTION("no matched account found...");
4709                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4710                 goto FINISH_OFF;
4711         }
4712
4713         col_index = 1;
4714         *result_sync_status = 0;
4715
4716         for (i = 0; i < count; i++) {
4717                 _get_table_field_data_int(result, &sync_status, col_index++);
4718                 *result_sync_status |= sync_status;
4719         }
4720
4721         EM_DEBUG_LOG("sync_status [%d]", sync_status);
4722
4723         sqlite3_free_table(result);
4724
4725         ret = true;
4726
4727 FINISH_OFF:
4728
4729
4730         if (err_code != NULL)
4731                 *err_code = error;
4732
4733         EM_DEBUG_FUNC_END("ret [%d]", ret);
4734         return ret;
4735 }
4736
4737 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)
4738 {
4739         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);
4740
4741         int error = EMAIL_ERROR_NONE, rc, ret = false, set_value = sync_status, result_sync_status;
4742         char sql_query_string[QUERY_SIZE] = {0, };
4743         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4744
4745         if (set_operator != SET_TYPE_SET && account_id) {
4746                 if (!emstorage_get_sync_status_of_account(multi_user_name, account_id, &result_sync_status, &error)) {
4747                         EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", error);
4748                         if (err_code != NULL)
4749                                 *err_code = error;
4750                         return false;
4751                 }
4752                 switch (set_operator) {
4753                         case SET_TYPE_UNION:
4754                                 set_value = result_sync_status | set_value;
4755                                 break;
4756                         case SET_TYPE_MINUS:
4757                                 set_value = result_sync_status & (~set_value);
4758                                 break;
4759                         default:
4760                                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_NOT_SUPPORTED [%d]", set_operator);
4761                                 error = EMAIL_ERROR_NOT_SUPPORTED;
4762                                 break;
4763                 }
4764                 EM_DEBUG_LOG("set_value [%d]", set_value);
4765         }
4766
4767         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4768
4769         if (account_id)
4770                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE account_id = %d", set_value, account_id);
4771         else
4772                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE incoming_server_type <> 5", set_value);
4773
4774         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4775
4776         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4777         if (error != EMAIL_ERROR_NONE) {
4778                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4779                 goto FINISH_OFF;
4780         }
4781
4782         rc = sqlite3_changes(local_db_handle);
4783
4784         if (rc == 0) {
4785                 EM_DEBUG_EXCEPTION("no matched account found...");
4786                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4787                 goto FINISH_OFF;
4788         }
4789
4790         ret = true;
4791
4792 FINISH_OFF:
4793         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4794
4795         if (error == EMAIL_ERROR_NONE) {
4796                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE_SYNC_STATUS, account_id, set_value, NULL, 0))
4797                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_UPDATE_SYNC_STATUS] : Notification failed");
4798         }
4799
4800         if (err_code != NULL)
4801                 *err_code = error;
4802
4803         EM_DEBUG_FUNC_END("ret [%d]", ret);
4804         return ret;
4805 }
4806
4807 INTERNAL_FUNC int emstorage_add_account(char *multi_user_name, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4808 {
4809         EM_DEBUG_FUNC_BEGIN("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4810
4811         if (!account_tbl) {
4812                 EM_DEBUG_EXCEPTION("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4813                 if (err_code != NULL)
4814                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4815                 return false;
4816         }
4817
4818         int row_count = 0;
4819         int i = 0;
4820         int rc = -1, ret = false;
4821         int error = EMAIL_ERROR_NONE;
4822         DB_STMT hStmt = NULL;
4823         char sql_query_string[QUERY_SIZE] = {0, };
4824         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4825         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4826
4827         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4828
4829         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4830
4831         char *sql = "SELECT rowid FROM mail_account_tbl;";
4832         char **result = NULL;
4833
4834         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, &row_count, NULL, NULL), rc);
4835         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
4836                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
4837
4838         if (NULL == result[1]) rc = 1;
4839         else {
4840                 for (i = 1; i <= row_count; i++) {
4841                         if (i != atoi(result[i])) {
4842                                 break;
4843                         }
4844                 }
4845
4846                 rc = i;
4847         }
4848         sqlite3_free_table(result);
4849         result = NULL;
4850
4851         if (rc < 0 && rc > EMAIL_ACCOUNT_MAX) {
4852                 EM_DEBUG_EXCEPTION("OVERFLOWED THE MAX ACCOUNT");
4853                 error = EMAIL_ERROR_ACCOUNT_MAX_COUNT;
4854                 goto FINISH_OFF;
4855         }
4856
4857         account_tbl->account_id = rc;
4858
4859         if ((error = _get_password_file_name(multi_user_name, account_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4860                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4861                 goto FINISH_OFF;
4862         }
4863
4864         EM_DEBUG_LOG(" >>>> ACCOUNT_ID [ %d ] ", account_tbl->account_id);
4865         SNPRINTF(sql_query_string, sizeof(sql_query_string),
4866                 "INSERT INTO mail_account_tbl VALUES "
4867                 "(        "
4868                 "    ? "  /*   account_id */
4869                 "  , ? "  /*   account_name */
4870                 "  , ? "  /*   logo_icon_path */
4871                 "  , ? "  /*   user_data */
4872                 "  , ? "  /*   user_data_length */
4873                 "  , ? "  /*   account_svc_id */
4874                 "  , ? "  /*   sync_status */
4875                 "  , ? "  /*   sync_disabled */
4876                 "  , ? "  /*   default_mail_slot_size */
4877                 "  , ? "  /*   roaming_option */
4878                 "  , ? "  /*   color_label */
4879                 "  , ? "  /*   user_display_name */
4880                 "  , ? "  /*   user_email_address */
4881                 "  , ? "  /*   reply_to_address */
4882                 "  , ? "  /*   return_address */
4883                 "  , ? "  /*   incoming_server_type */
4884                 "  , ? "  /*   incoming_server_address */
4885                 "  , ? "  /*   incoming_server_port_number */
4886                 "  , ? "  /*   incoming_server_user_name */
4887                 "  , ? "  /*   incoming_server_password */
4888                 "  , ? "  /*   incoming_server_secure_connection */
4889                 "  , ? "  /*   incoming_server_authentication_method */
4890                 "  , ? "  /*   retrieval_mode */
4891                 "  , ? "  /*   keep_mails_on_pop_server_after_download */
4892                 "  , ? "  /*   check_interval */
4893                 "  , ? "  /*   auto_download_size */
4894                 "  , ? "  /*   peak_interval */
4895                 "  , ? "  /*   peak_days */
4896                 "  , ? "  /*   peak_start_time */
4897                 "  , ? "  /*   peak_end_time */
4898                 "  , ? "  /*   outgoing_server_type */
4899                 "  , ? "  /*   outgoing_server_address */
4900                 "  , ? "  /*   outgoing_server_port_number */
4901                 "  , ? "  /*   outgoing_server_user_name */
4902                 "  , ? "  /*   outgoing_server_password */
4903                 "  , ? "  /*   outgoing_server_secure_connection */
4904                 "  , ? "  /*   outgoing_server_need_authentication */
4905                 "  , ? "  /*   outgoing_server_use_same_authenticator */
4906                 "  , ? "  /*   priority */
4907                 "  , ? "  /*   keep_local_copy */
4908                 "  , ? "  /*   req_delivery_receipt */
4909                 "  , ? "  /*   req_read_receipt */
4910                 "  , ? "  /*   download_limit */
4911                 "  , ? "  /*   block_address */
4912                 "  , ? "  /*   block_subject */
4913                 "  , ? "  /*   display_name_from */
4914                 "  , ? "  /*   reply_with_body */
4915                 "  , ? "  /*   forward_with_files */
4916                 "  , ? "  /*   add_myname_card */
4917                 "  , ? "  /*   add_signature */
4918                 "  , ? "  /*   signature */
4919                 "  , ? "  /*   add_my_address_to_bcc */
4920                 "  , ? "  /*   auto_resend_times */
4921                 "  , ? "  /*   outgoing_server_size_limit */
4922                 "  , ? "  /*   wifi_auto_download */
4923                 "  , ? "  /*   pop_before_smtp */
4924                 "  , ? "  /*   incoming_server_requires_apop */
4925                 "  , ? "  /*   smime_type */
4926                 "  , ? "  /*   certificate_path */
4927                 "  , ? "  /*   cipher_type */
4928                 "  , ? "  /*   digest_type */
4929                 "  , ? "  /*   notification_status */
4930                 "  , ? "  /*   vibrate_status */
4931                 "  , ? "  /*   display_content_status */
4932                 "  , ? "  /*   default_ringtone_status */
4933                 "  , ? "  /*   alert_ringtone_path */
4934                 "  , ? "  /*   user_name */
4935                 ") ");
4936
4937         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4938         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4939                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4940
4941         i = 0;
4942
4943         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_id);
4944         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4945         _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4946         _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4947         _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4948         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4949         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4950         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4951         _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4952         _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
4953         _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
4954         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4955         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4956         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4957         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4958         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4959         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4960         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4961         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4962         _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
4963         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4964         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
4965         _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
4966         _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
4967         _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
4968         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
4969         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
4970         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
4971         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
4972         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
4973         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
4974         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4975         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
4976         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
4977         _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
4978         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
4979         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
4980         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
4981         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
4982         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
4983         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
4984         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
4985         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
4986         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
4987         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
4988         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4989         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
4990         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
4991         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
4992         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
4993         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
4994         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
4995         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
4996         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
4997         _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
4998         _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
4999         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
5000         _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
5001         _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
5002         _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
5003         _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
5004         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
5005         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
5006         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
5007         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
5008         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5009         _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5010
5011         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5012
5013         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
5014                 ("sqlite3_step fail:%d", rc));
5015         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5016                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
5017
5018
5019         /*  save passwords to the secure storage */
5020         EM_DEBUG_LOG_SEC("save to the secure storage : recv_file[%s], send_file[%s]", recv_password_file_name, send_password_file_name);
5021         error = emcore_add_password_in_key_manager(recv_password_file_name, account_tbl->incoming_server_password);
5022         if (error != EMAIL_ERROR_NONE) {
5023                 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
5024                 goto FINISH_OFF;
5025         }
5026
5027         error = emcore_add_password_in_key_manager(send_password_file_name, account_tbl->outgoing_server_password);
5028         if (error != EMAIL_ERROR_NONE) {
5029                 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
5030                 goto FINISH_OFF;
5031         }
5032
5033         ret = true;
5034
5035 FINISH_OFF:
5036         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5037
5038         if (hStmt != NULL) {
5039                 rc = sqlite3_finalize(hStmt);
5040                 if (rc != SQLITE_OK) {
5041                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5042                         error = EMAIL_ERROR_DB_FAILURE;
5043                 }
5044         }
5045
5046         if (error == EMAIL_ERROR_NONE) {
5047                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_ADD, account_tbl->account_id, 0, NULL, 0))
5048                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_ADD] : Notification failed");
5049         }
5050
5051         if (err_code != NULL)
5052                 *err_code = error;
5053
5054         EM_DEBUG_FUNC_END("ret [%d]", ret);
5055         return ret;
5056 }
5057
5058 INTERNAL_FUNC int emstorage_delete_account(char *multi_user_name, int account_id, int transaction, int *err_code)
5059 {
5060         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
5061
5062         if (account_id < FIRST_ACCOUNT_ID) {
5063                 EM_DEBUG_EXCEPTION(" account_id[%d]", account_id);
5064
5065                 if (err_code != NULL)
5066                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5067                 return false;
5068         }
5069
5070         int rc = -1, ret = false;
5071         int error = EMAIL_ERROR_NONE;
5072
5073         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5074
5075         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5076
5077         /*  TODO : delete password files - file names can be obtained from db or a rule that makes a name */
5078         char sql_query_string[QUERY_SIZE] = {0, };
5079         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5080         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5081
5082         /*  get password file name */
5083         if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
5084                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
5085                 goto FINISH_OFF;
5086         }
5087
5088         /*  delete from db */
5089         memset(sql_query_string, 0x00, sizeof(sql_query_string));
5090         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_account_tbl WHERE account_id = %d", account_id);
5091         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5092         if (error != EMAIL_ERROR_NONE) {
5093                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5094                         goto FINISH_OFF;
5095         }
5096
5097         /*  validate account existence */
5098         rc = sqlite3_changes(local_db_handle);
5099         if (rc == 0) {
5100                 EM_DEBUG_EXCEPTION(" no matched account found...");
5101                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
5102                 goto FINISH_OFF;
5103         }
5104
5105                 /*  delete from secure storage */
5106         error = emcore_remove_password_in_key_manager(recv_password_file_name);
5107         if (error != EMAIL_ERROR_NONE) {
5108                 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5109                 goto FINISH_OFF;
5110         }
5111
5112         error = emcore_remove_password_in_key_manager(send_password_file_name);
5113         if (error != EMAIL_ERROR_NONE) {
5114                 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5115                 goto FINISH_OFF;
5116         }
5117
5118         ret = true;
5119
5120 FINISH_OFF:
5121         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5122
5123         if (err_code != NULL)
5124                 *err_code = error;
5125
5126         EM_DEBUG_FUNC_END("ret [%d]", ret);
5127         return ret;
5128 }
5129
5130
5131 INTERNAL_FUNC int emstorage_free_account(emstorage_account_tbl_t** account_list, int count, int *err_code)
5132 {
5133         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%d], err_code[%p]", account_list, count, err_code);
5134
5135         int ret = false;
5136         int error = EMAIL_ERROR_NONE;
5137
5138         if (count > 0) {
5139                 if (!account_list || !*account_list) {
5140                         EM_DEBUG_EXCEPTION("account_list[%p], count[%d]", account_list, count);
5141                         error = EMAIL_ERROR_INVALID_PARAM;
5142                         goto FINISH_OFF;
5143                 }
5144
5145                 emstorage_account_tbl_t* p = *account_list;
5146                 int i = 0;
5147
5148                 for (; i < count; i++) {
5149                         EM_SAFE_FREE(p[i].account_name);
5150                         EM_SAFE_FREE(p[i].incoming_server_address);
5151                         EM_SAFE_FREE(p[i].user_email_address);
5152                         EM_SAFE_FREE(p[i].user_data);
5153                         EM_SAFE_FREE(p[i].incoming_server_user_name);
5154                         EM_SAFE_FREE(p[i].incoming_server_password);
5155                         EM_SAFE_FREE(p[i].outgoing_server_address);
5156                         EM_SAFE_FREE(p[i].outgoing_server_user_name);
5157                         EM_SAFE_FREE(p[i].outgoing_server_password);
5158                         EM_SAFE_FREE(p[i].user_display_name);
5159                         EM_SAFE_FREE(p[i].reply_to_address);
5160                         EM_SAFE_FREE(p[i].return_address);
5161                         EM_SAFE_FREE(p[i].logo_icon_path);
5162                         EM_SAFE_FREE(p[i].options.display_name_from);
5163                         EM_SAFE_FREE(p[i].options.signature);
5164                         EM_SAFE_FREE(p[i].options.alert_ringtone_path);
5165                         EM_SAFE_FREE(p[i].certificate_path);
5166                         EM_SAFE_FREE(p[i].user_name);
5167                 }
5168
5169                 EM_SAFE_FREE(p);
5170                 *account_list = NULL;
5171         }
5172
5173         ret = true;
5174
5175 FINISH_OFF:
5176         if (err_code != NULL)
5177                 *err_code = error;
5178
5179         EM_DEBUG_FUNC_END("ret [%d]", ret);
5180         return ret;
5181 }
5182
5183 INTERNAL_FUNC int emstorage_get_mailbox_count(char *multi_user_name, int account_id, int local_yn, int *count, int transaction, int *err_code)
5184 {
5185         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);
5186
5187         if ((account_id < FIRST_ACCOUNT_ID) || (count == NULL)) {
5188                 EM_DEBUG_EXCEPTION(" account_list[%d], local_yn[%d], count[%p]", account_id, local_yn, count);
5189
5190                 if (err_code != NULL)
5191                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5192                 return false;
5193         }
5194
5195         int rc = -1, ret = false;
5196         int error = EMAIL_ERROR_NONE;
5197         char sql_query_string[QUERY_SIZE] = {0, };
5198
5199         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5200         EMSTORAGE_START_READ_TRANSACTION(transaction);
5201
5202         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);
5203
5204         char **result;
5205
5206         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5207         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
5208                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5209
5210         *count = atoi(result[1]);
5211         sqlite3_free_table(result);
5212
5213
5214         ret = true;
5215
5216 FINISH_OFF:
5217
5218         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5219
5220         if (err_code != NULL)
5221                 *err_code = error;
5222
5223         EM_DEBUG_FUNC_END("ret [%d]", ret);
5224         return ret;
5225 }
5226
5227 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)
5228 {
5229         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);
5230
5231         if (!select_num || !mailbox_list) {
5232                 EM_DEBUG_EXCEPTION("Invalid parameters");
5233
5234                 if (err_code != NULL)
5235                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5236
5237                 return false;
5238         }
5239
5240         int ret = false;
5241         int error = EMAIL_ERROR_NONE;
5242         char conditional_clause_string[QUERY_SIZE] = {0, };
5243         char ordering_clause_string[QUERY_SIZE] = {0, };
5244
5245         if (account_id == ALL_ACCOUNT) {
5246                 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5247                         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);
5248         } else {
5249                 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d  ", account_id);
5250                 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5251                         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);
5252         }
5253
5254         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5255
5256         switch (sort_type) {
5257                 case EMAIL_MAILBOX_SORT_BY_NAME_ASC:
5258                         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name ASC");
5259                         break;
5260
5261                 case EMAIL_MAILBOX_SORT_BY_NAME_DSC:
5262                         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name DESC");
5263                         break;
5264
5265                 case EMAIL_MAILBOX_SORT_BY_TYPE_ASC:
5266                         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type ASC");
5267                         break;
5268
5269                 case EMAIL_MAILBOX_SORT_BY_TYPE_DSC:
5270                         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type DEC");
5271                         break;
5272         }
5273
5274         EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5275
5276         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5277                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5278                 goto FINISH_OFF;
5279         }
5280
5281         ret = true;
5282
5283 FINISH_OFF:
5284
5285         if (err_code != NULL)
5286                 *err_code = error;
5287
5288         EM_DEBUG_FUNC_END("ret [%d]", ret);
5289         return ret;
5290 }
5291
5292 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)
5293 {
5294         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);
5295
5296         int ret = false;
5297         int error = EMAIL_ERROR_NONE;
5298         int where_clause_count = 0;
5299         char conditional_clause_string[QUERY_SIZE] = {0, };
5300         char ordering_clause_string[QUERY_SIZE] = {0, };
5301
5302         if (!select_num || !mailbox_list) {
5303                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5304                 error = EMAIL_ERROR_INVALID_PARAM;
5305                 goto FINISH_OFF;
5306         }
5307
5308         if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL) {
5309                 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);
5310                 where_clause_count++;
5311         }
5312
5313         if (account_id > 0) {
5314                 if (where_clause_count == 0) {
5315                         SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " WHERE ");
5316                         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);
5317                 } else
5318                         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);
5319         }
5320
5321         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY CASE WHEN MBT.mailbox_name"
5322                                                                                                  " GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\'"
5323                                                                                                  " THEN 2 ELSE 1 END ASC,"
5324                                                                                                  " MBT.mailbox_name COLLATE CONVERTUTF8 ASC ");
5325         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5326         EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5327
5328         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 1, 1, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5329                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5330                 goto FINISH_OFF;
5331         }
5332
5333         ret = true;
5334
5335 FINISH_OFF:
5336
5337         if (err_code != NULL)
5338                 *err_code = error;
5339
5340         EM_DEBUG_FUNC_END("ret [%d]", ret);
5341         return ret;
5342 }
5343
5344 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)
5345 {
5346         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);
5347
5348         int ret = false;
5349         int error = EMAIL_ERROR_NONE;
5350         char conditional_clause_string[QUERY_SIZE] = {0, };
5351
5352         if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list || !parent_mailbox_name) {
5353                 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);
5354                 if (err_code != NULL)
5355                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5356                 return false;
5357         }
5358
5359         sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d  AND UPPER(mailbox_name) LIKE UPPER('%q%%')", account_id, parent_mailbox_name);
5360         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5361
5362         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) {
5363                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5364                 goto FINISH_OFF;
5365         }
5366
5367         ret = true;
5368
5369 FINISH_OFF:
5370
5371         if (err_code != NULL)
5372                 *err_code = error;
5373
5374         EM_DEBUG_FUNC_END("ret [%d]", ret);
5375         return ret;
5376 }
5377
5378 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)
5379 {
5380         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);
5381         if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list) {
5382                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5383
5384                 if (err_code != NULL)
5385                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5386
5387                 return false;
5388         }
5389
5390         int ret = false;
5391         int error = EMAIL_ERROR_NONE;
5392         char conditional_clause_string[QUERY_SIZE] = {0, };
5393
5394         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND modifiable_yn = 0", account_id);
5395         EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
5396
5397         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, " ORDER BY mailbox_name", 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5398                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5399                 goto FINISH_OFF;
5400         }
5401
5402         ret = true;
5403
5404 FINISH_OFF:
5405
5406         if (err_code != NULL)
5407                 *err_code = error;
5408
5409         EM_DEBUG_FUNC_END("ret [%d]", ret);
5410         return ret;
5411 }
5412
5413 INTERNAL_FUNC int emstorage_stamp_last_sync_time_of_mailbox(char *multi_user_name, int input_mailbox_id, int input_transaction)
5414 {
5415         EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_transaction [%d]", input_mailbox_id, input_transaction);
5416
5417         int      result_code = false;
5418         int      error = EMAIL_ERROR_NONE;
5419         time_t   current_time = 0;
5420         char     sql_query_string[QUERY_SIZE] = {0, };
5421         sqlite3 *local_db_handle = NULL;
5422
5423         if (!input_mailbox_id) {
5424                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5425                 return EMAIL_ERROR_INVALID_PARAM;
5426         }
5427
5428         time(&current_time);
5429
5430         local_db_handle = emstorage_get_db_connection(multi_user_name);
5431     EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
5432
5433     SNPRINTF(sql_query_string, sizeof(sql_query_string),
5434         "UPDATE mail_box_tbl SET"
5435                 " last_sync_time = %d"
5436                 " WHERE mailbox_id = %d"
5437                 , (int)current_time
5438                 , input_mailbox_id);
5439
5440         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
5441         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5442         if (error != EMAIL_ERROR_NONE) {
5443                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5444                         goto FINISH_OFF;
5445         }
5446
5447 FINISH_OFF:
5448
5449         if (error == EMAIL_ERROR_NONE)
5450                 result_code = true;
5451
5452         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, result_code, error);
5453
5454         EM_DEBUG_FUNC_END("error [%d]", error);
5455         return error;
5456 }
5457
5458 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)
5459 {
5460         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);
5461         EM_PROFILE_BEGIN(profile_emstorage_get_mailbox_by_name);
5462
5463         if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !result_mailbox) {
5464                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], mailbox_name[%s], result_mailbox[%p]", account_id, local_yn, mailbox_name, result_mailbox);
5465
5466                 if (err_code != NULL)
5467                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5468                 return false;
5469         }
5470
5471         int ret = false;
5472         int error = EMAIL_ERROR_NONE;
5473         int result_count = 0;
5474         char conditional_clause_string[QUERY_SIZE] = {0, };
5475
5476         if (strcmp(mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
5477                 if (!(*result_mailbox = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t)))) {
5478                         EM_DEBUG_EXCEPTION("malloc failed...");
5479                         error = EMAIL_ERROR_OUT_OF_MEMORY;
5480                         goto FINISH_OFF;
5481                 }
5482
5483                 (*result_mailbox)->mailbox_id                 = 0;
5484                 (*result_mailbox)->account_id                 = account_id;
5485                 (*result_mailbox)->local_yn                   = 1;
5486                 (*result_mailbox)->mailbox_name               = EM_SAFE_STRDUP(mailbox_name);
5487                 (*result_mailbox)->mailbox_type               = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
5488                 (*result_mailbox)->alias                      = EM_SAFE_STRDUP(mailbox_name);
5489                 (*result_mailbox)->deleted_flag               = 0;
5490                 (*result_mailbox)->modifiable_yn              = 1;
5491                 (*result_mailbox)->total_mail_count_on_server = 1;
5492                 (*result_mailbox)->has_archived_mails         = 0;
5493                 (*result_mailbox)->mail_slot_size             = 0x0FFFFFFF;
5494                 (*result_mailbox)->no_select                  = 0;
5495         } else {
5496                 if (local_yn == -1)
5497                         sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d AND mailbox_name = '%q'", account_id, mailbox_name);
5498                 else
5499                         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);
5500
5501                 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5502
5503                 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5504                         EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5505                         goto FINISH_OFF;
5506                 }
5507         }
5508
5509         ret = true;
5510
5511 FINISH_OFF:
5512
5513         if (err_code != NULL)
5514                 *err_code = error;
5515
5516         EM_PROFILE_END(profile_emstorage_get_mailbox_by_name);
5517         EM_DEBUG_FUNC_END("ret [%d]", ret);
5518         return ret;
5519 }
5520
5521 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)
5522 {
5523         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);
5524
5525         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_USER_DEFINED) || !output_mailbox) {
5526                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_type[%d], output_mailbox[%p]", account_id, mailbox_type, output_mailbox);
5527                 if (err_code != NULL)
5528                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5529                 return false;
5530         }
5531
5532         int ret = false;
5533         int error = EMAIL_ERROR_NONE;
5534         int result_count = 0;
5535         emstorage_mailbox_tbl_t *result_mailbox = NULL;
5536         char conditional_clause_string[QUERY_SIZE] = {0,};
5537
5538
5539         SNPRINTF(conditional_clause_string, QUERY_SIZE, "WHERE account_id = %d AND mailbox_type = %d ", account_id, mailbox_type);
5540
5541         EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5542
5543         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", true, false, &result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5544                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl error [%d]", error);
5545                 goto FINISH_OFF;
5546         }
5547
5548         ret = true;
5549
5550 FINISH_OFF:
5551         if (ret == true)
5552                 *output_mailbox = result_mailbox;
5553
5554         if (err_code != NULL)
5555                 *err_code = error;
5556
5557         EM_DEBUG_FUNC_END("ret [%d]", ret);
5558         return ret;
5559 }
5560
5561 INTERNAL_FUNC int emstorage_get_mailbox_by_id(char *multi_user_name, int input_mailbox_id, emstorage_mailbox_tbl_t** output_mailbox)
5562 {
5563         EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_mailbox[%p]", input_mailbox_id, output_mailbox);
5564
5565         if (input_mailbox_id <= 0 || !output_mailbox) {
5566                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: input_mailbox_id[%d] output_mailbox[%p]", input_mailbox_id, output_mailbox);
5567                 return  EMAIL_ERROR_INVALID_PARAM;
5568         }
5569
5570         int  ret = EMAIL_ERROR_NONE;
5571         int  result_count = 0;
5572         char conditional_clause_string[QUERY_SIZE] = {0, };
5573
5574         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE MBT.mailbox_id = %d", input_mailbox_id);
5575
5576         EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5577
5578         if ((ret = emstorage_query_mailbox_tbl(multi_user_name,
5579                                                                                         conditional_clause_string,
5580                                                                                         "", true, false, output_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5581                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", ret);
5582                 goto FINISH_OFF;
5583         }
5584
5585 FINISH_OFF:
5586
5587         EM_DEBUG_FUNC_END("ret [%d]", ret);
5588         return ret;
5589 }
5590
5591 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)
5592 {
5593         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);
5594
5595         if (account_id < 0 || !keyword || !result_mailbox) {
5596                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], keyword[%s], result_mailbox[%p]", account_id, keyword, result_mailbox);
5597
5598                 if (err_code != NULL)
5599                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5600                 return false;
5601         }
5602
5603         int ret = false;
5604         int error = EMAIL_ERROR_NONE;
5605         char conditional_clause_string[QUERY_SIZE] = {0, };
5606
5607         if (account_id == 0)
5608                 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5609                                 "WHERE alias LIKE \'%%%q%%\'", keyword);
5610         else if (account_id > 0)
5611                 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5612                                 "WHERE account_id = %d AND alias LIKE \'%%%q%%\'", account_id, keyword);
5613
5614         EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5615
5616         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, result_count)) != EMAIL_ERROR_NONE) {
5617                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5618                 goto FINISH_OFF;
5619         }
5620
5621         ret = true;
5622
5623 FINISH_OFF:
5624
5625         if (err_code != NULL)
5626                 *err_code = error;
5627
5628         EM_DEBUG_FUNC_END("ret [%d]", ret);
5629         return ret;
5630 }
5631
5632 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)
5633 {
5634         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);
5635         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_id) {
5636                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_id[%p]", account_id, mailbox_type, mailbox_id);
5637                 if (err_code != NULL)
5638                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5639                 return false;
5640         }
5641
5642         int rc, ret = false;
5643         int error = EMAIL_ERROR_NONE;
5644         emstorage_account_tbl_t* account = NULL;
5645         DB_STMT hStmt = NULL;
5646         char sql_query_string[QUERY_SIZE] = {0, };
5647
5648         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5649         EMSTORAGE_START_READ_TRANSACTION(transaction);
5650
5651         /*  Check whether the account exists. */
5652         if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
5653                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5654                 goto FINISH_OFF;
5655         }
5656
5657         if (account)
5658                 emstorage_free_account(&account, 1, NULL);
5659
5660         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);
5661
5662         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5663
5664
5665         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5666
5667         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5668                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5669
5670
5671         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5672         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5673                 ("sqlite3_step fail:%d", rc));
5674
5675         if (rc == SQLITE_DONE) {
5676                 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5677                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5678                 goto FINISH_OFF;
5679         }
5680
5681         _get_stmt_field_data_int(hStmt, mailbox_id, 0);
5682
5683         ret = true;
5684
5685 FINISH_OFF:
5686         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5687         if (hStmt != NULL) {
5688                 rc = sqlite3_finalize(hStmt);
5689                 if (rc != SQLITE_OK) {
5690                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5691                         error = EMAIL_ERROR_DB_FAILURE;
5692                 }
5693         }
5694
5695         if (err_code != NULL)
5696                 *err_code = error;
5697
5698         EM_DEBUG_FUNC_END("ret [%d]", ret);
5699         return ret;
5700 }
5701
5702 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)
5703 {
5704         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);
5705         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_name) {
5706                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
5707                 if (err_code != NULL)
5708                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5709                 return false;
5710         }
5711
5712         int rc, ret = false;
5713         int error = EMAIL_ERROR_NONE;
5714         emstorage_account_tbl_t* account = NULL;
5715         DB_STMT hStmt = NULL;
5716         char sql_query_string[QUERY_SIZE] = {0, };
5717
5718         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5719         EMSTORAGE_START_READ_TRANSACTION(transaction);
5720
5721         /*  Check whether the account exists. */
5722         if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
5723                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5724                 goto FINISH_OFF;
5725         }
5726
5727         if (account)
5728                 emstorage_free_account(&account, 1, NULL);
5729
5730         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);
5731
5732         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5733
5734
5735         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5736
5737         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5738                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5739
5740
5741         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5742         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5743                 ("sqlite3_step fail:%d", rc));
5744
5745         if (rc == SQLITE_DONE) {
5746                 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5747                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5748                 goto FINISH_OFF;
5749         }
5750
5751         _get_stmt_field_data_string(hStmt, mailbox_name, 0, 0);
5752
5753         ret = true;
5754
5755 FINISH_OFF:
5756         if (hStmt != NULL) {
5757                 rc = sqlite3_finalize(hStmt);
5758                 if (rc != SQLITE_OK) {
5759                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5760                         error = EMAIL_ERROR_DB_FAILURE;
5761                 }
5762         }
5763
5764         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5765
5766         if (err_code != NULL)
5767                 *err_code = error;
5768
5769         EM_DEBUG_FUNC_END("ret [%d]", ret);
5770         return ret;
5771 }
5772
5773 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)
5774 {
5775         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);
5776         int ret = false;
5777         int error = EMAIL_ERROR_NONE;
5778         char sql_query_string[QUERY_SIZE] = {0, };
5779         char *replaced_mailbox_name = NULL;
5780
5781         if (mailbox_name) {
5782                 if (strstr(mailbox_name, "'")) {
5783                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5784                 } else {
5785                         replaced_mailbox_name = strdup(mailbox_name);
5786                 }
5787         }
5788
5789         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5790
5791         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5792
5793         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5794                 "UPDATE mail_box_tbl SET"
5795                 " modifiable_yn = %d"
5796                 " WHERE account_id = %d"
5797                 " AND local_yn = %d"
5798                 " AND mailbox_name = '%s'"
5799                 , modifiable_yn
5800                 , account_id
5801                 , local_yn
5802                 , replaced_mailbox_name);
5803
5804         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5805         if (error != EMAIL_ERROR_NONE) {
5806                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5807                         goto FINISH_OFF;
5808         }
5809
5810         ret = true;
5811
5812 FINISH_OFF:
5813         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5814
5815         EM_SAFE_FREE(replaced_mailbox_name);
5816
5817         if (err_code != NULL)
5818                 *err_code = error;
5819
5820         EM_DEBUG_FUNC_END("ret [%d]", ret);
5821         return ret;
5822
5823 }
5824
5825 INTERNAL_FUNC int emstorage_update_mailbox_total_count(char *multi_user_name,
5826                                                                                                                 int account_id,
5827                                                                                                                 int input_mailbox_id,
5828                                                                                                                 int total_count_on_server,
5829                                                                                                                 int transaction,
5830                                                                                                                 int *err_code)
5831 {
5832         EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], total_count_on_server[%d], "
5833                                                 "transaction[%d], err_code[%p]",
5834                                                 account_id, input_mailbox_id, total_count_on_server,  transaction, err_code);
5835
5836         int ret = false;
5837         int error = EMAIL_ERROR_NONE;
5838         char sql_query_string[QUERY_SIZE] = {0, };
5839
5840         if (account_id <= 0 || input_mailbox_id <= 0) {
5841                 EM_DEBUG_EXCEPTION("account_id[%d], input_mailbox_id[%d]", account_id, input_mailbox_id);
5842                 if (err_code != NULL)
5843                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5844                 EM_DEBUG_FUNC_END("ret [%d]", ret);
5845                 return false;
5846         }
5847
5848         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5849
5850         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5851
5852         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5853                 "UPDATE mail_box_tbl SET"
5854                 " total_mail_count_on_server = %d"
5855                 " WHERE account_id = %d"
5856                 " AND mailbox_id = %d"
5857                 , total_count_on_server
5858                 , account_id
5859                 , input_mailbox_id);
5860
5861         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
5862         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5863         if (error != EMAIL_ERROR_NONE) {
5864                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5865                         goto FINISH_OFF;
5866         }
5867
5868         ret = true;
5869
5870 FINISH_OFF:
5871
5872         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5873
5874         if (err_code != NULL)
5875                 *err_code = error;
5876
5877         EM_DEBUG_FUNC_END("ret [%d]", ret);
5878         return ret;
5879
5880 }
5881
5882 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)
5883 {
5884         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);
5885
5886         if (account_id < FIRST_ACCOUNT_ID || input_mailbox_id <= 0 || !result_mailbox) {
5887                 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);
5888
5889                 if (err_code != NULL)
5890                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5891                 EM_DEBUG_FUNC_END("ret [%d]", EMAIL_ERROR_INVALID_PARAM);
5892                 return false;
5893         }
5894
5895         int rc, ret = false;
5896         int error = EMAIL_ERROR_NONE;
5897         char sql_query_string[QUERY_SIZE] = {0, };
5898         DB_STMT hStmt = NULL;
5899         int i = 0;
5900
5901         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5902         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5903
5904         if (local_yn != -1) {
5905                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5906                         "UPDATE mail_box_tbl SET"
5907                         "  mailbox_id = ?"
5908                         ", mailbox_name = ?"
5909                         ", mailbox_type = ?"
5910                         ", alias = ?"
5911                         ", deleted_flag = ?"
5912                         ", modifiable_yn= ?"
5913                         ", mail_slot_size= ?"
5914                         ", total_mail_count_on_server = ?"
5915                         " WHERE account_id = %d"
5916                         " AND local_yn = %d"
5917                         " AND mailbox_id = '%d'"
5918                         , account_id
5919                         , local_yn
5920                         , input_mailbox_id);
5921         } else {
5922                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5923                         "UPDATE mail_box_tbl SET"
5924                         "  mailbox_id = ?"
5925                         ", mailbox_name = ?"
5926                         ", mailbox_type = ?"
5927                         ", alias = ?"
5928                         ", deleted_flag = ?"
5929                         ", modifiable_yn= ?"
5930                         ", mail_slot_size= ?"
5931                         ", total_mail_count_on_server = ?"
5932                         " WHERE account_id = %d"
5933                         " AND mailbox_id = '%d'"
5934                         , account_id
5935                         , input_mailbox_id);
5936         }
5937
5938
5939
5940         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5941         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5942                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5943
5944         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_id);
5945         _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);
5946         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_type);
5947         _bind_stmt_field_data_string(hStmt, i++, (char *)result_mailbox->alias ? result_mailbox->alias : "", 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
5948         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->deleted_flag);
5949         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->modifiable_yn);
5950         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mail_slot_size);
5951         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->total_mail_count_on_server);
5952
5953
5954         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5955         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
5956                 ("sqlite3_step fail:%d", rc));
5957         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5958                 ("sqlite3_step fail:%d", rc));
5959
5960         ret = true;
5961
5962 FINISH_OFF:
5963         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5964
5965         if (hStmt != NULL) {
5966                 rc = sqlite3_finalize(hStmt);
5967                 if (rc != SQLITE_OK) {
5968                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5969                         error = EMAIL_ERROR_DB_FAILURE;
5970                 }
5971         }
5972
5973         if (err_code != NULL)
5974                 *err_code = error;
5975
5976         EM_DEBUG_FUNC_END("ret [%d]", ret);
5977         return ret;
5978 }
5979
5980 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)
5981 {
5982         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);
5983
5984         if (account_id < FIRST_ACCOUNT_ID) {
5985                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
5986
5987                 if (err_code != NULL)
5988                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5989                 return false;
5990         }
5991
5992         int rc, ret = false;
5993         int error = EMAIL_ERROR_NONE;
5994         char sql_query_string[QUERY_SIZE] = {0, };
5995
5996         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5997         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5998
5999         EM_DEBUG_LOG("emstorage_update_mailbox_type");
6000
6001         DB_STMT hStmt_box_tbl = NULL;
6002         DB_STMT hStmt_mail_tbl = NULL;
6003         int i = 0;
6004
6005         /*  Update mail_box_tbl */
6006         if (local_yn != -1) {
6007                 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
6008                         "UPDATE mail_box_tbl SET"
6009                         " mailbox_type = ?"
6010                         " WHERE account_id = %d"
6011                         " AND local_yn = %d"
6012                         " AND mailbox_id = '%d'"
6013                         , account_id
6014                         , local_yn
6015                         , input_mailbox_id);
6016         } else {
6017                 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
6018                         "UPDATE mail_box_tbl SET"
6019                         " mailbox_type = ?"
6020                         " WHERE account_id = %d"
6021                         " AND mailbox_id = '%d'"
6022                         , account_id
6023                         , input_mailbox_id);
6024         }
6025
6026         EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6027
6028         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_box_tbl, NULL), rc);
6029
6030         if (SQLITE_OK != rc) {
6031                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6032                 error = EMAIL_ERROR_DB_FAILURE;
6033                 goto FINISH_OFF;
6034         }
6035
6036         _bind_stmt_field_data_int(hStmt_box_tbl, i++, new_mailbox_type);
6037
6038
6039         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_box_tbl), rc);
6040
6041         if (rc == SQLITE_FULL) {
6042                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6043                 error   = EMAIL_ERROR_MAIL_MEMORY_FULL;
6044                 goto FINISH_OFF;
6045         }
6046
6047         if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
6048                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6049                 error = EMAIL_ERROR_DB_FAILURE;
6050                 goto FINISH_OFF;
6051         }
6052
6053
6054         /*  Update mail_tbl */
6055         i = 0;
6056         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6057                         "UPDATE mail_tbl SET"
6058                         " mailbox_type = ?"
6059                         " WHERE account_id = %d"
6060                         " AND mailbox_id = '%d'"
6061                         , account_id
6062                         , input_mailbox_id);
6063
6064         EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
6065
6066         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_mail_tbl, NULL), rc);
6067         if (SQLITE_OK != rc) {
6068                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6069                 error = EMAIL_ERROR_DB_FAILURE;
6070                 goto FINISH_OFF;
6071         }
6072
6073         _bind_stmt_field_data_int(hStmt_mail_tbl, i++, new_mailbox_type);
6074
6075         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_mail_tbl), rc);
6076         if (rc == SQLITE_FULL) {
6077                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6078                 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
6079                 goto FINISH_OFF;
6080         }
6081
6082         if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
6083                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6084                 error = EMAIL_ERROR_DB_FAILURE;
6085                 goto FINISH_OFF;
6086         }
6087
6088         ret = true;
6089
6090 FINISH_OFF:
6091         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6092
6093         if (hStmt_box_tbl != NULL) {
6094                 rc = sqlite3_finalize(hStmt_box_tbl);
6095                 if (rc != SQLITE_OK) {
6096                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6097                         error = EMAIL_ERROR_DB_FAILURE;
6098                 }
6099         }
6100
6101         if (hStmt_mail_tbl != NULL) {
6102                 rc = sqlite3_finalize(hStmt_mail_tbl);
6103                 if (rc != SQLITE_OK) {
6104                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6105                         error = EMAIL_ERROR_DB_FAILURE;
6106                 }
6107         }
6108
6109         if (err_code != NULL)
6110                 *err_code = error;
6111
6112         EM_DEBUG_FUNC_END("ret [%d]", ret);
6113         return ret;
6114 }
6115
6116 INTERNAL_FUNC int emstorage_set_local_mailbox(char *multi_user_name, int input_mailbox_id, int input_is_local_mailbox, int transaction)
6117 {
6118         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);
6119
6120         int rc, ret = false;
6121         int error = EMAIL_ERROR_NONE;
6122         char sql_query_string[QUERY_SIZE] = {0, };
6123
6124         if (input_mailbox_id < 0) {
6125                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6126                 return EMAIL_ERROR_INVALID_PARAM;
6127         }
6128
6129         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6130
6131         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6132
6133         EM_DEBUG_LOG("emstorage_update_mailbox_type");
6134
6135         DB_STMT hStmt = NULL;
6136         int i = 0;
6137
6138         /*  Update mail_box_tbl */
6139         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6140                 "UPDATE mail_box_tbl SET"
6141                 " local_yn = ?"
6142                 " WHERE mailbox_id = %d"
6143                 , input_mailbox_id);
6144
6145         EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6146
6147         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6148         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6149                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6150
6151         _bind_stmt_field_data_int(hStmt, i++, input_is_local_mailbox);
6152
6153         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6154         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
6155                 ("sqlite3_step fail:%d", rc));
6156         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6157                 ("sqlite3_step fail:%d", rc));
6158 /*
6159         if (hStmt != NULL) {
6160                 rc = sqlite3_finalize(hStmt);
6161                 if (rc != SQLITE_OK) {
6162                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6163                         error = EMAIL_ERROR_DB_FAILURE;
6164                 }
6165                 hStmt = NULL;
6166         }
6167 */
6168         ret = true;
6169
6170 FINISH_OFF:
6171         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6172
6173         if (hStmt != NULL) {
6174                 rc = sqlite3_finalize(hStmt);
6175                 if (rc != SQLITE_OK) {
6176                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6177                         error = EMAIL_ERROR_DB_FAILURE;
6178                 }
6179         }
6180
6181         EM_DEBUG_FUNC_END("error [%d]", error);
6182         return error;
6183 }
6184
6185 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)
6186 {
6187         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);
6188         int i = 0;
6189         int err = EMAIL_ERROR_NONE;
6190         int result = false;
6191         int cur_mailbox_id_string = 0;
6192         int mailbox_id_string_buffer_length = 0;
6193         char  sql_query_string[QUERY_SIZE] = {0, };
6194         char *mailbox_id_string_buffer = NULL;
6195         char *parameter_string = NULL;
6196         sqlite3 *local_db_handle = NULL;
6197
6198         if (input_mailbox_id_array == NULL || input_mailbox_id_count == 0 || input_field_name == NULL) {
6199                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6200                 return EMAIL_ERROR_INVALID_PARAM;
6201         }
6202
6203         local_db_handle = emstorage_get_db_connection(multi_user_name);
6204
6205         /* Generating mail id list string */
6206         mailbox_id_string_buffer_length = MAILBOX_ID_STRING_LENGTH * input_mailbox_id_count;
6207
6208         mailbox_id_string_buffer = em_malloc(mailbox_id_string_buffer_length);
6209
6210         if (!mailbox_id_string_buffer) {
6211                 EM_DEBUG_EXCEPTION("em_mallocfailed");
6212                 err = EMAIL_ERROR_OUT_OF_MEMORY;
6213                 goto FINISH_OFF;
6214         }
6215
6216         for (i = 0; i < input_mailbox_id_count; i++)
6217                 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]);
6218
6219         if (EM_SAFE_STRLEN(mailbox_id_string_buffer) > 1)
6220                 mailbox_id_string_buffer[EM_SAFE_STRLEN(mailbox_id_string_buffer) - 1] = NULL_CHAR;
6221
6222         /* Generating notification parameter string */
6223         parameter_string = em_malloc(mailbox_id_string_buffer_length + EM_SAFE_STRLEN(input_field_name) + 2);
6224
6225         if (!parameter_string) {
6226                 EM_DEBUG_EXCEPTION("em_mallocfailed");
6227                 err = EMAIL_ERROR_OUT_OF_MEMORY;
6228                 goto FINISH_OFF;
6229         }
6230
6231         SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", input_field_name, 0x01, mailbox_id_string_buffer);
6232
6233         /* Write query string */
6234         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);
6235
6236         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
6237
6238         /* Execute query */
6239         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
6240         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6241         if (err != EMAIL_ERROR_NONE) {
6242                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
6243                         goto FINISH_OFF;
6244         }
6245
6246         if (sqlite3_changes(local_db_handle) == 0)
6247                 EM_DEBUG_LOG("no mail matched...");
6248
6249         result = true;
6250
6251 FINISH_OFF:
6252         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, err);
6253
6254         if (err == EMAIL_ERROR_NONE && parameter_string) {
6255                 if (!emcore_notify_storage_event(NOTI_MAILBOX_FIELD_UPDATE, input_account_id, 0, parameter_string, input_value))
6256                         EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_MAILBOX_FIELD_UPDATE [%s,%d]",
6257                                                                                          input_field_name, input_value);
6258         }
6259
6260         EM_SAFE_FREE(mailbox_id_string_buffer);
6261         EM_SAFE_FREE(parameter_string);
6262
6263         EM_DEBUG_FUNC_END("err [%d]", err);
6264         return err;
6265 }
6266
6267 INTERNAL_FUNC int emstorage_add_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox_tbl, int transaction, int *err_code)
6268 {
6269         EM_DEBUG_FUNC_BEGIN("mailbox_tbl[%p], transaction[%d], err_code[%p]", mailbox_tbl, transaction, err_code);
6270
6271         if (!mailbox_tbl) {
6272                 if (err_code != NULL)
6273                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6274                 return false;
6275         }
6276
6277         int rc, ret = false;
6278         int error = EMAIL_ERROR_NONE;
6279         DB_STMT hStmt = NULL;
6280         char sql_query_string[QUERY_SIZE] = {0,};
6281         char **result = NULL;
6282         time_t current_time;
6283         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6284         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6285
6286         EM_SAFE_STRCPY(sql_query_string, "SELECT max(rowid) FROM mail_box_tbl;");
6287
6288         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6289         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6290                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6291
6292         time(&current_time);
6293
6294         if (NULL == result[1])
6295                 rc = 1;
6296         else
6297                 rc = atoi(result[1]) + 1;
6298         sqlite3_free_table(result);
6299
6300         memset(sql_query_string, 0, sizeof(char) * QUERY_SIZE);
6301
6302         mailbox_tbl->mailbox_id = rc;
6303
6304         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6305                 "INSERT INTO mail_box_tbl VALUES "
6306                 "(?"    /* mailbox_id */
6307                 ", ?"    /* account_id */
6308                 ", ?"    /* local_yn */
6309                 ", ?"    /* mailbox_name */
6310                 ", ?"    /* mailbox_type */
6311                 ", ?"    /* alias */
6312                 ", ?"    /* deleted_flag */
6313                 ", ?"    /* modifiable_yn */
6314                 ", ?"    /* total_mail_count_on_server */
6315                 ", ?"    /* has_archived_mails */
6316                 ", ?"    /* mail_slot_size */
6317                 ", ?"    /* no_select */
6318                 ", ?"    /* last_sync_time */
6319                 ", ?"    /* eas_data_length */
6320                 ", ?"    /* eas_data */
6321                 ")");
6322
6323
6324         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6325         EM_DEBUG_LOG_DEV("After sqlite3_prepare hStmt = %p", hStmt);
6326         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6327                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6328
6329         int col_index = 0;
6330
6331         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_id);
6332         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->account_id);
6333         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->local_yn);
6334         _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
6335         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_type);
6336         _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->alias, 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
6337         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->deleted_flag);
6338         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->modifiable_yn);
6339         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->total_mail_count_on_server);
6340         _bind_stmt_field_data_int(hStmt, col_index++, 0);
6341         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mail_slot_size);
6342         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->no_select);
6343         _bind_stmt_field_data_int(hStmt, col_index++, current_time);
6344         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->eas_data_length);
6345         _bind_stmt_field_data_blob(hStmt, col_index++, (void*)mailbox_tbl->eas_data, mailbox_tbl->eas_data_length);
6346
6347
6348         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6349         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6350                 ("sqlite3_step fail:%dn", rc));
6351
6352         ret = true;
6353
6354 FINISH_OFF:
6355         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6356         if (hStmt != NULL) {
6357                 rc = sqlite3_finalize(hStmt);
6358                 if (rc != SQLITE_OK) {
6359                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6360                         error = EMAIL_ERROR_DB_FAILURE;
6361                 }
6362         }
6363
6364         if (error == EMAIL_ERROR_NONE) {
6365                 if (!emcore_notify_storage_event(NOTI_MAILBOX_ADD, mailbox_tbl->account_id, mailbox_tbl->mailbox_id,
6366                                                                   mailbox_tbl->mailbox_name, mailbox_tbl->mailbox_type))
6367                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6368         }
6369
6370         if (err_code != NULL)
6371                 *err_code = error;
6372
6373         EM_DEBUG_FUNC_END("ret [%d]", ret);
6374         return ret;
6375 }
6376
6377 INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(char *multi_user_name, int account_id, int modifiable_yn, int transaction, int *err_code)
6378 {
6379         EM_DEBUG_FUNC_BEGIN("account_id[%d], modifiable_yn[%d], err_code[%p]", account_id, modifiable_yn, err_code);
6380
6381         if (account_id < FIRST_ACCOUNT_ID) {
6382
6383                 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
6384
6385                 if (err_code != NULL)
6386                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6387                 return false;
6388         }
6389         int rc, ret = false;
6390         int error = EMAIL_ERROR_NONE;
6391         char sql_query_string[QUERY_SIZE] = {0,};
6392         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6393         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6394
6395
6396         SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET modifiable_yn = %d WHERE account_id = %d", modifiable_yn, account_id);
6397
6398         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6399         if (error != EMAIL_ERROR_NONE) {
6400                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6401                         goto FINISH_OFF;
6402         }
6403
6404         rc = sqlite3_changes(local_db_handle);
6405         if (rc == 0)
6406                 EM_DEBUG_EXCEPTION("All mailbox_name modifiable_yn set to 0 already");
6407
6408
6409         ret = true;
6410
6411 FINISH_OFF:
6412         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6413
6414         if (err_code != NULL)
6415                 *err_code = error;
6416         EM_DEBUG_FUNC_END("ret [%d]", ret);
6417         return ret;
6418
6419
6420 }
6421
6422 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)
6423 {
6424         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);
6425
6426         if (account_id < FIRST_ACCOUNT_ID) {
6427                 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
6428
6429                 if (err_code != NULL)
6430                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6431                 return false;
6432         }
6433
6434         int rc, ret = false;
6435         int error = EMAIL_ERROR_NONE;
6436         char sql_query_string[QUERY_SIZE] = {0, };
6437         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6438         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6439
6440         if (local_yn == -1)
6441                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_box_tbl WHERE account_id = %d ", account_id);
6442         else
6443                 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);
6444
6445         if (input_mailbox_id > 0) {             /* 0 means all mailbox */
6446                 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);
6447         }
6448
6449         EM_DEBUG_LOG_SEC("mailbox sql_query_string [%s]", sql_query_string);
6450         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6451         if (error != EMAIL_ERROR_NONE) {
6452                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6453                         goto FINISH_OFF;
6454         }
6455
6456         rc = sqlite3_changes(local_db_handle);
6457         if (rc == 0) {
6458                 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
6459                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
6460                 ret = true;
6461         }
6462         ret = true;
6463
6464 FINISH_OFF:
6465
6466         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6467
6468         if (error == EMAIL_ERROR_NONE) {
6469                 if (!emcore_notify_storage_event(NOTI_MAILBOX_DELETE, account_id, input_mailbox_id, NULL, 0))
6470                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6471         }
6472
6473         if (err_code != NULL)
6474                 *err_code = error;
6475
6476         EM_DEBUG_FUNC_END("ret [%d]", ret);
6477         return ret;
6478 }
6479
6480 INTERNAL_FUNC int emstorage_free_mailbox(emstorage_mailbox_tbl_t** mailbox_list, int count, int *err_code)
6481 {
6482         EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d], err_code[%p]", mailbox_list, count, err_code);
6483
6484         int ret = false;
6485         int error = EMAIL_ERROR_NONE;
6486
6487         if (count > 0) {
6488                 if (!mailbox_list || !*mailbox_list) {
6489                         EM_DEBUG_EXCEPTION(" mailbox_list[%p], count[%d]", mailbox_list, count);
6490
6491                         error = EMAIL_ERROR_INVALID_PARAM;
6492                         goto FINISH_OFF;
6493                 }
6494
6495                 emstorage_mailbox_tbl_t* p = *mailbox_list;
6496                 int i = 0;
6497
6498                 for (; i < count; i++) {
6499                         EM_SAFE_FREE(p[i].mailbox_name);
6500                         EM_SAFE_FREE(p[i].alias);
6501                         EM_SAFE_FREE(p[i].eas_data); /*valgrind*/
6502                 }
6503
6504                 EM_SAFE_FREE(p); *mailbox_list = NULL;
6505         }
6506
6507         ret = true;
6508
6509 FINISH_OFF:
6510         if (err_code != NULL)
6511                 *err_code = error;
6512
6513         EM_DEBUG_FUNC_END("ret [%d]", ret);
6514         return ret;
6515 }
6516
6517 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)
6518 {
6519         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);
6520
6521         if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !count) {
6522                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], count[%p], exist[%p]", account_id, mailbox_name, count);
6523
6524                 if (err_code != NULL)
6525                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6526                 return false;
6527         }
6528
6529         int rc = -1, ret = false;
6530         int error = EMAIL_ERROR_NONE;
6531         char sql_query_string[QUERY_SIZE] = {0, };
6532         char *replaced_mailbox_name = NULL;
6533
6534         if (strstr(mailbox_name, "'")) {
6535                 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6536         } else {
6537                 replaced_mailbox_name = EM_SAFE_STRDUP(mailbox_name);
6538         }
6539
6540         EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6541
6542
6543         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6544         EMSTORAGE_START_READ_TRANSACTION(transaction);
6545         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);
6546         EM_DEBUG_LOG_SEC(">>> SQL [ %s ] ", sql_query_string);
6547
6548         char **result;
6549
6550         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6551         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6552                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6553
6554         *count = atoi(result[1]);
6555         sqlite3_free_table(result);
6556
6557         ret = true;
6558
6559 FINISH_OFF:
6560         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6561
6562         EM_SAFE_FREE(replaced_mailbox_name);
6563
6564         if (err_code != NULL)
6565                 *err_code = error;
6566
6567         EM_DEBUG_FUNC_END("ret [%d]", ret);
6568         return ret;
6569 }
6570
6571 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)
6572 {
6573         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);
6574
6575         if (account_id < FIRST_ACCOUNT_ID || !uid || !exist) {
6576                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], uid[%p], exist[%p]", account_id, mailbox_name , uid, exist);
6577
6578                 if (err_code != NULL)
6579                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6580                 return false;
6581         }
6582
6583         int rc = -1, ret = false;
6584         int error = EMAIL_ERROR_NONE;
6585         char sql_query_string[QUERY_SIZE] = {0, };
6586         char *replaced_mailbox_name = NULL;
6587
6588         EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6589
6590         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6591         EMSTORAGE_START_READ_TRANSACTION(transaction);
6592
6593         if (mailbox_name) {
6594                 if (strstr(mailbox_name, "'")) {
6595                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6596                 } else {
6597                         replaced_mailbox_name = strdup(mailbox_name);
6598                 }
6599
6600                 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);
6601         } else {
6602                 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);
6603         }
6604
6605         char **result = NULL;
6606
6607         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6608         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6609                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6610
6611         *exist = atoi(result[1]);
6612         sqlite3_free_table(result);
6613
6614         if (*exist > 0)
6615                 *exist = 1;
6616         else
6617                 *exist = 0;
6618
6619         ret = true;
6620
6621 FINISH_OFF:
6622         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6623
6624         EM_SAFE_FREE(replaced_mailbox_name);
6625
6626         if (err_code != NULL)
6627                 *err_code = error;
6628
6629         EM_DEBUG_FUNC_END("ret [%d]", ret);
6630         return ret;
6631 }
6632
6633 INTERNAL_FUNC int emstorage_get_downloaded_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code)
6634 {
6635         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], err_code[%p]", mail_id, mail, err_code);
6636
6637         if (!mail || mail_id <= 0) {
6638                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
6639                 if (err_code != NULL)
6640                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6641                 return false;
6642         }
6643
6644         int rc, ret = false;
6645         int error = EMAIL_ERROR_NONE;
6646         DB_STMT hStmt = NULL;
6647         char sql_query_string[QUERY_SIZE] = {0, };
6648
6649         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6650         EMSTORAGE_START_READ_TRANSACTION(transaction);
6651
6652         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE local_uid = %d", mail_id);
6653
6654         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6655         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6656
6657         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6658                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6659
6660
6661         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6662         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6663                 ("sqlite3_step fail:%d", rc));
6664
6665         *mail = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
6666         if (*mail == NULL) {
6667                 error = EMAIL_ERROR_OUT_OF_MEMORY;
6668                 EM_DEBUG_EXCEPTION("Memory allocation for mail failed.");
6669                 goto FINISH_OFF;
6670
6671         }
6672         memset(*mail, 0x00, sizeof(emstorage_mail_tbl_t));
6673
6674         _get_stmt_field_data_int(hStmt, &((*mail)->account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6675         _get_stmt_field_data_int(hStmt, &((*mail)->mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6676         _get_stmt_field_data_string(hStmt, &((*mail)->server_mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6677         _get_stmt_field_data_int(hStmt, &((*mail)->mail_id), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6678         _get_stmt_field_data_string(hStmt, &((*mail)->server_mail_id), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6679         _get_stmt_field_data_int(hStmt, &((*mail)->mail_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6680         _get_stmt_field_data_char(hStmt, &((*mail)->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6681
6682         (*mail)->server_mail_status = 1;
6683
6684         ret = true;
6685
6686 FINISH_OFF:
6687
6688         if (hStmt != NULL) {
6689                 rc = sqlite3_finalize(hStmt);
6690                 if (rc != SQLITE_OK) {
6691                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6692                         error = EMAIL_ERROR_DB_FAILURE;
6693                 }
6694         }
6695
6696         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6697
6698         if (err_code != NULL)
6699                 *err_code = error;
6700
6701         EM_DEBUG_FUNC_END("ret [%d]", ret);
6702         return ret;
6703 }
6704
6705 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)
6706 {
6707         EM_PROFILE_BEGIN(emStorageGetDownloadList);
6708         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);
6709         if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid || !count) {
6710                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_id[%s], read_mail_uid[%p], count[%p]", account_id, mailbox_id, read_mail_uid, count);
6711
6712                 if (err_code != NULL)
6713                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6714                 return false;
6715         }
6716
6717         int rc, ret = false;
6718         int error = EMAIL_ERROR_NONE;
6719
6720         DB_STMT hStmt = NULL;
6721         char sql_query_string[QUERY_SIZE] = {0, };
6722
6723         emstorage_read_mail_uid_tbl_t* p_data_tbl = NULL;
6724         int i = 0;
6725
6726         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6727         EMSTORAGE_START_READ_TRANSACTION(transaction);
6728
6729         if (mailbox_id)
6730                 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);
6731         else
6732                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
6733
6734         EM_DEBUG_LOG_SEC(" sql_query_string : %s", sql_query_string);
6735
6736
6737
6738         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6739         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6740         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6741                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6742
6743
6744         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6745         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6746                 ("sqlite3_step fail:%d", rc));
6747
6748         char **result;
6749         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL); */
6750         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL), rc);
6751         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6752                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6753
6754         sqlite3_free_table(result);
6755         if (*count == 0) {
6756                 EM_DEBUG_LOG("No mail found in mail_read_mail_uid_tbl");
6757                 ret = true;
6758                 goto FINISH_OFF;
6759         }
6760
6761
6762         if (!(p_data_tbl = (emstorage_read_mail_uid_tbl_t*)malloc(sizeof(emstorage_read_mail_uid_tbl_t) * *count))) {
6763                 EM_DEBUG_EXCEPTION(" malloc failed...");
6764                 error = EMAIL_ERROR_OUT_OF_MEMORY;
6765                 goto FINISH_OFF;
6766         }
6767
6768         memset(p_data_tbl, 0x00, sizeof(emstorage_read_mail_uid_tbl_t)*(*count));
6769
6770         for (i = 0; i < *count; ++i) {
6771                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6772                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6773                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6774                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].local_uid), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6775                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].server_uid), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6776                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rfc822_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6777                 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6778                 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_flagged_field), FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6779
6780                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6781                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6782                         ("sqlite3_step fail:%d", rc));
6783         }
6784
6785         ret = true;
6786
6787 FINISH_OFF:
6788         if (ret == true)
6789                 *read_mail_uid = p_data_tbl;
6790         else if (p_data_tbl)
6791                 emstorage_free_read_mail_uid(&p_data_tbl, *count, NULL);
6792
6793         if (hStmt != NULL) {
6794                 rc = sqlite3_finalize(hStmt);
6795                 if (rc != SQLITE_OK) {
6796                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6797                         error = EMAIL_ERROR_DB_FAILURE;
6798                 }
6799         }
6800
6801         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6802
6803         if (err_code != NULL)
6804                 *err_code = error;
6805
6806         EM_PROFILE_END(emStorageGetDownloadList);
6807         EM_DEBUG_FUNC_END("ret [%d]", ret);
6808         return ret;
6809 }
6810
6811 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)
6812 {
6813         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);
6814
6815         if (account_id < FIRST_ACCOUNT_ID || !mail_size) {
6816                 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);
6817
6818                 if (err_code != NULL)
6819                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6820                 return false;
6821         }
6822
6823         int rc, ret = false;
6824         int error = EMAIL_ERROR_NONE;
6825         DB_STMT hStmt = NULL;
6826         char sql_query_string[QUERY_SIZE] = {0, };
6827         char *replaced_mailbox_name = NULL;
6828
6829         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6830         EMSTORAGE_START_READ_TRANSACTION(transaction);
6831
6832         if (mailbox_name) {
6833                 if (strstr(mailbox_name, "'")) {
6834                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6835                 } else {
6836                         replaced_mailbox_name = strdup(mailbox_name);
6837                 }
6838
6839                 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6840
6841                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6842                         "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6843                         "WHERE account_id = %d "
6844                         "AND mailbox_id = '%s' "
6845                         "AND local_uid = %d "
6846                         "AND mailbox_name = '%s' "
6847                         "AND server_uid = '%s'",
6848                         account_id, mailbox_id, local_uid, replaced_mailbox_name, uid);
6849         } else {
6850                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6851                         "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6852                         "WHERE account_id = %d "
6853                         "AND mailbox_id = '%s' "
6854                         "AND local_uid = %d "
6855                         "AND server_uid = '%s'",
6856                         account_id, mailbox_id, local_uid, uid);
6857         }
6858
6859
6860         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6861         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6862                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6863
6864
6865         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6866         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6867                 ("sqlite3_step fail:%d", rc));
6868
6869         if (rc == SQLITE_DONE) {
6870                 EM_DEBUG_LOG("no matched mail found....");
6871                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
6872                 goto FINISH_OFF;
6873         }
6874
6875         _get_stmt_field_data_int(hStmt, mail_size, 0);
6876
6877         ret = true;
6878
6879 FINISH_OFF:
6880         EM_SAFE_FREE(replaced_mailbox_name);
6881
6882         if (hStmt != NULL) {
6883                 rc = sqlite3_finalize(hStmt);
6884                 if (rc != SQLITE_OK) {
6885                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6886                         error = EMAIL_ERROR_DB_FAILURE;
6887                 }
6888         }
6889
6890         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6891
6892         if (err_code != NULL)
6893                 *err_code = error;
6894
6895         EM_DEBUG_FUNC_END("ret [%d]", ret);
6896         return ret;
6897 }
6898
6899 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)
6900 {
6901         EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], transaction[%d], err_code[%p]", read_mail_uid, transaction, err_code);
6902
6903         if (!read_mail_uid) {
6904                 EM_DEBUG_EXCEPTION("read_mail_uid[%p]", read_mail_uid);
6905                 if (err_code != NULL)
6906                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6907                 return false;
6908         }
6909
6910         int rc, rc2,  ret = false;
6911         int error = EMAIL_ERROR_NONE;
6912         DB_STMT hStmt = NULL;
6913         char sql_query_string[QUERY_SIZE] = {0, };
6914
6915         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6916         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6917
6918         char *sql = "SELECT max(rowid) FROM mail_read_mail_uid_tbl;";
6919         char **result = NULL;
6920
6921
6922         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6923         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6924                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6925
6926         if (NULL == result[1]) rc = 1;
6927         else rc = atoi(result[1])+1;
6928         sqlite3_free_table(result);
6929
6930         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6931                 "INSERT INTO mail_read_mail_uid_tbl VALUES "
6932                 "(?"  /* account_id */
6933                 ", ?"  /* mailbox_id */
6934                 ", ?"  /* mailbox_name */
6935                 ", ?"  /* local_uid */
6936                 ", ?"  /* server_uid */
6937                 ", ?"  /* rfc822_size */
6938                 ", ?"  /* sync_status */
6939                 ", ?"  /* flags_seen_field */
6940                 ", ?"  /* flags_flagged_field */
6941                 ", ?)");
6942
6943
6944         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
6945         if (rc2 != SQLITE_OK) {
6946                 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6947                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6948
6949                 error = EMAIL_ERROR_DB_FAILURE;
6950                 goto FINISH_OFF;
6951         }
6952
6953         EM_DEBUG_LOG("account_id[%d] mailbox_id[%d] local_uid [%d]"
6954                    "server_uid[%s] rfc822_size[%d] rc[%d]",
6955          read_mail_uid->account_id, read_mail_uid->mailbox_id, read_mail_uid->local_uid,
6956          read_mail_uid->server_uid, read_mail_uid->rfc822_size, rc);
6957
6958         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->account_id);
6959         _bind_stmt_field_data_int(hStmt, LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->mailbox_id);
6960         _bind_stmt_field_data_int(hStmt, LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->local_uid);
6961         _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);
6962         _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);
6963         _bind_stmt_field_data_int(hStmt, RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->rfc822_size);
6964         _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_seen_field);
6965         _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_flagged_field);
6966         _bind_stmt_field_data_int(hStmt, IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, rc);
6967
6968
6969         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6970         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
6971                 ("sqlite3_step fail:%d", rc));
6972         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6973                 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
6974
6975
6976         ret = true;
6977
6978 FINISH_OFF:
6979         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6980         if (hStmt != NULL) {
6981                 rc = sqlite3_finalize(hStmt);
6982                 if (rc != SQLITE_OK) {
6983                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6984                         error = EMAIL_ERROR_DB_FAILURE;
6985                 }
6986         }
6987
6988         if (err_code != NULL)
6989                 *err_code = error;
6990
6991         EM_DEBUG_FUNC_END("ret [%d]", ret);
6992         return ret;
6993 }
6994
6995 #ifdef __FEATURE_BODY_SEARCH__
6996 INTERNAL_FUNC int emstorage_add_mail_text(char *multi_user_name, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
6997 {
6998         EM_DEBUG_FUNC_BEGIN("mail_text[%p], transaction[%d], err_code[%p]", mail_text, transaction, err_code);
6999
7000         if (!mail_text) {
7001                 EM_DEBUG_EXCEPTION("mail_text[%p]", mail_text);
7002                 if (err_code != NULL)
7003                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7004                 return false;
7005         }
7006
7007         int rc, rc2,  ret = false;
7008         int error = EMAIL_ERROR_NONE;
7009         DB_STMT hStmt = NULL;
7010         char sql_query_string[QUERY_SIZE] = {0, };
7011
7012         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7013         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7014
7015         char *sql = "SELECT max(rowid) FROM mail_text_tbl;";
7016         char **result = NULL;
7017
7018         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7019         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7020                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7021         sqlite3_free_table(result);
7022
7023         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7024                 "INSERT INTO mail_text_tbl VALUES "
7025                 "(?"
7026                 ", ?"
7027                 ", ?"
7028                 ", ?)");
7029
7030         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
7031         if (rc2 != SQLITE_OK) {
7032                 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7033                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc2, sqlite3_errmsg(local_db_handle));
7034
7035                 error = EMAIL_ERROR_DB_FAILURE;
7036                 goto FINISH_OFF;
7037         }
7038
7039         EM_DEBUG_LOG("mail_id[%d] account_id[%d] mailbox_id[%d]", mail_text->mail_id,
7040                                        mail_text->account_id, mail_text->mailbox_id);
7041         EM_DEBUG_LOG_DEV("body_text VALUE [%s] ", mail_text->body_text);
7042
7043         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mail_id);
7044         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->account_id);
7045         _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mailbox_id);
7046         _bind_stmt_field_data_string(hStmt, BODY_TEXT_IDX_IN_MAIL_TEXT_TBL, (char *)mail_text->body_text, 0, -1);
7047
7048         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7049         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
7050                 ("sqlite3_step fail:%d", rc));
7051         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7052                 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
7053
7054         ret = true;
7055
7056 FINISH_OFF:
7057         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7058         if (hStmt != NULL) {
7059                 rc = sqlite3_finalize(hStmt);
7060                 if (rc != SQLITE_OK) {
7061                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7062                         error = EMAIL_ERROR_DB_FAILURE;
7063                 }
7064         }
7065
7066         if (err_code != NULL)
7067                 *err_code = error;
7068
7069         EM_DEBUG_FUNC_END("ret [%d]", ret);
7070         return ret;
7071 }
7072 #endif
7073
7074 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)
7075 {
7076         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);
7077
7078         if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid) {
7079                 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);
7080
7081                 if (err_code != NULL)
7082                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7083                 return false;
7084         }
7085
7086         int rc, ret = false;
7087         int error = EMAIL_ERROR_NONE;
7088         DB_STMT hStmt = NULL;
7089         char sql_query_string[QUERY_SIZE] = {0, };
7090
7091         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7092
7093         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7094
7095         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7096                 "UPDATE mail_read_mail_uid_tbl SET"
7097                 "  account_id = ?"
7098                 ", mailbox_id = ?"
7099                 ", mailbox_name = ?"
7100                 ", local_uid  = ?"
7101                 ", server_uid = ?"
7102                 ", rfc822_size = ?"
7103                 ", flags_seen_field  = ?"
7104                 ", flags_flagged_field  = ?"
7105                 " WHERE account_id = ?"
7106                 " AND mailbox_id  = ?"
7107                 " AND local_uid   = ?"
7108                 " AND mailbox_name= ?"
7109                 " AND server_uid = ?");
7110
7111
7112         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7113         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7114         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7115                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7116
7117
7118         int i = 0;
7119
7120         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->account_id);
7121         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->mailbox_id);
7122         _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7123         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->local_uid);
7124         _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->server_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7125         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->rfc822_size);
7126         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_seen_field);
7127         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_flagged_field);
7128         _bind_stmt_field_data_int(hStmt, i++, account_id);
7129         _bind_stmt_field_data_int(hStmt, i++, mailbox_id);
7130         _bind_stmt_field_data_int(hStmt, i++, local_uid);
7131         _bind_stmt_field_data_string(hStmt, i++, (char*)mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7132         _bind_stmt_field_data_string(hStmt, i++, (char*)uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7133
7134
7135         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7136         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7137                 ("sqlite3_step fail:%d", rc));
7138
7139         ret = true;
7140
7141 FINISH_OFF:
7142         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7143         if (hStmt != NULL) {
7144                 rc = sqlite3_finalize(hStmt);
7145                 if (rc != SQLITE_OK) {
7146                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7147                         error = EMAIL_ERROR_DB_FAILURE;
7148                 }
7149         }
7150
7151         if (err_code != NULL)
7152                 *err_code = error;
7153
7154         EM_DEBUG_FUNC_END("ret [%d]", ret);
7155         return ret;
7156 }
7157
7158 INTERNAL_FUNC int emstorage_remove_downloaded_mail(char *multi_user_name,
7159                                                                                                         int account_id,
7160                                                                                                         int mailbox_id,
7161                                                                                                         char *mailbox_name,
7162                                                                                                         char *uid,
7163                                                                                                         int transaction,
7164                                                                                                         int *err_code)
7165 {
7166         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], mailbox_id[%d], mailbox_name[%s], "
7167                                                         "uid[%s], transaction[%d], err_code[%p]",
7168                                                         account_id, mailbox_id, mailbox_name, uid, transaction, err_code);
7169
7170         if (account_id < FIRST_ACCOUNT_ID) {
7171                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_name[%s], uid[%s]", account_id, mailbox_name, uid);
7172
7173                 if (err_code != NULL)
7174                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7175                 return false;
7176         }
7177
7178         int ret = false;
7179         int error = EMAIL_ERROR_NONE;
7180         char sql_query_string[QUERY_SIZE] = {0, };
7181         char *replaced_mailbox_name = NULL;
7182         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7183
7184         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7185
7186         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7187                                 "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d ", account_id);
7188
7189         if (mailbox_id > 0) {
7190                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7191                                         sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7192                                         "AND mailbox_id = %d ", mailbox_id);
7193         }
7194
7195         if (mailbox_name) {             /*  NULL means all mailbox_name */
7196                 if (strstr(mailbox_name, "'")) {
7197                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
7198                 } else {
7199                         replaced_mailbox_name = strdup(mailbox_name);
7200                 }
7201
7202                 SNPRINTF(sql_query_string+EM_SAFE_STRLEN(sql_query_string),
7203                                         sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7204                                         "AND mailbox_name = '%s' ", replaced_mailbox_name);
7205         }
7206
7207         if (uid) {              /*  NULL means all mail */
7208                 sqlite3_snprintf(sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7209                                                  sql_query_string + EM_SAFE_STRLEN(sql_query_string), "AND server_uid = '%q' ",
7210                                                  uid);
7211         }
7212
7213         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7214         if (error != EMAIL_ERROR_NONE) {
7215                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7216                         goto FINISH_OFF;
7217         }
7218
7219         ret = true;
7220
7221 FINISH_OFF:
7222         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7223
7224         EM_SAFE_FREE(replaced_mailbox_name);
7225
7226         if (err_code != NULL)
7227                 *err_code = error;
7228
7229         EM_DEBUG_FUNC_END("ret [%d]", ret);
7230         return ret;
7231 }
7232
7233 INTERNAL_FUNC int emstorage_free_read_mail_uid(emstorage_read_mail_uid_tbl_t** read_mail_uid, int count, int *err_code)
7234 {
7235         EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], count[%d], err_code[%p]", read_mail_uid, count, err_code);
7236
7237         int ret = false;
7238         int error = EMAIL_ERROR_NONE;
7239
7240         if (count > 0) {
7241                 if (!read_mail_uid || !*read_mail_uid) {
7242                         EM_DEBUG_EXCEPTION(" read_mail_uid[%p], count[%d]", read_mail_uid, count);
7243
7244                         error = EMAIL_ERROR_INVALID_PARAM;
7245                         goto FINISH_OFF;
7246                 }
7247
7248                 emstorage_read_mail_uid_tbl_t* p = *read_mail_uid;
7249                 int i;
7250
7251                 for (i = 0; i < count; i++) {
7252                         EM_SAFE_FREE(p[i].mailbox_name);
7253                         EM_SAFE_FREE(p[i].server_uid);
7254                 }
7255
7256                 EM_SAFE_FREE(p); *read_mail_uid = NULL;
7257         }
7258
7259         ret = true;
7260
7261 FINISH_OFF:
7262         if (err_code != NULL)
7263                 *err_code = error;
7264
7265         EM_DEBUG_FUNC_END("ret [%d]", ret);
7266         return ret;
7267 }
7268
7269 INTERNAL_FUNC int emstorage_get_rule_count_by_account_id(char *multi_user_name, int account_id, int *count, int transaction, int *err_code)
7270 {
7271         EM_DEBUG_FUNC_BEGIN("account_id [%d], count[%p], transaction[%d], err_code[%p]", count, transaction, err_code);
7272
7273         if (!count) {
7274                 EM_DEBUG_EXCEPTION("count[%p]", count);
7275
7276                 if (err_code != NULL)
7277                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7278                 return false;
7279         }
7280
7281         int rc = -1, ret = false;
7282         int error =  EMAIL_ERROR_NONE;
7283         char sql_query_string[QUERY_SIZE] = {0, };
7284
7285         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7286         EMSTORAGE_START_READ_TRANSACTION(transaction);
7287
7288         if (account_id != ALL_ACCOUNT)
7289                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl where account_id = %d", account_id);
7290         else
7291                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl");
7292
7293         char **result;
7294
7295         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7296         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7297                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7298
7299         *count = atoi(result[1]);
7300         sqlite3_free_table(result);
7301
7302         ret = true;
7303
7304 FINISH_OFF:
7305         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7306
7307         if (err_code != NULL)
7308                 *err_code = error;
7309
7310         EM_DEBUG_FUNC_END("ret [%d]", ret);
7311         return ret;
7312 }
7313
7314 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)
7315 {
7316         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);
7317
7318         if (!select_num || !is_completed || !rule_list) {               /*  only global rule supported. */
7319                 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);
7320
7321                 if (err_code != NULL)
7322                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7323                 return false;
7324         }
7325
7326         int ret = false;
7327         int error = EMAIL_ERROR_NONE;
7328
7329         emstorage_rule_tbl_t* p_data_tbl = NULL;
7330         int i = 0, count = 0;
7331         DB_STMT hStmt = NULL;
7332         char sql_query_string[QUERY_SIZE] = {0, };
7333
7334         int rc;
7335
7336         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7337         EMSTORAGE_START_READ_TRANSACTION(transaction);
7338
7339         if (account_id != ALL_ACCOUNT) {
7340                 if (type)
7341                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d AND type = %d", account_id, type);
7342                 else
7343                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d ORDER BY type", account_id);
7344         } else {
7345                 if (type)
7346                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE type = %d", type);
7347                 else
7348                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl ORDER BY type");
7349         }
7350
7351         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7352 /*      EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt); */
7353         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7354                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7355
7356
7357         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7358         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7359                 ("sqlite3_step fail:%d", rc));
7360
7361         char **result;
7362         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL); */
7363         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
7364         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7365                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7366
7367         sqlite3_free_table(result);
7368
7369         if (count == 0) {
7370                 EM_DEBUG_LOG_DEV("No matching rule found...");
7371                 ret = true;
7372                 error = EMAIL_ERROR_FILTER_NOT_FOUND; /*there is no matched rule*/
7373                 goto FINISH_OFF;
7374         }
7375
7376
7377         if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t) * count))) {
7378                 EM_DEBUG_EXCEPTION(" malloc failed...");
7379
7380                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7381                 goto FINISH_OFF;
7382         }
7383
7384         memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t) * count);
7385
7386         for (i = 0; i < count; i++) {
7387                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7388                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7389                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7390                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].type), TYPE_IDX_IN_MAIL_RULE_TBL);
7391                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7392                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7393                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7394                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7395                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7396                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7397
7398                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7399                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7400                         ("sqlite3_step fail:%d", rc));
7401         }
7402
7403         ret = true;
7404
7405 FINISH_OFF:
7406
7407         EM_DEBUG_LOG("[%d] rules found.", count);
7408
7409         if (ret == true) {
7410                 *rule_list = p_data_tbl;
7411                 *select_num = count;
7412         } else if (p_data_tbl != NULL)
7413                 emstorage_free_rule(&p_data_tbl, count, NULL); /* CID FIX */
7414
7415         if (hStmt != NULL) {
7416                 rc = sqlite3_finalize(hStmt);
7417                 if (rc != SQLITE_OK) {
7418                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7419                         error = EMAIL_ERROR_DB_FAILURE;
7420                 }
7421         }
7422
7423         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7424
7425         if (err_code != NULL)
7426                 *err_code = error;
7427
7428         EM_DEBUG_FUNC_END("ret [%d]", ret);
7429         return ret;
7430 }
7431
7432 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)
7433 {
7434         EM_DEBUG_FUNC_BEGIN("rule_id[%d], rule[%p], transaction[%d], err_code[%p]", rule_id, rule, transaction, err_code);
7435         int error = EMAIL_ERROR_NONE;
7436         int ret = false;
7437         DB_STMT hStmt = NULL;
7438
7439         if (rule_id <= 0) {
7440                 EM_DEBUG_EXCEPTION("Invalid parameter");
7441                 error = EMAIL_ERROR_INVALID_PARAM;
7442                 goto FINISH_OFF;
7443         }
7444
7445         if (!rule) {
7446                 EM_DEBUG_EXCEPTION("rule_id[%d], rule[%p]", rule_id, rule);
7447                 error = EMAIL_ERROR_INVALID_PARAM;
7448                 goto FINISH_OFF;
7449         }
7450
7451         emstorage_rule_tbl_t* p_data_tbl = NULL;
7452         int rc;
7453
7454         char sql_query_string[QUERY_SIZE] = {0, };
7455         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7456         EMSTORAGE_START_READ_TRANSACTION(transaction);
7457
7458         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7459
7460         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7461         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7462                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7463
7464
7465         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7466         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7467                 ("sqlite3_step fail:%d", rc));
7468
7469         if (rc == SQLITE_DONE) {
7470                 EM_DEBUG_EXCEPTION(" no matched rule found...");
7471                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7472                 goto FINISH_OFF;
7473         }
7474
7475         if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t)))) {
7476                 EM_DEBUG_EXCEPTION(" malloc failed...");
7477                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7478                 goto FINISH_OFF;
7479         }
7480
7481         memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t));
7482         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7483         _get_stmt_field_data_int(hStmt, &(p_data_tbl->rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7484         _get_stmt_field_data_string(hStmt, &(p_data_tbl->filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7485         _get_stmt_field_data_int(hStmt, &(p_data_tbl->type), TYPE_IDX_IN_MAIL_RULE_TBL);
7486         _get_stmt_field_data_string(hStmt, &(p_data_tbl->value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7487         _get_stmt_field_data_string(hStmt, &(p_data_tbl->value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7488         _get_stmt_field_data_int(hStmt, &(p_data_tbl->action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7489         _get_stmt_field_data_int(hStmt, &(p_data_tbl->target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7490         _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7491         _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7492
7493         ret = true;
7494
7495 FINISH_OFF:
7496
7497         if (ret == true)
7498                 *rule = p_data_tbl;
7499
7500         if (hStmt != NULL) {
7501                 rc = sqlite3_finalize(hStmt);
7502                 if (rc != SQLITE_OK) {
7503                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7504                         error = EMAIL_ERROR_DB_FAILURE;
7505                 }
7506         }
7507
7508         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7509
7510         if (err_code != NULL)
7511                 *err_code = error;
7512
7513         EM_DEBUG_FUNC_END("ret [%d]", ret);
7514         return ret;
7515 }
7516
7517 INTERNAL_FUNC int emstorage_change_rule(char *multi_user_name, int rule_id, emstorage_rule_tbl_t* new_rule, int transaction, int *err_code)
7518 {
7519         EM_DEBUG_FUNC_BEGIN("rule_id[%d], new_rule[%p], transaction[%d], err_code[%p]", rule_id, new_rule, transaction, err_code);
7520
7521         if (!new_rule) {                /*  only global rule supported. */
7522                 EM_DEBUG_EXCEPTION("rule_id[%d], new_rule[%p]", rule_id, new_rule);
7523
7524                 if (err_code != NULL)
7525                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7526                 return false;
7527         }
7528
7529         int rc, ret = false;
7530         int error = EMAIL_ERROR_NONE;
7531
7532         DB_STMT hStmt = NULL;
7533         char sql_query_string[QUERY_SIZE] = {0, };
7534         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7535         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7536
7537         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7538                 "UPDATE mail_rule_tbl SET"
7539                 "  filter_name = ?"
7540                 ", type = ?"
7541                 ", value = ?"
7542                 ", value2 = ?"
7543                 ", action_type = ?"
7544                 ", target_mailbox_id = ?"
7545                 ", flag1 = ?"
7546                 ", flag2 = ?"
7547                 ", account_id = ?"
7548                 ", rule_id = ?"
7549                 " WHERE rule_id = %d"
7550                 , rule_id);
7551
7552
7553         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7554         EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
7555         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7556                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7557
7558         int i = 0;
7559
7560         _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->filter_name, 0, FILTER_NAME_LEN_IN_MAIL_RULE_TBL);
7561         _bind_stmt_field_data_int(hStmt, i++, new_rule->type);
7562         _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7563         _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7564         _bind_stmt_field_data_int(hStmt, i++, new_rule->action_type);
7565         _bind_stmt_field_data_int(hStmt, i++, new_rule->target_mailbox_id);
7566         _bind_stmt_field_data_int(hStmt, i++, new_rule->flag1);
7567         _bind_stmt_field_data_int(hStmt, i++, new_rule->flag2);
7568         _bind_stmt_field_data_int(hStmt, i++, new_rule->account_id);
7569         _bind_stmt_field_data_int(hStmt, i++, rule_id);
7570
7571
7572         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7573         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7574                 ("sqlite3_step fail:%d", rc));
7575
7576         ret = true;
7577
7578 FINISH_OFF:
7579         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
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         if (err_code != NULL)
7589                 *err_code = error;
7590
7591         EM_DEBUG_FUNC_END("ret [%d]", ret);
7592         return ret;
7593 }
7594
7595 INTERNAL_FUNC int emstorage_find_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7596 {
7597         EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7598
7599         if (!rule) {
7600                 EM_DEBUG_LOG("rule is NULL");
7601                 if (err_code != NULL)
7602                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7603                 return false;
7604         }
7605
7606         DB_STMT hStmt = NULL;
7607         char sql_query_string[QUERY_SIZE] = {0,};
7608         int error = EMAIL_ERROR_NONE;
7609         int rc = 0;
7610         int ret = false;
7611
7612         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7613         EMSTORAGE_START_READ_TRANSACTION(transaction);
7614
7615         switch (rule->action_type) {
7616         case EMAIL_FILTER_MOVE:
7617                 if (rule->type == EMAIL_PRIORITY_SENDER) {
7618                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7619                                 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7620                                 rule->action_type, rule->type, rule->value2);
7621                 } else {
7622                                 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7623                                         "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);
7624                 }
7625                 break;
7626         case EMAIL_FILTER_BLOCK:
7627                 if (rule->type == EMAIL_FILTER_FROM)
7628                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7629                                 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7630                                 rule->action_type, rule->type, rule->value2);
7631                 else if (rule->type == EMAIL_FILTER_SUBJECT)
7632                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7633                                 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value) = UPPER(\'%q\')",
7634                                 rule->action_type, rule->type, rule->value);
7635                 else if (rule->type == (EMAIL_FILTER_SUBJECT | EMAIL_FILTER_FROM))
7636                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7637                                 "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\'))",
7638                                 rule->action_type, EMAIL_FILTER_SUBJECT, rule->value, EMAIL_FILTER_FROM, rule->value2);
7639                 break;
7640
7641         default:
7642                 EM_DEBUG_EXCEPTION("Invalid parameter : rule->action_type[%d]", rule->action_type);
7643                 error = EMAIL_ERROR_INVALID_PARAM;
7644                 goto FINISH_OFF;
7645                 break;
7646         }
7647
7648         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7649         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7650                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
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         if (rc == SQLITE_DONE) {
7657                 EM_DEBUG_EXCEPTION(" no matched rule found...");
7658                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7659         }
7660
7661         ret = true;
7662
7663 FINISH_OFF:
7664
7665         if (hStmt != NULL) {
7666                 rc = sqlite3_finalize(hStmt);
7667                 if (rc != SQLITE_OK) {
7668                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7669                         error = EMAIL_ERROR_DB_FAILURE;
7670                 }
7671         }
7672
7673         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7674
7675         if (err_code)
7676                 *err_code = error;
7677
7678         EM_DEBUG_FUNC_END("ret [%d]", ret);
7679         return ret;
7680 }
7681
7682 INTERNAL_FUNC int emstorage_add_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7683 {
7684         EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7685
7686         if (!rule) {    /*  only global rule supported. */
7687                 EM_DEBUG_LOG("rule is NULL");
7688                 if (err_code != NULL)
7689                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7690                 return false;
7691         }
7692
7693         int rc, rc_2, ret = false;
7694         int error = EMAIL_ERROR_NONE;
7695         DB_STMT hStmt = NULL;
7696         char sql_query_string[QUERY_SIZE] = {0, };
7697
7698         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7699
7700         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7701
7702         char *sql;
7703         char **result;
7704         sql = "SELECT max(rowid) FROM mail_rule_tbl;";
7705
7706         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7707         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7708                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7709
7710         if (NULL == result[1])
7711                 rc = 1;
7712         else
7713                 rc = atoi(result[1])+1;
7714
7715         sqlite3_free_table(result);
7716
7717         rule->rule_id = rc;
7718
7719         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7720                 "INSERT INTO mail_rule_tbl VALUES "
7721                 "(?"            /*  account id */
7722                 ", ?"           /*  rule_id */
7723                 ", ?"           /*  filter_name */
7724                 ", ?"           /*  type */
7725                 ", ?"           /*  value */
7726                 ", ?"           /*  value2 */
7727                 ", ?"           /*  action_type */
7728                 ", ?"           /*  target_mailbox_id */
7729                 ", ?"           /*  flag1 */
7730                 ", ?)");        /*  flag2 */
7731
7732         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc_2);
7733         if (rc_2 != SQLITE_OK) {
7734                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc_2, sqlite3_errmsg(local_db_handle));
7735                 error = EMAIL_ERROR_DB_FAILURE;
7736                 goto FINISH_OFF;
7737         }
7738
7739         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL, rule->account_id);
7740         _bind_stmt_field_data_int(hStmt, RULE_ID_IDX_IN_MAIL_RULE_TBL, rule->rule_id);
7741         _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);
7742         _bind_stmt_field_data_int(hStmt, TYPE_IDX_IN_MAIL_RULE_TBL, rule->type);
7743         _bind_stmt_field_data_string(hStmt, VALUE_IDX_IN_MAIL_RULE_TBL, (char*)rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7744         _bind_stmt_field_data_string(hStmt, VALUE2_IDX_IN_MAIL_RULE_TBL, (char*)rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7745         _bind_stmt_field_data_int(hStmt, ACTION_TYPE_IDX_IN_MAIL_RULE_TBL, rule->action_type);
7746         _bind_stmt_field_data_int(hStmt, TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL, rule->target_mailbox_id);
7747         _bind_stmt_field_data_int(hStmt, FLAG1_IDX_IN_MAIL_RULE_TBL, rule->flag1);
7748         _bind_stmt_field_data_int(hStmt, FLAG2_IDX_IN_MAIL_RULE_TBL, rule->flag2);
7749
7750         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7751         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7752                 ("sqlite3_step fail:%d", rc));
7753
7754         ret = true;
7755
7756 FINISH_OFF:
7757         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7758         if (hStmt != NULL) {
7759                 rc = sqlite3_finalize(hStmt);
7760                 if (rc != SQLITE_OK) {
7761                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7762                         error = EMAIL_ERROR_DB_FAILURE;
7763                 }
7764         }
7765
7766         if (err_code != NULL)
7767                 *err_code = error;
7768
7769         EM_DEBUG_FUNC_END("ret [%d]", ret);
7770         return ret;
7771 }
7772
7773 INTERNAL_FUNC int emstorage_delete_rule(char *multi_user_name, int rule_id, int transaction, int *err_code)
7774 {
7775         EM_DEBUG_FUNC_BEGIN("rule_id[%d], transaction[%d], err_code[%p]", rule_id, transaction, err_code);
7776
7777         if (rule_id <= 0) {             /*  only global rule supported. */
7778                 EM_DEBUG_EXCEPTION("rule_id[%d]", rule_id);
7779
7780                 if (err_code != NULL)
7781                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7782                 return false;
7783         }
7784
7785         int rc, ret = false;
7786         int error = EMAIL_ERROR_NONE;
7787         char sql_query_string[QUERY_SIZE] = {0, };
7788         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7789         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7790
7791         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7792         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7793         if (error != EMAIL_ERROR_NONE) {
7794                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7795                         goto FINISH_OFF;
7796         }
7797
7798         rc = sqlite3_changes(local_db_handle);
7799         if (rc == 0) {
7800                 EM_DEBUG_EXCEPTION(" no matched rule found...");
7801
7802                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7803                 goto FINISH_OFF;
7804         }
7805
7806         ret = true;
7807
7808 FINISH_OFF:
7809         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7810
7811         if (err_code != NULL)
7812                 *err_code = error;
7813
7814         EM_DEBUG_FUNC_END("ret [%d]", ret);
7815         return ret;
7816 }
7817
7818 INTERNAL_FUNC int emstorage_free_rule(emstorage_rule_tbl_t** rule_list, int count, int *err_code)
7819 {
7820         EM_DEBUG_FUNC_BEGIN("rule_list[%p], conut[%d], err_code[%p]", rule_list, count, err_code);
7821
7822         int ret = false;
7823         int error = EMAIL_ERROR_NONE;
7824
7825         if (count > 0) {
7826                 if (!rule_list || !*rule_list) {
7827                         EM_DEBUG_EXCEPTION(" rule_list[%p], conut[%d]", rule_list, count);
7828
7829                         error = EMAIL_ERROR_INVALID_PARAM;
7830                         goto FINISH_OFF;
7831                 }
7832
7833                 emstorage_rule_tbl_t* p = *rule_list;
7834                 int i = 0;
7835
7836                 for (; i < count; i++) {
7837                         EM_SAFE_FREE(p[i].value);
7838                 }
7839
7840                 EM_SAFE_FREE(p); *rule_list = NULL;
7841         }
7842
7843 FINISH_OFF:
7844         if (err_code != NULL)
7845                 *err_code = error;
7846
7847         EM_DEBUG_FUNC_END("ret [%d]", ret);
7848         return ret;
7849 }
7850
7851 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)
7852 {
7853         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);
7854
7855         if (!total && !unseen) {
7856                 EM_DEBUG_EXCEPTION(" accoun_id[%d], mailbox_id[%d], total[%p], unseen[%p]", account_id, mailbox_id, total, unseen);
7857                 if (err_code != NULL)
7858                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7859                 return false;
7860         }
7861
7862         int rc = -1, ret = false;
7863         int error = EMAIL_ERROR_NONE;
7864         DB_STMT hStmt = NULL;
7865         char sql_query_string[QUERY_SIZE] = {0, };
7866         char *replaced_mailbox_name = NULL;
7867
7868         memset(&sql_query_string, 0x00, sizeof(sql_query_string));
7869         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7870         EMSTORAGE_START_READ_TRANSACTION(transaction);
7871
7872         if (total) {
7873                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl");
7874
7875                 if (account_id != ALL_ACCOUNT) {
7876                         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);
7877                         if (mailbox_id)
7878                                 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);
7879                 } else if (mailbox_id)
7880                         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);
7881
7882 #ifdef USE_GET_RECORD_COUNT_API
7883                 char **result;
7884
7885                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7886                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF2; },
7887                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7888
7889                 *total = atoi(result[1]);
7890                 sqlite3_free_table(result);
7891 #else
7892
7893                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7894                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF2; },
7895                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7896
7897                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7898                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF2; },
7899                         ("sqlite3_step fail:%d", rc));
7900                 _get_stmt_field_data_int(hStmt, total, 0);
7901 #endif          /*  USE_GET_RECORD_COUNT_API */
7902         }
7903
7904         if (unseen) {
7905                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl WHERE flags_seen_field = 0");               /*  fSEEN = 0x01 */
7906
7907                 if (account_id != ALL_ACCOUNT)
7908                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND account_id = %d", account_id);
7909
7910                 if (mailbox_id) {
7911                         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);
7912                 } else
7913                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_type NOT IN (3, 5)");
7914
7915                 char **result;
7916                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7917                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7918                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7919
7920                 *unseen = atoi(result[1]);
7921                 sqlite3_free_table(result);
7922
7923         }
7924 FINISH_OFF:
7925         ret = true;
7926
7927 FINISH_OFF2:
7928
7929 #ifndef USE_PREPARED_QUERY_
7930         if (hStmt != NULL) {
7931                 rc = sqlite3_finalize(hStmt);
7932                 if (rc != SQLITE_OK) {
7933                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7934                         error = EMAIL_ERROR_DB_FAILURE;
7935                 }
7936         }
7937 #endif
7938
7939         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7940
7941         EM_SAFE_FREE(replaced_mailbox_name);
7942
7943         if (err_code != NULL)
7944                 *err_code = error;
7945
7946         EM_DEBUG_FUNC_END("ret [%d]", ret);
7947         return ret;
7948 }
7949
7950 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)
7951 {
7952         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
7953
7954         if (mail_id <= 0 || !mail) {
7955                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
7956                 if (err_code != NULL)
7957                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7958                 return false;
7959         }
7960
7961         int col_index = 0;
7962         emstorage_mail_tbl_t* p_data_tbl = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
7963
7964         if (p_data_tbl == NULL) {
7965                 EM_DEBUG_EXCEPTION("malloc failed...");
7966                 if (err_code != NULL)
7967                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
7968                 return false;
7969         }
7970
7971         memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
7972         DB_STMT hStmt = NULL;
7973         char sql_query_string[QUERY_SIZE] = {0, };
7974
7975         int rc, ret = false;
7976         int error = EMAIL_ERROR_NONE;
7977
7978         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7979         EMSTORAGE_START_READ_TRANSACTION(transaction);
7980
7981         switch (type) {
7982                 case RETRIEVE_SUMMARY:
7983                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7984                                 "SELECT account_id, "
7985                                 "mail_id, "
7986                                 "mailbox_id, "
7987                                 "server_mail_status, "
7988                                 "server_mailbox_name, "
7989                                 "server_mail_id, "
7990                                 "file_path_plain, "
7991                                 "file_path_html,"
7992                                 "file_path_mime_entity, "
7993                                 "flags_seen_field, "
7994                                 "save_status, "
7995                                 "lock_status, "
7996                                 "thread_id, "
7997                                 "thread_item_count "
7998                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
7999                         break;
8000
8001                 case RETRIEVE_FIELDS_FOR_DELETE:
8002                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8003                                 "SELECT account_id, "
8004                                 "mail_id, "
8005                                 "server_mail_status, "
8006                                 "server_mailbox_name, "
8007                                 "server_mail_id "
8008                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8009                         break;
8010
8011                 case RETRIEVE_ACCOUNT:
8012                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8013                                 "SELECT account_id "
8014                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8015                         break;
8016
8017                 case RETRIEVE_FLAG:
8018                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8019                                 "SELECT account_id, "
8020                                 "flags_seen_field, "
8021                                 "thread_id, "
8022                                 "mailbox_id "
8023                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8024                         break;
8025
8026                 default:
8027                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8028                         error = EMAIL_ERROR_INVALID_PARAM;
8029                         goto FINISH_OFF;
8030         }
8031
8032         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8033
8034         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8035         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8036                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8037
8038
8039         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8040         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8041                 ("sqlite3_step fail:%d", rc));
8042
8043         if (rc == SQLITE_DONE) {
8044                 EM_DEBUG_LOG("no matched mail found...");
8045                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8046                 goto FINISH_OFF;
8047         }
8048         switch (type) {
8049                 case RETRIEVE_SUMMARY:
8050                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8051                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8052                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8053                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8054                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8055                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8056                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, col_index++);
8057                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, col_index++);
8058                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, col_index++);
8059                         _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8060                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), col_index++);
8061                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), col_index++);
8062                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8063                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_item_count), col_index++);
8064                         break;
8065
8066                 case RETRIEVE_FIELDS_FOR_DELETE:
8067                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8068                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8069                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8070                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8071                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8072                         break;
8073
8074                 case RETRIEVE_ACCOUNT:
8075                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8076                         break;
8077
8078                 case RETRIEVE_FLAG:
8079                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8080                         _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8081                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8082                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8083                         break;
8084         }
8085
8086         ret = true;
8087
8088 FINISH_OFF:
8089         if (ret == true)
8090                 *mail = p_data_tbl;
8091         else if (p_data_tbl != NULL)
8092                 emstorage_free_mail(&p_data_tbl,  1, NULL);
8093
8094         if (hStmt != NULL) {
8095                 rc = sqlite3_finalize(hStmt);
8096                 if (rc != SQLITE_OK) {
8097                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8098                         error = EMAIL_ERROR_DB_FAILURE;
8099                 }
8100         }
8101
8102
8103         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8104
8105         if (err_code != NULL)
8106                 *err_code = error;
8107
8108         EM_DEBUG_FUNC_END("ret [%d]", ret);
8109         return ret;
8110 }
8111
8112 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)
8113 {
8114         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);
8115
8116         int ret = false;
8117         int error = EMAIL_ERROR_NONE;
8118         int query_string_length = 0;
8119         int i = 0, item_count = 0, rc = -1, field_count, col_index, cur_sql_query_string = 0;
8120         char **result = NULL;
8121         char *sql_query_string = NULL;
8122         emstorage_mail_tbl_t* p_data_tbl = NULL;
8123         sqlite3 *local_db_handle = NULL;
8124
8125         if (number_of_mails <= 0 || !mail_ids) {
8126                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8127                 if (err_code != NULL)
8128                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8129                 return false;
8130         }
8131
8132         p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * number_of_mails);
8133
8134         query_string_length = (sizeof(char) * 8 * number_of_mails) + 512;
8135         sql_query_string = (char*)em_malloc(query_string_length);
8136
8137         if (p_data_tbl == NULL || sql_query_string == NULL) {
8138                 EM_DEBUG_EXCEPTION("malloc failed...");
8139
8140                 EM_SAFE_FREE(p_data_tbl);
8141                 EM_SAFE_FREE(sql_query_string);
8142
8143                 if (err_code != NULL)
8144                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8145                 return false;
8146         }
8147
8148         local_db_handle = emstorage_get_db_connection(multi_user_name);
8149
8150         EMSTORAGE_START_READ_TRANSACTION(transaction);
8151
8152         switch (type) {
8153                 case RETRIEVE_SUMMARY:
8154                         cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8155                                 "SELECT account_id, "
8156                                 "mail_id, "
8157                                 "mailbox_id, "
8158                                 "server_mail_status, "
8159                                 "server_mailbox_name, "
8160                                 "server_mail_id, "
8161                                 "file_path_plain, "
8162                                 "file_path_html, "
8163                                 "file_path_mime_entity, "
8164                                 "subject, "
8165                                 "flags_seen_field, "
8166                                 "save_status, "
8167                                 "lock_status, "
8168                                 "thread_id, "
8169                                 "thread_item_count "
8170                                 "FROM mail_tbl WHERE mail_id in (");
8171                         field_count = 15;
8172                         break;
8173
8174                 case RETRIEVE_FIELDS_FOR_DELETE:
8175                         cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8176                                 "SELECT account_id, "
8177                                 "mail_id, "
8178                                 "server_mail_status, "
8179                                 "server_mailbox_name, "
8180                                 "server_mail_id "
8181                                 "FROM mail_tbl WHERE mail_id in (");
8182                         field_count = 5;
8183                         break;
8184
8185                 case RETRIEVE_ACCOUNT:
8186                         cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8187                                 "SELECT account_id FROM mail_tbl WHERE mail_id in (");
8188                         field_count = 1;
8189                         break;
8190
8191                 case RETRIEVE_FLAG:
8192                         cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8193                                 "SELECT account_id, "
8194                                 "mail_id, "
8195                                 "mailbox_id, "
8196                                 "flags_seen_field, "
8197                                 "thread_id "
8198                                 "FROM mail_tbl WHERE mail_id in (");
8199                         field_count = 5;
8200                         break;
8201
8202                 default:
8203                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8204                         error = EMAIL_ERROR_INVALID_PARAM;
8205                         goto FINISH_OFF;
8206         }
8207
8208         for (i = 0; i < number_of_mails; i++)
8209                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_string_length, "%d,", mail_ids[i]);
8210         sql_query_string[EM_SAFE_STRLEN(sql_query_string) - 1] = ')';
8211
8212         EM_DEBUG_LOG_SEC("Query [%s], Length [%d]", sql_query_string, EM_SAFE_STRLEN(sql_query_string));
8213
8214         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &item_count, 0, NULL), rc);
8215         if (SQLITE_OK != rc && -1 != rc) {
8216                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
8217                 error = EMAIL_ERROR_DB_FAILURE;
8218                 goto FINISH_OFF;
8219         }
8220
8221         if (rc == SQLITE_DONE) {
8222                 EM_DEBUG_LOG("no matched mail found...");
8223                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8224                 goto FINISH_OFF;
8225         }
8226
8227         EM_DEBUG_LOG("item_count [%d]", item_count);
8228
8229         if (number_of_mails != item_count) {
8230                 EM_DEBUG_EXCEPTION("Can't find all emails");
8231                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8232                 goto FINISH_OFF;
8233         }
8234
8235         col_index = field_count;
8236
8237         for (i = 0; i < item_count; i++)        {
8238                 switch (type) {
8239                         case RETRIEVE_SUMMARY:
8240                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8241                                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8242                                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8243                                 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8244                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8245                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8246                                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
8247                                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
8248                                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
8249                                 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 0, col_index++);
8250                                 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8251                                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
8252                                 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
8253                                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8254                                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
8255                                 break;
8256
8257                         case RETRIEVE_FIELDS_FOR_DELETE:
8258                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8259                                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8260                                 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8261                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8262                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8263                                 break;
8264
8265                         case RETRIEVE_ACCOUNT:
8266                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8267                                 break;
8268
8269                         case RETRIEVE_FLAG:
8270                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8271                                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8272                                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8273                                 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8274                                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8275                                 break;
8276                 }
8277         }
8278
8279         ret = true;
8280
8281 FINISH_OFF:
8282         if (ret == true)
8283                 *mail = p_data_tbl;
8284         else
8285                 emstorage_free_mail(&p_data_tbl, number_of_mails, NULL);
8286
8287         if (result)
8288                 sqlite3_free_table(result);
8289
8290         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8291
8292         EM_SAFE_FREE(sql_query_string);
8293
8294         if (err_code != NULL)
8295                 *err_code = error;
8296
8297         EM_DEBUG_FUNC_END("ret [%d]", ret);
8298         return ret;
8299 }
8300
8301 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)
8302 {
8303         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8304
8305         if (mail_id <= 0 || !mail) {
8306                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
8307                 if (err_code != NULL)
8308                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8309                 return false;
8310         }
8311
8312         int ret = false, error = EMAIL_ERROR_NONE, count;
8313         char conditional_clause[QUERY_SIZE] = {0, };
8314         emstorage_mail_tbl_t* p_data_tbl = NULL;
8315
8316         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8317         EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8318
8319         if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8320                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8321                 goto FINISH_OFF;
8322         }
8323
8324         ret = true;
8325
8326 FINISH_OFF:
8327         if (ret == true)
8328                 *mail = p_data_tbl;
8329         else if (p_data_tbl != NULL)
8330                 emstorage_free_mail(&p_data_tbl, 1, &error);
8331
8332         if (err_code != NULL)
8333                 *err_code = error;
8334
8335         EM_DEBUG_FUNC_END("ret [%d]", ret);
8336         return ret;
8337 }
8338
8339 #ifdef __FEATURE_BODY_SEARCH__
8340 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)
8341 {
8342         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
8343
8344         if (mail_id <= 0 || !mail_text) {
8345                 EM_DEBUG_EXCEPTION("mail_id[%d], mail_text[%p]", mail_id, mail_text);
8346                 if (err_code != NULL)
8347                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8348                 return false;
8349         }
8350
8351         int ret = false;
8352         int error = EMAIL_ERROR_NONE;
8353         int count = 0;
8354         char conditional_clause[QUERY_SIZE] = {0, };
8355         emstorage_mail_text_tbl_t *p_data_tbl = NULL;
8356
8357         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8358         EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8359
8360         if (!emstorage_query_mail_text_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8361                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8362                 goto FINISH_OFF;
8363         }
8364
8365         ret = true;
8366
8367 FINISH_OFF:
8368         if (ret == true)
8369                 *mail_text = p_data_tbl;
8370         else if (p_data_tbl != NULL)
8371                 emstorage_free_mail_text(&p_data_tbl, 1, &error);
8372
8373         if (err_code != NULL)
8374                 *err_code = error;
8375
8376         EM_DEBUG_FUNC_END("ret [%d]", ret);
8377         return ret;
8378 }
8379 #endif
8380
8381 INTERNAL_FUNC int emstorage_mail_search_start(char *multi_user_name,
8382                                                                                         emstorage_search_filter_t *search,
8383                                                                                         int account_id,
8384                                                                                         int mailbox_id,
8385                                                                                         int sorting,
8386                                                                                         DB_STMT *search_handle,
8387                                                                                         int *searched,
8388                                                                                         int transaction,
8389                                                                                         int *err_code)
8390 {
8391         EM_DEBUG_FUNC_BEGIN("search[%p], account_id[%d], mailbox_id[%d], sorting[%d], "
8392                                                 "search_handle[%p], searched[%p], transaction[%d], err_code[%p]",
8393                                                 search, account_id, mailbox_id, sorting, search_handle,
8394                                                 searched, transaction, err_code);
8395
8396         if (!search_handle || !searched) {
8397                 if (err_code != NULL)
8398                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8399                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8400                 EM_DEBUG_FUNC_END("false");
8401                 return false;
8402         }
8403
8404         emstorage_search_filter_t* p = search;
8405         int error = EMAIL_ERROR_NONE;
8406         DB_STMT hStmt = NULL;
8407         char sql_query_string[QUERY_SIZE] = {0, };
8408         int rc, ret = false;
8409         int and = false, mail_count = 0;
8410
8411         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8412         EMSTORAGE_START_READ_TRANSACTION(transaction);
8413
8414         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl");
8415
8416         if (account_id != ALL_ACCOUNT) {
8417                 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);
8418                 and = true;
8419         }
8420
8421         if (mailbox_id) {
8422                 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);
8423                 and = true;
8424         }
8425
8426         while (p) {
8427                 if (p->key_type) {
8428                         if (!strncmp(p->key_type, "subject", strlen("subject"))) {
8429                                 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);
8430                                 and = true;
8431                         } else if (!strncmp(p->key_type, "full_address_from", strlen("full_address_from"))) {
8432                                 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);
8433                                 and = true;
8434                         } else if (!strncmp(p->key_type, "full_address_to", strlen("full_address_to"))) {
8435                                 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);
8436                                 and = true;
8437                         } else if (!strncmp(p->key_type, "email_address", strlen("email_address"))) {
8438                                 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);
8439                                 and = true;
8440                         }
8441                         p = p->next;
8442                 }
8443         }
8444
8445         if (sorting)
8446                 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");
8447
8448         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
8449
8450
8451         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8452         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8453                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8454
8455
8456         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8457         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8458                 ("sqlite3_step fail:%d", rc));
8459
8460         char **result;
8461
8462         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &mail_count, NULL, NULL), rc);
8463         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
8464                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8465
8466         sqlite3_free_table(result);
8467
8468         ret = true;
8469
8470 FINISH_OFF:
8471         if (ret == true) {
8472                 *search_handle = hStmt;
8473                 *searched = mail_count;
8474                 EM_DEBUG_LOG("mail_count [%d]", mail_count);
8475         } else {
8476                 if (hStmt != NULL) {
8477                         rc = sqlite3_finalize(hStmt);
8478                         if (rc != SQLITE_OK) {
8479                                 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8480                                 error = EMAIL_ERROR_DB_FAILURE;
8481                         }
8482                 }
8483
8484                 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8485         }
8486
8487         if (err_code != NULL)
8488                 *err_code = error;
8489
8490         EM_DEBUG_FUNC_END("ret [%d]", ret);
8491         return ret;
8492 }
8493
8494 INTERNAL_FUNC int emstorage_mail_search_result(DB_STMT search_handle, emstorage_mail_field_type_t type, void** data, int transaction, int *err_code)
8495 {
8496         EM_DEBUG_FUNC_BEGIN("search_handle[%d], type[%d], data[%p], transaction[%d], err_code[%p]", search_handle, type, data, transaction, err_code);
8497
8498         if (search_handle == 0 || !data) {
8499                 EM_DEBUG_EXCEPTION(" search_handle[%d], type[%d], data[%p]", search_handle, type, data);
8500
8501                 if (err_code != NULL)
8502                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8503                 return false;
8504         }
8505
8506         emstorage_mail_tbl_t* p_data_tbl = NULL;
8507         DB_STMT hStmt = search_handle;
8508         int rc, ret = false;
8509         int error = EMAIL_ERROR_NONE;
8510
8511         switch (type) {
8512                 case RETRIEVE_ID:
8513                         _get_stmt_field_data_int(hStmt, (int *)data, MAIL_ID_IDX_IN_MAIL_TBL);
8514                         break;
8515
8516                 case RETRIEVE_ENVELOPE:
8517                 case RETRIEVE_ALL:
8518                         if (!(p_data_tbl = em_malloc(sizeof(emstorage_mail_tbl_t)))) {
8519                                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
8520                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
8521                                 goto FINISH_OFF;
8522                         }
8523
8524                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8525                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8526                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_size), MAIL_SIZE_IDX_IN_MAIL_TBL);
8527                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8528                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_from), 1, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL);
8529                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_to), 1, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL);
8530                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject), 1, SUBJECT_IDX_IN_MAIL_TBL);
8531                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->body_download_status), BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL);
8532                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL);
8533                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8534                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8535                         _get_stmt_field_data_time_t(hStmt, &(p_data_tbl->date_time), DATETIME_IDX_IN_MAIL_TBL);
8536                         _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL);
8537                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->DRM_status), DRM_STATUS_IDX_IN_MAIL_TBL);
8538                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->priority), PRIORITY_IDX_IN_MAIL_TBL);
8539                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), SAVE_STATUS_IDX_IN_MAIL_TBL);
8540                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), LOCK_STATUS_IDX_IN_MAIL_TBL);
8541                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->report_status), REPORT_STATUS_IDX_IN_MAIL_TBL);
8542                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8543                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->meeting_request_status), MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL);
8544                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->message_class), MESSAGE_CLASS_IDX_IN_MAIL_TBL);
8545                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->digest_type), DIGEST_TYPE_IDX_IN_MAIL_TBL);
8546                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->smime_type), SMIME_TYPE_IDX_IN_MAIL_TBL);
8547                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->scheduled_sending_time), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8548                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->remaining_resend_times), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8549                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->tag_id), TAG_ID_IDX_IN_MAIL_TBL);
8550                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->eas_data_length), EAS_DATA_LENGTH_IDX_IN_MAIL_TBL);
8551                         _get_stmt_field_data_blob(hStmt, (void**)&(p_data_tbl->eas_data), EAS_DATA_IDX_IN_MAIL_TBL);
8552
8553                         if (type == RETRIEVE_ALL) {
8554                                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8555                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8556                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_reply), 1, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL);
8557                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_cc), 1, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL);
8558                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_bcc), 1, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL);
8559                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_return), 1, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL);
8560                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->message_id), 0, MESSAGE_ID_IDX_IN_MAIL_TBL);
8561                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8562                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8563                                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_count), ATTACHMENT_COUNT_IDX_IN_MAIL_TBL);
8564                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8565                                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->replied_time), REPLIED_TIME_IDX_IN_MAIL_TBL);
8566                                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->forwarded_time), FORWARDED_TIME_IDX_IN_MAIL_TBL);
8567                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->default_charset), 0, DEFAULT_CHARSET_IDX_IN_MAIL_TBL);
8568                         }
8569
8570                         if (p_data_tbl->body_download_status) {
8571                                 struct stat buf;
8572
8573                                 if (p_data_tbl->file_path_html) {
8574                                         if (stat(p_data_tbl->file_path_html, &buf) == -1)
8575                                                 p_data_tbl->body_download_status = 0;
8576                                 } else if (p_data_tbl->file_path_plain) {
8577                                         if (stat(p_data_tbl->file_path_plain, &buf) == -1)
8578                                                 p_data_tbl->body_download_status = 0;
8579                                 } else
8580                                         p_data_tbl->body_download_status = 0;
8581                         }
8582
8583                         *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8584                         break;
8585
8586                 case RETRIEVE_SUMMARY:
8587                         if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8588                                 EM_DEBUG_EXCEPTION(" malloc failed...");
8589
8590                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
8591                                 goto FINISH_OFF;
8592                         }
8593
8594                         memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8595
8596                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8597                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8598                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8599                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8600                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8601
8602                         *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8603                         break;
8604
8605                 case RETRIEVE_ADDRESS:
8606                         if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8607                                 EM_DEBUG_EXCEPTION(" malloc failed...");
8608                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
8609                                 goto FINISH_OFF;
8610                         }
8611
8612                         memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8613                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8614                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8615                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8616                         *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8617                         break;
8618
8619                 default:
8620                         break;
8621         }
8622
8623         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8624         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8625                 ("sqlite3_step fail:%d", rc));
8626
8627         ret = true;
8628
8629 FINISH_OFF:
8630
8631         if (err_code != NULL)
8632                 *err_code = error;
8633
8634         if (ret == false && p_data_tbl)
8635                 emstorage_free_mail(&p_data_tbl, 1, NULL);
8636
8637         EM_DEBUG_FUNC_END("ret [%d]", ret);
8638         return ret;
8639 }
8640
8641 INTERNAL_FUNC int emstorage_mail_search_end(DB_STMT search_handle, int transaction, int *err_code)
8642 {
8643         EM_DEBUG_FUNC_BEGIN("search_handle[%d], transaction[%d], err_code[%p]", search_handle, transaction, err_code);
8644
8645         int error = EMAIL_ERROR_NONE;
8646         int rc, ret = false;
8647
8648         if (search_handle == 0) {
8649                 EM_DEBUG_EXCEPTION(" search_handle[%d]", search_handle);
8650                 error = EMAIL_ERROR_INVALID_PARAM;
8651                 goto FINISH_OFF;
8652         }
8653
8654         DB_STMT hStmt = search_handle;
8655
8656         rc = sqlite3_finalize(hStmt);
8657         if (rc != SQLITE_OK) {
8658                 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8659                 error = EMAIL_ERROR_DB_FAILURE;
8660         }
8661
8662         ret = true;
8663
8664 FINISH_OFF:
8665         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8666
8667         if (err_code != NULL)
8668                 *err_code = error;
8669
8670         EM_DEBUG_FUNC_END("ret [%d]", ret);
8671         return ret;
8672 }
8673
8674 INTERNAL_FUNC int emstorage_change_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t *mail, int transaction, int *err_code)
8675 {
8676         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8677
8678         if (mail_id <= 0 || !mail) {
8679                 EM_DEBUG_EXCEPTION(" mail_id[%d], mail[%p]", mail_id, mail);
8680
8681                 if (err_code != NULL)
8682                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8683                 return false;
8684         }
8685
8686         DB_STMT hStmt = NULL;
8687         char sql_query_string[QUERY_SIZE] = {0, };
8688         int rc = -1;
8689         int ret = false;
8690         int error = EMAIL_ERROR_NONE;
8691         int i = 0;
8692         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8693         char mailbox_id_param_string[10] = {0,};
8694
8695         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
8696
8697         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8698                 "UPDATE mail_tbl SET"
8699                 "  mail_id = ?"
8700                 ", account_id = ?"
8701                 ", mailbox_id = ?"
8702                 ", mail_size = ?"
8703                 ", server_mail_status = ?"
8704                 ", server_mailbox_name = ?"
8705                 ", server_mail_id = ?"
8706                 ", reference_mail_id = ?"
8707                 ", full_address_from = ?"
8708                 ", full_address_reply = ?"  /* 10 */
8709                 ", full_address_to = ?"
8710                 ", full_address_cc = ?"
8711                 ", full_address_bcc = ?"
8712                 ", full_address_return = ?"
8713                 ", subject = ?"
8714                 ", body_download_status = ?"
8715                 ", file_path_plain = ?"
8716                 ", file_path_html = ?"
8717                 ", file_path_mime_entity = ?"
8718                 ", date_time = ?"
8719                 ", flags_seen_field      = ?"
8720                 ", flags_deleted_field   = ?"
8721                 ", flags_flagged_field   = ?"
8722                 ", flags_answered_field  = ?"
8723                 ", flags_recent_field    = ?"
8724                 ", flags_draft_field     = ?"
8725                 ", flags_forwarded_field = ?"
8726                 ", DRM_status = ?"
8727                 ", priority = ?"
8728                 ", save_status = ?"
8729                 ", lock_status = ?"
8730                 ", message_id = ?"
8731                 ", report_status = ?"
8732                 ", preview_text = ?"
8733                 ", smime_type = ?"
8734                 ", scheduled_sending_time = ?"
8735                 ", remaining_resend_times = ?"
8736                 ", tag_id = ?"
8737                 ", replied_time = ?"
8738                 ", forwarded_time = ?"
8739                 ", default_charset = ?"
8740                 ", eas_data_length = ?"
8741                 ", eas_data = ?"
8742                 " WHERE mail_id = %d AND account_id != 0 "
8743                 , mail_id);
8744
8745
8746         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8747         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8748                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8749
8750         _bind_stmt_field_data_int(hStmt, i++, mail->mail_id);
8751         _bind_stmt_field_data_int(hStmt, i++, mail->account_id);
8752         _bind_stmt_field_data_int(hStmt, i++, mail->mailbox_id);
8753         _bind_stmt_field_data_int(hStmt, i++, mail->mail_size);
8754         _bind_stmt_field_data_int(hStmt, i++, mail->server_mail_status);
8755         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mailbox_name, 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
8756         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mail_id, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
8757         _bind_stmt_field_data_int(hStmt, i++, mail->reference_mail_id);
8758         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
8759         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
8760         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
8761         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
8762         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
8763         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
8764         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8765         _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8766         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8767         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8768         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
8769         _bind_stmt_field_data_int(hStmt, i++, mail->date_time);
8770         _bind_stmt_field_data_char(hStmt, i++, mail->flags_seen_field);
8771         _bind_stmt_field_data_char(hStmt, i++, mail->flags_deleted_field);
8772         _bind_stmt_field_data_char(hStmt, i++, mail->flags_flagged_field);
8773         _bind_stmt_field_data_char(hStmt, i++, mail->flags_answered_field);
8774         _bind_stmt_field_data_char(hStmt, i++, mail->flags_recent_field);
8775         _bind_stmt_field_data_char(hStmt, i++, mail->flags_draft_field);
8776         _bind_stmt_field_data_char(hStmt, i++, mail->flags_forwarded_field);
8777         _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
8778         _bind_stmt_field_data_int(hStmt, i++, mail->priority);
8779         _bind_stmt_field_data_int(hStmt, i++, mail->save_status);
8780         _bind_stmt_field_data_int(hStmt, i++, mail->lock_status);
8781         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->message_id, 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
8782         _bind_stmt_field_data_int(hStmt, i++, mail->report_status);
8783         _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8784         _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
8785         _bind_stmt_field_data_int(hStmt, i++, mail->scheduled_sending_time);
8786         _bind_stmt_field_data_int(hStmt, i++, mail->remaining_resend_times);
8787         _bind_stmt_field_data_int(hStmt, i++, mail->tag_id);
8788         _bind_stmt_field_data_int(hStmt, i++, mail->replied_time);
8789         _bind_stmt_field_data_int(hStmt, i++, mail->forwarded_time);
8790         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
8791         _bind_stmt_field_data_int(hStmt, i++, mail->eas_data_length);
8792         _bind_stmt_field_data_blob(hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
8793
8794         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8795         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8796                 ("sqlite3_step fail:%d", rc));
8797
8798         rc = sqlite3_changes(local_db_handle);
8799         if (rc == 0) {
8800                 EM_DEBUG_LOG(" no matched mail found...");
8801                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8802                 goto FINISH_OFF;
8803         }
8804
8805         ret = true;
8806
8807 FINISH_OFF:
8808         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
8809         if (hStmt != NULL) {
8810                 rc = sqlite3_finalize(hStmt);
8811                 if (rc != SQLITE_OK) {
8812                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8813                         error = EMAIL_ERROR_DB_FAILURE;
8814                 }
8815         }
8816
8817
8818         if (error == EMAIL_ERROR_NONE && mail) {
8819                 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
8820                 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail->mail_id, mailbox_id_param_string, 0))
8821                         EM_DEBUG_EXCEPTION("emcore_notify_storage_eventfailed [NOTI_MAIL_UPDATE]");
8822         }
8823
8824         if (err_code != NULL)
8825                 *err_code = error;
8826
8827         EM_DEBUG_FUNC_END("ret [%d]", ret);
8828         return ret;
8829 }
8830
8831 /**
8832   *  emstorage_clean_save_status(int save_status, int  *err_code) - set the all mail status to the set value
8833   *
8834   *
8835   **/
8836 INTERNAL_FUNC int emstorage_clean_save_status(char *multi_user_name, int save_status, int  *err_code)
8837 {
8838         EM_DEBUG_FUNC_BEGIN("save_status[%d], err_code[%p]", save_status, err_code);
8839
8840         EM_IF_NULL_RETURN_VALUE(err_code, false);
8841
8842         int ret = false;
8843         int error = EMAIL_ERROR_NONE;
8844         int rc = 0;
8845         char sql_query_string[QUERY_SIZE] = {0, };
8846         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8847
8848         memset(sql_query_string, 0x00, sizeof(sql_query_string));
8849         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);
8850
8851         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8852         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
8853         if (error != EMAIL_ERROR_NONE) {
8854                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
8855                         goto FINISH_OFF;
8856         }
8857
8858         rc = sqlite3_changes(local_db_handle);
8859         if (rc == 0) {
8860                 EM_DEBUG_LOG(" No Matched Mail Exists ");
8861                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8862         }
8863
8864         ret = true;
8865
8866 FINISH_OFF:
8867
8868         if (err_code != NULL)
8869                 *err_code = error;
8870
8871         EM_DEBUG_FUNC_END("ret [%d]", ret);
8872         return ret;
8873 }
8874
8875 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)
8876 {
8877         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);
8878         int i = 0;
8879         int error = EMAIL_ERROR_NONE;
8880         int ret = false;
8881         int query_size = 0;
8882         int cur_mail_id_string = 0;
8883         int mail_id_string_buffer_length = 0;
8884         int parameter_string_length = 0;
8885         char  *sql_query_string = NULL;
8886         char *mail_id_string_buffer = NULL;
8887         char *parameter_string = NULL;
8888         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8889         email_mail_attribute_type target_mail_attribute_type = 0;
8890
8891         if (!mail_ids  || !field_name || account_id == 0) {
8892                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8893                 if (err_code != NULL)
8894                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8895                 return false;
8896         }
8897
8898         if ((error = emcore_get_attribute_type_by_mail_field_name(field_name, &target_mail_attribute_type)) != EMAIL_ERROR_NONE) {
8899                 EM_DEBUG_EXCEPTION("emstorageemcore_get_attribute_type_by_mail_field_name failed [%d]", error);
8900                 if (err_code != NULL)
8901                         *err_code = error;
8902                 return false;
8903         }
8904
8905         /* Generating mail id list string */
8906         mail_id_string_buffer_length = MAIL_ID_STRING_LENGTH * mail_ids_count;
8907
8908         mail_id_string_buffer = em_malloc(mail_id_string_buffer_length);
8909
8910         if (!mail_id_string_buffer) {
8911                 EM_DEBUG_EXCEPTION("em_mallocfailed");
8912                 if (err_code != NULL)
8913                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8914                 return false;
8915         }
8916
8917         for (i = 0; i < mail_ids_count; i++)
8918                 cur_mail_id_string += SNPRINTF_OFFSET(mail_id_string_buffer, cur_mail_id_string, mail_id_string_buffer_length, "%d,", mail_ids[i]);
8919
8920         if (EM_SAFE_STRLEN(mail_id_string_buffer) > 1)
8921                 mail_id_string_buffer[EM_SAFE_STRLEN(mail_id_string_buffer) - 1] = NULL_CHAR;
8922
8923         /* Generating notification parameter string */
8924         parameter_string_length = mail_id_string_buffer_length + EM_SAFE_STRLEN(field_name) + 2;
8925         parameter_string = em_malloc(parameter_string_length);
8926
8927         if (!parameter_string) {
8928                 EM_DEBUG_EXCEPTION("em_mallocfailed");
8929                 if (err_code != NULL)
8930                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8931                 EM_SAFE_FREE(mail_id_string_buffer);
8932                 return false;
8933         }
8934
8935         SNPRINTF(parameter_string, parameter_string_length, "%s%c%s", field_name, 0x01, mail_id_string_buffer);
8936         query_size = EM_SAFE_STRLEN(mail_id_string_buffer) + EM_SAFE_STRLEN(field_name) + 250;
8937
8938         sql_query_string = em_malloc(query_size);
8939         if (sql_query_string == NULL) {
8940                 EM_DEBUG_EXCEPTION("em_mallocfailed");
8941                 error = EMAIL_ERROR_OUT_OF_MEMORY;
8942                 goto FINISH_OFF;
8943         }
8944         /* Write query string */
8945         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);
8946
8947         EM_DEBUG_LOG_DEV("sql_query_string [%s]", sql_query_string);
8948
8949         /* Execute query */
8950         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
8951         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
8952         if (error != EMAIL_ERROR_NONE) {
8953                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
8954                         goto FINISH_OFF;
8955         }
8956
8957         if (sqlite3_changes(local_db_handle) == 0)
8958                 EM_DEBUG_LOG("no mail matched...");
8959
8960         ret = true;
8961
8962 FINISH_OFF:
8963         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
8964
8965         if (error == EMAIL_ERROR_NONE && parameter_string) {
8966                 if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, target_mail_attribute_type, parameter_string, value))
8967                         EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_MAIL_FIELD_UPDATE [%s,%d]", field_name, value);
8968         }
8969
8970         EM_SAFE_FREE(mail_id_string_buffer);
8971         EM_SAFE_FREE(parameter_string);
8972         EM_SAFE_FREE(sql_query_string);
8973
8974
8975         if (err_code != NULL)
8976                 *err_code = error;
8977
8978         EM_DEBUG_FUNC_END("error [%d]", error);
8979         return ret;
8980 }
8981
8982 #ifdef __FEATURE_BODY_SEARCH__
8983 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)
8984 {
8985         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
8986
8987         if (mail_id <= 0 || !mail_text) {
8988                 EM_DEBUG_EXCEPTION(" mail_id[%d], mail_text[%p]", mail_id, mail_text);
8989                 if (err_code != NULL)
8990                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8991                 return false;
8992         }
8993
8994         int ret = false;
8995         int error = EMAIL_ERROR_NONE;
8996         DB_STMT hStmt = NULL;
8997         char sql_query_string[QUERY_SIZE] = {0, };
8998
8999         int i = 0;
9000         int rc = 0;
9001
9002         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9003         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9004
9005         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9006                 "UPDATE mail_text_tbl SET"
9007                 " body_text = ?"
9008                 " WHERE mail_id = %d AND account_id != 0"
9009                 , mail_id);
9010         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9011
9012         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9013         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9014                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9015
9016         i = 0;
9017         _bind_stmt_field_data_string(hStmt, i++, (char *)mail_text->body_text, 0, -1);
9018
9019         if (hStmt != NULL) {
9020                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9021                 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9022                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9023
9024                 rc = sqlite3_changes(local_db_handle);
9025                 if (rc == 0) {
9026                         EM_DEBUG_LOG(" no matched mail found...");
9027                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
9028                         goto FINISH_OFF;
9029                 }
9030         }
9031
9032         ret = true;
9033
9034 FINISH_OFF:
9035         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9036
9037         if (hStmt != NULL) {
9038                 rc = sqlite3_finalize(hStmt);
9039                 if (rc != SQLITE_OK) {
9040                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9041                         error = EMAIL_ERROR_DB_FAILURE;
9042                 }
9043                 hStmt = NULL;
9044         }
9045
9046         if (err_code != NULL)
9047                 *err_code = error;
9048
9049         EM_DEBUG_FUNC_END("ret [%d]", ret);
9050         return ret;
9051 }
9052 #endif
9053
9054 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)
9055 {
9056         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
9057
9058         int ret = false;
9059         int error = EMAIL_ERROR_NONE;
9060         int move_flag = 0;
9061         int rc = 0;
9062         int i = 0;
9063         int mailbox_id = 0;
9064         DB_STMT hStmt = NULL;
9065         char sql_query_string[QUERY_SIZE] = {0, };
9066         char mailbox_id_param_string[10] = {0,};
9067         sqlite3 *local_db_handle = NULL;
9068
9069         if (mail_id <= 0 || !mail) {
9070                 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], mail[%p]", mail_id, type, mail);
9071                 if (err_code != NULL)
9072                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9073                 return false;
9074         }
9075
9076         local_db_handle = emstorage_get_db_connection(multi_user_name);
9077
9078         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9079
9080         switch (type) {
9081                 case APPEND_BODY:
9082                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9083                                 "UPDATE mail_tbl SET"
9084                                 "  body_download_status = ?"
9085                                 ", file_path_plain = ?"
9086                                 ", file_path_html = ?"
9087                                 ", file_path_mime_entity = ?"
9088                                 ", flags_seen_field      = ?"
9089                                 ", flags_deleted_field   = ?"
9090                                 ", flags_flagged_field   = ?"
9091                                 ", flags_answered_field  = ?"
9092                                 ", flags_recent_field    = ?"
9093                                 ", flags_draft_field     = ?"
9094                                 ", flags_forwarded_field = ?"
9095                                 ", DRM_status = ?"
9096                                 ", attachment_count = ?"
9097                                 ", preview_text= ?"
9098                                 ", meeting_request_status = ? "
9099                                 ", message_class = ? "
9100                                 ", digest_type = ? "
9101                                 ", smime_type = ? "
9102                                 " WHERE mail_id = %d AND account_id != 0"
9103                                 , mail_id);
9104
9105
9106                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9107                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9108                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9109                          i = 0;
9110
9111                         _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9112                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9113                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9114                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9115                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_seen_field);
9116                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_deleted_field);
9117                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_flagged_field);
9118                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_answered_field);
9119                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_recent_field);
9120                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_draft_field);
9121                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_forwarded_field);
9122                         _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9123                         _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9124                         _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9125                         _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9126                         _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9127                         _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9128                         _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9129                         break;
9130
9131                 case UPDATE_MAILBOX: {
9132                                 int err;
9133                                 emstorage_mailbox_tbl_t *mailbox_tbl;
9134
9135                                 if ((err = emstorage_get_mailbox_by_id(multi_user_name, mail->mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
9136                                         EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_id failed [%d]", err);
9137                                         goto FINISH_OFF;
9138                                 }
9139
9140                                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9141                                         "UPDATE mail_tbl SET"
9142                                         " mailbox_id = '%d'"
9143                                         ",mailbox_type = '%d'"
9144                                         " WHERE mail_id = %d AND account_id != 0"
9145                                         , mailbox_tbl->mailbox_id
9146                                         , mailbox_tbl->mailbox_type
9147                                         , mail_id);
9148                                         move_flag = 1;
9149
9150
9151                                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9152                                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9153                                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9154
9155                                 i = 0;
9156                                 _bind_stmt_field_data_string(hStmt, i++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
9157
9158                                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL); /*prevent 26251*/
9159
9160                         }
9161                         break;
9162
9163                 case UPDATE_FLAG:
9164                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9165                                 "UPDATE mail_tbl SET"
9166                                 " flags_seen_field      = %d"
9167                                 ",flags_deleted_field   = %d"
9168                                 ",flags_flagged_field   = %d"
9169                                 ",flags_answered_field  = %d"
9170                                 ",flags_recent_field    = %d"
9171                                 ",flags_draft_field     = %d"
9172                                 ",flags_forwarded_field = %d"
9173                                 "  WHERE mail_id = %d AND account_id != 0"
9174                                 , mail->flags_seen_field
9175                                 , mail->flags_deleted_field
9176                                 , mail->flags_flagged_field
9177                                 , mail->flags_answered_field
9178                                 , mail->flags_recent_field
9179                                 , mail->flags_draft_field
9180                                 , mail->flags_forwarded_field
9181                                 , mail_id);
9182                         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9183
9184
9185                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9186                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9187                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9188
9189                         break;
9190
9191                 case UPDATE_EXTRA_FLAG:
9192                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9193                                 "UPDATE mail_tbl SET"
9194                                 "  priority = %d"
9195                                 ", save_status = %d"
9196                                 ", lock_status = %d"
9197                                 ", report_status = %d"
9198                                 ", DRM_status = %d"
9199                                 " WHERE mail_id = %d AND account_id != 0"
9200                                 , mail->priority
9201                                 , mail->save_status
9202                                 , mail->lock_status
9203                                 , mail->report_status
9204                                 , mail->DRM_status
9205                                 , mail_id);
9206                         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9207
9208
9209                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9210                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9211                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9212                         break;
9213
9214                 case UPDATE_STICKY_EXTRA_FLAG:
9215                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9216                                 "UPDATE mail_tbl SET"
9217                                 "  lock_status = %d"
9218                                 "  WHERE mail_id = %d AND account_id != 0"
9219                                 , mail->lock_status
9220                                 , mail_id);
9221                         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9222
9223
9224                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9225                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9226                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9227                         break;
9228
9229                 case UPDATE_MAIL:
9230                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9231                                 "UPDATE mail_tbl SET"
9232                                 "  full_address_from = ?"
9233                                 ", full_address_reply = ?"
9234                                 ", full_address_to = ?"
9235                                 ", full_address_cc = ?"
9236                                 ", full_address_bcc = ?"
9237                                 ", full_address_return = ?"
9238                                 ", subject = ?"
9239                                 ", file_path_plain = ?"
9240                                 ", date_time = ?"
9241                                 ", flags_seen_field = ?"
9242                                 ", flags_deleted_field = ?"
9243                                 ", flags_flagged_field = ?"
9244                                 ", flags_answered_field = ?"
9245                                 ", flags_recent_field = ?"
9246                                 ", flags_draft_field = ?"
9247                                 ", flags_forwarded_field = ?"
9248                                 ", priority = ?"
9249                                 ", save_status = ?"
9250                                 ", lock_status = ?"
9251                                 ", report_status = ?"
9252                                 ", DRM_status = ?"
9253                                 ", file_path_html = ?"
9254                                 ", file_path_mime_entity = ?"
9255                                 ", mail_size = ?"
9256                                 ", preview_text = ?"
9257                                 ", body_download_status = ?"
9258                                 ", attachment_count = ?"
9259                                 ", inline_content_count = ?"
9260                                 ", meeting_request_status = ?"
9261                                 ", message_class = ?"
9262                                 ", digest_type = ?"
9263                                 ", smime_type = ?"
9264                                 ", scheduled_sending_time = ?"
9265                                 ", remaining_resend_times = ?"
9266                                 ", tag_id = ?"
9267                                 ", replied_time = ?"
9268                                 ", forwarded_time = ?"
9269                                 ", default_charset = ?"
9270                                 ", eas_data_length = ?"
9271                                 ", eas_data = ?"
9272                                 " WHERE mail_id = %d AND account_id != 0"
9273                                 , mail_id);
9274
9275
9276                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9277                         EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
9278                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9279                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9280                         i = 0;
9281                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
9282                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
9283                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
9284                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
9285                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
9286                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
9287                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9288                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9289                         _bind_stmt_field_data_time_t(hStmt, i++, mail->date_time);
9290                         _bind_stmt_field_data_char(hStmt, i++, mail->flags_seen_field);
9291                         _bind_stmt_field_data_char(hStmt, i++, mail->flags_deleted_field);
9292                         _bind_stmt_field_data_char(hStmt, i++, mail->flags_flagged_field);
9293                         _bind_stmt_field_data_char(hStmt, i++, mail->flags_answered_field);
9294                         _bind_stmt_field_data_char(hStmt, i++, mail->flags_recent_field);
9295                         _bind_stmt_field_data_char(hStmt, i++, mail->flags_draft_field);
9296                         _bind_stmt_field_data_char(hStmt, i++, mail->flags_forwarded_field);
9297                         _bind_stmt_field_data_int(hStmt, i++, mail->priority);
9298                         _bind_stmt_field_data_int(hStmt, i++, mail->save_status);
9299                         _bind_stmt_field_data_int(hStmt, i++, mail->lock_status);
9300                         _bind_stmt_field_data_int(hStmt, i++, mail->report_status);
9301                         _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9302                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9303                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9304                         _bind_stmt_field_data_int(hStmt, i++, mail->mail_size);
9305                         _bind_stmt_field_data_nstring(hStmt, i++, (char*)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9306                         _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9307                         _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9308                         _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9309                         _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9310                         _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9311                         _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9312                         _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9313                         _bind_stmt_field_data_int(hStmt, i++, mail->scheduled_sending_time);
9314                         _bind_stmt_field_data_int(hStmt, i++, mail->remaining_resend_times);
9315                         _bind_stmt_field_data_int(hStmt, i++, mail->tag_id);
9316                         _bind_stmt_field_data_int(hStmt, i++, mail->replied_time);
9317                         _bind_stmt_field_data_int(hStmt, i++, mail->forwarded_time);
9318                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
9319                         _bind_stmt_field_data_int(hStmt, i++, mail->eas_data_length);
9320                         _bind_stmt_field_data_blob(hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
9321                         break;
9322
9323                 case UPDATE_DATETIME: {
9324                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9325                                 "UPDATE mail_tbl SET"
9326                                 " date_time = '%ld'"
9327                                 " WHERE mail_id = %d AND account_id != 0"
9328                                 , mail->date_time
9329                                 , mail_id);
9330
9331                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9332                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9333                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9334                         break;
9335                 }
9336
9337                 case UPDATE_FROM_CONTACT_INFO:
9338                         EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_FROM_CONTACT_INFO");
9339                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9340                                 "UPDATE mail_tbl SET"
9341                                 " email_address_sender = ?,"
9342                                 " WHERE mail_id = %d",
9343                                 mail_id);
9344
9345                         hStmt = NULL;
9346
9347                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9348                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9349                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9350                         i = 0;
9351                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9352                         break;
9353
9354                 case UPDATE_TO_CONTACT_INFO:
9355                         EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_TO_CONTACT_INFO");
9356                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9357                                 "UPDATE mail_tbl SET"
9358                                 " email_address_recipient = ?,"
9359                                 " WHERE mail_id = %d",
9360                                 mail_id);
9361
9362                         hStmt = NULL;
9363
9364                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9365                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9366                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9367                         i = 0;
9368                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9369                         break;
9370
9371                         case UPDATE_ALL_CONTACT_INFO:
9372                                 EM_DEBUG_LOG("emstorage_change_mail_field - mail change type is UPDATE_ALL_CONTACT_INFO");
9373                                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9374                                         "UPDATE mail_tbl SET"
9375                                         " email_address_sender = ?,"
9376                                         " email_address_recipient = ?,"
9377                                         " WHERE mail_id = %d",
9378                                         mail_id);
9379
9380                                 hStmt = NULL;
9381
9382                                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9383                                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9384                                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9385                                 i = 0;
9386                                 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9387                                 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9388                                 break;
9389
9390
9391 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
9392                         case UPDATE_PARTIAL_BODY_DOWNLOAD:
9393
9394                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9395                         "UPDATE mail_tbl SET"
9396                         "  body_download_status = ?"
9397                         ", file_path_plain = ?"
9398                         ", file_path_html = ?"
9399                         ", file_path_mime_entity = ?"
9400                         ", attachment_count = ?"
9401                         ", inline_content_count = ?"
9402                         ", preview_text = ?"
9403                         ", digest_type = ?"
9404                         ", smime_type = ?"
9405                         " WHERE mail_id = %d"
9406                         , mail_id);
9407
9408
9409                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9410                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9411                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9412                          i = 0;
9413
9414                         _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9415                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9416                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html,  0, TEXT_2_LEN_IN_MAIL_TBL);
9417                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9418                         _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9419                         _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9420                         _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text,    0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9421                         _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9422                         _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9423
9424                         break;
9425
9426 #endif
9427                 case UPDATE_FILE_PATH:
9428                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9429                                 "UPDATE mail_tbl SET"
9430                                 ", file_path_plain = ?"
9431                                 ", file_path_html = ?"
9432                                 ", file_path_mime_entity = ?"
9433                                 " WHERE mail_id = %d"
9434                                 , mail_id);
9435
9436
9437                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9438                         EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
9439                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9440                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9441                         i = 0;
9442                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9443                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9444                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9445                         break;
9446
9447                 default:
9448                         EM_DEBUG_LOG(" type[%d]", type);
9449
9450                         error = EMAIL_ERROR_INVALID_PARAM;
9451                         goto FINISH_OFF;
9452         }
9453
9454         if (hStmt != NULL) {
9455
9456         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9457                 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
9458                 ("sqlite3_step fail:%d", rc));
9459                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9460                         ("sqlite3_step fail:%d", rc));
9461                 rc = sqlite3_changes(local_db_handle);
9462                 if (rc == 0) {
9463                         EM_DEBUG_LOG(" no matched mail found...");
9464                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
9465                         goto FINISH_OFF;
9466                 }
9467         }
9468
9469         if (mail->account_id == 0) {
9470                 emstorage_mail_tbl_t* mail_for_account_tbl = NULL;
9471                 if (!emstorage_get_mail_field_by_id(multi_user_name, mail_id, RETRIEVE_ACCOUNT, &mail_for_account_tbl, true, &error) || !mail_for_account_tbl) {
9472                         EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id error [%d]", error);
9473                         goto FINISH_OFF;
9474                 }
9475                 mail->account_id = mail_for_account_tbl->account_id;
9476                 if (mail_for_account_tbl)
9477                         emstorage_free_mail(&mail_for_account_tbl, 1, NULL);
9478         }
9479
9480         ret = true;
9481
9482 FINISH_OFF:
9483         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9484
9485         if (hStmt != NULL) {
9486                 rc = sqlite3_finalize(hStmt);
9487                 if (rc != SQLITE_OK) {
9488                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9489                         error = EMAIL_ERROR_DB_FAILURE;
9490                 }
9491                 hStmt = NULL;
9492         }
9493
9494         if (error == EMAIL_ERROR_NONE &&  move_flag != 1 && transaction) {
9495                 if (!emstorage_get_mailbox_id_by_mailbox_type(multi_user_name, mail->account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &mailbox_id, false, &error))
9496                         EM_DEBUG_EXCEPTION("emstorage_get_mailbox_id_by_mailbox_type error [%d]", error);
9497
9498                 if (mail->mailbox_id == mailbox_id) {
9499                         SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
9500                         if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, mailbox_id_param_string, type))
9501                                 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventerror [ NOTI_MAIL_UPDATE ] >>>> ");
9502                 } else {
9503                         /* h.gahlaut@samsung.com: Jan 10, 2011 Publishing noti to refresh outbox when email sending status changes */
9504                         if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, NULL, type))
9505                                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventerror [ NOTI_MAIL_UPDATE ]");
9506                 }
9507         }
9508
9509         if (err_code != NULL)
9510                 *err_code = error;
9511
9512         EM_DEBUG_FUNC_END("ret [%d]", ret);
9513         return ret;
9514 }
9515
9516 INTERNAL_FUNC int emstorage_increase_mail_id(char *multi_user_name, int *mail_id, int transaction, int *err_code)
9517 {
9518         EM_DEBUG_FUNC_BEGIN("mail_id[%p], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9519
9520         int rc, ret = false;
9521         int error = EMAIL_ERROR_NONE;
9522         int latest_mail_id = 0;
9523         sqlite3 *local_db_handle = NULL;
9524         char *sql = "SELECT MAX(mail_id) FROM mail_tbl;";
9525         char **result = NULL;
9526
9527 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9528         _timedlock_shm_mutex(mapped_for_generating_mail_id, 2);
9529 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9530
9531         ret = vconf_get_int(VCONF_KEY_LATEST_MAIL_ID, &latest_mail_id);
9532         if (ret < 0 || latest_mail_id == 0) {
9533                 EM_DEBUG_LOG("vconf_get_int() failed [%d] or latest_mail_id is zero", ret);
9534
9535         local_db_handle = emstorage_get_db_connection(multi_user_name);
9536
9537                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9538                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
9539                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9540                 if (NULL == result[1])
9541                         rc = 1;
9542                 else
9543                         rc = atoi(result[1]) + 1;
9544
9545                 sqlite3_free_table(result);
9546                 latest_mail_id = rc;
9547         }
9548
9549         latest_mail_id++;
9550
9551         ret = vconf_set_int(VCONF_KEY_LATEST_MAIL_ID, latest_mail_id);
9552
9553         if (mail_id)
9554                 *mail_id = latest_mail_id;
9555
9556 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9557         _unlockshm_mutex(mapped_for_generating_mail_id);
9558 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9559
9560         ret = true;
9561
9562 FINISH_OFF:
9563
9564         if (err_code != NULL)
9565                 *err_code = error;
9566
9567         EM_DEBUG_FUNC_END("ret [%d]", ret);
9568         return ret;
9569 }
9570
9571 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)
9572 {
9573         EM_PROFILE_BEGIN(profile_emstorage_add_mail);
9574         EM_DEBUG_FUNC_BEGIN("mail_tbl_data[%p], get_id[%d], transaction[%d], err_code[%p]", mail_tbl_data, get_id, transaction, err_code);
9575
9576         if (!mail_tbl_data) {
9577                 EM_DEBUG_EXCEPTION("mail_tbl_data[%p], get_id[%d]", mail_tbl_data, get_id);
9578                 if (err_code != NULL)
9579                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9580                 return false;
9581         }
9582
9583         int rc, ret = false;
9584         int error = EMAIL_ERROR_NONE;
9585         char sql_query_string[QUERY_SIZE] = {0, };
9586         DB_STMT hStmt = NULL;
9587
9588         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9589
9590         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9591
9592         if (get_id) {
9593                 /*  increase unique id */
9594                 char *sql = "SELECT max(rowid) FROM mail_tbl;";
9595                 char **result;
9596
9597                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9598 /*              EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
9599                         ("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle))); */
9600                 if (rc != SQLITE_OK) {
9601                         EM_DEBUG_EXCEPTION("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle));
9602                         error = EMAIL_ERROR_DB_FAILURE;
9603                         sqlite3_free_table(result);
9604                         goto FINISH_OFF;
9605                 }
9606
9607                 if (NULL == result[1])
9608                         rc = 1;
9609                 else
9610                         rc = atoi(result[1])+1;
9611
9612                 sqlite3_free_table(result);
9613
9614                 mail_tbl_data->mail_id   = rc;
9615                 mail_tbl_data->thread_id = rc;
9616         }
9617
9618         if (mail_tbl_data->date_time == 0)
9619                 mail_tbl_data->date_time = time(NULL);
9620
9621         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9622                 "INSERT INTO mail_tbl VALUES "
9623                 "(?" /*  mail_id */
9624                 ", ?" /*  account_id */
9625                 ", ?" /*  mailbox_id */
9626                 ", ?" /*  mailbox_type */
9627                 ", ?" /*  subject */
9628
9629                 ", ?" /*  date_time */
9630                 ", ?" /*  server_mail_status */
9631                 ", ?" /*  server_mailbox_name */
9632                 ", ?" /*  server_mail_id */
9633                 ", ?" /*  message_id */
9634
9635                 ", ?" /*  reference_mail_id */
9636                 ", ?" /*  full_address_from */
9637                 ", ?" /*  full_address_reply */
9638                 ", ?" /*  full_address_to */
9639                 ", ?" /*  full_address_cc */
9640
9641                 ", ?" /*  full_address_bcc */
9642                 ", ?" /*  full_address_return */
9643                 ", ?" /*  email_address_sender */
9644                 ", ?" /*  email_address_recipient */
9645                 ", ?" /*  alias_sender */
9646
9647                 ", ?" /*  alias_recipient */
9648                 ", ?" /*  body_download_status */
9649                 ", ?" /*  file_path_plain */
9650                 ", ?" /*  file_path_html */
9651                 ", ?" /*  file_path_mime_entity */
9652
9653                 ", ?" /*  mail_size */
9654                 ", ?" /*  flags_seen_field */
9655                 ", ?" /*  flags_deleted_field */
9656                 ", ?" /*  flags_flagged_field */
9657                 ", ?" /*  flags_answered_field */
9658
9659                 ", ?" /*  flags_recent_field */
9660                 ", ?" /*  flags_draft_field */
9661                 ", ?" /*  flags_forwarded_field */
9662                 ", ?" /*  DRM_status */
9663                 ", ?" /*  priority */
9664
9665                 ", ?" /*  save_status */
9666                 ", ?" /*  lock_status */
9667                 ", ?" /*  report_status */
9668                 ", ?" /*  attachment_count */
9669                 ", ?" /*  inline_content_count */
9670
9671                 ", ?" /*  thread_id */
9672                 ", ?" /*  thread_item_count */
9673                 ", ?" /*  preview_text */
9674                 ", ?" /*  meeting_request_status */
9675                 ", ?" /*  message_class */
9676
9677                 ", ?" /*  digest_type */
9678                 ", ?" /*  smime_type */
9679                 ", ?" /*  scheduled_sending_time */
9680                 ", ?" /*  remaining_resend_times */
9681                 ", ?" /*  tag_id */
9682
9683                 ", ?" /*  replied_time */
9684                 ", ?" /*  forwarded_time */
9685                 ", ?" /*  default charset */
9686                 ", ?" /*  eas_data_length */
9687                 ", ?" /*  eas_data */
9688                 ", ?" /*  user_name */
9689                 ")");
9690
9691         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle,
9692                                                    sql_query_string,
9693                                                    EM_SAFE_STRLEN(sql_query_string),
9694                                                    &hStmt,
9695                                                    NULL),
9696                                     rc);
9697         if (rc != SQLITE_OK) {
9698                 EM_DEBUG_EXCEPTION("sqlite3_prepare error [%d] [%s] SQL(%s) ",
9699                              rc, sql_query_string, sqlite3_errmsg(local_db_handle));
9700                 error = EMAIL_ERROR_DB_FAILURE;
9701                 goto FINISH_OFF;
9702         }
9703
9704         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mail_id);
9705         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, mail_tbl_data->account_id);
9706         _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_id);
9707         _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_type);
9708         _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9709
9710         _bind_stmt_field_data_int(hStmt, DATETIME_IDX_IN_MAIL_TBL, mail_tbl_data->date_time);
9711         _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->server_mail_status);
9712         _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);
9713         _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);
9714         _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);
9715
9716         _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, mail_tbl_data->reference_mail_id);
9717         _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);
9718         _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);
9719         _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);
9720         _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);
9721
9722         _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);
9723         _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);
9724         _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);
9725         _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);
9726         _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);
9727
9728         _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);
9729         _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->body_download_status);
9730         _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);
9731         _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);
9732         _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);
9733
9734         _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, mail_tbl_data->mail_size);
9735         _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_seen_field);
9736         _bind_stmt_field_data_int(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_deleted_field);
9737         _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_flagged_field);
9738         _bind_stmt_field_data_int(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_answered_field);
9739
9740         _bind_stmt_field_data_int(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_recent_field);
9741         _bind_stmt_field_data_int(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_draft_field);
9742         _bind_stmt_field_data_int(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_forwarded_field);
9743         _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->DRM_status);
9744         _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, mail_tbl_data->priority);
9745
9746         _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->save_status);
9747         _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->lock_status);
9748         _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->report_status);
9749         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->attachment_count);
9750         _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->inline_content_count);
9751
9752         _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, mail_tbl_data->thread_id);
9753         _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->thread_item_count);
9754         _bind_stmt_field_data_nstring(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9755         _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->meeting_request_status);
9756         _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, mail_tbl_data->message_class);
9757
9758         _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->digest_type);
9759         _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->smime_type);
9760         _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->scheduled_sending_time);
9761         _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, mail_tbl_data->remaining_resend_times);
9762         _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, mail_tbl_data->tag_id);
9763
9764         _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->replied_time);
9765         _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->forwarded_time);
9766         _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);
9767         _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, mail_tbl_data->eas_data_length);
9768         _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, (void*)mail_tbl_data->eas_data, mail_tbl_data->eas_data_length);
9769         _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);
9770
9771         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9772         if (rc == SQLITE_FULL) {
9773                 EM_DEBUG_EXCEPTION("sqlite3_step error [%d]", rc);
9774                 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
9775                 goto FINISH_OFF;
9776         }
9777         if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
9778                 EM_DEBUG_EXCEPTION("sqlite3_step error [%d]", rc);
9779                 error = EMAIL_ERROR_DB_FAILURE;
9780                 goto FINISH_OFF;
9781         }
9782         ret = true;
9783
9784 FINISH_OFF:
9785         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9786
9787         if (hStmt != NULL) {
9788                 rc = sqlite3_finalize(hStmt);
9789                 if (rc != SQLITE_OK) {
9790                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9791                         error = EMAIL_ERROR_DB_FAILURE;
9792                 }
9793         }
9794
9795         if (err_code != NULL)
9796                 *err_code = error;
9797
9798         EM_PROFILE_END(profile_emstorage_add_mail);
9799         EM_DEBUG_FUNC_END("ret [%d]", ret);
9800         return ret;
9801 }
9802
9803 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)
9804 {
9805         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);
9806
9807         int ret = false, i, cur_conditional_clause = 0;
9808         int error = EMAIL_ERROR_NONE;
9809         int target_account_id;
9810         int conditional_clause_len = 0;
9811         char *sql_query_string = NULL, *conditional_clause = NULL;
9812         emstorage_mailbox_tbl_t *result_mailbox = NULL;
9813         email_mailbox_type_e target_mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED;
9814         char* target_mailbox_name = NULL;
9815
9816         if (!mail_ids || input_mailbox_id <= 0) {
9817                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9818                 if (err_code != NULL)
9819                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9820                 return false;
9821         }
9822
9823         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9824
9825         if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &result_mailbox)) != EMAIL_ERROR_NONE || !result_mailbox) {
9826                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", error);
9827                 if (err_code != NULL)
9828                         *err_code = error;
9829                 return false;
9830         }
9831
9832         if (result_mailbox->mailbox_name) {
9833                 if (strstr(result_mailbox->mailbox_name, "'")) {
9834                         target_mailbox_name = em_replace_all_string(result_mailbox->mailbox_name, "'", "''");
9835                 } else {
9836                         target_mailbox_name = strdup(result_mailbox->mailbox_name);
9837                 }
9838         }
9839
9840         target_mailbox_type = result_mailbox->mailbox_type;
9841         target_account_id   = result_mailbox->account_id;
9842         emstorage_free_mailbox(&result_mailbox, 1, NULL);
9843
9844         conditional_clause_len =  (sizeof(char) * 8 * number_of_mails) + 512;
9845         conditional_clause = em_malloc(conditional_clause_len);
9846         if (conditional_clause == NULL) {
9847                 EM_DEBUG_EXCEPTION("em_mallocfailed");
9848                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9849                 goto FINISH_OFF;
9850         }
9851         cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE mail_id in (");
9852
9853         for (i = 0; i < number_of_mails; i++)
9854                 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
9855
9856         /* prevent 34415 */
9857         char *last_comma = rindex(conditional_clause, ',');
9858         if (last_comma) *last_comma = ')'; /* replace , with) */
9859
9860         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9861
9862         /* Updating a mail_tbl */
9863
9864         sql_query_string = em_malloc(conditional_clause_len);
9865         if (sql_query_string == NULL) {
9866                 EM_DEBUG_EXCEPTION("em_mallocfailed");
9867                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9868                 goto FINISH_OFF;
9869         }
9870         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);
9871         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9872
9873         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9874         if (error != EMAIL_ERROR_NONE) {
9875                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9876                         goto FINISH_OFF;
9877         }
9878
9879         /* Updating a mail_attachment_tbl */
9880         memset(sql_query_string, 0x00, conditional_clause_len);
9881         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);
9882         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9883         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9884         if (error != EMAIL_ERROR_NONE) {
9885                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9886                         goto FINISH_OFF;
9887         }
9888
9889         /* Updating a mail_meeting_tbl */
9890         memset(sql_query_string, 0x00, conditional_clause_len);
9891         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);
9892         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9893         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9894         if (error != EMAIL_ERROR_NONE) {
9895                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9896                         goto FINISH_OFF;
9897         }
9898
9899 #ifdef __FEATURE_BODY_SEARCH__
9900         /* Updating mail_text_tbl */
9901         memset(sql_query_string, 0x00, conditional_clause_len);
9902         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);
9903         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9904         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9905         if (error != EMAIL_ERROR_NONE) {
9906                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9907                         goto FINISH_OFF;
9908         }
9909 #endif
9910
9911         /* Updating a mail_read_mail_uid_tbl */
9912         memset(conditional_clause, 0x00, conditional_clause_len);
9913         cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE local_uid in (");
9914
9915         for (i = 0; i < number_of_mails; i++)
9916                 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
9917
9918         /* prevent 34415 */
9919         last_comma = rindex(conditional_clause, ',');
9920         if (last_comma) *last_comma = ')'; /* replace , with) */
9921
9922         memset(sql_query_string, 0x00, conditional_clause_len);
9923         SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_read_mail_uid_tbl SET mailbox_name = '%s', mailbox_id = %d, account_id = %d %s", target_mailbox_name, input_mailbox_id, target_account_id, conditional_clause);
9924         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9925         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9926         if (error != EMAIL_ERROR_NONE) {
9927                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9928                         goto FINISH_OFF;
9929         }
9930
9931         ret = true;
9932
9933 FINISH_OFF:
9934         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9935
9936         EM_SAFE_FREE(target_mailbox_name);
9937         EM_SAFE_FREE(conditional_clause);
9938         EM_SAFE_FREE(sql_query_string);
9939
9940         if (err_code != NULL)
9941                 *err_code = error;
9942
9943         EM_DEBUG_FUNC_END("ret [%d]", ret);
9944         return ret;
9945 }
9946
9947 INTERNAL_FUNC int emstorage_delete_mail(char *multi_user_name, int mail_id, int from_server, int transaction, int *err_code)
9948 {
9949         EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9950
9951         if (!mail_id) {
9952                 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
9953                 if (err_code != NULL)
9954                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9955                 return false;
9956         }
9957
9958         int ret = false;
9959         int error = EMAIL_ERROR_NONE;
9960         char sql_query_string[QUERY_SIZE] = {0, };
9961         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9962         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9963
9964         memset(sql_query_string, 0x00, sizeof(sql_query_string));
9965
9966         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id = %d ", mail_id);
9967         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9968         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9969         if (error != EMAIL_ERROR_NONE) {
9970                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9971                         goto FINISH_OFF;
9972         }
9973
9974         ret = true;
9975
9976 FINISH_OFF:
9977         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9978
9979         if (err_code != NULL)
9980                 *err_code = error;
9981
9982         EM_DEBUG_FUNC_END("ret [%d]", ret);
9983         return ret;
9984 }
9985
9986 INTERNAL_FUNC int emstorage_delete_multiple_mails(char *multi_user_name, int mail_ids[], int number_of_mails, int transaction, int *err_code)
9987 {
9988         EM_DEBUG_FUNC_BEGIN("mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", mail_ids, number_of_mails, transaction, err_code);
9989
9990         int ret = false, i, cur_sql_query_string = 0;
9991         int error = EMAIL_ERROR_NONE;
9992         int query_size = 0;
9993         char *sql_query_string = NULL;
9994
9995         if (!mail_ids) {
9996                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9997                 if (err_code != NULL)
9998                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9999                 return false;
10000         }
10001
10002         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10003         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10004
10005         query_size = (sizeof(char) * 8 * number_of_mails) + 512;
10006         sql_query_string =  em_malloc(query_size);
10007         if (sql_query_string == NULL) {
10008                 EM_DEBUG_EXCEPTION("em_mallocfailed");
10009                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10010                 goto FINISH_OFF;
10011         }
10012
10013         cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_tbl WHERE mail_id in (");
10014
10015         for (i = 0; i < number_of_mails; i++)
10016                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
10017
10018         /* prevent 34414 */
10019         char *last_comma = rindex(sql_query_string, ',');
10020         if (last_comma != NULL) *last_comma = ')'; /* replace , with) */
10021
10022         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10023         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10024         if (error != EMAIL_ERROR_NONE) {
10025                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10026                         goto FINISH_OFF;
10027         }
10028
10029 #ifdef __FEATURE_BODY_SEARCH__
10030         /* delete mail_text from mail_text_tbl */
10031         cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_text_tbl WHERE mail_id in (");
10032
10033         for (i = 0; i < number_of_mails; i++)
10034                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
10035
10036         last_comma = rindex(sql_query_string, ',');
10037         *last_comma = ')'; /* replace , with) */
10038
10039         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10040         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10041         if (error != EMAIL_ERROR_NONE) {
10042                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10043                         goto FINISH_OFF;
10044         }
10045 #endif
10046
10047         ret = true;
10048
10049 FINISH_OFF:
10050         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10051
10052         EM_SAFE_FREE(sql_query_string);
10053         if (err_code != NULL)
10054                 *err_code = error;
10055
10056         EM_DEBUG_FUNC_END("ret [%d]", ret);
10057         return ret;
10058 }
10059
10060 INTERNAL_FUNC int emstorage_delete_mail_by_account(char *multi_user_name, int account_id, int transaction, int *err_code)
10061 {
10062         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
10063
10064         if (account_id < FIRST_ACCOUNT_ID) {
10065                 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
10066
10067                 if (err_code != NULL)
10068                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10069                 return false;
10070         }
10071
10072         int rc, ret = false;
10073         int error = EMAIL_ERROR_NONE;
10074         char sql_query_string[QUERY_SIZE] = {0, };
10075
10076         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10077         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10078
10079         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d", account_id);
10080         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10081         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10082         if (error != EMAIL_ERROR_NONE) {
10083                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10084                         goto FINISH_OFF;
10085         }
10086
10087         rc = sqlite3_changes(local_db_handle);
10088         if (rc == 0) {
10089                 EM_DEBUG_LOG(" no mail found...");
10090                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10091         }
10092
10093         /* Delete all mails  mail_read_mail_uid_tbl table based on account id */
10094         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
10095         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10096         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10097         if (error != EMAIL_ERROR_NONE) {
10098                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10099                         goto FINISH_OFF;
10100         }
10101
10102         rc = sqlite3_changes(local_db_handle);
10103         if (rc == 0) {
10104                 EM_DEBUG_LOG("No mail found...");
10105                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10106         }
10107
10108 #ifdef __FEATURE_BODY_SEARCH__
10109         /* Delete all mail_text in mail_text_tbl table based on account id */
10110         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE account_id = %d", account_id);
10111         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10112         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10113         if (error != EMAIL_ERROR_NONE) {
10114                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10115                         goto FINISH_OFF;
10116         }
10117
10118         rc = sqlite3_changes(local_db_handle);
10119         if (rc == 0) {
10120                 EM_DEBUG_LOG("No mail found...");
10121                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10122         }
10123 #endif
10124
10125         ret = true;
10126
10127 FINISH_OFF:
10128         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10129
10130         if (error == EMAIL_ERROR_NONE) {
10131                 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_WITH_ACCOUNT, account_id, 0 , NULL, 0))
10132                         EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAIL_DELETE_ALL ]");
10133         }
10134
10135         if (err_code != NULL)
10136                 *err_code = error;
10137
10138         EM_DEBUG_FUNC_END("ret [%d]", ret);
10139         return ret;
10140 }
10141
10142 INTERNAL_FUNC int emstorage_delete_mail_by_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox, int transaction, int *err_code)
10143 {
10144         EM_DEBUG_FUNC_BEGIN("mailbox[%p], transaction[%d], err_code[%p]", mailbox, transaction, err_code);
10145
10146         if (mailbox == NULL) {
10147                 EM_DEBUG_EXCEPTION("mailbox [%p]", mailbox);
10148                 if (err_code != NULL)
10149                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10150                 return false;
10151         }
10152
10153         int ret = false;
10154         int error = EMAIL_ERROR_NONE;
10155         char sql_query_string[QUERY_SIZE] = {0, };
10156         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10157         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10158
10159         if (strcmp(mailbox->mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
10160                 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);
10161                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10162                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10163                 if (error != EMAIL_ERROR_NONE) {
10164                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10165                         goto FINISH_OFF;
10166                 }
10167
10168                 /* Delete Mails from mail_read_mail_uid_tbl */
10169                 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);
10170                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10171                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10172                 if (error != EMAIL_ERROR_NONE) {
10173                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10174                         goto FINISH_OFF;
10175                 }
10176         } else {
10177                 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);
10178                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10179                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10180                 if (error != EMAIL_ERROR_NONE) {
10181                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10182                         goto FINISH_OFF;
10183                 }
10184
10185                 /* Delete Mails from mail_read_mail_uid_tbl */
10186                 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);
10187                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10188                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10189                 if (error != EMAIL_ERROR_NONE) {
10190                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10191                         goto FINISH_OFF;
10192                 }
10193
10194 #ifdef __FEATURE_BODY_SEARCH__
10195                 /* Delete Mails from mail_text_tbl */
10196                 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);
10197                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10198                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10199                 if (error != EMAIL_ERROR_NONE) {
10200                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10201                         goto FINISH_OFF;
10202                 }
10203 #endif
10204         }
10205         ret = true;
10206
10207 FINISH_OFF:
10208         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10209
10210         if (error == EMAIL_ERROR_NONE) {
10211                 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_ALL, mailbox->account_id, mailbox->mailbox_id , mailbox->mailbox_name, 0))
10212                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventFailed [ NOTI_MAIL_DELETE_ALL ] >>>> ");
10213         }
10214
10215         if (err_code != NULL)
10216                 *err_code = error;
10217
10218         EM_DEBUG_FUNC_END("ret [%d]", ret);
10219         return ret;
10220 }
10221
10222 INTERNAL_FUNC int emstorage_free_mail(emstorage_mail_tbl_t** mail_list, int count, int *err_code)
10223 {
10224         EM_DEBUG_FUNC_BEGIN("mail_list[%p], count[%d], err_code[%p]", mail_list, count, err_code);
10225
10226         if (count > 0) {
10227                 if ((mail_list == NULL) || (*mail_list == NULL)) {
10228                         EM_DEBUG_EXCEPTION("mail_ilst[%p], count[%d]", mail_list, count);
10229
10230                         if (err_code)
10231                                 *err_code = EMAIL_ERROR_INVALID_PARAM;
10232                         return false;
10233                 }
10234
10235                 emstorage_mail_tbl_t* p = *mail_list;
10236                 int i = 0;
10237
10238                 for (; i < count; i++, p++) {
10239                         EM_SAFE_FREE(p->server_mailbox_name);
10240                         EM_SAFE_FREE(p->server_mail_id);
10241                         EM_SAFE_FREE(p->full_address_from);
10242                         EM_SAFE_FREE(p->full_address_reply);
10243                         EM_SAFE_FREE(p->full_address_to);
10244                         EM_SAFE_FREE(p->full_address_cc);
10245                         EM_SAFE_FREE(p->full_address_bcc);
10246                         EM_SAFE_FREE(p->full_address_return);
10247                         EM_SAFE_FREE(p->subject);
10248                         EM_SAFE_FREE(p->file_path_plain);
10249                         EM_SAFE_FREE(p->file_path_html);
10250                         EM_SAFE_FREE(p->file_path_mime_entity);
10251                         EM_SAFE_FREE(p->message_id);
10252                         EM_SAFE_FREE(p->email_address_sender);
10253                         EM_SAFE_FREE(p->email_address_recipient);
10254                         EM_SAFE_FREE(p->preview_text);
10255                         EM_SAFE_FREE(p->alias_sender);
10256                         EM_SAFE_FREE(p->alias_recipient);
10257                         EM_SAFE_FREE(p->default_charset);
10258                         EM_SAFE_FREE(p->pgp_password);
10259                         EM_SAFE_FREE(p->eas_data);
10260                         EM_SAFE_FREE(p->user_name);
10261                 }
10262                 EM_SAFE_FREE(*mail_list);
10263         }
10264
10265         if (err_code != NULL)
10266                 *err_code = EMAIL_ERROR_NONE;
10267
10268         EM_DEBUG_FUNC_END();
10269         return true;
10270 }
10271
10272 #ifdef __FEATURE_BODY_SEARCH__
10273 INTERNAL_FUNC void emstorage_free_mail_text(emstorage_mail_text_tbl_t** mail_text_list, int count, int *err_code)
10274 {
10275         EM_DEBUG_FUNC_BEGIN("mail_text_list[%p], count[%d], err_code[%p]", mail_text_list, count, err_code);
10276
10277         if (count > 0) {
10278                 if ((mail_text_list == NULL) || (*mail_text_list == NULL)) {
10279                         EM_DEBUG_LOG("Nothing to free: mail_text_list[%p]", mail_text_list);
10280                         return;
10281                 }
10282
10283                 emstorage_mail_text_tbl_t *p = *mail_text_list;
10284                 int i = 0;
10285
10286                 for (; i < count; i++, p++) {
10287                         EM_SAFE_FREE(p->body_text);
10288                 }
10289                 EM_SAFE_FREE(*mail_text_list);
10290         }
10291
10292         EM_DEBUG_FUNC_END();
10293 }
10294 #endif
10295
10296 INTERNAL_FUNC int emstorage_get_attachment_count(char *multi_user_name, int mail_id, int *count, int transaction, int *err_code)
10297 {
10298         EM_DEBUG_FUNC_BEGIN("mail_id[%d], count[%p], transaction[%d], err_code[%p]", mail_id, count, transaction, err_code);
10299
10300         if (mail_id <= 0 || !count) {
10301                 EM_DEBUG_EXCEPTION("mail_id[%d], count[%p]", mail_id, count);
10302                 if (err_code != NULL)
10303                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10304                 return false;
10305         }
10306
10307         int rc = -1, ret = false;
10308         int error = EMAIL_ERROR_NONE;
10309         char sql_query_string[QUERY_SIZE] = {0, };
10310
10311         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10312         EMSTORAGE_START_READ_TRANSACTION(transaction);
10313
10314         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
10315
10316         char **result;
10317
10318         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10319         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10320                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10321
10322         *count = atoi(result[1]);
10323         sqlite3_free_table(result);
10324
10325         ret = true;
10326
10327 FINISH_OFF:
10328         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10329
10330         if (err_code != NULL)
10331                 *err_code = error;
10332
10333         EM_DEBUG_FUNC_END("ret [%d]", ret);
10334         return ret;
10335 }
10336
10337 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)
10338 {
10339         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);
10340
10341         if (input_mail_id <= 0 || !output_attachment_list || !output_attachment_count) {
10342                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10343                 return EMAIL_ERROR_INVALID_PARAM;
10344         }
10345
10346         int                         error = EMAIL_ERROR_NONE;
10347         int                         i = 0;
10348         int                         rc = -1;
10349         char                      **result = NULL;
10350         char                        sql_query_string[QUERY_SIZE] = {0, };
10351         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10352         DB_STMT hStmt = NULL;
10353         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10354
10355         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
10356         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", input_mail_id);
10357         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10358         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10359                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10360
10361         *output_attachment_count = atoi(result[1]);
10362         sqlite3_free_table(result);
10363
10364         if (*output_attachment_count == 0) {
10365                 error = EMAIL_ERROR_NONE;
10366                 goto FINISH_OFF;
10367         }
10368
10369         p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * (*output_attachment_count));
10370
10371         if (!p_data_tbl) {
10372                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
10373                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10374                 goto FINISH_OFF;
10375         }
10376
10377         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE mail_id = %d ORDER BY attachment_id", input_mail_id);
10378         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
10379
10380         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10381         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)));
10382
10383         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10384         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },  ("sqlite3_step fail:%d", rc));
10385
10386         if (rc == SQLITE_DONE) {
10387                 EM_DEBUG_EXCEPTION("no matched attachment found...");
10388                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10389                 goto FINISH_OFF;
10390         }
10391         for (i = 0; i < *output_attachment_count; i++) {
10392                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10393                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10394                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10395                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10396                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10397                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10398                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10399                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10400                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10401                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10402                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10403                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10404                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10405                 EM_DEBUG_LOG("attachment[%d].attachment_id : %d", i, p_data_tbl[i].attachment_id);
10406                 EM_DEBUG_LOG("attachment_mime_type : %s", p_data_tbl[i].attachment_mime_type);
10407                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10408                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
10409         }
10410
10411 FINISH_OFF:
10412
10413         if (error == EMAIL_ERROR_NONE)
10414                 *output_attachment_list = p_data_tbl;
10415         else if (p_data_tbl != NULL)
10416                 emstorage_free_attachment(&p_data_tbl, *output_attachment_count, NULL);
10417
10418         if (hStmt) {
10419                 rc = sqlite3_finalize(hStmt);
10420                 if (rc != SQLITE_OK) {
10421                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10422                         error = EMAIL_ERROR_DB_FAILURE;
10423                         if (*output_attachment_list)
10424                                 emstorage_free_attachment(output_attachment_list, *output_attachment_count, NULL); /* prevent */
10425                 }
10426         }
10427
10428         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
10429
10430         EM_DEBUG_FUNC_END("error [%d]", error);
10431         return error;
10432 }
10433
10434 INTERNAL_FUNC int emstorage_get_attachment(char *multi_user_name, int attachment_id, emstorage_attachment_tbl_t **attachment, int transaction, int *err_code)
10435 {
10436         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], transaction[%d], err_code[%p]", attachment_id, attachment, transaction, err_code);
10437
10438         if (attachment_id <= 0 || !attachment) {
10439                 EM_DEBUG_EXCEPTION("attachment_id[%d], attachment[%p]", attachment_id, attachment);
10440                 if (err_code != NULL)
10441                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10442                 return false;
10443         }
10444
10445         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10446         int rc, ret = false;
10447         int error = EMAIL_ERROR_NONE;
10448         char sql_query_string[QUERY_SIZE] = {0, };
10449
10450         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10451         EMSTORAGE_START_READ_TRANSACTION(transaction);
10452
10453         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_id = %d",  attachment_id);
10454
10455         sqlite3_stmt* hStmt = NULL;
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_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
10459
10460         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10461                 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10462
10463
10464         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10465         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10466                 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10467
10468         if (rc == SQLITE_DONE) {
10469                 EM_DEBUG_LOG("no matched attachment found...");
10470                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10471                 goto FINISH_OFF;
10472         }
10473
10474         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10475                 EM_DEBUG_EXCEPTION("malloc failed...");
10476                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10477                 goto FINISH_OFF;
10478         }
10479
10480         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10481         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10482         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10483         _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10484         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10485         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10486         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10487         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10488         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10489         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10490         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10491         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10492         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10493
10494 #ifdef __ATTACHMENT_OPTI__
10495                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10496                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10497 #endif
10498
10499         ret = true;
10500
10501 FINISH_OFF:
10502         if (ret == true)
10503                 *attachment = p_data_tbl;
10504
10505         if (hStmt != NULL) {
10506                 rc = sqlite3_finalize(hStmt);
10507                 if (rc != SQLITE_OK) {
10508                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10509                         error = EMAIL_ERROR_DB_FAILURE;
10510                 }
10511         }
10512
10513         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10514
10515         if (err_code != NULL)
10516                 *err_code = error;
10517
10518         EM_DEBUG_FUNC_END("ret [%d]", ret);
10519         return ret;
10520 }
10521
10522 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)
10523 {
10524         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);
10525
10526         if (mail_id <= 0 || nth <= 0 || !attachment_tbl) {
10527                 EM_DEBUG_EXCEPTION(" mail_id[%d], nth[%d], attachment[%p]", mail_id, nth, attachment_tbl);
10528
10529                 if (err_code != NULL)
10530                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10531                 return false;
10532         }
10533
10534         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10535         char *p = NULL;
10536         int rc, ret = false;
10537         int error = EMAIL_ERROR_NONE;
10538         char sql_query_string[QUERY_SIZE] = {0, };
10539
10540         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10541         EMSTORAGE_START_READ_TRANSACTION(transaction);
10542
10543         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));
10544         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10545
10546         DB_STMT hStmt = NULL;
10547
10548         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10549         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10550                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10551
10552
10553         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10554         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10555                 ("sqlite3_step fail:%d", rc));
10556
10557         if (rc == SQLITE_DONE) {
10558                 EM_DEBUG_EXCEPTION("no matched attachment found: mail_id[%d] nth[%d]", mail_id, nth);
10559                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10560                 goto FINISH_OFF;
10561         }
10562
10563         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10564                 EM_DEBUG_EXCEPTION(" malloc failed...");
10565                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10566                 goto FINISH_OFF;
10567         }
10568
10569         p_data_tbl->attachment_id = sqlite3_column_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10570         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10571                 p_data_tbl->attachment_name = cpy_str(p);
10572         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10573                 p_data_tbl->attachment_path = cpy_str(p);
10574         if ((p = (char *)sqlite3_column_text(hStmt, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10575                 p_data_tbl->content_id = cpy_str(p);
10576         p_data_tbl->attachment_size = sqlite3_column_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10577         p_data_tbl->mail_id = sqlite3_column_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10578         p_data_tbl->account_id = sqlite3_column_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10579         p_data_tbl->mailbox_id = sqlite3_column_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10580         p_data_tbl->attachment_save_status = sqlite3_column_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10581         p_data_tbl->attachment_drm_type = sqlite3_column_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10582         p_data_tbl->attachment_drm_method = sqlite3_column_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10583         p_data_tbl->attachment_inline_content_status = sqlite3_column_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10584         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10585                 p_data_tbl->attachment_mime_type = cpy_str(p);
10586 #ifdef __ATTACHMENT_OPTI__
10587                 p_data_tbl->encoding = sqlite3_column_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10588                 if ((p = (char *)sqlite3_column_text(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10589                         p_data_tbl->section = cpy_str(p);
10590 #endif
10591         ret = true;
10592
10593 FINISH_OFF:
10594         if (ret == true)
10595                 *attachment_tbl = p_data_tbl;
10596
10597         if (hStmt != NULL) {
10598                 rc = sqlite3_finalize(hStmt);
10599                 if (rc != SQLITE_OK) {
10600                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10601                         error = EMAIL_ERROR_DB_FAILURE;
10602                 }
10603         }
10604
10605         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10606
10607         if (err_code != NULL)
10608                 *err_code = error;
10609
10610         EM_DEBUG_FUNC_END("ret [%d]", ret);
10611         return ret;
10612 }
10613
10614 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)
10615 {
10616         EM_DEBUG_FUNC_BEGIN("attachment_path[%p], attachment[%p], transaction[%d], err_code[%p]", attachment_path, attachment, transaction, err_code);
10617
10618         if (attachment_path == NULL || !attachment) {
10619                 EM_DEBUG_EXCEPTION("attachment_path[%p], attachment[%p]", attachment_path, attachment);
10620                 if (err_code != NULL)
10621                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10622                 return false;
10623         }
10624
10625         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10626         int rc, ret = false;
10627         int error = EMAIL_ERROR_NONE;
10628         char sql_query_string[QUERY_SIZE] = {0, };
10629
10630         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10631         EMSTORAGE_START_READ_TRANSACTION(transaction);
10632
10633         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_path = '%s'", attachment_path);
10634
10635         sqlite3_stmt* hStmt = NULL;
10636
10637         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10638         EM_DEBUG_LOG_DEV("before sqlite3_prepare hStmt = %p", hStmt);
10639
10640         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10641                 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10642
10643
10644         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10645         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10646                 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10647
10648         if (rc == SQLITE_DONE) {
10649                 EM_DEBUG_LOG("no matched attachment found...");
10650                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10651                 goto FINISH_OFF;
10652         }
10653
10654         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10655                 EM_DEBUG_EXCEPTION("malloc failed...");
10656                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10657                 goto FINISH_OFF;
10658         }
10659
10660         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10661         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10662         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10663         _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10664         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10665         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10666         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10667         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10668         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10669         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10670         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10671         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10672         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10673
10674 #ifdef __ATTACHMENT_OPTI__
10675                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10676                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10677 #endif
10678
10679         ret = true;
10680
10681 FINISH_OFF:
10682         if (ret == true)
10683                 *attachment = p_data_tbl;
10684
10685         if (hStmt != NULL) {
10686                 rc = sqlite3_finalize(hStmt);
10687                 if (rc != SQLITE_OK) {
10688                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10689                         error = EMAIL_ERROR_DB_FAILURE;
10690                 }
10691         }
10692
10693         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10694
10695         if (err_code != NULL)
10696                 *err_code = error;
10697
10698         EM_DEBUG_FUNC_END("ret [%d]", ret);
10699         return ret;
10700 }
10701
10702 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)
10703 {
10704         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], attachment[%p], transaction[%d], err_code[%p]", mail_id, type, attachment, transaction, err_code);
10705
10706         if (mail_id <= 0 || !attachment) {
10707                 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], attachment[%p]", mail_id, type, attachment);
10708                 if (err_code != NULL)
10709                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10710                 return false;;
10711         }
10712
10713         int rc, ret = false;
10714         int error = EMAIL_ERROR_NONE;
10715         DB_STMT hStmt = NULL;
10716         char sql_query_string[QUERY_SIZE] = {0, };
10717
10718         int i = 0;
10719
10720         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10721
10722         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10723
10724         switch (type) {
10725                 case UPDATE_MAILBOX:
10726                                 EM_DEBUG_LOG("UPDATE_MAILBOX");
10727                         if (!attachment->mailbox_id) {
10728                                 EM_DEBUG_EXCEPTION(" attachment->mailbox_id[%d]", attachment->mailbox_id);
10729                                 error = EMAIL_ERROR_INVALID_PARAM;
10730                                 goto FINISH_OFF;
10731                         }
10732                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
10733                                 "UPDATE mail_attachment_tbl SET mailbox_id = ? WHERE mail_id = %d", mail_id);
10734
10735                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10736                         EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10737                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10738                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10739
10740                         _bind_stmt_field_data_int(hStmt, i++, attachment->mailbox_id);
10741                         break;
10742
10743                 case UPDATE_SAVENAME:
10744                         EM_DEBUG_LOG("UPDATE_SAVENAME");
10745                         if (!attachment->attachment_path) {
10746                                 EM_DEBUG_EXCEPTION(" attachment->attachment_path[%p]", attachment->attachment_path);
10747                                 error = EMAIL_ERROR_INVALID_PARAM;
10748                                 goto FINISH_OFF;
10749                         }
10750
10751                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
10752                                 "UPDATE mail_attachment_tbl SET"
10753                                 "  attachment_size = ?"
10754                                 ", attachment_save_status = ?"
10755                                 ", attachment_path = ?"
10756                                 " WHERE mail_id = %d"
10757                                 " AND attachment_id = %d"
10758                                 , attachment->mail_id
10759                                 , attachment->attachment_id);
10760
10761
10762                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10763                         EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10764                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10765                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10766
10767                         _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_size);
10768                         _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_save_status);
10769                         _bind_stmt_field_data_string(hStmt, i++, (char *)attachment->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10770                         break;
10771
10772                 default:
10773                         EM_DEBUG_LOG("type[%d]", type);
10774                         error = EMAIL_ERROR_INVALID_PARAM;
10775                         goto FINISH_OFF;
10776         }
10777         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10778
10779         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10780         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10781                 ("sqlite3_step fail:%d", rc));
10782         ret = true;
10783
10784 FINISH_OFF:
10785
10786         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10787
10788         if (hStmt != NULL) {
10789                 rc = sqlite3_finalize(hStmt);
10790                 if (rc != SQLITE_OK) {
10791                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10792                         error = EMAIL_ERROR_DB_FAILURE;
10793                 }
10794         }
10795
10796         if (err_code != NULL)
10797                 *err_code = error;
10798         EM_DEBUG_FUNC_END("ret [%d]", ret);
10799         return ret;
10800 }
10801
10802 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)
10803 {
10804         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);
10805
10806         int rc = 0;
10807         int ret = false;
10808         int error = EMAIL_ERROR_NONE;
10809         int field_idx = 0;
10810         int account_id = 0;
10811         char sql_query_string[QUERY_SIZE] = {0, };
10812         char *replaced_mailbox_name = NULL;
10813         char *replaced_alias = NULL;
10814         sqlite3 *local_db_handle = NULL;
10815         DB_STMT hStmt = NULL;
10816         emstorage_mailbox_tbl_t *old_mailbox_data = NULL;
10817
10818         if (input_mailbox_id <= 0 || !input_new_mailbox_name || !input_new_mailbox_alias) {
10819                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10820                 return EMAIL_ERROR_INVALID_PARAM;
10821         }
10822
10823         if (strstr(input_new_mailbox_name, "'")) {
10824                 replaced_mailbox_name = em_replace_all_string(input_new_mailbox_name, "'", "''");
10825         } else {
10826                 replaced_mailbox_name = strdup(input_new_mailbox_name);
10827         }
10828
10829         if (strstr(input_new_mailbox_alias, "'")) {
10830                 replaced_alias = em_replace_all_string(input_new_mailbox_alias, "'", "''");
10831         } else {
10832                 replaced_alias = strdup(input_new_mailbox_alias);
10833         }
10834
10835         local_db_handle = emstorage_get_db_connection(multi_user_name);
10836
10837         if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
10838                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", error);
10839                 EM_SAFE_FREE(replaced_mailbox_name);
10840                 EM_SAFE_FREE(replaced_alias);
10841                 return error;
10842         }
10843
10844     if (old_mailbox_data == NULL) {
10845         EM_DEBUG_LOG("old_mailbox_data is NULL");
10846         error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
10847         goto FINISH_OFF;
10848     }
10849
10850         account_id = old_mailbox_data->account_id;
10851
10852         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
10853
10854         if (input_eas_data && input_eas_data_length > 0) {
10855                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10856                         "UPDATE mail_box_tbl SET"
10857                         " mailbox_name = ?"
10858                         ",alias = ?"
10859                         ",eas_data = ?"
10860                         ",eas_data_length = ?"
10861                         " WHERE mailbox_id = ?");
10862
10863                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10864
10865                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10866                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10867
10868                 _bind_stmt_field_data_string(hStmt, field_idx++, input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10869                 _bind_stmt_field_data_string(hStmt, field_idx++, input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10870                 _bind_stmt_field_data_blob(hStmt, field_idx++, input_eas_data, input_eas_data_length);
10871                 _bind_stmt_field_data_int(hStmt, field_idx++, input_eas_data_length);
10872                 _bind_stmt_field_data_int(hStmt, field_idx++, input_mailbox_id);
10873         } else {
10874                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10875                                 "UPDATE mail_box_tbl SET"
10876                                 " mailbox_name = ?"
10877                                 ",alias = ?"
10878                                 " WHERE mailbox_id = ?");
10879
10880                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10881
10882                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10883                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10884
10885                 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10886                 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10887                 _bind_stmt_field_data_int(hStmt, field_idx++ , input_mailbox_id);
10888         }
10889
10890
10891         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10892         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
10893                 ("sqlite3_step fail:%d", rc));
10894         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10895                 ("sqlite3_step fail:%d", rc));
10896
10897         if (sqlite3_changes(local_db_handle) == 0)
10898                 EM_DEBUG_LOG("no mail_meeting_tbl matched...");
10899
10900         ret = true;
10901
10902 FINISH_OFF:
10903         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, error);
10904         if (error == EMAIL_ERROR_NONE) {
10905                 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME, account_id, input_mailbox_id, input_new_mailbox_name, 0))
10906                         EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAILBOX_RENAME ] >>>> ");
10907         } else {
10908                 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME_FAIL, account_id, input_mailbox_id, input_new_mailbox_name, error))
10909                         EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAILBOX_RENAME_FAIL ] >>>> ");
10910         }
10911
10912         EM_SAFE_FREE(replaced_mailbox_name);
10913         EM_SAFE_FREE(replaced_alias);
10914
10915         if (old_mailbox_data)
10916                 emstorage_free_mailbox(&old_mailbox_data, 1, NULL);
10917
10918         if (hStmt != NULL) {
10919                 rc = sqlite3_finalize(hStmt);
10920                 if (rc != SQLITE_OK) {
10921                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10922                         error = EMAIL_ERROR_DB_FAILURE;
10923                 }
10924         }
10925
10926         EM_DEBUG_FUNC_END("error [%d]", error);
10927         return error;
10928 }
10929
10930 INTERNAL_FUNC int emstorage_get_new_attachment_no(char *multi_user_name, int *attachment_no, int *err_code)
10931 {
10932         EM_DEBUG_FUNC_BEGIN("attachment_no [%p], err_code[%p]", attachment_no, err_code);
10933         int rc, ret = false;
10934         int error = EMAIL_ERROR_NONE;
10935         char *sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
10936         char **result;
10937
10938         if (!attachment_no) {
10939                 EM_DEBUG_EXCEPTION("Invalid attachment");
10940                 if (err_code != NULL)
10941                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10942                 return false;
10943         }
10944
10945         *attachment_no = -1;
10946
10947         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10948
10949
10950         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
10951         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10952                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
10953
10954         if (NULL == result[1])
10955                 rc = 1;
10956         else
10957                 rc = atoi(result[1])+1;
10958
10959         sqlite3_free_table(result);
10960
10961         *attachment_no = rc;
10962         EM_DEBUG_LOG("attachment_no [%d]", *attachment_no);
10963         ret = true;
10964
10965 FINISH_OFF:
10966
10967         if (err_code != NULL)
10968                 *err_code = error;
10969
10970         EM_DEBUG_FUNC_END("ret [%d]", ret);
10971         return ret;
10972 }
10973
10974 INTERNAL_FUNC int emstorage_add_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int iscopy, int transaction, int *err_code)
10975 {
10976         EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], iscopy[%d], transaction[%d], err_code[%p]", attachment_tbl, iscopy, transaction, err_code);
10977
10978         char *sql = NULL;
10979         char **result;
10980         int rc, ret = false;
10981         int error = EMAIL_ERROR_NONE;
10982         char sql_query_string[QUERY_SIZE] = {0, };
10983
10984         DB_STMT hStmt = NULL;
10985         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10986
10987         if (!attachment_tbl) {
10988                 EM_DEBUG_EXCEPTION("attachment_tbl[%p], iscopy[%d]", attachment_tbl, iscopy);
10989                 if (err_code != NULL)
10990                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10991                 return false;
10992         }
10993
10994         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10995
10996         sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
10997
10998         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
10999         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
11000                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
11001
11002         if (NULL == result[1]) rc = 1;
11003         else rc = atoi(result[1]) + 1;
11004         sqlite3_free_table(result);
11005
11006         attachment_tbl->attachment_id = rc;
11007
11008         SNPRINTF(sql_query_string, sizeof(sql_query_string),
11009                 "INSERT INTO mail_attachment_tbl VALUES "
11010                 "(?"    /* attachment_id */
11011                 ", ?"   /* attachment_name */
11012                 ", ?"   /* attachment_path */
11013                 ", ?"   /* content_id */
11014                 ", ?"   /* attachment_size */
11015                 ", ?"   /* mail_id */
11016                 ", ?"   /* account_id */
11017                 ", ?"   /* mailbox_id */
11018                 ", ?"   /* attachment_save_status */
11019                 ", ?"   /* attachment_drm_type */
11020                 ", ?"   /* attachment_drm_method */
11021                 ", ?"   /* attachment_inline_content_status */
11022                 ", ?"   /* attachment_mime_type */
11023 #ifdef __ATTACHMENT_OPTI__
11024                 ", ?"
11025                 ", ?"
11026 #endif
11027                 ")");
11028
11029
11030         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11031         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11032                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11033
11034         _bind_stmt_field_data_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_id);
11035         _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);
11036         _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);
11037         _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);
11038         _bind_stmt_field_data_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_size);
11039         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mail_id);
11040         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->account_id);
11041         _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mailbox_id);
11042         _bind_stmt_field_data_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_save_status);
11043         _bind_stmt_field_data_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_type);
11044         _bind_stmt_field_data_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_method);
11045         _bind_stmt_field_data_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_inline_content_status);
11046         _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);
11047 #ifdef __ATTACHMENT_OPTI__
11048         _bind_stmt_field_data_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->encoding);
11049         _bind_stmt_field_data_string(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->section, 0, ATTACHMENT_LEN_IN_MAIL_ATTACHMENT_TBL);
11050 #endif
11051
11052
11053         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11054         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11055                 ("sqlite3_step fail:%d", rc));
11056         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11057                 ("sqlite3_step fail:%d", rc));
11058
11059         rc = sqlite3_changes(local_db_handle);
11060         if (rc == 0) {
11061                 EM_DEBUG_LOG(" no matched mail found...");
11062                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11063                 goto FINISH_OFF;
11064         }
11065
11066         ret = true;
11067
11068 FINISH_OFF:
11069         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11070
11071         if (hStmt != NULL) {
11072                 rc = sqlite3_finalize(hStmt);
11073                 if (rc != SQLITE_OK) {
11074                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11075                         error = EMAIL_ERROR_DB_FAILURE;
11076                 }
11077         }
11078
11079         if (err_code != NULL)
11080         *err_code = error;
11081
11082         EM_DEBUG_FUNC_END("ret [%d]", ret);
11083         return ret;
11084 }
11085
11086 INTERNAL_FUNC int emstorage_update_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int transaction, int *err_code)
11087 {
11088         EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], transaction[%d], err_code[%p]", attachment_tbl, transaction, err_code);
11089
11090         int rc, ret = false, field_idx = 0;
11091         int error = EMAIL_ERROR_NONE;
11092         DB_STMT hStmt = NULL;
11093         char sql_query_string[QUERY_SIZE] = {0, };
11094
11095         if (!attachment_tbl) {
11096                 EM_DEBUG_EXCEPTION(" attachment_tbl[%p] ", attachment_tbl);
11097                 if (err_code)
11098                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11099                 return false;
11100         }
11101
11102         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11103
11104         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11105
11106         SNPRINTF(sql_query_string, sizeof(sql_query_string),
11107                 "UPDATE mail_attachment_tbl SET  "
11108                 "  attachment_name = ?"
11109                 ", attachment_path =  ?"
11110                 ", content_id = ?"
11111                 ", attachment_size = ?"
11112                 ", mail_id = ?"
11113                 ", account_id = ?"
11114                 ", mailbox_id = ?"
11115                 ", attachment_save_status = ?"
11116                 ", attachment_drm_type = ?"
11117                 ", attachment_drm_method = ?"
11118                 ", attachment_inline_content_status = ? "
11119                 ", attachment_mime_type = ? "
11120                 " WHERE attachment_id = ?;");
11121
11122
11123         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11124
11125         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11126                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11127
11128         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
11129         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
11130         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->content_id, 0, CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL);
11131         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_size);
11132         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->mail_id);
11133         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->account_id);
11134         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->mailbox_id);
11135         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_save_status);
11136         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_drm_type);
11137         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_drm_method);
11138         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_inline_content_status);
11139         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_mime_type, 0, ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL);
11140         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_id);
11141
11142
11143         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11144         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11145                 ("sqlite3_step fail:%d", rc));
11146         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11147                 ("sqlite3_step fail:%d", rc));
11148
11149         rc = sqlite3_changes(local_db_handle);
11150         if (rc == 0) {
11151                 EM_DEBUG_LOG(" no matched mail found...");
11152                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11153                 goto FINISH_OFF;
11154         }
11155
11156         ret = true;
11157
11158 FINISH_OFF:
11159         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11160         if (hStmt != NULL) {
11161                 rc = sqlite3_finalize(hStmt);
11162                 if (rc != SQLITE_OK) {
11163                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11164                         error = EMAIL_ERROR_DB_FAILURE;
11165                 }
11166         }
11167
11168         if (err_code != NULL)
11169         *err_code = error;
11170
11171         EM_DEBUG_FUNC_END("ret [%d]", ret);
11172         return ret;
11173 }
11174
11175 INTERNAL_FUNC int emstorage_delete_attachment_on_db(char *multi_user_name, int attachment_id, int transaction, int *err_code)
11176 {
11177         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], transaction[%d], err_code[%p]", attachment_id, transaction, err_code);
11178
11179         if (attachment_id < 0) {
11180                 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
11181                 if (err_code != NULL)
11182                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11183                 return false;
11184         }
11185
11186         int ret = false;
11187         int error = EMAIL_ERROR_NONE;
11188         char sql_query_string[QUERY_SIZE] = {0, };
11189         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11190
11191         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11192
11193         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
11194
11195         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11196         if (error != EMAIL_ERROR_NONE) {
11197                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11198                         goto FINISH_OFF;
11199         }
11200
11201         ret = true;
11202
11203 FINISH_OFF:
11204         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11205
11206         if (err_code)
11207                 *err_code = error;
11208
11209         EM_DEBUG_FUNC_END("ret [%d]", ret);
11210         return ret;
11211 }
11212
11213 INTERNAL_FUNC int emstorage_delete_all_attachments_of_mail(char *multi_user_name, int mail_id, int transaction, int *err_code)
11214 {
11215         EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
11216         int ret = false;
11217         int error = EMAIL_ERROR_NONE;
11218         char sql_query_string[QUERY_SIZE] = {0, };
11219         sqlite3 *local_db_handle = NULL;
11220
11221         if (mail_id <= 0) {
11222                 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
11223                 if (err_code != NULL)
11224                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11225                 return false;
11226         }
11227
11228         local_db_handle = emstorage_get_db_connection(multi_user_name);
11229
11230         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11231
11232         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
11233         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11234         if (error != EMAIL_ERROR_NONE) {
11235                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11236                         goto FINISH_OFF;
11237         }
11238
11239         ret = true;
11240
11241 FINISH_OFF:
11242         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11243
11244         if (err_code)
11245                 *err_code = error;
11246
11247         EM_DEBUG_FUNC_END("ret [%d]", ret);
11248         return ret;
11249 }
11250
11251 INTERNAL_FUNC int emstorage_delete_attachment_all_on_db(char *multi_user_name, int account_id, char *mailbox, int transaction, int *err_code)
11252 {
11253         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox, transaction, err_code);
11254
11255         int error = EMAIL_ERROR_NONE;
11256         int ret = false;
11257         char sql_query_string[QUERY_SIZE] = {0, };
11258         char *replaced_mailbox = NULL;
11259         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11260
11261         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11262
11263         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl");
11264
11265         if (account_id != ALL_ACCOUNT) /*  '0' means all account */
11266                 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);
11267
11268         if (mailbox)    /*  NULL means all mailbox_name */ {
11269                 if (strstr(mailbox, "'")) {
11270                         replaced_mailbox = em_replace_all_string(mailbox, "'", "''");
11271                 } else {
11272                         replaced_mailbox = strdup(mailbox);
11273                 }
11274
11275                 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);
11276                 EM_SAFE_FREE(replaced_mailbox); /*prevent 49434*/
11277         }
11278
11279         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11280         if (error != EMAIL_ERROR_NONE) {
11281                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11282                         goto FINISH_OFF;
11283         }
11284
11285         ret = true;
11286
11287 FINISH_OFF:
11288         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11289
11290         if (err_code != NULL)
11291                 *err_code = error;
11292
11293         EM_DEBUG_FUNC_END("ret [%d]", ret);
11294         return ret;
11295 }
11296
11297 INTERNAL_FUNC int emstorage_free_attachment(emstorage_attachment_tbl_t** attachment_tbl_list, int count, int *err_code)
11298 {
11299         EM_DEBUG_FUNC_BEGIN("attachment_tbl_list[%p], count[%d], err_code[%p]", attachment_tbl_list, count, err_code);
11300
11301         if (count > 0) {
11302                 if ((attachment_tbl_list == NULL) || (*attachment_tbl_list == NULL)) {
11303                         EM_DEBUG_LOG("Nothing to free: attachment_tbl_list[%p], count[%d]", attachment_tbl_list, count);
11304                         if (err_code != NULL)
11305                                 *err_code = EMAIL_ERROR_INVALID_PARAM;
11306                         return false;
11307                 }
11308
11309                 emstorage_attachment_tbl_t* p = *attachment_tbl_list;
11310                 int i;
11311
11312                 for (i = 0; i < count; i++) {
11313                         EM_SAFE_FREE(p[i].attachment_name);
11314                         EM_SAFE_FREE(p[i].attachment_path);
11315                         EM_SAFE_FREE(p[i].content_id);
11316                         EM_SAFE_FREE(p[i].attachment_mime_type);
11317 #ifdef __ATTACHMENT_OPTI__
11318                         EM_SAFE_FREE(p[i].section);
11319 #endif
11320                 }
11321
11322                 EM_SAFE_FREE(p);
11323                 *attachment_tbl_list = NULL;
11324         }
11325
11326         if (err_code != NULL)
11327                 *err_code = EMAIL_ERROR_NONE;
11328         EM_DEBUG_FUNC_END();
11329         return true;
11330 }
11331
11332 INTERNAL_FUNC int emstorage_begin_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11333 {
11334         EM_PROFILE_BEGIN(emStorageBeginTransaction);
11335         int ret = true;
11336
11337         _timedlock_shm_mutex(mapped_for_db_lock, 2);
11338
11339         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11340
11341         int rc;
11342         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN immediate;", NULL, NULL, NULL), rc);
11343         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11344                 ("SQL(BEGIN) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11345
11346         if (ret == false) {
11347                 if (err_code != NULL) *err_code = EMAIL_ERROR_DB_FAILURE;
11348         }
11349
11350         EM_PROFILE_END(emStorageBeginTransaction);
11351         EM_DEBUG_FUNC_END("ret [%d]", ret);
11352         return ret;
11353 }
11354
11355 INTERNAL_FUNC int emstorage_commit_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11356 {
11357         EM_DEBUG_FUNC_BEGIN();
11358         int ret = true;
11359         int rc;
11360
11361         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11362
11363         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
11364         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; }, ("SQL(END) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11365
11366         if (ret == false && err_code != NULL)
11367                 *err_code = EMAIL_ERROR_DB_FAILURE;
11368
11369         _unlockshm_mutex(mapped_for_db_lock);
11370
11371         EM_DEBUG_FUNC_END("ret [%d]", ret);
11372         return ret;
11373 }
11374
11375 INTERNAL_FUNC int emstorage_rollback_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11376 {
11377         EM_DEBUG_FUNC_BEGIN();
11378         int ret = true;
11379         int rc;
11380         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11381
11382         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "ROLLBACK;", NULL, NULL, NULL), rc);
11383         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11384                 ("SQL(ROLLBACK) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11385
11386         if (ret == false && err_code != NULL)
11387                 *err_code = EMAIL_ERROR_DB_FAILURE;
11388
11389         _unlockshm_mutex(mapped_for_db_lock);
11390
11391         EM_DEBUG_FUNC_END("ret [%d]", ret);
11392         return ret;
11393 }
11394
11395 INTERNAL_FUNC int emstorage_is_mailbox_full(char *multi_user_name, int account_id, email_mailbox_t *mailbox, int *result, int *err_code)
11396 {
11397         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], result[%p], err_code[%p]", account_id, mailbox, result, err_code);
11398
11399         if (account_id < FIRST_ACCOUNT_ID || !mailbox || !result) {
11400                 if (mailbox)
11401                         EM_DEBUG_EXCEPTION("Invalid Parameter. accoun_id[%d], mailbox[%p]", account_id, mailbox);
11402
11403                 if (err_code != NULL)
11404                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11405
11406                 return false;
11407         }
11408
11409         int ret = false;
11410         int error = EMAIL_ERROR_NONE;
11411         int mail_count = 0;
11412
11413         if (!emstorage_get_mail_count(multi_user_name, account_id, mailbox->mailbox_id, &mail_count, NULL, true, &error)) {
11414                 EM_DEBUG_EXCEPTION("emstorage_get_mail_count failed [%d]", error);
11415                 goto FINISH_OFF;
11416         }
11417
11418         if (mailbox) {
11419                 EM_DEBUG_LOG("mail_count[%d] mail_slot_size[%d]", mail_count, mailbox->mail_slot_size);
11420                 if (mail_count >= mailbox->mail_slot_size)
11421                         *result = true;
11422                 else
11423                         *result = false;
11424
11425                 ret = true;
11426         }
11427
11428         ret = true;
11429 FINISH_OFF:
11430
11431         if (err_code != NULL)
11432                 *err_code = error;
11433
11434         EM_DEBUG_FUNC_END("ret [%d]", ret);
11435         return ret;
11436 }
11437
11438 INTERNAL_FUNC int emstorage_clear_mail_data(char *multi_user_name, int transaction, int *err_code)
11439 {
11440         EM_DEBUG_FUNC_BEGIN("transaction[%d], err_code[%p]", transaction, err_code);
11441
11442         int ret = false;
11443         int error = EMAIL_ERROR_NONE;
11444         char sql_query_string[QUERY_SIZE] = {0, };
11445         const email_db_object_t* tables = _g_db_tables;
11446         const email_db_object_t* indexes = _g_db_indexes;
11447
11448         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11449         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11450
11451         if (!emstorage_delete_dir((char *)MAILHOME, &error)) {
11452                 EM_DEBUG_EXCEPTION(" emstorage_delete_dir failed - %d", error);
11453
11454                 goto FINISH_OFF;
11455         }
11456
11457         mkdir(MAILHOME, DIRECTORY_PERMISSION);
11458         mkdir(MAILTEMP, DIRECTORY_PERMISSION);
11459         chmod(MAILTEMP, 0777);
11460
11461         /*  first clear index. */
11462         while (indexes->object_name) {
11463                 if (indexes->data_flag) {
11464                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP index %s", indexes->object_name);
11465                         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11466                         if (error != EMAIL_ERROR_NONE) {
11467                                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11468                                         goto FINISH_OFF;
11469                         }
11470                 }
11471                 indexes++;
11472         }
11473
11474         while (tables->object_name) {
11475                 if (tables->data_flag) {
11476                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP table %s", tables->object_name);
11477                         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11478                         if (error != EMAIL_ERROR_NONE) {
11479                                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11480                                         goto FINISH_OFF;
11481                         }
11482                 }
11483
11484                 tables++;
11485         }
11486         ret = true;
11487
11488 FINISH_OFF:
11489         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11490
11491         if (err_code != NULL)
11492                 *err_code = error;
11493
11494         EM_DEBUG_FUNC_END("ret [%d]", ret);
11495         return ret;
11496 }
11497 /*======================= DB File Utils =============================================*/
11498 #include <dirent.h>
11499 #include <sys/types.h>
11500 #define  DIR_SEPERATOR "/"
11501
11502 INTERNAL_FUNC char *emstorage_make_directory_path_from_file_path(char *file_name)
11503 {
11504         EM_DEBUG_FUNC_BEGIN("Filename [ %p ]", file_name);
11505         char delims[] = "/";
11506         char *result = NULL;
11507         gchar **token = NULL;
11508
11509         token = g_strsplit_set(file_name, delims, 1);
11510
11511         if (token && token[0]) {
11512                 EM_DEBUG_LOG_SEC(">>>> Directory_name [ %s ]", token[0]);
11513                 result = EM_SAFE_STRDUP(token[0]);
11514         } else
11515                 EM_DEBUG_LOG(">>>> No Need to create Directory");
11516
11517         g_strfreev(token);
11518
11519         return result;
11520 }
11521
11522 INTERNAL_FUNC int emstorage_get_save_name(char *multi_user_name, int account_id, int mail_id, int atch_id,
11523                                                                                         char *fname, char *move_buf, char *path_buf, int maxlen, int *err_code)
11524 {
11525         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);
11526         EM_PROFILE_BEGIN(profile_emstorage_get_save_name);
11527
11528         int ret = false;
11529         int error = EMAIL_ERROR_NONE;
11530         char *dir_name = NULL;
11531         char create_dir[1024] = {0};
11532         char *temp_file = NULL;
11533         char *prefix_path = NULL;
11534         char *modified_fname = NULL;
11535
11536         if (!move_buf || !path_buf || account_id < FIRST_ACCOUNT_ID || mail_id < 0 || atch_id < 0) {
11537                 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);
11538                 error = EMAIL_ERROR_INVALID_PARAM;
11539                 goto FINISH_OFF;
11540         }
11541
11542         snprintf(path_buf, 512, "%s", MAILHOME);
11543         snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, account_id);
11544
11545         if (mail_id > 0)
11546                 snprintf(path_buf+EM_SAFE_STRLEN(path_buf),   512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, mail_id);
11547
11548         if (atch_id > 0)
11549                 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, atch_id);
11550
11551         if (fname) {
11552                 temp_file = EM_SAFE_STRDUP(fname);
11553                 if (temp_file && strstr(temp_file, "/")) {
11554                         dir_name = emstorage_make_directory_path_from_file_path(temp_file);
11555                 }
11556         }
11557
11558         if (dir_name) {
11559                 snprintf(create_dir, sizeof(create_dir), "%s%s%s", path_buf, DIR_SEPERATOR, dir_name);
11560                 EM_DEBUG_LOG(">>>>> DIR PATH [%s]", create_dir);
11561                 mkdir(create_dir, DIRECTORY_PERMISSION);
11562                 EM_SAFE_FREE(dir_name);
11563         }
11564
11565         if (fname) {
11566                 EM_DEBUG_LOG_DEV(">>>>> fname [%s]", fname);
11567
11568                 /* Did not allow the slash */
11569                 modified_fname = reg_replace_new(fname, "/", "_");
11570                 EM_DEBUG_LOG("modified_fname : [%s]", modified_fname);
11571
11572                 if (modified_fname == NULL) modified_fname = g_strdup(fname);
11573
11574                 if (EM_SAFE_STRLEN(modified_fname) + EM_SAFE_STRLEN(path_buf) + strlen(DIR_SEPERATOR) > maxlen - 1) {
11575                         char *modified_name = NULL;
11576                         int remain_len  = (maxlen - 1) - EM_SAFE_STRLEN(path_buf) - strlen(DIR_SEPERATOR);
11577
11578                         if (remain_len <= 0) {
11579                                 error = EMAIL_ERROR_MAX_EXCEEDED;
11580                                 goto FINISH_OFF;
11581                         }
11582
11583                         if (remain_len > MAX_FILENAME) {
11584                                 remain_len = MAX_FILENAME;
11585                         }
11586
11587                         modified_name = em_shrink_filename(modified_fname, remain_len);
11588
11589                         if (!modified_name) {
11590                                 error = EMAIL_ERROR_MAX_EXCEEDED;
11591                                 goto FINISH_OFF;
11592                         }
11593
11594                         snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),"%s%s", DIR_SEPERATOR, modified_name);
11595                         EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11596                         EM_SAFE_FREE(modified_name);
11597                 } else {
11598                         if (EM_SAFE_STRLEN(modified_fname) > MAX_FILENAME - 1) {
11599                                 char *modified_name = NULL;
11600
11601                                 modified_name = em_shrink_filename(modified_fname, MAX_FILENAME);
11602                                 if (!modified_name) {
11603                                         error = EMAIL_ERROR_MAX_EXCEEDED;
11604                                         goto FINISH_OFF;
11605                                 }
11606
11607                                 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),"%s%s", DIR_SEPERATOR, modified_name);
11608                                 EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11609                                 EM_SAFE_FREE(modified_name);
11610                         } else {
11611                                 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),"%s%s", DIR_SEPERATOR, modified_fname);
11612                         }
11613                 }
11614         }
11615
11616         EM_DEBUG_LOG_SEC(">>>>> path_buf [%s]", path_buf);
11617
11618     if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11619                 error = emcore_get_container_path(multi_user_name, &prefix_path);
11620                 if (error != EMAIL_ERROR_NONE) {
11621                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11622                         goto FINISH_OFF;
11623                 }
11624         snprintf(move_buf, 512, "%s/%s", prefix_path, path_buf);
11625         EM_DEBUG_LOG("move_buf : [%s]", move_buf);
11626     } else {
11627         snprintf(move_buf, 512, "%s", path_buf);
11628         EM_DEBUG_LOG("move_buf : [%s]", move_buf);
11629     }
11630
11631         ret = true;
11632
11633 FINISH_OFF:
11634
11635         EM_SAFE_FREE(temp_file);
11636         EM_SAFE_FREE(prefix_path);
11637         EM_SAFE_FREE(modified_fname);
11638
11639         if (err_code != NULL)
11640                 *err_code = error;
11641
11642         EM_PROFILE_END(profile_emstorage_get_save_name);
11643         EM_DEBUG_FUNC_END("ret [%d]", ret);
11644         return ret;
11645 }
11646
11647 /*
11648 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)
11649 {
11650         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);
11651
11652         if (!name_buf || account_id < FIRST_ACCOUNT_ID) {
11653                 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);
11654                 if (err_code != NULL)
11655                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11656                 return false;
11657         }
11658
11659         sprintf(name_buf+EM_SAFE_STRLEN(name_buf),      "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
11660
11661         if (mail_id > 0)
11662                 sprintf(name_buf+EM_SAFE_STRLEN(name_buf),      "%s%d", DIR_SEPERATOR, mail_id);
11663         else
11664                 goto FINISH_OFF;
11665
11666         if (atch_id > 0)
11667                 sprintf(name_buf+EM_SAFE_STRLEN(name_buf),      "%s%d", DIR_SEPERATOR, atch_id);
11668         else
11669                 goto FINISH_OFF;
11670
11671 FINISH_OFF:
11672         sprintf(name_buf+EM_SAFE_STRLEN(name_buf),      ".DELE");
11673
11674         EM_DEBUG_FUNC_END();
11675         return true;
11676 }
11677 */
11678
11679 INTERNAL_FUNC int emstorage_create_dir(char *multi_user_name, int account_id, int mail_id, int atch_id, int *err_code)
11680 {
11681         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], err_code[%p]", account_id, mail_id, atch_id, err_code);
11682         EM_PROFILE_BEGIN(profile_emcore_save_create_dir);
11683         int ret = false;
11684         int error = EMAIL_ERROR_NONE;
11685         char buf[512];
11686         struct stat sbuf;
11687         char *prefix_path = NULL;
11688         char errno_buf[ERRNO_BUF_SIZE] = {0};
11689
11690         memset(buf, 0x00, sizeof(buf));
11691
11692     if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11693                 error = emcore_get_container_path(multi_user_name, &prefix_path);
11694                 if (error != EMAIL_ERROR_NONE) {
11695                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11696                         goto FINISH_OFF;
11697                 }
11698         } else {
11699                 prefix_path = strdup("");
11700         }
11701
11702         if (account_id >= FIRST_ACCOUNT_ID) {
11703                 SNPRINTF(buf, sizeof(buf), "%s%s%s%s%d", prefix_path,
11704                                                                                                 DIR_SEPERATOR,
11705                                                                                                 MAILHOME,
11706                                                                                                 DIR_SEPERATOR,
11707                                                                                                 account_id);
11708
11709                 if (stat(buf, &sbuf) == 0) {
11710                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11711                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11712                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11713                                 goto FINISH_OFF;
11714                         }
11715                 } else {
11716                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11717                                 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11718                                 EM_DEBUG_EXCEPTION("mkdir failed: %s", EM_STRERROR(errno_buf));
11719                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11720                                 if (errno == 28)
11721                                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11722                                 goto FINISH_OFF;
11723                         }
11724
11725                         if (account_id == EML_FOLDER) {
11726                                 chmod(buf, 0777);
11727                         }
11728                 }
11729         }
11730
11731         if (mail_id > 0) {
11732                 int space_left_in_buffer = sizeof(buf) - EM_SAFE_STRLEN(buf);
11733
11734                 if (account_id < FIRST_ACCOUNT_ID) {
11735                         EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11736                         error = EMAIL_ERROR_INVALID_PARAM;
11737                         goto FINISH_OFF;
11738                 }
11739
11740                 if (space_left_in_buffer + 10 > sizeof(buf)) {
11741                         EM_DEBUG_EXCEPTION("Buffer overflowed");
11742                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11743                         goto FINISH_OFF;
11744                 }
11745
11746                 SNPRINTF(buf+EM_SAFE_STRLEN(buf), space_left_in_buffer, "%s%d", DIR_SEPERATOR, mail_id);
11747
11748                 if (stat(buf, &sbuf) == 0) {
11749                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11750                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11751                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11752                                 goto FINISH_OFF;
11753                         }
11754                 } else {
11755                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11756                                 EM_DEBUG_EXCEPTION("mkdir failed [%s]", buf);
11757                                 EM_DEBUG_EXCEPTION("mkdir failed [%d][%s]", errno, EM_STRERROR(errno_buf));
11758                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11759                                 if (errno == 28)
11760                                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11761                                 goto FINISH_OFF;
11762                         }
11763
11764                         if (account_id == EML_FOLDER) {
11765                                 chmod(buf, 0777);
11766                         }
11767                 }
11768         }
11769
11770         if (atch_id > 0) {
11771                 if (account_id < FIRST_ACCOUNT_ID || mail_id <= 0) {
11772                         EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11773                         error = EMAIL_ERROR_INVALID_PARAM;
11774                         goto FINISH_OFF;
11775                 }
11776
11777                 SNPRINTF(buf+EM_SAFE_STRLEN(buf), sizeof(buf)-(EM_SAFE_STRLEN(buf)+1), "%s%d", DIR_SEPERATOR, atch_id);
11778
11779                 if (stat(buf, &sbuf) == 0) {
11780                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11781                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11782                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11783                                 goto FINISH_OFF;
11784                         }
11785                 } else {
11786                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11787                                 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11788                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11789                                 if (errno == 28)
11790                                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11791                                 goto FINISH_OFF;
11792                         }
11793
11794                         if (account_id == EML_FOLDER) {
11795                                 chmod(buf, 0777);
11796                         }
11797                 }
11798         }
11799
11800         ret = true;
11801
11802 FINISH_OFF:
11803
11804         EM_SAFE_FREE(prefix_path);
11805
11806         if (err_code != NULL)
11807                 *err_code = error;
11808
11809         EM_PROFILE_END(profile_emcore_save_create_dir);
11810         EM_DEBUG_FUNC_END("ret [%d]", ret);
11811         return ret;
11812 }
11813
11814 INTERNAL_FUNC int emstorage_copy_file(char *src_file, char *dst_file, int sync_status, int *err_code)
11815 {
11816         EM_DEBUG_FUNC_BEGIN("src_file[%s], dst_file[%s], err_code[%p]", src_file, dst_file, err_code);
11817         EM_DEBUG_LOG("Using the fsync function");
11818         int ret = false;
11819         int error = EMAIL_ERROR_NONE;
11820         struct stat st_buf;
11821
11822         int fp_src = 0;
11823         int fp_dst = 0;
11824         int nread = 0;
11825         char buf[FILE_MAX_BUFFER_SIZE] = {0};
11826         char errno_buf[ERRNO_BUF_SIZE] = {0};
11827
11828         if (!src_file || !dst_file) {
11829                 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
11830
11831                 error = EMAIL_ERROR_INVALID_PARAM;
11832                 goto FINISH_OFF;
11833         }
11834
11835         if (stat(src_file, &st_buf) < 0) {
11836                 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", src_file);
11837
11838                 error = EMAIL_ERROR_SYSTEM_FAILURE;             /* EMAIL_ERROR_INVALID_PATH; */
11839                 goto FINISH_OFF;
11840         }
11841
11842         error = em_open(src_file, O_RDONLY, 0, &fp_src);
11843         if (error != EMAIL_ERROR_NONE) {
11844                         EM_DEBUG_EXCEPTION(">>>> Source Fail em_open %s Failed: %d", src_file, error);
11845                         goto FINISH_OFF;
11846         }
11847
11848         error = em_open(dst_file, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, &fp_dst); /*prevent 24474*/
11849         if (error != EMAIL_ERROR_NONE) {
11850                         EM_DEBUG_EXCEPTION(">>>> Destination Fail em_open %s:  %d", dst_file, error);
11851                         goto FINISH_OFF;
11852         }
11853
11854         memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
11855
11856         while ((nread = read(fp_src, buf, FILE_MAX_BUFFER_SIZE)) > 0) {
11857                 if (nread > 0 && nread <= FILE_MAX_BUFFER_SIZE) {
11858                         EM_DEBUG_LOG("Nread Value [%d]", nread);
11859                         char *buf_ptr;
11860                         ssize_t byte_written = 0;
11861                         size_t remain_byte = nread;
11862                         buf_ptr = buf;
11863                         errno = 0;
11864
11865                         while (remain_byte > 0 && buf_ptr && errno == 0) {
11866                                 byte_written = write(fp_dst, buf_ptr, remain_byte);
11867
11868                                 if (byte_written < 0) {
11869                                         /* interrupted by a signal */
11870                                         if (errno == EINTR) {
11871                                                 errno = 0;
11872                                                 continue;
11873                                         }
11874
11875                                         EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
11876                                         error = EMAIL_ERROR_UNKNOWN;
11877                                         goto FINISH_OFF;
11878                                 }
11879                                 EM_DEBUG_LOG("NWRITTEN [%d]", byte_written);
11880                                 remain_byte -= byte_written;
11881                                 buf_ptr += byte_written;
11882                         }
11883                 }
11884
11885                 memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
11886         }
11887
11888         ret = true;
11889
11890 FINISH_OFF:
11891         EM_SAFE_CLOSE(fp_src);
11892
11893         if (fp_dst >= 0) { /*prevent 24474*/
11894                 if (sync_status) {
11895                         EM_DEBUG_LOG("Before fsync");
11896                         fsync(fp_dst);
11897                 }
11898                 close(fp_dst);
11899         }
11900
11901         if (nread < 0 || error == EMAIL_ERROR_UNKNOWN)
11902                 remove(dst_file);
11903
11904         if (err_code != NULL)
11905                 *err_code = error;
11906         EM_DEBUG_FUNC_END("ret [%d]", ret);
11907         return ret;
11908 }
11909 /* create Directory if user has deleted [deepam.p@samsung.com] */
11910 INTERNAL_FUNC void emstorage_create_dir_if_delete()
11911 {
11912         EM_DEBUG_FUNC_BEGIN();
11913
11914         mkdir(DATA_PATH, DIRECTORY_PERMISSION);
11915         mkdir(EMAILPATH, DIRECTORY_PERMISSION);
11916         mkdir(MAILHOME, DIRECTORY_PERMISSION);
11917         mkdir(MAILTEMP, DIRECTORY_PERMISSION);
11918         chmod(MAILTEMP, 0777);
11919
11920         EM_DEBUG_FUNC_END();
11921 }
11922
11923 static int _get_temp_file_name(char **filename, int *err_code)
11924 {
11925         EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
11926
11927         int ret = false;
11928         int error = EMAIL_ERROR_NONE;
11929
11930         if (filename == NULL) {
11931                 EM_DEBUG_EXCEPTION(" filename[%p]", filename);
11932                 error = EMAIL_ERROR_INVALID_PARAM;
11933                 goto FINISH_OFF;
11934         }
11935
11936         char tempname[512] = {0x00, };
11937         struct timeval tv;
11938
11939         gettimeofday(&tv, NULL);
11940         srand(tv.tv_usec);
11941         unsigned int seed = time(NULL);
11942         SNPRINTF(tempname, sizeof(tempname), "%s%c%d", MAILTEMP, '/', rand_r(&seed));
11943
11944         char *p = EM_SAFE_STRDUP(tempname);
11945         if (p == NULL) {
11946                 EM_DEBUG_EXCEPTION(" strdup failed...");
11947                 error = EMAIL_ERROR_OUT_OF_MEMORY;
11948                 goto FINISH_OFF;
11949         }
11950
11951         *filename = p;
11952
11953         ret = true;
11954
11955 FINISH_OFF:
11956         if (err_code != NULL)
11957                 *err_code = error;
11958
11959         EM_DEBUG_FUNC_END("ret [%d]", ret);
11960         return ret;
11961 }
11962
11963 INTERNAL_FUNC int emstorage_add_content_type(char *file_path, char *char_set, int *err_code)
11964 {
11965         EM_DEBUG_FUNC_BEGIN("File path [ %p ]  Character Set [ %p ] err_code [ %p]", file_path, char_set, err_code);
11966
11967         EM_IF_NULL_RETURN_VALUE(file_path, false);
11968         EM_IF_NULL_RETURN_VALUE(char_set, false);
11969         EM_IF_NULL_RETURN_VALUE(err_code, false);
11970
11971         char *buf =  NULL;
11972         char *buf1 = NULL;
11973         struct stat st_buf;
11974         int buf_size = 0;
11975         char *low_char_set = NULL;
11976         char *match_str = NULL;
11977         int nwritten = 0;
11978         int ret = false;
11979         int error = EMAIL_ERROR_NONE;
11980         int data_count_to_written = 0;
11981         char *temp_file_name = NULL;
11982         int err = 0;
11983         FILE* fp_src = NULL;
11984         FILE* fp_dest = NULL;
11985         int nread = 0;
11986
11987
11988         if (stat(file_path, &st_buf) < 0) {
11989                 EM_DEBUG_EXCEPTION_SEC(" stat(\"%s\") failed...", file_path);
11990                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11991                 goto FINISH_OFF;
11992         }
11993
11994         buf_size =  st_buf.st_size;
11995         EM_DEBUG_LOG(">>>> File Size [ %d ] ", buf_size);
11996         buf = (char *)calloc(1, buf_size+1);
11997
11998         if (!buf) {
11999                 EM_DEBUG_LOG(">>> Memory cannot be allocated ");
12000                 goto FINISH_OFF;
12001         }
12002
12003         error = em_fopen(file_path, "rb", &fp_src);
12004         if (error != EMAIL_ERROR_NONE || fp_src == NULL) {
12005                 EM_DEBUG_EXCEPTION_SEC(" file_path fopen failed - %s [%d]", file_path, error);
12006                 goto FINISH_OFF;
12007         }
12008
12009         if ((nread = fread(buf, 1, buf_size, fp_src)) > 0) {
12010                 if (nread > 0 && nread <= buf_size) {
12011                         EM_DEBUG_LOG(">>>> Nread Value [ %d ] ", nread);
12012
12013                         /**
12014                           *   1.Add check for whether content type is there.
12015                           *   2. If not based on the character set, Append it in File
12016                           **/
12017
12018                         low_char_set = calloc(1, EM_SAFE_STRLEN(char_set) + strlen(" \" /></head>") +1); /*prevent 34359*/
12019                         if (low_char_set == NULL) {
12020                                 EM_DEBUG_EXCEPTION("calloc failed");
12021                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12022                                 goto FINISH_OFF;
12023                         }
12024
12025                         strncat(low_char_set, char_set, EM_SAFE_STRLEN(char_set));
12026                         EM_DEBUG_LOG(">>>> CHAR SET [ %s ] ", low_char_set);
12027                         strncat(low_char_set, " \" /></head>", strlen(" \" /></head>")); /*prevent 34359*/
12028                         EM_DEBUG_LOG(">>> CHARSET [ %s ] ", low_char_set);
12029                         match_str = strstr(buf, CONTENT_TYPE_DATA);
12030
12031                         if (match_str == NULL) {
12032                                 EM_DEBUG_LOG(">>>>emstorage_add_content_type 3 ");
12033                                 if (fp_src != NULL) {
12034                                         fclose(fp_src);
12035                                         fp_src = NULL;
12036                                 }
12037                                 data_count_to_written = EM_SAFE_STRLEN(low_char_set)+strlen(CONTENT_DATA)+1; /*prevent 34359*/
12038                                 buf1 = (char *)calloc(1, data_count_to_written);
12039
12040                                 if (buf1) {
12041                                         strncat(buf1, CONTENT_DATA, strlen(CONTENT_DATA)); /*prevent 34359*/
12042                                         EM_DEBUG_LOG(">>>>> BUF 1 [ %s ] ", buf1);
12043                                         strncat(buf1, low_char_set, EM_SAFE_STRLEN(low_char_set));
12044                                         EM_DEBUG_LOG(">>>> HTML TAG DATA  [ %s ] ", buf1);
12045
12046                                         /* 1. Create a temporary file name */
12047                                         if (!_get_temp_file_name(&temp_file_name, &err)) {
12048                                                         EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed - %d", err);
12049                                                         if (err_code != NULL) *err_code = err;
12050                                                         goto FINISH_OFF;
12051                                         }
12052                                         EM_DEBUG_LOG_SEC(">>>>>>> TEMP APPEND FILE PATH [ %s ] ", temp_file_name);
12053
12054                                         /* Open the Temp file in Append mode */
12055                                         error = em_fopen(temp_file_name, "ab", &fp_dest);
12056                                         if (error != EMAIL_ERROR_NONE) {
12057                                                 EM_DEBUG_EXCEPTION_SEC(" fopen failed - %s [%d]", temp_file_name, error);
12058                                                 goto FINISH_OFF;
12059                                         }
12060
12061                                         /* 2. write the Latest data */
12062                                         nwritten = fwrite(buf1, data_count_to_written-1, 1, fp_dest);
12063
12064                                         if (nwritten > 0) {
12065                                                 EM_DEBUG_LOG(" Latest Data  : [%d ] bytes written ", nwritten);
12066                                                 nwritten = 0;
12067                                                 /* 3. Append old data */
12068                                                 nwritten = fwrite(buf, nread-1, 1, fp_dest);
12069
12070                                                 if (nwritten <= 0) {
12071                                                         EM_DEBUG_EXCEPTION(" Error Occured while writing Old data : [%d ] bytes written ", nwritten);
12072                                                         error = EMAIL_ERROR_SYSTEM_FAILURE;
12073                                                         goto FINISH_OFF;
12074                                                 } else {
12075                                                         EM_DEBUG_LOG(">>>> OLD data appended [ %d ] ", nwritten);
12076
12077                                                         if (!emstorage_move_file(temp_file_name, file_path, false, &err)) {
12078                                                                 EM_DEBUG_EXCEPTION(" emstorage_move_file failed - %d", err);
12079                                                                 goto FINISH_OFF;
12080                                                         }
12081                                                 }
12082
12083                                         } else {
12084                                                 EM_DEBUG_EXCEPTION(" Error Occured while writing New data : [%d ] bytes written ", nwritten);
12085                                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12086                                                 goto FINISH_OFF;
12087                                         }
12088                                 }
12089                         }
12090                 }
12091         }
12092
12093         ret = true;
12094 FINISH_OFF:
12095
12096         EM_SAFE_FREE(buf);
12097         EM_SAFE_FREE(buf1);
12098         EM_SAFE_FREE(low_char_set);
12099         EM_SAFE_FREE(temp_file_name);
12100
12101         if (fp_src != NULL) {
12102                 fclose(fp_src);
12103                 fp_src = NULL;
12104         }
12105
12106         if (fp_dest != NULL) {
12107                 fclose(fp_dest);
12108                 fp_dest = NULL;
12109         }
12110
12111         if (err_code)
12112                 *err_code = error;
12113
12114         EM_DEBUG_FUNC_END("ret [%d]", ret);
12115         return ret;
12116
12117 }
12118
12119 INTERNAL_FUNC int emstorage_move_file(char *src_file, char *dst_file, int sync_status, int *err_code)
12120 {
12121         EM_DEBUG_FUNC_BEGIN("src_file[%p], dst_file[%p], err_code[%p]", src_file, dst_file, err_code);
12122
12123         int ret = false;
12124         int error = EMAIL_ERROR_NONE;
12125         char errno_buf[ERRNO_BUF_SIZE] = {0};
12126
12127         if (src_file == NULL || dst_file == NULL) {
12128                 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
12129                 error = EMAIL_ERROR_INVALID_PARAM;
12130                 goto FINISH_OFF;
12131         }
12132
12133         EM_DEBUG_LOG_SEC("src_file[%s], dst_file[%s]", src_file, dst_file);
12134
12135         if (strcmp(src_file, dst_file) != 0) {
12136                 if (rename(src_file, dst_file) != 0) {
12137                         /* EM_DEBUG_EXCEPTION("%s", strerror(errno)); */
12138                         if (errno == EXDEV) {   /* oldpath and newpath are not on the same mounted file system.  (Linux permits a file system to be mounted at multiple points,  but  rename() */
12139                                 /*  does not work across different mount points, even if the same file system is mounted on both.)       */
12140                                 EM_DEBUG_LOG("oldpath and newpath are not on the same mounted file system.");
12141                                 if (!emstorage_copy_file(src_file, dst_file, sync_status, &error)) {
12142                                         EM_DEBUG_EXCEPTION("emstorage_copy_file failed - %d", error);
12143                                         goto FINISH_OFF;
12144                                 }
12145                                 remove(src_file);
12146                                 EM_DEBUG_LOG("src[%s] removed", src_file);
12147                         } else {
12148                                 if (errno == ENOENT) {
12149                                         struct stat temp_file_stat;
12150                                         if (stat(src_file, &temp_file_stat) < 0) {
12151                                                 EM_DEBUG_EXCEPTION("no src file found [%s] : %s", src_file, EM_STRERROR(errno_buf));
12152                                         }
12153                                         if (stat(dst_file, &temp_file_stat) < 0)
12154                                                 EM_DEBUG_EXCEPTION("no dst file found [%s] : %s", dst_file, EM_STRERROR(errno_buf));
12155
12156                                         error = EMAIL_ERROR_FILE_NOT_FOUND;
12157                                         goto FINISH_OFF;
12158                                 } else {
12159                                         EM_DEBUG_EXCEPTION("rename failed: %s", EM_STRERROR(errno_buf));
12160                                         error = EMAIL_ERROR_SYSTEM_FAILURE;
12161                                         goto FINISH_OFF;
12162                                 }
12163                         }
12164                 }
12165         } else {
12166                 EM_DEBUG_LOG("src[%s] = dst[%s]", src_file, dst_file);
12167         }
12168
12169         ret = true;
12170
12171 FINISH_OFF:
12172         if (err_code != NULL)
12173                 *err_code = error;
12174
12175         EM_DEBUG_FUNC_END("ret [%d]", ret);
12176         return ret;
12177 }
12178
12179 INTERNAL_FUNC int emstorage_delete_file(char *src_file, int *err_code)
12180 {
12181         EM_DEBUG_FUNC_BEGIN("src_file[%p], err_code[%p]", src_file, err_code);
12182
12183         int ret = false;
12184         int error = EMAIL_ERROR_NONE;
12185
12186         if (src_file == NULL) {
12187                 EM_DEBUG_EXCEPTION(" src_file[%p]", src_file);
12188
12189                 error = EMAIL_ERROR_INVALID_PARAM;
12190                 goto FINISH_OFF;
12191         }
12192
12193         if (remove(src_file) != 0) {
12194                 if (errno != ENOENT) {
12195                         EM_DEBUG_EXCEPTION(" remove failed - %d", errno);
12196
12197                         error = EMAIL_ERROR_SYSTEM_FAILURE;
12198                         goto FINISH_OFF;
12199                 } else {
12200                         EM_DEBUG_EXCEPTION(" no file found...");
12201
12202                         error = EMAIL_ERROR_FILE_NOT_FOUND;
12203                 }
12204         }
12205
12206         ret = true;
12207
12208 FINISH_OFF:
12209         if (err_code != NULL)
12210                 *err_code = error;
12211
12212         EM_DEBUG_FUNC_END("ret [%d]", ret);
12213         return ret;
12214 }
12215
12216
12217 INTERNAL_FUNC int emstorage_delete_dir(char *src_dir, int *err_code)
12218 {
12219         EM_DEBUG_FUNC_BEGIN("src_dir[%p], err_code[%p]", src_dir, err_code);
12220
12221         if (src_dir == NULL) {
12222                 EM_DEBUG_EXCEPTION("src_dir[%p]", src_dir);
12223
12224                 if (err_code != NULL)
12225                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12226                 return false;
12227         }
12228
12229         int error = EMAIL_ERROR_NONE;
12230
12231         DIR *dirp;
12232         struct dirent *dp;
12233         struct stat sbuf;
12234         char buf[512];
12235
12236         dirp = opendir(src_dir);
12237
12238         if (dirp == NULL) {
12239                 if (errno == ENOENT) {
12240                         EM_DEBUG_EXCEPTION("directory[%s] does not exist...", src_dir);
12241                         if (err_code != NULL)
12242                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12243                         return true;
12244                 } else {
12245                         EM_DEBUG_EXCEPTION("opendir failed [%s] [%d]", src_dir, errno);
12246                         if (err_code != NULL)
12247                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12248                         return false;
12249                 }
12250         }
12251
12252         while ((dp = readdir(dirp))) {
12253                 if (strncmp(dp->d_name, ".", 1) == 0 || strncmp(dp->d_name, "..", 2) == 0) /* prevent 34360 */
12254                         continue;
12255
12256                 SNPRINTF(buf, sizeof(buf), "%s/%s", src_dir, dp->d_name);
12257
12258                 if (lstat(buf, &sbuf) == 0 || stat(buf, &sbuf) == 0) {
12259                         /*  check directory */
12260                         if ((sbuf.st_mode & S_IFMT) == S_IFDIR) {       /*  directory */
12261                                 /*  recursive call */
12262                                 if (!emstorage_delete_dir(buf, &error)) {
12263                                         closedir(dirp);
12264                                         if (err_code != NULL)
12265                                                 *err_code = error;
12266                                         return false;
12267                                 }
12268                         } else {        /*  file */
12269                                 if (remove(buf) < 0) {
12270                                         EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", buf, errno);
12271                                         closedir(dirp);
12272                                         if (err_code != NULL)
12273                                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12274                                         return false;
12275                                 }
12276                         }
12277                 } else
12278                         EM_DEBUG_EXCEPTION("content does not exist...");
12279         }
12280
12281         closedir(dirp);
12282
12283         EM_DEBUG_LOG_DEV("remove direcotory [%s]", src_dir);
12284
12285         /* EM_DEBUG_FUNC_BEGIN(); */
12286
12287         if (remove(src_dir) < 0) {
12288                 EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", src_dir, errno);
12289                 if (err_code != NULL)
12290                         *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12291                 return false;
12292         }
12293
12294         if (err_code != NULL)
12295                 *err_code = error;
12296
12297         return true;
12298 }
12299
12300 /* faizan.h@samsung.com */
12301 INTERNAL_FUNC int emstorage_update_server_uid(char *multi_user_name,
12302                                                                                                 int mail_id,
12303                                                                                                 char *old_server_uid,
12304                                                                                                 char *new_server_uid,
12305                                                                                                 int *err_code)
12306 {
12307         EM_DEBUG_FUNC_BEGIN("new_server_uid[%s], old_server_uid[%s]", new_server_uid, old_server_uid);
12308         int ret = false;
12309         int error = EMAIL_ERROR_NONE;
12310         int transaction = true;
12311         int temp_strlen = 0;
12312         int and_operation = 0;
12313         char sql_query_string[QUERY_SIZE] = {0, };
12314         char conditional_clause_string[QUERY_SIZE] = {0};
12315
12316         if ((mail_id <= 0 || !old_server_uid) && !new_server_uid) {
12317                 EM_DEBUG_EXCEPTION("Invalid parameters");
12318                 if (err_code)
12319                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12320                 return false;
12321         }
12322
12323         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12324         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12325
12326         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE ");
12327
12328         if (mail_id > 0) {
12329                 temp_strlen = strlen(conditional_clause_string);
12330                 SNPRINTF(conditional_clause_string + temp_strlen, sizeof(conditional_clause_string) - temp_strlen,
12331                                         "mail_id = %d ", mail_id);
12332                 and_operation = 1;
12333         }
12334
12335         if (old_server_uid) {
12336                 temp_strlen = strlen(conditional_clause_string);
12337                 if (!and_operation) {
12338                         sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
12339                                                         "server_mail_id = '%q'", old_server_uid);
12340                 } else {
12341                         sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
12342                                                         "and server_mail_id = '%q'", old_server_uid);
12343                 }
12344         }
12345
12346         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
12347                                                 "UPDATE mail_tbl SET server_mail_id = '%q' %s", new_server_uid, conditional_clause_string);
12348
12349         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12350         if (error != EMAIL_ERROR_NONE) {
12351                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12352                 goto FINISH_OFF;
12353         }
12354
12355         ret = true;
12356
12357 FINISH_OFF:
12358
12359         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12360
12361         if (err_code != NULL)
12362                 *err_code = error;
12363
12364         EM_DEBUG_FUNC_END("ret [%d]", ret);
12365         return ret;
12366
12367 }
12368
12369 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)
12370 {
12371         EM_DEBUG_FUNC_BEGIN_SEC("mail_id[%d], new_server_uid[%s], mbox_name[%s]", mail_id, new_server_uid, mbox_name);
12372
12373         int ret = false;
12374         int error = EMAIL_ERROR_NONE;
12375         char sql_query_string[QUERY_SIZE] = {0, };
12376         int transaction = true;
12377
12378         if (!mail_id || !new_server_uid || !mbox_name) {
12379                 EM_DEBUG_EXCEPTION("Invalid parameters");
12380                 if (err_code != NULL)
12381                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12382                 return false;
12383         }
12384
12385         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12386         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12387
12388
12389         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12390                  "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);
12391
12392         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12393         if (error != EMAIL_ERROR_NONE) {
12394                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12395                         goto FINISH_OFF;
12396         }
12397
12398         ret     = true;
12399
12400 FINISH_OFF:
12401
12402         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12403
12404         if (err_code != NULL)
12405                 *err_code = error;
12406
12407         EM_DEBUG_FUNC_END("ret [%d]", ret);
12408         return ret;
12409
12410 }
12411
12412 INTERNAL_FUNC int emstorage_update_save_status(char *multi_user_name, int account_id, int *err_code)
12413 {
12414         EM_DEBUG_FUNC_BEGIN();
12415
12416         int ret = false;
12417         int transaction = true;
12418         int error = EMAIL_ERROR_NONE;
12419         char sql_query_string[QUERY_SIZE] = {0,};
12420
12421         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12422
12423         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12424
12425         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12426
12427         if (account_id <= ALL_ACCOUNT)
12428                 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);
12429         else
12430                 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);
12431
12432         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12433         if (error != EMAIL_ERROR_NONE) {
12434                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12435                         goto FINISH_OFF;
12436         }
12437
12438         ret = true;
12439
12440 FINISH_OFF:
12441         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12442
12443         if (err_code != NULL)
12444                 *err_code = error;
12445
12446         EM_DEBUG_FUNC_END("ret [%d]", ret);
12447         return ret;
12448
12449 }
12450
12451 int emstorage_get_unread_mailid(char *multi_user_name, int account_id, int vip_mode, int **mail_ids, int *mail_number, int *err_code)
12452 {
12453         EM_DEBUG_FUNC_BEGIN();
12454
12455         if ((!mail_ids) || (account_id <= 0 &&  account_id != -1)) {
12456                 EM_DEBUG_EXCEPTION(" mail_id[%p], account_id[%d] ", mail_ids, account_id);
12457                 if (err_code != NULL)
12458                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12459                 return false;
12460         }
12461
12462         int ret = false;
12463         int rc = -1;
12464         int error = EMAIL_ERROR_NONE;
12465         int count = 0;
12466         int i = 0;
12467         int col_index = 0;
12468         int *p_mail_ids = NULL;
12469         int transaction = false;
12470         char **result = NULL;
12471         char sql_query_string[QUERY_SIZE] = {0, };
12472         char temp_query_string[QUERY_SIZE] = {0,};
12473         char sql_select_query_string[QUERY_SIZE] = {0, };
12474
12475         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12476         EMSTORAGE_START_READ_TRANSACTION(transaction);
12477
12478         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12479
12480         if (account_id == -1)
12481                 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string), "SELECT mail_id FROM mail_tbl WHERE flags_seen_field = 0 AND (save_status = %d or save_status = %d)", EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12482         else
12483                 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string), "SELECT mail_id FROM mail_tbl WHERE account_id = %d AND flags_seen_field = 0 AND (save_status = %d or save_status = %d)", account_id, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12484
12485         if (vip_mode) {
12486                 SNPRINTF(temp_query_string, sizeof(temp_query_string), "%s AND tag_id < 0", sql_select_query_string);
12487         } else {
12488                 SNPRINTF(temp_query_string, sizeof(temp_query_string), "%s", sql_select_query_string);
12489         }
12490
12491         SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s ORDER BY date_time ASC", temp_query_string);
12492
12493         EM_DEBUG_LOG_SEC("query: [%s]", sql_query_string);
12494
12495         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
12496         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
12497                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12498
12499         EM_DEBUG_LOG("Count : %d", count);
12500
12501         if (count == 0) {
12502                 EM_DEBUG_EXCEPTION("no Mails found...");
12503                 ret = false;
12504                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
12505                 goto FINISH_OFF;
12506         }
12507
12508         p_mail_ids = em_malloc(count * sizeof(int));
12509         if (p_mail_ids == NULL) {
12510                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
12511                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12512                 goto FINISH_OFF;
12513         }
12514
12515         col_index = 1;
12516
12517         for (i = 0; i < count; i++) {
12518                 _get_table_field_data_int(result, &(p_mail_ids[i]), col_index++);
12519         }
12520
12521         ret = true;
12522
12523 FINISH_OFF:
12524
12525         if (result)
12526                 sqlite3_free_table(result);
12527
12528         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12529
12530 //      sqlite3_db_release_memory(local_db_handle);
12531
12532         if (ret == true) {
12533                 if (mail_ids != NULL)
12534                         *mail_ids = p_mail_ids;
12535
12536                 if (mail_number != NULL)
12537                         *mail_number = count;
12538         } else {
12539                 EM_SAFE_FREE(p_mail_ids);
12540         }
12541
12542         if (err_code != NULL)
12543                 *err_code = error;
12544
12545         EM_DEBUG_FUNC_END("ret [%d]", ret);
12546         return ret;
12547 }
12548
12549 int setting_system_command(const char *command)
12550
12551  {
12552         int pid = 0, status = 0;
12553         char *const environ[] = { NULL };
12554
12555         if (command == 0)
12556                 return 1;
12557
12558         pid = fork();
12559
12560         if (pid == -1)
12561                 return -1;
12562
12563         if (pid == 0) {
12564                 char *argv[4];
12565
12566                 argv[0] = "sh";
12567                 argv[1] = "-c";
12568                 argv[2] = (char *)command;
12569                 argv[3] = 0;
12570
12571                 execve("/bin/sh", argv, environ);
12572                 abort();
12573         }
12574
12575         do {
12576                 if (waitpid(pid, &status, 0) == -1) {
12577                         if (errno != EINTR)
12578                                 return -1;
12579                 } else {
12580                         return status;
12581                 }
12582         } while (1);
12583
12584         return 0;
12585 }
12586
12587
12588 INTERNAL_FUNC int emstorage_mail_get_total_diskspace_usage(unsigned long *total_usage,  int transaction, int *err_code)
12589 {
12590         EM_DEBUG_FUNC_BEGIN("total_usage[%p],  transaction[%d], err_code[%p]", total_usage, transaction, err_code);
12591
12592         if (!total_usage) {
12593                 EM_DEBUG_EXCEPTION("total_usage[%p]", total_usage);
12594
12595                 if (err_code != NULL)
12596                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12597                 return false;
12598         }
12599
12600         int   ret = false;
12601         int   error = EMAIL_ERROR_NONE;
12602         char  syscmd[256] = {0, };
12603         char  line[256] = {0, };
12604         char *line_from_file = NULL;
12605         FILE *fp = NULL;
12606         unsigned long total_diskusage = 0;
12607
12608         SNPRINTF(syscmd, sizeof(syscmd), "touch %s", SETTING_MEMORY_TEMP_FILE_PATH);
12609         if (setting_system_command(syscmd) == -1) {
12610                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
12611
12612                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12613                 goto FINISH_OFF;
12614         }
12615
12616         SNPRINTF(syscmd, sizeof(syscmd), "du -hsk %s > %s", EMAILPATH, SETTING_MEMORY_TEMP_FILE_PATH);
12617         EM_DEBUG_LOG(" cmd : %s", syscmd);
12618         if (setting_system_command(syscmd) == -1) {
12619                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : Setting > Memory] System Command [%s] is failed", syscmd);
12620
12621                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12622                 goto FINISH_OFF;
12623         }
12624
12625         error = em_fopen(SETTING_MEMORY_TEMP_FILE_PATH, "r", &fp);
12626         if (error != EMAIL_ERROR_NONE) {
12627                 perror(SETTING_MEMORY_TEMP_FILE_PATH);
12628                 goto FINISH_OFF;
12629         }
12630
12631         line_from_file = fgets(line, sizeof(line), fp);
12632
12633         if (line_from_file == NULL) {
12634                 EM_DEBUG_EXCEPTION("fgets failed");
12635                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12636                 goto FINISH_OFF;
12637         }
12638         total_diskusage = strtoul(line, NULL, 10);
12639
12640         memset(syscmd, 0, sizeof(syscmd));
12641         SNPRINTF(syscmd, sizeof(syscmd), "rm -f %s", SETTING_MEMORY_TEMP_FILE_PATH);
12642         if (setting_system_command(syscmd) == -1) {
12643                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage :  [Setting > Memory] System Command [%s] is failed", syscmd);
12644                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12645                 goto FINISH_OFF;
12646         }
12647
12648         EM_DEBUG_LOG("[Setting > Memory] @@@@@ Size of Directory [%s] is %ld KB", EMAILPATH, total_diskusage);
12649
12650         ret = true;
12651
12652 FINISH_OFF:
12653         if (err_code != NULL)
12654                 *err_code = error;
12655
12656         if (ret)
12657                 *total_usage = total_diskusage;
12658         else
12659                 *total_usage = 0;
12660
12661         if (fp) fclose(fp); /* prevent 32730 */
12662
12663         EM_DEBUG_FUNC_END("ret [%d]", ret);
12664         return ret;
12665 }
12666 #define MAILHOME_UTF8    tzplatform_mkpath(TZ_USER_DATA, "email/.email_data/7/348/UTF-8")
12667
12668 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)
12669 {
12670         DB_STMT hStmt = NULL;
12671         int ret = false;
12672         int error = EMAIL_ERROR_NONE;
12673         int rc = 0;
12674         char sql_query_string[QUERY_SIZE] = {0, };
12675
12676         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12677                 "INSERT INTO mail_tbl VALUES "
12678                 "(?" /*  mail_id */
12679                 ", ?" /*  account_id */
12680                 ", ?" /*  mail_size */
12681                 ", ?" /*  server_mail_status */
12682                 ", ?" /*  server_mailbox_name */
12683                 ", ?" /*  server_mail_id */
12684                 ", ?" /*  reference_mail_id */
12685                 ", ?" /*  full_address_from */
12686                 ", ?" /*  full_address_reply */
12687                 ", ?" /*  full_address_to */
12688                 ", ?" /*  full_address_cc */
12689                 ", ?" /*  full_address_bcc */
12690                 ", ?" /*  full_address_return */
12691                 ", ?" /*  subject */
12692                 ", ?" /*  body_download_status */
12693                 ", ?" /*  file_path_plain */
12694                 ", ?" /*  file_path_html */
12695                 ", ?" /*  date_time */
12696                 ", ?" /*  flags_seen_field */
12697                 ", ?" /*  flags_deleted_field */
12698                 ", ?" /*  flags_flagged_field */
12699                 ", ?" /*  flags_answered_field */
12700                 ", ?" /*  flags_recent_field */
12701                 ", ?" /*  flags_draft_field */
12702                 ", ?" /*  flags_forwarded_field */
12703                 ", ?" /*  DRM_status */
12704                 ", ?" /*  priority */
12705                 ", ?" /*  save_status */
12706                 ", ?" /*  lock_status */
12707                 ", ?" /*  message_id */
12708                 ", ?" /*  report_status */
12709                 ", ?" /*  email_address_sender */
12710                 ", ?" /*  email_address_recipient */
12711                 ", ?" /*  attachment_count */
12712                 ", ?" /*  inline_content_count */
12713                 ", ?" /*  preview_text */
12714                 ", ?" /*  thread_id */
12715                 ", ?" /*  mailbox_type */
12716                 ", ?" /*  alias_sender */
12717                 ", ?" /*  alias_recipient */
12718                 ", ?" /*  thread_item_count */
12719                 ", ?" /*  meeting_request_status */
12720                 ", ?" /*  message_class */
12721                 ", ?" /*  digest_type */
12722                 ", ?" /*  smime_type */
12723                 ", ?" /*  scheduled_sending_time */
12724                 ", ?" /*  remaining_resend_times */
12725                 ", ?" /*  tag_id   */
12726                 ", ?" /*  replied_time */
12727                 ", ?" /*  forwarded_time */
12728                 ", ?" /*  default_charset */
12729                 ", ?" /*  eas_data_length */
12730                 ", ?" /*  eas_data */
12731                 ")");
12732
12733         int transaction = true;
12734         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12735
12736         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12737
12738
12739         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12740         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
12741                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12742
12743         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_id);
12744         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, account_id);
12745         _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, EMAIL_MAILBOX_TYPE_OUTBOX);
12746         _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, "save test - long", 1, SUBJECT_LEN_IN_MAIL_TBL);
12747         _bind_stmt_field_data_string(hStmt, DATETIME_IDX_IN_MAIL_TBL, "20100316052908", 0, DATETIME_LEN_IN_MAIL_TBL);
12748         _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, 0);
12749         _bind_stmt_field_data_string(hStmt, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL, "", 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
12750         _bind_stmt_field_data_string(hStmt, SERVER_MAIL_ID_IDX_IN_MAIL_TBL, "", 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
12751         _bind_stmt_field_data_string(hStmt, MESSAGE_ID_IDX_IN_MAIL_TBL, "", 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
12752         _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, 0);
12753         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL, "<test08@streaming.s3glab.net>", 1, FROM_LEN_IN_MAIL_TBL);
12754         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL, "", 1, REPLY_TO_LEN_IN_MAIL_TBL);
12755         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL, full_address_to, 1, TO_LEN_IN_MAIL_TBL);
12756         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL, full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
12757         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL, full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
12758         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL, "", 1, RETURN_PATH_LEN_IN_MAIL_TBL);
12759         _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);
12760         _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);
12761         _bind_stmt_field_data_string(hStmt, ALIAS_SENDER_IDX_IN_MAIL_TBL, "send_alias", 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12762         _bind_stmt_field_data_string(hStmt, ALIAS_RECIPIENT_IDX_IN_MAIL_TBL, "recipient_alias", 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12763         _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, 1);
12764         _bind_stmt_field_data_string(hStmt, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL, (char *)MAILHOME_UTF8, 0, TEXT_1_LEN_IN_MAIL_TBL);
12765         _bind_stmt_field_data_string(hStmt, FILE_PATH_HTML_IDX_IN_MAIL_TBL, "", 0, TEXT_2_LEN_IN_MAIL_TBL);
12766         _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, 4);
12767         _bind_stmt_field_data_char(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, 0);
12768         _bind_stmt_field_data_char(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, 0);
12769         _bind_stmt_field_data_char(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, 0);
12770         _bind_stmt_field_data_char(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, 0);
12771         _bind_stmt_field_data_char(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, 0);
12772         _bind_stmt_field_data_char(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, 0);
12773         _bind_stmt_field_data_char(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, 0);
12774         _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, 0);
12775         _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, 0);
12776         _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, 0);
12777         _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, 0);
12778         _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, 0);
12779         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12780         _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, 0);
12781         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12782         _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, 0);
12783         _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, 0);
12784         _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, "preview body", 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
12785         _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, 0);
12786         _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, 0);
12787         _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, 0);
12788         _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, 0);
12789         _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, 0);
12790         _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, 0);
12791         _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, 0);
12792         _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, 0);
12793         _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, 0);
12794         _bind_stmt_field_data_string(hStmt, DEFAULT_CHARSET_IDX_IN_MAIL_TBL, "UTF-8", 0, TEXT_2_LEN_IN_MAIL_TBL);
12795         _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, 0);
12796         _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, NULL, 0);
12797
12798         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12799         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
12800                 ("sqlite3_step fail:%d", rc));
12801         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
12802                 ("sqlite3_step fail:%d", rc));
12803         ret = true;
12804
12805 FINISH_OFF:
12806         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12807         if (hStmt != NULL) {
12808                 rc = sqlite3_finalize(hStmt);
12809                 if (rc != SQLITE_OK) {
12810                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
12811                         error = EMAIL_ERROR_DB_FAILURE;
12812                 }
12813         }
12814
12815         if (err_code != NULL)
12816                 *err_code = error;
12817
12818         EM_DEBUG_FUNC_END("ret [%d]", ret);
12819         return ret;
12820 }
12821
12822
12823 INTERNAL_FUNC int emstorage_get_max_mail_count()
12824 {
12825         return EMAIL_MAIL_MAX_COUNT;
12826 }
12827
12828 #define STRIPPED_SUBJECT_BUFFER_SIZE 4086
12829
12830 INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(char *multi_user_name,
12831                                                                                                                         emstorage_mail_tbl_t *mail_tbl,
12832                                                                                                                         int *thread_id,
12833                                                                                                                         int *result_latest_mail_id_in_thread,
12834                                                                                                                         int *thread_item_count)
12835 {
12836         EM_DEBUG_FUNC_BEGIN("mail_tbl [%p], thread_id [%p], "
12837                                                 "result_latest_mail_id_in_thread [%p], thread_item_count [%p]",
12838                                                 mail_tbl, thread_id, result_latest_mail_id_in_thread, thread_item_count);
12839         EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
12840
12841         int rc = 0, query_size = 0, query_size_account = 0;
12842         int i = 0;
12843         int search_thread = false;
12844         int account_id = 0;
12845         int err_code = EMAIL_ERROR_NONE;
12846         int count = 0, result_thread_id = -1, latest_mail_id_in_thread = -1;
12847         time_t latest_date_time = 0;
12848         char *subject = NULL;
12849         char *p_subject = NULL;
12850         char *sql_query_string = NULL, *sql_account = NULL;
12851         int col_index = 4;
12852         int temp_thread_id = -1;
12853         char *sql_format = "SELECT mail_id, thread_id, date_time, subject "
12854                                                 "FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
12855         char *sql_format_account = " AND account_id = %d ";
12856         char *sql_format_order_by = " ORDER BY thread_id, date_time DESC ";
12857         char **result = NULL;
12858         char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
12859         char stripped_subject2[STRIPPED_SUBJECT_BUFFER_SIZE];
12860
12861         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12862
12863         EM_DEBUG_LOG("subject: [%p], mail_id: [%d]", subject, mail_tbl->mail_id);
12864
12865         EM_IF_NULL_RETURN_VALUE(mail_tbl, EMAIL_ERROR_INVALID_PARAM);
12866         EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
12867         EM_IF_NULL_RETURN_VALUE(result_latest_mail_id_in_thread, EMAIL_ERROR_INVALID_PARAM);
12868         EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
12869
12870         account_id   = mail_tbl->account_id;
12871         subject      = mail_tbl->subject;
12872
12873         EM_DEBUG_LOG_SEC("subject: [%s]", subject);
12874
12875         if (EM_SAFE_STRLEN(subject) == 0 && mail_tbl->mail_id != 0) {
12876                 result_thread_id = mail_tbl->mail_id;
12877                 count = 1;
12878                 goto FINISH_OFF;
12879         }
12880
12881         if (em_find_pos_stripped_subject_for_thread_view(subject,
12882                                                                                                                 stripped_subject,
12883                                                                                                                 STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE)      {
12884                 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
12885                 err_code =  EMAIL_ERROR_UNKNOWN;
12886                 result_thread_id = -1;
12887                 goto FINISH_OFF;
12888         }
12889
12890         EM_DEBUG_LOG_SEC("stripped_subject: [%s]", stripped_subject);
12891
12892         if (EM_SAFE_STRLEN(stripped_subject) == 0) {
12893                 result_thread_id = -1;
12894                 goto FINISH_OFF;
12895         }
12896
12897         EM_DEBUG_LOG_SEC("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s",
12898                                                 EM_SAFE_STRLEN(stripped_subject), stripped_subject);
12899
12900         if (account_id > 0)     {
12901                 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
12902                 sql_account = malloc(query_size_account);
12903                 if (sql_account == NULL) {
12904                         EM_DEBUG_EXCEPTION("malloc for sql_account  is failed %d", query_size_account);
12905                         err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
12906                         goto FINISH_OFF;
12907                 }
12908                 snprintf(sql_account, query_size_account, sql_format_account, account_id);
12909         }
12910
12911         /* prevent 34362 */
12912         query_size = strlen(sql_format) + strlen(stripped_subject)*2 + 50 + query_size_account + strlen(sql_format_order_by); /*  + query_size_mailbox; */
12913         sql_query_string = malloc(query_size);
12914
12915         if (sql_query_string == NULL) {
12916                 EM_DEBUG_EXCEPTION("malloc for sql  is failed %d", query_size);
12917                 err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
12918                 goto FINISH_OFF;
12919         }
12920
12921         sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mail_tbl->mailbox_id);
12922
12923         if (account_id > 0)
12924                 strcat(sql_query_string, sql_account);
12925
12926         strcat(sql_query_string, sql_format_order_by);
12927         strcat(sql_query_string, ";");
12928
12929         EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
12930
12931         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL),
12932                                                                         rc);
12933
12934         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
12935                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12936
12937         EM_DEBUG_LOG("Result rows count : %d", count);
12938
12939         if (count == 0)
12940                 result_thread_id = -1;
12941         else {
12942                 for (i = 0; i < count; i++) {
12943                         EM_SAFE_FREE(p_subject);
12944
12945                         _get_table_field_data_int(result, &latest_mail_id_in_thread, col_index++);
12946                         _get_table_field_data_int(result, &result_thread_id, col_index++);
12947                         _get_table_field_data_time_t(result, &latest_date_time, col_index++);
12948                         _get_table_field_data_string(result, &p_subject, 0, col_index++);
12949
12950                         if (temp_thread_id == result_thread_id)
12951                                 continue;
12952
12953                         temp_thread_id = result_thread_id;
12954
12955                         if (em_find_pos_stripped_subject_for_thread_view(p_subject,
12956                                                                                                                                 stripped_subject2,
12957                                                                                                                                 STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE)      {
12958                                 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
12959                                 err_code = EMAIL_ERROR_UNKNOWN;
12960                                 result_thread_id = -1;
12961                                 goto FINISH_OFF;
12962                         }
12963
12964                         if (g_strcmp0(stripped_subject2, stripped_subject) == 0) {
12965                                 if (latest_date_time < mail_tbl->date_time)
12966                                         *result_latest_mail_id_in_thread = latest_mail_id_in_thread;
12967                                 else
12968                                         *result_latest_mail_id_in_thread = mail_tbl->mail_id;
12969
12970                                 search_thread = true;
12971                                 break;
12972                         }
12973
12974                         if (search_thread) {
12975                                 EM_DEBUG_LOG("latest_mail_id_in_thread [%d], mail_id [%d]",
12976                                                                 latest_mail_id_in_thread, mail_tbl->mail_id);
12977                         } else {
12978                                 result_thread_id = -1;
12979                                 count = 0;
12980                         }
12981                 }
12982
12983         }
12984
12985 FINISH_OFF:
12986
12987         *thread_id = result_thread_id;
12988         *thread_item_count = count;
12989
12990         EM_DEBUG_LOG("Result thread id : %d", *thread_id);
12991         EM_DEBUG_LOG("Result count : %d", *thread_item_count);
12992         EM_DEBUG_LOG("err_code : %d", err_code);
12993
12994         EM_SAFE_FREE(sql_account);
12995         EM_SAFE_FREE(sql_query_string);
12996         EM_SAFE_FREE(p_subject);
12997
12998         sqlite3_free_table(result);
12999
13000         EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
13001
13002         return err_code;
13003 }
13004
13005 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)
13006 {
13007         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);
13008         EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
13009
13010         int rc = 0;
13011         int query_size = 0;
13012         int query_size_account = 0;
13013         int err_code = EMAIL_ERROR_NONE;
13014         int count = 0;
13015         int result_thread_id = -1;
13016         char *sql_query_string = NULL;
13017         char *sql_account = NULL;
13018         char *sql_format = "SELECT thread_id FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
13019         char *sql_format_account = " AND account_id = %d ";
13020         char *sql_format_order_by = " ORDER BY date_time DESC ";
13021         char **result = NULL;
13022         char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
13023         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13024
13025         EM_IF_NULL_RETURN_VALUE(mail_subject, EMAIL_ERROR_INVALID_PARAM);
13026         EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
13027         EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
13028
13029         EM_DEBUG_LOG_SEC("subject: [%s]", mail_subject);
13030
13031         if (em_find_pos_stripped_subject_for_thread_view(mail_subject, stripped_subject, STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE)     {
13032                 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view  is failed");
13033                 err_code =  EMAIL_ERROR_UNKNOWN;
13034                 result_thread_id = -1;
13035                 goto FINISH_OFF;
13036         }
13037
13038         if (EM_SAFE_STRLEN(stripped_subject) == 0) {
13039                 result_thread_id = -1;
13040                 goto FINISH_OFF;
13041         }
13042
13043         EM_DEBUG_LOG("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s", EM_SAFE_STRLEN(stripped_subject), stripped_subject);
13044
13045         if (account_id > 0) {
13046                 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
13047                 sql_account = malloc(query_size_account);
13048                 if (sql_account == NULL) {
13049                         EM_DEBUG_EXCEPTION("malloc for sql_account  is failed %d", query_size_account);
13050                         err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
13051                         goto FINISH_OFF;
13052                 }
13053                 snprintf(sql_account, query_size_account, sql_format_account, account_id);
13054         }
13055
13056         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; */
13057         sql_query_string = malloc(query_size);
13058
13059         if (sql_query_string == NULL) {
13060                 EM_DEBUG_EXCEPTION("malloc for sql  is failed %d", query_size);
13061                 err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
13062                 goto FINISH_OFF;
13063         }
13064
13065         sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mailbox_id);
13066
13067         if (account_id > 0)
13068                 strcat(sql_query_string, sql_account);
13069
13070         strcat(sql_query_string, sql_format_order_by);
13071         strcat(sql_query_string, ";");
13072
13073         EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
13074
13075         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
13076
13077         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13078                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13079
13080         EM_DEBUG_LOG("Result rows count : %d", count);
13081
13082         if (count == 0)
13083                 result_thread_id = -1;
13084         else {
13085                 _get_table_field_data_int(result, &result_thread_id, 1);
13086         }
13087
13088 FINISH_OFF:
13089         *thread_id = result_thread_id;
13090         *thread_item_count = count;
13091
13092         EM_DEBUG_LOG("Result thread id : %d", *thread_id);
13093         EM_DEBUG_LOG("Result count : %d", *thread_item_count);
13094         EM_DEBUG_LOG("err_code : %d", err_code);
13095
13096         EM_SAFE_FREE(sql_account);
13097         EM_SAFE_FREE(sql_query_string);
13098
13099         sqlite3_free_table(result);
13100
13101         EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
13102
13103         return err_code;
13104 }
13105
13106 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)
13107 {
13108         EM_DEBUG_FUNC_BEGIN();
13109
13110         int count = 0, ret = false;
13111         int error = EMAIL_ERROR_NONE;
13112         emstorage_mail_tbl_t *p_data_tbl = NULL;
13113         char conditional_clause[QUERY_SIZE] = {0, };
13114
13115         EM_IF_NULL_RETURN_VALUE(mail_tbl, false);
13116
13117         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE thread_id = %d AND thread_item_count > 0", thread_id);
13118         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
13119
13120         if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
13121                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
13122                 goto FINISH_OFF;
13123         }
13124
13125         if (p_data_tbl)
13126                 EM_DEBUG_LOG("thread_id : %d, thread_item_count : %d", p_data_tbl[0].thread_id, p_data_tbl[0].thread_item_count);
13127
13128         ret = true;
13129
13130 FINISH_OFF:
13131         if (ret == true)
13132                 *mail_tbl = p_data_tbl;
13133         else if (p_data_tbl != NULL)
13134                 emstorage_free_mail(&p_data_tbl, 1, NULL);
13135
13136         if (err_code != NULL)
13137                 *err_code = error;
13138
13139         EM_DEBUG_FUNC_END("ret [%d]", ret);
13140         return ret;
13141 }
13142
13143 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)
13144 {
13145         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]"
13146                 , account_id , mailbox_id , search_type , search_value , sorting , sender_list, sender_count, err_code);
13147
13148         if ((!sender_list) || (!sender_count)) {
13149                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
13150                 if (err_code != NULL)
13151                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13152                 return false;
13153         }
13154
13155         int rc = -1, ret = false;
13156         int error = EMAIL_ERROR_NONE;
13157         int count = 0;
13158         int i, col_index = 0;
13159         int read_count = 0;
13160         email_sender_list_t *p_sender_list = NULL;
13161         char sql_query_string[QUERY_SIZE] = {0, };
13162         char **result = NULL;
13163         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13164
13165         SNPRINTF(sql_query_string, sizeof(sql_query_string),
13166                 "SELECT email_address_sender, alias_sender, COUNT(email_address_sender), SUM(flags_seen_field = 1) "
13167                 "FROM mail_tbl ");
13168
13169         /*  mailbox_id */
13170         if (mailbox_id)
13171                 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);
13172         else    /*  NULL  means all mailbox_name. but except for trash(3), spambox(5), all emails(for GMail, 7) */
13173                 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) ");
13174
13175         /*  account id */
13176         /*  '0' (ALL_ACCOUNT) means all account */
13177         if (account_id > ALL_ACCOUNT)
13178                 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);
13179
13180         if (search_value) {
13181                 switch (search_type) {
13182                         case EMAIL_SEARCH_FILTER_SUBJECT:
13183                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13184                                         " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\')) ", search_value);
13185                                 break;
13186                         case EMAIL_SEARCH_FILTER_SENDER:
13187                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13188                                         " AND  ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13189                                         ") ", search_value);
13190                                 break;
13191                         case EMAIL_SEARCH_FILTER_RECIPIENT:
13192                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13193                                         " AND ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13194                                         "       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13195                                         "       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13196                                         ") ", search_value, search_value, search_value);
13197                                 break;
13198                         case EMAIL_SEARCH_FILTER_ALL:
13199                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13200                                         " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') "
13201                                         "       OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13202                                         "                       OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13203                                         "                       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13204                                         "                       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13205                                         "               ) "
13206                                         "       )"
13207                                         ")", search_value, search_value, search_value, search_value, search_value);
13208                                 break;
13209                 }
13210         }
13211
13212
13213         /*  sorting option is not available now. The order of sender list is ascending order by display name */
13214         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
13215                 "GROUP BY email_address_sender "
13216                 "ORDER BY UPPER(alias_sender) ");
13217
13218         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
13219
13220         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
13221         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13222                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13223
13224         EM_DEBUG_LOG("Count of Sender [%d]", count);
13225
13226         if (!(p_sender_list = (email_sender_list_t*)em_malloc(sizeof(email_sender_list_t) * count))) {
13227                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
13228                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13229                 goto FINISH_OFF;
13230         }
13231
13232         col_index = 4;
13233
13234         EM_DEBUG_LOG(">>>> DATA ASSIGN START >>");
13235         for (i = 0; i < count; i++) {
13236                 _get_table_field_data_string(result, &(p_sender_list[i].address), 1, col_index++);
13237                 _get_table_field_data_string(result, &(p_sender_list[i].display_name), 1, col_index++);
13238                 _get_table_field_data_int(result, &(p_sender_list[i].total_count), col_index++);
13239                 _get_table_field_data_int(result, &(read_count), col_index++);
13240                 p_sender_list[i].unread_count = p_sender_list[i].total_count - read_count;              /*  unread count = total - read          */
13241         }
13242         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >>", count);
13243
13244         sqlite3_free_table(result);
13245         result = NULL;
13246
13247         ret = true;
13248
13249 FINISH_OFF:
13250         if (ret == true) {
13251                 *sender_list = p_sender_list;
13252                 *sender_count = count;
13253                 EM_DEBUG_LOG(">>>> COUNT : %d >>", count);
13254         }
13255
13256
13257         if (err_code != NULL)
13258                 *err_code = error;
13259
13260         EM_DEBUG_FUNC_END("ret [%d]", ret);
13261         return ret;
13262 }
13263
13264 INTERNAL_FUNC int emstorage_free_sender_list(email_sender_list_t **sender_list, int count)
13265 {
13266         EM_DEBUG_FUNC_BEGIN("sender_list[%p], count[%d]", sender_list, count);
13267
13268         int err = EMAIL_ERROR_NONE;
13269
13270         if (count > 0) {
13271                 if (!sender_list || !*sender_list) {
13272                         EM_DEBUG_EXCEPTION("sender_list[%p], count[%d]", sender_list, count);
13273                         err = EMAIL_ERROR_INVALID_PARAM;
13274                         return err;
13275                 }
13276
13277                 email_sender_list_t* p = *sender_list;
13278                 int i = 0;
13279
13280                 for (; i < count; i++) {
13281                         EM_SAFE_FREE(p[i].address);
13282                         EM_SAFE_FREE(p[i].display_name);
13283                 }
13284
13285                 EM_SAFE_FREE(p);
13286                 *sender_list = NULL;
13287         }
13288
13289         return err;
13290 }
13291
13292
13293 INTERNAL_FUNC int emstorage_free_address_info_list(email_address_info_list_t **address_info_list)
13294 {
13295         EM_DEBUG_FUNC_BEGIN("address_info_list[%p]", address_info_list);
13296
13297         int err = EMAIL_ERROR_NONE;
13298         email_address_info_t *p_address_info = NULL;
13299         GList *list = NULL;
13300         GList *node = NULL;
13301         int i = 0;
13302
13303         if (!address_info_list || !*address_info_list) {
13304                 EM_DEBUG_EXCEPTION("address_info_list[%p]", address_info_list);
13305                 err = EMAIL_ERROR_INVALID_PARAM;
13306                 return err;
13307         }
13308
13309         /*  delete GLists */
13310         for (i = EMAIL_ADDRESS_TYPE_FROM; i <= EMAIL_ADDRESS_TYPE_BCC; i++) {
13311                 switch (i) {
13312                         case EMAIL_ADDRESS_TYPE_FROM:
13313                                 list = (*address_info_list)->from;
13314                                 break;
13315                         case EMAIL_ADDRESS_TYPE_TO:
13316                                 list = (*address_info_list)->to;
13317                                 break;
13318                         case EMAIL_ADDRESS_TYPE_CC:
13319                                 list = (*address_info_list)->cc;
13320                                 break;
13321                         case EMAIL_ADDRESS_TYPE_BCC:
13322                                 list = (*address_info_list)->bcc;
13323                                 break;
13324                 }
13325
13326                 /*  delete dynamic-allocated memory for each item */
13327                 node = g_list_first(list);
13328                 while (node != NULL) {
13329                         p_address_info = (email_address_info_t*)node->data;
13330                         EM_SAFE_FREE(p_address_info->address);
13331                         EM_SAFE_FREE(p_address_info->display_name);
13332                         EM_SAFE_FREE(node->data);
13333
13334                         node = g_list_next(node);
13335                 }
13336                 g_list_free(list);
13337         }
13338
13339         EM_SAFE_FREE(*address_info_list);
13340         *address_info_list = NULL;
13341
13342         EM_DEBUG_FUNC_END("err [%d]", err);
13343         return err;
13344 }
13345 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
13346
13347 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)
13348 {
13349         EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
13350
13351         if (!local_activity || !activity_id) {
13352                 EM_DEBUG_EXCEPTION("local_activity[%p], transaction[%d], activity_id[%p], err_code[%p]", local_activity, activity_id, transaction, err_code);
13353                 if (err_code != NULL)
13354                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13355                 return false;
13356         }
13357
13358         int rc = -1;
13359         int ret = false;
13360         int error = EMAIL_ERROR_NONE;
13361         int i = 0;
13362
13363         char sql_query_string[QUERY_SIZE] = {0, };
13364         DB_STMT hStmt = NULL;
13365         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13366
13367         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13368         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13369         SNPRINTF(sql_query_string, sizeof(sql_query_string),
13370                 "INSERT INTO mail_partial_body_activity_tbl VALUES "
13371                 "("
13372                 "? "  /* Account ID */
13373                 ",?"  /* Local Mail ID */
13374                 ",?"  /* Server mail ID */
13375                 ",?"  /* Activity ID */
13376                 ",?"  /* Activity type*/
13377                 ",?"  /* Mailbox ID*/
13378                 ",?"  /* Mailbox name*/
13379                 ",?"  /* Multi User Name */
13380                 ") ");
13381
13382         char *sql = "SELECT max(rowid) FROM mail_partial_body_activity_tbl;";
13383         char **result = NULL;
13384
13385
13386         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13387
13388         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13389                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13390
13391         if (NULL == result[1]) rc = 1;
13392         else rc = atoi(result[1])+1;
13393         sqlite3_free_table(result);
13394         result = NULL;
13395
13396         *activity_id = local_activity->activity_id = rc;
13397
13398         EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [ %d ], MAIL ID [ %d ], ACTIVITY TYPE [ %d ], SERVER MAIL ID [ %lu ]", \
13399                 local_activity->activity_id, local_activity->mail_id, local_activity->activity_type, local_activity->server_mail_id);
13400
13401         if (local_activity->mailbox_id)
13402                 EM_DEBUG_LOG(" MAILBOX ID [ %d ]", local_activity->mailbox_id);
13403
13404
13405         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13406         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13407                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13408
13409
13410         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
13411         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
13412         _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
13413         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
13414         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
13415         _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
13416         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->mailbox_name, 0, 3999);
13417         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
13418
13419
13420         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13421
13422         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
13423                 ("sqlite3_step fail:%d", rc));
13424         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13425                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
13426
13427         ret = true;
13428
13429 FINISH_OFF:
13430         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13431         if (hStmt != NULL) {
13432                 rc = sqlite3_finalize(hStmt);
13433                 hStmt = NULL;
13434                 if (rc != SQLITE_OK) {
13435                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13436                         error = EMAIL_ERROR_DB_FAILURE;
13437                 }
13438         }
13439
13440         if (err_code != NULL)
13441                 *err_code = error;
13442
13443         EM_DEBUG_FUNC_END("ret [%d]", ret);
13444         return ret;
13445 }
13446
13447 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)
13448 {
13449         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13450
13451         if (account_id < FIRST_ACCOUNT_ID || NULL == mailbox_list || *mailbox_list == NULL || NULL == count) {
13452                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13453                 if (err_code != NULL)
13454                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13455                 return false;
13456         }
13457
13458         int ret = false;
13459         int error = EMAIL_ERROR_NONE;
13460         char **result;
13461         int i = 0, rc = -1;
13462         int *mbox_list = NULL;
13463         DB_STMT hStmt = NULL;
13464         char sql_query_string[QUERY_SIZE] = {0, };
13465
13466         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13467         EMSTORAGE_START_READ_TRANSACTION(transaction);
13468
13469         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13470         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);
13471
13472
13473         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13474         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13475                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13476
13477         *count = atoi(result[1]);
13478         sqlite3_free_table(result);
13479
13480         if (!*count) {
13481                 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
13482                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13483                 ret = true;
13484                 goto FINISH_OFF;
13485         }
13486         EM_DEBUG_LOG("Mailbox count = %d", *count);
13487
13488         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13489
13490         /* 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); */
13491         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);
13492
13493         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13494
13495
13496         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13497
13498
13499         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13500                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13501
13502
13503         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13504         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13505                 ("sqlite3_step fail:%d", rc));
13506
13507         mbox_list = (int *)em_malloc(sizeof(int) * (*count));
13508         if (NULL == mbox_list) {
13509                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
13510                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13511                 goto FINISH_OFF;
13512         }
13513
13514         memset(mbox_list, 0x00, sizeof(int) * (*count));
13515
13516         for (i = 0; i < (*count); i++) {
13517                 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
13518                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13519                 /* EM_DEBUG_LOG("In emstorage_get_pdb_mailbox_list() loop, After sqlite3_step(), , i = %d, rc = %d.", i,  rc); */
13520                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13521                         ("sqlite3_step fail:%d", rc));
13522                 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
13523         }
13524
13525         ret = true;
13526
13527 FINISH_OFF:
13528         if (ret == true)
13529                 *mailbox_list = mbox_list;
13530         else
13531                 EM_SAFE_FREE(mbox_list);
13532
13533         if (hStmt != NULL) {
13534                 rc = sqlite3_finalize(hStmt);
13535                 hStmt = NULL;
13536                 if (rc != SQLITE_OK) {
13537                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13538                         error = EMAIL_ERROR_DB_FAILURE;
13539                 }
13540         }
13541
13542         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13543         if (err_code != NULL)
13544                 *err_code = error;
13545         EM_DEBUG_FUNC_END("ret [%d]", ret);
13546         return ret;
13547 }
13548
13549 INTERNAL_FUNC int emstorage_get_pbd_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
13550 {
13551         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13552
13553         if (NULL == account_list || NULL == count) {
13554                 EM_DEBUG_EXCEPTION("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13555                 if (err_code != NULL)
13556                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13557                 return false;
13558         }
13559
13560         int ret = false;
13561         int error = EMAIL_ERROR_NONE;
13562         char *sql = "SELECT count(distinct account_id) FROM mail_partial_body_activity_tbl";
13563         char **result;
13564         int i = 0, rc = -1;
13565         int *result_account_list = NULL;
13566         DB_STMT hStmt = NULL;
13567         char sql_query_string[QUERY_SIZE] = {0, };
13568
13569         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13570
13571         EMSTORAGE_START_READ_TRANSACTION(transaction);
13572
13573         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13574         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13575                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13576
13577         *count = atoi(result[1]);
13578         sqlite3_free_table(result);
13579
13580         if (!*count) {
13581                 EM_DEBUG_EXCEPTION("no account found...");
13582                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13583                 ret = true;
13584                 goto FINISH_OFF;
13585         }
13586
13587         EM_DEBUG_LOG("Account count [%d]", *count);
13588
13589         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13590
13591         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_partial_body_activity_tbl");
13592
13593         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13594
13595
13596         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13597
13598         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
13599         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13600                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13601
13602
13603         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13604         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13605                 ("sqlite3_step fail:%d", rc));
13606
13607         if (NULL == (result_account_list = (int *)em_malloc(sizeof(int) * (*count)))) {
13608                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
13609                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13610                 goto FINISH_OFF;
13611         }
13612
13613         memset(result_account_list, 0x00, sizeof(int) * (*count));
13614
13615         for (i = 0; i < (*count); i++) {
13616                 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
13617
13618                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13619                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13620                         ("sqlite3_step fail:%d", rc));
13621                 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
13622         }
13623
13624         ret = true;
13625
13626 FINISH_OFF:
13627         if (ret == true)
13628                 *account_list = result_account_list;
13629         else
13630                 EM_SAFE_FREE(result_account_list);
13631
13632         if (hStmt != NULL) {
13633                 rc = sqlite3_finalize(hStmt);
13634                 hStmt = NULL;
13635                 if (rc != SQLITE_OK) {
13636                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13637                         error = EMAIL_ERROR_DB_FAILURE;
13638                 }
13639         }
13640
13641         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13642         if (err_code != NULL)
13643                 *err_code = error;
13644         EM_DEBUG_FUNC_END("ret [%d]", ret);
13645         return ret;
13646 }
13647
13648 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)
13649 {
13650         EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
13651
13652         if (account_id < FIRST_ACCOUNT_ID || NULL == event_start || 0 == input_mailbox_id || NULL == count) {
13653                 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);
13654
13655                 if (err_code != NULL)
13656                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13657                 return false;
13658         }
13659
13660         int rc = -1;
13661         int ret = false;
13662         char **result;
13663         int error = EMAIL_ERROR_NONE;
13664         int i = 0;
13665         DB_STMT hStmt = NULL;
13666         email_event_partial_body_thd* event_list = NULL;
13667         char sql_query_string[QUERY_SIZE] = {0, };
13668
13669         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13670
13671         EMSTORAGE_START_READ_TRANSACTION(transaction);
13672
13673         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13674         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);
13675
13676
13677         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13678         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13679                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13680
13681         *count = atoi(result[1]);
13682         sqlite3_free_table(result);
13683
13684         EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
13685
13686         if (!*count) {
13687                 EM_DEBUG_LOG("No matched activity found in mail_partial_body_activity_tbl");
13688                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
13689                 ret = true;
13690                 goto FINISH_OFF;
13691         }
13692         EM_DEBUG_LOG("Activity Count = %d", *count);
13693
13694         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13695         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);
13696
13697         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13698
13699
13700         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13701
13702         EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
13703         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13704                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13705
13706
13707         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13708         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13709                 ("sqlite3_step fail:%d", rc));
13710
13711         if (!(event_list = (email_event_partial_body_thd*)em_malloc(sizeof(email_event_partial_body_thd) * (*count)))) {
13712                 EM_DEBUG_EXCEPTION("Malloc failed");
13713
13714                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13715                 goto FINISH_OFF;
13716         }
13717         memset(event_list, 0x00, sizeof(email_event_partial_body_thd) * (*count));
13718
13719         for (i = 0; i < (*count); i++) {
13720                 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_IDX_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13721                 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13722                 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13723                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13724                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_type), ACTIVITY_TYPE_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13725                 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13726                 _get_stmt_field_data_string(hStmt, &(event_list[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13727                 _get_stmt_field_data_string(hStmt, &(event_list[i].multi_user_name), 0, MULTI_USER_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13728
13729                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13730                 /* EM_DEBUG_LOG("In emstorage_get_pbd_activity_data() loop, After sqlite3_step(), , i = %d, rc = %d.", i,  rc); */
13731                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13732                         ("sqlite3_step fail:%d", rc));
13733
13734                 event_list[i].event_type = 0;
13735         }
13736
13737         ret = true;
13738
13739 FINISH_OFF:
13740         if (true == ret)
13741           *event_start = event_list;
13742         else {
13743                 for (i = 0; i < (*count); i++)
13744                         emcore_free_partial_body_thd_event(event_list, NULL);
13745                 EM_SAFE_FREE(event_list); /*prevent 54559*/
13746                 *event_start = NULL;
13747                 *count = 0;
13748         }
13749
13750         if (hStmt != NULL) {
13751                 rc = sqlite3_finalize(hStmt);
13752                 hStmt = NULL;
13753                 if (rc != SQLITE_OK) {
13754                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13755                         error = EMAIL_ERROR_DB_FAILURE;
13756                 }
13757         }
13758
13759         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13760         if (err_code != NULL)
13761                 *err_code = error;
13762
13763         EM_DEBUG_FUNC_END("ret [%d]", ret);
13764         return ret;
13765 }
13766
13767 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)
13768 {
13769         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);
13770
13771
13772         if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
13773                 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);
13774
13775                 if (err_code != NULL)
13776                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13777                 return false;
13778         }
13779
13780         int rc = -1;
13781         int ret = false;
13782         int error = EMAIL_ERROR_NONE;
13783         char sql_query_string[QUERY_SIZE] = {0, };
13784         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13785
13786         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13787         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13788
13789         if (activity_id == 0)
13790                 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);
13791         else
13792                 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);
13793
13794         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13795         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
13796         if (error != EMAIL_ERROR_NONE) {
13797                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
13798                         goto FINISH_OFF;
13799         }
13800
13801         /*  validate activity existence */
13802         rc = sqlite3_changes(local_db_handle);
13803         if (rc == 0) {
13804                 EM_DEBUG_EXCEPTION("No matching activity found");
13805                 error = EMAIL_ERROR_DATA_NOT_FOUND;
13806                 ret = true;
13807                 goto FINISH_OFF;
13808         }
13809
13810         ret = true;
13811
13812 FINISH_OFF:
13813         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13814
13815         if (err_code != NULL)
13816                 *err_code = error;
13817
13818         EM_DEBUG_FUNC_END("ret [%d]", ret);
13819         return ret;
13820 }
13821
13822 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)
13823 {
13824         EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
13825
13826         if (account_id < FIRST_ACCOUNT_ID || NULL == activity_count || NULL == err_code) {
13827                 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
13828                 if (err_code != NULL)
13829                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13830                 return false;
13831         }
13832         int rc = -1;
13833         int ret = false;
13834         int error = EMAIL_ERROR_NONE;
13835         char sql_query_string[QUERY_SIZE] = {0, };
13836
13837         DB_STMT hStmt = NULL;
13838
13839         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13840
13841         EMSTORAGE_START_READ_TRANSACTION(transaction);
13842         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13843
13844         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);
13845
13846         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13847
13848
13849         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13850         EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
13851         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13852                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13853
13854
13855         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13856         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13857                 ("sqlite3_step fail:%d", rc));
13858
13859         _get_stmt_field_data_int(hStmt, activity_count, 0);
13860
13861         EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
13862
13863         ret = true;
13864
13865 FINISH_OFF:
13866
13867         if (hStmt != NULL) {
13868                 rc = sqlite3_finalize(hStmt);
13869                 hStmt = NULL;
13870                 if (rc != SQLITE_OK) {
13871                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13872                         error = EMAIL_ERROR_DB_FAILURE;
13873                 }
13874         }
13875
13876         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13877
13878         if (err_code != NULL)
13879                 *err_code = error;
13880
13881         EM_DEBUG_FUNC_END("ret [%d]", ret);
13882         return ret;
13883 }
13884
13885 INTERNAL_FUNC int emstorage_get_pbd_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
13886 {
13887         EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
13888
13889         if (NULL == activity_count || NULL == err_code) {
13890                 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
13891                 if (err_code != NULL)
13892                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13893                 return false;
13894         }
13895         int rc = -1;
13896         int ret = false;
13897         int error = EMAIL_ERROR_NONE;
13898         DB_STMT hStmt = NULL;
13899         char sql_query_string[QUERY_SIZE] = {0, };
13900
13901         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13902
13903         EMSTORAGE_START_READ_TRANSACTION(transaction);
13904         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13905
13906         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl;");
13907
13908         EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
13909
13910         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13911         EM_DEBUG_LOG_DEV("  before sqlite3_prepare hStmt = %p", hStmt);
13912         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13913                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13914
13915         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13916         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13917                 ("sqlite3_step fail:%d", rc));
13918
13919         _get_stmt_field_data_int(hStmt, activity_count, 0);
13920
13921         EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
13922
13923         ret = true;
13924
13925 FINISH_OFF:
13926
13927
13928         if (hStmt != NULL) {
13929                 rc = sqlite3_finalize(hStmt);
13930                 hStmt = NULL;
13931                 if (rc != SQLITE_OK) {
13932                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13933                         error = EMAIL_ERROR_DB_FAILURE;
13934                 }
13935         }
13936
13937         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13938         if (err_code != NULL)
13939                 *err_code = error;
13940
13941         EM_DEBUG_FUNC_END("ret [%d]", ret);
13942         return ret;
13943 }
13944
13945 INTERNAL_FUNC int emstorage_delete_full_pbd_activity_data(char *multi_user_name, int account_id, int transaction, int *err_code)
13946 {
13947         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
13948         if (account_id < FIRST_ACCOUNT_ID) {
13949                 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
13950                 if (err_code != NULL)
13951                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13952                 return false;
13953         }
13954
13955         int rc = -1;
13956         int ret = false;
13957         int error = EMAIL_ERROR_NONE;
13958         char sql_query_string[QUERY_SIZE] = {0, };
13959
13960         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13961
13962         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13963         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13964         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d", account_id);
13965
13966         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13967         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
13968         if (error != EMAIL_ERROR_NONE) {
13969                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
13970                         goto FINISH_OFF;
13971         }
13972
13973         rc = sqlite3_changes(local_db_handle);
13974         if (rc == 0) {
13975                 EM_DEBUG_EXCEPTION("No matching activities found in mail_partial_body_activity_tbl");
13976                 error = EMAIL_ERROR_DATA_NOT_FOUND;
13977                 ret = true;
13978                 goto FINISH_OFF;
13979         }
13980
13981         ret = true;
13982
13983 FINISH_OFF:
13984
13985         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13986
13987         if (err_code != NULL)
13988                 *err_code = error;
13989
13990         EM_DEBUG_FUNC_END("ret [%d]", ret);
13991         return ret;
13992 }
13993
13994 /*Himanshu[h.gahlaut]-> Added below API to update mail_partial_body_activity_tbl
13995 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*/
13996
13997 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)
13998 {
13999         EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
14000
14001         int rc = -1, ret = false;
14002         int error = EMAIL_ERROR_NONE;
14003         char sql_query_string[QUERY_SIZE] = {0, };
14004
14005         int transaction = true;
14006
14007         if (!old_server_uid || !new_server_uid || !mbox_name) {
14008                 EM_DEBUG_EXCEPTION("Invalid parameters");
14009                 error = EMAIL_ERROR_INVALID_PARAM;
14010                 return false;
14011         }
14012
14013         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14014
14015         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14016         memset(sql_query_string, 0x00, sizeof(sql_query_string));
14017         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14018                  "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);
14019
14020         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14021         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14022         if (error != EMAIL_ERROR_NONE) {
14023                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14024                         goto FINISH_OFF;
14025         }
14026
14027         rc = sqlite3_changes(local_db_handle);
14028         if (rc == 0) {
14029                 EM_DEBUG_LOG("No matching found in mail_partial_body_activity_tbl");
14030         }
14031
14032         ret = true;
14033
14034 FINISH_OFF:
14035         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14036
14037         if (err_code != NULL)
14038                 *err_code = error;
14039
14040         EM_DEBUG_FUNC_END("ret [%d]", ret);
14041         return ret;
14042 }
14043
14044
14045 INTERNAL_FUNC int emstorage_create_file(char *data_string, size_t file_size, char *dst_file_name, int *err_code)
14046 {
14047         EM_DEBUG_FUNC_BEGIN_SEC("file_size[%d] , dst_file_name[%s], err_code[%p]", file_size, dst_file_name, err_code);
14048
14049         int ret = false;
14050         int error = EMAIL_ERROR_NONE;
14051         FILE* fp_dst = NULL;
14052         char errno_buf[ERRNO_BUF_SIZE] = {0};
14053
14054         if (!data_string || !dst_file_name) {
14055                 EM_DEBUG_LOG("data_string[%p], dst_file_name[%p]", data_string, dst_file_name);
14056                 error = EMAIL_ERROR_INVALID_PARAM;
14057                 goto FINISH_OFF;
14058         }
14059
14060         error = em_fopen(dst_file_name, "w", &fp_dst);
14061         if (error != EMAIL_ERROR_NONE) {
14062                 EM_DEBUG_EXCEPTION_SEC("em_fopen failed - %s: %d", dst_file_name, error);
14063                 goto FINISH_OFF;
14064         }
14065
14066         if (fwrite(data_string, 1, file_size, fp_dst) == 0) {
14067                 EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
14068                 error = EMAIL_ERROR_UNKNOWN;
14069                 goto FINISH_OFF;
14070         }
14071
14072         ret = true;
14073
14074 FINISH_OFF:
14075
14076         if (fp_dst != NULL)
14077                 fclose(fp_dst);
14078
14079         if (err_code != NULL)
14080                 *err_code = error;
14081
14082         EM_DEBUG_FUNC_END("ret [%d]", ret);
14083         return ret;
14084 }
14085
14086 #endif /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
14087
14088
14089
14090 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
14091 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)
14092 {
14093         EM_DEBUG_FUNC_BEGIN();
14094         int rc = -1;
14095         int ret = false;
14096         int error = EMAIL_ERROR_NONE;
14097         char sql_query_string[QUERY_SIZE] = {0, };
14098
14099         int transaction = true;
14100
14101         if (!old_server_uid || !new_server_uid || !mbox_name) {
14102                 EM_DEBUG_EXCEPTION("Invalid parameters");
14103                 if (err_code != NULL)
14104                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14105                 return false;
14106         }
14107
14108         EM_DEBUG_LOG_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
14109
14110         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14111         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14112
14113
14114         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14115                  "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);
14116
14117         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
14118         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14119         if (error != EMAIL_ERROR_NONE) {
14120                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14121                         goto FINISH_OFF;
14122         }
14123
14124         rc = sqlite3_changes(local_db_handle);
14125         if (rc == 0) {
14126                 EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
14127                 error = EMAIL_ERROR_DATA_NOT_FOUND;
14128                 goto FINISH_OFF;
14129         }
14130
14131         ret = true;
14132
14133 FINISH_OFF:
14134         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14135
14136         if (err_code != NULL)
14137                 *err_code = error;
14138
14139         EM_DEBUG_FUNC_END("ret [%d]", ret);
14140         return ret;
14141
14142 }
14143
14144
14145 /**
14146  * @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);
14147  * Prepare an array of mail_id and corresponding server mail id.
14148  *
14149  *@author                                       h.gahlaut@samsung.com
14150  * @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)
14151  *                                                      where 88 is the length of fixed keywords including ending null character in the QUERY to be formed
14152  * @param[out] idset                    Returns the array of mail_id and corresponding server_mail_id sorted by server_mail_ids
14153  * @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
14154  * @param[out] err_code         Returns the error code.
14155  * @remarks                                     An Example of Query to be exexuted in this API:
14156  *                                                      SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (12, 13, 56, 78);
14157  * @return This function returns true on success or false on failure.
14158  */
14159 INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *multi_user_name,
14160                                                                                                                 char *mail_ids,
14161                                                                                                                 email_id_set_t** idset,
14162                                                                                                                 int *id_set_count,
14163                                                                                                                 int *err_code)
14164 {
14165         EM_DEBUG_FUNC_BEGIN();
14166         EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds);
14167
14168         int error = EMAIL_ERROR_NONE;
14169         int ret = false;
14170         email_id_set_t* p_id_set = NULL;
14171         int count = 0;
14172         const int buf_size = QUERY_SIZE;
14173         char sql_query_string[QUERY_SIZE] = {0, };
14174         int space_left_in_query_buffer = buf_size;
14175         int i = 0;
14176         int rc = -1;
14177         char *server_mail_id = NULL;
14178         char **result = NULL;
14179         int col_index = 0;
14180
14181
14182         if (NULL == mail_ids || NULL == idset || NULL == id_set_count) {
14183                 EM_DEBUG_EXCEPTION("Invalid Parameters mail_ids[%p] idset[%p] id_set_count [%p]",
14184                                                         mail_ids, idset, id_set_count);
14185                 if (err_code != NULL)
14186                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14187                 return false;
14188         }
14189
14190         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14191
14192         SNPRINTF(sql_query_string, space_left_in_query_buffer,
14193                         "SELECT local_uid, server_uid FROM mail_read_mail_uid_tbl WHERE local_uid in (%s) ORDER BY server_uid",
14194                         mail_ids);
14195
14196         EM_DEBUG_LOG_SEC("SQL Query formed [%s] ", sql_query_string);
14197
14198         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14199         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14200         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result);
14201                                                 goto FINISH_OFF; },     ("SQL(%s) sqlite3_get_table fail:%d -%s",
14202                                                 sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14203
14204         EM_DEBUG_LOG(" Count of mails [%d ]", count);
14205
14206         if (count <= 0) {
14207                 EM_DEBUG_EXCEPTION("Can't find proper mail");
14208                 error = EMAIL_ERROR_DATA_NOT_FOUND;
14209                 goto FINISH_OFF;
14210         }
14211
14212         if (NULL == (p_id_set = (email_id_set_t*)em_malloc(sizeof(email_id_set_t) * count))) {
14213                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
14214                 error = EMAIL_ERROR_OUT_OF_MEMORY;
14215                 goto FINISH_OFF;
14216         }
14217
14218         col_index = 2;
14219
14220         EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds_Loop);
14221         EM_DEBUG_LOG(">>>> DATA ASSIGN START");
14222         for (i = 0; i < count; i++) {
14223                 _get_table_field_data_int(result, &(p_id_set[i].mail_id), col_index++);
14224                 _get_table_field_data_string(result, &server_mail_id, 1, col_index++);
14225                 if (server_mail_id) {
14226                         p_id_set[i].server_mail_id = strtoul(server_mail_id, NULL, 10);
14227                         EM_SAFE_FREE(server_mail_id);
14228                 }
14229         }
14230         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d]", count);
14231         EM_PROFILE_END(EmStorageGetIdSetFromMailIds_Loop);
14232
14233         sqlite3_free_table(result);
14234         result = NULL;
14235
14236         ret = true;
14237
14238 FINISH_OFF:
14239
14240         if (ret == true) {
14241                 *idset = p_id_set;
14242                 *id_set_count = count;
14243                 EM_DEBUG_LOG(" idset[%p] id_set_count [%d]", *idset, *id_set_count);
14244         } else
14245                 EM_SAFE_FREE(p_id_set);
14246
14247
14248         if (err_code != NULL)
14249                 *err_code = error;
14250
14251         EM_PROFILE_END(EmStorageGetIdSetFromMailIds);
14252
14253         EM_DEBUG_FUNC_END("ret [%d]", ret);
14254         return ret;
14255 }
14256
14257
14258
14259 #endif
14260
14261 INTERNAL_FUNC int emstorage_delete_triggers_from_lucene(char *multi_user_name)
14262 {
14263         EM_DEBUG_FUNC_BEGIN();
14264         int ret = true, transaction = true;
14265         int error = EMAIL_ERROR_NONE;
14266         char sql_query_string[QUERY_SIZE] = {0, };
14267         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14268         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14269
14270         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerDelete;");
14271         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14272         if (error != EMAIL_ERROR_NONE) {
14273                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14274                         goto FINISH_OFF;
14275         }
14276
14277         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerInsert;");
14278         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14279         if (error != EMAIL_ERROR_NONE) {
14280                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14281                         goto FINISH_OFF;
14282         }
14283
14284         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerUpdate;");
14285         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14286         if (error != EMAIL_ERROR_NONE) {
14287                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14288                         goto FINISH_OFF;
14289         }
14290
14291         ret = true;
14292
14293 FINISH_OFF:
14294         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14295
14296         EM_DEBUG_FUNC_END("ret [%d]", ret);
14297         return ret;
14298 }
14299
14300 INTERNAL_FUNC int emstorage_update_tag_id(char *multi_user_name, int old_filter_id, int new_filter_id, int *err_code)
14301 {
14302         EM_DEBUG_FUNC_BEGIN("new_filter_id[%d], old_filter_id[%d]", new_filter_id, old_filter_id);
14303         int ret = false;
14304         int error = EMAIL_ERROR_NONE;
14305         char sql_query_string[QUERY_SIZE] = {0, };
14306         int transaction = true;
14307
14308         if (old_filter_id < 0 || new_filter_id < 0) {
14309                 EM_DEBUG_EXCEPTION("Invalid parameters");
14310                 if (err_code != NULL)
14311                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14312                 return false;
14313         }
14314
14315         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14316
14317         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14318
14319         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14320                  "UPDATE mail_tbl SET tag_id=%d WHERE tag_id=%d ", new_filter_id, old_filter_id);
14321
14322         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14323         if (error != EMAIL_ERROR_NONE) {
14324                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14325                         goto FINISH_OFF;
14326         }
14327
14328         ret = true;
14329
14330 FINISH_OFF:
14331         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14332
14333         if (err_code != NULL)
14334                 *err_code = error;
14335
14336         EM_DEBUG_FUNC_END("ret [%d]", ret);
14337         return ret;
14338 }
14339
14340 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)
14341 {
14342         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);
14343
14344         if ((account_id < 0) || (dest_mailbox_id < 0) || (!rule)) {
14345                 EM_DEBUG_EXCEPTION("Invalid Parameter");
14346
14347                 if (err_code != NULL)
14348                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14349                 return false;
14350         }
14351
14352         int rc = -1, ret = false, error = EMAIL_ERROR_NONE;
14353         int count = 0, col_index = 0, i = 0, where_pararaph_length = 0, *mail_list = NULL;
14354         int tag_id = rule->rule_id;
14355         char **result = NULL, *where_pararaph = NULL;
14356         char sql_query_string[QUERY_SIZE] = {0, };
14357         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14358
14359         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl ");
14360
14361         where_pararaph_length = EM_SAFE_STRLEN(rule->value) + 2 * (EM_SAFE_STRLEN(rule->value2)) + 100;
14362         where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
14363         if (where_pararaph == NULL) {
14364                 EM_DEBUG_EXCEPTION("malloc failed for where_pararaph.");
14365                 error = EMAIL_ERROR_OUT_OF_MEMORY;
14366                 goto FINISH_OFF;
14367         }
14368
14369         if (account_id != ALL_ACCOUNT)
14370                 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (0)", account_id);
14371         else
14372                 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_type NOT in (0)");
14373
14374         if (rule->type & EMAIL_FILTER_SUBJECT) {
14375                 if (rule->flag2 == RULE_TYPE_INCLUDES)
14376                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject like \'%%%q%%\'", rule->value);
14377                 else /*  RULE_TYPE_EXACTLY */
14378                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject = \'%q\'", rule->value);
14379         }
14380
14381         if (rule->type & EMAIL_FILTER_FROM) {
14382                 if (rule->flag2 == RULE_TYPE_INCLUDES)
14383                         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);
14384 #ifdef __FEATURE_COMPARE_DOMAIN__
14385                 else if (rule->flag2 == RULE_TYPE_COMPARE_DOMAIN)
14386                         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);
14387 #endif /*__FEATURE_COMPARE_DOMAIN__ */
14388                 else /*  RULE_TYPE_EXACTLY */
14389                         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);
14390         }
14391
14392         if (rule->type == EMAIL_PRIORITY_SENDER) {
14393                 if (rule->flag2 == RULE_TYPE_INCLUDES)
14394                         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);
14395                 else /*  RULE_TYPE_EXACTLY */
14396                         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);
14397
14398                 tag_id = PRIORITY_SENDER_TAG_ID;
14399         }
14400
14401         /* prevent 34361 */
14402         if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14403                 strcat(sql_query_string, where_pararaph);
14404
14405         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14406
14407         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14408         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14409         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
14410                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14411
14412         EM_DEBUG_LOG("Count of mails [%d]", count);
14413
14414         if (count) {
14415                 mail_list = malloc(sizeof(int) * count);
14416                 if (mail_list == NULL) {
14417                         EM_DEBUG_EXCEPTION("malloc failed for mail_list.");
14418                         error = EMAIL_ERROR_OUT_OF_MEMORY;
14419                         goto FINISH_OFF;
14420                 }
14421
14422                 col_index = 1;
14423
14424                 for (i = 0; i < count; i++)
14425                         _get_table_field_data_int(result, &(mail_list[i]), col_index++);
14426
14427                 memset(sql_query_string, 0x00, QUERY_SIZE);
14428                 if (reset) {
14429                         switch (rule->action_type) {
14430                         case EMAIL_FILTER_MOVE:
14431                                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = 0 ");
14432                                 break;
14433                         case EMAIL_FILTER_BLOCK:
14434                         default:
14435                                 EM_DEBUG_LOG("Not support : action_type[%d]", rule->action_type);
14436                                 ret = true;
14437                                 goto FINISH_OFF;
14438                         }
14439                 } else {
14440                         switch (rule->action_type) {
14441                         case EMAIL_FILTER_MOVE:
14442                                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = %d ", tag_id);
14443                                 break;
14444                         case EMAIL_FILTER_BLOCK:
14445                                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET mailbox_id = %d, mailbox_type = %d ", dest_mailbox_id, dest_mailbox_type);
14446                                 break;
14447                         default:
14448                                 EM_DEBUG_LOG("Not support");
14449                                 ret = true;
14450                                 goto FINISH_OFF;
14451                         }
14452                 }
14453                 /* prevent 34361 */
14454                 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14455                         strcat(sql_query_string, where_pararaph);
14456
14457                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14458                 if (error != EMAIL_ERROR_NONE) {
14459                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14460                                 goto FINISH_OFF;
14461                 }
14462
14463 #ifdef __FEATURE_BODY_SEARCH__
14464                 /* Updating mail_text_tbl */
14465                 if (rule->action_type == EMAIL_FILTER_BLOCK) {
14466                         memset(sql_query_string, 0x00, QUERY_SIZE);
14467                         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_text_tbl SET mailbox_id = %d ", dest_mailbox_id);
14468                         if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14469                                 strcat(sql_query_string, where_pararaph);
14470
14471                         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14472                         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14473                         if (error != EMAIL_ERROR_NONE) {
14474                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14475                                 goto FINISH_OFF;
14476                         }
14477                 }
14478 #endif
14479         }
14480
14481         ret = true;
14482
14483 FINISH_OFF:
14484
14485         if (ret) {
14486                 if (filtered_mail_id_list)
14487                         *filtered_mail_id_list = mail_list;
14488
14489                 if (count_of_mails)
14490                         *count_of_mails = count;
14491         } else
14492                 EM_SAFE_FREE(mail_list);
14493
14494         sqlite3_free_table(result);
14495         result = NULL;
14496
14497
14498         EM_SAFE_FREE(where_pararaph);
14499
14500         if (err_code != NULL)
14501                 *err_code = error;
14502
14503         EM_DEBUG_FUNC_END("ret [%d]", ret);
14504         return ret;
14505 }
14506
14507 #define EMAIL_SLOT_UNIT 25
14508 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)
14509 {
14510         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);
14511         int ret = false, err = EMAIL_ERROR_NONE;
14512         int where_pararaph_length = 0;
14513         char *where_pararaph = NULL;
14514         char sql_query_string[QUERY_SIZE] = {0, };
14515         int and = 0;
14516         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14517
14518         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
14519
14520         if (new_slot_size > 0)
14521                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = %d ", new_slot_size);
14522         else if (new_slot_size == 0)
14523                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", EMAIL_SLOT_UNIT);
14524         else
14525                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", new_slot_size * -1);
14526
14527
14528         if (mailbox_id)
14529                 where_pararaph_length = 80;
14530         else
14531                 where_pararaph_length = 50;
14532
14533         if (new_slot_size == 0)
14534                 where_pararaph_length += 70;
14535
14536         where_pararaph = malloc(sizeof(char) * where_pararaph_length);
14537         if (where_pararaph == NULL) {
14538                 EM_DEBUG_EXCEPTION("Memory allocation failed for where_pararaph");
14539                 err = EMAIL_ERROR_OUT_OF_MEMORY;
14540                 goto FINISH_OFF;
14541         }
14542         memset(where_pararaph, 0x00, where_pararaph_length);
14543
14544         if (account_id > ALL_ACCOUNT) {
14545                 and = 1;
14546                 if (mailbox_id)
14547                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_id = %d ", mailbox_id);
14548                 else
14549                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d ", account_id);
14550         }
14551
14552         if (new_slot_size == 0)
14553                 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"));
14554
14555         if (strlen(sql_query_string) + EM_SAFE_STRLEN(where_pararaph) < QUERY_SIZE) /* prevent 34363 */
14556                 strcat(sql_query_string, where_pararaph);
14557         else {
14558                 EM_DEBUG_EXCEPTION("Query buffer overflowed !!!");
14559                 err = EMAIL_ERROR_OUT_OF_MEMORY;
14560                 goto FINISH_OFF;
14561         }
14562
14563         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14564         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14565         if (err != EMAIL_ERROR_NONE) {
14566                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
14567                         goto FINISH_OFF;
14568         }
14569
14570         ret = true;
14571
14572 FINISH_OFF:
14573         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
14574
14575         EM_SAFE_FREE(where_pararaph);
14576
14577         if (err_code != NULL)
14578                 *err_code = err;
14579
14580         EM_DEBUG_FUNC_END("ret [%d]", ret);
14581         return ret;
14582 }
14583
14584 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)
14585 {
14586         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);
14587
14588         if (!meeting_req || meeting_req->mail_id <= 0) {
14589                 if (meeting_req)
14590                 EM_DEBUG_EXCEPTION("mail_id[%]d", meeting_req->mail_id);
14591
14592                 if (err_code != NULL)
14593                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14594
14595                 return false;
14596         }
14597
14598         int rc = -1;
14599         int ret = false;
14600         int error = EMAIL_ERROR_NONE;
14601         DB_STMT hStmt = NULL;
14602         char sql_query_string[QUERY_SIZE] = {0, };
14603         int col_index = 0;
14604         time_t temp_unix_time = 0;
14605
14606         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14607         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14608
14609         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14610                 "INSERT INTO mail_meeting_tbl VALUES "
14611                 "(?"            /*  mail_id */
14612                 ", ?"           /*  account_id */
14613                 ", ?"           /*  mailbox_id */
14614                 ", ?"           /*  meeting_response */
14615                 ", ?"           /*  start_time */
14616                 ", ?"           /*  end_time */
14617                 ", ?"           /*  location */
14618                 ", ?"           /*  global_object_id */
14619                 ", ?"           /*  offset */
14620                 ", ?"           /*  standard_name */
14621                 ", ?"           /*  standard_time_start_date */
14622                 ", ?"           /*  standard_biad */
14623                 ", ?"           /*  daylight_name */
14624                 ", ?"           /*  daylight_time_start_date */
14625                 ", ?"           /*  daylight_bias */
14626                 ")");
14627
14628         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14629         if (rc != SQLITE_OK) {
14630                 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
14631                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
14632
14633                 error = EMAIL_ERROR_DB_FAILURE;
14634                 goto FINISH_OFF;
14635         }
14636
14637         col_index = 0;
14638         /*
14639         EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14640         EM_DEBUG_LOG_SEC(">>>>> account_id[%d]", account_id);
14641         EM_DEBUG_LOG_SEC(">>>>> mailbox_name[%s]", mailbox_name);
14642         EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14643         EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14644         EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14645         EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14646         EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14647         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14648         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14649         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14650         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14651         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14652         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14653         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14654         */
14655         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->mail_id);
14656         _bind_stmt_field_data_int(hStmt, col_index++, account_id);
14657         _bind_stmt_field_data_int(hStmt, col_index++, input_mailbox_id);
14658         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14659
14660         temp_unix_time = timegm(&(meeting_req->start_time));
14661         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14662         temp_unix_time = timegm(&(meeting_req->end_time));
14663         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14664
14665         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->location, 0, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14666         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->global_object_id, 0, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14667
14668         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14669         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.standard_name, 0, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14670         temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14671         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14672         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14673
14674         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.daylight_name, 0, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14675         temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14676         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14677         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
14678
14679
14680         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14681         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
14682                 ("sqlite3_step fail:%d", rc));
14683         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14684                 ("sqlite3_step fail:%d", rc));
14685
14686         ret = true;
14687
14688 FINISH_OFF:
14689         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14690         if (hStmt != NULL) {
14691                 rc = sqlite3_finalize(hStmt);
14692                 if (rc != SQLITE_OK) {
14693                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14694                         error = EMAIL_ERROR_DB_FAILURE;
14695                 }
14696         }
14697
14698         if (err_code != NULL)
14699                 *err_code = error;
14700
14701         EM_DEBUG_FUNC_END("ret [%d]", ret);
14702         return ret;
14703 }
14704
14705 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)
14706 {
14707         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);
14708
14709         int i = 0;
14710         int col_index = 0;
14711         int rc;
14712         int count = 0;
14713         int dummy = 0;
14714         int err = EMAIL_ERROR_NONE;
14715         char **result = NULL;
14716         char sql_query_string[QUERY_SIZE] = {0, };
14717         email_meeting_request_t* p_temp_meeting_req = NULL;
14718         sqlite3 *local_db_handle = NULL;
14719         time_t temp_unix_time;
14720
14721         if (conditional_clause == NULL || output_meeting_req == NULL || output_result_count == NULL) {
14722                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14723                 err = EMAIL_ERROR_INVALID_PARAM;
14724                 goto FINISH_OFF;
14725         }
14726
14727         col_index = _field_count_of_table[CREATE_TABLE_MAIL_MEETING_TBL];
14728         EM_DEBUG_LOG("col_index [%d]", col_index);
14729
14730         local_db_handle = emstorage_get_db_connection(multi_user_name);
14731
14732         EMSTORAGE_START_READ_TRANSACTION(transaction);
14733
14734         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_meeting_tbl %s", conditional_clause);
14735
14736         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14737
14738         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14739         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
14740                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14741
14742         if (!count) {
14743                 EM_DEBUG_EXCEPTION("No meeting_request found...");
14744                 err = EMAIL_ERROR_DATA_NOT_FOUND;
14745                 goto FINISH_OFF;
14746         }
14747
14748         EM_DEBUG_LOG("There are [%d] meeting requests.", count);
14749         if (!(p_temp_meeting_req = (email_meeting_request_t*)em_malloc(sizeof(email_meeting_request_t) * count))) {
14750                 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
14751                 err = EMAIL_ERROR_OUT_OF_MEMORY;
14752                 goto FINISH_OFF;
14753         }
14754
14755         EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
14756
14757         for (i = 0; i < count; i++) {
14758                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].mail_id), col_index++);
14759                 _get_table_field_data_int(result, &dummy, col_index++); /* account_id. but why should this field exist in DB table? */
14760                 _get_table_field_data_int(result, &dummy, col_index++); /* mailbox_id */
14761                 _get_table_field_data_int(result, (int*)&(p_temp_meeting_req[i].meeting_response), col_index++);
14762                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* start time */
14763                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].start_time));
14764                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* end time */
14765                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].end_time));
14766                 _get_table_field_data_string(result, &p_temp_meeting_req[i].location, 1, col_index++);
14767                 _get_table_field_data_string(result, &p_temp_meeting_req[i].global_object_id, 1, col_index++);
14768                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.offset_from_GMT), col_index++);
14769                 _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++);
14770                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14771                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.standard_time_start_date));
14772                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.standard_bias), col_index++);
14773                 _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++);
14774                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14775                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.daylight_time_start_date));
14776                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.daylight_bias), col_index++);
14777         }
14778
14779
14780 FINISH_OFF:
14781         if (result)
14782                 sqlite3_free_table(result);
14783
14784         if (err == EMAIL_ERROR_NONE) {
14785                 if (p_temp_meeting_req)
14786                         *output_meeting_req = p_temp_meeting_req;
14787                 *output_result_count = count;
14788         } else
14789                 EM_SAFE_FREE(p_temp_meeting_req);
14790
14791         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14792
14793 //      sqlite3_db_release_memory(local_db_handle);
14794
14795         EM_DEBUG_FUNC_END("err [%d]", err);
14796         return err;
14797 }
14798
14799 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)
14800 {
14801         EM_DEBUG_FUNC_BEGIN();
14802
14803         int count = 0;
14804         int ret = false;
14805         int error = EMAIL_ERROR_NONE;
14806         char conditional_clause[QUERY_SIZE] = {0, };
14807
14808         EM_IF_NULL_RETURN_VALUE(meeting_req, false);
14809
14810
14811         SNPRINTF(conditional_clause, QUERY_SIZE, " WHERE mail_id = %d", mail_id);
14812         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
14813
14814         if ((error = emstorage_query_meeting_request(multi_user_name, conditional_clause, meeting_req, &count, transaction)) != EMAIL_ERROR_NONE) {
14815                 EM_DEBUG_EXCEPTION("emstorage_query_meeting_request failed. [%d]", error);
14816                 goto FINISH_OFF;
14817         }
14818
14819         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->mail_id[%d]", (*meeting_req)->mail_id);
14820         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->meeting_response[%d]", (*meeting_req)->meeting_response);
14821         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->start_time[%s]", asctime(&((*meeting_req)->start_time)));
14822         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->end_time[%s]", asctime(&((*meeting_req)->end_time)));
14823         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->location[%s]", (*meeting_req)->location);
14824         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->global_object_id[%s]", (*meeting_req)->global_object_id);
14825         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.offset_from_GMT[%d]", (*meeting_req)->time_zone.offset_from_GMT);
14826         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_name[%s]", (*meeting_req)->time_zone.standard_name);
14827         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.standard_time_start_date)));
14828         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_bias[%d]", (*meeting_req)->time_zone.standard_bias);
14829         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_name[%s]", (*meeting_req)->time_zone.daylight_name);
14830         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.daylight_time_start_date)));
14831         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_bias[%d]", (*meeting_req)->time_zone.daylight_bias);
14832         ret = true;
14833
14834 FINISH_OFF:
14835
14836         if (err_code != NULL)
14837                 *err_code = error;
14838
14839         EM_DEBUG_FUNC_END("ret [%d]", ret);
14840         return ret;
14841 }
14842
14843 INTERNAL_FUNC int emstorage_update_meeting_request(char *multi_user_name, email_meeting_request_t* meeting_req, int transaction, int *err_code)
14844 {
14845         EM_DEBUG_FUNC_BEGIN("meeting_req[%p], transaction[%d], err_code[%p]", meeting_req, transaction, err_code);
14846
14847         int ret = false;
14848         int error = EMAIL_ERROR_NONE;
14849         int rc;
14850         DB_STMT hStmt = NULL;
14851         char sql_query_string[QUERY_SIZE] = {0, };
14852         time_t temp_unix_time = 0;
14853
14854         if (!meeting_req) {
14855                 EM_DEBUG_EXCEPTION("Invalid Parameter!");
14856                 if (err_code != NULL)
14857                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14858                 return false;
14859         }
14860
14861         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14862         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14863
14864         memset(sql_query_string, 0x00, sizeof(sql_query_string));
14865         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14866                 "UPDATE mail_meeting_tbl "
14867                 "SET "
14868                 /* "  account_id = ?, "         //  not update here, this can be changed when move or copy */
14869                 /* "  mailbox_name = ?, "               //  not update here, this can be changed when move or copy */
14870                 "  meeting_response = ?, "
14871                 "  start_time = ?, "
14872                 "  end_time = ?, "
14873                 "  location = ?, "
14874                 "  global_object_id = ?, "
14875                 "  offset = ?, "
14876                 "  standard_name = ?, "
14877                 "  standard_time_start_date = ?, "
14878                 "  standard_bias = ?, "
14879                 "  daylight_name = ?, "
14880                 "  daylight_time_start_date = ?, "
14881                 "  daylight_bias = ? "
14882                 "WHERE mail_id = %d",
14883                 meeting_req->mail_id);
14884
14885         EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
14886
14887         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14888         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14889                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14890 /*
14891         EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14892         EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14893         EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14894         EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14895         EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14896         EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14897         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14898         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14899         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14900         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14901         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14902         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14903         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14904 */
14905         int col_index = 0;
14906
14907         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14908         temp_unix_time = timegm(&(meeting_req->start_time));
14909         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14910         temp_unix_time = timegm(&(meeting_req->end_time));
14911         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14912         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->location, 1, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14913         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->global_object_id, 1, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14914         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14915         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.standard_name, 1, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14916         temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14917         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14918         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14919         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.daylight_name, 1, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14920         temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14921         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14922         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
14923
14924
14925         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14926         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14927                 ("sqlite3_step fail:%d", rc));
14928         ret = true;
14929
14930
14931 FINISH_OFF:
14932         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14933
14934         if (hStmt != NULL) {
14935                 rc = sqlite3_finalize(hStmt);
14936                 if (rc != SQLITE_OK) {
14937                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14938                         error = EMAIL_ERROR_DB_FAILURE;
14939                 }
14940         }
14941
14942         if (err_code != NULL)
14943                 *err_code = error;
14944
14945         EM_DEBUG_FUNC_END("ret [%d]", ret);
14946         return ret;
14947 }
14948
14949 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)
14950 {
14951         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);
14952
14953         if (account_id < ALL_ACCOUNT || mail_id < 0) {
14954                 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d]", account_id, mail_id);
14955
14956                 if (err_code != NULL)
14957                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14958                 return false;
14959         }
14960
14961         int ret = false;
14962         int error = EMAIL_ERROR_NONE;
14963         int and = false;
14964         char sql_query_string[QUERY_SIZE] = {0, };
14965
14966         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14967         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14968
14969         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_meeting_tbl ");
14970
14971         if (account_id != ALL_ACCOUNT) {                /*  NOT '0' means a specific account. '0' means all account */
14972                 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);
14973                 and = true;
14974         }
14975         if (mail_id > 0) {
14976                 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);
14977                 and = true;
14978         }
14979         if (input_mailbox_id > 0) {             /*  0 means all mailbox_id */
14980                 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);
14981         }
14982
14983         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14984         if (error != EMAIL_ERROR_NONE) {
14985                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14986                         goto FINISH_OFF;
14987         }
14988
14989         ret = true;
14990
14991 FINISH_OFF:
14992         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14993
14994         if (err_code)
14995                 *err_code = error;
14996
14997         EM_DEBUG_FUNC_END("ret [%d]", ret);
14998         return ret;
14999 }
15000
15001
15002 INTERNAL_FUNC void emstorage_free_meeting_request(email_meeting_request_t *meeting_req)
15003 {
15004         EM_DEBUG_FUNC_BEGIN();
15005
15006         if (!meeting_req) return;
15007
15008         EM_SAFE_FREE(meeting_req->location);
15009         EM_SAFE_FREE(meeting_req->global_object_id);
15010
15011         EM_DEBUG_FUNC_END();
15012 }
15013
15014 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)
15015 {
15016         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);
15017         EM_PROFILE_BEGIN(profile_emstorage_get_overflowed_mail_id_list);
15018         char sql_query_string[QUERY_SIZE] = {0, };
15019         char **result = NULL;
15020         int rc = -1, ret = false;
15021         int error = EMAIL_ERROR_NONE;
15022         int counter = 0, col_index = 0;
15023         int result_mail_id_count = 0;
15024         int *result_mail_id_list = NULL;
15025
15026         if (input_mailbox_id <= 0 || !mail_id_list || !mail_id_count || account_id < 1) {
15027                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15028                 if (err_code)
15029                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15030                 return false;
15031         }
15032
15033         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);
15034
15035         EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
15036
15037         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15038         EMSTORAGE_START_READ_TRANSACTION(transaction);
15039
15040         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_mail_id_count, 0, NULL), rc);
15041         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15042                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15043
15044         if (!result_mail_id_count) {
15045                 EM_DEBUG_LOG("No mail found...");
15046                 ret = false;
15047                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
15048                 goto FINISH_OFF;
15049         }
15050
15051         EM_DEBUG_LOG("There are [%d] overflowed mails in mailbox_id [%d]", result_mail_id_count, input_mailbox_id);
15052
15053         if (!(result_mail_id_list = (int *)malloc(sizeof(int) * result_mail_id_count))) {
15054                 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
15055                 error = EMAIL_ERROR_OUT_OF_MEMORY;
15056                 sqlite3_free_table(result);
15057                 goto FINISH_OFF;
15058         }
15059
15060         memset(result_mail_id_list, 0x00, sizeof(int) * result_mail_id_count);
15061
15062         col_index = 1;
15063
15064         for (counter = 0; counter < result_mail_id_count; counter++)
15065                 _get_table_field_data_int(result, result_mail_id_list + counter, col_index++);
15066
15067         ret = true;
15068
15069 FINISH_OFF:
15070         EM_DEBUG_LOG("finish off [%d]", ret);
15071
15072         if (result)
15073                 sqlite3_free_table(result);
15074
15075         if (ret == true) {
15076                 *mail_id_list = result_mail_id_list;
15077                 *mail_id_count = result_mail_id_count;
15078         } else
15079                 EM_SAFE_FREE(result_mail_id_list);
15080
15081         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15082
15083         if (err_code != NULL)
15084                 *err_code = error;
15085
15086         EM_PROFILE_END(profile_emstorage_get_overflowed_mail_id_list);
15087         EM_DEBUG_FUNC_END("ret [%d]", ret);
15088         return ret;
15089 }
15090
15091 INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(char *multi_user_name, int mail_id, int *thread_id, int *err_code)
15092 {
15093         EM_DEBUG_FUNC_BEGIN("mail_id[%d], thread_id[%p], err_code[%p]", mail_id, thread_id, err_code);
15094
15095         int rc = -1, ret = false;
15096         int err = EMAIL_ERROR_NONE;
15097         char sql_query_string[QUERY_SIZE] = {0, };
15098         char **result;
15099         int result_count = 0;
15100
15101         if (mail_id == 0 || thread_id == NULL) {
15102                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15103                 if (err_code)
15104                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15105                 return false;
15106         }
15107
15108         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15109
15110         memset(sql_query_string, 0, QUERY_SIZE);
15111         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT thread_id FROM mail_tbl WHERE mail_id = %d", mail_id);
15112
15113         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15114         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15115         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15116                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15117
15118         if (!result_count) {
15119                 EM_DEBUG_LOG("No mail found...");
15120                 ret = false;
15121                 err = EMAIL_ERROR_MAIL_NOT_FOUND;
15122                 /* sqlite3_free_table(result); */
15123                 goto FINISH_OFF;
15124         }
15125
15126         _get_table_field_data_int(result, thread_id, 1);
15127
15128         sqlite3_free_table(result);
15129
15130         ret = true;
15131
15132 FINISH_OFF:
15133
15134         if (err_code != NULL)
15135                 *err_code = err;
15136
15137         EM_DEBUG_FUNC_END("ret [%d]", ret);
15138         return ret;
15139 }
15140
15141 INTERNAL_FUNC int emstorage_update_latest_thread_mail(char *multi_user_name,
15142                                                                                                                 int account_id,
15143                                                                                                                 int mailbox_id,
15144                                                                                                                 int mailbox_type,
15145                                                                                                                 int thread_id,
15146                                                                                                                 int *updated_thread_id,
15147                                                                                                                 int latest_mail_id,
15148                                                                                                                 int thread_item_count,
15149                                                                                                                 int noti_type,
15150                                                                                                                 int transaction,
15151                                                                                                                 int *err_code)
15152 {
15153         EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id [%d], thread_id[%d], updated_thread_id[%p], "
15154                                                 "latest_mail_id [%d], thread_item_count[%d], err_code[%p]",
15155                                                 account_id, mailbox_id, thread_id, updated_thread_id,
15156                                                 latest_mail_id, thread_item_count, err_code);
15157
15158         int rc = -1, ret = false;
15159         int err = EMAIL_ERROR_NONE;
15160         char sql_query_string[QUERY_SIZE] = {0, };
15161         char **result = NULL;
15162         int result_count = 0;
15163
15164         if (thread_id == 0) {
15165                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15166                 if (err_code)
15167                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15168                 return false;
15169         }
15170
15171         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15172
15173         if (thread_item_count == 0 && latest_mail_id == 0) {
15174                 memset(sql_query_string, 0, QUERY_SIZE);
15175                 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);
15176
15177                 /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15178                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15179                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15180                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15181                 EM_DEBUG_LOG("result_count[%d]", result_count);
15182                 if (result_count == 0) {
15183                         EM_DEBUG_LOG("No mail found...");
15184                         ret = false;
15185                         if (err_code)
15186                                 *err_code =  EMAIL_ERROR_MAIL_NOT_FOUND;
15187                         sqlite3_free_table(result);
15188                         return false;
15189                 }
15190
15191                 _get_table_field_data_int(result, &latest_mail_id, 2);
15192                 _get_table_field_data_int(result, &thread_item_count, 3);
15193
15194                 EM_DEBUG_LOG("latest_mail_id[%d]", latest_mail_id);
15195                 EM_DEBUG_LOG("thread_item_count[%d]", thread_item_count);
15196
15197                 sqlite3_free_table(result);
15198         }
15199
15200         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15201
15202         if (thread_item_count < 0) {
15203                 memset(sql_query_string, 0, QUERY_SIZE);
15204                 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);
15205                 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15206                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15207                 if (err != EMAIL_ERROR_NONE) {
15208                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15209                                 goto FINISH_OFF;
15210                 }
15211         } else if (thread_id != latest_mail_id) {
15212                 /* Initialize the thread id */
15213                 memset(sql_query_string, 0, QUERY_SIZE);
15214                 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);
15215                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15216                 if (err != EMAIL_ERROR_NONE) {
15217                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15218                                 goto FINISH_OFF;
15219                 }
15220
15221                 /* update the thread item count */
15222                 memset(sql_query_string, 0, QUERY_SIZE);
15223                 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);
15224                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15225                 if (err != EMAIL_ERROR_NONE) {
15226                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15227                                 goto FINISH_OFF;
15228                 }
15229         } else {
15230                 memset(sql_query_string, 0, QUERY_SIZE);
15231                 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);
15232                 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15233                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15234                 if (err != EMAIL_ERROR_NONE) {
15235                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15236                                 goto FINISH_OFF;
15237                 }
15238         }
15239         ret = true;
15240
15241 FINISH_OFF:
15242         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15243
15244         if (thread_id != latest_mail_id) {
15245                 if (err == EMAIL_ERROR_NONE) {
15246                         EM_DEBUG_LOG("noti_type[%d]", noti_type);
15247
15248                         if (latest_mail_id > 0 && thread_id > 0 && noti_type > 0) {
15249                                 char mailbox_id_str[25] = {0,};
15250                                 snprintf(mailbox_id_str, sizeof(mailbox_id_str), "%d", mailbox_id);
15251                                 if (!emcore_notify_storage_event(noti_type, thread_id, latest_mail_id, mailbox_id_str, account_id))
15252                                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventfailed [NOTI_THREAD_ID_CHANGED] >>>> ");
15253
15254                                 if (updated_thread_id) *updated_thread_id = latest_mail_id;
15255                         }
15256                 }
15257         } else if (thread_item_count >= 0) {
15258                 if (err == EMAIL_ERROR_NONE) {
15259                         char parameter_string[500] = {0,};
15260                         SNPRINTF(parameter_string, sizeof(parameter_string), "%s%c%d", "thread_item_count", 0x01, latest_mail_id);
15261                         if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT, parameter_string, thread_item_count))
15262                                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventfailed [NOTI_MAIL_FIELD_UPDATE] >>>> ");
15263                 }
15264         }
15265
15266         if (err_code != NULL)
15267                 *err_code = err;
15268
15269         EM_DEBUG_FUNC_END("ret [%d]", ret);
15270         return ret;
15271 }
15272
15273 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)
15274 {
15275         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);
15276
15277         int ret = false;
15278         int err = EMAIL_ERROR_NONE;
15279         char sql_query_string[QUERY_SIZE] = {0, };
15280
15281         if (thread_id == 0) {
15282                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15283                 if (err_code)
15284                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15285                 return false;
15286         }
15287
15288         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15289
15290         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15291
15292         memset(sql_query_string, 0, QUERY_SIZE);
15293         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);
15294         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15295         if (err != EMAIL_ERROR_NONE) {
15296                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15297                 goto FINISH_OFF;
15298         }
15299
15300         ret = true;
15301
15302 FINISH_OFF:
15303         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15304
15305         if (err_code != NULL)
15306                 *err_code = err;
15307
15308         EM_DEBUG_FUNC_END("ret [%d]", ret);
15309         return ret;
15310 }
15311
15312 #ifdef __FEATURE_LOCAL_ACTIVITY__
15313 /**
15314   *     emstorage_add_activity - Add Email Local activity during OFFLINE mode
15315   *
15316   */
15317 INTERNAL_FUNC int emstorage_add_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15318 {
15319         EM_DEBUG_FUNC_BEGIN();
15320
15321         EM_DEBUG_LOG(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15322
15323         int rc = -1, ret = false;
15324         int error = EMAIL_ERROR_NONE;
15325         DB_STMT hStmt = NULL;
15326         char sql_query_string[8192] = { 0x00, };
15327         int i = 0;
15328
15329         if (!local_activity) {
15330                 EM_DEBUG_EXCEPTION(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15331                 if (err_code != NULL)
15332                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15333                 return false;
15334         }
15335
15336         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15337
15338         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15339
15340         memset(sql_query_string, 0x00 , sizeof(sql_query_string));
15341         SNPRINTF(sql_query_string, sizeof(sql_query_string), "INSERT INTO mail_local_activity_tbl VALUES (?, ?, ?, ?, ?, ?, ?)");
15342
15343         EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ] ", local_activity->activity_id);
15344         EM_DEBUG_LOG(">>>>> MAIL ID [ %d ] ", local_activity->mail_id);
15345         EM_DEBUG_LOG(">>>>> ACCOUNT ID [ %d ] ", local_activity->account_id);
15346         EM_DEBUG_LOG(">>>>> ACTIVITY TYPE [ %d ] ", local_activity->activity_type);
15347         EM_DEBUG_LOG_SEC(">>>>> SERVER MAIL ID [ %s ] ", local_activity->server_mailid);
15348         EM_DEBUG_LOG(">>>>> SOURCE MAILBOX [ %s ] ", local_activity->src_mbox);
15349         EM_DEBUG_LOG(">>>>> DEST MAILBOX   [ %s ] ", local_activity->dest_mbox);
15350
15351         EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15352
15353
15354         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15355         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15356                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15357
15358         EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15359
15360         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
15361         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
15362         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
15363         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
15364         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->server_mailid, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
15365         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->src_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15366         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->dest_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15367
15368
15369         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15370
15371         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
15372                 ("sqlite3_step fail:%d", rc));
15373         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15374                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
15375
15376         ret = true;
15377
15378 FINISH_OFF:
15379         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15380
15381         if (hStmt != NULL) {
15382                 rc = sqlite3_finalize(hStmt);
15383                 if (rc != SQLITE_OK) {
15384                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15385                         error = EMAIL_ERROR_DB_FAILURE;
15386                 }
15387         }
15388
15389         if (err_code != NULL)
15390                 *err_code = error;
15391
15392         EM_DEBUG_FUNC_END("ret [%d]", ret);
15393         return ret;
15394 }
15395
15396 /**
15397   *     emstorage_get_activity - Get the Local activity Information
15398   *
15399   *
15400   */
15401 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)
15402 {
15403         EM_DEBUG_FUNC_BEGIN();
15404
15405         int i = 0, count = 0, rc = -1, ret = false;
15406         int error = EMAIL_ERROR_NONE;
15407         emstorage_activity_tbl_t *p_activity_tbl = NULL;
15408         char sql_query_string[1024] = {0x00, };
15409         char **result = NULL;
15410         int col_index ;
15411
15412         EM_IF_NULL_RETURN_VALUE(activity_list, false);
15413         EM_IF_NULL_RETURN_VALUE(select_num, false);
15414
15415
15416         if (!select_num || !activity_list || account_id <= 0 || activityid < 0) {
15417                 EM_DEBUG_LOG(" select_num[%p], activity_list[%p] account_id [%d] activityid [%d] ", select_num, activity_list, account_id, activityid);
15418                 if (err_code)
15419                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15420                 return false;
15421         }
15422
15423         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15424
15425         EMSTORAGE_START_READ_TRANSACTION(transaction);
15426
15427         memset(sql_query_string, 0x00, sizeof(sql_query_string));
15428
15429         if (activityid == ALL_ACTIVITIES) {
15430                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d order by activity_id", account_id);
15431         } else {
15432                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d AND activity_id = %d ", account_id, activityid);
15433         }
15434
15435         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
15436
15437
15438
15439         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15440         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15441                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15442
15443         col_index = 7;
15444
15445         if (!(p_activity_tbl = (emstorage_activity_tbl_t*)em_malloc(sizeof(emstorage_activity_tbl_t) * count))) {
15446                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
15447                 error = EMAIL_ERROR_OUT_OF_MEMORY;
15448                 goto FINISH_OFF;
15449         }
15450
15451
15452         for (i = 0; i < count; i++) {
15453                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15454                 if (result[col_index])
15455                         p_activity_tbl[i].activity_id = atoi(result[col_index++]);
15456
15457                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15458                 if (result[col_index])
15459                         p_activity_tbl[i].account_id = atoi(result[col_index++]);
15460
15461                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15462                 if (result[col_index])
15463                         p_activity_tbl[i].mail_id = atoi(result[col_index++]);
15464
15465                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15466                 if (result[col_index])
15467                         p_activity_tbl[i].activity_type = atoi(result[col_index++]);
15468
15469
15470                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15471                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15472                         p_activity_tbl[i].server_mailid = EM_SAFE_STRDUP(result[col_index++]);
15473                 else
15474                         col_index++;
15475
15476                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15477                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15478                         p_activity_tbl[i].src_mbox = EM_SAFE_STRDUP(result[col_index++]);
15479                 else
15480                         col_index++;
15481
15482                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15483                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15484                         p_activity_tbl[i].dest_mbox = EM_SAFE_STRDUP(result[col_index++]);
15485                 else
15486                         col_index++;
15487
15488         }
15489
15490         if (result)
15491                 sqlite3_free_table(result);
15492
15493         ret = true;
15494
15495 FINISH_OFF:
15496
15497         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15498
15499         if (ret == true) {
15500                 *activity_list = p_activity_tbl;
15501                 *select_num = count;
15502                 EM_DEBUG_LOG(">>>> COUNT : %d >> ", count);
15503         } else if (p_activity_tbl != NULL) {
15504                 emstorage_free_local_activity(&p_activity_tbl, count, NULL);
15505         }
15506
15507
15508         if (err_code != NULL)
15509                 *err_code = error;
15510
15511         EM_DEBUG_FUNC_END("ret [%d]", ret);
15512         return ret;
15513 }
15514
15515
15516 INTERNAL_FUNC int emstorage_get_next_activity_id(int *activity_id, int *err_code)
15517 {
15518
15519         EM_DEBUG_FUNC_BEGIN();
15520
15521         int ret = false;
15522         int err = EMAIL_ERROR_NONE;
15523         int rc = -1;
15524         char *sql = NULL;
15525         char **result = NULL;
15526
15527         if (NULL == activity_id) {
15528                 EM_DEBUG_EXCEPTION(" activity_id[%p]", activity_id);
15529                 if (err_code)
15530                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15531                 return false;
15532         }
15533
15534         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15535
15536         /*  increase unique id */
15537
15538         sql = "SELECT max(rowid) FROM mail_local_activity_tbl;";
15539
15540         /*  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); */
15541         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15542                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
15543
15544         if (NULL == result[1])
15545                 rc = 1;
15546         else
15547                 rc = atoi(result[1])+1;
15548
15549         *activity_id = rc;
15550
15551         if (result)
15552                 sqlite3_free_table(result);
15553
15554         ret = true;
15555
15556         FINISH_OFF:
15557
15558         if (err_code)
15559                 *err_code = err;
15560
15561         EM_DEBUG_FUNC_END("ret [%d]", ret);
15562         return ret;
15563
15564 }
15565
15566 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)
15567 {
15568
15569         EM_DEBUG_FUNC_BEGIN();
15570
15571         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);
15572
15573         if (account_id <= 0 || NULL == activity_id_list || NULL == activity_id_count || lowest_activity_type <= 0 || highest_activity_type <= 0) {
15574                 if (err_code != NULL)
15575                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15576                 return false;
15577         }
15578
15579         int ret = false;
15580         int error = EMAIL_ERROR_NONE;
15581         int i = 0, rc = -1, count = 0;
15582         char sql_query_string[1024] = {0x00, };
15583         int *activity_ids = NULL;
15584         int col_index = 0;
15585         char **result = NULL;
15586         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15587         EMSTORAGE_START_READ_TRANSACTION(transaction);
15588
15589         memset(sql_query_string, 0x00, sizeof(sql_query_string));
15590
15591         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);
15592
15593         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
15594
15595
15596         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15597         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15598                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15599
15600         col_index = 1;
15601
15602         EM_DEBUG_LOG(" Activity COUNT : %d ... ", count);
15603
15604         if (NULL == (activity_ids = (int *)em_malloc(sizeof(int) * count))) {
15605                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
15606                 error = EMAIL_ERROR_OUT_OF_MEMORY;
15607                 goto FINISH_OFF;
15608         }
15609
15610         for (i = 0; i < count; i++) {
15611                 activity_ids[i] = atoi(result[col_index]);
15612                 col_index++;
15613                 EM_DEBUG_LOG("activity_id %d", activity_ids[i]);
15614         }
15615
15616         ret = true;
15617
15618 FINISH_OFF:
15619
15620
15621         if (ret == true) {
15622                 *activity_id_count = count;
15623                 *activity_id_list = activity_ids;
15624         } else if (activity_ids != NULL) /* Prevent defect - 216566 */
15625                 EM_SAFE_FREE(activity_ids);
15626
15627
15628         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15629         if (err_code != NULL) {
15630                 *err_code = error;
15631         }
15632
15633         EM_DEBUG_FUNC_END("ret [%d]", ret);
15634         return ret;
15635 }
15636
15637 INTERNAL_FUNC int emstorage_free_activity_id_list(int *activity_id_list, int *error_code)
15638 {
15639         EM_DEBUG_FUNC_BEGIN();
15640
15641         int error = EMAIL_ERROR_NONE;
15642         int ret = false;
15643
15644         EM_DEBUG_LOG(" activity_id_list [%p]", activity_id_list);
15645
15646         if (NULL == activity_id_list) {
15647                 error = EMAIL_ERROR_INVALID_PARAM;
15648                 goto FINISH_OFF;
15649         } else {
15650                 EM_SAFE_FREE(activity_id_list);
15651         }
15652
15653
15654         ret = true;
15655
15656         FINISH_OFF:
15657
15658         if (NULL != error_code) {
15659                 *error_code = error;
15660         }
15661
15662         EM_DEBUG_FUNC_END("ret [%d]", ret);
15663         return ret;
15664 }
15665
15666 /**
15667  * emstorage_delete_local_activity - Deletes the Local acitivity Generated based on activity_type
15668  * or based on server mail id
15669  *
15670  */
15671 INTERNAL_FUNC int emstorage_delete_local_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15672 {
15673         EM_DEBUG_FUNC_BEGIN();
15674
15675
15676         EM_DEBUG_LOG(" local_activity[%p] ", local_activity);
15677
15678         if (!local_activity) {
15679                 EM_DEBUG_EXCEPTION(" local_activity[%p] ", local_activity);
15680                 if (err_code != NULL)
15681                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15682                 return false;
15683         }
15684
15685         int rc = -1, ret = false;                       /* Prevent_FIX  */
15686         int err = EMAIL_ERROR_NONE;
15687         int query_and = 0;
15688         int query_where = 0;
15689         char sql_query_string[8192] = { 0x00, };
15690         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15691         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15692
15693         memset(sql_query_string, 0x00, sizeof(sql_query_string));
15694
15695         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_local_activity_tbl ");
15696
15697         EM_DEBUG_LOG_SEC(">>> Query [ %s ] ", sql_query_string);
15698
15699         if (local_activity->account_id) {
15700                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15701                 " WHERE account_id = %d ", local_activity->account_id);
15702                 query_and = 1;
15703                 query_where = 1;
15704         }
15705
15706         if (local_activity->server_mailid) {
15707                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15708                 " %s %s server_mailid = '%s' ", query_where ? "" : "WHERE", query_and ? "AND" : "", local_activity->server_mailid);
15709                 query_and = 1;
15710                 query_where = 1;
15711         }
15712
15713
15714         if (local_activity->mail_id) {
15715                 EM_DEBUG_LOG(">>>> MAIL ID [ %d ] , ACTIVITY TYPE [%d ]", local_activity->mail_id, local_activity->activity_type);
15716
15717                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15718                 " %s %s mail_id = %d  ", query_where ? "" : "WHERE", query_and ? "AND" : "", local_activity->mail_id);
15719
15720                 query_and = 1;
15721                 query_where = 1;
15722
15723         }
15724
15725         if (local_activity->activity_type > 0) {
15726                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15727                 " %s %s activity_type = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "" , local_activity->activity_type);
15728         }
15729
15730         if (local_activity->activity_id > 0) {
15731                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15732                 " %s %s activity_id = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "" , local_activity->activity_id);
15733
15734         }
15735
15736         EM_DEBUG_LOG_SEC(">>>>> Query [ %s ] ", sql_query_string);
15737         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15738         if (error != EMAIL_ERROR_NONE) {
15739                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
15740                         goto FINISH_OFF;
15741         }
15742
15743         rc = sqlite3_changes(local_db_handle);
15744         if (rc == 0) {
15745                 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
15746                 err = EMAIL_ERROR_MAILBOX_NOT_FOUND;
15747         }
15748
15749         ret = true;
15750
15751 FINISH_OFF:
15752         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15753
15754         if (hStmt != NULL) {
15755                 rc = sqlite3_finalize(hStmt);
15756                 if (rc != SQLITE_OK) {
15757                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15758                         error = EMAIL_ERROR_DB_FAILURE;
15759                 }
15760         }
15761
15762         if (err_code != NULL)
15763                 *err_code = err;
15764         EM_DEBUG_FUNC_END("ret [%d]", ret);
15765         return ret;
15766 }
15767
15768 /**
15769 *       emstorage_free_local_activity - Free the Local Activity data
15770 */
15771 INTERNAL_FUNC int emstorage_free_local_activity(emstorage_activity_tbl_t **local_activity_list, int count, int *err_code)
15772 {
15773         EM_DEBUG_FUNC_BEGIN();
15774
15775         EM_DEBUG_LOG(" local_activity_list[%p], count[%d], err_code[%p]", local_activity_list, count, err_code);
15776
15777         int ret = false;
15778         int error = EMAIL_ERROR_INVALID_PARAM;
15779
15780         if (count > 0) {
15781                 if (!local_activity_list || !*local_activity_list) {
15782                         EM_DEBUG_EXCEPTION(" local_activity_list[%p], count[%d]", local_activity_list, count);
15783
15784                         error = EMAIL_ERROR_INVALID_PARAM;
15785                         goto FINISH_OFF;
15786                 }
15787
15788                 emstorage_activity_tbl_t* p = *local_activity_list;
15789                 int i = 0;
15790                 if (p) {
15791                         for (; i < count; i++) {
15792                                 EM_SAFE_FREE(p[i].dest_mbox);
15793                                 EM_SAFE_FREE(p[i].src_mbox);
15794                                 EM_SAFE_FREE(p[i].server_mailid);
15795                         }
15796
15797                         EM_SAFE_FREE(p);
15798                         *local_activity_list = NULL;
15799                 }
15800         }
15801
15802         ret = true;
15803
15804 FINISH_OFF:
15805
15806         if (err_code != NULL)
15807                 *err_code = error;
15808
15809         EM_DEBUG_FUNC_END("ret [%d]", ret);
15810         return ret;
15811
15812 }
15813 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
15814
15815
15816 static int _get_key_value_string_for_list_filter_rule(email_list_filter_rule_t *input_list_filter_rule, char **output_key_value_string)
15817 {
15818         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_key_value_string [%p]", input_list_filter_rule, output_key_value_string);
15819
15820         int  ret = EMAIL_ERROR_NONE;
15821         char key_value_string[QUERY_SIZE] = { 0, };
15822         char *temp_key_value_1 = NULL;
15823         char *temp_key_value_2 = NULL;
15824
15825         if (input_list_filter_rule == NULL || output_key_value_string == NULL) {
15826                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15827                 return EMAIL_ERROR_INVALID_PARAM;
15828         }
15829
15830         switch (input_list_filter_rule->target_attribute) {
15831         case EMAIL_MAIL_ATTRIBUTE_MAIL_ID:
15832         case EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID:
15833         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID:
15834         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE:
15835         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_STATUS:
15836         case EMAIL_MAIL_ATTRIBUTE_REFERENCE_MAIL_ID:
15837         case EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS:
15838         case EMAIL_MAIL_ATTRIBUTE_MAIL_SIZE:
15839         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN:
15840         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML:
15841         case EMAIL_MAIL_ATTRIBUTE_FILE_SIZE:
15842         case EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD:
15843         case EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD:
15844         case EMAIL_MAIL_ATTRIBUTE_FLAGS_FLAGGED_FIELD:
15845         case EMAIL_MAIL_ATTRIBUTE_FLAGS_ANSWERED_FIELD:
15846         case EMAIL_MAIL_ATTRIBUTE_FLAGS_RECENT_FIELD:
15847         case EMAIL_MAIL_ATTRIBUTE_FLAGS_DRAFT_FIELD:
15848         case EMAIL_MAIL_ATTRIBUTE_FLAGS_FORWARDED_FIELD:
15849         case EMAIL_MAIL_ATTRIBUTE_DRM_STATUS:
15850         case EMAIL_MAIL_ATTRIBUTE_PRIORITY:
15851         case EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS:
15852         case EMAIL_MAIL_ATTRIBUTE_LOCK_STATUS:
15853         case EMAIL_MAIL_ATTRIBUTE_REPORT_STATUS:
15854         case EMAIL_MAIL_ATTRIBUTE_ATTACHMENT_COUNT:
15855         case EMAIL_MAIL_ATTRIBUTE_INLINE_CONTENT_COUNT:
15856         case EMAIL_MAIL_ATTRIBUTE_THREAD_ID:
15857         case EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT:
15858         case EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS:
15859         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_CLASS:
15860         case EMAIL_MAIL_ATTRIBUTE_DIGEST_TYPE:
15861         case EMAIL_MAIL_ATTRIBUTE_SMIME_TYPE:
15862         case EMAIL_MAIL_ATTRIBUTE_REMAINING_RESEND_TIMES:
15863         case EMAIL_MAIL_ATTRIBUTE_TAG_ID:
15864         case EMAIL_MAIL_ATTRIBUTE_EAS_DATA_LENGTH_TYPE:
15865                 SNPRINTF(key_value_string, QUERY_SIZE, "%d", input_list_filter_rule->key_value.integer_type_value);
15866                 break;
15867
15868         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
15869         case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
15870         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
15871         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
15872         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
15873         case EMAIL_MAIL_ATTRIBUTE_FROM:
15874         case EMAIL_MAIL_ATTRIBUTE_TO:
15875         case EMAIL_MAIL_ATTRIBUTE_CC:
15876         case EMAIL_MAIL_ATTRIBUTE_BCC:
15877         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
15878                 if (input_list_filter_rule->key_value.string_type_value == NULL) {
15879                         EM_DEBUG_EXCEPTION("Invalid string_type_value [%p]", input_list_filter_rule->key_value.string_type_value);
15880                         ret = EMAIL_ERROR_INVALID_PARAM;
15881                         goto FINISH_OFF;
15882                 }
15883
15884                 temp_key_value_1 = input_list_filter_rule->key_value.string_type_value;
15885
15886                 temp_key_value_2 = em_replace_all_string(temp_key_value_1, "_", "\\_");
15887                 temp_key_value_1 = em_replace_all_string(temp_key_value_2, "%", "\\%");
15888
15889                 if (input_list_filter_rule->rule_type == EMAIL_LIST_FILTER_RULE_INCLUDE)
15890                         SNPRINTF(key_value_string, QUERY_SIZE, "\'%%%s%%\'", temp_key_value_1);
15891                 else
15892                         SNPRINTF(key_value_string, QUERY_SIZE, "\'%s\'", temp_key_value_1);
15893                 break;
15894
15895         case EMAIL_MAIL_ATTRIBUTE_DATE_TIME:
15896         case EMAIL_MAIL_ATTRIBUTE_SCHEDULED_SENDING_TIME:
15897         case EMAIL_MAIL_ATTRIBUTE_REPLIED_TIME:
15898         case EMAIL_MAIL_ATTRIBUTE_FORWARDED_TIME:
15899                 SNPRINTF(key_value_string, QUERY_SIZE, "%d", (int)input_list_filter_rule->key_value.datetime_type_value);
15900                 break;
15901
15902         default:
15903                 ret = EMAIL_ERROR_INVALID_PARAM;
15904                 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
15905                 break;
15906         }
15907
15908         if (ret == EMAIL_ERROR_NONE && EM_SAFE_STRLEN(key_value_string) > 0) {
15909                 *output_key_value_string = strdup(key_value_string);
15910         }
15911
15912 FINISH_OFF:
15913
15914         EM_SAFE_FREE(temp_key_value_1);
15915         EM_SAFE_FREE(temp_key_value_2);
15916
15917         EM_DEBUG_FUNC_END("ret [%d]", ret);
15918         return ret;
15919 }
15920
15921 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15922 static int _get_cert_password_file_name(int index, char *cert_password_file_name)
15923 {
15924         EM_DEBUG_FUNC_BEGIN("index : [%d]", index);
15925
15926         if (index <= 0 || !cert_password_file_name) {
15927                 EM_DEBUG_EXCEPTION("Invalid parameter");
15928                 return EMAIL_ERROR_INVALID_PARAM;
15929         }
15930
15931         sprintf(cert_password_file_name, ".email_cert_%d", index);
15932
15933         EM_DEBUG_FUNC_END();
15934         return EMAIL_ERROR_NONE;
15935 }
15936 #endif
15937
15938 static int _make_filter_rule_string(email_list_filter_rule_t *input_list_filter_rule, char **output_string)
15939 {
15940         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
15941
15942         int   ret = EMAIL_ERROR_NONE;
15943         int   is_alpha = 0;
15944         int   length_field_name = 0;
15945         int   length_value = 0;
15946         char  result_rule_string[QUERY_SIZE] = { 0 , };
15947         char *mod_field_name_string = NULL;
15948         char *mod_value_string = NULL;
15949         char *temp_field_name_string = NULL;
15950         char *temp_key_value_string = NULL;
15951
15952         if (input_list_filter_rule == NULL || output_string == NULL) {
15953                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15954                 return  EMAIL_ERROR_INVALID_PARAM;
15955         }
15956
15957         temp_field_name_string = emcore_get_mail_field_name_by_attribute_type(input_list_filter_rule->target_attribute);
15958
15959         if (temp_field_name_string == NULL) {
15960                 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
15961                 return EMAIL_ERROR_INVALID_PARAM;
15962         }
15963
15964         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) {
15965                 EM_DEBUG_EXCEPTION("_get_key_value_string_for_list_filter_rule failed");
15966                 return EMAIL_ERROR_INVALID_PARAM;
15967         }
15968
15969         length_field_name = EM_SAFE_STRLEN(temp_field_name_string);
15970         length_value      = EM_SAFE_STRLEN(temp_key_value_string);
15971
15972         switch (input_list_filter_rule->target_attribute) {
15973         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
15974         case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
15975         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
15976         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
15977         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
15978         case EMAIL_MAIL_ATTRIBUTE_FROM:
15979         case EMAIL_MAIL_ATTRIBUTE_TO:
15980         case EMAIL_MAIL_ATTRIBUTE_CC:
15981         case EMAIL_MAIL_ATTRIBUTE_BCC:
15982         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
15983                 is_alpha = 1;
15984                 break;
15985         default:
15986                 is_alpha = 0;
15987                 break;
15988         }
15989
15990         if (is_alpha == 1 && input_list_filter_rule->case_sensitivity == false) {
15991                 length_field_name += strlen("UPPER() ");
15992                 length_value      += strlen("UPPER() ");
15993                 mod_field_name_string = em_malloc(sizeof(char) * length_field_name);
15994                 if (mod_field_name_string == NULL) {
15995                         EM_DEBUG_EXCEPTION("em_mallocfailed");
15996                         EM_SAFE_FREE(temp_field_name_string);
15997                         ret = EMAIL_ERROR_OUT_OF_MEMORY;
15998                         goto FINISH_OFF;
15999                 }
16000
16001                 mod_value_string = em_malloc(sizeof(char) * length_value);
16002                 if (mod_value_string == NULL) {
16003                         EM_DEBUG_EXCEPTION("em_mallocfailed");
16004                         EM_SAFE_FREE(temp_field_name_string);
16005                         ret = EMAIL_ERROR_OUT_OF_MEMORY;
16006                         goto FINISH_OFF;
16007                 }
16008
16009                 SNPRINTF(mod_field_name_string, length_field_name, "UPPER(%s)", temp_field_name_string);
16010                 SNPRINTF(mod_value_string,      length_value, "UPPER(%s)", temp_key_value_string);
16011                 EM_SAFE_FREE(temp_key_value_string);
16012         } else {
16013                 mod_field_name_string = strdup(temp_field_name_string);
16014                 mod_value_string      = temp_key_value_string;
16015         }
16016
16017         switch (input_list_filter_rule->rule_type) {
16018         case EMAIL_LIST_FILTER_RULE_EQUAL:
16019                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = %s ", mod_field_name_string, mod_value_string);
16020                 break;
16021         case EMAIL_LIST_FILTER_RULE_NOT_EQUAL:
16022                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s != %s ", mod_field_name_string, mod_value_string);
16023                 break;
16024         case EMAIL_LIST_FILTER_RULE_LESS_THAN:
16025                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s < %s ", mod_field_name_string, mod_value_string);
16026                 break;
16027         case EMAIL_LIST_FILTER_RULE_GREATER_THAN:
16028                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s > %s ", mod_field_name_string, mod_value_string);
16029                 break;
16030         case EMAIL_LIST_FILTER_RULE_LESS_THAN_OR_EQUAL:
16031                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s <= %s ", mod_field_name_string, mod_value_string);
16032                 break;
16033         case EMAIL_LIST_FILTER_RULE_GREATER_THAN_OR_EQUAL:
16034                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s >= %s ", mod_field_name_string, mod_value_string);
16035                 break;
16036         case EMAIL_LIST_FILTER_RULE_INCLUDE:
16037                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s LIKE %s ", mod_field_name_string, mod_value_string);
16038                 break;
16039         case EMAIL_LIST_FILTER_RULE_IN:
16040                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s IN (%s) ", mod_field_name_string, mod_value_string);
16041                 break;
16042         case EMAIL_LIST_FILTER_RULE_NOT_IN:
16043                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s NOT IN (%s) ", mod_field_name_string, mod_value_string);
16044                 break;
16045         default:
16046                 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16047                 ret = EMAIL_ERROR_INVALID_PARAM;
16048                 goto FINISH_OFF;
16049         }
16050
16051         *output_string = strdup(result_rule_string);
16052
16053 FINISH_OFF:
16054         EM_SAFE_FREE(mod_field_name_string);
16055         EM_SAFE_FREE(mod_value_string);
16056
16057         EM_DEBUG_FUNC_END("ret [%d]", ret);
16058         return ret;
16059 }
16060
16061 static int _make_filter_attach_rule_string(char *multi_user_name, email_list_filter_rule_attach_t *input_list_filter_rule, char **output_string)
16062 {
16063         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16064
16065         char *field_name_string = NULL;
16066         char  key_value_string[QUERY_SIZE] = {0,};
16067         char  result_rule_string[QUERY_SIZE] = {0,};
16068         int rc = -1;
16069         int count = 0;
16070         int query_size = 0;
16071         int cur_query = 0;
16072         int col_index = 0;
16073         int error = EMAIL_ERROR_NONE;
16074         char **result = NULL;
16075         char sql_query_string[QUERY_SIZE] = {0,};
16076         char *sql_query_string2 = NULL;
16077         sqlite3 *local_db_handle = NULL;
16078         int *mail_ids = NULL;
16079
16080         if (input_list_filter_rule == NULL || output_string == NULL) {
16081                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16082                 return  EMAIL_ERROR_INVALID_PARAM;
16083         }
16084
16085         field_name_string = EM_SAFE_STRDUP("attachment_name");
16086         SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16087
16088         switch (input_list_filter_rule->rule_type) {
16089
16090         case EMAIL_LIST_FILTER_RULE_INCLUDE:
16091                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16092                 break;
16093
16094         case EMAIL_LIST_FILTER_RULE_MATCH:
16095                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16096                 break;
16097
16098         default:
16099                 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16100                 error = EMAIL_ERROR_INVALID_PARAM;
16101                 goto FINISH_OFF;
16102         }
16103
16104         local_db_handle = emstorage_get_db_connection(multi_user_name);
16105
16106         EMSTORAGE_START_READ_TRANSACTION(true);
16107         SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_attachment_tbl %s", result_rule_string);
16108
16109         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16110         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16111                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16112
16113         col_index = 1;
16114
16115         if (!count) {
16116                 EM_DEBUG_LOG("No mail found...");
16117                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
16118                 *output_string = strdup("mail_id IN () ");
16119                 goto FINISH_OFF;
16120         }
16121
16122         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
16123         int i = 0;
16124         if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16125                 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16126                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16127                 goto FINISH_OFF;
16128         }
16129
16130         for (i = 0; i < count; i++) {
16131                 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16132                 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16133         }
16134
16135         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >>", count);
16136         sqlite3_free_table(result);
16137         EMSTORAGE_FINISH_READ_TRANSACTION(true);
16138
16139 //      sqlite3_db_release_memory(local_db_handle);
16140
16141
16142         query_size = (10 * count) + strlen("mail_id IN ()  ");
16143
16144         sql_query_string2 = em_malloc(query_size);
16145         if (sql_query_string2 == NULL) {
16146                 EM_DEBUG_EXCEPTION("em_mallocfailed");
16147                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16148                 goto FINISH_OFF;
16149         }
16150
16151         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN (");
16152         for (i = 0; i < count-1; i++) {
16153                 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16154         }
16155         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d) ", mail_ids[count-1]);
16156
16157         *output_string = strdup(sql_query_string2);
16158 FINISH_OFF:
16159
16160         EM_SAFE_FREE(mail_ids); /* prevent */
16161         EM_SAFE_FREE(sql_query_string2);
16162         EM_SAFE_FREE(field_name_string);
16163         EM_DEBUG_FUNC_END("error [%d]", error);
16164         return error;
16165 }
16166
16167 static int _make_filter_fts_rule_string(char *multi_user_name, email_list_filter_rule_fts_t *input_list_filter_rule, char **output_string)
16168 {
16169         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16170         char *field_name_string = NULL;
16171         char key_value_string[QUERY_SIZE] = {0,};
16172         char  result_rule_string[QUERY_SIZE] = {0,};
16173         int rc = -1;
16174         int count = 0;
16175         int col_index = 0;
16176         int query_size = 0;
16177         int error = EMAIL_ERROR_NONE;
16178         char **result = NULL;
16179         char sql_query_string[QUERY_SIZE] = {0,};
16180         char *sql_query_string2 = NULL;
16181         sqlite3 *local_db_handle = NULL;
16182         int *mail_ids = NULL;
16183
16184         if (input_list_filter_rule == NULL || output_string == NULL) {
16185                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16186                 return  EMAIL_ERROR_INVALID_PARAM;
16187         }
16188
16189         field_name_string = EM_SAFE_STRDUP("body_text");
16190         SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16191
16192         switch (input_list_filter_rule->rule_type) {
16193
16194         case EMAIL_LIST_FILTER_RULE_INCLUDE:
16195                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16196                 break;
16197
16198         case EMAIL_LIST_FILTER_RULE_MATCH:
16199                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16200                 break;
16201
16202         default:
16203                 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16204                 error = EMAIL_ERROR_INVALID_PARAM;
16205                 goto FINISH_OFF;
16206         }
16207
16208         local_db_handle = emstorage_get_db_connection(multi_user_name);
16209
16210         EMSTORAGE_START_READ_TRANSACTION(true);
16211         SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_text_tbl %s", result_rule_string);
16212
16213         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16214         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16215                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16216
16217         col_index = 1;
16218
16219         if (!count) {
16220                 EM_DEBUG_LOG("No mail found...");
16221                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
16222                 *output_string = strdup("mail_id IN () ");
16223                 goto FINISH_OFF;
16224         }
16225
16226         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
16227         int i = 0;
16228
16229         if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16230                 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16231                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16232                 goto FINISH_OFF;
16233         }
16234
16235         for (i = 0; i < count; i++) {
16236                 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16237                 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16238         }
16239
16240         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >>", count);
16241         sqlite3_free_table(result);
16242         EMSTORAGE_FINISH_READ_TRANSACTION(true);
16243
16244 //      sqlite3_db_release_memory(local_db_handle);
16245
16246
16247         query_size = (10 * count) + strlen("mail_id IN ()  ");
16248         sql_query_string2 = em_malloc(query_size);
16249         if (sql_query_string2 == NULL) {
16250                 EM_DEBUG_EXCEPTION("em_mallocfailed");
16251                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16252                 goto FINISH_OFF;
16253         }
16254         int cur_query = 0;
16255         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN (");
16256         for (i = 0; i < count-1; i++) {
16257                 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16258         }
16259         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d) ", mail_ids[count-1]);
16260
16261         *output_string = strdup(sql_query_string2);
16262
16263 FINISH_OFF:
16264         EM_SAFE_FREE(mail_ids); /* prevent */
16265         EM_SAFE_FREE(sql_query_string2);
16266         EM_SAFE_FREE(field_name_string);
16267         EM_DEBUG_FUNC_END("error [%d]", error);
16268         return error;
16269 }
16270
16271 static int _make_order_rule_string(char *multi_user_name, email_list_sorting_rule_t *input_sorting_rule, char **output_string)
16272 {
16273         EM_DEBUG_FUNC_BEGIN("input_sorting_rule [%p], output_string [%p]", input_sorting_rule, output_string);
16274
16275         char  result_rule_string[QUERY_SIZE] = { 0 , };
16276         int   ret = EMAIL_ERROR_NONE;
16277
16278         emstorage_account_tbl_t *account_tbl_array = NULL;
16279         int count = 0;
16280         int i = 0;
16281         char *result_str = NULL;
16282         char *tmp_str1 = NULL;
16283         char *tmp_str2 = NULL;
16284         char query_per_account[QUERY_SIZE] = { 0 , };
16285
16286         if (input_sorting_rule->force_boolean_check) {
16287                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = 0 ", emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16288         } else
16289                 EM_SAFE_STRCPY(result_rule_string, emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16290
16291         switch (input_sorting_rule->sort_order) {
16292                 case EMAIL_SORT_ORDER_ASCEND:
16293                         EM_SAFE_STRCAT(result_rule_string, " ASC ");
16294                         break;
16295
16296                 case EMAIL_SORT_ORDER_DESCEND:
16297                         EM_SAFE_STRCAT(result_rule_string, " DESC ");
16298                         break;
16299
16300                 case EMAIL_SORT_ORDER_NOCASE_ASCEND:
16301                         EM_SAFE_STRCAT(result_rule_string, " COLLATE NOCASE ASC ");
16302                         break;
16303
16304                 case EMAIL_SORT_ORDER_NOCASE_DESCEND:
16305                         EM_SAFE_STRCAT(result_rule_string, " COLLATE NOCASE DESC ");
16306                         break;
16307
16308                 case EMAIL_SORT_ORDER_TO_CCBCC:
16309                         memset(result_rule_string, 0, QUERY_SIZE);
16310                         if (input_sorting_rule->key_value.string_type_value)
16311                                 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16312                                                 " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 ELSE 2 END ",
16313                                                 input_sorting_rule->key_value.string_type_value);
16314                         break;
16315
16316                 case EMAIL_SORT_ORDER_TO_CC_BCC:
16317                         memset(result_rule_string, 0, QUERY_SIZE);
16318                         if (input_sorting_rule->key_value.string_type_value)
16319                                 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16320                                                 " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 WHEN full_address_cc LIKE \'%%%q%%\' THEN 2 ELSE 3 END ",
16321                                                 input_sorting_rule->key_value.string_type_value, input_sorting_rule->key_value.string_type_value);
16322                         break;
16323
16324                 case EMAIL_SORT_ORDER_TO_CCBCC_ALL:
16325                         if (!emstorage_get_account_list(multi_user_name, &count, &account_tbl_array, true, false, NULL)) {
16326                                 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed");
16327                                 goto FINISH_OFF;
16328                         }
16329
16330                         if (!count) {
16331                                 EM_DEBUG_LOG("No account exist");
16332                                 ret = EMAIL_ERROR_INVALID_PARAM;
16333                                 goto FINISH_OFF;
16334                         }
16335
16336                         for (i = 0; i < count; i++) {
16337                                 if (i > 0 && result_str) {
16338                                         tmp_str2 = result_str;
16339                                         result_str = g_strconcat(tmp_str2, " OR ", NULL);
16340                                         EM_SAFE_FREE(tmp_str2);
16341                                 }
16342
16343                                 memset(query_per_account, 0, QUERY_SIZE);
16344                                 snprintf(query_per_account, QUERY_SIZE,
16345                                                 "(account_id = %d AND full_address_to LIKE \'%%%s%%\')",
16346                                                 account_tbl_array[i].account_id, account_tbl_array[i].user_email_address);
16347
16348                                 tmp_str1 = result_str;
16349                                 if (tmp_str1)
16350                                         result_str = g_strconcat(tmp_str1, query_per_account, NULL);
16351                                 else
16352                                         result_str = g_strdup(query_per_account);
16353                                 EM_SAFE_FREE(tmp_str1);
16354                         }
16355
16356                         snprintf(result_rule_string, QUERY_SIZE,
16357                                         " CASE WHEN %s THEN 1 ELSE 2 END ", result_str);
16358
16359                         EM_SAFE_FREE(result_str);
16360                         if (account_tbl_array)
16361                                 emstorage_free_account(&account_tbl_array, count, NULL);
16362                         break;
16363
16364                 case EMAIL_SORT_ORDER_LOCALIZE_ASCEND:
16365                         memset(result_rule_string, 0, QUERY_SIZE);
16366                         sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16367                                 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END ASC, %s COLLATE NOCASE ASC ",
16368                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16369                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16370                         break;
16371
16372                 case EMAIL_SORT_ORDER_LOCALIZE_DESCEND:
16373                         memset(result_rule_string, 0, QUERY_SIZE);
16374                         sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16375                                 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END DESC, %s COLLATE NOCASE DESC ",
16376                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16377                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16378                         break;
16379
16380                 default:
16381                         EM_DEBUG_EXCEPTION("Invalid sort_order [%d]", input_sorting_rule->sort_order);
16382                         ret = EMAIL_ERROR_INVALID_PARAM;
16383                         goto FINISH_OFF;
16384         }
16385
16386         *output_string = strdup(result_rule_string);
16387
16388 FINISH_OFF:
16389         EM_DEBUG_FUNC_END("ret [%d]", ret);
16390         return ret;
16391 }
16392
16393 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)
16394 {
16395         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);
16396         int ret = EMAIL_ERROR_NONE;
16397         int i = 0;
16398         int string_offset = 0;
16399         int query_size = 0;
16400         int new_query_size = 0;
16401         char *conditional_clause_string = NULL;
16402         char *result_string_for_a_item = NULL;
16403
16404         if ((input_filter_count > 0 && !input_filter_list) || (input_sorting_rule_count > 0 && !input_sorting_rule_list) || output_conditional_clause == NULL) {
16405                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16406                 return EMAIL_ERROR_INVALID_PARAM;
16407         }
16408
16409         conditional_clause_string = em_malloc(QUERY_SIZE);
16410         if (conditional_clause_string == NULL) {
16411                 EM_DEBUG_EXCEPTION("Memory is full");
16412                 return EMAIL_ERROR_OUT_OF_MEMORY;
16413         }
16414
16415         if (input_filter_count > 0) {
16416                 query_size = QUERY_SIZE;
16417                 g_strlcpy(conditional_clause_string, " WHERE ", QUERY_SIZE);
16418
16419                 for (i = 0; i < input_filter_count; i++) {
16420                         switch (input_filter_list[i].list_filter_item_type) {
16421                         case EMAIL_LIST_FILTER_ITEM_RULE:
16422                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE", i);
16423                                 _make_filter_rule_string(&(input_filter_list[i].list_filter_item.rule), &result_string_for_a_item);
16424                                 break;
16425
16426                         case EMAIL_LIST_FILTER_ITEM_RULE_FTS:
16427                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_FTS", i);
16428                                 _make_filter_fts_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_fts), &result_string_for_a_item);
16429                                 break;
16430
16431                         case EMAIL_LIST_FILTER_ITEM_RULE_ATTACH:
16432                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_ATTACH", i);
16433                                 _make_filter_attach_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_attach), &result_string_for_a_item);
16434                                 break;
16435
16436                         case EMAIL_LIST_FILTER_ITEM_OPERATOR:
16437                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_OPERATOR", i);
16438                                 switch (input_filter_list[i].list_filter_item.operator_type) {
16439                                 case EMAIL_LIST_FILTER_OPERATOR_AND:
16440                                         result_string_for_a_item = strdup("AND ");
16441                                         break;
16442                                 case EMAIL_LIST_FILTER_OPERATOR_OR:
16443                                         result_string_for_a_item = strdup("OR ");
16444                                         break;
16445                                 case EMAIL_LIST_FILTER_OPERATOR_LEFT_PARENTHESIS:
16446                                         result_string_for_a_item = strdup(" (");
16447                                         break;
16448                                 case EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS:
16449                                         result_string_for_a_item = strdup(") ");
16450                                         break;
16451                                 }
16452                                 break;
16453
16454                         default:
16455                                 EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", input_filter_list[i].list_filter_item_type);
16456                                 ret = EMAIL_ERROR_INVALID_PARAM;
16457                                 goto FINISH_OFF;
16458                         }
16459
16460                         if (result_string_for_a_item == NULL) {
16461                                 EM_DEBUG_EXCEPTION("result_string_for_a_item is null");
16462                                 ret = EMAIL_ERROR_INVALID_PARAM;
16463                                 goto FINISH_OFF;
16464                         }
16465
16466                         if (strlen(conditional_clause_string) + EM_SAFE_STRLEN(result_string_for_a_item) >= query_size) { /* prevent 34364 */
16467                                 EM_DEBUG_LOG("QUERY is too long");
16468                                 new_query_size = EM_SAFE_STRLEN(result_string_for_a_item) + EM_SAFE_STRLEN(conditional_clause_string) + QUERY_SIZE;
16469                                 conditional_clause_string = realloc(conditional_clause_string, new_query_size);
16470                                 if (conditional_clause_string == NULL) {
16471                                         EM_DEBUG_EXCEPTION("realloc failed");
16472                                         ret = EMAIL_ERROR_OUT_OF_MEMORY;
16473                                         goto FINISH_OFF;
16474                                 }
16475
16476                                 query_size = new_query_size;
16477                         }
16478
16479                         strcat(conditional_clause_string, result_string_for_a_item);
16480                         EM_SAFE_FREE(result_string_for_a_item);
16481                 }
16482         }
16483
16484         if (input_sorting_rule_count > 0) {
16485                 strcat(conditional_clause_string, "ORDER BY ");
16486
16487                 for (i = 0; i < input_sorting_rule_count; i++) {
16488                         if ((ret = _make_order_rule_string(multi_user_name, &input_sorting_rule_list[i], &result_string_for_a_item)) != EMAIL_ERROR_NONE) {
16489                                 EM_DEBUG_EXCEPTION("_make_order_rule_string failed. [%d]", ret);
16490                                 goto FINISH_OFF;
16491                         }
16492                         if (i > 0)
16493                                 strcat(conditional_clause_string, ", ");
16494                         strcat(conditional_clause_string, result_string_for_a_item);
16495                         EM_SAFE_FREE(result_string_for_a_item);
16496                 }
16497         }
16498
16499         if (input_start_index != -1 && input_limit_count != -1) {
16500                 string_offset = strlen(conditional_clause_string);
16501                 SNPRINTF_OFFSET(conditional_clause_string, string_offset, query_size, " LIMIT %d, %d", input_start_index, input_limit_count);
16502         }
16503
16504         *output_conditional_clause = strdup(conditional_clause_string);
16505
16506 FINISH_OFF:
16507         EM_SAFE_FREE(result_string_for_a_item);
16508         EM_SAFE_FREE(conditional_clause_string);
16509
16510         EM_DEBUG_FUNC_END("ret [%d]", ret);
16511         return ret;
16512 }
16513
16514 INTERNAL_FUNC int emstorage_free_list_filter(email_list_filter_t **input_filter_list, int input_filter_count)
16515 {
16516         EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d]", input_filter_list, input_filter_count);
16517         int err = EMAIL_ERROR_NONE;
16518         int i = 0;
16519         email_list_filter_t *temp_filter_list = NULL;
16520
16521         EM_IF_NULL_RETURN_VALUE(input_filter_list, EMAIL_ERROR_INVALID_PARAM);
16522
16523         for (i = 0; i < input_filter_count; i++) {
16524                 temp_filter_list = (*input_filter_list) + i;
16525                 if (!temp_filter_list) {
16526                         continue;
16527                 }
16528
16529                 if (temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE) {
16530                         switch (temp_filter_list->list_filter_item.rule.target_attribute) {
16531                         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
16532                         case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
16533                         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
16534                         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
16535                         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
16536                         case EMAIL_MAIL_ATTRIBUTE_FROM:
16537                         case EMAIL_MAIL_ATTRIBUTE_TO:
16538                         case EMAIL_MAIL_ATTRIBUTE_CC:
16539                         case EMAIL_MAIL_ATTRIBUTE_BCC:
16540                         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN:
16541                         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML:
16542                         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
16543                                 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule.key_value.string_type_value);
16544                                 break;
16545                         default:
16546                                 break;
16547                         }
16548                 } 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) {
16549                                 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_fts.key_value.string_type_value);
16550                 } 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) {
16551                                 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_attach.key_value.string_type_value);
16552                 }
16553         }
16554
16555         EM_SAFE_FREE(*input_filter_list);
16556
16557         EM_DEBUG_FUNC_END("err [%d]", err);
16558         return err;
16559 }
16560
16561 /* Tasks --------------------------------------------------------------------------*/
16562 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)
16563 {
16564         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);
16565         int ret = 0;
16566         int i = 0;
16567         int task_id = 0;
16568         int err = EMAIL_ERROR_NONE;
16569         int rc = -1;
16570         DB_STMT hStmt = NULL;
16571         char sql_query_string[QUERY_SIZE] = {0, };
16572         sqlite3 *local_db_handle = NULL;
16573         char *sql = "SELECT max(rowid) FROM mail_task_tbl;";
16574         char **result = NULL;
16575
16576         if (input_task_parameter == NULL || output_task_id == NULL) {
16577                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16578                 return EMAIL_ERROR_INVALID_PARAM;
16579         }
16580
16581         local_db_handle = emstorage_get_db_connection(multi_user_name);
16582         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, err);
16583
16584         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
16585         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16586                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
16587
16588         if (NULL == result[1])
16589                 task_id = 1;
16590         else
16591                 task_id = atoi(result[1])+1;
16592
16593         *output_task_id = task_id;
16594
16595         sqlite3_free_table(result);
16596         result = NULL;
16597
16598         SNPRINTF(sql_query_string, sizeof(sql_query_string),
16599                 "INSERT INTO mail_task_tbl VALUES "
16600                 "(        "
16601                 "    ? "  /*   task_id */
16602                 "  , ? "  /*   task_type */
16603                 "  , ? "  /*   task_status */
16604                 "  , ? "  /*   task_priority */
16605                 "  , ? "  /*   task_parameter_length */
16606                 "  , ? "  /*   task_parameter */
16607                 "  , ? "  /*   date_time */
16608                 ") ");
16609
16610         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16611         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16612                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16613
16614         EM_DEBUG_LOG_SEC(">>>> SQL STMT [%s] ", sql_query_string);
16615
16616
16617         _bind_stmt_field_data_int(hStmt, i++, task_id);
16618         _bind_stmt_field_data_int(hStmt, i++, input_task_type);
16619         _bind_stmt_field_data_int(hStmt, i++, EMAIL_TASK_STATUS_WAIT);
16620         _bind_stmt_field_data_int(hStmt, i++, input_task_priority);
16621         _bind_stmt_field_data_int(hStmt, i++, input_task_parameter_length);
16622         _bind_stmt_field_data_blob(hStmt, i++, input_task_parameter, input_task_parameter_length);
16623         _bind_stmt_field_data_int(hStmt, i++, time(NULL));
16624
16625         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16626
16627         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {err = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
16628                 ("sqlite3_step fail:%d", rc));
16629         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16630                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
16631
16632         ret = (err == EMAIL_ERROR_NONE);
16633
16634 FINISH_OFF:
16635         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, err);
16636
16637         if (hStmt != NULL) {
16638                 rc = sqlite3_finalize(hStmt);
16639                 if (rc != SQLITE_OK) {
16640                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16641                         err = EMAIL_ERROR_DB_FAILURE;
16642                 }
16643         }
16644
16645         EM_DEBUG_FUNC_END("err [%d]", err);
16646         return err;
16647 }
16648
16649 INTERNAL_FUNC int emstorage_delete_task(char *multi_user_name, int task_id, int transaction)
16650 {
16651         EM_DEBUG_FUNC_BEGIN("task_id[%d], transaction[%d]", task_id, transaction);
16652         int ret = false;
16653         int err = EMAIL_ERROR_NONE;
16654         char sql_query_string[QUERY_SIZE] = {0, };
16655
16656         sqlite3 *local_db_handle = NULL;
16657
16658         if (task_id < 0) {
16659                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16660                 return EMAIL_ERROR_INVALID_PARAM;
16661         }
16662
16663         local_db_handle = emstorage_get_db_connection(multi_user_name);
16664
16665         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
16666
16667         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_task_tbl WHERE task_id = %d", task_id);
16668         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16669         if (err != EMAIL_ERROR_NONE) {
16670                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16671                         goto FINISH_OFF;
16672         }
16673
16674         ret = true;
16675
16676 FINISH_OFF:
16677         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
16678
16679         EM_DEBUG_FUNC_END("err [%d]", err);
16680         return err;
16681 }
16682
16683 INTERNAL_FUNC int emstorage_update_task_status(char *multi_user_name, int task_id, email_task_status_type_t task_status, int transaction)
16684 {
16685         EM_DEBUG_FUNC_BEGIN("task_id[%d] task_status[%d] transaction[%d]", task_id, task_status, transaction);
16686         int ret = false;
16687         int err = EMAIL_ERROR_NONE;
16688         char sql_query_string[QUERY_SIZE] = {0, };
16689
16690         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16691         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
16692
16693         SNPRINTF(sql_query_string, sizeof(sql_query_string),
16694                 "UPDATE mail_task_tbl SET"
16695                 " task_status = %d"
16696                 " WHERE task_id = %d"
16697                 , task_status
16698                 , task_id);
16699         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16700         if (err != EMAIL_ERROR_NONE) {
16701                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16702                         goto FINISH_OFF;
16703         }
16704
16705         ret = true;
16706
16707 FINISH_OFF:
16708         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
16709
16710         EM_DEBUG_FUNC_END("err [%d]", err);
16711         return err;
16712 }
16713
16714 INTERNAL_FUNC int emstorage_query_task(char *multi_user_name,
16715                                                                         const char *input_conditional_clause,
16716                                                                         const char *input_ordering_clause,
16717                                                                         email_task_t **output_task_list,
16718                                                                         int *output_task_count)
16719 {
16720         EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_ordering_clause [%p], "
16721                                                 "output_task_list[%p], output_task_count[%d]",
16722                                                 input_conditional_clause, input_ordering_clause, output_task_list, output_task_count);
16723         int i = 0, count = 0, rc = -1;
16724         int cur_query = 0;
16725         int field_index = 0;
16726         int err = EMAIL_ERROR_NONE;
16727         email_task_t *task_item_from_tbl = NULL;
16728         char sql_query_string[QUERY_SIZE] = {0, };
16729         char *field_list = "task_id, task_type, task_status, task_priority, task_parameter_length, task_parameter ";
16730         char **result;
16731         sqlite3 *local_db_handle = NULL;
16732         DB_STMT hStmt = NULL;
16733
16734         EM_IF_NULL_RETURN_VALUE(input_conditional_clause, false);
16735         EM_IF_NULL_RETURN_VALUE(output_task_count, false);
16736
16737         local_db_handle = emstorage_get_db_connection(multi_user_name);
16738
16739         SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
16740                                         "SELECT COUNT(*) FROM mail_task_tbl %s", input_conditional_clause);
16741         EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
16742
16743         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
16744         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16745                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16746
16747         count = atoi(result[1]);
16748         sqlite3_free_table(result);
16749
16750         EM_DEBUG_LOG("count = %d", rc);
16751
16752         if (count == 0) {
16753                 EM_DEBUG_EXCEPTION("no task found...");
16754                 err = EMAIL_ERROR_TASK_NOT_FOUND;
16755                 goto FINISH_OFF;
16756         }
16757
16758         SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
16759                                         "SELECT %s FROM mail_task_tbl %s %s", field_list, input_conditional_clause, input_ordering_clause);
16760         EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
16761
16762         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16763
16764         EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
16765         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16766                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16767
16768         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16769         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16770                 ("sqlite3_step fail:%d", rc));
16771
16772         if (rc == SQLITE_DONE) {
16773                 EM_DEBUG_EXCEPTION("no task found...");
16774                 err = EMAIL_ERROR_TASK_NOT_FOUND;
16775                 count = 0;
16776                 goto FINISH_OFF;
16777         }
16778
16779         if (!(task_item_from_tbl = (email_task_t*)em_malloc(sizeof(email_task_t) * count))) {
16780                 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
16781                 err = EMAIL_ERROR_OUT_OF_MEMORY;
16782                 goto FINISH_OFF;
16783         }
16784
16785         for (i = 0; i < count; i++) {
16786                 /*  get recordset */
16787                 field_index = 0;
16788
16789                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_id), field_index++);
16790                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_type), field_index++);
16791                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_status), field_index++);
16792                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_priority), field_index++);
16793                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_parameter_length), field_index++);
16794                 _get_stmt_field_data_blob(hStmt, (void**)&(task_item_from_tbl[i].task_parameter), field_index++);
16795
16796                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16797                 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i,  rc);
16798                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16799                         ("sqlite3_step fail:%d", rc));
16800         }
16801
16802 FINISH_OFF:
16803
16804         if (err == EMAIL_ERROR_NONE) {
16805                 if (output_task_list)
16806                         *output_task_list = task_item_from_tbl;
16807                 *output_task_count = count;
16808         } else {
16809                 if (task_item_from_tbl) {
16810                         for (i = 0; i < count; i++)
16811                                 EM_SAFE_FREE(task_item_from_tbl[i].task_parameter);
16812
16813                         free(task_item_from_tbl);
16814                 }
16815         }
16816
16817         if (hStmt != NULL) {
16818                 rc = sqlite3_finalize(hStmt);
16819                 hStmt = NULL;
16820                 if (rc != SQLITE_OK) {
16821                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16822                         err = EMAIL_ERROR_DB_FAILURE;
16823                 }
16824         }
16825
16826         EM_DEBUG_FUNC_END("err [%d]", err);
16827         return err;
16828 }
16829
16830 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)
16831 {
16832         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);
16833         int err = EMAIL_ERROR_NONE;
16834
16835         if (message_id == NULL) {
16836                 EM_DEBUG_EXCEPTION("Invalid parameter");
16837                 err = EMAIL_ERROR_INVALID_PARAM;
16838                 return err;
16839         }
16840
16841         int rc = -1;
16842         int count = 0;
16843         int temp_mail_id = 0;
16844         int where_pararaph_length = 0;
16845         char *where_pararaph = NULL;
16846         char sql_query_string[QUERY_SIZE] = {0, };
16847         char **result = NULL;
16848         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16849
16850         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id from mail_tbl ");
16851
16852         where_pararaph_length = EM_SAFE_STRLEN(message_id) + 100;
16853         where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
16854         if (where_pararaph == NULL) {
16855                 EM_DEBUG_EXCEPTION("em_mallocfailed");
16856                 err = EMAIL_ERROR_OUT_OF_MEMORY;
16857                 goto FINISH_OFF;
16858         }
16859
16860         if (account_id != ALL_ACCOUNT)
16861                 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);
16862         else
16863                 sqlite3_snprintf(where_pararaph_length, where_pararaph, "WHERE mailbox_type = %d AND message_id like '%q'", input_mailbox_type, message_id);
16864
16865         if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
16866                 strcat(sql_query_string, where_pararaph);
16867
16868         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
16869
16870         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16871         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16872                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16873
16874
16875         EM_DEBUG_LOG("Count of mails [%d]", count);
16876
16877         if (count) {
16878                 _get_table_field_data_int(result, &temp_mail_id, 1);
16879                 EM_DEBUG_LOG("Searched mail_id [%d]", temp_mail_id);
16880
16881                 memset(sql_query_string, 0x00, QUERY_SIZE);
16882                 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string, "UPDATE mail_tbl set server_mail_id = '%q'", server_uid);
16883
16884                 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
16885                         strcat(sql_query_string, where_pararaph);
16886
16887                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16888                 if (err != EMAIL_ERROR_NONE) {
16889                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16890                         goto FINISH_OFF;
16891                 }
16892
16893         } else {
16894                 err = EMAIL_ERROR_MAIL_NOT_FOUND;
16895         }
16896
16897 FINISH_OFF:
16898
16899         sqlite3_free_table(result);
16900         result = NULL;
16901
16902
16903         EM_SAFE_FREE(where_pararaph);
16904
16905         if (mail_id != NULL)
16906                 *mail_id = temp_mail_id;
16907
16908         EM_DEBUG_FUNC_END("err : [%d]", err);
16909         return err;
16910 }
16911 /* Tasks --------------------------------------------------------------------------*/
16912
16913 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
16914 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)
16915 {
16916         EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
16917
16918         if (!local_activity || !activity_id) {
16919                 EM_DEBUG_EXCEPTION("local_activity[%p], activity_id[%p]", local_activity, activity_id);
16920                 if (err_code != NULL)
16921                         *err_code = EMAIL_ERROR_INVALID_PARAM;
16922                 return false;
16923         }
16924
16925         int rc = -1;
16926         int ret = false;
16927         int error = EMAIL_ERROR_NONE;
16928         int i = 0;
16929
16930         char sql_query_string[QUERY_SIZE] = {0, };
16931         DB_STMT hStmt = NULL;
16932
16933         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16934         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
16935
16936         memset(sql_query_string, 0x00, sizeof(sql_query_string));
16937         SNPRINTF(sql_query_string, sizeof(sql_query_string),
16938                 "INSERT INTO mail_auto_download_activity_tbl VALUES "
16939                 "("
16940                 "? "  /* Activity ID */
16941                 ",?"  /* Status */
16942                 ",?"  /* Account ID */
16943                 ",?"  /* Local Mail ID */
16944                 ",?"  /* Server mail ID */
16945                 ",?"  /* Mailbox ID*/
16946                 ",?"  /* Multi USER NAME */
16947                 ") ");
16948
16949         char *sql = "SELECT max(rowid) FROM mail_auto_download_activity_tbl;";
16950         char **result = NULL;
16951
16952
16953         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
16954
16955         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16956                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
16957
16958         if (NULL == result[1]) rc = 1;
16959         else rc = atoi(result[1])+1;
16960         sqlite3_free_table(result);
16961         result = NULL;
16962
16963         *activity_id = local_activity->activity_id = rc;
16964
16965         EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [%d], MAIL ID [%d], SERVER MAIL ID [%lu]",
16966                         local_activity->activity_id, local_activity->mail_id, local_activity->server_mail_id);
16967
16968         if (local_activity->mailbox_id)
16969                 EM_DEBUG_LOG(" MAILBOX ID [%d]", local_activity->mailbox_id);
16970
16971
16972         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16973         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16974                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16975
16976
16977         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
16978         _bind_stmt_field_data_int(hStmt, i++, local_activity->status);
16979         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
16980         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
16981         _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
16982         _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
16983         _bind_stmt_field_data_string(hStmt, i++, (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
16984
16985
16986         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16987
16988         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
16989                 ("sqlite3_step fail:%d", rc));
16990         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16991                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
16992
16993         ret = true;
16994
16995 FINISH_OFF:
16996         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
16997         if (hStmt != NULL) {
16998                 rc = sqlite3_finalize(hStmt);
16999                 hStmt = NULL;
17000                 if (rc != SQLITE_OK) {
17001                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17002                         error = EMAIL_ERROR_DB_FAILURE;
17003                 }
17004         }
17005
17006         if (err_code != NULL)
17007                 *err_code = error;
17008
17009         EM_DEBUG_FUNC_END("ret [%d]", ret);
17010         return ret;
17011 }
17012
17013
17014 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)
17015 {
17016         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);
17017
17018         if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
17019                 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);
17020
17021                 if (err_code != NULL)
17022                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17023                 return false;
17024         }
17025
17026         int rc = -1;
17027         int ret = false;
17028         int error = EMAIL_ERROR_NONE;
17029         char sql_query_string[QUERY_SIZE] = {0, };
17030         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17031
17032         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17033         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17034
17035         if (activity_id == 0)
17036                 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);
17037         else
17038                 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);
17039
17040         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17041         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17042         if (error != EMAIL_ERROR_NONE) {
17043                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17044                 goto FINISH_OFF;
17045         }
17046
17047         /*  validate activity existence */
17048         rc = sqlite3_changes(local_db_handle);
17049         if (rc == 0) {
17050                 EM_DEBUG_EXCEPTION("No matching activity found");
17051                 error = EMAIL_ERROR_DATA_NOT_FOUND;
17052                 ret = true;
17053                 goto FINISH_OFF;
17054         }
17055
17056         ret = true;
17057
17058 FINISH_OFF:
17059         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17060
17061         if (err_code != NULL)
17062                 *err_code = error;
17063
17064         EM_DEBUG_FUNC_END("ret [%d]", ret);
17065         return ret;
17066 }
17067
17068 INTERNAL_FUNC int emstorage_delete_all_auto_download_activity(char *multi_user_name, int account_id, int transaction, int *err_code)
17069 {
17070         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17071
17072         if (account_id < FIRST_ACCOUNT_ID) {
17073                 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17074                 if (err_code != NULL)
17075                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17076                 return false;
17077         }
17078
17079         int rc = -1;
17080         int ret = false;
17081         int error = EMAIL_ERROR_NONE;
17082         char sql_query_string[QUERY_SIZE] = {0, };
17083
17084         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17085
17086         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17087         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17088         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d", account_id);
17089
17090         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17091         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17092         if (error != EMAIL_ERROR_NONE) {
17093                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17094                         goto FINISH_OFF;
17095         }
17096
17097         rc = sqlite3_changes(local_db_handle);
17098         if (rc == 0) {
17099                 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17100                 error = EMAIL_ERROR_DATA_NOT_FOUND;
17101                 ret = true;
17102                 goto FINISH_OFF;
17103         }
17104
17105         ret = true;
17106
17107 FINISH_OFF:
17108         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17109
17110         if (err_code != NULL)
17111                 *err_code = error;
17112
17113         EM_DEBUG_FUNC_END("ret [%d]", ret);
17114         return ret;
17115 }
17116
17117
17118 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)
17119 {
17120         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17121
17122         if (account_id < FIRST_ACCOUNT_ID || mailbox_id < 0) {
17123                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17124                 if (err_code != NULL)
17125                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17126                 return false;
17127         }
17128
17129         int rc = -1;
17130         int ret = false;
17131         int error = EMAIL_ERROR_NONE;
17132         char sql_query_string[QUERY_SIZE] = {0, };
17133
17134         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17135
17136         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17137         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17138         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);
17139
17140         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17141         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17142         if (error != EMAIL_ERROR_NONE) {
17143                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17144                         goto FINISH_OFF;
17145         }
17146
17147         rc = sqlite3_changes(local_db_handle);
17148         if (rc == 0) {
17149                 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17150                 error = EMAIL_ERROR_DATA_NOT_FOUND;
17151                 ret = true;
17152                 goto FINISH_OFF;
17153         }
17154
17155         ret = true;
17156
17157 FINISH_OFF:
17158         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17159
17160         if (err_code != NULL)
17161                 *err_code = error;
17162
17163         EM_DEBUG_FUNC_END("ret [%d]", ret);
17164         return ret;
17165 }
17166
17167
17168 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)
17169 {
17170         EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
17171
17172         if (account_id < FIRST_ACCOUNT_ID || !event_start || input_mailbox_id <= 0 || !count) {
17173                 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);
17174
17175                 if (err_code != NULL)
17176                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17177                 return false;
17178         }
17179
17180         int rc = -1;
17181         int ret = false;
17182         char **result;
17183         int error = EMAIL_ERROR_NONE;
17184         int i = 0;
17185         DB_STMT hStmt = NULL;
17186         email_event_auto_download *event_list = NULL;
17187         char sql_query_string[QUERY_SIZE] = {0, };
17188
17189         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17190
17191         EMSTORAGE_START_READ_TRANSACTION(transaction);
17192
17193         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17194         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);
17195
17196
17197         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17198         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17199                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17200
17201         *count = atoi(result[1]);
17202         sqlite3_free_table(result);
17203
17204         EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
17205
17206         if (!*count) {
17207                 EM_DEBUG_LOG("No matched activity found in mail_auto_download_activity_tbl");
17208                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
17209                 ret = true;
17210                 goto FINISH_OFF;
17211         }
17212         EM_DEBUG_LOG("Activity Count = %d", *count);
17213
17214         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17215         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);
17216
17217         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17218
17219
17220         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17221
17222         EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
17223         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17224                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17225
17226
17227         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17228         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17229                 ("sqlite3_step fail:%d", rc));
17230
17231         if (!(event_list = (email_event_auto_download *)em_malloc(sizeof(email_event_auto_download)*(*count)))) {
17232                 EM_DEBUG_EXCEPTION("Malloc failed");
17233
17234                 error = EMAIL_ERROR_OUT_OF_MEMORY;
17235                 goto FINISH_OFF;
17236         }
17237
17238         for (i = 0; i < (*count); i++) {
17239                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17240                 _get_stmt_field_data_int(hStmt, &(event_list[i].status), STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17241                 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17242                 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17243                 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17244                 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17245
17246                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17247                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17248                         ("sqlite3_step fail:%d", rc));
17249         }
17250
17251         ret = true;
17252
17253 FINISH_OFF:
17254
17255         if (true == ret)
17256           *event_start = event_list;
17257         else {
17258                 EM_SAFE_FREE(event_list);
17259                 *event_start = NULL;
17260                 *count = 0;
17261         }
17262
17263         if (hStmt != NULL) {
17264                 rc = sqlite3_finalize(hStmt);
17265                 hStmt = NULL;
17266                 if (rc != SQLITE_OK) {
17267                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17268                         error = EMAIL_ERROR_DB_FAILURE;
17269                 }
17270         }
17271
17272         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17273         if (err_code != NULL)
17274                 *err_code = error;
17275
17276         EM_DEBUG_FUNC_END("ret [%d]", ret);
17277         return ret;
17278 }
17279
17280
17281 INTERNAL_FUNC int emstorage_get_auto_download_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
17282 {
17283         EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
17284
17285         if (!activity_count || !err_code) {
17286                 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
17287                 if (err_code != NULL)
17288                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17289                 return false;
17290         }
17291
17292         int rc = -1;
17293         int ret = false;
17294         int error = EMAIL_ERROR_NONE;
17295         DB_STMT hStmt = NULL;
17296         char sql_query_string[QUERY_SIZE] = {0, };
17297
17298         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17299
17300         EMSTORAGE_START_READ_TRANSACTION(transaction);
17301         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17302
17303         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl;");
17304
17305         EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
17306
17307
17308         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17309         EM_DEBUG_LOG_DEV("before sqlite3_prepare hStmt = %p", hStmt);
17310         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17311                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17312
17313
17314         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17315         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17316                 ("sqlite3_step fail:%d", rc));
17317
17318         _get_stmt_field_data_int(hStmt, activity_count, 0);
17319
17320         EM_DEBUG_LOG("counts of activities in activity table [%d]", *activity_count);
17321
17322         ret = true;
17323
17324 FINISH_OFF:
17325
17326         if (hStmt != NULL) {
17327                 rc = sqlite3_finalize(hStmt);
17328                 hStmt = NULL;
17329                 if (rc != SQLITE_OK) {
17330                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17331                         error = EMAIL_ERROR_DB_FAILURE;
17332                 }
17333         }
17334
17335         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17336         if (err_code != NULL)
17337                 *err_code = error;
17338
17339         EM_DEBUG_FUNC_END("ret [%d]", ret);
17340         return ret;
17341 }
17342
17343
17344 INTERNAL_FUNC int emstorage_get_auto_download_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
17345 {
17346         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
17347
17348         if (!account_list || !count) {
17349                 EM_DEBUG_EXCEPTION("account_list[%p], count[%p]", account_list, count);
17350                 if (err_code != NULL)
17351                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17352                 return false;
17353         }
17354
17355         int ret = false;
17356         int error = EMAIL_ERROR_NONE;
17357         char *sql = "SELECT count(distinct account_id) FROM mail_auto_download_activity_tbl";
17358         char **result;
17359         int i = 0, rc = -1;
17360         int *result_account_list = NULL;
17361         DB_STMT hStmt = NULL;
17362         char sql_query_string[QUERY_SIZE] = {0, };
17363         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17364
17365         EMSTORAGE_START_READ_TRANSACTION(transaction);
17366
17367         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
17368         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17369                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
17370
17371         *count = atoi(result[1]);
17372         sqlite3_free_table(result);
17373
17374         if (!*count) {
17375                 EM_DEBUG_EXCEPTION("no account found...");
17376                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17377                 ret = true;
17378                 goto FINISH_OFF;
17379         }
17380
17381         EM_DEBUG_LOG("Account count [%d]", *count);
17382
17383         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17384
17385         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_auto_download_activity_tbl");
17386
17387         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17388
17389
17390         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17391
17392         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
17393         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17394                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17395
17396
17397         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17398         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17399                 ("sqlite3_step fail:%d", rc));
17400
17401         if (NULL == (result_account_list = (int *)em_malloc(sizeof(int)*(*count)))) {
17402                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
17403                 error = EMAIL_ERROR_OUT_OF_MEMORY;
17404                 goto FINISH_OFF;
17405         }
17406
17407         for (i = 0; i < (*count); i++) {
17408                 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
17409
17410                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17411                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17412                         ("sqlite3_step fail:%d", rc));
17413                 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
17414         }
17415
17416         ret = true;
17417
17418 FINISH_OFF:
17419
17420         if (ret == true)
17421                 *account_list = result_account_list;
17422         else
17423                 EM_SAFE_FREE(result_account_list);
17424
17425         if (hStmt != NULL) {
17426                 rc = sqlite3_finalize(hStmt);
17427                 hStmt = NULL;
17428                 if (rc != SQLITE_OK) {
17429                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17430                         error = EMAIL_ERROR_DB_FAILURE;
17431                 }
17432         }
17433
17434         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17435         if (err_code != NULL)
17436                 *err_code = error;
17437         EM_DEBUG_FUNC_END("ret [%d]", ret);
17438         return ret;
17439 }
17440
17441
17442 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)
17443 {
17444         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
17445
17446         if (account_id < FIRST_ACCOUNT_ID || !mailbox_list || !count) {
17447                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p]", account_id, mailbox_list, count);
17448                 if (err_code != NULL)
17449                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17450                 return false;
17451         }
17452
17453         int ret = false;
17454         int error = EMAIL_ERROR_NONE;
17455         char **result;
17456         int i = 0, rc = -1;
17457         int *mbox_list = NULL;
17458         DB_STMT hStmt = NULL;
17459         char sql_query_string[QUERY_SIZE] = {0, };
17460
17461         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17462
17463         EMSTORAGE_START_READ_TRANSACTION(transaction);
17464
17465         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17466         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);
17467
17468
17469         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17470         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17471                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17472
17473         *count = atoi(result[1]);
17474         sqlite3_free_table(result);
17475
17476         if (!*count) {
17477                 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
17478                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17479                 ret = true;
17480                 goto FINISH_OFF;
17481         }
17482         EM_DEBUG_LOG("Mailbox count = %d", *count);
17483
17484         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17485
17486         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);
17487
17488         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
17489
17490
17491         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17492
17493
17494         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17495                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17496
17497
17498         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17499         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17500                 ("sqlite3_step fail:%d", rc));
17501
17502         mbox_list = (int *)em_malloc(sizeof(int)*(*count)); /* prevent */
17503         if (mbox_list == NULL) {
17504                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
17505                 error = EMAIL_ERROR_OUT_OF_MEMORY;
17506                 goto FINISH_OFF;
17507         }
17508
17509         for (i = 0; i < (*count); i++) {
17510                 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
17511                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17512
17513                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17514                         ("sqlite3_step fail:%d", rc));
17515                 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
17516         }
17517
17518         ret = true;
17519
17520 FINISH_OFF:
17521
17522         if (ret == true)
17523                 *mailbox_list = mbox_list;
17524         else
17525                 EM_SAFE_FREE(mbox_list);
17526
17527         if (hStmt != NULL) {
17528                 rc = sqlite3_finalize(hStmt);
17529                 hStmt = NULL;
17530                 if (rc != SQLITE_OK) {
17531                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17532                         error = EMAIL_ERROR_DB_FAILURE;
17533                 }
17534         }
17535
17536         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17537         if (err_code != NULL)
17538                 *err_code = error;
17539         EM_DEBUG_FUNC_END("ret [%d]", ret);
17540         return ret;
17541 }
17542
17543
17544 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)
17545 {
17546         EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
17547
17548         if (account_id < FIRST_ACCOUNT_ID || !activity_count || !err_code) {
17549                 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
17550                 if (err_code != NULL)
17551                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17552                 return false;
17553         }
17554
17555         int rc = -1;
17556         int ret = false;
17557         int error = EMAIL_ERROR_NONE;
17558         char sql_query_string[QUERY_SIZE] = {0, };
17559         DB_STMT hStmt = NULL;
17560
17561         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17562
17563         EMSTORAGE_START_READ_TRANSACTION(transaction);
17564         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17565         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);
17566
17567         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
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         EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
17571         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17572                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17573
17574         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17575         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17576                 ("sqlite3_step fail:%d", rc));
17577
17578         _get_stmt_field_data_int(hStmt, activity_count, 0);
17579
17580         EM_DEBUG_LOG("count of activities in activity table [%d]", *activity_count);
17581
17582         ret = true;
17583
17584 FINISH_OFF:
17585
17586         if (hStmt != NULL) {
17587                 rc = sqlite3_finalize(hStmt);
17588                 hStmt = NULL;
17589                 if (rc != SQLITE_OK) {
17590                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17591                         error = EMAIL_ERROR_DB_FAILURE;
17592                 }
17593         }
17594
17595         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17596
17597         if (err_code != NULL)
17598                 *err_code = error;
17599
17600         EM_DEBUG_FUNC_END("ret [%d]", ret);
17601         return ret;
17602 }
17603
17604
17605 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)
17606 {
17607         EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mailbox_id[%d]", old_server_uid, new_server_uid, mailbox_id);
17608
17609         int rc = -1, ret = false;
17610         int error = EMAIL_ERROR_NONE;
17611         char sql_query_string[QUERY_SIZE] = {0, };
17612         int transaction = true;
17613
17614         if (!old_server_uid || !new_server_uid || (!mailbox_name && mailbox_id < 0)) {
17615                 EM_DEBUG_EXCEPTION("Invalid parameters");
17616                 error = EMAIL_ERROR_INVALID_PARAM;
17617                 return false;
17618         }
17619
17620         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17621
17622         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17623         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17624
17625         if (mailbox_id > 0)
17626                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17627                          "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);
17628         else if (mailbox_name)
17629                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17630                          "UPDATE mail_auto_download_activity_tbl SET server_mail_id = %s WHERE server_mail_id = %s ", new_server_uid, old_server_uid);
17631
17632         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17633         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17634         if (error != EMAIL_ERROR_NONE) {
17635                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17636                         goto FINISH_OFF;
17637         }
17638
17639         rc = sqlite3_changes(local_db_handle);
17640         if (rc == 0) {
17641                 EM_DEBUG_LOG("No matching found in mail_auto_download_activity_tbl");
17642         }
17643
17644         ret = true;
17645
17646 FINISH_OFF:
17647
17648         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17649
17650         if (err_code != NULL)
17651                 *err_code = error;
17652
17653         EM_DEBUG_FUNC_END("ret [%d]", ret);
17654         return ret;
17655 }
17656
17657 #endif
17658
17659 #ifdef __FEATURE_UPDATE_DB_TABLE_SCHEMA__
17660
17661 typedef struct {
17662         char *column_name;
17663         char *column_type;
17664 } email_column_info_t;
17665
17666 static int get_column_information_from_table_callback(void *arg1, int argc, char **argv, char **input_column_name)
17667 {
17668         EM_DEBUG_FUNC_BEGIN("arg1[%p] argc[%d] argv[%p] column_name[%p]", arg1, argc, argv, input_column_name);
17669
17670         int i = 0;
17671         int validated = 0;
17672         char *column_name = NULL;
17673         char *column_type = NULL;
17674         GList *new_list = *((GList**)arg1);
17675         email_column_info_t *column_info_item = NULL;
17676
17677         for (i = 0; i < argc; ++i) {
17678                 /* EM_DEBUG_LOG("%s = %s", input_column_name[i], argv[i]); */
17679                 if (EM_SAFE_STRCMP(input_column_name[i], "name") == 0) {
17680                         if (column_name)
17681                                 EM_SAFE_FREE(column_name);
17682
17683                         column_name = EM_SAFE_STRDUP(argv[i]);
17684                         validated = 1;
17685                 } else if (EM_SAFE_STRCMP(input_column_name[i], "type") == 0) {
17686                         if (column_type)
17687                                 EM_SAFE_FREE(column_type);
17688
17689                         column_type = EM_SAFE_STRDUP(argv[i]);
17690                 }
17691         }
17692
17693         if (validated) {
17694                 EM_DEBUG_LOG("column_name[%s] column_type[%s]", column_name, column_type);
17695                 column_info_item = em_malloc(sizeof(email_column_info_t));
17696         if (column_info_item == NULL) {
17697             EM_DEBUG_EXCEPTION("em_mallocfailed");
17698             goto FINISH_OFF;
17699         }
17700
17701                 column_info_item->column_name = EM_SAFE_STRDUP(column_name);
17702                 column_info_item->column_type = EM_SAFE_STRDUP(column_type);
17703                 new_list = g_list_append(new_list, (gpointer)column_info_item);
17704                 *((GList**)arg1) = new_list;
17705         }
17706
17707 FINISH_OFF:
17708
17709     EM_SAFE_FREE(column_name);
17710     EM_SAFE_FREE(column_type);
17711
17712         EM_DEBUG_FUNC_END();
17713         return 0;
17714 }
17715
17716 static int emstorage_get_column_information_from_table(char *multi_user_name, const char *input_table_name, GList **output_column_info)
17717 {
17718         EM_DEBUG_FUNC_BEGIN("input_table_name[%p] output_column_info[%p]", input_table_name, output_column_info);
17719         int err = EMAIL_ERROR_NONE;
17720         int result_from_sqlite = 0;
17721         char *error_message_from_sqlite = NULL;
17722         char sql_query_string[QUERY_SIZE] = {0, };
17723         GList *new_list = NULL;
17724         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17725
17726         SNPRINTF(sql_query_string, QUERY_SIZE, "pragma table_info(%s);", input_table_name);
17727
17728         result_from_sqlite = sqlite3_exec(local_db_handle, sql_query_string, get_column_information_from_table_callback, &new_list, &error_message_from_sqlite);
17729
17730         if (result_from_sqlite != SQLITE_OK)
17731                 EM_DEBUG_EXCEPTION("sqlite3_exec returns [%d]", result_from_sqlite);
17732
17733         EM_DEBUG_LOG("new_list[%p] output_column_info[%p]", new_list, output_column_info);
17734
17735         if (new_list && output_column_info) {
17736                 EM_DEBUG_LOG("g_list_length[%d]", g_list_length(new_list));
17737                 *output_column_info = new_list;
17738         }
17739
17740         EM_DEBUG_FUNC_END("err [%d]", err);
17741         return err;
17742 }
17743
17744 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)
17745 {
17746         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);
17747         int error = EMAIL_ERROR_NONE;
17748         int rc = -1;
17749         sqlite3 *local_db_handle = NULL;
17750         char sql_query_string[QUERY_SIZE] = {0, };
17751
17752         if (input_full_query == NULL || input_source_table_name == NULL || input_new_table_name == NULL) {
17753                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17754                 error = EMAIL_ERROR_INVALID_PARAM;
17755                 goto FINISH_OFF;
17756         }
17757
17758         local_db_handle = emstorage_get_db_connection(multi_user_name);
17759
17760         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17761         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17762
17763         EM_DEBUG_LOG("[%s] will be replaced by [%s]", input_source_table_name, input_new_table_name);
17764
17765         EM_SAFE_STRNCPY(sql_query_string, input_full_query[input_query_index], sizeof(sql_query_string)-1); /*prevent 21984*/
17766         reg_replace(sql_query_string, input_source_table_name, input_new_table_name);
17767
17768         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17769 FINISH_OFF:
17770
17771         if (error == EMAIL_ERROR_NONE) {
17772                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17773         } else {
17774                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17775         }
17776
17777         EM_DEBUG_FUNC_END("error [%d]", error);
17778         return error;
17779 }
17780
17781 static int emstorage_add_column(char *multi_user_name, char *input_table_name, email_column_info_t *input_new_column)
17782 {
17783         EM_DEBUG_FUNC_BEGIN("input_table_name[%p] input_new_column[%p]", input_table_name, input_new_column);
17784         int error = EMAIL_ERROR_NONE;
17785         int rc = -1;
17786         sqlite3 *local_db_handle = NULL;
17787         char sql_query_string[QUERY_SIZE] = {0, };
17788
17789         if (input_table_name == NULL || input_new_column == NULL) {
17790                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17791                 error = EMAIL_ERROR_INVALID_PARAM;
17792                 goto FINISH_OFF;
17793         }
17794
17795         local_db_handle = emstorage_get_db_connection(multi_user_name);
17796
17797         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17798         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17799         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);
17800         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17801 FINISH_OFF:
17802
17803         if (error == EMAIL_ERROR_NONE) {
17804                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17805         } else {
17806                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17807         }
17808
17809         EM_DEBUG_FUNC_END("error [%d]", error);
17810         return error;
17811 }
17812
17813 static int emstorage_drop_table(char *multi_user_name, char *input_table_name)
17814 {
17815         EM_DEBUG_FUNC_BEGIN("input_table_name[%p]", input_table_name);
17816         int error = EMAIL_ERROR_NONE;
17817         int rc = -1;
17818         sqlite3 *local_db_handle = NULL;
17819         char sql_query_string[QUERY_SIZE] = {0, };
17820
17821         if (input_table_name == NULL) {
17822                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17823                 error = EMAIL_ERROR_INVALID_PARAM;
17824                 goto FINISH_OFF;
17825         }
17826
17827         local_db_handle = emstorage_get_db_connection(multi_user_name);
17828
17829         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17830         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17831         SNPRINTF(sql_query_string, QUERY_SIZE, "DROP TABLE %s;", input_table_name);
17832         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17833 FINISH_OFF:
17834
17835         if (error == EMAIL_ERROR_NONE) {
17836                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17837         } else {
17838                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17839         }
17840
17841         EM_DEBUG_FUNC_END("error [%d]", error);
17842         return error;
17843 }
17844
17845 gint glist_compare_column_name(gconstpointer old_column_info, gconstpointer new_column_info)
17846 {
17847         EM_DEBUG_FUNC_BEGIN("old_column_info[%p] new_column_info[%p]", old_column_info, new_column_info);
17848         email_column_info_t *left_one  = (email_column_info_t*)old_column_info;
17849         email_column_info_t *right_one = (email_column_info_t*)new_column_info;
17850
17851         if (old_column_info == NULL || new_column_info == NULL)
17852                 return -1;
17853
17854         return EM_SAFE_STRCMP((char*)left_one->column_name, (char*)right_one->column_name);
17855 }
17856
17857 INTERNAL_FUNC int emstorage_update_db_table_schema(char *multi_user_name)
17858 {
17859         EM_DEBUG_FUNC_BEGIN();
17860         int i = 0;
17861         int j = 0;
17862         int error = EMAIL_ERROR_NONE;
17863         int query_len = 0;
17864         email_column_info_t *new_column_info = NULL;
17865         email_column_info_t *p_column_info = NULL;
17866         char **create_table_query = NULL;
17867         GList *found_data = NULL;
17868         GList *column_list_of_old_table = NULL;
17869         GList *column_list_of_new_table = NULL;
17870         char table_names[CREATE_TABLE_MAX][2][50] = { { "mail_account_tbl", "mail_account_tbl_new" },
17871                 { "mail_box_tbl", "mail_box_tbl_new" },
17872                 { "mail_read_mail_uid_tbl", "mail_read_mail_uid_tbl_new" },
17873                 { "mail_rule_tbl", "mail_rule_tbl_new" },
17874                 { "mail_tbl", "mail_tbl_new" },
17875                 { "mail_attachment_tbl", "mail_attachment_tbl_new" },
17876 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
17877                 { "mail_partial_body_activity_tbl", "mail_partial_body_activity_tbl_new" },
17878 #else
17879                 { "", "" },
17880 #endif
17881                 { "mail_meeting_tbl", "mail_meeting_tbl_new" },
17882 #ifdef __FEATURE_LOCAL_ACTIVITY__
17883                 { "mail_local_activity_tbl", "mail_local_activity_tbl_new" },
17884 #else
17885                 { "", "" },
17886 #endif
17887                 { "mail_certificate_tbl", "mail_certificate_tbl_new" },
17888                 { "mail_task_tbl", "mail_task_tbl_new" },
17889 #ifdef __FEATURE_BODY_SEARCH__
17890                 { "mail_text_tbl", "mail_text_tbl_new" },
17891 #else
17892                 { "", "" },
17893 #endif
17894
17895 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
17896                 { "mail_auto_download_activity_tbl", "mail_auto_download_activity_tbl_new" }
17897 #else
17898                 { "", "" }
17899 #endif
17900
17901         };
17902
17903         error = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
17904
17905         if (error != EMAIL_ERROR_NONE) {
17906                 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
17907                 goto FINISH_OFF;
17908         }
17909
17910         if (query_len < CREATE_TABLE_MAX) {
17911                 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
17912                 error = EMAIL_ERROR_SYSTEM_FAILURE;
17913                 goto FINISH_OFF;
17914         }
17915
17916         for (i = CREATE_TABLE_MAIL_ACCOUNT_TBL; i < CREATE_TABLE_MAX; i++) {
17917                 EM_DEBUG_LOG("table [%s] new_table [%s]", table_names[i][0], table_names[i][1]);
17918                 if (EM_SAFE_STRLEN(table_names[i][0]) && EM_SAFE_STRLEN(table_names[i][1])) {
17919                         /* Check existing of _new table */
17920                         emstorage_drop_table(multi_user_name, table_names[i][1]);
17921                         error = emstorage_create_renamed_table(multi_user_name, create_table_query, i, table_names[i][0], table_names[i][1]);
17922                         if (error != EMAIL_ERROR_NONE) {
17923                                 EM_DEBUG_EXCEPTION("emstorage_create_renamed_table failed [%d]", error);
17924                                 goto FINISH_OFF;
17925                         }
17926
17927                         emstorage_get_column_information_from_table(multi_user_name, table_names[i][0], &column_list_of_old_table);
17928                         emstorage_get_column_information_from_table(multi_user_name, table_names[i][1], &column_list_of_new_table);
17929
17930                         /* Compare fields and add new field */
17931                         for (j = 0; j < g_list_length(column_list_of_new_table); j++) {
17932                                 new_column_info = (email_column_info_t*)g_list_nth_data(column_list_of_new_table, j);
17933                                 found_data = g_list_find_custom(column_list_of_old_table, (gconstpointer)new_column_info, glist_compare_column_name);
17934                                 if (found_data == NULL) {
17935                                         /* add new field*/
17936                                         emstorage_add_column(multi_user_name, table_names[i][0], new_column_info);
17937                                 }
17938                         }
17939
17940                         emstorage_drop_table(multi_user_name, table_names[i][1]);
17941                 } else
17942                         EM_DEBUG_LOG("Skipped");
17943         }
17944
17945 FINISH_OFF:
17946         if (create_table_query) {
17947                 int i = 0;
17948                 for (i = 0; i < query_len; i++) {
17949                         if (create_table_query[i]) {
17950                                 EM_SAFE_FREE(create_table_query[i]);
17951                         }
17952                 }
17953                 EM_SAFE_FREE(create_table_query);
17954         }
17955
17956         found_data = g_list_first(column_list_of_old_table);
17957         while (found_data != NULL) {
17958                 p_column_info = (email_column_info_t *)found_data->data;
17959                 EM_SAFE_FREE(p_column_info->column_name);
17960                 EM_SAFE_FREE(p_column_info->column_type);
17961                 EM_SAFE_FREE(p_column_info);
17962
17963                 found_data = g_list_next(found_data);
17964         }
17965         g_list_free(column_list_of_old_table);
17966
17967         found_data = g_list_first(column_list_of_new_table);
17968         while (found_data != NULL) {
17969                 p_column_info = (email_column_info_t *)found_data->data;
17970                 EM_SAFE_FREE(p_column_info->column_name);
17971                 EM_SAFE_FREE(p_column_info->column_type);
17972                 EM_SAFE_FREE(p_column_info);
17973
17974                 found_data = g_list_next(found_data);
17975         }
17976         g_list_free(column_list_of_new_table);
17977
17978         EM_DEBUG_FUNC_END("error [%d]", error);
17979         return error;
17980 }
17981 #endif /* __FEATURE_UPDATE_DB_TABLE_SCHEMA__ */
17982
17983 /*EOF*/