fix svace issue
[platform/core/messaging/email-service.git] / email-core / email-storage / email-storage.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21
22
23 /******************************************************************************
24  * File: email-storage.c
25  * Desc: email-service Storage Library on Sqlite3
26  *
27  * Auth:
28  *
29  * History:
30  *      2007.03.02 : created
31  *****************************************************************************/
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <stdbool.h>
37 #include <time.h>
38 #include <sys/types.h>
39 #include <sys/time.h>
40 #include <sys/wait.h>
41 #include <glib.h>
42 #include <sys/stat.h>
43 #include <unistd.h>
44 #include <errno.h>
45 #include <dirent.h>
46 #include <vconf.h>
47 #include <sys/mman.h>
48 #include <fcntl.h>
49 #include <tzplatform_config.h>
50
51 #define __USE_UNIX98
52 #define __USE_GNU
53 #include <pthread.h>
54 #include <grp.h>
55 #include <pwd.h>
56
57 #include "email-internal-types.h"
58 #include "email-convert.h"
59 #include "email-core-utils.h"
60 #include "email-utilities.h"
61 #include "email-storage.h"
62 #include "email-debug-log.h"
63 #include "email-types.h"
64 #include "email-convert.h"
65 #include "email-core-signal.h"
66 #include "email-core-event.h"
67 #include "email-core-container.h"
68 #include "email-core-gmime.h"
69 #include "email-core-key-manager.h"
70
71 #define SETTING_MEMORY_TEMP_FILE_PATH "/tmp/email_tmp_file"
72 #define CONTENT_DATA                  "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset="
73 #define CONTENT_TYPE_DATA             "<meta http-equiv=\"Content-Type\" content=\"text/html; charset="
74
75 #define FLAG_SEEN       0x01
76 #define FLAG_DELETED    0x02
77 #define FLAG_FLAGGED    0x04
78 #define FLAG_ANSWERED   0x08
79 #define FLAG_OLD        0x10
80 #define FLAG_DRAFT      0x20
81
82 #undef close
83
84 #define ISSUE_ORGANIZATION_LEN_IN_MAIL_CERTIFICATE_TBL  256
85 #define EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL       128
86 #define SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL      1027
87 #define FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL           256
88
89 #define ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL            50
90 #define RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL   50
91 #define EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL              128
92 #define USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL               50
93 #define PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL                50
94 #define SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL     50
95 #define SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL            50
96 #define SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL        50
97 #define DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL            30
98 #define REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL           128
99 #define RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL             128
100 #define LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL          256
101 #define DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL       256
102 #define SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL               256
103 #define MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL                128
104 #define ALIAS_LEN_IN_MAIL_BOX_TBL                       128
105 #define LOCAL_MBOX_LEN_IN_MAIL_READ_MAIL_UID_TBL        128
106 #define MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL      128
107 #define S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL             128
108 #define DATA2_LEN_IN_MAIL_READ_MAIL_UID_TBL             256
109 #define FILTER_NAME_LEN_IN_MAIL_RULE_TBL                256
110 #define VALUE_LEN_IN_MAIL_RULE_TBL                      256
111 #define VALUE2_LEN_IN_MAIL_RULE_TBL                     256
112 #define DEST_MAILBOX_LEN_IN_MAIL_RULE_TBL               128
113 #define MAILBOX_NAME_LEN_IN_MAIL_ATTACHMENT_TBL         128
114 #define ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL      256
115 #define ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL      256
116 #define CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL           256
117 #define ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL 128
118 #define MAILBOX_LEN_IN_MAIL_TBL                         128
119 #define SERVER_MAILBOX_LEN_IN_MAIL_TBL                  128
120 #define SERVER_MAIL_ID_LEN_IN_MAIL_TBL                  128
121 #define FROM_LEN_IN_MAIL_TBL                            256
122 #define SENDER_LEN_IN_MAIL_TBL                          256
123 #define REPLY_TO_LEN_IN_MAIL_TBL                        256
124 #define TO_LEN_IN_MAIL_TBL                              3999
125 #define CC_LEN_IN_MAIL_TBL                              3999
126 #define BCC_LEN_IN_MAIL_TBL                             3999
127 #define RETURN_PATH_LEN_IN_MAIL_TBL                     3999
128 #define SUBJECT_LEN_IN_MAIL_TBL                         1027
129 #define THREAD_TOPIC_LEN_IN_MAIL_TBL                    256
130 #define TEXT_1_LEN_IN_MAIL_TBL                          256
131 #define TEXT_2_LEN_IN_MAIL_TBL                          256
132 #define MIME_ENTITY_LEN_IN_MAIL_TBL                     256
133 #define DATETIME_LEN_IN_MAIL_TBL                        128
134 #define MESSAGE_ID_LEN_IN_MAIL_TBL                      256
135 #define FROM_CONTACT_NAME_LEN_IN_MAIL_TBL               3999
136 #define FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL              3999
137 #define TO_CONTACT_NAME_LEN_IN_MAIL_TBL                 3999
138 #define TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL                3999
139 #define MAILBOX_LEN_IN_MAIL_MEETING_TBL                 128
140 #define LOCATION_LEN_IN_MAIL_MEETING_TBL                1024
141 #define GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL        512
142 #define STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL           32
143 #define DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL           32
144 #define PREVIEWBODY_LEN_IN_MAIL_TBL                     512
145 #define CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL        256
146 #define UID                                             "root"          /* UID = root */
147 #define UID_DEFAULT                                     0               /* UID = root */
148 #define GID                                             "priv_email"    /* GID = priv_email */
149 #define GID_DEFAULT                                     10901           /* GID = priv_email */
150 #define MAX_GRP_BUF_SIZE (1024 * 4)
151
152 /*  this define is used for query to change data (delete, insert, update) */
153 #define EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction_flag, error_code) \
154         do { \
155                 if (transaction_flag) { \
156                         if (emstorage_begin_transaction(multi_user_name, NULL, NULL, &error_code) == false) { \
157                                 EM_DEBUG_EXCEPTION("emstorage_begin_transaction error [%d]", error_code); \
158                                 goto FINISH_OFF; \
159                         } \
160                 } \
161         } while (0)
162
163 /*  this define is used for query to change data (delete, insert, update) */
164 #define EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction_flag, result_code, error_code) \
165         do { \
166                 if (transaction_flag) { \
167                         if (result_code == true) { \
168                                 if (emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL) == false) { \
169                                         EM_DEBUG_EXCEPTION("emstorage_commit_transaction error"); \
170                                         error_code = EMAIL_ERROR_DB_FAILURE; \
171                                         result_code = false; \
172                                 } \
173                         } \
174                         else { \
175                                 if (emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL) == false) { \
176                                         EM_DEBUG_EXCEPTION("emstorage_rollback_transaction error"); \
177                                         error_code = EMAIL_ERROR_DB_FAILURE; \
178                                 } \
179                         } \
180                 } \
181         } while (0)
182
183 /*  this define is used for query to read (select) */
184 #define EMSTORAGE_START_READ_TRANSACTION(transaction_flag) \
185         if (transaction_flag) { \
186                 /*_timedlock_shm_mutex(mapped_for_db_lock, 2);*/\
187         }
188
189 /*  this define is used for query to read (select) */
190 #define EMSTORAGE_FINISH_READ_TRANSACTION(transaction_flag) \
191         if (transaction_flag) { \
192                 /*_unlockshm_mutex(mapped_for_db_lock);*/\
193         }
194
195 /*  for safety DB operation */
196 static pthread_mutex_t _db_handle_lock = PTHREAD_MUTEX_INITIALIZER;
197
198
199 #define _MULTIPLE_DB_HANDLE
200
201 #ifdef _MULTIPLE_DB_HANDLE
202
203 typedef struct {
204     char *user_name;
205         pthread_t       thread_id;
206         sqlite3 *db_handle;
207 } db_handle_t;
208
209 #define MAX_DB_CLIENT 100
210
211 /* static int _db_handle_count = 0; */
212 db_handle_t _db_handle_list[MAX_DB_CLIENT] = {{NULL, 0, 0}, };
213
214
215 sqlite3 *emstorage_get_db_handle(char *multi_user_name)
216 {
217         EM_DEBUG_FUNC_BEGIN();
218         int i;
219         pthread_t current_thread_id = THREAD_SELF();
220         sqlite3 *result_db_handle = NULL;
221
222         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
223                 ENTER_CRITICAL_SECTION(_db_handle_lock);
224                 for (i = 0; i < MAX_DB_CLIENT; i++) {
225                         if (EM_SAFE_STRCASECMP(_db_handle_list[i].user_name, multi_user_name) != 0)
226                                 continue;
227
228                         if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
229                                 EM_DEBUG_LOG_DEV("found db handle at [%d]", i);
230                                 result_db_handle = _db_handle_list[i].db_handle;
231                                 break;
232                         }
233                 }
234                 LEAVE_CRITICAL_SECTION(_db_handle_lock);
235         } else {
236                 ENTER_CRITICAL_SECTION(_db_handle_lock);
237                 for (i = 0; i < MAX_DB_CLIENT; i++) {
238                         if (EM_SAFE_STRLEN(_db_handle_list[i].user_name) > 0)
239                                 continue;
240
241                         if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
242                                 EM_DEBUG_LOG_DEV("found db handle at [%d]", i);
243                                 result_db_handle = _db_handle_list[i].db_handle;
244                                 break;
245                         }
246                 }
247                 LEAVE_CRITICAL_SECTION(_db_handle_lock);
248         }
249
250         if (!result_db_handle)
251                 EM_DEBUG_LOG("no db_handle for [%d] found", current_thread_id);
252
253         EM_DEBUG_FUNC_END();
254         return result_db_handle;
255 }
256
257 int emstorage_set_db_handle(char *multi_user_name, sqlite3 *db_handle)
258 {
259         EM_DEBUG_FUNC_BEGIN();
260         int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
261         pthread_t current_thread_id = THREAD_SELF();
262
263         ENTER_CRITICAL_SECTION(_db_handle_lock);
264         for (i = 0; i < MAX_DB_CLIENT; i++)     {
265                 if (_db_handle_list[i].thread_id == 0) {
266                         _db_handle_list[i].thread_id = current_thread_id;
267                         _db_handle_list[i].db_handle = db_handle;
268             /* Only distinguished container and host  */
269             _db_handle_list[i].user_name = EM_SAFE_STRDUP(multi_user_name);
270                         EM_DEBUG_LOG("current_thread_id [%d], index [%d]", current_thread_id, i);
271                         error_code =  EMAIL_ERROR_NONE;
272                         break;
273                 }
274         }
275         LEAVE_CRITICAL_SECTION(_db_handle_lock);
276
277         if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
278                 EM_DEBUG_EXCEPTION("Exceeded the limitation of db client. Can't find empty slot in _db_handle_list.");
279
280         EM_DEBUG_FUNC_END("error_code [%d]", error_code);
281         return error_code;
282 }
283
284 int emstorage_remove_db_handle()
285 {
286         EM_DEBUG_FUNC_BEGIN();
287         int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
288         ENTER_CRITICAL_SECTION(_db_handle_lock);
289         for (i = 0; i < MAX_DB_CLIENT; i++) {
290                 if (_db_handle_list[i].thread_id == THREAD_SELF()) {
291                         _db_handle_list[i].thread_id = 0;
292                         _db_handle_list[i].db_handle = NULL;
293             EM_SAFE_FREE(_db_handle_list[i].user_name);
294
295                         EM_DEBUG_LOG("index [%d]", i);
296                         error_code = EMAIL_ERROR_NONE;
297                         break;
298                 }
299         }
300         LEAVE_CRITICAL_SECTION(_db_handle_lock);
301
302         if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
303                 EM_DEBUG_EXCEPTION("Can't find proper thread_id");
304
305         EM_DEBUG_FUNC_END("error_code [%d]", error_code);
306         return error_code;
307 }
308
309 int emstorage_reset_db_handle_list()
310 {
311         EM_DEBUG_FUNC_BEGIN();
312         int i;
313
314         ENTER_CRITICAL_SECTION(_db_handle_lock);
315         for (i = 0; i < MAX_DB_CLIENT; i++) {
316                 _db_handle_list[i].thread_id = 0;
317                 _db_handle_list[i].db_handle = NULL;
318         EM_SAFE_FREE(_db_handle_list[i].user_name);
319         }
320         LEAVE_CRITICAL_SECTION(_db_handle_lock)
321
322         EM_DEBUG_FUNC_END();
323         return EMAIL_ERROR_NONE;
324 }
325
326
327 sqlite3 *emstorage_get_db_connection(char *multi_user_name)
328 {
329         return emstorage_db_open(multi_user_name, NULL);
330 }
331
332 #else   /*  _MULTIPLE_DB_HANDLE */
333
334 sqlite3 *_db_handle = NULL;
335
336 sqlite3 *emstorage_get_db_connection()
337 {
338         if (NULL == _db_handle)
339                 emstorage_db_open(NULL);
340         return _db_handle;
341 }
342 #endif  /*  _MULTIPLE_DB_HANDLE */
343
344 /* ------------------------------------------------------------------------------ */
345 /*  Mutex using shared memory */
346 typedef struct {
347         pthread_mutex_t mutex;
348         int data;
349 } mmapped_t;
350
351 mmapped_t       *mapped_for_db_lock = NULL;
352 int              shm_fd_for_db_lock = 0;
353
354 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
355 mmapped_t       *mapped_for_generating_mail_id = NULL;
356 int              shm_fd_for_generating_mail_id = 0;
357 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
358
359 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__
360 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
361         do {\
362                 _timedlock_shm_mutex(mapped_for_db_lock, 2);\
363                 return_value = function_call;\
364                 _unlockshm_mutex(mapped_for_db_lock);\
365         } while (0)
366
367 #else /*  __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
368 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
369         {  return_value = function_call; }
370 #endif /*  __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
371
372 static int emstorage_exec_query_by_prepare_v2(sqlite3 *local_db_handle, char *query_string)
373 {
374         EM_DEBUG_FUNC_BEGIN("local_db_handle[%p] query_string[%p]", local_db_handle, query_string);
375         int error = EMAIL_ERROR_NONE;
376         int rc = 0;
377         DB_STMT db_statement = NULL;
378
379         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, query_string, EM_SAFE_STRLEN(query_string), &db_statement, NULL), rc);
380         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
381                 ("sqlite3_prepare failed [%d] [%s]", rc, query_string));
382
383         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(db_statement), rc);
384         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
385         ("sqlite3_step failed [%d] [%s]", rc, query_string));
386
387 FINISH_OFF:
388
389         if (db_statement != NULL) {
390                 rc = sqlite3_finalize(db_statement);
391                 if (rc != SQLITE_OK) {
392                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
393                         error = EMAIL_ERROR_DB_FAILURE;
394                 }
395         }
396
397         return error;
398 }
399
400 INTERNAL_FUNC int emstorage_shm_file_init(const char *shm_file_name)
401 {
402         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
403         char errno_buf[ERRNO_BUF_SIZE] = {0};
404
405         if (!shm_file_name) {
406                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
407                 return EMAIL_ERROR_INVALID_PARAM;
408         }
409
410         int fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); /*  note: permission is not working */
411         if (fd < 0) {
412                 EM_DEBUG_EXCEPTION("shm_open errno [%d] [%s]", errno, EM_STRERROR(errno_buf));
413                 return EMAIL_ERROR_SYSTEM_FAILURE;
414         }
415
416         fchmod(fd, 0666);
417         EM_DEBUG_LOG("** Create SHM FILE **");
418         if (ftruncate(fd, sizeof(mmapped_t)) != 0) {
419                 EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
420                 return EMAIL_ERROR_SYSTEM_FAILURE;
421         }
422
423         mmapped_t *m = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
424         if (m == MAP_FAILED) {
425                 EM_DEBUG_EXCEPTION("mmap errno [%d]", errno);
426                 EM_SAFE_CLOSE(fd);
427                 return EMAIL_ERROR_SYSTEM_FAILURE;
428         }
429
430         m->data = 0;
431
432         pthread_mutexattr_t mattr;
433         pthread_mutexattr_init(&mattr);
434         pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
435         pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
436         pthread_mutex_init(&(m->mutex), &mattr);
437         pthread_mutexattr_destroy(&mattr);
438
439     pthread_mutex_destroy(&(m->mutex));
440     munmap(m, sizeof(mmapped_t));
441
442         EM_SAFE_CLOSE(fd);
443         EM_DEBUG_FUNC_END();
444         return EMAIL_ERROR_NONE;
445 }
446
447 int emstorage_shm_file_destroy(const char *shm_file_name)
448 {
449         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
450         char errno_buf[ERRNO_BUF_SIZE] = {0};
451
452         if (!shm_file_name) {
453                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
454                 return EMAIL_ERROR_INVALID_PARAM;
455         }
456
457         if (shm_unlink(shm_file_name) != 0)
458                 EM_DEBUG_EXCEPTION("shm_unlink failed: %s", EM_STRERROR(errno_buf));
459         EM_DEBUG_FUNC_END();
460         return EMAIL_ERROR_NONE;
461 }
462
463 int _initialize_shm_mutex(const char *shm_file_name, int *param_shm_fd, mmapped_t **param_mapped)
464 {
465         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p] param_shm_fd [%p], param_mapped [%p]", shm_file_name, param_shm_fd, param_mapped);
466         char errno_buf[ERRNO_BUF_SIZE] = {0};
467
468         if (!shm_file_name || !param_shm_fd || !param_mapped) {
469                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
470                 return EMAIL_ERROR_INVALID_PARAM;
471         }
472
473         if (!(*param_mapped)) {
474                 EM_DEBUG_LOG("** mapping begin **");
475                 if (!(*param_shm_fd)) { /*  open shm_file_name at first. Otherwise, the num of files in /proc/pid/fd will be increasing  */
476                         *param_shm_fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
477                         if ((*param_shm_fd) == -1) {
478                                 EM_DEBUG_EXCEPTION("FAIL: shm_open(): %s", EM_STRERROR(errno_buf));
479                                 return EMAIL_ERROR_SYSTEM_FAILURE;
480                         }
481                 }
482
483         fchmod((*param_shm_fd), 0666);
484         EM_DEBUG_LOG("** Create SHM FILE **");
485         if (ftruncate((*param_shm_fd), sizeof(mmapped_t)) != 0) {
486             EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
487             return EMAIL_ERROR_SYSTEM_FAILURE;
488         }
489
490                 mmapped_t *tmp = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ|PROT_WRITE, MAP_SHARED, (*param_shm_fd), 0);
491                 if (tmp == MAP_FAILED) {
492                         EM_DEBUG_EXCEPTION("mmap failed: %s", EM_STRERROR(errno_buf));
493                         return EMAIL_ERROR_SYSTEM_FAILURE;
494                 }
495
496         tmp->data = 0;
497
498         pthread_mutexattr_t mattr;
499         pthread_mutexattr_init(&mattr);
500         pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
501         pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
502         pthread_mutex_init(&(tmp->mutex), &mattr);
503         pthread_mutexattr_destroy(&mattr);
504
505                 *param_mapped = tmp;
506         }
507
508         EM_DEBUG_FUNC_END();
509         return EMAIL_ERROR_NONE;
510 }
511
512 int _timedlock_shm_mutex(mmapped_t *param_mapped, int sec)
513 {
514         EM_DEBUG_FUNC_BEGIN("param_mapped [%p], sec [%d]", param_mapped, sec);
515
516         if (!param_mapped) {
517                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
518                 return EMAIL_ERROR_INVALID_PARAM;
519         }
520
521         struct timespec abs_time;
522         clock_gettime(CLOCK_REALTIME, &abs_time);
523         abs_time.tv_sec += sec;
524         char errno_buf[ERRNO_BUF_SIZE] = {0};
525
526         int err = pthread_mutex_timedlock(&(param_mapped->mutex), &abs_time);
527
528         if (err == EOWNERDEAD) {
529                 err = pthread_mutex_consistent(&(param_mapped->mutex));
530                 EM_DEBUG_EXCEPTION("Previous owner is dead with lock. Fix mutex : %s", EM_STRERROR(errno_buf));
531         } else if (err != 0) {
532                 EM_DEBUG_EXCEPTION("ERROR : %s", EM_STRERROR(errno_buf));
533                 return err;
534         }
535
536         EM_DEBUG_FUNC_END();
537         return EMAIL_ERROR_NONE;
538 }
539
540 void _unlockshm_mutex(mmapped_t *param_mapped)
541 {
542         EM_DEBUG_FUNC_BEGIN();
543         pthread_mutex_unlock(&(param_mapped->mutex));
544         EM_DEBUG_FUNC_END();
545 }
546 /* ------------------------------------------------------------------------------ */
547
548
549 static int _open_counter = 0;
550
551 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name);
552 static int _read_password_from_secure_storage(char *file_name, char **password);
553
554 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
555 static int _get_cert_password_file_name(int index, char *cert_password_file_name);
556 #endif
557
558 typedef struct {
559         const char *object_name;
560         unsigned int data_flag;
561 } email_db_object_t;
562
563 static const email_db_object_t _g_db_tables[] = {
564         { "mail_read_mail_uid_tbl", 1},
565         { "mail_tbl", 1},
566         { "mail_attachment_tbl", 1},
567         { NULL,  0},
568 };
569
570 static const email_db_object_t _g_db_indexes[] = {
571         { "mail_read_mail_uid_idx1", 1},
572         { "mail_idx1", 1},
573         { "mail_attachment_idx1", 1},
574         { NULL,  1},
575 };
576
577 enum {
578         CREATE_TABLE_MAIL_ACCOUNT_TBL,
579         CREATE_TABLE_MAIL_BOX_TBL,
580         CREATE_TABLE_MAIL_READ_MAIL_UID_TBL,
581         CREATE_TABLE_MAIL_RULE_TBL,
582         CREATE_TABLE_MAIL_TBL,
583         CREATE_TABLE_MAIL_ATTACHMENT_TBL,
584 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
585         CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL,
586 #else
587         CREATE_TABLE_DUMMY_INDEX1,
588 #endif
589         CREATE_TABLE_MAIL_MEETING_TBL,
590 #ifdef __FEATURE_LOCAL_ACTIVITY__
591         CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL,
592 #else
593         CREATE_TABLE_DUMMY_INDEX2,
594 #endif
595         CREATE_TABLE_MAIL_CERTIFICATE_TBL,
596         CREATE_TABLE_MAIL_TASK_TBL,
597 #ifdef __FEATURE_BODY_SEARCH__
598         CREATE_TABLE_MAIL_TEXT_TBL,
599 #else
600         CREATE_TABLE_DUMMY_INDEX3,
601 #endif
602
603 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
604         CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
605 #else
606         CREATE_TABLE_DUMMY_INDEX4,
607 #endif
608
609         /*CREATE INDEX*/
610         CREATE_TABLE_MAIL_ACCOUNT_IDX,
611         CREATE_TABLE_MAIL_BOX_IDX,
612         CREATE_TABLE_MAIL_READ_MAIL_UID_IDX,
613         CREATE_TABLE_MAIL_IDX,
614         CREATE_TABLE_MAIL_ATTACHMENT_IDX,
615         CREATE_TABLE_MAIL_MEETING_IDX,
616         CREATE_TABLE_MAIL_TASK_IDX,
617         CREATE_TABLE_MAIL_DATETIME_IDX,
618         CREATE_TABLE_MAIL_THREAD_IDX,
619         CREATE_TABLE_MAX,
620 };
621
622 enum {
623         DATA1_IDX_IN_MAIL_ACTIVITY_TBL = 0,
624         TRANSTYPE_IDX_IN_MAIL_ACTIVITY_TBL,
625         FLAG_IDX_IN_MAIL_ACTIVITY_TBL,
626 };
627
628 enum {
629         CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL = 0,
630         ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
631         ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
632         ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
633         EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
634         EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
635         EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
636         ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL,
637         EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL,
638         SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL,
639         FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL,
640 };
641
642 enum {
643         TO_RECIPIENT = 0,
644         CC_RECIPIENT,
645         BCC_RECIPIENT,
646 };
647 enum {
648         ACCOUNT_ID_IDX_IN_MAIL_BOX_TBL = 0,
649         LOCAL_YN_IDX_IN_MAIL_BOX_TBL,
650         MAILBOX_NAME_IDX_IN_MAIL_BOX_TBL,
651         MAILBOX_TYPE_IDX_IN_MAIL_BOX_TBL,
652         ALIAS_IDX_IN_MAIL_BOX_TBL,
653         SYNC_WITH_SERVER_YN_IDX_IN_MAIL_BOX_TBL,
654         MODIFIABLE_YN_IDX_IN_MAIL_BOX_TBL,
655         TOTAL_MAIL_COUNT_ON_SERVER_IDX_IN_MAIL_BOX_TBL,
656         ARCHIVE_IDX_IN_MAIL_BOX_TBL,
657         MAIL_SLOT_SIZE_IDX_IN_MAIL_BOX_TBL,
658 };
659
660 enum {
661         ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL = 0,
662         LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
663         MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL,
664         LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
665         SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
666         RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL,
667         SYNC_STATUS_IDX_IN_MAIL_READ_MAIL_UID_TBL,
668         FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL,
669         FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL,
670         IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL,          /* unused */
671 };
672
673 #ifdef __FEATURE_BODY_SEARCH__
674 enum {
675         MAIL_ID_IDX_IN_MAIL_TEXT_TBL = 0,
676         ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL,
677         MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL,
678         BODY_TEXT_IDX_IN_MAIL_TEXT_TBL,
679         FIELD_COUNT_OF_MAIL_TEXT_TBL,
680 };
681 #endif
682
683 enum {
684         ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL = 0,
685         RULE_ID_IDX_IN_MAIL_RULE_TBL,
686         FILTER_NAME_IDX_IN_MAIL_RULE_TBL,
687         TYPE_IDX_IN_MAIL_RULE_TBL,
688         VALUE_IDX_IN_MAIL_RULE_TBL,
689         VALUE2_IDX_IN_MAIL_RULE_TBL,
690         ACTION_TYPE_IDX_IN_MAIL_RULE_TBL,
691         TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL,
692         FLAG1_IDX_IN_MAIL_RULE_TBL,
693         FLAG2_IDX_IN_MAIL_RULE_TBL,
694 };
695
696 enum {
697         MAIL_ID_IDX_IN_MAIL_TBL = 0,
698         ACCOUNT_ID_IDX_IN_MAIL_TBL,
699         MAILBOX_ID_IDX_IN_MAIL_TBL,
700         MAILBOX_TYPE_IDX_IN_MAIL_TBL,
701         SUBJECT_IDX_IN_MAIL_TBL,
702         DATETIME_IDX_IN_MAIL_TBL,
703         SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL,
704         SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL,
705         SERVER_MAIL_ID_IDX_IN_MAIL_TBL,
706         MESSAGE_ID_IDX_IN_MAIL_TBL,
707         REFERENCE_ID_IDX_IN_MAIL_TBL,
708         FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL,
709         FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL,
710         FULL_ADDRESS_TO_IDX_IN_MAIL_TBL,
711         FULL_ADDRESS_CC_IDX_IN_MAIL_TBL,
712         FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL,
713         FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL,
714         EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL,
715         EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL,
716         ALIAS_SENDER_IDX_IN_MAIL_TBL,
717         ALIAS_RECIPIENT_IDX_IN_MAIL_TBL,
718         BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL,
719         FILE_PATH_PLAIN_IDX_IN_MAIL_TBL,
720         FILE_PATH_HTML_IDX_IN_MAIL_TBL,
721         FILE_PATH_MIME_ENTITY_IDX_IN_MAIL_TBL,
722         MAIL_SIZE_IDX_IN_MAIL_TBL,
723         FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL,
724         FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL,
725         FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL,
726         FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL,
727         FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL,
728         FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL,
729         FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL,
730         DRM_STATUS_IDX_IN_MAIL_TBL,
731         PRIORITY_IDX_IN_MAIL_TBL,
732         SAVE_STATUS_IDX_IN_MAIL_TBL,
733         LOCK_STATUS_IDX_IN_MAIL_TBL,
734         REPORT_STATUS_IDX_IN_MAIL_TBL,
735         ATTACHMENT_COUNT_IDX_IN_MAIL_TBL,
736         INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL,
737         THREAD_ID_IDX_IN_MAIL_TBL,
738         THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL,
739         PREVIEW_TEXT_IDX_IN_MAIL_TBL,
740         MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL,
741         MESSAGE_CLASS_IDX_IN_MAIL_TBL,
742         DIGEST_TYPE_IDX_IN_MAIL_TBL,
743         SMIME_TYPE_IDX_IN_MAIL_TBL,
744         SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL,
745         REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL,
746         TAG_ID_IDX_IN_MAIL_TBL,
747         REPLIED_TIME_IDX_IN_MAIL_TBL,
748         FORWARDED_TIME_IDX_IN_MAIL_TBL,
749         DEFAULT_CHARSET_IDX_IN_MAIL_TBL,
750         EAS_DATA_LENGTH_IDX_IN_MAIL_TBL,
751         EAS_DATA_IDX_IN_MAIL_TBL,
752         USER_NAME_IDX_IN_MAIL_TBL,
753         FIELD_COUNT_OF_MAIL_TBL,  /* End of mail_tbl */
754 };
755
756 enum {
757         ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL = 0,
758         ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL,
759         ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL,
760         CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
761         ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL,
762         MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
763         ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
764         MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
765         ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
766         ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
767         ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL,
768         ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
769         ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
770 #ifdef __ATTACHMENT_OPTI__
771         ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL,
772         SECTION_IDX_IN_MAIL_ATTACHMENT_TBL,
773 #endif
774 };
775
776 enum {
777         IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL = 0,
778 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
779         MAIL_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
780         ACCOUNT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
781         ADDRESS_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
782         ADDRESS_IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL,
783 #endif
784         ADDRESS_IDX_IN_MAIL_CONTACT_SYNC_TBL,
785         CONTACT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
786         STORAGE_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
787         CONTACT_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
788 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
789         DISPLAY_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
790         FLAG1_IDX_IN_MAIL_CONTACT_SYNC_TBL,
791 #endif
792 };
793
794 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
795 enum {
796         ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL = 0,
797         STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
798         ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
799         MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
800         SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
801         MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
802     MULTI_USER_NAME_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
803 };
804 #endif
805
806 /* sowmya.kr 03032010, changes for get list of mails for given addr list */
807 typedef struct _em_mail_id_list {
808         int mail_id;
809         struct _em_mail_id_list *next;
810 } em_mail_id_list;
811
812 static char *g_test_query[] = {
813                 /*  1. select mail_account_tbl */
814                 "SELECT"
815                 " account_name, "
816                 " incoming_server_type, "
817                 " incoming_server_address, "
818                 " user_email_address, "
819                 " incoming_server_user_name, "
820                 " incoming_server_password, "
821                 " retrieval_mode, "
822                 " incoming_server_port_number, "
823                 " incoming_server_secure_connection, "
824                 " incoming_server_authentication_method,"
825                 " outgoing_server_type, "
826                 " outgoing_server_address, "
827                 " outgoing_server_port_number, "
828                 " outgoing_server_need_authentication, "
829                 " outgoing_server_secure_connection, "
830                 " outgoing_server_user_name, "
831                 " outgoing_server_password, "
832                 " display_name, "
833                 " reply_to_addr, "
834                 " return_addr, "
835                 " account_id, "
836                 " keep_mails_on_pop_server_after_download, "
837                 " auto_resend_times, "
838                 " outgoing_server_size_limit, "
839                 " wifi_auto_download, "
840                 " pop_before_smtp, "
841                 " incoming_server_requires_apop,"
842                 " logo_icon_path, "
843                 " is_preset_account, "
844                 " check_interval, "
845                 " priority, "
846                 " keep_local_copy, "
847                 " req_delivery_receipt, "
848                 " req_read_receipt, "
849                 " download_limit, "
850                 " block_address, "
851                 " block_subject, "
852                 " display_name_from, "
853                 " reply_with_body, "
854                 " forward_with_files, "
855                 " add_myname_card, "
856                 " add_signature, "
857                 " signature"
858                 ", add_my_address_to_bcc"
859                 ", notification_status "
860                 ", vibrate_status "
861                 ", display_content_status "
862                 ", default_ringtone_status "
863                 ", alert_ringtone_path "
864                 ", account_svc_id "
865                 ", index_color "
866                 ", sync_status "
867                 ", sync_disabled "
868                 ", smime_type"
869                 ", certificate_path"
870                 ", cipher_type"
871                 ", digest_type"
872                 " FROM mail_account_tbl",
873                 /*  2. select mail_box_tbl */
874                 "SELECT "
875                 "   mailbox_id, "
876                 "   account_id, "
877                 "   local_yn,  "
878                 "   mailbox_name,  "
879                 "   mailbox_type,   "
880                 "   alias,  "
881                 "   deleted_flag,  "
882                 "   modifiable_yn,  "
883                 "   total_mail_count_on_server,  "
884                 "   has_archived_mails, "
885                 "   mail_slot_size, "
886                 "   no_select, "
887                 "   last_sync_time "
888                 " FROM mail_box_tbl ",
889                 /*  3. select mail_read_mail_uid_tbl */
890                 "SELECT  "
891                 "   account_id,  "
892                 "   mailbox_id,  "
893                 "   mailbox_name,  "
894                 "   local_uid,  "
895                 "   server_uid,  "
896                 "   rfc822_size ,  "
897                 "   sync_status,  "
898                 "   flags_seen_field,  "
899                 "   idx_num "
900                 " FROM mail_read_mail_uid_tbl ",
901                 /*  4. select mail_rule_tbl */
902                 "SELECT "
903                 "   account_id, "
904                 "   rule_id, "
905                 "   filter_name, "
906                 "   type, "
907                 "   value, "
908                 "   value2, "
909                 "   action_type, "
910                 "   target_mailbox_id,  "
911                 "   flag1, "
912                 "   flag2 "
913                 " FROM mail_rule_tbl    ",
914                 /*  5. select mail_tbl */
915                 "SELECT"
916                 "       mail_id, "
917                 "       account_id, "
918                 "       mailbox_id, "
919                 "       mailbox_name, "
920                 "   mailbox_type, "
921                 "   subject, "
922                 "       date_time, "
923                 "       server_mail_status, "
924                 "       server_mailbox_name, "
925                 "       server_mail_id, "
926                 "   message_id, "
927                 "       reference_mail_id, "
928                 "   full_address_from, "
929                 "   full_address_reply, "
930                 "   full_address_to, "
931                 "   full_address_cc, "
932                 "   full_address_bcc, "
933                 "   full_address_return, "
934                 "   email_address_sender, "
935                 "   email_address_recipient, "
936                 "   alias_sender, "
937                 "   alias_recipient, "
938                 "       body_download_status, "
939                 "       file_path_plain, "
940                 "       file_path_html, "
941                 "   file_path_mime_entity, "
942                 "       mail_size, "
943                 "   flags_seen_field     ,"
944                 "   flags_deleted_field  ,"
945                 "   flags_flagged_field  ,"
946                 "   flags_answered_field ,"
947                 "   flags_recent_field   ,"
948                 "   flags_draft_field    ,"
949                 "   flags_forwarded_field,"
950                 "       DRM_status, "
951                 "       priority, "
952                 "       save_status, "
953                 "       lock_status, "
954                 "       report_status, "
955                 "   attachment_count, "
956                 "       inline_content_count, "
957                 "       thread_id, "
958                 "       thread_item_count, "
959                 "   preview_text, "
960                 "       meeting_request_status, "
961                 "   message_class, "
962                 "   digest_type, "
963                 "   smime_type "
964                 " FROM mail_tbl",
965                 /*  6. select mail_attachment_tbl */
966                 "SELECT "
967                 "       attachment_id, "
968                 "       attachment_name, "
969                 "       attachment_path, "
970                 "       attachment_size, "
971                 "       mail_id,  "
972                 "       account_id, "
973                 "       mailbox_id, "
974                 "       attachment_save_status,  "
975                 "       attachment_drm_type,  "
976                 "       attachment_drm_method,  "
977                 "       attachment_inline_content_status,  "
978                 "       attachment_mime_type  "
979                 " FROM mail_attachment_tbl ",
980
981 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
982                 "SELECT  "
983                 "   account_id, "
984                 "   mail_id, "
985                 "   server_mail_id, "
986                 "   activity_id, "
987                 "   activity_type, "
988                 "   mailbox_id, "
989                 "   mailbox_name "
990                 "   multi_user_name "
991                 " FROM mail_partial_body_activity_tbl ",
992 #endif
993
994                 "SELECT  "
995                 "   mail_id, "
996                 "   account_id, "
997                 "   mailbox_id, "
998                 "   meeting_response, "
999                 "   start_time, "
1000                 "   end_time, "
1001                 "   location, "
1002                 "   global_object_id, "
1003                 "   offset, "
1004                 "   standard_name, "
1005                 "   standard_time_start_date, "
1006                 "   standard_bias, "
1007                 "   daylight_name, "
1008                 "   daylight_time_start_date, "
1009                 "   daylight_bias "
1010                 " FROM mail_meeting_tbl ",
1011
1012 #ifdef __FEATURE_LOCAL_ACTIVITY__
1013                 "SELECT "
1014                 "       activity_id, "
1015                 "   account_id, "
1016                 "   mail_id, "
1017                 "   activity_type, "
1018                 "   server_mailid, "
1019                 "   src_mbox , "
1020                 "       dest_mbox "
1021                 " FROM mail_local_activity_tbl  ",
1022 #endif
1023                 "SELECT "
1024                 "       certificate_id, "
1025                 "   issue_year, "
1026                 "   issue_month, "
1027                 "   issue_day, "
1028                 "   expiration_year, "
1029                 "   expiration_month, "
1030                 "   expiration_day, "
1031                 "   issue_organization_name, "
1032                 "   email_address, "
1033                 "   subject_str, "
1034                 "   filepath, "
1035                 "   password "
1036                 " FROM mail_certificate_tbl     ",
1037                 "SELECT "
1038                 "       task_id, "
1039                 "   task_type, "
1040                 "   task_status, "
1041                 "   task_priority, "
1042                 "   task_parameter_length, "
1043                 "   task_parameter , "
1044                 "       date_time "
1045                 " FROM mail_task_tbl    ",
1046 #ifdef __FEATURE_BODY_SEARCH__
1047                 "SELECT "
1048                 "       mail_id, "
1049                 "   account_id, "
1050                 "   mailbox_id, "
1051                 "   body_text "
1052                 " FROM mail_text_tbl    ",
1053 #endif
1054 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
1055                 "SELECT  "
1056                 "   activity_id, "
1057                 "   status, "
1058                 "   account_id, "
1059                 "   mail_id, "
1060                 "   server_mail_id, "
1061                 "   mailbox_id, "
1062                 "   multi_user_name, "
1063                 " FROM mail_auto_download_activity_tbl ",
1064 #endif
1065                 NULL,
1066 };
1067
1068 int _field_count_of_table[CREATE_TABLE_MAX] = { 0, };
1069
1070 static int _get_table_field_data_char(char  **table, char *buf, int index)
1071 {
1072         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1073                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1074                 return false;
1075         }
1076
1077         if (table[index] != NULL) {
1078                 *buf = (char)atoi(table[index]);
1079                 return true;
1080         }
1081
1082         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
1083
1084         *buf = 0;
1085         return false;
1086 }
1087
1088 static int _get_table_field_data_int(char  **table, int *buf, int index)
1089 {
1090         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1091                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1092                 return false;
1093         }
1094
1095         if (table[index] != NULL) {
1096                 *buf = atoi(table[index]);
1097                 return true;
1098         }
1099
1100         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
1101
1102         *buf = 0;
1103         return false;
1104 }
1105
1106 static int _get_table_field_data_time_t(char  **table, time_t *buf, int index)
1107 {
1108         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1109                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1110                 return false;
1111         }
1112
1113         if (table[index] != NULL) {
1114                 *buf = (time_t)atol(table[index]);
1115                 return true;
1116         }
1117
1118         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
1119
1120         *buf = 0;
1121         return false;
1122 }
1123
1124 static int _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
1125 {
1126         int ret = false;
1127
1128         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1129                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1130                 return false;
1131         }
1132
1133         char *pTemp = table[index];
1134         int sLen = 0;
1135         if (pTemp == NULL)
1136                 *buf = NULL;
1137         else {
1138                 sLen = EM_SAFE_STRLEN(pTemp);
1139                 if (sLen) {
1140                         *buf = (char *) em_malloc(sLen + 1);
1141                         if (*buf == NULL) {
1142                                 EM_DEBUG_EXCEPTION("malloc is failed");
1143                                 goto FINISH_OFF;
1144                         }
1145                         strncpy(*buf, pTemp, sLen);
1146                 } else
1147                         *buf = NULL;
1148         }
1149 #ifdef _PRINT_STORAGE_LOG_
1150         if (*buf)
1151                 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", *buf, index);
1152         else
1153                 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1154 #endif
1155         ret = true;
1156 FINISH_OFF:
1157
1158         return ret;
1159 }
1160
1161 static int _get_table_field_data_string_without_allocation(char **table, char *buf, int buffer_size, int ucs2, int index)
1162 {
1163         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1164                 EM_DEBUG_EXCEPTION(" table[%p], buf[%p], index[%d]", table, buf, index);
1165                         return false;
1166         }
1167
1168         char *pTemp = table[index];
1169
1170         if (pTemp == NULL)
1171                 buf = NULL;
1172         else {
1173                 memset(buf, 0, buffer_size);
1174                 strncpy(buf, pTemp, buffer_size - 1);
1175         }
1176 #ifdef _PRINT_STORAGE_LOG_
1177         if (buf)
1178                 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", buf, index);
1179         else
1180                 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1181 #endif
1182
1183         return true;
1184 }
1185
1186 static int _get_table_field_data_blob(char **table, void **buffer, int buffer_size, int index)
1187 {
1188         if ((table == NULL) || (buffer == NULL) || (index < 0)) {
1189                 EM_DEBUG_EXCEPTION(" table[%p], buffer[%p], buffer_size [%d], index[%d]", table, buffer, buffer_size, index);
1190                 return false;
1191         }
1192
1193         char *temp_buffer = table[index];
1194
1195         if (temp_buffer == NULL)
1196                 buffer = NULL;
1197         else {
1198                 *buffer = malloc(buffer_size);
1199                 if (*buffer == NULL) {
1200                         EM_DEBUG_EXCEPTION("allocation failed.");
1201                         return false;
1202                 }
1203                 memset(*buffer, 0, buffer_size);
1204                 memcpy(*buffer, temp_buffer, buffer_size);
1205         }
1206 #ifdef _PRINT_STORAGE_LOG_
1207         if (buf)
1208                 EM_DEBUG_LOG("_get_table_field_data_string - buffer[%s], index[%d]", buffer, index);
1209         else
1210                 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1211 #endif
1212
1213         return true;
1214 }
1215
1216 static int _get_stmt_field_data_char(DB_STMT hStmt, char *buf, int index)
1217 {
1218         if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1219                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1220                 return false;
1221         }
1222
1223         if (sqlite3_column_text(hStmt, index) != NULL) {
1224                 *buf = (char)sqlite3_column_int(hStmt, index);
1225 #ifdef _PRINT_STORAGE_LOG_
1226                         EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1227 #endif
1228                 return true;
1229         }
1230
1231         EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1232
1233         return false;
1234 }
1235
1236 static int _get_stmt_field_data_int(DB_STMT hStmt, int *buf, int index)
1237 {
1238         if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1239                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1240                 return false;
1241         }
1242
1243         if (sqlite3_column_text(hStmt, index) != NULL) {
1244                 *buf = sqlite3_column_int(hStmt, index);
1245 #ifdef _PRINT_STORAGE_LOG_
1246                         EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1247 #endif
1248                 return true;
1249         }
1250
1251         EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1252
1253         return false;
1254 }
1255
1256 static int _get_stmt_field_data_time_t(DB_STMT hStmt, time_t *buf, int index)
1257 {
1258         if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1259                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1260                 return false;
1261         }
1262
1263         if (sqlite3_column_text(hStmt, index) != NULL) {
1264                 *buf = (time_t)sqlite3_column_int(hStmt, index);
1265 #ifdef _PRINT_STORAGE_LOG_
1266                 EM_DEBUG_LOG("_get_stmt_field_data_time_t [%d]", *buf);
1267 #endif
1268                 return true;
1269         }
1270
1271         EM_DEBUG_LOG("_get_stmt_field_data_time_t fail. index [%d]", index);
1272         return false;
1273 }
1274
1275 static int _get_stmt_field_data_string(DB_STMT hStmt, char **buf, int ucs2, int index)
1276 {
1277         if (!hStmt || !buf || (index < 0)) { /*prevent 39619*/
1278                 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1279                 return false;
1280         }
1281
1282         int sLen = 0;
1283         sLen = sqlite3_column_bytes(hStmt, index);
1284
1285 #ifdef _PRINT_STORAGE_LOG_
1286                 EM_DEBUG_LOG("_get_stmt_field_data_string sqlite3_column_bytes sLen[%d]", sLen);
1287 #endif
1288
1289         if (sLen > 0) {
1290                 *buf = (char *) em_malloc(sLen + 1);
1291                 if (*buf == NULL) {
1292                         EM_DEBUG_EXCEPTION("em_mallocfailed");
1293                         return false;
1294                 }
1295
1296                 strncpy(*buf, (char *)sqlite3_column_text(hStmt, index), sLen);
1297         } else
1298                 *buf = NULL;
1299
1300 #ifdef _PRINT_STORAGE_LOG_
1301         if (*buf)
1302                 EM_DEBUG_LOG("buf[%s], index[%d]", *buf, index);
1303         else
1304                 EM_DEBUG_LOG("_get_stmt_field_data_string - No string got");
1305 #endif
1306
1307         return false;
1308 }
1309
1310 static void _get_stmt_field_data_blob(DB_STMT hStmt, void **buf, int index)
1311 {
1312         if (!hStmt || !buf || (index < 0)) { /*prevent 39618*/
1313                 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1314                 return;
1315         }
1316
1317         int sLen = 0;
1318         sLen = sqlite3_column_bytes(hStmt, index);
1319
1320 #ifdef _PRINT_STORAGE_LOG_
1321         EM_DEBUG_LOG("_get_stmt_field_data_blob sqlite3_column_bytes sLen[%d]", sLen);
1322 #endif
1323
1324         if (sLen > 0) {
1325                 *buf = (char *) em_malloc(sLen);
1326                 if (*buf == NULL) {
1327                         EM_DEBUG_EXCEPTION("em_mallocfailed");
1328                         return;
1329                 }
1330
1331                 memcpy(*buf, (void *)sqlite3_column_blob(hStmt, index), sLen);
1332         } else
1333                 *buf = NULL;
1334
1335 }
1336
1337 static int _bind_stmt_field_data_char(DB_STMT hStmt, int index, char value)
1338 {
1339         if ((hStmt == NULL) || (index < 0)) {
1340                 EM_DEBUG_EXCEPTION("index[%d]", index);
1341                 return false;
1342         }
1343
1344         int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1345
1346         if (ret != SQLITE_OK) {
1347                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1348                 return false;
1349         }
1350
1351         return true;
1352 }
1353
1354 static int _bind_stmt_field_data_int(DB_STMT hStmt, int index, int value)
1355 {
1356         if ((hStmt == NULL) || (index < 0)) {
1357                 EM_DEBUG_EXCEPTION("index[%d]", index);
1358                 return false;
1359         }
1360
1361         int ret = sqlite3_bind_int(hStmt, index+1, value);
1362
1363         if (ret != SQLITE_OK) {
1364                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1365                 return false;
1366         }
1367
1368         return true;
1369 }
1370
1371 static int _bind_stmt_field_data_time_t(DB_STMT hStmt, int index, time_t value)
1372 {
1373         if ((hStmt == NULL) || (index < 0)) {
1374                 EM_DEBUG_EXCEPTION("index[%d]", index);
1375                 return false;
1376         }
1377
1378         int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1379
1380         if (ret != SQLITE_OK) {
1381                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1382                 return false;
1383         }
1384
1385         return true;
1386 }
1387
1388 static int _bind_stmt_field_data_string(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1389 {
1390         if ((hStmt == NULL) || (index < 0)) {
1391                 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1392                 return false;
1393         }
1394
1395 #ifdef _PRINT_STORAGE_LOG_
1396         EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1397 #endif
1398
1399         int ret = 0;
1400         if (value != NULL)
1401                 ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1402         else
1403                 ret = sqlite3_bind_text(hStmt, index+1, "", -1, NULL);
1404
1405         if (ret != SQLITE_OK) {
1406                 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1407                 return false;
1408         }
1409         return true;
1410 }
1411
1412 /* destroy function for sqlite3_bind_text */
1413 void _bind_stmt_free_string(void* arg)
1414 {
1415         EM_DEBUG_FUNC_BEGIN();
1416         char* p = (char*) arg;
1417         EM_SAFE_FREE(p);
1418         EM_DEBUG_FUNC_END();
1419 }
1420
1421 static int _bind_stmt_field_data_nstring(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1422 {
1423         if ((hStmt == NULL) || (index < 0)) {
1424                 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1425                 return false;
1426         }
1427
1428 #ifdef _PRINT_STORAGE_LOG_
1429         EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1430 #endif
1431
1432         int ret = 0;
1433         if (value != NULL) {
1434                 if (strlen(value) <= max_len)
1435                         ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1436                 else {
1437                         char *buf = (char*)em_malloc(sizeof(char) * (max_len));
1438                         if (buf == NULL) {
1439                                 EM_DEBUG_EXCEPTION("em_mallocfailed");
1440                                 return false;
1441                         }
1442                         snprintf(buf, max_len-1, "%s", value);
1443                         ret = sqlite3_bind_text(hStmt, index+1, buf, -1, _bind_stmt_free_string);
1444                 }
1445         } else
1446                 ret = sqlite3_bind_text(hStmt, index+1, "", -1, NULL);
1447
1448         if (ret != SQLITE_OK) {
1449                 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1450                 return false;
1451         }
1452         return true;
1453 }
1454
1455
1456 static int _bind_stmt_field_data_blob(DB_STMT hStmt, int index, void *blob, int blob_size)
1457 {
1458         if ((hStmt == NULL) || (index < 0)) {
1459                 EM_DEBUG_EXCEPTION("index[%d], blob_size[%d]", index, blob_size);
1460                 return false;
1461         }
1462
1463 #ifdef _PRINT_STORAGE_LOG_
1464         EM_DEBUG_LOG("hStmt = %p, index = %d, blob_size = %d, blob = [%p]", hStmt, index, blob_size, blob);
1465 #endif
1466
1467         int ret = 0;
1468         if (blob_size > 0)
1469                 ret = sqlite3_bind_blob(hStmt, index+1, blob, blob_size, SQLITE_STATIC);
1470         else
1471                 ret = sqlite3_bind_null(hStmt, index+1);
1472
1473         if (ret != SQLITE_OK) {
1474                 EM_DEBUG_EXCEPTION("sqlite3_bind_blob fail [%d]", ret);
1475                 return false;
1476         }
1477         return true;
1478 }
1479
1480
1481 static int _delete_temp_file(const char *path)
1482 {
1483         EM_DEBUG_FUNC_BEGIN("path[%p]", path);
1484
1485         DIR *dp = NULL;
1486         struct dirent *entry = NULL;
1487
1488         char buf[1024] = {0x00, };
1489
1490         if ((dp = opendir(path)) == NULL) {
1491                 EM_DEBUG_EXCEPTION("opendir(\"%s\") failed...", path);
1492                 return false;
1493         }
1494
1495         while ((entry = readdir(dp)) != NULL) {
1496                 SNPRINTF(buf, sizeof(buf), "%s/%s", path, entry->d_name);
1497                 remove(buf);
1498         }
1499
1500         closedir(dp);
1501         EM_DEBUG_FUNC_END();
1502         return true;
1503 }
1504
1505 char *cpy_str(char *src)
1506 {
1507         char *p = NULL;
1508
1509         if (src) {
1510                 if (!(p = em_malloc((int)EM_SAFE_STRLEN(src) + 1))) {
1511                         EM_DEBUG_EXCEPTION("mailoc failed...");
1512                         return NULL;
1513                 }
1514                 strncpy(p, src, EM_SAFE_STRLEN(src));
1515         }
1516
1517         return p;
1518 }
1519
1520 static void _emstorage_close_once(void)
1521 {
1522         EM_DEBUG_FUNC_BEGIN();
1523
1524         EM_DEBUG_FUNC_END();
1525 }
1526
1527 INTERNAL_FUNC int emstorage_close(int *err_code)
1528 {
1529         EM_DEBUG_FUNC_BEGIN();
1530
1531         int ret = false;
1532         int error = EMAIL_ERROR_NONE;
1533
1534         if (!emstorage_db_close(NULL, &error))
1535
1536         if (--_open_counter == 0)
1537                 _emstorage_close_once();
1538
1539         ret = true;
1540
1541         if (err_code != NULL)
1542                 *err_code = error;
1543
1544         EM_DEBUG_FUNC_END("ret [%d]", ret);
1545         return ret;
1546 }
1547
1548 static void *_emstorage_open_once(char *multi_user_name, int *err_code)
1549 {
1550         EM_DEBUG_FUNC_BEGIN();
1551
1552         int error = EMAIL_ERROR_NONE;
1553
1554     if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1555         char buf[MAX_PATH] = {0};
1556                 char *prefix_path = NULL;
1557
1558                 error = emcore_get_container_path(multi_user_name, &prefix_path);
1559                 if (error != EMAIL_ERROR_NONE) {
1560                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
1561                         goto FINISH_OFF;
1562                 }
1563
1564         memset(buf, 0x00, sizeof(buf));
1565         SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, EMAIL_PATH);
1566         mkdir(buf, DIRECTORY_PERMISSION);
1567
1568         memset(buf, 0x00, sizeof(buf));
1569         SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAIL_HOME);
1570         mkdir(buf, DIRECTORY_PERMISSION);
1571
1572         memset(buf, 0x00, sizeof(buf));
1573         SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAIL_TEMP);
1574         mkdir(buf, DIRECTORY_PERMISSION);
1575
1576         _delete_temp_file(buf);
1577                 EM_SAFE_FREE(prefix_path);
1578     } else {
1579         mkdir(DATA_PATH, DIRECTORY_PERMISSION);
1580         mkdir(EMAIL_PATH, DIRECTORY_PERMISSION);
1581         mkdir(MAIL_HOME, DIRECTORY_PERMISSION);
1582         mkdir(MAIL_TEMP, DIRECTORY_PERMISSION);
1583
1584         _delete_temp_file(MAIL_TEMP);
1585     }
1586
1587         if (!emstorage_create_table(multi_user_name, EMAIL_CREATE_DB_NORMAL, &error)) {
1588                 EM_DEBUG_EXCEPTION(" emstorage_create_table failed - %d", error);
1589                 goto FINISH_OFF;
1590         }
1591
1592 FINISH_OFF:
1593
1594
1595         if (err_code != NULL)
1596                 *err_code = error;
1597
1598         return NULL;
1599 }
1600
1601  /*  pData : a parameter which is registered when busy handler is registerd */
1602  /*  count : retry count */
1603 static int _callback_sqlite_busy_handler(void *pData, int count)
1604 {
1605         if (10 - count > 0) {
1606                 struct timespec time = {
1607                         .tv_sec = 0,
1608                         .tv_nsec = (count + 1) * 100 * 1000 * 1000
1609                 };
1610                 EM_DEBUG_LOG("Busy handler called!!: PID[%d] / CNT [%d]", getpid(), count);
1611                 nanosleep(&time, NULL);
1612                 return 1;
1613         } else {
1614                 EM_DEBUG_EXCEPTION("Busy handler will be returned SQLITE_BUSY error PID[%d] / CNT[%d]", getpid(), count);
1615                 return 0;
1616         }
1617 }
1618
1619 static int _callback_collation_utf7_sort(void *data, int length_text_a, const void *text_a,
1620                                                                                                          int length_text_b, const void *text_b)
1621 {
1622     EM_DEBUG_FUNC_BEGIN();
1623     int result = 0;
1624     char *converted_string_a = NULL;
1625     char *converted_string_b = NULL;
1626
1627         EM_DEBUG_LOG_DEV("text_a : [%s]", text_a);
1628         converted_string_a = emcore_convert_mutf7_to_utf8((char *)text_a);
1629         EM_DEBUG_LOG_DEV("Converted text_a : [%s]", converted_string_a);
1630
1631         EM_DEBUG_LOG_DEV("text_b : [%s]", text_b);
1632         converted_string_b = emcore_convert_mutf7_to_utf8((char *)text_b);
1633         EM_DEBUG_LOG_DEV("Converted text_b : [%s]", converted_string_b);
1634
1635         if (converted_string_a && converted_string_b)
1636                 result = strcmp(converted_string_a, converted_string_b);
1637
1638         EM_SAFE_FREE(converted_string_a);
1639         EM_SAFE_FREE(converted_string_b);
1640
1641         EM_DEBUG_FUNC_END();
1642         return result;
1643 }
1644
1645 static int _delete_all_files_and_directories(char *db_file_path, int *err_code)
1646 {
1647         EM_DEBUG_FUNC_BEGIN();
1648
1649         int error = EMAIL_ERROR_NONE;
1650         int ret = false;
1651
1652         if (!emstorage_delete_file(db_file_path, &error)) {
1653                 if (error != EMAIL_ERROR_FILE_NOT_FOUND) {
1654                         EM_DEBUG_EXCEPTION_SEC("remove failed - %s", EMAIL_SERVICE_DB_FILE_PATH);
1655                         goto FINISH_OFF;
1656                 }
1657         }
1658
1659         if (!emstorage_delete_dir((char *)MAIL_HOME, &error)) {
1660                 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed");
1661                 goto FINISH_OFF;
1662         }
1663
1664         ret = true;
1665
1666 FINISH_OFF:
1667         if (err_code)
1668                 *err_code = error;
1669         EM_DEBUG_FUNC_END();
1670         return ret;
1671 }
1672
1673 static int _recovery_from_malformed_db_file(char *db_file_path, int *err_code)
1674 {
1675         EM_DEBUG_FUNC_BEGIN();
1676
1677         int error = EMAIL_ERROR_NONE;
1678         int ret = false;
1679
1680         /* Delete all files and directories */
1681         if (!_delete_all_files_and_directories(db_file_path, &error)) {
1682                 EM_DEBUG_EXCEPTION("_delete_all_files_and_directories failed [%d]", error);
1683                 goto FINISH_OFF;
1684         }
1685
1686         /* Delete all accounts on MyAccount */
1687
1688         /* Delete all managed connection to DB */
1689         emstorage_reset_db_handle_list();
1690
1691         ret = true;
1692
1693 FINISH_OFF:
1694         if (err_code)
1695                 *err_code = error;
1696         EM_DEBUG_FUNC_END();
1697         return ret;
1698 }
1699
1700 int _xsystem(const char *argv[])
1701 {
1702         int status = 0;
1703         pid_t pid;
1704         pid = fork();
1705
1706         switch (pid) {
1707                 case -1:
1708                         perror("fork failed");
1709                         return -1;
1710                 case 0:
1711                         if (execvp(argv[0], (char *const *)argv) == -1) {
1712                                 perror("execute init db script");
1713                                 _exit(-1);
1714                         }
1715
1716                         _exit(2);
1717                 default:
1718                         /* parent */
1719                         break;
1720         }
1721
1722         if (waitpid(pid, &status, 0) == -1) {
1723                 perror("waitpid failed");
1724                 return -1;
1725         }
1726
1727         if (WIFSIGNALED(status)) {
1728                 perror("signal");
1729                 return -1;
1730         }
1731
1732         if (!WIFEXITED(status)) {
1733                 perror("should not happen");
1734                 return -1;
1735         }
1736
1737         return WEXITSTATUS(status);
1738 }
1739
1740 #define SCRIPT_INIT_DB "/usr/bin/email-service_init_db.sh"
1741
1742 INTERNAL_FUNC int emstorage_init_db(char *multi_user_name)
1743 {
1744         EM_DEBUG_FUNC_BEGIN();
1745         int err = EMAIL_ERROR_NONE;
1746         char *prefix_path = NULL;
1747         char *output_file_path = NULL;
1748         char temp_file_path[MAX_PATH] = {0};
1749         gid_t email_gid = -1;
1750         uid_t email_uid = -1;
1751         struct group *gr;
1752         struct group buf_group;
1753         struct passwd *pw;
1754         struct passwd buf_passwd;
1755         char buf_gr[MAX_GRP_BUF_SIZE];
1756         char buf_pw[MAX_GRP_BUF_SIZE];
1757         int result = 0;
1758
1759         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1760                 err = emcore_get_container_path(multi_user_name, &prefix_path);
1761                 if (err != EMAIL_ERROR_NONE) {
1762                         if (err != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1763                                 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", err);
1764                                 goto FINISH_OFF;
1765                         }
1766                 }
1767         } else {
1768                 prefix_path = strdup("");
1769         }
1770
1771         if (err == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1772                 err = emcore_get_canonicalize_path((char *)EMAIL_SERVICE_DB_FILE_PATH, &output_file_path);
1773                 if (err != EMAIL_ERROR_NONE) {
1774                         EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", err);
1775                         goto FINISH_OFF;
1776                 }
1777
1778                 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
1779         } else {
1780                 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
1781         }
1782
1783         EM_DEBUG_LOG("db file path : [%s]", temp_file_path);
1784
1785         if (!g_file_test(temp_file_path, G_FILE_TEST_EXISTS)) {
1786                 int ret = true;
1787                 const char *argv_script[] = {"/bin/sh", SCRIPT_INIT_DB, NULL};
1788                 ret = _xsystem(argv_script);
1789
1790                 if (ret == -1) {
1791                         EM_DEBUG_EXCEPTION("_xsystem failed");
1792                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1793                 }
1794
1795                 result = getgrnam_r(GID, &buf_group, buf_gr, sizeof(buf_gr), &gr);
1796                 if (result == 0 && NULL != gr)
1797                         email_gid = gr->gr_gid;
1798                 else
1799                         email_gid = GID_DEFAULT;
1800
1801
1802                 result = getpwnam_r(UID, &buf_passwd, buf_pw, sizeof(buf_pw), &pw);
1803                 if (result == 0 && NULL != pw)
1804                         email_uid = pw->pw_uid;
1805                 else
1806                         email_uid = UID_DEFAULT;
1807
1808
1809                 chmod(EMAIL_SERVICE_DB_FILE_PATH, 0660);
1810                 chmod(EMAIL_SERVICE_DB_JOURNAL_FILE_PATH, 0660);
1811                 chmod(EMAIL_SQL_PATH, 0660);
1812
1813                 chown(EMAIL_SERVICE_DB_FILE_PATH, email_uid, email_gid);
1814                 chown(EMAIL_SERVICE_DB_JOURNAL_FILE_PATH, email_uid, email_gid);
1815                 chown(EMAIL_SQL_PATH, email_uid, email_gid);
1816
1817         }
1818
1819 FINISH_OFF:
1820
1821         EM_SAFE_FREE(prefix_path);
1822         EM_SAFE_FREE(output_file_path);
1823
1824         EM_DEBUG_FUNC_END();
1825         return err;
1826 }
1827
1828 INTERNAL_FUNC int em_db_open(char *db_file_path, sqlite3 **sqlite_handle, int *err_code)
1829 {
1830         EM_DEBUG_FUNC_BEGIN();
1831         int rc = 0;
1832         int error = EMAIL_ERROR_NONE;
1833         int ret = false;
1834
1835         EM_DEBUG_LOG_DEV("*sqlite_handle[%p]", *sqlite_handle);
1836
1837         if (*sqlite_handle) { /*prevent 33351*/
1838                 EM_DEBUG_LOG_DEV(">>>>> DB Already Opened......");
1839                 if (err_code != NULL)
1840                         *err_code = error;
1841                 return true;
1842         }
1843
1844     EM_DEBUG_LOG("DB file path : [%s]", db_file_path);
1845
1846         /*  db open */
1847         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1848         if (SQLITE_OK != rc) {
1849                 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1850                 if (SQLITE_PERM == rc || SQLITE_CANTOPEN == rc) {
1851                         error = EMAIL_ERROR_PERMISSION_DENIED;
1852                 } else {
1853                         error = EMAIL_ERROR_DB_FAILURE;
1854                 }
1855                 sqlite3_close(*sqlite_handle);
1856                 *sqlite_handle = NULL;
1857
1858                 if (SQLITE_CORRUPT == rc) /* SQLITE_CORRUPT : The database disk image is malformed */ {/* Recovery DB file */
1859                         EM_DEBUG_LOG("The database disk image is malformed. Trying to remove and create database disk image and directories");
1860                         if (!_recovery_from_malformed_db_file(db_file_path, &error)) {
1861                                 EM_DEBUG_EXCEPTION("_recovery_from_malformed_db_file failed [%d]", error);
1862                                 goto FINISH_OFF;
1863                         }
1864
1865                         EM_DEBUG_LOG("Open DB again");
1866                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1867                         if (SQLITE_OK != rc) {
1868                                 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1869                                 if (SQLITE_PERM == rc) {
1870                                         error = EMAIL_ERROR_PERMISSION_DENIED;
1871                                 } else {
1872                                         error = EMAIL_ERROR_DB_FAILURE;
1873                                 }
1874                                 sqlite3_close(*sqlite_handle);
1875                                 *sqlite_handle = NULL;
1876                                 goto FINISH_OFF; /*prevent 33351*/
1877                         }
1878                 } else
1879                         goto FINISH_OFF;
1880         }
1881
1882         /* register busy handler */
1883         EM_DEBUG_LOG_DEV(">>>>> Register DB Handle to busy handler: *sqlite_handle[%p]", *sqlite_handle);
1884         rc = sqlite3_busy_handler(*sqlite_handle, _callback_sqlite_busy_handler, NULL);  /*  Busy Handler registration, NULL is a parameter which will be passed to handler */
1885         if (SQLITE_OK != rc) {
1886                 EM_DEBUG_EXCEPTION("sqlite3_busy_handler fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1887                 error = EMAIL_ERROR_DB_FAILURE;
1888                 sqlite3_close(*sqlite_handle);
1889                 *sqlite_handle = NULL;
1890                 goto FINISH_OFF;
1891         }
1892
1893         /* Register collation callback function */
1894         rc = sqlite3_create_collation(*sqlite_handle, "CONVERTUTF8", SQLITE_UTF8, NULL, _callback_collation_utf7_sort);
1895         if (SQLITE_OK != rc) {
1896                 EM_DEBUG_EXCEPTION("sqlite3_create_collation failed : [%d][%s]", rc, sqlite3_errmsg(*sqlite_handle));
1897                 error = EMAIL_ERROR_DB_FAILURE;
1898                 sqlite3_close(*sqlite_handle);
1899                 *sqlite_handle = NULL;
1900                 goto FINISH_OFF;
1901         }
1902
1903         ret = true;
1904
1905 FINISH_OFF:
1906         if (err_code != NULL)
1907                 *err_code = error;
1908
1909         EM_DEBUG_FUNC_END("ret [%d]", ret);
1910         return ret;
1911 }
1912
1913 INTERNAL_FUNC sqlite3* emstorage_db_open(char *multi_user_name, int *err_code)
1914 {
1915         EM_DEBUG_FUNC_BEGIN();
1916
1917         sqlite3 *_db_handle = NULL;
1918
1919         int error = EMAIL_ERROR_NONE;
1920         char *prefix_path = NULL;
1921
1922         _db_handle = emstorage_get_db_handle(multi_user_name);
1923
1924         if (_db_handle == NULL) {
1925                 char *output_file_path = NULL;
1926         char temp_file_path[MAX_PATH] = {0};
1927
1928         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1929                         error = emcore_get_container_path(multi_user_name, &prefix_path);
1930                         if (error != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION && error != EMAIL_ERROR_NONE) {
1931                                 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", error);
1932                                 goto FINISH_OFF;
1933                         }
1934         } else {
1935             prefix_path = strdup("");
1936         }
1937
1938                 if (error == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1939                         if ((error = emcore_get_canonicalize_path((char *)EMAIL_SERVICE_DB_FILE_PATH, &output_file_path)) != EMAIL_ERROR_NONE) {
1940                                 EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", error);
1941                                 goto FINISH_OFF;
1942                         }
1943
1944                         SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
1945                         EM_SAFE_FREE(output_file_path);
1946                 } else {
1947                         SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
1948                 }
1949
1950                 if (!em_db_open(temp_file_path, &_db_handle, &error)) {
1951                         EM_DEBUG_EXCEPTION("em_db_open failed[%d]", error);
1952                         goto FINISH_OFF;
1953                 }
1954
1955                 _initialize_shm_mutex(SHM_FILE_FOR_DB_LOCK, &shm_fd_for_db_lock, &mapped_for_db_lock);
1956
1957 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
1958                 _initialize_shm_mutex(SHM_FILE_FOR_MAIL_ID_LOCK, &shm_fd_for_generating_mail_id, &mapped_for_generating_mail_id);
1959 #endif /*__FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
1960
1961                 emstorage_set_db_handle(multi_user_name, _db_handle);
1962
1963                 emstorage_initialize_field_count();
1964         }
1965
1966 FINISH_OFF:
1967
1968         EM_SAFE_FREE(prefix_path);
1969
1970         if (err_code != NULL)
1971                 *err_code = error;
1972
1973         EM_DEBUG_FUNC_END("ret [%p]", _db_handle);
1974         return _db_handle;
1975 }
1976
1977 INTERNAL_FUNC int emstorage_db_close(char *multi_user_name, int *err_code)
1978 {
1979         EM_DEBUG_FUNC_BEGIN();
1980 #ifdef _MULTIPLE_DB_HANDLE
1981         sqlite3 *_db_handle = emstorage_get_db_handle(multi_user_name);
1982 #endif
1983
1984         int error = EMAIL_ERROR_NONE;
1985         int ret = false;
1986
1987         if (_db_handle) {
1988                 ret = sqlite3_close(_db_handle);
1989                 if (ret != SQLITE_OK) {
1990                         EM_DEBUG_EXCEPTION(" sqlite3_close fail - %d", ret);
1991                         error = EMAIL_ERROR_DB_FAILURE;
1992                         ret = false;
1993                         goto FINISH_OFF;
1994                 }
1995 #ifdef _MULTIPLE_DB_HANDLE
1996                 emstorage_remove_db_handle();
1997 #endif
1998                 _db_handle = NULL;
1999         }
2000
2001         ret = true;
2002
2003 FINISH_OFF:
2004         if (err_code != NULL)
2005                 *err_code = error;
2006
2007         EM_DEBUG_FUNC_END("ret [%d]", ret);
2008         return ret;
2009 }
2010
2011 INTERNAL_FUNC int emstorage_open(char *multi_user_name, int *err_code)
2012 {
2013         EM_DEBUG_FUNC_BEGIN();
2014
2015     int ret = false;
2016     int error = EMAIL_ERROR_NONE;
2017     int retValue;
2018         char *prefix_path = NULL;
2019     char buf[MAX_PATH] = {0};
2020
2021     if (EM_SAFE_STRLEN(multi_user_name) <= 0) {
2022         SNPRINTF(buf, sizeof(buf), "%s", DB_PATH);
2023     } else {
2024                 error = emcore_get_container_path(multi_user_name, &prefix_path);
2025                 if (error != EMAIL_ERROR_NONE) {
2026                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
2027                         goto FINISH_OFF;
2028                 }
2029         SNPRINTF(buf, sizeof(buf), "%s/%s", prefix_path, DB_PATH);
2030     }
2031
2032         if (!g_file_test(buf, G_FILE_TEST_EXISTS)) {
2033             retValue = mkdir(buf, DIRECTORY_PERMISSION);
2034
2035                 EM_DEBUG_LOG("mkdir return- %d", retValue);
2036             EM_DEBUG_LOG("emstorage_open - before sqlite3_open - pid = %d", getpid());
2037         }
2038
2039     if (emstorage_db_open(multi_user_name, &error) == NULL) {
2040         EM_DEBUG_EXCEPTION("emstorage_db_open failed[%d]", error);
2041         goto FINISH_OFF;
2042     }
2043
2044     if (_open_counter++ == 0) {
2045         _emstorage_open_once(multi_user_name, &error);
2046     }
2047
2048     ret = true;
2049
2050 FINISH_OFF:
2051
2052         EM_SAFE_FREE(prefix_path);
2053
2054     if (err_code != NULL)
2055         *err_code = error;
2056
2057     EM_DEBUG_FUNC_END("ret [%d]", ret);
2058     return ret;
2059 }
2060
2061 static int emstorage_get_field_count_from_create_table_query(char *input_create_table_query, int *output_field_count)
2062 {
2063         EM_DEBUG_FUNC_BEGIN("input_create_table_query[%d], output_field_count[%p]", input_create_table_query, output_field_count);
2064         int err = EMAIL_ERROR_NONE;
2065         int field_count = 0;
2066         char *pos = NULL;
2067
2068         if (input_create_table_query == NULL || output_field_count == NULL) {
2069                 err = EMAIL_ERROR_INVALID_PARAM;
2070                 goto FINISH_OFF;
2071         }
2072
2073         pos = input_create_table_query;
2074
2075         do {
2076                 field_count++;
2077                 if (pos == NULL || *pos == (char)0)
2078                         break;
2079                 pos += 1;
2080                 pos = strchr(pos, ',');
2081         } while (pos);
2082
2083         *output_field_count = field_count;
2084
2085         EM_DEBUG_LOG_DEV("field_count [%d]", field_count);
2086
2087 FINISH_OFF:
2088
2089         EM_DEBUG_FUNC_END("err [%d]", err);
2090         return err;
2091 }
2092
2093 INTERNAL_FUNC int emstorage_initialize_field_count()
2094 {
2095         EM_DEBUG_FUNC_BEGIN();
2096         int err = EMAIL_ERROR_NONE;
2097         int query_len = 0;
2098         char **create_table_query = NULL;
2099
2100         if (_field_count_of_table[CREATE_TABLE_MAIL_ACCOUNT_TBL] != 0) {
2101                 err = EMAIL_ERROR_ALREADY_INITIALIZED;
2102                 goto FINISH_OFF;
2103         }
2104
2105         err = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2106         if (err != EMAIL_ERROR_NONE) {
2107                 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", err);
2108                 goto FINISH_OFF;
2109         }
2110
2111         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]));
2112         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]));
2113         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]));
2114         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]));
2115         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_TBL]));
2116         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]));
2117         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]));
2118         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]));
2119 #ifdef __FEATURE_LOCAL_ACTIVITY__
2120         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]));
2121 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2122         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]));
2123         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]));
2124         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]));
2125 FINISH_OFF:
2126
2127         if (create_table_query) {
2128                 int i = 0;
2129                 for (i = 0; i < query_len; i++) {
2130                         if (create_table_query[i]) {
2131                                 EM_SAFE_FREE(create_table_query[i]);
2132                         }
2133                 }
2134                 EM_SAFE_FREE(create_table_query);
2135         }
2136
2137         EM_DEBUG_FUNC_END("err [%d]", err);
2138         return err;
2139 }
2140
2141 INTERNAL_FUNC int emstorage_create_table(char *multi_user_name, emstorage_create_db_type_t type, int *err_code)
2142 {
2143         EM_DEBUG_FUNC_BEGIN();
2144
2145         int error = EMAIL_ERROR_NONE;
2146         int rc = -1, ret = false;
2147         int query_len = 0;
2148         char sql_query_string[QUERY_SIZE] = {0, };
2149         char **create_table_query = NULL;
2150
2151         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2152
2153         error = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2154         if (error != EMAIL_ERROR_NONE) {
2155                 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
2156                 goto FINISH_OFF2;
2157         }
2158
2159         if (query_len < CREATE_TABLE_MAX) {
2160                 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
2161                 error = EMAIL_ERROR_SYSTEM_FAILURE;
2162                 goto FINISH_OFF2;
2163         }
2164
2165         EM_DEBUG_LOG("local_db_handle = %p.", local_db_handle);
2166
2167         char *sql;
2168         char **result = NULL;
2169
2170         /*  1. create mail_account_tbl */
2171         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_account_tbl';";
2172         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2173         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2174                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2175
2176         EM_DEBUG_LOG("emstorage_create_table - result[1] = %s %c", result[1], result[1]);
2177
2178         if (atoi(result[1]) < 1) {
2179                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2180                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2181                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2182
2183                 EM_DEBUG_LOG("CREATE TABLE mail_account_tbl");
2184                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ACCOUNT_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_account_tbl unique index */
2192                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ACCOUNT_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_account_tbl */
2201         else if (type == EMAIL_CREATE_DB_CHECK) {
2202                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], NULL, NULL, NULL), rc);
2203                 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)));
2204         }
2205
2206         sqlite3_free_table(result);
2207         result = NULL;
2208
2209         /*  2. create mail_box_tbl */
2210         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_box_tbl';";
2211
2212         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2213         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2214                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
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_box_tbl");
2222
2223                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_BOX_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_local_mailbox_tbl unique index */
2231                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_BOX_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_box_tbl */
2240         else if (type == EMAIL_CREATE_DB_CHECK) {
2241                 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_BOX_TBL], NULL, NULL, NULL);
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_BOX_TBL], rc, sqlite3_errmsg(local_db_handle)));
2244         }
2245         sqlite3_free_table(result);
2246         result = NULL;
2247
2248         /*  3. create mail_read_mail_uid_tbl */
2249         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_read_mail_uid_tbl';";
2250         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2251         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2252                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2253
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_read_mail_uid_tbl");
2261
2262                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_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                 /*  create mail_read_mail_uid_tbl unique index */
2270                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_IDX]);
2271                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2272                 if (error != EMAIL_ERROR_NONE) {
2273                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2274                         goto FINISH_OFF;
2275                 }
2276
2277                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2278         } /*  mail_read_mail_uid_tbl */
2279         else if (type == EMAIL_CREATE_DB_CHECK) {
2280                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], NULL, NULL, NULL), rc);
2281                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2282                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], rc, sqlite3_errmsg(local_db_handle)));
2283         }
2284         sqlite3_free_table(result);
2285         result = NULL;
2286
2287         /*  4. create mail_rule_tbl */
2288         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_rule_tbl';";
2289
2290         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2291         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2292                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2293
2294         if (atoi(result[1]) < 1) {
2295                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2296                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2297                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2298
2299                 EM_DEBUG_LOG("CREATE TABLE mail_rule_tbl");
2300
2301                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_RULE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2302                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2303                 if (error != EMAIL_ERROR_NONE) {
2304                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2305                         goto FINISH_OFF;
2306                 }
2307
2308                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2309         } /*  mail_rule_tbl */
2310         else if (type == EMAIL_CREATE_DB_CHECK) {
2311                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_RULE_TBL], NULL, NULL, NULL), rc);
2312                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2313                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_RULE_TBL], rc, sqlite3_errmsg(local_db_handle)));
2314         }
2315         sqlite3_free_table(result);
2316         result = NULL;
2317
2318         /*  5. create mail_tbl */
2319         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_tbl';";
2320         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2321         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2322                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2323
2324         if (atoi(result[1]) < 1) {
2325                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2326                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2327                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2328                 EM_DEBUG_LOG("CREATE TABLE mail_tbl");
2329
2330                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2331                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2332                 if (error != EMAIL_ERROR_NONE) {
2333                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2334                         goto FINISH_OFF;
2335                 }
2336
2337                 /*  create mail_tbl unique index */
2338                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_IDX]);
2339                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2340                 if (error != EMAIL_ERROR_NONE) {
2341                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2342                         goto FINISH_OFF;
2343                 }
2344
2345                 /*  create mail_tbl index for date_time */
2346                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_DATETIME_IDX]);
2347                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2348                 if (error != EMAIL_ERROR_NONE) {
2349                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2350                         goto FINISH_OFF;
2351                 }
2352
2353                 /*  create mail_tbl index for thread_item_count */
2354                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_THREAD_IDX]);
2355                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2356                 if (error != EMAIL_ERROR_NONE) {
2357                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2358                         goto FINISH_OFF;
2359                 }
2360
2361                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2362                 /*  just one time call */
2363 /*              EFTSInitFTSIndex(FTS_EMAIL_IDX); */
2364         } /*  mail_tbl */
2365         else if (type == EMAIL_CREATE_DB_CHECK) {
2366                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TBL], NULL, NULL, NULL), rc);
2367                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2368                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TBL], rc, sqlite3_errmsg(local_db_handle)));
2369         }
2370         sqlite3_free_table(result);
2371         result = NULL;
2372
2373         /*  6. create mail_attachment_tbl */
2374         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_attachment_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                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2381                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2382                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2383
2384                 EM_DEBUG_LOG("CREATE TABLE mail_attachment_tbl");
2385
2386                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2387                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2388                 if (error != EMAIL_ERROR_NONE) {
2389                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2390                         goto FINISH_OFF;
2391                 }
2392
2393                 /*  create mail_attachment_tbl unique index */
2394                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_IDX]);
2395                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2396                 if (error != EMAIL_ERROR_NONE) {
2397                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2398                         goto FINISH_OFF;
2399                 }
2400
2401                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2402         } /*  mail_attachment_tbl */
2403         else if (type == EMAIL_CREATE_DB_CHECK) {
2404                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], NULL, NULL, NULL), rc);
2405                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2406                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], rc, sqlite3_errmsg(local_db_handle)));
2407         }
2408         sqlite3_free_table(result);
2409         result = NULL;
2410
2411 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
2412
2413         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_partial_body_activity_tbl';";
2414         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2415         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2416                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2417
2418         if (atoi(result[1]) < 1) {
2419
2420                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2421                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2422                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2423
2424                 EM_DEBUG_LOG("CREATE TABLE mail_partial_body_activity_tbl");
2425
2426                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
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_rule_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_PARTIAL_BODY_ACTIVITY_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_PARTIAL_BODY_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2439         }
2440         sqlite3_free_table(result);
2441         result = NULL;
2442
2443 #endif /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
2444
2445         /*  create mail_meeting_tbl */
2446         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_meeting_tbl';";
2447         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2448         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2449                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2450
2451         if (atoi(result[1]) < 1) {
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_meeting_tbl");
2457
2458                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_MEETING_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
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                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_MEETING_IDX]);
2466                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2467                 if (error != EMAIL_ERROR_NONE) {
2468                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2469                         goto FINISH_OFF;
2470                 }
2471
2472                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2473         } /*  mail_contact_sync_tbl */
2474         else if (type == EMAIL_CREATE_DB_CHECK) {
2475                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], NULL, NULL, NULL), rc);
2476                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2477                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], rc, sqlite3_errmsg(local_db_handle)));
2478         }
2479         sqlite3_free_table(result);
2480         result = NULL;
2481
2482 #ifdef __FEATURE_LOCAL_ACTIVITY__
2483
2484                 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_local_activity_tbl';";
2485                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2486                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2487                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2488
2489                 if (atoi(result[1]) < 1) {
2490
2491                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2492                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2493                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2494
2495                         EM_DEBUG_LOG(" CREATE TABLE mail_local_activity_tbl");
2496
2497                         SNPRINTF(sql_query_string, sizeof(sql_query_string), create_table_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL]);
2498                         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2499                         if (error != EMAIL_ERROR_NONE) {
2500                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2501                                 goto FINISH_OFF;
2502                         }
2503
2504                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2505                 } /*  mail_rule_tbl */
2506                 else if (type == EMAIL_CREATE_DB_CHECK) {
2507                         rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL);
2508                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2509                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2510                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2511                 }
2512                 sqlite3_free_table(result);
2513                 result = NULL;
2514 #endif /*  __FEATURE_LOCAL_ACTIVITY__ */
2515         /*  create mail_certificate_tbl */
2516         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_certificate_tbl';";
2517         /*  rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL);   */
2518         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2519         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)));
2520
2521         if (atoi(result[1]) < 1) {
2522                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2523                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2524
2525                 EM_DEBUG_LOG("CREATE TABLE mail_certificate_tbl");
2526
2527                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2528                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2529                 if (error != EMAIL_ERROR_NONE) {
2530                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2531                         goto FINISH_OFF;
2532                 }
2533
2534                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2535         } /*  mail_contact_sync_tbl */
2536         else if (type == EMAIL_CREATE_DB_CHECK) {
2537                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], NULL, NULL, NULL), rc);
2538                 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)));
2539         }
2540
2541         sqlite3_free_table(result);
2542         result = NULL;
2543
2544         /*  create mail_task_tbl */
2545         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_task_tbl';";
2546         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2547         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)));
2548
2549         if (atoi(result[1]) < 1) {
2550                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2551                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2552
2553                 EM_DEBUG_LOG("CREATE TABLE mail_task_tbl");
2554
2555                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TASK_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2556                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2557                 if (error != EMAIL_ERROR_NONE) {
2558                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2559                         goto FINISH_OFF;
2560                 }
2561
2562                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_TASK_IDX]);
2563                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2564                 if (error != EMAIL_ERROR_NONE) {
2565                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2566                         goto FINISH_OFF;
2567                 }
2568
2569                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2570         } /*  mail_task_tbl */
2571         else if (type == EMAIL_CREATE_DB_CHECK) {
2572                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TASK_TBL], NULL, NULL, NULL), rc);
2573                 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)));
2574         }
2575
2576         sqlite3_free_table(result);
2577         result = NULL;
2578
2579 #ifdef __FEATURE_BODY_SEARCH__
2580         /*  create mail_text_tbl */
2581         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_text_tbl';";
2582         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2583         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2584
2585         if (atoi(result[1]) < 1) {
2586                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2587                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2588
2589                 EM_DEBUG_LOG("CREATE TABLE mail_text_tbl");
2590
2591                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TEXT_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2592                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2593                 if (error != EMAIL_ERROR_NONE) {
2594                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2595                         goto FINISH_OFF;
2596                 }
2597
2598                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2599         } /*  mail_text_tbl */
2600         else if (type == EMAIL_CREATE_DB_CHECK) {
2601                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TEXT_TBL], NULL, NULL, NULL), rc);
2602                 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)));
2603         }
2604
2605         sqlite3_free_table(result);
2606         result = NULL;
2607
2608 #endif
2609
2610 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
2611         /*  create mail_auto_download_activity_tbl */
2612         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_auto_download_activity_tbl';";
2613         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2614         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)));
2615
2616         if (atoi(result[1]) < 1) {
2617                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2618                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2619
2620                 EM_DEBUG_LOG("CREATE TABLE mail_auto_download_activity_tbl");
2621
2622                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], sizeof(sql_query_string)-1);
2623                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2624                 if (error != EMAIL_ERROR_NONE) {
2625                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2626                         goto FINISH_OFF;
2627                 }
2628
2629                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2630         } /*  mail_auto_download_activity_tbl */
2631         else if (type == EMAIL_CREATE_DB_CHECK) {
2632                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2633                 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)));
2634         }
2635
2636         sqlite3_free_table(result);
2637         result = NULL;
2638 #endif
2639
2640
2641         ret = true;
2642
2643 FINISH_OFF:
2644
2645         if (result) sqlite3_free_table(result);
2646
2647         if (ret == true) {
2648                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2649         } else {
2650                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
2651         }
2652
2653 FINISH_OFF2:
2654         if (create_table_query) {
2655                 int i = 0;
2656                 for (i = 0; i < query_len; i++) {
2657                         if (create_table_query[i]) {
2658                                 EM_SAFE_FREE(create_table_query[i]);
2659                         }
2660                 }
2661                 EM_SAFE_FREE(create_table_query);
2662         }
2663
2664         if (err_code != NULL)
2665                 *err_code = error;
2666
2667         EM_DEBUG_FUNC_END("ret [%d]", ret);
2668         return ret;
2669 }
2670
2671 /* Query series --------------------------------------------------------------*/
2672 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)
2673 {
2674         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);
2675         int rc = -1;
2676         int query_size = 0;
2677         int error = EMAIL_ERROR_NONE;
2678         DB_STMT hStmt = NULL;
2679         char *sql_query_string = NULL;
2680         char **result;
2681         sqlite3 *local_db_handle = NULL;
2682
2683         if (!input_conditional_clause || (!output_total_mail_count && !output_unseen_mail_count)) {
2684                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2685                 return EMAIL_ERROR_INVALID_PARAM;
2686         }
2687
2688         query_size = EM_SAFE_STRLEN(input_conditional_clause) + QUERY_SIZE;
2689         sql_query_string = em_malloc(query_size);
2690         if (sql_query_string == NULL) {
2691                 EM_DEBUG_EXCEPTION("em_mallocfailed");
2692                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2693                 goto FINISH_OFF;
2694         }
2695
2696         local_db_handle = emstorage_get_db_connection(multi_user_name);
2697
2698         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2699
2700         SNPRINTF(sql_query_string, query_size, "SELECT COUNT(*) FROM mail_tbl");
2701         EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
2702
2703         if (output_total_mail_count) {
2704                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
2705                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2706                         ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
2707
2708                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2709                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2710                         ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
2711                 _get_stmt_field_data_int(hStmt, output_total_mail_count, 0);
2712         }
2713
2714         if (output_unseen_mail_count) {
2715                 EM_SAFE_STRCAT(sql_query_string, " AND flags_seen_field = 0 ");
2716
2717                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
2718                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2719                         ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2720
2721                 *output_unseen_mail_count = atoi(result[1]);
2722                 sqlite3_free_table(result);
2723         }
2724
2725 FINISH_OFF:
2726
2727         if (hStmt != NULL) {
2728                 rc = sqlite3_finalize(hStmt);
2729                 if (rc != SQLITE_OK) {
2730                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
2731                         error = EMAIL_ERROR_DB_FAILURE;
2732                 }
2733         }
2734
2735         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2736
2737         EM_SAFE_FREE(sql_query_string);
2738
2739         EM_DEBUG_FUNC_END("error [%d]", error);
2740         return error;
2741 }
2742
2743 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)
2744 {
2745         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);
2746
2747         int      i = 0;
2748         int      count = 0;
2749         int      rc = -1;
2750         int      cur_query = 0;
2751         int      col_index;
2752         int      query_size = 0;
2753         int      error = EMAIL_ERROR_NONE;
2754         int     *result_mail_id_list = NULL;
2755         char   **result = NULL;
2756         char     *sql_query_string = NULL;
2757         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2758
2759         EM_IF_NULL_RETURN_VALUE(input_conditional_clause, EMAIL_ERROR_INVALID_PARAM);
2760         EM_IF_NULL_RETURN_VALUE(output_mail_id_list, EMAIL_ERROR_INVALID_PARAM);
2761         EM_IF_NULL_RETURN_VALUE(output_mail_id_count, EMAIL_ERROR_INVALID_PARAM);
2762
2763         query_size = strlen(input_conditional_clause) + strlen("SELECT mail_id FROM mail_tbl ") + 10;  // 10 is extra space
2764         sql_query_string = em_malloc(query_size);
2765         if (sql_query_string == NULL) {
2766                 EM_DEBUG_EXCEPTION("em_mallocfailed");
2767                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2768                 goto FINISH_OFF;
2769         }
2770
2771         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2772
2773         /* Composing query */
2774         SNPRINTF_OFFSET(sql_query_string, cur_query, query_size, "SELECT mail_id FROM mail_tbl ");
2775         EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
2776
2777         EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
2778
2779         /* Performing query */
2780         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2781         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2782                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2783
2784         col_index = 1;
2785
2786         /*  to get mail list */
2787         if (count == 0) {
2788                 EM_DEBUG_LOG("No mail found...");
2789                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2790                 goto FINISH_OFF;
2791         }
2792
2793         EM_DEBUG_LOG("There are [%d] mails.", count);
2794
2795         if (!(result_mail_id_list = (int*)em_malloc(sizeof(int) * count))) {
2796                 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
2797                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2798                 goto FINISH_OFF;
2799         }
2800
2801         EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2802
2803         for (i = 0; i < count; i++)
2804                 _get_table_field_data_int(result, result_mail_id_list + i, col_index++);
2805
2806         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >> ", count);
2807
2808         *output_mail_id_list  = result_mail_id_list;
2809         *output_mail_id_count = count;
2810
2811 FINISH_OFF:
2812
2813         if (result)
2814                 sqlite3_free_table(result);
2815
2816         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2817
2818         EM_SAFE_FREE(sql_query_string);
2819
2820         if (error != EMAIL_ERROR_NONE)
2821                 EM_SAFE_FREE(result_mail_id_list);
2822
2823         EM_DEBUG_FUNC_END("error [%d]", error);
2824         return error;
2825 }
2826
2827 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)
2828 {
2829         EM_DEBUG_FUNC_BEGIN();
2830         EM_PROFILE_BEGIN(emstorage_query_mail_list_func);
2831
2832         int i = 0, count = 0, rc = -1, to_get_count = (result_mail_list) ? 0 : 1;
2833         int sql_query_string_length = 0;
2834         int local_inline_content_count = 0, local_attachment_count = 0;
2835         int cur_query = 0, base_count = 0, col_index;
2836         int ret = false, error = EMAIL_ERROR_NONE;
2837         char *date_time_string = NULL;
2838         char **result = NULL;
2839         char *field_mail_id = "mail_id";
2840         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 ";
2841         char *select_query_form = "SELECT %s FROM mail_tbl ";
2842         char *target_field = NULL;
2843         char *sql_query_string = NULL;
2844         email_mail_list_item_t *mail_list_item_from_tbl = NULL;
2845         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2846
2847         EM_IF_NULL_RETURN_VALUE(conditional_clause, false);
2848         EM_IF_NULL_RETURN_VALUE(result_count, false);
2849
2850         EMSTORAGE_START_READ_TRANSACTION(transaction);
2851
2852         /*  select clause */
2853         if (to_get_count) /*  count only */
2854                 target_field = field_mail_id;
2855         else /* mail list in plain form */
2856                 target_field = field_all;
2857
2858         sql_query_string_length = EM_SAFE_STRLEN(select_query_form) + EM_SAFE_STRLEN(target_field) + EM_SAFE_STRLEN(conditional_clause);
2859
2860         if (sql_query_string_length)
2861                 sql_query_string = em_malloc(sql_query_string_length);
2862
2863         if (sql_query_string == NULL) {
2864                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
2865                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2866                 goto FINISH_OFF;
2867         }
2868
2869         cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, sql_query_string_length, select_query_form, target_field);
2870
2871         strncat(sql_query_string, conditional_clause, sql_query_string_length - cur_query);
2872
2873         EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
2874
2875         /*  performing query */
2876         EM_PROFILE_BEGIN(emstorage_query_mail_list_performing_query);
2877         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2878         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2879                 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2880         EM_PROFILE_END(emstorage_query_mail_list_performing_query);
2881
2882         if (!base_count)
2883                 base_count = ({
2884                                 int i = 0;
2885                                 char *tmp = NULL;
2886                                 for (tmp = field_all; tmp && *(tmp + 1); tmp = index(tmp + 1, ',')) i++ ;
2887                                 i;
2888                                 });
2889
2890         col_index = base_count;
2891
2892         EM_DEBUG_LOG_DEV("base_count [%d]", base_count);
2893
2894         if (to_get_count) {
2895                 /*  to get count */
2896                 if (!count) {
2897                         EM_DEBUG_LOG_DEV("No mail found...");
2898                         ret = false;
2899                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
2900                         goto FINISH_OFF;
2901                 }
2902                 EM_DEBUG_LOG_DEV("There are [%d] mails.", count);
2903         } else {
2904                 /*  to get mail list */
2905                 if (!count) {
2906                         EM_DEBUG_LOG_DEV("No mail found...");
2907                         ret = false;
2908                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
2909                         goto FINISH_OFF;
2910                 }
2911
2912                 EM_DEBUG_LOG_DEV("There are [%d] mails.", count);
2913                 if (!(mail_list_item_from_tbl = (email_mail_list_item_t*)em_malloc(sizeof(email_mail_list_item_t) * count))) {
2914                         EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
2915                         error = EMAIL_ERROR_OUT_OF_MEMORY;
2916                         goto FINISH_OFF;
2917                 }
2918
2919                 EM_PROFILE_BEGIN(emstorage_query_mail_list_loop);
2920                 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >> ");
2921                 for (i = 0; i < count; i++) {
2922                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_id), col_index++);
2923                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].account_id), col_index++);
2924                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mailbox_id), col_index++);
2925                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].mailbox_type), col_index++);
2926                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].full_address_from, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2927                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_sender, MAX_EMAIL_ADDRESS_LENGTH, 1, col_index++);
2928                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_recipient, STRING_LENGTH_FOR_DISPLAY,  1, col_index++);
2929                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].subject, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2930                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].body_download_status), col_index++);
2931                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_size), col_index++);
2932                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_seen_field), col_index++);
2933                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_deleted_field), col_index++);
2934                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_flagged_field), col_index++);
2935                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_answered_field), col_index++);
2936                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_recent_field), col_index++);
2937                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_draft_field), col_index++);
2938                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_forwarded_field), col_index++);
2939                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].DRM_status), col_index++);
2940                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].priority), col_index++);
2941                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].save_status), col_index++);
2942                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].lock_status), col_index++);
2943                         _get_table_field_data_int(result, &local_attachment_count, col_index++);
2944                         _get_table_field_data_int(result, &local_inline_content_count, col_index++);
2945                         _get_table_field_data_time_t(result, &(mail_list_item_from_tbl[i].date_time), col_index++);
2946                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].preview_text, MAX_PREVIEW_TEXT_LENGTH, 1, col_index++);
2947                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_id), col_index++);
2948                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_item_count), col_index++);
2949                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].meeting_request_status), col_index++);
2950                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].message_class), col_index++);
2951                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].smime_type), col_index++);
2952                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].scheduled_sending_time), col_index++);
2953                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].remaining_resend_times), col_index++);
2954                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].tag_id), col_index++);
2955                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].eas_data_length), col_index++);
2956                         _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++);
2957
2958                         mail_list_item_from_tbl[i].attachment_count = (local_attachment_count > 0) ? 1 : 0;
2959                 }
2960                 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >> ", count);
2961                 EM_PROFILE_END(emstorage_query_mail_list_loop);
2962         }
2963
2964         ret = true;
2965
2966 FINISH_OFF:
2967         EM_DEBUG_LOG("MAIL_COUNT [%d]", count);
2968
2969         if (result)
2970                 sqlite3_free_table(result);
2971
2972         if (to_get_count)
2973                 *result_count = count;
2974         else {
2975                 if (ret == true) {
2976                         if (result_mail_list)
2977                                 *result_mail_list = mail_list_item_from_tbl;
2978                         *result_count = count;
2979                 } else
2980                         EM_SAFE_FREE(mail_list_item_from_tbl);
2981         }
2982
2983         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2984
2985 //      sqlite3_db_release_memory(local_db_handle);
2986
2987         EM_SAFE_FREE(sql_query_string);
2988         EM_SAFE_FREE(date_time_string);
2989
2990         if (err_code != NULL)
2991                 *err_code = error;
2992
2993         EM_PROFILE_END(emstorage_query_mail_list_func);
2994         EM_DEBUG_FUNC_END("ret [%d]", ret);
2995         return ret;
2996 }
2997
2998
2999 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)
3000 {
3001         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);
3002
3003         if (!conditional_clause || !result_mail_tbl || !result_count) {
3004                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: [%p] [%p] [%p]", conditional_clause, result_mail_tbl, result_mail_tbl);
3005                 if (err_code != NULL)
3006                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3007                 return false;
3008         }
3009
3010         int i, col_index = FIELD_COUNT_OF_MAIL_TBL, rc, ret = false, count;
3011         int error = EMAIL_ERROR_NONE;
3012         char **result = NULL, sql_query_string[QUERY_SIZE] = {0, };
3013         emstorage_mail_tbl_t* p_data_tbl = NULL;
3014         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3015
3016         EMSTORAGE_START_READ_TRANSACTION(transaction);
3017
3018         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl %s", conditional_clause);
3019
3020         EM_DEBUG_LOG_DEV("Query[%s]", sql_query_string);
3021
3022         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3023         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3024                 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
3025
3026         if (!count) {
3027                 EM_DEBUG_LOG("No mail found...");
3028                 ret = false;
3029                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
3030                 goto FINISH_OFF;
3031         }
3032
3033         EM_DEBUG_LOG("There are [%d] mails.", count);
3034         if (!(p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * count))) {
3035                 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
3036                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3037                 goto FINISH_OFF;
3038         }
3039
3040         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >> ");
3041         for (i = 0; i < count; i++) {
3042                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
3043                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3044                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3045                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_type), col_index++);
3046                 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 1, col_index++);
3047                 _get_table_field_data_time_t (result, &(p_data_tbl[i].date_time), col_index++);
3048                 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
3049                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
3050                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
3051                 _get_table_field_data_string(result, &(p_data_tbl[i].message_id), 0, col_index++);
3052                 _get_table_field_data_int(result, &(p_data_tbl[i].reference_mail_id), col_index++);
3053                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_from), 1, col_index++);
3054                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_reply), 1, col_index++);
3055                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_to), 1, col_index++);
3056                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_cc), 1, col_index++);
3057                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_bcc), 1, col_index++);
3058                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_return), 1, col_index++);
3059                 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_sender), 1, col_index++);
3060                 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_recipient), 1, col_index++);
3061                 _get_table_field_data_string(result, &(p_data_tbl[i].alias_sender), 1, col_index++);
3062                 _get_table_field_data_string(result, &(p_data_tbl[i].alias_recipient), 1, col_index++);
3063                 _get_table_field_data_int(result, &(p_data_tbl[i].body_download_status), col_index++);
3064                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
3065                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
3066                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
3067                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_size), col_index++);
3068                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_seen_field), col_index++);
3069                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_deleted_field), col_index++);
3070                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_flagged_field), col_index++);
3071                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_answered_field), col_index++);
3072                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_recent_field), col_index++);
3073                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_draft_field), col_index++);
3074                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_forwarded_field), col_index++);
3075                 _get_table_field_data_int(result, &(p_data_tbl[i].DRM_status), col_index++);
3076                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].priority), col_index++);
3077                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
3078                 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
3079                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].report_status), col_index++);
3080                 _get_table_field_data_int(result, &(p_data_tbl[i].attachment_count), col_index++);
3081                 _get_table_field_data_int(result, &(p_data_tbl[i].inline_content_count), col_index++);
3082                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
3083                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
3084                 _get_table_field_data_string(result, &(p_data_tbl[i].preview_text), 1, col_index++);
3085                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].meeting_request_status), col_index++);
3086                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].message_class), col_index++);
3087                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].digest_type), col_index++);
3088                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].smime_type), col_index++);
3089                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].scheduled_sending_time), col_index++);
3090                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].remaining_resend_times), col_index++);
3091                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].tag_id), col_index++);
3092                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].replied_time), col_index++);
3093                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].forwarded_time), col_index++);
3094                 _get_table_field_data_string(result, &(p_data_tbl[i].default_charset), 0, col_index++);
3095                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3096                 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3097                 _get_table_field_data_string(result, &(p_data_tbl[i].user_name), 0, col_index++);
3098         }
3099
3100         ret = true;
3101
3102 FINISH_OFF:
3103         if (result)
3104                 sqlite3_free_table(result);
3105
3106         if (ret) {
3107                 *result_mail_tbl = p_data_tbl;
3108                 *result_count = count;
3109         } else {
3110                 *result_mail_tbl = NULL;
3111                 *result_count = 0;
3112         }
3113
3114         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3115
3116 //      sqlite3_db_release_memory(local_db_handle);
3117
3118         if (err_code != NULL)
3119                 *err_code = error;
3120
3121         EM_DEBUG_FUNC_END("ret [%d]", ret);
3122         return ret;
3123 }
3124
3125 #ifdef __FEATURE_BODY_SEARCH__
3126 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)
3127 {
3128         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);
3129
3130         if (!conditional_clause || !result_mail_text_tbl || !result_count) { /*prevent 50930*/
3131                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3132                 if (err_code != NULL)
3133                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3134                 return false;
3135         }
3136
3137         int i = 0;
3138         int rc = 0;
3139         int ret = false;
3140         int count = 0;
3141         int col_index = FIELD_COUNT_OF_MAIL_TEXT_TBL;
3142         int error = EMAIL_ERROR_NONE;
3143         char **result = NULL;
3144         char sql_query_string[QUERY_SIZE] = {0, };
3145         emstorage_mail_text_tbl_t* p_data_tbl = NULL;
3146         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3147
3148         EMSTORAGE_START_READ_TRANSACTION(transaction);
3149
3150         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_text_tbl %s", conditional_clause);
3151
3152         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
3153
3154         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3155         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3156                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3157
3158         if (!count) {
3159                 EM_DEBUG_LOG("No mail found...");
3160                 ret = false;
3161                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
3162                 goto FINISH_OFF;
3163         }
3164
3165         EM_DEBUG_LOG("There are [%d] mails.", count);
3166         if (!(p_data_tbl = (emstorage_mail_text_tbl_t *)em_malloc(sizeof(emstorage_mail_text_tbl_t) * count))) {
3167                 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_text_tbl_t failed...");
3168                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3169                 goto FINISH_OFF;
3170         }
3171
3172         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
3173         for (i = 0; i < count; i++) {
3174                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
3175                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3176                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3177                 _get_table_field_data_string(result, &(p_data_tbl[i].body_text), 0, col_index++);
3178         }
3179
3180         ret = true;
3181
3182 FINISH_OFF:
3183         if (result)
3184                 sqlite3_free_table(result);
3185
3186         if (ret == true) {
3187                 *result_mail_text_tbl = p_data_tbl;
3188                 *result_count = count;
3189         } else
3190                 EM_SAFE_FREE(p_data_tbl);
3191
3192         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3193
3194 //      sqlite3_db_release_memory(local_db_handle);
3195
3196         if (err_code != NULL)
3197                 *err_code = error;
3198
3199         EM_DEBUG_FUNC_END("ret [%d]", ret);
3200         return ret;
3201 }
3202 #endif
3203
3204 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)
3205 {
3206         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);
3207
3208         int i = 0;
3209         int rc;
3210         int count = 0;
3211         int col_index = 0;
3212         int error = EMAIL_ERROR_NONE;
3213         int read_count = 0;
3214         int total_count = 0;
3215         char **result;
3216         char sql_query_string[QUERY_SIZE] = {0, };
3217         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 ";
3218         emstorage_mailbox_tbl_t* p_data_tbl = NULL;
3219         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3220
3221         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
3222
3223         if (input_get_mail_count == 0) {        /* without mail count */
3224                 col_index = 15;
3225                 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);
3226         } else {        /* with read count and total count */
3227                 col_index = 17;
3228                 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);
3229         }
3230
3231         EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
3232
3233         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
3234         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3235                 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string))
3236
3237         EM_DEBUG_LOG_DEV("result count [%d]", count);
3238
3239         if (count == 0) {
3240                 EM_DEBUG_LOG_SEC("Can't find mailbox query[%s]", sql_query_string);
3241                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
3242                 goto FINISH_OFF;
3243         }
3244
3245         if ((p_data_tbl = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t) * count)) == NULL) {
3246                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
3247                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3248                 goto FINISH_OFF;
3249         }
3250
3251         for (i = 0; i < count; i++) {
3252                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3253                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3254                 _get_table_field_data_int(result, &(p_data_tbl[i].local_yn), col_index++);
3255                 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
3256                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].mailbox_type), col_index++);
3257                 _get_table_field_data_string(result, &(p_data_tbl[i].alias), 0, col_index++);
3258                 _get_table_field_data_int(result, &(p_data_tbl[i].deleted_flag), col_index++);
3259                 _get_table_field_data_int(result, &(p_data_tbl[i].modifiable_yn), col_index++);
3260                 _get_table_field_data_int(result, &(p_data_tbl[i].total_mail_count_on_server), col_index++);
3261                 _get_table_field_data_int(result, &(p_data_tbl[i].has_archived_mails), col_index++);
3262                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_slot_size), col_index++);
3263                 _get_table_field_data_int(result, &(p_data_tbl[i].no_select), col_index++);
3264                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].last_sync_time), col_index++);
3265                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3266                 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3267
3268                 if (input_get_mail_count == 1) {
3269                         _get_table_field_data_int(result, &(total_count), col_index++);
3270                         p_data_tbl[i].total_mail_count_on_local = total_count;
3271                         _get_table_field_data_int(result, &(read_count), col_index++);
3272                         p_data_tbl[i].unread_count = total_count - read_count;                  /*  return unread count, NOT  */
3273                 }
3274         }
3275
3276
3277 FINISH_OFF:
3278         if (result)
3279                 sqlite3_free_table(result);
3280
3281         if (error == EMAIL_ERROR_NONE) {
3282                 *output_mailbox_list = p_data_tbl;
3283                 *output_mailbox_count = count;
3284                 EM_DEBUG_LOG("Mailbox Count [%d]", count);
3285         }
3286
3287         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
3288
3289 //      sqlite3_db_release_memory(local_db_handle);
3290
3291         EM_DEBUG_FUNC_END("error [%d]", error);
3292         return error;
3293 }
3294
3295 /* Query series --------------------------------------------------------------*/
3296 INTERNAL_FUNC int emstorage_check_duplicated_account(char *multi_user_name, email_account_t* account, int transaction, int *err_code)
3297 {
3298         EM_DEBUG_FUNC_BEGIN();
3299         int rc = -1, ret = false;
3300         int error = EMAIL_ERROR_NONE;
3301         char **result;
3302         int count;
3303         char sql_query_string[QUERY_SIZE] = {0, };
3304         sqlite3 *local_db_handle = NULL;
3305
3306         if (account == NULL || account->user_email_address == NULL || account->incoming_server_user_name == NULL || account->incoming_server_address == NULL ||
3307                 account->outgoing_server_user_name == NULL || account->outgoing_server_address == NULL) {
3308                 if (err_code != NULL)
3309                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3310                 return false;
3311         }
3312
3313         local_db_handle = emstorage_get_db_connection(multi_user_name);
3314
3315         EMSTORAGE_START_READ_TRANSACTION(transaction);
3316
3317         SNPRINTF(sql_query_string, sizeof(sql_query_string),
3318                 "SELECT COUNT(*) FROM mail_account_tbl "
3319                 " WHERE "
3320                 " user_email_address = '%s' AND "
3321                 " incoming_server_user_name = '%s' AND "
3322                 " incoming_server_type = %d AND "
3323                 " incoming_server_address = '%s' AND "
3324                 " outgoing_server_user_name = '%s' AND "
3325                 " outgoing_server_type = %d AND "
3326                 " outgoing_server_address = '%s'; ",
3327                 account->user_email_address,
3328                 account->incoming_server_user_name, account->incoming_server_type, account->incoming_server_address,
3329                 account->outgoing_server_user_name, account->outgoing_server_type, account->outgoing_server_address
3330         );
3331         EM_DEBUG_LOG_SEC("Query[%s]", sql_query_string);
3332         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
3333         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3334                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3335
3336         count = atoi(result[1]);
3337         sqlite3_free_table(result);
3338
3339         EM_DEBUG_LOG("Count of Duplicated Account Information: [%d]", count);
3340
3341         if (count == 0) {       /*  not duplicated account */
3342                 ret = true;
3343                 EM_DEBUG_LOG_SEC("NOT duplicated account: user_email_address[%s]", account->user_email_address);
3344         } else {        /*  duplicated account */
3345                 ret = false;
3346                 EM_DEBUG_LOG_SEC("The same account already exists. Duplicated account: user_email_address[%s]", account->user_email_address);
3347                 error = EMAIL_ERROR_ALREADY_EXISTS;
3348         }
3349
3350 FINISH_OFF:
3351
3352         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3353
3354         if (err_code != NULL)
3355                 *err_code = error;
3356
3357         EM_DEBUG_FUNC_END("ret [%d]", ret);
3358         return ret;
3359
3360 }
3361
3362 INTERNAL_FUNC int emstorage_get_account_count(char *multi_user_name, int *count, int transaction, int *err_code)
3363 {
3364         EM_DEBUG_FUNC_BEGIN();
3365
3366         if (!count) {
3367                 if (err_code != NULL)
3368                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3369                 return false;
3370         }
3371
3372         int rc = -1, ret = false;
3373         int error = EMAIL_ERROR_NONE;
3374
3375         DB_STMT hStmt = NULL;
3376         char sql_query_string[QUERY_SIZE] = {0, };
3377
3378
3379         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3380         EMSTORAGE_START_READ_TRANSACTION(transaction);
3381         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_account_tbl");
3382         EM_DEBUG_LOG_SEC("SQL STMT [%s]", sql_query_string);
3383         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string,
3384                                                           EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3385         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
3386         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3387                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3388
3389         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3390         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3391                 ("sqlite3_step fail:%d", rc));
3392
3393         *count = sqlite3_column_int(hStmt, 0);
3394
3395         ret = true;
3396
3397 FINISH_OFF:
3398
3399         if (hStmt != NULL) {
3400                 rc = sqlite3_finalize(hStmt);
3401                 hStmt = NULL;
3402                 if (rc != SQLITE_OK) {
3403                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3404                         error = EMAIL_ERROR_DB_FAILURE;
3405                 }
3406         }
3407
3408         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3409
3410         if (err_code != NULL)
3411                 *err_code = error;
3412
3413         EM_DEBUG_FUNC_END("ret [%d]", ret);
3414         return ret;
3415 }
3416
3417 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)
3418 {
3419         EM_DEBUG_FUNC_BEGIN();
3420
3421         int i = 0, count = 0, rc = -1, ret = false;
3422         int field_index = 0;
3423         int sql_len = 0;
3424         int error = EMAIL_ERROR_NONE;
3425         emstorage_account_tbl_t *p_data_tbl = NULL;
3426         char sql_query_string[QUERY_SIZE] = {0, };
3427         char *sql = "SELECT count(*) FROM mail_account_tbl;";
3428         char **result;
3429         sqlite3 *local_db_handle = NULL;
3430         DB_STMT hStmt = NULL;
3431
3432         if (!select_num || !account_list) {
3433                 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, account_list);
3434                 if (err_code != NULL)
3435                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3436                 return false;
3437         }
3438
3439         local_db_handle = emstorage_get_db_connection(multi_user_name);
3440
3441         EMSTORAGE_START_READ_TRANSACTION(transaction);
3442
3443         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
3444         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3445                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
3446
3447         count = atoi(result[1]);
3448         sqlite3_free_table(result);
3449
3450         EM_DEBUG_LOG_DEV("count = %d", rc);
3451
3452         if (count <= 0) {
3453                 EM_DEBUG_LOG("no account found...");
3454                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3455                 ret = true;
3456                 goto FINISH_OFF;
3457         }
3458         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
3459         sql_len = EM_SAFE_STRLEN(sql_query_string);
3460
3461         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
3462                 "account_id, "
3463                 "account_name, "
3464                 "logo_icon_path, "
3465                 "user_data, "
3466                 "user_data_length, "
3467                 "account_svc_id, "
3468                 "sync_status, "
3469                 "sync_disabled, "
3470                 "default_mail_slot_size, "
3471                 "roaming_option, "
3472                 "color_label, "
3473                 "user_display_name, "
3474                 "user_email_address, "
3475                 "reply_to_address, "
3476                 "return_address, "
3477                 "incoming_server_type, "
3478                 "incoming_server_address, "
3479                 "incoming_server_port_number, "
3480                 "incoming_server_user_name, "
3481                 "incoming_server_password, "
3482                 "incoming_server_secure_connection, "
3483                 "incoming_server_authentication_method, "
3484                 "retrieval_mode, "
3485                 "keep_mails_on_pop_server_after_download, "
3486                 "check_interval, "
3487                 "auto_download_size, "
3488                 "peak_interval, "
3489                 "peak_days, "
3490                 "peak_start_time, "
3491                 "peak_end_time, "
3492                 "outgoing_server_type, "
3493                 "outgoing_server_address, "
3494                 "outgoing_server_port_number, "
3495                 "outgoing_server_user_name, "
3496                 "outgoing_server_password, "
3497                 "outgoing_server_secure_connection, "
3498                 "outgoing_server_need_authentication, "
3499                 "outgoing_server_use_same_authenticator, "
3500                 "priority, "
3501                 "keep_local_copy, "
3502                 "req_delivery_receipt, "
3503                 "req_read_receipt, "
3504                 "download_limit, "
3505                 "block_address, "
3506                 "block_subject, "
3507                 "display_name_from, "
3508                 "reply_with_body, "
3509                 "forward_with_files, "
3510                 "add_myname_card, "
3511                 "add_signature, "
3512                 "signature, "
3513                 "add_my_address_to_bcc, "
3514                 "notification_status, "
3515                 "vibrate_status, "
3516                 "display_content_status, "
3517                 "default_ringtone_status, "
3518                 "alert_ringtone_path, "
3519                 "auto_resend_times, "
3520                 "outgoing_server_size_limit, "
3521                 "wifi_auto_download, "
3522                 "pop_before_smtp, "
3523                 "incoming_server_requires_apop, "
3524                 "smime_type, "
3525                 "certificate_path, "
3526                 "cipher_type, "
3527                 "digest_type"
3528         );
3529
3530         sql_len = EM_SAFE_STRLEN(sql_query_string);
3531
3532         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " FROM mail_account_tbl ORDER BY account_id");
3533
3534         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3535
3536         EM_DEBUG_LOG_DEV("After sqlite3_prepare_v2 hStmt = %p", hStmt);
3537         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3538                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3539
3540         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3541         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3542                 ("sqlite3_step fail:%d", rc));
3543         if (rc == SQLITE_DONE) {
3544                 EM_DEBUG_EXCEPTION("no account found...");
3545
3546                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3547                 count = 0;
3548                 ret = true;
3549                 goto FINISH_OFF;
3550         }
3551
3552         if (!(p_data_tbl = (emstorage_account_tbl_t*)malloc(sizeof(emstorage_account_tbl_t) * count))) {
3553                 EM_DEBUG_EXCEPTION("malloc failed...");
3554                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3555                 goto FINISH_OFF;
3556         }
3557         memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * count);
3558         for (i = 0; i < count; i++) {
3559                 /*  get recordset */
3560                 field_index = 0;
3561                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_id), field_index++);
3562                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].account_name), 0, field_index++);
3563                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].logo_icon_path), 0, field_index++);
3564                 _get_stmt_field_data_blob(hStmt, &(p_data_tbl[i].user_data), field_index++);
3565                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].user_data_length), field_index++);
3566                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_svc_id), field_index++);
3567                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_status), field_index++);
3568                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_disabled), field_index++);
3569                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].default_mail_slot_size), field_index++);
3570                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].roaming_option), field_index++);
3571                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].color_label), field_index++);
3572                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_display_name), 0, field_index++);
3573                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_email_address), 0, field_index++);
3574                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].reply_to_address), 0, field_index++);
3575                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].return_address), 0, field_index++);
3576                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].incoming_server_type), field_index++);
3577                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_address), 0, field_index++);
3578                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_port_number), field_index++);
3579                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_user_name), 0, field_index++);
3580                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_password), 0, field_index++);
3581                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_secure_connection), field_index++);
3582                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].incoming_server_authentication_method), field_index++);
3583                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].retrieval_mode), field_index++);
3584                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].keep_mails_on_pop_server_after_download), field_index++);
3585                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].check_interval), field_index++);
3586                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_download_size), field_index++);
3587                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_interval), field_index++);
3588                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_days), field_index++);
3589                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_start_time), field_index++);
3590                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_end_time), field_index++);
3591                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].outgoing_server_type), field_index++);
3592                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_address), 0, field_index++);
3593                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_port_number), field_index++);
3594                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_user_name), 0, field_index++);
3595                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_password), 0, field_index++);
3596                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_secure_connection), field_index++);
3597                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_need_authentication), field_index++);
3598                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_use_same_authenticator), field_index++);
3599                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.priority), field_index++);
3600                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.keep_local_copy), field_index++);
3601                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_delivery_receipt), field_index++);
3602                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_read_receipt), field_index++);
3603                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.download_limit), field_index++);
3604                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_address), field_index++);
3605                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_subject), field_index++);
3606                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.display_name_from), 0, field_index++);
3607                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.reply_with_body), field_index++);
3608                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.forward_with_files), field_index++);
3609                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_myname_card), field_index++);
3610                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_signature), field_index++);
3611                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.signature), 0, field_index++);
3612                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.add_my_address_to_bcc), field_index++);
3613                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.notification_status), field_index++);
3614                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.vibrate_status), field_index++);
3615                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.display_content_status), field_index++);
3616                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.default_ringtone_status), field_index++);
3617                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.alert_ringtone_path), 0, field_index++);
3618                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_resend_times), field_index++);
3619                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_size_limit), field_index++);
3620                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].wifi_auto_download), field_index++);
3621                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].pop_before_smtp), field_index++);
3622                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_requires_apop), field_index++);
3623                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].smime_type), field_index++);
3624                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].certificate_path), 0, field_index++);
3625                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].cipher_type), field_index++);
3626                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].digest_type), field_index++);
3627
3628                 /* EAS passwd is not accessible */
3629                 if (with_password == true && p_data_tbl[i].incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
3630                         /*  get password from the secure storage */
3631                         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3632                         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3633
3634                         EM_SAFE_FREE(p_data_tbl[i].incoming_server_password);
3635                         EM_SAFE_FREE(p_data_tbl[i].outgoing_server_password);
3636
3637                         /*  get password file name */
3638                         error = _get_password_file_name(multi_user_name, p_data_tbl[i].account_id,
3639                                                    recv_password_file_name,
3640                                                    send_password_file_name);
3641                         if (error != EMAIL_ERROR_NONE) {
3642                                 EM_DEBUG_EXCEPTION("_get_password_file_name error [%d]", error);
3643                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3644                                 goto FINISH_OFF;
3645                         }
3646
3647                         /*  read password from secure storage */
3648                         error = _read_password_from_secure_storage(recv_password_file_name,
3649                                           &(p_data_tbl[i].incoming_server_password));
3650                         if (error < 0) {
3651                                 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s] error [%d]",
3652                                                     recv_password_file_name, error);
3653                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3654                                 goto FINISH_OFF;
3655                         }
3656                         error = _read_password_from_secure_storage(send_password_file_name,
3657                                           &(p_data_tbl[i].outgoing_server_password));
3658                         if (error < 0) {
3659                                 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s]  error [%d]",
3660                                                      send_password_file_name, error);
3661                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3662                                 goto FINISH_OFF;
3663                         }
3664                 }
3665
3666                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3667                 EM_DEBUG_LOG_DEV("after sqlite3_step(), i = %d, rc = %d.", i,  rc);
3668                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3669                         ("sqlite3_step fail:%d", rc));
3670         }
3671
3672         ret = true;
3673
3674 FINISH_OFF:
3675         if (ret == true) {
3676                 *account_list = p_data_tbl;
3677                 *select_num = count;
3678                 EM_DEBUG_LOG_DEV("COUNT : %d", count);
3679         } else if (p_data_tbl != NULL)
3680                 emstorage_free_account(&p_data_tbl, count, NULL);
3681
3682         if (hStmt != NULL) {
3683                 rc = sqlite3_finalize(hStmt);
3684                 hStmt = NULL;
3685                 if (rc != SQLITE_OK) {
3686                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3687                         error = EMAIL_ERROR_DB_FAILURE;
3688                 }
3689         }
3690
3691         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3692
3693         if (err_code != NULL)
3694                 *err_code = error;
3695
3696         EM_DEBUG_FUNC_END("ret [%d]", ret);
3697         return ret;
3698 }
3699
3700 INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(char *multi_user_name,
3701                                                                                                                         char *server_mail_id,
3702                                                                                                                         int mailbox_id,
3703                                                                                                                         emstorage_mail_tbl_t **mail,
3704                                                                                                                         int transaction,
3705                                                                                                                         int *err_code)
3706 {
3707         EM_DEBUG_FUNC_BEGIN_SEC("mailbox_id [%d], server_mail_id[%s], mail[%p], transaction[%d], err_code[%p]",
3708                                                         mailbox_id, server_mail_id, mail, transaction, err_code);
3709
3710         int ret = false, error = EMAIL_ERROR_NONE, result_count;
3711         char conditional_clause[QUERY_SIZE] = {0, };
3712         emstorage_mail_tbl_t* p_data_tbl = NULL;
3713
3714         if (!server_mail_id || !mail) {
3715                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3716                 error = EMAIL_ERROR_INVALID_PARAM;
3717                 goto FINISH_OFF;
3718         }
3719
3720         sqlite3_snprintf(sizeof(conditional_clause), conditional_clause, "WHERE server_mail_id = '%q'", server_mail_id);
3721
3722         if (mailbox_id > 0) {
3723                 SNPRINTF(conditional_clause + strlen(conditional_clause),
3724                                         QUERY_SIZE - strlen(conditional_clause),
3725                                         " AND mailbox_id = %d",
3726                                         mailbox_id);
3727         }
3728
3729         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
3730         if (!emstorage_query_mail_tbl(multi_user_name,
3731                                                                         conditional_clause,
3732                                                                         transaction,
3733                                                                         &p_data_tbl,
3734                                                                         &result_count,
3735                                                                         &error)) {
3736                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3737                 goto FINISH_OFF;
3738         }
3739
3740         ret = true;
3741
3742 FINISH_OFF:
3743
3744         if (ret == true)
3745                 *mail = p_data_tbl;
3746
3747         if (err_code != NULL)
3748                 *err_code = error;
3749
3750         EM_DEBUG_FUNC_END("ret [%d]", ret);
3751         return ret;
3752 }
3753
3754
3755 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)
3756 {
3757         int cur_clause = 0, conditional_clause_count = 0, i = 0;
3758
3759         if (account_id < ALL_ACCOUNT) {
3760                 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3761                 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3762         }
3763
3764         /*  where clause */
3765         if (account_id != ALL_ACCOUNT) {
3766                 cur_clause += (conditional_clause_count++ == 0) ?
3767                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE account_id = %d", account_id) :
3768                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND account_id = %d", account_id);
3769         }
3770
3771         if (mailbox_id > 0) {
3772                 cur_clause += (conditional_clause_count++ == 0) ?
3773                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_id = %d", mailbox_id) :
3774                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_id = %d", mailbox_id);
3775         }
3776
3777         if (thread_id > 0) {
3778                 cur_clause += (conditional_clause_count++ == 0) ?
3779                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_id = %d ", thread_id) :
3780                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_id = %d ", thread_id);
3781         } else if (thread_id == EMAIL_LIST_TYPE_THREAD) {
3782                 cur_clause += (conditional_clause_count++ == 0) ?
3783                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_item_count > 0") :
3784                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_item_count > 0");
3785         } else if (thread_id == EMAIL_LIST_TYPE_LOCAL) {
3786                 cur_clause += (conditional_clause_count++ == 0) ?
3787                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE server_mail_status = 0") :
3788                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND server_mail_status = 0");
3789         } else if (thread_id == EMAIL_LIST_TYPE_UNREAD) {
3790                 cur_clause += (conditional_clause_count++ == 0) ?
3791                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_seen_field == 0") :
3792                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_seen_field == 0");
3793         }
3794
3795         /*  EM_DEBUG_LOG("where clause added [%s]", conditional_clause_string); */
3796         if (addr_list && addr_list->address_count > 0) {
3797                 if (!addr_list->address_type) {
3798                         cur_clause += (conditional_clause_count++ == 0) ?
3799                                 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE email_address_sender IN(\"%s\"", addr_list->address_list[0]) :
3800                                 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND email_address_sender IN(\"%s\"", addr_list->address_list[0]);
3801
3802                         for (i = 1; i < addr_list->address_count; i++)
3803                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3804
3805                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3806                 } else {
3807                         cur_clause += (conditional_clause_count++ == 0) ?
3808                                 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]) :
3809                                 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]);
3810
3811                         for (i = 1; i < addr_list->address_count; i++)
3812                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3813
3814                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3815                 }
3816         }
3817
3818         if (input_except_delete_flagged_mails) {
3819                 cur_clause += (conditional_clause_count++ == 0) ?
3820                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_deleted_field = 0") :
3821                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_deleted_field = 0");
3822         }
3823
3824         if (search_value) {
3825                 switch (search_type) {
3826                         case EMAIL_SEARCH_FILTER_SUBJECT:
3827                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3828                                         " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3829                                 break;
3830                         case EMAIL_SEARCH_FILTER_SENDER:
3831                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3832                                         " %s  ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3833                                         ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3834                                 break;
3835                         case EMAIL_SEARCH_FILTER_RECIPIENT:
3836                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3837                                         " %s ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3838                                         "       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3839                                         "       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3840                                         ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value);
3841                                 break;
3842                         case EMAIL_SEARCH_FILTER_ALL:
3843                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3844                                         " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\' "
3845                                         "       OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3846                                         "                       OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3847                                         "                       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3848                                         "                       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3849                                         "               ) "
3850                                         "       )"
3851                                         ")", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value, search_value, search_value);
3852                                 break;
3853                 }
3854         }
3855
3856         /*  EM_DEBUG_LOG("where clause [%s]", conditional_clause_string); */
3857         static char sorting_str[][50] = {
3858                          " ORDER BY date_time DESC",                                             /* case EMAIL_SORT_DATETIME_HIGH: */
3859                          " ORDER BY date_time ASC",                                               /* case EMAIL_SORT_DATETIME_LOW: */
3860                          " ORDER BY full_address_from DESC, date_time DESC", /* case EMAIL_SORT_SENDER_HIGH: */
3861                          " ORDER BY full_address_from ASC, date_time DESC",  /* case EMAIL_SORT_SENDER_LOW: */
3862                          " ORDER BY full_address_to DESC, date_time DESC",   /* case EMAIL_SORT_RCPT_HIGH: */
3863                          " ORDER BY full_address_to ASC, date_time DESC",       /* case EMAIL_SORT_RCPT_LOW: */
3864                          " ORDER BY subject DESC, date_time DESC",                 /* case EMAIL_SORT_SUBJECT_HIGH: */
3865                          " ORDER BY subject ASC, date_time DESC",                       /* case EMAIL_SORT_SUBJECT_LOW: */
3866                          " ORDER BY priority DESC, date_time DESC",               /* case EMAIL_SORT_PRIORITY_HIGH: */
3867                          " ORDER BY priority ASC, date_time DESC",                 /* case EMAIL_SORT_PRIORITY_LOW: */
3868                          " ORDER BY attachment_count DESC, date_time DESC",  /* case EMAIL_SORT_ATTACHMENT_HIGH: */
3869                          " ORDER BY attachment_count ASC, date_time DESC",   /* case EMAIL_SORT_ATTACHMENT_LOW: */
3870                          " ORDER BY lock_status DESC, date_time DESC",     /* case EMAIL_SORT_FAVORITE_HIGH: */
3871                          " ORDER BY lock_status ASC, date_time DESC",           /* case EMAIL_SORT_FAVORITE_LOW: */
3872                          " ORDER BY mailbox_id DESC, date_time DESC",     /* case EMAIL_SORT_MAILBOX_ID_HIGH: */
3873                          " ORDER BY mailbox_id ASC, date_time DESC",            /* case EMAIL_SORT_MAILBOX_ID_LOW: */
3874                          " ORDER BY flags_flagged_field DESC, date_time DESC",  /* case EMAIL_SORT_FLAGGED_FLAG_HIGH: */
3875                          " ORDER BY flags_flagged_field ASC, date_time DESC",   /* case EMAIL_SORT_FLAGGED_FLAG_LOW: */
3876                          " ORDER BY flags_seen_field DESC, date_time DESC",     /* case EMAIL_SORT_SEEN_FLAG_HIGH: */
3877                          " ORDER BY flags_seen_field ASC, date_time DESC"       /* case EMAIL_SORT_SEEN_FLAG_LOW: */
3878                          };
3879
3880         if (sorting < EMAIL_SORT_END && sorting >= 0)
3881                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " %s", sorting_str[sorting]);
3882         else
3883                 EM_DEBUG_LOG(" Invalid Sorting order ");
3884
3885         /*  limit clause */
3886         if (start_index != -1 && limit_count != -1)
3887                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " LIMIT %d, %d", start_index, limit_count);
3888
3889         return true;
3890 }
3891
3892
3893 /**
3894   *     emstorage_get_mail_list - Get the mail list information.
3895   *
3896   *
3897   */
3898 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)
3899 {
3900         EM_DEBUG_FUNC_BEGIN();
3901         EM_PROFILE_BEGIN(emstorage_get_mail_list_func);
3902
3903         int ret = false, error = EMAIL_ERROR_NONE;
3904         char conditional_clause_string[QUERY_SIZE] = { 0, };
3905
3906         if (account_id < ALL_ACCOUNT) {
3907                 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3908                 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3909         }
3910         EM_IF_NULL_RETURN_VALUE(result_count, false);
3911
3912         _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);
3913
3914         EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
3915
3916         if (!emstorage_query_mail_list(multi_user_name, conditional_clause_string, transaction, mail_list, result_count, &error)) {
3917                 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3918                 goto FINISH_OFF;
3919         }
3920
3921         ret = true;
3922
3923 FINISH_OFF:
3924         if (err_code != NULL)
3925                 *err_code = error;
3926
3927         EM_PROFILE_END(emstorage_get_mail_list_func);
3928         EM_DEBUG_FUNC_END("ret [%d]", ret);
3929         return ret;
3930 }
3931
3932
3933 /**
3934   *     emstorage_get_mails - Get the Mail list information based on mailbox_name name
3935   *
3936   *
3937   */
3938 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)
3939 {
3940         EM_DEBUG_FUNC_BEGIN();
3941         EM_PROFILE_BEGIN(emStorageGetMails);
3942
3943         int count = 0, ret = false, error = EMAIL_ERROR_NONE;
3944         emstorage_mail_tbl_t *p_data_tbl = NULL;
3945         char conditional_clause_string[QUERY_SIZE] = {0, };
3946
3947         EM_IF_NULL_RETURN_VALUE(mail_list, false);
3948         EM_IF_NULL_RETURN_VALUE(result_count, false);
3949
3950         if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3951                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3952                 if (err_code != NULL)
3953                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3954                 return false;
3955         }
3956
3957         _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);
3958
3959         EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
3960
3961         if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause_string, transaction, &p_data_tbl, &count,  &error)) {
3962                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3963                 goto FINISH_OFF;
3964         }
3965
3966         ret = true;
3967
3968 FINISH_OFF:
3969
3970         if (ret == true) {
3971                 *mail_list = p_data_tbl;
3972                 *result_count = count;
3973                 EM_DEBUG_LOG("COUNT : %d", count);
3974         } else if (p_data_tbl != NULL)
3975                 emstorage_free_mail(&p_data_tbl, count, NULL);
3976
3977         if (err_code != NULL)
3978                 *err_code = error;
3979
3980         EM_PROFILE_END(emStorageGetMails);
3981         EM_DEBUG_FUNC_END("ret [%d]", ret);
3982         return ret;
3983 }
3984
3985
3986
3987 /**
3988   *     emstorage_get_searched_mail_list - Get the mail list information after filtering
3989   *
3990   *
3991   */
3992 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)
3993 {
3994         EM_DEBUG_FUNC_BEGIN();
3995
3996         int ret = false, error = EMAIL_ERROR_NONE;
3997         char conditional_clause[QUERY_SIZE] = {0, };
3998         char *temp_search_value = NULL;
3999         char *temp_search_value2 = NULL;
4000
4001         EM_IF_NULL_RETURN_VALUE(mail_list, false);
4002         EM_IF_NULL_RETURN_VALUE(result_count, false);
4003
4004         if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
4005                 EM_DEBUG_EXCEPTION("select_num[%p], Mail_list[%p]", result_count, mail_list);
4006                 error = EMAIL_ERROR_INVALID_PARAM;
4007                 goto FINISH_OFF;
4008         }
4009
4010         temp_search_value = em_replace_all_string((char*)search_value, "_", "\\_");
4011         temp_search_value2 = em_replace_all_string(temp_search_value, "%", "\\%");
4012
4013         _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);
4014
4015         EM_DEBUG_LOG("conditional_clause[%s]", conditional_clause);
4016
4017         if (!emstorage_query_mail_list(multi_user_name, conditional_clause, transaction, mail_list, result_count, &error)) {
4018                 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
4019                 goto FINISH_OFF;
4020         }
4021
4022         ret = true;
4023
4024 FINISH_OFF:
4025         EM_DEBUG_LOG("emstorage_get_searched_mail_list finish off");
4026
4027         if (err_code != NULL)
4028                 *err_code = error;
4029
4030         EM_SAFE_FREE(temp_search_value);
4031         EM_SAFE_FREE(temp_search_value2);
4032
4033         EM_DEBUG_FUNC_END("ret [%d]", ret);
4034         return ret;
4035 }
4036
4037
4038 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name)
4039 {
4040         EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
4041
4042         if (account_id <= 0 || !recv_password_file_name || !send_password_file_name) {
4043                 EM_DEBUG_EXCEPTION("Invalid parameter");
4044                 return EMAIL_ERROR_INVALID_PARAM;
4045         }
4046
4047     EM_DEBUG_LOG("MULTI_USER_NAME : [%s]", multi_user_name);
4048
4049         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
4050                 snprintf(recv_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_recv_%s", account_id, multi_user_name);
4051                 snprintf(send_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_send_%s", account_id, multi_user_name);
4052         } else {
4053                 snprintf(recv_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_recv", account_id);
4054                 snprintf(send_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_send", account_id);
4055         }
4056
4057         EM_DEBUG_FUNC_END();
4058         return EMAIL_ERROR_NONE;
4059 }
4060
4061 static int _read_password_from_secure_storage(char *file_name, char **password)
4062 {
4063         EM_DEBUG_FUNC_BEGIN_SEC("file_name[%s]", file_name);
4064
4065         if (!file_name || !password) {
4066                 EM_DEBUG_EXCEPTION("Invalid Parameter");
4067                 return EMAIL_ERROR_INVALID_PARAM;
4068         }
4069
4070         int ret = EMAIL_ERROR_NONE;
4071
4072         ret = emcore_get_password_in_key_manager(file_name, password);
4073         if (ret != EMAIL_ERROR_NONE) {
4074                 EM_DEBUG_EXCEPTION("emcore_get_password_in_key_manager failed : [%d]", ret);
4075                 goto FINISH_OFF;
4076         }
4077
4078 FINISH_OFF:
4079
4080         EM_DEBUG_FUNC_END("ret [%d]", ret);
4081         return ret;
4082 }
4083
4084 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)
4085 {
4086         EM_DEBUG_FUNC_BEGIN("account_id[%d], pulloption[%d], account[%p], transaction[%d], err_code[%p]", account_id, pulloption, account, transaction, err_code);
4087
4088         if (!account) {
4089                 EM_DEBUG_EXCEPTION("account_id[%d], account[%p]", account_id, account);
4090                 if (err_code != NULL)
4091                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4092                 return false;
4093         }
4094
4095         int ret = false;
4096         int error = EMAIL_ERROR_NONE;
4097         emstorage_account_tbl_t* p_data_tbl = NULL;
4098         DB_STMT hStmt = NULL;
4099         char sql_query_string[QUERY_SIZE] = {0, };
4100         int rc = -1;
4101         int sql_len = 0;
4102         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4103         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4104
4105         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4106
4107         EMSTORAGE_START_READ_TRANSACTION(transaction);
4108
4109         /*  Make query string */
4110         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
4111         sql_len = EM_SAFE_STRLEN(sql_query_string);
4112
4113         if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4114                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4115                         "incoming_server_type,"
4116                         "incoming_server_address,"
4117                         "user_email_address,"
4118                         "incoming_server_user_name,"
4119                         "retrieval_mode,"
4120                         "incoming_server_port_number,"
4121                         "incoming_server_secure_connection,"
4122                         "incoming_server_authentication_method,"
4123                         "outgoing_server_type,"
4124                         "outgoing_server_address,"
4125                         "outgoing_server_port_number,"
4126                         "outgoing_server_need_authentication,"
4127                         "outgoing_server_secure_connection,"
4128                         "outgoing_server_user_name,"
4129                         "user_display_name,"
4130                         "reply_to_address,"
4131                         "return_address,"
4132                         "account_id,"
4133                         "keep_mails_on_pop_server_after_download,"
4134                         "auto_download_size,"
4135                         "peak_interval,"
4136                         "peak_days,"
4137                         "peak_start_time,"
4138                         "peak_end_time,"
4139                         "outgoing_server_use_same_authenticator,"
4140                         "auto_resend_times,"
4141                         "outgoing_server_size_limit,"
4142                         "wifi_auto_download,"
4143                         "pop_before_smtp,"
4144                         "incoming_server_requires_apop,"
4145                         "logo_icon_path,"
4146                         "user_data,"
4147                         "user_data_length,"
4148                         "color_label,"
4149                         "check_interval,"
4150                         "sync_status,");
4151                 sql_len = EM_SAFE_STRLEN(sql_query_string);
4152         }
4153
4154         if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME) {
4155                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " account_name, ");
4156                 sql_len = EM_SAFE_STRLEN(sql_query_string);
4157         }
4158
4159         /*  get from secure storage, not from db */
4160         if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4161                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4162                         "priority,"
4163                         "keep_local_copy,"
4164                         "req_delivery_receipt,"
4165                         "req_read_receipt,"
4166                         "download_limit,"
4167                         "block_address,"
4168                         "block_subject,"
4169                         "display_name_from,"
4170                         "reply_with_body,"
4171                         "forward_with_files,"
4172                         "add_myname_card,"
4173                         "add_signature,"
4174                         "signature,"
4175                         "add_my_address_to_bcc,"
4176                         "notification_status,"
4177                         "vibrate_status,"
4178                         "display_content_status,"
4179                         "default_ringtone_status,"
4180                         "alert_ringtone_path,"
4181                         "account_svc_id,"
4182                         "sync_disabled,"
4183                         "default_mail_slot_size,"
4184                         "roaming_option,"
4185                         "smime_type,"
4186                         "certificate_path,"
4187                         "cipher_type,"
4188                         "digest_type,");
4189
4190                 sql_len = EM_SAFE_STRLEN(sql_query_string);
4191         }
4192
4193         /*  dummy value, FROM WHERE clause */
4194         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "0 FROM mail_account_tbl WHERE account_id = %d", account_id);
4195
4196         /*  FROM clause */
4197         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
4198
4199         /*  execute a sql and count rows */
4200         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4201         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4202                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4203
4204         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4205         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4206                 ("sqlite3_step fail:%d", rc));
4207
4208         if (rc == SQLITE_DONE) {
4209                 EM_DEBUG_EXCEPTION("no matched account found...");
4210                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4211                 goto FINISH_OFF;
4212         }
4213
4214         /*  Assign query result to structure */
4215         if (!(p_data_tbl = (emstorage_account_tbl_t *)malloc(sizeof(emstorage_account_tbl_t) * 1))) {
4216                 EM_DEBUG_EXCEPTION("malloc failed...");
4217                 error = EMAIL_ERROR_OUT_OF_MEMORY;
4218                 goto FINISH_OFF;
4219         }
4220
4221         memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * 1);
4222         int col_index = 0;
4223
4224         if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4225                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_type), col_index++);
4226                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_address),  0, col_index++);
4227                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_email_address), 0, col_index++);
4228                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_user_name), 0, col_index++);
4229                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->retrieval_mode), col_index++);
4230                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_port_number), col_index++);
4231                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_secure_connection), col_index++);
4232                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_authentication_method), col_index++);
4233                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->outgoing_server_type), col_index++);
4234                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_address), 0, col_index++);
4235                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_port_number), col_index++);
4236                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_need_authentication), col_index++);
4237                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_secure_connection), col_index++);
4238                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_user_name), 0, col_index++);
4239                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_display_name), 0, col_index++);
4240                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->reply_to_address), 0, col_index++);
4241                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->return_address), 0, col_index++);
4242                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
4243                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->keep_mails_on_pop_server_after_download), col_index++);
4244                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_download_size), col_index++);
4245                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_interval), col_index++);
4246                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_days), col_index++);
4247                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_start_time), col_index++);
4248                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_end_time), col_index++);
4249                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_use_same_authenticator), col_index++);
4250                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_resend_times), col_index++);
4251                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_size_limit), col_index++);
4252                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->wifi_auto_download), col_index++);
4253                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->pop_before_smtp), col_index++);
4254                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_requires_apop), col_index++);
4255                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->logo_icon_path), 0, col_index++);
4256                 _get_stmt_field_data_blob(hStmt, &p_data_tbl->user_data, col_index++);
4257                 _get_stmt_field_data_int(hStmt, &p_data_tbl->user_data_length, col_index++);
4258                 _get_stmt_field_data_int(hStmt, &p_data_tbl->color_label, col_index++);
4259                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->check_interval), col_index++);
4260                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->sync_status), col_index++);
4261         }
4262
4263         if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME)
4264                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->account_name), 0, col_index++);
4265
4266         if (pulloption & EMAIL_ACC_GET_OPT_PASSWORD) {
4267                 /*  get password file name */
4268                 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) {
4269                         EM_DEBUG_EXCEPTION("_get_password_file_name failed [%d]", error);
4270                         goto FINISH_OFF;
4271                 }
4272
4273                 /*  read password from secure storage */
4274                 if ((error = _read_password_from_secure_storage(recv_password_file_name, &(p_data_tbl->incoming_server_password))) < 0) {
4275                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4276                         goto FINISH_OFF;
4277                 }
4278
4279                 EM_DEBUG_LOG_DEV("recv_password_file_name[%s], password[%s]", recv_password_file_name,  p_data_tbl->incoming_server_password);
4280
4281                 if (p_data_tbl->outgoing_server_use_same_authenticator == 0) {
4282                         if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl->outgoing_server_password))) < 0) {
4283                                 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4284                                 goto FINISH_OFF;
4285                         }
4286                         EM_DEBUG_LOG_DEV("send_password_file_name[%s], password[%s]", send_password_file_name,  p_data_tbl->outgoing_server_password);
4287                 }
4288         }
4289
4290         if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4291                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.priority), col_index++);
4292                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.keep_local_copy), col_index++);
4293                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_delivery_receipt), col_index++);
4294                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_read_receipt), col_index++);
4295                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.download_limit), col_index++);
4296                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_address), col_index++);
4297                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_subject), col_index++);
4298                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.display_name_from), 0, col_index++);
4299                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.reply_with_body), col_index++);
4300                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.forward_with_files), col_index++);
4301                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_myname_card), col_index++);
4302                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_signature), col_index++);
4303                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.signature), 0, col_index++);
4304                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.add_my_address_to_bcc), col_index++);
4305                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.notification_status), col_index++);
4306                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.vibrate_status), col_index++);
4307                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.display_content_status), col_index++);
4308                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.default_ringtone_status), col_index++);
4309                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.alert_ringtone_path), 0, col_index++);
4310                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_svc_id), col_index++);
4311                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->sync_disabled), col_index++);
4312                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->default_mail_slot_size), col_index++);
4313                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->roaming_option), col_index++);
4314                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->smime_type), col_index++);
4315                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->certificate_path), 0, col_index++);
4316                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->cipher_type), col_index++);
4317                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->digest_type), col_index++);
4318         }
4319
4320         ret = true;
4321
4322 FINISH_OFF:
4323         if (ret == true)
4324                 *account = p_data_tbl;
4325         else {
4326                 if (p_data_tbl)
4327                         emstorage_free_account((emstorage_account_tbl_t **)&p_data_tbl, 1, NULL);
4328         }
4329         if (hStmt != NULL) {
4330                 rc = sqlite3_finalize(hStmt);
4331                 if (rc != SQLITE_OK) {
4332                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4333                         error = EMAIL_ERROR_DB_FAILURE;
4334                 }
4335         }
4336
4337         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4338
4339         if (err_code != NULL)
4340                 *err_code = error;
4341
4342         EM_DEBUG_FUNC_END("ret [%d]", ret);
4343         return ret;
4344 }
4345
4346 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)
4347 {
4348         EM_DEBUG_FUNC_BEGIN("account_id[%d], password_length[%p], err_code[%p]", account_id, password_length, err_code);
4349
4350         if (account_id <= 0 || password_length == NULL) {
4351                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4352                 if (err_code != NULL)
4353                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4354                 return false;
4355         }
4356
4357         int ret = false;
4358         int error = EMAIL_ERROR_NONE;
4359         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4360         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4361         char *temp_password = NULL;
4362
4363
4364         /*  get password file name */
4365         if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4366                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4367                 goto FINISH_OFF;
4368         }
4369
4370         /*  read password from secure storage */
4371         if (password_type == EMAIL_GET_INCOMING_PASSWORD_LENGTH) {
4372                 if ((error = _read_password_from_secure_storage(recv_password_file_name, &temp_password)) < 0 || !temp_password) {
4373                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed...");
4374                         goto FINISH_OFF;
4375                 }
4376         } else if (password_type == EMAIL_GET_OUTGOING_PASSWORD_LENGTH) {
4377                 if ((error = _read_password_from_secure_storage(send_password_file_name, &temp_password)) < 0 || !temp_password) {
4378                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed...");
4379                         goto FINISH_OFF;
4380                 }
4381         } else {
4382                 EM_DEBUG_LOG("Invalid password type");
4383                 if (err_code != NULL)
4384                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4385                 goto FINISH_OFF;
4386         }
4387
4388         *password_length = EM_SAFE_STRLEN(temp_password);
4389
4390         EM_DEBUG_LOG_SEC("recv_password_file_name[%s], *password_length[%d]", recv_password_file_name,  *password_length);
4391
4392         ret = true;
4393
4394 FINISH_OFF:
4395         EM_SAFE_FREE(temp_password);
4396
4397         if (err_code != NULL)
4398                 *err_code = error;
4399
4400         EM_DEBUG_FUNC_END("ret [%d]", ret);
4401         return ret;
4402 }
4403
4404 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)
4405 {
4406         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);
4407
4408         int err = EMAIL_ERROR_NONE;
4409         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4410         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4411
4412         if (input_incoming_server_password == NULL && input_outgoing_server_password == NULL) {
4413                 EM_DEBUG_EXCEPTION_SEC("Invalid param");
4414                 err = EMAIL_ERROR_INVALID_PARAM;
4415                 goto FINISH_OFF;
4416         }
4417
4418         /*  get password file name */
4419         if ((err = _get_password_file_name(multi_user_name,
4420                                                                                 input_account_id,
4421                                                                                 recv_password_file_name,
4422                                                                                 send_password_file_name)) != EMAIL_ERROR_NONE) {
4423                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4424                 goto FINISH_OFF;
4425         }
4426
4427         EM_DEBUG_LOG_SEC("recv_password_file_name [%s] input_incoming_server_password [%s]",
4428                                                 recv_password_file_name, input_incoming_server_password);
4429         EM_DEBUG_LOG_SEC("send_password_file_name [%s] input_outgoing_server_password [%s]",
4430                                                 send_password_file_name, input_outgoing_server_password);
4431
4432         if (input_incoming_server_password) {
4433                 err = emcore_remove_password_in_key_manager(recv_password_file_name);
4434                 if (err != EMAIL_ERROR_NONE) {
4435                         EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", recv_password_file_name);
4436                         goto FINISH_OFF;
4437                 }
4438
4439                 /*  save recv passwords to the secure storage */
4440                 err = emcore_add_password_in_key_manager(recv_password_file_name, input_incoming_server_password);
4441                 if (err != EMAIL_ERROR_NONE) {
4442                         EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4443                         goto FINISH_OFF;
4444                 }
4445         }
4446
4447         if (input_outgoing_server_password) {
4448                 err = emcore_remove_password_in_key_manager(send_password_file_name);
4449                 if (err != EMAIL_ERROR_NONE) {
4450                         EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", send_password_file_name);
4451                         goto FINISH_OFF;
4452                 }
4453
4454                 /*  save send passwords to the secure storage */
4455                 err = emcore_add_password_in_key_manager(send_password_file_name, input_outgoing_server_password);
4456                 if (err != EMAIL_ERROR_NONE) {
4457                         EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4458                         goto FINISH_OFF;
4459                 }
4460         }
4461 FINISH_OFF:
4462
4463         EM_DEBUG_FUNC_END("err [%d]", err);
4464         return err;
4465 }
4466
4467 INTERNAL_FUNC int emstorage_update_account(char *multi_user_name, int account_id, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4468 {
4469         EM_DEBUG_FUNC_BEGIN("account_id[%d], account[%p], transaction[%d], err_code[%p]", account_id, account_tbl, transaction, err_code);
4470
4471         if (account_id < FIRST_ACCOUNT_ID || !account_tbl) {
4472                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4473                 if (err_code != NULL)
4474                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4475                 return false;
4476         }
4477
4478         int error = EMAIL_ERROR_NONE;
4479         int rc, ret = false;
4480
4481         DB_STMT hStmt = NULL;
4482         char sql_query_string[QUERY_SIZE] = {0, };
4483
4484         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4485
4486         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4487
4488         SNPRINTF(sql_query_string, sizeof(sql_query_string),
4489                 "UPDATE mail_account_tbl SET"
4490                 "  account_name = ?"
4491                 ", logo_icon_path = ?"
4492                 ", user_data = ?"
4493                 ", user_data_length = ?"
4494                 ", account_svc_id = ?"
4495                 ", sync_status = ?"
4496                 ", sync_disabled = ?"
4497                 ", default_mail_slot_size = ?"
4498                 ", roaming_option = ?"
4499                 ", color_label = ?"
4500                 ", user_display_name = ?"
4501                 ", user_email_address = ?"
4502                 ", reply_to_address = ?"
4503                 ", return_address = ?"
4504                 ", incoming_server_type = ?"
4505                 ", incoming_server_address = ?"
4506                 ", incoming_server_port_number = ?"
4507                 ", incoming_server_user_name = ?"
4508                 ", incoming_server_secure_connection = ?"
4509                 ", incoming_server_authentication_method = ?"
4510                 ", retrieval_mode = ?"
4511                 ", keep_mails_on_pop_server_after_download = ?"
4512                 ", check_interval = ?"
4513                 ", auto_download_size = ?"
4514                 ", peak_interval = ?"
4515                 ", peak_days = ?"
4516                 ", peak_start_time = ?"
4517                 ", peak_end_time = ?"
4518                 ", outgoing_server_type = ?"
4519                 ", outgoing_server_address = ?"
4520                 ", outgoing_server_port_number = ?"
4521                 ", outgoing_server_user_name = ?"
4522                 ", outgoing_server_secure_connection = ?"
4523                 ", outgoing_server_need_authentication = ?"
4524                 ", outgoing_server_use_same_authenticator = ?"
4525                 ", priority = ?"
4526                 ", keep_local_copy = ?"
4527                 ", req_delivery_receipt = ?"
4528                 ", req_read_receipt = ?"
4529                 ", download_limit = ?"
4530                 ", block_address = ?"
4531                 ", block_subject = ?"
4532                 ", display_name_from = ?"
4533                 ", reply_with_body = ?"
4534                 ", forward_with_files = ?"
4535                 ", add_myname_card = ?"
4536                 ", add_signature = ?"
4537                 ", signature = ?"
4538                 ", add_my_address_to_bcc = ?"
4539                 ", notification_status = ?"
4540                 ", vibrate_status = ?"
4541                 ", display_content_status = ?"
4542                 ", default_ringtone_status = ?"
4543                 ", alert_ringtone_path = ?"
4544                 ", auto_resend_times = ?"
4545                 ", outgoing_server_size_limit = ?"
4546                 ", wifi_auto_download = ?"
4547                 ", pop_before_smtp = ?"
4548                 ", incoming_server_requires_apop = ?"
4549                 ", smime_type = ?"
4550                 ", certificate_path = ?"
4551                 ", cipher_type = ?"
4552                 ", digest_type = ?"
4553                 ", user_name = ?"
4554                 " WHERE account_id = ?");
4555
4556         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4557         EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
4558         EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
4559         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4560                 ("sqlite3_prepare fail:(%d) %s", rc, sqlite3_errmsg(local_db_handle)));
4561
4562         int i = 0;
4563
4564         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4565         _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4566         _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4567         _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4568         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4569         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4570         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4571         _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4572         _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
4573         _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
4574         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4575         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4576         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4577         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4578         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4579         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4580         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4581         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4582         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4583         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
4584         _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
4585         _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
4586         _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
4587         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
4588         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
4589         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
4590         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
4591         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
4592         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
4593         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4594         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
4595         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
4596         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
4597         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
4598         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
4599         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
4600         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
4601         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
4602         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
4603         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
4604         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
4605         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
4606         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4607         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
4608         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
4609         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
4610         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
4611         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
4612         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
4613         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
4614         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
4615         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
4616         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
4617         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4618         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
4619         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
4620         _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
4621         _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
4622         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
4623         _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
4624         _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4625         _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
4626         _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
4627         _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4628         _bind_stmt_field_data_int(hStmt, i++, account_id);
4629
4630         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4631         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
4632                 ("sqlite3_step fail:%d", rc));
4633         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4634                 ("sqlite3_step fail:%d", rc));
4635
4636         /*  validate account existence */
4637         rc = sqlite3_changes(local_db_handle);
4638         if (rc == 0) {
4639                 EM_DEBUG_EXCEPTION("no matched account found...");
4640                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4641                 goto FINISH_OFF;
4642         }
4643
4644         if (account_tbl->incoming_server_password || account_tbl->outgoing_server_password) {
4645                 if ((error = emstorage_update_account_password(multi_user_name, account_id, account_tbl->incoming_server_password, account_tbl->outgoing_server_password)) != EMAIL_ERROR_NONE) {
4646                         EM_DEBUG_EXCEPTION("emstorage_update_account_password failed [%d]", error);
4647                         goto FINISH_OFF;
4648                 }
4649         }
4650
4651         ret = true;
4652
4653 FINISH_OFF:
4654         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4655
4656         if (hStmt != NULL) {
4657                 rc = sqlite3_finalize(hStmt);
4658                 if (rc != SQLITE_OK) {
4659                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4660                         error = EMAIL_ERROR_DB_FAILURE;
4661                 }
4662         }
4663
4664         if (error == EMAIL_ERROR_NONE) {
4665                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_tbl->account_id, 0, NULL, 0))
4666                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_ACCOUNT_UPDATE] : Notification Failed >>> ");
4667         }
4668
4669         if (err_code != NULL)
4670                 *err_code = error;
4671
4672         EM_DEBUG_FUNC_END("ret [%d]", ret);
4673         return ret;
4674 }
4675
4676 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)
4677 {
4678         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], field_name[%s], value[%d], transaction[%d]", account_id, field_name, value, transaction);
4679         int error = EMAIL_ERROR_NONE;
4680         int result = 0;
4681         char sql_query_string[QUERY_SIZE] = {0, };
4682         sqlite3 *local_db_handle = NULL;
4683
4684         if (!account_id  || !field_name) {
4685                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4686                 return EMAIL_ERROR_INVALID_PARAM;
4687         }
4688
4689         local_db_handle = emstorage_get_db_connection(multi_user_name);
4690
4691         /* Write query string */
4692         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_account_tbl SET %s = %d WHERE account_id = %d", field_name, value, account_id);
4693
4694         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4695
4696         /* Execute query */
4697         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4698
4699         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4700         if (error != EMAIL_ERROR_NONE) {
4701                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4702                 goto FINISH_OFF;
4703         }
4704
4705         if (sqlite3_changes(local_db_handle) == 0)
4706                 EM_DEBUG_LOG("no mail matched...");
4707
4708
4709 FINISH_OFF:
4710         result = (error == EMAIL_ERROR_NONE) ? true : false;
4711         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, error);
4712
4713         if (error == EMAIL_ERROR_NONE) {
4714                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_id, 0, field_name, value))
4715                         EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_ACCOUNT_UPDATE [%s,%d]", field_name, value);
4716         }
4717         EM_DEBUG_FUNC_END("error [%d]", error);
4718         return error;
4719 }
4720
4721 INTERNAL_FUNC int emstorage_get_sync_status_of_account(char *multi_user_name, int account_id, int *result_sync_status, int *err_code)
4722 {
4723         EM_DEBUG_FUNC_BEGIN("account_id[%d], result_sync_status [%p], err_code[%p]", account_id, result_sync_status, err_code);
4724
4725         if (!result_sync_status) {
4726                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4727                 if (err_code != NULL)
4728                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4729                 return false;
4730         }
4731
4732         int error = EMAIL_ERROR_NONE, rc, ret = false, sync_status, count, i, col_index;
4733         char sql_query_string[QUERY_SIZE] = {0, };
4734         char **result = NULL;
4735         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4736
4737         if (account_id)
4738                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl WHERE account_id = %d", account_id);
4739         else
4740                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl");
4741
4742         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
4743         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
4744                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4745
4746         if (!count) {
4747                 EM_DEBUG_EXCEPTION("no matched account found...");
4748                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4749                 goto FINISH_OFF;
4750         }
4751
4752         col_index = 1;
4753         *result_sync_status = 0;
4754
4755         for (i = 0; i < count; i++) {
4756                 _get_table_field_data_int(result, &sync_status, col_index++);
4757                 *result_sync_status |= sync_status;
4758         }
4759
4760         EM_DEBUG_LOG("sync_status [%d]", sync_status);
4761
4762         sqlite3_free_table(result);
4763
4764         ret = true;
4765
4766 FINISH_OFF:
4767
4768
4769         if (err_code != NULL)
4770                 *err_code = error;
4771
4772         EM_DEBUG_FUNC_END("ret [%d]", ret);
4773         return ret;
4774 }
4775
4776 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)
4777 {
4778         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);
4779
4780         int error = EMAIL_ERROR_NONE, rc, ret = false, set_value = sync_status, result_sync_status;
4781         char sql_query_string[QUERY_SIZE] = {0, };
4782         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4783
4784         if (set_operator != SET_TYPE_SET && account_id) {
4785                 if (!emstorage_get_sync_status_of_account(multi_user_name, account_id, &result_sync_status, &error)) {
4786                         EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", error);
4787                         if (err_code != NULL)
4788                                 *err_code = error;
4789                         return false;
4790                 }
4791                 switch (set_operator) {
4792                         case SET_TYPE_UNION:
4793                                 set_value = result_sync_status | set_value;
4794                                 break;
4795                         case SET_TYPE_MINUS:
4796                                 set_value = result_sync_status & (~set_value);
4797                                 break;
4798                         default:
4799                                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_NOT_SUPPORTED [%d]", set_operator);
4800                                 error = EMAIL_ERROR_NOT_SUPPORTED;
4801                                 break;
4802                 }
4803                 EM_DEBUG_LOG("set_value [%d]", set_value);
4804         }
4805
4806         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4807
4808         if (account_id)
4809                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE account_id = %d", set_value, account_id);
4810         else
4811                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE incoming_server_type <> 5", set_value);
4812
4813         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4814
4815         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4816         if (error != EMAIL_ERROR_NONE) {
4817                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4818                 goto FINISH_OFF;
4819         }
4820
4821         rc = sqlite3_changes(local_db_handle);
4822
4823         if (rc == 0) {
4824                 EM_DEBUG_EXCEPTION("no matched account found...");
4825                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4826                 goto FINISH_OFF;
4827         }
4828
4829         ret = true;
4830
4831 FINISH_OFF:
4832         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4833
4834         if (error == EMAIL_ERROR_NONE) {
4835                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE_SYNC_STATUS, account_id, set_value, NULL, 0))
4836                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_UPDATE_SYNC_STATUS] : Notification failed");
4837         }
4838
4839         if (err_code != NULL)
4840                 *err_code = error;
4841
4842         EM_DEBUG_FUNC_END("ret [%d]", ret);
4843         return ret;
4844 }
4845
4846 INTERNAL_FUNC int emstorage_add_account(char *multi_user_name, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4847 {
4848         EM_DEBUG_FUNC_BEGIN("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4849
4850         if (!account_tbl) {
4851                 EM_DEBUG_EXCEPTION("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4852                 if (err_code != NULL)
4853                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4854                 return false;
4855         }
4856
4857         int row_count = 0;
4858         int i = 0;
4859         int rc = -1, ret = false;
4860         int error = EMAIL_ERROR_NONE;
4861         DB_STMT hStmt = NULL;
4862         char sql_query_string[QUERY_SIZE] = {0, };
4863         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4864         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4865
4866         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4867
4868         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4869
4870         char *sql = "SELECT rowid FROM mail_account_tbl;";
4871         char **result = NULL;
4872
4873         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, &row_count, NULL, NULL), rc);
4874         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
4875                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
4876
4877         if (NULL == result[1]) rc = 1;
4878         else {
4879                 for (i = 1; i <= row_count; i++) {
4880                         if (i != atoi(result[i])) {
4881                                 break;
4882                         }
4883                 }
4884
4885                 rc = i;
4886         }
4887         sqlite3_free_table(result);
4888         result = NULL;
4889
4890         if (rc < 0 || rc > EMAIL_ACCOUNT_MAX) {
4891                 EM_DEBUG_EXCEPTION("OVERFLOWED THE MAX ACCOUNT");
4892                 error = EMAIL_ERROR_ACCOUNT_MAX_COUNT;
4893                 goto FINISH_OFF;
4894         }
4895
4896         account_tbl->account_id = rc;
4897
4898         if ((error = _get_password_file_name(multi_user_name, account_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4899                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4900                 goto FINISH_OFF;
4901         }
4902
4903         EM_DEBUG_LOG(" >>>> ACCOUNT_ID [ %d ] ", account_tbl->account_id);
4904         SNPRINTF(sql_query_string, sizeof(sql_query_string),
4905                 "INSERT INTO mail_account_tbl VALUES "
4906                 "(        "
4907                 "    ? "  /*   account_id */
4908                 "  , ? "  /*   account_name */
4909                 "  , ? "  /*   logo_icon_path */
4910                 "  , ? "  /*   user_data */
4911                 "  , ? "  /*   user_data_length */
4912                 "  , ? "  /*   account_svc_id */
4913                 "  , ? "  /*   sync_status */
4914                 "  , ? "  /*   sync_disabled */
4915                 "  , ? "  /*   default_mail_slot_size */
4916                 "  , ? "  /*   roaming_option */
4917                 "  , ? "  /*   color_label */
4918                 "  , ? "  /*   user_display_name */
4919                 "  , ? "  /*   user_email_address */
4920                 "  , ? "  /*   reply_to_address */
4921                 "  , ? "  /*   return_address */
4922                 "  , ? "  /*   incoming_server_type */
4923                 "  , ? "  /*   incoming_server_address */
4924                 "  , ? "  /*   incoming_server_port_number */
4925                 "  , ? "  /*   incoming_server_user_name */
4926                 "  , ? "  /*   incoming_server_password */
4927                 "  , ? "  /*   incoming_server_secure_connection */
4928                 "  , ? "  /*   incoming_server_authentication_method */
4929                 "  , ? "  /*   retrieval_mode */
4930                 "  , ? "  /*   keep_mails_on_pop_server_after_download */
4931                 "  , ? "  /*   check_interval */
4932                 "  , ? "  /*   auto_download_size */
4933                 "  , ? "  /*   peak_interval */
4934                 "  , ? "  /*   peak_days */
4935                 "  , ? "  /*   peak_start_time */
4936                 "  , ? "  /*   peak_end_time */
4937                 "  , ? "  /*   outgoing_server_type */
4938                 "  , ? "  /*   outgoing_server_address */
4939                 "  , ? "  /*   outgoing_server_port_number */
4940                 "  , ? "  /*   outgoing_server_user_name */
4941                 "  , ? "  /*   outgoing_server_password */
4942                 "  , ? "  /*   outgoing_server_secure_connection */
4943                 "  , ? "  /*   outgoing_server_need_authentication */
4944                 "  , ? "  /*   outgoing_server_use_same_authenticator */
4945                 "  , ? "  /*   priority */
4946                 "  , ? "  /*   keep_local_copy */
4947                 "  , ? "  /*   req_delivery_receipt */
4948                 "  , ? "  /*   req_read_receipt */
4949                 "  , ? "  /*   download_limit */
4950                 "  , ? "  /*   block_address */
4951                 "  , ? "  /*   block_subject */
4952                 "  , ? "  /*   display_name_from */
4953                 "  , ? "  /*   reply_with_body */
4954                 "  , ? "  /*   forward_with_files */
4955                 "  , ? "  /*   add_myname_card */
4956                 "  , ? "  /*   add_signature */
4957                 "  , ? "  /*   signature */
4958                 "  , ? "  /*   add_my_address_to_bcc */
4959                 "  , ? "  /*   auto_resend_times */
4960                 "  , ? "  /*   outgoing_server_size_limit */
4961                 "  , ? "  /*   wifi_auto_download */
4962                 "  , ? "  /*   pop_before_smtp */
4963                 "  , ? "  /*   incoming_server_requires_apop */
4964                 "  , ? "  /*   smime_type */
4965                 "  , ? "  /*   certificate_path */
4966                 "  , ? "  /*   cipher_type */
4967                 "  , ? "  /*   digest_type */
4968                 "  , ? "  /*   notification_status */
4969                 "  , ? "  /*   vibrate_status */
4970                 "  , ? "  /*   display_content_status */
4971                 "  , ? "  /*   default_ringtone_status */
4972                 "  , ? "  /*   alert_ringtone_path */
4973                 "  , ? "  /*   user_name */
4974                 ") ");
4975
4976         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4977         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4978                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4979
4980         i = 0;
4981
4982         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_id);
4983         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4984         _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4985         _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4986         _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4987         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4988         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4989         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4990         _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4991         _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
4992         _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
4993         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4994         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4995         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4996         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4997         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4998         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4999         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
5000         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
5001         _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
5002         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
5003         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
5004         _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
5005         _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
5006         _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
5007         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
5008         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
5009         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
5010         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
5011         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
5012         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
5013         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5014         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
5015         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
5016         _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
5017         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
5018         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
5019         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
5020         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
5021         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
5022         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
5023         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
5024         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
5025         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
5026         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
5027         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
5028         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
5029         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
5030         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
5031         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
5032         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
5033         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
5034         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
5035         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
5036         _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
5037         _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
5038         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
5039         _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
5040         _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
5041         _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
5042         _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
5043         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
5044         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
5045         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
5046         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
5047         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5048         _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5049
5050         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5051
5052         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
5053                 ("sqlite3_step fail:%d", rc));
5054         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5055                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
5056
5057
5058         /*  save passwords to the secure storage */
5059         EM_DEBUG_LOG_SEC("save to the secure storage : recv_file[%s], send_file[%s]", recv_password_file_name, send_password_file_name);
5060         error = emcore_add_password_in_key_manager(recv_password_file_name, account_tbl->incoming_server_password);
5061         if (error != EMAIL_ERROR_NONE) {
5062                 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
5063                 goto FINISH_OFF;
5064         }
5065
5066         error = emcore_add_password_in_key_manager(send_password_file_name, account_tbl->outgoing_server_password);
5067         if (error != EMAIL_ERROR_NONE) {
5068                 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
5069                 goto FINISH_OFF;
5070         }
5071
5072         ret = true;
5073
5074 FINISH_OFF:
5075         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5076
5077         if (hStmt != NULL) {
5078                 rc = sqlite3_finalize(hStmt);
5079                 if (rc != SQLITE_OK) {
5080                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5081                         error = EMAIL_ERROR_DB_FAILURE;
5082                 }
5083         }
5084
5085         if (error == EMAIL_ERROR_NONE) {
5086                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_ADD, account_tbl->account_id, 0, NULL, 0))
5087                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_ADD] : Notification failed");
5088         }
5089
5090         if (err_code != NULL)
5091                 *err_code = error;
5092
5093         EM_DEBUG_FUNC_END("ret [%d]", ret);
5094         return ret;
5095 }
5096
5097 INTERNAL_FUNC int emstorage_delete_account(char *multi_user_name, int account_id, int transaction, int *err_code)
5098 {
5099         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
5100
5101         if (account_id < FIRST_ACCOUNT_ID) {
5102                 EM_DEBUG_EXCEPTION(" account_id[%d]", account_id);
5103
5104                 if (err_code != NULL)
5105                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5106                 return false;
5107         }
5108
5109         int rc = -1, ret = false;
5110         int error = EMAIL_ERROR_NONE;
5111
5112         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5113
5114         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5115
5116         /*  TODO : delete password files - file names can be obtained from db or a rule that makes a name */
5117         char sql_query_string[QUERY_SIZE] = {0, };
5118         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5119         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5120
5121         /*  get password file name */
5122         if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
5123                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
5124                 goto FINISH_OFF;
5125         }
5126
5127         /*  delete from db */
5128         memset(sql_query_string, 0x00, sizeof(sql_query_string));
5129         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_account_tbl WHERE account_id = %d", account_id);
5130         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5131         if (error != EMAIL_ERROR_NONE) {
5132                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5133                         goto FINISH_OFF;
5134         }
5135
5136         /*  validate account existence */
5137         rc = sqlite3_changes(local_db_handle);
5138         if (rc == 0) {
5139                 EM_DEBUG_EXCEPTION(" no matched account found...");
5140                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
5141                 goto FINISH_OFF;
5142         }
5143
5144                 /*  delete from secure storage */
5145         error = emcore_remove_password_in_key_manager(recv_password_file_name);
5146         if (error != EMAIL_ERROR_NONE) {
5147                 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5148                 goto FINISH_OFF;
5149         }
5150
5151         error = emcore_remove_password_in_key_manager(send_password_file_name);
5152         if (error != EMAIL_ERROR_NONE) {
5153                 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5154                 goto FINISH_OFF;
5155         }
5156
5157         ret = true;
5158
5159 FINISH_OFF:
5160         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5161
5162         if (err_code != NULL)
5163                 *err_code = error;
5164
5165         EM_DEBUG_FUNC_END("ret [%d]", ret);
5166         return ret;
5167 }
5168
5169
5170 INTERNAL_FUNC int emstorage_free_account(emstorage_account_tbl_t** account_list, int count, int *err_code)
5171 {
5172         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%d], err_code[%p]", account_list, count, err_code);
5173
5174         int ret = false;
5175         int error = EMAIL_ERROR_NONE;
5176
5177         if (count > 0) {
5178                 if (!account_list || !*account_list) {
5179                         EM_DEBUG_EXCEPTION("account_list[%p], count[%d]", account_list, count);
5180                         error = EMAIL_ERROR_INVALID_PARAM;
5181                         goto FINISH_OFF;
5182                 }
5183
5184                 emstorage_account_tbl_t* p = *account_list;
5185                 int i = 0;
5186
5187                 for (; i < count; i++) {
5188                         EM_SAFE_FREE(p[i].account_name);
5189                         EM_SAFE_FREE(p[i].incoming_server_address);
5190                         EM_SAFE_FREE(p[i].user_email_address);
5191                         EM_SAFE_FREE(p[i].user_data);
5192                         EM_SAFE_FREE(p[i].incoming_server_user_name);
5193                         EM_SAFE_FREE(p[i].incoming_server_password);
5194                         EM_SAFE_FREE(p[i].outgoing_server_address);
5195                         EM_SAFE_FREE(p[i].outgoing_server_user_name);
5196                         EM_SAFE_FREE(p[i].outgoing_server_password);
5197                         EM_SAFE_FREE(p[i].user_display_name);
5198                         EM_SAFE_FREE(p[i].reply_to_address);
5199                         EM_SAFE_FREE(p[i].return_address);
5200                         EM_SAFE_FREE(p[i].logo_icon_path);
5201                         EM_SAFE_FREE(p[i].options.display_name_from);
5202                         EM_SAFE_FREE(p[i].options.signature);
5203                         EM_SAFE_FREE(p[i].options.alert_ringtone_path);
5204                         EM_SAFE_FREE(p[i].certificate_path);
5205                         EM_SAFE_FREE(p[i].user_name);
5206                 }
5207
5208                 EM_SAFE_FREE(p);
5209                 *account_list = NULL;
5210         }
5211
5212         ret = true;
5213
5214 FINISH_OFF:
5215         if (err_code != NULL)
5216                 *err_code = error;
5217
5218         EM_DEBUG_FUNC_END("ret [%d]", ret);
5219         return ret;
5220 }
5221
5222 INTERNAL_FUNC int emstorage_get_mailbox_count(char *multi_user_name, int account_id, int local_yn, int *count, int transaction, int *err_code)
5223 {
5224         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);
5225
5226         if ((account_id < FIRST_ACCOUNT_ID) || (count == NULL)) {
5227                 EM_DEBUG_EXCEPTION(" account_list[%d], local_yn[%d], count[%p]", account_id, local_yn, count);
5228
5229                 if (err_code != NULL)
5230                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5231                 return false;
5232         }
5233
5234         int rc = -1, ret = false;
5235         int error = EMAIL_ERROR_NONE;
5236         char sql_query_string[QUERY_SIZE] = {0, };
5237
5238         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5239         EMSTORAGE_START_READ_TRANSACTION(transaction);
5240
5241         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);
5242
5243         char **result;
5244
5245         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5246         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
5247                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5248
5249         *count = atoi(result[1]);
5250         sqlite3_free_table(result);
5251
5252
5253         ret = true;
5254
5255 FINISH_OFF:
5256
5257         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5258
5259         if (err_code != NULL)
5260                 *err_code = error;
5261
5262         EM_DEBUG_FUNC_END("ret [%d]", ret);
5263         return ret;
5264 }
5265
5266 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)
5267 {
5268         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);
5269
5270         if (!select_num || !mailbox_list) {
5271                 EM_DEBUG_EXCEPTION("Invalid parameters");
5272
5273                 if (err_code != NULL)
5274                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5275
5276                 return false;
5277         }
5278
5279         int ret = false;
5280         int error = EMAIL_ERROR_NONE;
5281         char conditional_clause_string[QUERY_SIZE] = {0, };
5282         char ordering_clause_string[QUERY_SIZE] = {0, };
5283
5284         if (account_id == ALL_ACCOUNT) {
5285                 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5286                         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);
5287         } else {
5288                 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d  ", account_id);
5289                 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5290                         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);
5291         }
5292
5293         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5294
5295         switch (sort_type) {
5296                 case EMAIL_MAILBOX_SORT_BY_NAME_ASC:
5297                         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name ASC");
5298                         break;
5299
5300                 case EMAIL_MAILBOX_SORT_BY_NAME_DSC:
5301                         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name DESC");
5302                         break;
5303
5304                 case EMAIL_MAILBOX_SORT_BY_TYPE_ASC:
5305                         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type ASC");
5306                         break;
5307
5308                 case EMAIL_MAILBOX_SORT_BY_TYPE_DSC:
5309                         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type DEC");
5310                         break;
5311         }
5312
5313         EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5314
5315         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5316                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5317                 goto FINISH_OFF;
5318         }
5319
5320         ret = true;
5321
5322 FINISH_OFF:
5323
5324         if (err_code != NULL)
5325                 *err_code = error;
5326
5327         EM_DEBUG_FUNC_END("ret [%d]", ret);
5328         return ret;
5329 }
5330
5331 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)
5332 {
5333         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);
5334
5335         int ret = false;
5336         int error = EMAIL_ERROR_NONE;
5337         int where_clause_count = 0;
5338         char conditional_clause_string[QUERY_SIZE] = {0, };
5339         char ordering_clause_string[QUERY_SIZE] = {0, };
5340
5341         if (!select_num || !mailbox_list) {
5342                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5343                 error = EMAIL_ERROR_INVALID_PARAM;
5344                 goto FINISH_OFF;
5345         }
5346
5347         if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL) {
5348                 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);
5349                 where_clause_count++;
5350         }
5351
5352         if (account_id > 0) {
5353                 if (where_clause_count == 0) {
5354                         SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " WHERE ");
5355                         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);
5356                 } else
5357                         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);
5358         }
5359
5360         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY CASE WHEN MBT.mailbox_name"
5361                                                                                                  " GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\'"
5362                                                                                                  " THEN 2 ELSE 1 END ASC,"
5363                                                                                                  " MBT.mailbox_name COLLATE CONVERTUTF8 ASC ");
5364         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5365         EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5366
5367         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 1, 1, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5368                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5369                 goto FINISH_OFF;
5370         }
5371
5372         ret = true;
5373
5374 FINISH_OFF:
5375
5376         if (err_code != NULL)
5377                 *err_code = error;
5378
5379         EM_DEBUG_FUNC_END("ret [%d]", ret);
5380         return ret;
5381 }
5382
5383 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)
5384 {
5385         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);
5386
5387         int ret = false;
5388         int error = EMAIL_ERROR_NONE;
5389         char conditional_clause_string[QUERY_SIZE] = {0, };
5390
5391         if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list || !parent_mailbox_name) {
5392                 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);
5393                 if (err_code != NULL)
5394                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5395                 return false;
5396         }
5397
5398         sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d  AND (UPPER(mailbox_name) = UPPER('%q') OR UPPER(mailbox_name) LIKE UPPER('%q/%%'))", account_id, parent_mailbox_name, parent_mailbox_name);
5399         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5400
5401         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) {
5402                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5403                 goto FINISH_OFF;
5404         }
5405
5406         ret = true;
5407
5408 FINISH_OFF:
5409
5410
5411         if (err_code != NULL)
5412                 *err_code = error;
5413
5414         EM_DEBUG_FUNC_END("ret [%d]", ret);
5415         return ret;
5416 }
5417
5418 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)
5419 {
5420         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);
5421         if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list) {
5422                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5423
5424                 if (err_code != NULL)
5425                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5426
5427                 return false;
5428         }
5429
5430         int ret = false;
5431         int error = EMAIL_ERROR_NONE;
5432         char conditional_clause_string[QUERY_SIZE] = {0, };
5433
5434         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND modifiable_yn = 0", account_id);
5435         EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
5436
5437         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) {
5438                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5439                 goto FINISH_OFF;
5440         }
5441
5442         ret = true;
5443
5444 FINISH_OFF:
5445
5446         if (err_code != NULL)
5447                 *err_code = error;
5448
5449         EM_DEBUG_FUNC_END("ret [%d]", ret);
5450         return ret;
5451 }
5452
5453 INTERNAL_FUNC int emstorage_stamp_last_sync_time_of_mailbox(char *multi_user_name, int input_mailbox_id, int input_transaction)
5454 {
5455         EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_transaction [%d]", input_mailbox_id, input_transaction);
5456
5457         int      result_code = false;
5458         int      error = EMAIL_ERROR_NONE;
5459         time_t   current_time = 0;
5460         char     sql_query_string[QUERY_SIZE] = {0, };
5461         sqlite3 *local_db_handle = NULL;
5462
5463         if (!input_mailbox_id) {
5464                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5465                 return EMAIL_ERROR_INVALID_PARAM;
5466         }
5467
5468         time(&current_time);
5469
5470         local_db_handle = emstorage_get_db_connection(multi_user_name);
5471     EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
5472
5473     SNPRINTF(sql_query_string, sizeof(sql_query_string),
5474         "UPDATE mail_box_tbl SET"
5475                 " last_sync_time = %d"
5476                 " WHERE mailbox_id = %d"
5477                 , (int)current_time
5478                 , input_mailbox_id);
5479
5480         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
5481         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5482         if (error != EMAIL_ERROR_NONE) {
5483                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5484                         goto FINISH_OFF;
5485         }
5486
5487 FINISH_OFF:
5488
5489         if (error == EMAIL_ERROR_NONE)
5490                 result_code = true;
5491
5492         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, result_code, error);
5493
5494         EM_DEBUG_FUNC_END("error [%d]", error);
5495         return error;
5496 }
5497
5498 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)
5499 {
5500         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);
5501         EM_PROFILE_BEGIN(profile_emstorage_get_mailbox_by_name);
5502
5503         if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !result_mailbox) {
5504                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], mailbox_name[%s], result_mailbox[%p]", account_id, local_yn, mailbox_name, result_mailbox);
5505
5506                 if (err_code != NULL)
5507                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5508                 return false;
5509         }
5510
5511         int ret = false;
5512         int error = EMAIL_ERROR_NONE;
5513         int result_count = 0;
5514         char conditional_clause_string[QUERY_SIZE] = {0, };
5515
5516         if (strcmp(mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
5517                 if (!(*result_mailbox = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t)))) {
5518                         EM_DEBUG_EXCEPTION("malloc failed...");
5519                         error = EMAIL_ERROR_OUT_OF_MEMORY;
5520                         goto FINISH_OFF;
5521                 }
5522
5523                 (*result_mailbox)->mailbox_id                 = 0;
5524                 (*result_mailbox)->account_id                 = account_id;
5525                 (*result_mailbox)->local_yn                   = 1;
5526                 (*result_mailbox)->mailbox_name               = EM_SAFE_STRDUP(mailbox_name);
5527                 (*result_mailbox)->mailbox_type               = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
5528                 (*result_mailbox)->alias                      = EM_SAFE_STRDUP(mailbox_name);
5529                 (*result_mailbox)->deleted_flag               = 0;
5530                 (*result_mailbox)->modifiable_yn              = 1;
5531                 (*result_mailbox)->total_mail_count_on_server = 1;
5532                 (*result_mailbox)->has_archived_mails         = 0;
5533                 (*result_mailbox)->mail_slot_size             = 0x0FFFFFFF;
5534                 (*result_mailbox)->no_select                  = 0;
5535         } else {
5536                 if (local_yn == -1)
5537                         sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d AND mailbox_name = '%q'", account_id, mailbox_name);
5538                 else
5539                         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);
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, "", 0, transaction, result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5544                         EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5545                         goto FINISH_OFF;
5546                 }
5547         }
5548
5549         ret = true;
5550
5551 FINISH_OFF:
5552
5553         if (err_code != NULL)
5554                 *err_code = error;
5555
5556         EM_PROFILE_END(profile_emstorage_get_mailbox_by_name);
5557         EM_DEBUG_FUNC_END("ret [%d]", ret);
5558         return ret;
5559 }
5560
5561 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)
5562 {
5563         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);
5564
5565         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_USER_DEFINED) || !output_mailbox) {
5566                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_type[%d], output_mailbox[%p]", account_id, mailbox_type, output_mailbox);
5567                 if (err_code != NULL)
5568                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5569                 return false;
5570         }
5571
5572         int ret = false;
5573         int error = EMAIL_ERROR_NONE;
5574         int result_count = 0;
5575         emstorage_mailbox_tbl_t *result_mailbox = NULL;
5576         char conditional_clause_string[QUERY_SIZE] = {0,};
5577
5578
5579         SNPRINTF(conditional_clause_string, QUERY_SIZE, "WHERE account_id = %d AND mailbox_type = %d ", account_id, mailbox_type);
5580
5581         EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5582
5583         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", true, false, &result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5584                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl error [%d]", error);
5585                 goto FINISH_OFF;
5586         }
5587
5588         ret = true;
5589
5590 FINISH_OFF:
5591         if (ret == true)
5592                 *output_mailbox = result_mailbox;
5593
5594         if (err_code != NULL)
5595                 *err_code = error;
5596
5597         EM_DEBUG_FUNC_END("ret [%d]", ret);
5598         return ret;
5599 }
5600
5601 INTERNAL_FUNC int emstorage_get_mailbox_by_id(char *multi_user_name, int input_mailbox_id, emstorage_mailbox_tbl_t** output_mailbox)
5602 {
5603         EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_mailbox[%p]", input_mailbox_id, output_mailbox);
5604
5605         if (input_mailbox_id <= 0 || !output_mailbox) {
5606                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: input_mailbox_id[%d] output_mailbox[%p]", input_mailbox_id, output_mailbox);
5607                 return  EMAIL_ERROR_INVALID_PARAM;
5608         }
5609
5610         int  ret = EMAIL_ERROR_NONE;
5611         int  result_count = 0;
5612         char conditional_clause_string[QUERY_SIZE] = {0, };
5613
5614         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE MBT.mailbox_id = %d", input_mailbox_id);
5615
5616         EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5617
5618         if ((ret = emstorage_query_mailbox_tbl(multi_user_name,
5619                                                                                         conditional_clause_string,
5620                                                                                         "", true, false, output_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5621                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", ret);
5622                 goto FINISH_OFF;
5623         }
5624
5625 FINISH_OFF:
5626
5627         EM_DEBUG_FUNC_END("ret [%d]", ret);
5628         return ret;
5629 }
5630
5631 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)
5632 {
5633         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);
5634
5635         if (account_id < 0 || !keyword || !result_mailbox) {
5636                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], keyword[%s], result_mailbox[%p]", account_id, keyword, result_mailbox);
5637
5638                 if (err_code != NULL)
5639                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5640                 return false;
5641         }
5642
5643         int ret = false;
5644         int error = EMAIL_ERROR_NONE;
5645         char conditional_clause_string[QUERY_SIZE] = {0, };
5646
5647         if (account_id == 0)
5648                 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5649                                 "WHERE alias LIKE \'%%%q%%\'", keyword);
5650         else if (account_id > 0)
5651                 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5652                                 "WHERE account_id = %d AND alias LIKE \'%%%q%%\'", account_id, keyword);
5653
5654         EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5655
5656         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, result_count)) != EMAIL_ERROR_NONE) {
5657                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5658                 goto FINISH_OFF;
5659         }
5660
5661         ret = true;
5662
5663 FINISH_OFF:
5664
5665         if (err_code != NULL)
5666                 *err_code = error;
5667
5668         EM_DEBUG_FUNC_END("ret [%d]", ret);
5669         return ret;
5670 }
5671
5672 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)
5673 {
5674         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);
5675         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_id) {
5676                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_id[%p]", account_id, mailbox_type, mailbox_id);
5677                 if (err_code != NULL)
5678                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5679                 return false;
5680         }
5681
5682         int rc, ret = false;
5683         int error = EMAIL_ERROR_NONE;
5684         emstorage_account_tbl_t* account = NULL;
5685         DB_STMT hStmt = NULL;
5686         char sql_query_string[QUERY_SIZE] = {0, };
5687
5688         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5689         EMSTORAGE_START_READ_TRANSACTION(transaction);
5690
5691         /*  Check whether the account exists. */
5692         if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
5693                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5694                 goto FINISH_OFF;
5695         }
5696
5697         if (account)
5698                 emstorage_free_account(&account, 1, NULL);
5699
5700         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);
5701
5702         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5703
5704
5705         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5706
5707         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5708                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5709
5710
5711         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5712         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5713                 ("sqlite3_step fail:%d", rc));
5714
5715         if (rc == SQLITE_DONE) {
5716                 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5717                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5718                 goto FINISH_OFF;
5719         }
5720
5721         _get_stmt_field_data_int(hStmt, mailbox_id, 0);
5722
5723         ret = true;
5724
5725 FINISH_OFF:
5726         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5727         if (hStmt != NULL) {
5728                 rc = sqlite3_finalize(hStmt);
5729                 if (rc != SQLITE_OK) {
5730                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5731                         error = EMAIL_ERROR_DB_FAILURE;
5732                 }
5733         }
5734
5735         if (err_code != NULL)
5736                 *err_code = error;
5737
5738         EM_DEBUG_FUNC_END("ret [%d]", ret);
5739         return ret;
5740 }
5741
5742 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)
5743 {
5744         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);
5745         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_name) {
5746                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
5747                 if (err_code != NULL)
5748                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5749                 return false;
5750         }
5751
5752         int rc, ret = false;
5753         int error = EMAIL_ERROR_NONE;
5754         emstorage_account_tbl_t* account = NULL;
5755         DB_STMT hStmt = NULL;
5756         char sql_query_string[QUERY_SIZE] = {0, };
5757
5758         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5759         EMSTORAGE_START_READ_TRANSACTION(transaction);
5760
5761         /*  Check whether the account exists. */
5762         if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
5763                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5764                 goto FINISH_OFF;
5765         }
5766
5767         if (account)
5768                 emstorage_free_account(&account, 1, NULL);
5769
5770         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);
5771
5772         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5773
5774
5775         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5776
5777         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5778                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5779
5780
5781         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5782         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5783                 ("sqlite3_step fail:%d", rc));
5784
5785         if (rc == SQLITE_DONE) {
5786                 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5787                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5788                 goto FINISH_OFF;
5789         }
5790
5791         _get_stmt_field_data_string(hStmt, mailbox_name, 0, 0);
5792
5793         ret = true;
5794
5795 FINISH_OFF:
5796         if (hStmt != NULL) {
5797                 rc = sqlite3_finalize(hStmt);
5798                 if (rc != SQLITE_OK) {
5799                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5800                         error = EMAIL_ERROR_DB_FAILURE;
5801                 }
5802         }
5803
5804         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5805
5806         if (err_code != NULL)
5807                 *err_code = error;
5808
5809         EM_DEBUG_FUNC_END("ret [%d]", ret);
5810         return ret;
5811 }
5812
5813 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)
5814 {
5815         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);
5816         int ret = false;
5817         int error = EMAIL_ERROR_NONE;
5818         char sql_query_string[QUERY_SIZE] = {0, };
5819         char *replaced_mailbox_name = NULL;
5820
5821         if (mailbox_name) {
5822                 if (strstr(mailbox_name, "'")) {
5823                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5824                 } else {
5825                         replaced_mailbox_name = strdup(mailbox_name);
5826                 }
5827         }
5828
5829         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5830
5831         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5832
5833         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5834                 "UPDATE mail_box_tbl SET"
5835                 " modifiable_yn = %d"
5836                 " WHERE account_id = %d"
5837                 " AND local_yn = %d"
5838                 " AND mailbox_name = '%s'"
5839                 , modifiable_yn
5840                 , account_id
5841                 , local_yn
5842                 , replaced_mailbox_name);
5843
5844         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5845         if (error != EMAIL_ERROR_NONE) {
5846                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5847                         goto FINISH_OFF;
5848         }
5849
5850         ret = true;
5851
5852 FINISH_OFF:
5853         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5854
5855         EM_SAFE_FREE(replaced_mailbox_name);
5856
5857         if (err_code != NULL)
5858                 *err_code = error;
5859
5860         EM_DEBUG_FUNC_END("ret [%d]", ret);
5861         return ret;
5862
5863 }
5864
5865 INTERNAL_FUNC int emstorage_update_mailbox_total_count(char *multi_user_name,
5866                                                                                                                 int account_id,
5867                                                                                                                 int input_mailbox_id,
5868                                                                                                                 int total_count_on_server,
5869                                                                                                                 int transaction,
5870                                                                                                                 int *err_code)
5871 {
5872         EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], total_count_on_server[%d], "
5873                                                 "transaction[%d], err_code[%p]",
5874                                                 account_id, input_mailbox_id, total_count_on_server,  transaction, err_code);
5875
5876         int ret = false;
5877         int error = EMAIL_ERROR_NONE;
5878         char sql_query_string[QUERY_SIZE] = {0, };
5879
5880         if (account_id <= 0 || input_mailbox_id <= 0) {
5881                 EM_DEBUG_EXCEPTION("account_id[%d], input_mailbox_id[%d]", account_id, input_mailbox_id);
5882                 if (err_code != NULL)
5883                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5884                 EM_DEBUG_FUNC_END("ret [%d]", ret);
5885                 return false;
5886         }
5887
5888         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5889
5890         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5891
5892         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5893                 "UPDATE mail_box_tbl SET"
5894                 " total_mail_count_on_server = %d"
5895                 " WHERE account_id = %d"
5896                 " AND mailbox_id = %d"
5897                 , total_count_on_server
5898                 , account_id
5899                 , input_mailbox_id);
5900
5901         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
5902         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5903         if (error != EMAIL_ERROR_NONE) {
5904                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5905                         goto FINISH_OFF;
5906         }
5907
5908         ret = true;
5909
5910 FINISH_OFF:
5911
5912         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5913
5914         if (err_code != NULL)
5915                 *err_code = error;
5916
5917         EM_DEBUG_FUNC_END("ret [%d]", ret);
5918         return ret;
5919
5920 }
5921
5922 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)
5923 {
5924         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);
5925
5926         if (account_id < FIRST_ACCOUNT_ID || input_mailbox_id <= 0 || !result_mailbox) {
5927                 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);
5928
5929                 if (err_code != NULL)
5930                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5931                 EM_DEBUG_FUNC_END("ret [%d]", EMAIL_ERROR_INVALID_PARAM);
5932                 return false;
5933         }
5934
5935         int rc, ret = false;
5936         int error = EMAIL_ERROR_NONE;
5937         char sql_query_string[QUERY_SIZE] = {0, };
5938         DB_STMT hStmt = NULL;
5939         int i = 0;
5940
5941         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5942         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5943
5944         if (local_yn != -1) {
5945                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5946                         "UPDATE mail_box_tbl SET"
5947                         "  mailbox_id = ?"
5948                         ", mailbox_name = ?"
5949                         ", mailbox_type = ?"
5950                         ", alias = ?"
5951                         ", deleted_flag = ?"
5952                         ", modifiable_yn= ?"
5953                         ", mail_slot_size= ?"
5954                         ", total_mail_count_on_server = ?"
5955                         " WHERE account_id = %d"
5956                         " AND local_yn = %d"
5957                         " AND mailbox_id = '%d'"
5958                         , account_id
5959                         , local_yn
5960                         , input_mailbox_id);
5961         } else {
5962                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5963                         "UPDATE mail_box_tbl SET"
5964                         "  mailbox_id = ?"
5965                         ", mailbox_name = ?"
5966                         ", mailbox_type = ?"
5967                         ", alias = ?"
5968                         ", deleted_flag = ?"
5969                         ", modifiable_yn= ?"
5970                         ", mail_slot_size= ?"
5971                         ", total_mail_count_on_server = ?"
5972                         " WHERE account_id = %d"
5973                         " AND mailbox_id = '%d'"
5974                         , account_id
5975                         , input_mailbox_id);
5976         }
5977
5978
5979
5980         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5981         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5982                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5983
5984         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_id);
5985         _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);
5986         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_type);
5987         _bind_stmt_field_data_string(hStmt, i++, (char *)result_mailbox->alias ? result_mailbox->alias : "", 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
5988         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->deleted_flag);
5989         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->modifiable_yn);
5990         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mail_slot_size);
5991         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->total_mail_count_on_server);
5992
5993
5994         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5995         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
5996                 ("sqlite3_step fail:%d", rc));
5997         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5998                 ("sqlite3_step fail:%d", rc));
5999
6000         ret = true;
6001
6002 FINISH_OFF:
6003         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6004
6005         if (hStmt != NULL) {
6006                 rc = sqlite3_finalize(hStmt);
6007                 if (rc != SQLITE_OK) {
6008                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6009                         error = EMAIL_ERROR_DB_FAILURE;
6010                 }
6011         }
6012
6013         if (err_code != NULL)
6014                 *err_code = error;
6015
6016         EM_DEBUG_FUNC_END("ret [%d]", ret);
6017         return ret;
6018 }
6019
6020 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)
6021 {
6022         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);
6023
6024         if (account_id < FIRST_ACCOUNT_ID) {
6025                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
6026
6027                 if (err_code != NULL)
6028                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6029                 return false;
6030         }
6031
6032         int rc, ret = false;
6033         int error = EMAIL_ERROR_NONE;
6034         char sql_query_string[QUERY_SIZE] = {0, };
6035
6036         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6037         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6038
6039         EM_DEBUG_LOG("emstorage_update_mailbox_type");
6040
6041         DB_STMT hStmt_box_tbl = NULL;
6042         DB_STMT hStmt_mail_tbl = NULL;
6043         int i = 0;
6044
6045         /*  Update mail_box_tbl */
6046         if (local_yn != -1) {
6047                 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
6048                         "UPDATE mail_box_tbl SET"
6049                         " mailbox_type = ?"
6050                         " WHERE account_id = %d"
6051                         " AND local_yn = %d"
6052                         " AND mailbox_id = '%d'"
6053                         , account_id
6054                         , local_yn
6055                         , input_mailbox_id);
6056         } else {
6057                 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
6058                         "UPDATE mail_box_tbl SET"
6059                         " mailbox_type = ?"
6060                         " WHERE account_id = %d"
6061                         " AND mailbox_id = '%d'"
6062                         , account_id
6063                         , input_mailbox_id);
6064         }
6065
6066         EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6067
6068         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_box_tbl, NULL), rc);
6069
6070         if (SQLITE_OK != rc) {
6071                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6072                 error = EMAIL_ERROR_DB_FAILURE;
6073                 goto FINISH_OFF;
6074         }
6075
6076         _bind_stmt_field_data_int(hStmt_box_tbl, i++, new_mailbox_type);
6077
6078
6079         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_box_tbl), rc);
6080
6081         if (rc == SQLITE_FULL) {
6082                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6083                 error   = EMAIL_ERROR_MAIL_MEMORY_FULL;
6084                 goto FINISH_OFF;
6085         }
6086
6087         if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
6088                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6089                 error = EMAIL_ERROR_DB_FAILURE;
6090                 goto FINISH_OFF;
6091         }
6092
6093
6094         /*  Update mail_tbl */
6095         i = 0;
6096         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6097                         "UPDATE mail_tbl SET"
6098                         " mailbox_type = ?"
6099                         " WHERE account_id = %d"
6100                         " AND mailbox_id = '%d'"
6101                         , account_id
6102                         , input_mailbox_id);
6103
6104         EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
6105
6106         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_mail_tbl, NULL), rc);
6107         if (SQLITE_OK != rc) {
6108                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6109                 error = EMAIL_ERROR_DB_FAILURE;
6110                 goto FINISH_OFF;
6111         }
6112
6113         _bind_stmt_field_data_int(hStmt_mail_tbl, i++, new_mailbox_type);
6114
6115         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_mail_tbl), rc);
6116         if (rc == SQLITE_FULL) {
6117                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6118                 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
6119                 goto FINISH_OFF;
6120         }
6121
6122         if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
6123                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6124                 error = EMAIL_ERROR_DB_FAILURE;
6125                 goto FINISH_OFF;
6126         }
6127
6128         ret = true;
6129
6130 FINISH_OFF:
6131         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6132
6133         if (hStmt_box_tbl != NULL) {
6134                 rc = sqlite3_finalize(hStmt_box_tbl);
6135                 if (rc != SQLITE_OK) {
6136                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6137                         error = EMAIL_ERROR_DB_FAILURE;
6138                 }
6139         }
6140
6141         if (hStmt_mail_tbl != NULL) {
6142                 rc = sqlite3_finalize(hStmt_mail_tbl);
6143                 if (rc != SQLITE_OK) {
6144                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6145                         error = EMAIL_ERROR_DB_FAILURE;
6146                 }
6147         }
6148
6149         if (err_code != NULL)
6150                 *err_code = error;
6151
6152         EM_DEBUG_FUNC_END("ret [%d]", ret);
6153         return ret;
6154 }
6155
6156 INTERNAL_FUNC int emstorage_set_local_mailbox(char *multi_user_name, int input_mailbox_id, int input_is_local_mailbox, int transaction)
6157 {
6158         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);
6159
6160         int rc, ret = false;
6161         int error = EMAIL_ERROR_NONE;
6162         char sql_query_string[QUERY_SIZE] = {0, };
6163
6164         if (input_mailbox_id < 0) {
6165                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6166                 return EMAIL_ERROR_INVALID_PARAM;
6167         }
6168
6169         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6170
6171         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6172
6173         EM_DEBUG_LOG("emstorage_update_mailbox_type");
6174
6175         DB_STMT hStmt = NULL;
6176         int i = 0;
6177
6178         /*  Update mail_box_tbl */
6179         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6180                 "UPDATE mail_box_tbl SET"
6181                 " local_yn = ?"
6182                 " WHERE mailbox_id = %d"
6183                 , input_mailbox_id);
6184
6185         EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6186
6187         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6188         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6189                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6190
6191         _bind_stmt_field_data_int(hStmt, i++, input_is_local_mailbox);
6192
6193         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6194         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
6195                 ("sqlite3_step fail:%d", rc));
6196         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6197                 ("sqlite3_step fail:%d", rc));
6198 /*
6199         if (hStmt != NULL) {
6200                 rc = sqlite3_finalize(hStmt);
6201                 if (rc != SQLITE_OK) {
6202                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6203                         error = EMAIL_ERROR_DB_FAILURE;
6204                 }
6205                 hStmt = NULL;
6206         }
6207 */
6208         ret = true;
6209
6210 FINISH_OFF:
6211         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6212
6213         if (hStmt != NULL) {
6214                 rc = sqlite3_finalize(hStmt);
6215                 if (rc != SQLITE_OK) {
6216                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6217                         error = EMAIL_ERROR_DB_FAILURE;
6218                 }
6219         }
6220
6221         EM_DEBUG_FUNC_END("error [%d]", error);
6222         return error;
6223 }
6224
6225 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)
6226 {
6227         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);
6228         int i = 0;
6229         int err = EMAIL_ERROR_NONE;
6230         int result = false;
6231         int cur_mailbox_id_string = 0;
6232         int mailbox_id_string_buffer_length = 0;
6233         char  sql_query_string[QUERY_SIZE] = {0, };
6234         char *mailbox_id_string_buffer = NULL;
6235         char *parameter_string = NULL;
6236         sqlite3 *local_db_handle = NULL;
6237
6238         if (input_mailbox_id_array == NULL || input_mailbox_id_count == 0 || input_field_name == NULL) {
6239                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6240                 return EMAIL_ERROR_INVALID_PARAM;
6241         }
6242
6243         local_db_handle = emstorage_get_db_connection(multi_user_name);
6244
6245         /* Generating mail id list string */
6246         mailbox_id_string_buffer_length = MAILBOX_ID_STRING_LENGTH * input_mailbox_id_count;
6247
6248         mailbox_id_string_buffer = em_malloc(mailbox_id_string_buffer_length);
6249
6250         if (!mailbox_id_string_buffer) {
6251                 EM_DEBUG_EXCEPTION("em_mallocfailed");
6252                 err = EMAIL_ERROR_OUT_OF_MEMORY;
6253                 goto FINISH_OFF;
6254         }
6255
6256         for (i = 0; i < input_mailbox_id_count; i++)
6257                 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]);
6258
6259         if (EM_SAFE_STRLEN(mailbox_id_string_buffer) > 1)
6260                 mailbox_id_string_buffer[EM_SAFE_STRLEN(mailbox_id_string_buffer) - 1] = NULL_CHAR;
6261
6262         /* Generating notification parameter string */
6263         parameter_string = em_malloc(mailbox_id_string_buffer_length + EM_SAFE_STRLEN(input_field_name) + 2);
6264
6265         if (!parameter_string) {
6266                 EM_DEBUG_EXCEPTION("em_mallocfailed");
6267                 err = EMAIL_ERROR_OUT_OF_MEMORY;
6268                 goto FINISH_OFF;
6269         }
6270
6271         SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", input_field_name, 0x01, mailbox_id_string_buffer);
6272
6273         /* Write query string */
6274         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);
6275
6276         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
6277
6278         /* Execute query */
6279         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
6280         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6281         if (err != EMAIL_ERROR_NONE) {
6282                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
6283                         goto FINISH_OFF;
6284         }
6285
6286         if (sqlite3_changes(local_db_handle) == 0)
6287                 EM_DEBUG_LOG("no mail matched...");
6288
6289         result = true;
6290
6291 FINISH_OFF:
6292         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, err);
6293
6294         if (err == EMAIL_ERROR_NONE && parameter_string) {
6295                 if (!emcore_notify_storage_event(NOTI_MAILBOX_FIELD_UPDATE, input_account_id, 0, parameter_string, input_value))
6296                         EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_MAILBOX_FIELD_UPDATE [%s,%d]",
6297                                                                                          input_field_name, input_value);
6298         }
6299
6300         EM_SAFE_FREE(mailbox_id_string_buffer);
6301         EM_SAFE_FREE(parameter_string);
6302
6303         EM_DEBUG_FUNC_END("err [%d]", err);
6304         return err;
6305 }
6306
6307 INTERNAL_FUNC int emstorage_add_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox_tbl, int transaction, int *err_code)
6308 {
6309         EM_DEBUG_FUNC_BEGIN("mailbox_tbl[%p], transaction[%d], err_code[%p]", mailbox_tbl, transaction, err_code);
6310
6311         if (!mailbox_tbl) {
6312                 if (err_code != NULL)
6313                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6314                 return false;
6315         }
6316
6317         int rc, ret = false;
6318         int error = EMAIL_ERROR_NONE;
6319         DB_STMT hStmt = NULL;
6320         char sql_query_string[QUERY_SIZE] = {0,};
6321         char **result = NULL;
6322         time_t current_time;
6323         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6324         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6325
6326         EM_SAFE_STRCPY(sql_query_string, "SELECT max(rowid) FROM mail_box_tbl;");
6327
6328         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6329         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6330                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6331
6332         time(&current_time);
6333
6334         if (NULL == result[1])
6335                 rc = 1;
6336         else
6337                 rc = atoi(result[1]) + 1;
6338         sqlite3_free_table(result);
6339
6340         memset(sql_query_string, 0, sizeof(char) * QUERY_SIZE);
6341
6342         mailbox_tbl->mailbox_id = rc;
6343
6344         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6345                 "INSERT INTO mail_box_tbl VALUES "
6346                 "(?"    /* mailbox_id */
6347                 ", ?"    /* account_id */
6348                 ", ?"    /* local_yn */
6349                 ", ?"    /* mailbox_name */
6350                 ", ?"    /* mailbox_type */
6351                 ", ?"    /* alias */
6352                 ", ?"    /* deleted_flag */
6353                 ", ?"    /* modifiable_yn */
6354                 ", ?"    /* total_mail_count_on_server */
6355                 ", ?"    /* has_archived_mails */
6356                 ", ?"    /* mail_slot_size */
6357                 ", ?"    /* no_select */
6358                 ", ?"    /* last_sync_time */
6359                 ", ?"    /* eas_data_length */
6360                 ", ?"    /* eas_data */
6361                 ")");
6362
6363
6364         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6365         EM_DEBUG_LOG_DEV("After sqlite3_prepare hStmt = %p", hStmt);
6366         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6367                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6368
6369         int col_index = 0;
6370
6371         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_id);
6372         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->account_id);
6373         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->local_yn);
6374         _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
6375         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_type);
6376         _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->alias, 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
6377         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->deleted_flag);
6378         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->modifiable_yn);
6379         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->total_mail_count_on_server);
6380         _bind_stmt_field_data_int(hStmt, col_index++, 0);
6381         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mail_slot_size);
6382         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->no_select);
6383         _bind_stmt_field_data_int(hStmt, col_index++, current_time);
6384         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->eas_data_length);
6385         _bind_stmt_field_data_blob(hStmt, col_index++, (void*)mailbox_tbl->eas_data, mailbox_tbl->eas_data_length);
6386
6387
6388         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6389         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6390                 ("sqlite3_step fail:%dn", rc));
6391
6392         ret = true;
6393
6394 FINISH_OFF:
6395         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6396         if (hStmt != NULL) {
6397                 rc = sqlite3_finalize(hStmt);
6398                 if (rc != SQLITE_OK) {
6399                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6400                         error = EMAIL_ERROR_DB_FAILURE;
6401                 }
6402         }
6403
6404         if (error == EMAIL_ERROR_NONE) {
6405                 if (!emcore_notify_storage_event(NOTI_MAILBOX_ADD, mailbox_tbl->account_id, mailbox_tbl->mailbox_id,
6406                                                                   mailbox_tbl->mailbox_name, mailbox_tbl->mailbox_type))
6407                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6408         }
6409
6410         if (err_code != NULL)
6411                 *err_code = error;
6412
6413         EM_DEBUG_FUNC_END("ret [%d]", ret);
6414         return ret;
6415 }
6416
6417 INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(char *multi_user_name, int account_id, int modifiable_yn, int transaction, int *err_code)
6418 {
6419         EM_DEBUG_FUNC_BEGIN("account_id[%d], modifiable_yn[%d], err_code[%p]", account_id, modifiable_yn, err_code);
6420
6421         if (account_id < FIRST_ACCOUNT_ID) {
6422
6423                 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
6424
6425                 if (err_code != NULL)
6426                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6427                 return false;
6428         }
6429         int rc, ret = false;
6430         int error = EMAIL_ERROR_NONE;
6431         char sql_query_string[QUERY_SIZE] = {0,};
6432         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6433         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6434
6435
6436         SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET modifiable_yn = %d WHERE account_id = %d", modifiable_yn, account_id);
6437
6438         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6439         if (error != EMAIL_ERROR_NONE) {
6440                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6441                         goto FINISH_OFF;
6442         }
6443
6444         rc = sqlite3_changes(local_db_handle);
6445         if (rc == 0)
6446                 EM_DEBUG_EXCEPTION("All mailbox_name modifiable_yn set to 0 already");
6447
6448
6449         ret = true;
6450
6451 FINISH_OFF:
6452         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6453
6454         if (err_code != NULL)
6455                 *err_code = error;
6456         EM_DEBUG_FUNC_END("ret [%d]", ret);
6457         return ret;
6458
6459
6460 }
6461
6462 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)
6463 {
6464         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);
6465
6466         if (account_id < FIRST_ACCOUNT_ID) {
6467                 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
6468
6469                 if (err_code != NULL)
6470                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6471                 return false;
6472         }
6473
6474         int rc, ret = false;
6475         int error = EMAIL_ERROR_NONE;
6476         char sql_query_string[QUERY_SIZE] = {0, };
6477         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6478         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6479
6480         if (local_yn == -1)
6481                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_box_tbl WHERE account_id = %d ", account_id);
6482         else
6483                 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);
6484
6485         if (input_mailbox_id > 0) {             /* 0 means all mailbox */
6486                 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);
6487         }
6488
6489         EM_DEBUG_LOG_SEC("mailbox sql_query_string [%s]", sql_query_string);
6490         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6491         if (error != EMAIL_ERROR_NONE) {
6492                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6493                         goto FINISH_OFF;
6494         }
6495
6496         rc = sqlite3_changes(local_db_handle);
6497         if (rc == 0) {
6498                 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
6499                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
6500                 ret = true;
6501         }
6502         ret = true;
6503
6504 FINISH_OFF:
6505
6506         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6507
6508         if (error == EMAIL_ERROR_NONE) {
6509                 if (!emcore_notify_storage_event(NOTI_MAILBOX_DELETE, account_id, input_mailbox_id, NULL, 0))
6510                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6511         }
6512
6513         if (err_code != NULL)
6514                 *err_code = error;
6515
6516         EM_DEBUG_FUNC_END("ret [%d]", ret);
6517         return ret;
6518 }
6519
6520 INTERNAL_FUNC int emstorage_free_mailbox(emstorage_mailbox_tbl_t** mailbox_list, int count, int *err_code)
6521 {
6522         EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d], err_code[%p]", mailbox_list, count, err_code);
6523
6524         int ret = false;
6525         int error = EMAIL_ERROR_NONE;
6526
6527         if (count > 0) {
6528                 if (!mailbox_list || !*mailbox_list) {
6529                         EM_DEBUG_EXCEPTION(" mailbox_list[%p], count[%d]", mailbox_list, count);
6530
6531                         error = EMAIL_ERROR_INVALID_PARAM;
6532                         goto FINISH_OFF;
6533                 }
6534
6535                 emstorage_mailbox_tbl_t* p = *mailbox_list;
6536                 int i = 0;
6537
6538                 for (; i < count; i++) {
6539                         EM_SAFE_FREE(p[i].mailbox_name);
6540                         EM_SAFE_FREE(p[i].alias);
6541                         EM_SAFE_FREE(p[i].eas_data); /*valgrind*/
6542                 }
6543
6544                 EM_SAFE_FREE(p); *mailbox_list = NULL;
6545         }
6546
6547         ret = true;
6548
6549 FINISH_OFF:
6550         if (err_code != NULL)
6551                 *err_code = error;
6552
6553         EM_DEBUG_FUNC_END("ret [%d]", ret);
6554         return ret;
6555 }
6556
6557 INTERNAL_FUNC int emstorage_get_count_read_mail_uid(char *multi_user_name, int account_id, char *mailbox_name, int *count, int transaction, int *err_code)
6558 {
6559         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);
6560
6561         if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !count) {
6562                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], count[%p], exist[%p]", account_id, mailbox_name, count);
6563
6564                 if (err_code != NULL)
6565                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6566                 return false;
6567         }
6568
6569         int rc = -1, ret = false;
6570         int error = EMAIL_ERROR_NONE;
6571         char sql_query_string[QUERY_SIZE] = {0, };
6572         char *replaced_mailbox_name = NULL;
6573
6574         if (strstr(mailbox_name, "'")) {
6575                 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6576         } else {
6577                 replaced_mailbox_name = EM_SAFE_STRDUP(mailbox_name);
6578         }
6579
6580         EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6581
6582
6583         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6584         EMSTORAGE_START_READ_TRANSACTION(transaction);
6585         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);
6586         EM_DEBUG_LOG_SEC(">>> SQL [ %s ] ", sql_query_string);
6587
6588         char **result;
6589
6590         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6591         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6592                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6593
6594         *count = atoi(result[1]);
6595         sqlite3_free_table(result);
6596
6597         ret = true;
6598
6599 FINISH_OFF:
6600         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6601
6602         EM_SAFE_FREE(replaced_mailbox_name);
6603
6604         if (err_code != NULL)
6605                 *err_code = error;
6606
6607         EM_DEBUG_FUNC_END("ret [%d]", ret);
6608         return ret;
6609 }
6610
6611 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)
6612 {
6613         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);
6614
6615         if (account_id < FIRST_ACCOUNT_ID || !uid || !exist) {
6616                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], uid[%p], exist[%p]", account_id, mailbox_name , uid, exist);
6617
6618                 if (err_code != NULL)
6619                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6620                 return false;
6621         }
6622
6623         int rc = -1, ret = false;
6624         int error = EMAIL_ERROR_NONE;
6625         char sql_query_string[QUERY_SIZE] = {0, };
6626         char *replaced_mailbox_name = NULL;
6627
6628         EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6629
6630         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6631         EMSTORAGE_START_READ_TRANSACTION(transaction);
6632
6633         if (mailbox_name) {
6634                 if (strstr(mailbox_name, "'")) {
6635                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6636                 } else {
6637                         replaced_mailbox_name = strdup(mailbox_name);
6638                 }
6639
6640                 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);
6641         } else {
6642                 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);
6643         }
6644
6645         char **result = NULL;
6646
6647         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6648         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6649                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6650
6651         *exist = atoi(result[1]);
6652         sqlite3_free_table(result);
6653
6654         if (*exist > 0)
6655                 *exist = 1;
6656         else
6657                 *exist = 0;
6658
6659         ret = true;
6660
6661 FINISH_OFF:
6662         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6663
6664         EM_SAFE_FREE(replaced_mailbox_name);
6665
6666         if (err_code != NULL)
6667                 *err_code = error;
6668
6669         EM_DEBUG_FUNC_END("ret [%d]", ret);
6670         return ret;
6671 }
6672
6673 INTERNAL_FUNC int emstorage_get_downloaded_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code)
6674 {
6675         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], err_code[%p]", mail_id, mail, err_code);
6676
6677         if (!mail || mail_id <= 0) {
6678                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
6679                 if (err_code != NULL)
6680                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6681                 return false;
6682         }
6683
6684         int rc, ret = false;
6685         int error = EMAIL_ERROR_NONE;
6686         DB_STMT hStmt = NULL;
6687         char sql_query_string[QUERY_SIZE] = {0, };
6688
6689         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6690         EMSTORAGE_START_READ_TRANSACTION(transaction);
6691
6692         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE local_uid = %d", mail_id);
6693
6694         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6695         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6696
6697         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6698                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6699
6700
6701         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6702         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6703                 ("sqlite3_step fail:%d", rc));
6704
6705         *mail = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
6706         if (*mail == NULL) {
6707                 error = EMAIL_ERROR_OUT_OF_MEMORY;
6708                 EM_DEBUG_EXCEPTION("Memory allocation for mail failed.");
6709                 goto FINISH_OFF;
6710
6711         }
6712         memset(*mail, 0x00, sizeof(emstorage_mail_tbl_t));
6713
6714         _get_stmt_field_data_int(hStmt, &((*mail)->account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6715         _get_stmt_field_data_int(hStmt, &((*mail)->mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6716         _get_stmt_field_data_string(hStmt, &((*mail)->server_mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6717         _get_stmt_field_data_int(hStmt, &((*mail)->mail_id), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6718         _get_stmt_field_data_string(hStmt, &((*mail)->server_mail_id), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6719         _get_stmt_field_data_int(hStmt, &((*mail)->mail_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6720         _get_stmt_field_data_char(hStmt, &((*mail)->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6721
6722         (*mail)->server_mail_status = 1;
6723
6724         ret = true;
6725
6726 FINISH_OFF:
6727
6728         if (hStmt != NULL) {
6729                 rc = sqlite3_finalize(hStmt);
6730                 if (rc != SQLITE_OK) {
6731                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6732                         error = EMAIL_ERROR_DB_FAILURE;
6733                 }
6734         }
6735
6736         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6737
6738         if (err_code != NULL)
6739                 *err_code = error;
6740
6741         EM_DEBUG_FUNC_END("ret [%d]", ret);
6742         return ret;
6743 }
6744
6745 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)
6746 {
6747         EM_PROFILE_BEGIN(emStorageGetDownloadList);
6748         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);
6749         if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid || !count) {
6750                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_id[%s], read_mail_uid[%p], count[%p]", account_id, mailbox_id, read_mail_uid, count);
6751
6752                 if (err_code != NULL)
6753                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6754                 return false;
6755         }
6756
6757         int rc, ret = false;
6758         int error = EMAIL_ERROR_NONE;
6759
6760         DB_STMT hStmt = NULL;
6761         char sql_query_string[QUERY_SIZE] = {0, };
6762
6763         emstorage_read_mail_uid_tbl_t* p_data_tbl = NULL;
6764         int i = 0;
6765
6766         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6767         EMSTORAGE_START_READ_TRANSACTION(transaction);
6768
6769         if (mailbox_id)
6770                 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);
6771         else
6772                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
6773
6774         EM_DEBUG_LOG_SEC(" sql_query_string : %s", sql_query_string);
6775
6776
6777
6778         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6779         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6780         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6781                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6782
6783
6784         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6785         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6786                 ("sqlite3_step fail:%d", rc));
6787
6788         char **result;
6789         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL); */
6790         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL), rc);
6791         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6792                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6793
6794         sqlite3_free_table(result);
6795         if (*count == 0) {
6796                 EM_DEBUG_LOG("No mail found in mail_read_mail_uid_tbl");
6797                 ret = true;
6798                 goto FINISH_OFF;
6799         }
6800
6801
6802         if (!(p_data_tbl = (emstorage_read_mail_uid_tbl_t*)malloc(sizeof(emstorage_read_mail_uid_tbl_t) * *count))) {
6803                 EM_DEBUG_EXCEPTION(" malloc failed...");
6804                 error = EMAIL_ERROR_OUT_OF_MEMORY;
6805                 goto FINISH_OFF;
6806         }
6807
6808         memset(p_data_tbl, 0x00, sizeof(emstorage_read_mail_uid_tbl_t)*(*count));
6809
6810         for (i = 0; i < *count; ++i) {
6811                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6812                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6813                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6814                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].local_uid), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6815                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].server_uid), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6816                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rfc822_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6817                 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6818                 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_flagged_field), FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6819
6820                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6821                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6822                         ("sqlite3_step fail:%d", rc));
6823         }
6824
6825         ret = true;
6826
6827 FINISH_OFF:
6828         if (ret == true)
6829                 *read_mail_uid = p_data_tbl;
6830         else if (p_data_tbl)
6831                 emstorage_free_read_mail_uid(&p_data_tbl, *count, NULL);
6832
6833         if (hStmt != NULL) {
6834                 rc = sqlite3_finalize(hStmt);
6835                 if (rc != SQLITE_OK) {
6836                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6837                         error = EMAIL_ERROR_DB_FAILURE;
6838                 }
6839         }
6840
6841         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6842
6843         if (err_code != NULL)
6844                 *err_code = error;
6845
6846         EM_PROFILE_END(emStorageGetDownloadList);
6847         EM_DEBUG_FUNC_END("ret [%d]", ret);
6848         return ret;
6849 }
6850
6851 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)
6852 {
6853         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);
6854
6855         if (account_id < FIRST_ACCOUNT_ID || !mail_size) {
6856                 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);
6857
6858                 if (err_code != NULL)
6859                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6860                 return false;
6861         }
6862
6863         int rc, ret = false;
6864         int error = EMAIL_ERROR_NONE;
6865         DB_STMT hStmt = NULL;
6866         char sql_query_string[QUERY_SIZE] = {0, };
6867         char *replaced_mailbox_name = NULL;
6868
6869         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6870         EMSTORAGE_START_READ_TRANSACTION(transaction);
6871
6872         if (mailbox_name) {
6873                 if (strstr(mailbox_name, "'")) {
6874                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6875                 } else {
6876                         replaced_mailbox_name = strdup(mailbox_name);
6877                 }
6878
6879                 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6880
6881                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6882                         "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6883                         "WHERE account_id = %d "
6884                         "AND mailbox_id = '%s' "
6885                         "AND local_uid = %d "
6886                         "AND mailbox_name = '%s' "
6887                         "AND server_uid = '%s'",
6888                         account_id, mailbox_id, local_uid, replaced_mailbox_name, uid);
6889         } else {
6890                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6891                         "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6892                         "WHERE account_id = %d "
6893                         "AND mailbox_id = '%s' "
6894                         "AND local_uid = %d "
6895                         "AND server_uid = '%s'",
6896                         account_id, mailbox_id, local_uid, uid);
6897         }
6898
6899
6900         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6901         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6902                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6903
6904
6905         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6906         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6907                 ("sqlite3_step fail:%d", rc));
6908
6909         if (rc == SQLITE_DONE) {
6910                 EM_DEBUG_LOG("no matched mail found....");
6911                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
6912                 goto FINISH_OFF;
6913         }
6914
6915         _get_stmt_field_data_int(hStmt, mail_size, 0);
6916
6917         ret = true;
6918
6919 FINISH_OFF:
6920         EM_SAFE_FREE(replaced_mailbox_name);
6921
6922         if (hStmt != NULL) {
6923                 rc = sqlite3_finalize(hStmt);
6924                 if (rc != SQLITE_OK) {
6925                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6926                         error = EMAIL_ERROR_DB_FAILURE;
6927                 }
6928         }
6929
6930         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6931
6932         if (err_code != NULL)
6933                 *err_code = error;
6934
6935         EM_DEBUG_FUNC_END("ret [%d]", ret);
6936         return ret;
6937 }
6938
6939 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)
6940 {
6941         EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], transaction[%d], err_code[%p]", read_mail_uid, transaction, err_code);
6942
6943         if (!read_mail_uid) {
6944                 EM_DEBUG_EXCEPTION("read_mail_uid[%p]", read_mail_uid);
6945                 if (err_code != NULL)
6946                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6947                 return false;
6948         }
6949
6950         int rc, rc2,  ret = false;
6951         int error = EMAIL_ERROR_NONE;
6952         DB_STMT hStmt = NULL;
6953         char sql_query_string[QUERY_SIZE] = {0, };
6954
6955         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6956         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6957
6958         char *sql = "SELECT max(rowid) FROM mail_read_mail_uid_tbl;";
6959         char **result = NULL;
6960
6961
6962         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6963         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6964                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6965
6966         if (NULL == result[1]) rc = 1;
6967         else rc = atoi(result[1])+1;
6968         sqlite3_free_table(result);
6969
6970         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6971                 "INSERT INTO mail_read_mail_uid_tbl VALUES "
6972                 "(?"  /* account_id */
6973                 ", ?"  /* mailbox_id */
6974                 ", ?"  /* mailbox_name */
6975                 ", ?"  /* local_uid */
6976                 ", ?"  /* server_uid */
6977                 ", ?"  /* rfc822_size */
6978                 ", ?"  /* sync_status */
6979                 ", ?"  /* flags_seen_field */
6980                 ", ?"  /* flags_flagged_field */
6981                 ", ?)");
6982
6983
6984         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
6985         if (rc2 != SQLITE_OK) {
6986                 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6987                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6988
6989                 error = EMAIL_ERROR_DB_FAILURE;
6990                 goto FINISH_OFF;
6991         }
6992
6993         EM_DEBUG_LOG("account_id[%d] mailbox_id[%d] local_uid [%d]"
6994                    "server_uid[%s] rfc822_size[%d] rc[%d]",
6995          read_mail_uid->account_id, read_mail_uid->mailbox_id, read_mail_uid->local_uid,
6996          read_mail_uid->server_uid, read_mail_uid->rfc822_size, rc);
6997
6998         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->account_id);
6999         _bind_stmt_field_data_int(hStmt, LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->mailbox_id);
7000         _bind_stmt_field_data_int(hStmt, LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->local_uid);
7001         _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);
7002         _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);
7003         _bind_stmt_field_data_int(hStmt, RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->rfc822_size);
7004         _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_seen_field);
7005         _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_flagged_field);
7006         _bind_stmt_field_data_int(hStmt, IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, rc);
7007
7008
7009         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7010         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
7011                 ("sqlite3_step fail:%d", rc));
7012         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7013                 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
7014
7015
7016         ret = true;
7017
7018 FINISH_OFF:
7019         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7020         if (hStmt != NULL) {
7021                 rc = sqlite3_finalize(hStmt);
7022                 if (rc != SQLITE_OK) {
7023                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7024                         error = EMAIL_ERROR_DB_FAILURE;
7025                 }
7026         }
7027
7028         if (err_code != NULL)
7029                 *err_code = error;
7030
7031         EM_DEBUG_FUNC_END("ret [%d]", ret);
7032         return ret;
7033 }
7034
7035 #ifdef __FEATURE_BODY_SEARCH__
7036 INTERNAL_FUNC int emstorage_add_mail_text(char *multi_user_name, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
7037 {
7038         EM_DEBUG_FUNC_BEGIN("mail_text[%p], transaction[%d], err_code[%p]", mail_text, transaction, err_code);
7039
7040         if (!mail_text) {
7041                 EM_DEBUG_EXCEPTION("mail_text[%p]", mail_text);
7042                 if (err_code != NULL)
7043                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7044                 return false;
7045         }
7046
7047         int rc, rc2,  ret = false;
7048         int error = EMAIL_ERROR_NONE;
7049         DB_STMT hStmt = NULL;
7050         char sql_query_string[QUERY_SIZE] = {0, };
7051
7052         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7053         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7054
7055         char *sql = "SELECT max(rowid) FROM mail_text_tbl;";
7056         char **result = NULL;
7057
7058         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7059         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7060                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7061         sqlite3_free_table(result);
7062
7063         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7064                 "INSERT INTO mail_text_tbl VALUES "
7065                 "(?"
7066                 ", ?"
7067                 ", ?"
7068                 ", ?)");
7069
7070         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
7071         if (rc2 != SQLITE_OK) {
7072                 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7073                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc2, sqlite3_errmsg(local_db_handle));
7074
7075                 error = EMAIL_ERROR_DB_FAILURE;
7076                 goto FINISH_OFF;
7077         }
7078
7079         EM_DEBUG_LOG("mail_id[%d] account_id[%d] mailbox_id[%d]", mail_text->mail_id,
7080                                        mail_text->account_id, mail_text->mailbox_id);
7081         EM_DEBUG_LOG_DEV("body_text VALUE [%s] ", mail_text->body_text);
7082
7083         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mail_id);
7084         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->account_id);
7085         _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mailbox_id);
7086         _bind_stmt_field_data_string(hStmt, BODY_TEXT_IDX_IN_MAIL_TEXT_TBL, (char *)mail_text->body_text, 0, -1);
7087
7088         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7089         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
7090                 ("sqlite3_step fail:%d", rc));
7091         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7092                 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
7093
7094         ret = true;
7095
7096 FINISH_OFF:
7097         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7098         if (hStmt != NULL) {
7099                 rc = sqlite3_finalize(hStmt);
7100                 if (rc != SQLITE_OK) {
7101                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7102                         error = EMAIL_ERROR_DB_FAILURE;
7103                 }
7104         }
7105
7106         if (err_code != NULL)
7107                 *err_code = error;
7108
7109         EM_DEBUG_FUNC_END("ret [%d]", ret);
7110         return ret;
7111 }
7112 #endif
7113
7114 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)
7115 {
7116         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);
7117
7118         if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid) {
7119                 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);
7120
7121                 if (err_code != NULL)
7122                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7123                 return false;
7124         }
7125
7126         int rc, ret = false;
7127         int error = EMAIL_ERROR_NONE;
7128         DB_STMT hStmt = NULL;
7129         char sql_query_string[QUERY_SIZE] = {0, };
7130
7131         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7132
7133         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7134
7135         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7136                 "UPDATE mail_read_mail_uid_tbl SET"
7137                 "  account_id = ?"
7138                 ", mailbox_id = ?"
7139                 ", mailbox_name = ?"
7140                 ", local_uid  = ?"
7141                 ", server_uid = ?"
7142                 ", rfc822_size = ?"
7143                 ", flags_seen_field  = ?"
7144                 ", flags_flagged_field  = ?"
7145                 " WHERE account_id = ?"
7146                 " AND mailbox_id  = ?"
7147                 " AND local_uid   = ?"
7148                 " AND mailbox_name= ?"
7149                 " AND server_uid = ?");
7150
7151
7152         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7153         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7154         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7155                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7156
7157
7158         int i = 0;
7159
7160         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->account_id);
7161         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->mailbox_id);
7162         _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7163         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->local_uid);
7164         _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->server_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7165         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->rfc822_size);
7166         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_seen_field);
7167         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_flagged_field);
7168         _bind_stmt_field_data_int(hStmt, i++, account_id);
7169         _bind_stmt_field_data_int(hStmt, i++, mailbox_id);
7170         _bind_stmt_field_data_int(hStmt, i++, local_uid);
7171         _bind_stmt_field_data_string(hStmt, i++, (char*)mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7172         _bind_stmt_field_data_string(hStmt, i++, (char*)uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7173
7174
7175         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7176         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7177                 ("sqlite3_step fail:%d", rc));
7178
7179         ret = true;
7180
7181 FINISH_OFF:
7182         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7183         if (hStmt != NULL) {
7184                 rc = sqlite3_finalize(hStmt);
7185                 if (rc != SQLITE_OK) {
7186                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7187                         error = EMAIL_ERROR_DB_FAILURE;
7188                 }
7189         }
7190
7191         if (err_code != NULL)
7192                 *err_code = error;
7193
7194         EM_DEBUG_FUNC_END("ret [%d]", ret);
7195         return ret;
7196 }
7197
7198 INTERNAL_FUNC int emstorage_remove_downloaded_mail(char *multi_user_name,
7199                                                                                                         int account_id,
7200                                                                                                         int mailbox_id,
7201                                                                                                         char *mailbox_name,
7202                                                                                                         char *uid,
7203                                                                                                         int transaction,
7204                                                                                                         int *err_code)
7205 {
7206         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], mailbox_id[%d], mailbox_name[%s], "
7207                                                         "uid[%s], transaction[%d], err_code[%p]",
7208                                                         account_id, mailbox_id, mailbox_name, uid, transaction, err_code);
7209
7210         if (account_id < FIRST_ACCOUNT_ID) {
7211                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_name[%s], uid[%s]", account_id, mailbox_name, uid);
7212
7213                 if (err_code != NULL)
7214                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7215                 return false;
7216         }
7217
7218         int ret = false;
7219         int error = EMAIL_ERROR_NONE;
7220         char sql_query_string[QUERY_SIZE] = {0, };
7221         char *replaced_mailbox_name = NULL;
7222         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7223
7224         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7225
7226         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7227                                 "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d ", account_id);
7228
7229         if (mailbox_id > 0) {
7230                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7231                                         sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7232                                         "AND mailbox_id = %d ", mailbox_id);
7233         }
7234
7235         if (mailbox_name) {             /*  NULL means all mailbox_name */
7236                 if (strstr(mailbox_name, "'")) {
7237                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
7238                 } else {
7239                         replaced_mailbox_name = strdup(mailbox_name);
7240                 }
7241
7242                 SNPRINTF(sql_query_string+EM_SAFE_STRLEN(sql_query_string),
7243                                         sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7244                                         "AND mailbox_name = '%s' ", replaced_mailbox_name);
7245         }
7246
7247         if (uid) {              /*  NULL means all mail */
7248                 sqlite3_snprintf(sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7249                                                  sql_query_string + EM_SAFE_STRLEN(sql_query_string), "AND server_uid = '%q' ",
7250                                                  uid);
7251         }
7252
7253         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7254         if (error != EMAIL_ERROR_NONE) {
7255                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7256                         goto FINISH_OFF;
7257         }
7258
7259         ret = true;
7260
7261 FINISH_OFF:
7262         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7263
7264         EM_SAFE_FREE(replaced_mailbox_name);
7265
7266         if (err_code != NULL)
7267                 *err_code = error;
7268
7269         EM_DEBUG_FUNC_END("ret [%d]", ret);
7270         return ret;
7271 }
7272
7273 INTERNAL_FUNC int emstorage_free_read_mail_uid(emstorage_read_mail_uid_tbl_t** read_mail_uid, int count, int *err_code)
7274 {
7275         EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], count[%d], err_code[%p]", read_mail_uid, count, err_code);
7276
7277         int ret = false;
7278         int error = EMAIL_ERROR_NONE;
7279
7280         if (count > 0) {
7281                 if (!read_mail_uid || !*read_mail_uid) {
7282                         EM_DEBUG_EXCEPTION(" read_mail_uid[%p], count[%d]", read_mail_uid, count);
7283
7284                         error = EMAIL_ERROR_INVALID_PARAM;
7285                         goto FINISH_OFF;
7286                 }
7287
7288                 emstorage_read_mail_uid_tbl_t* p = *read_mail_uid;
7289                 int i;
7290
7291                 for (i = 0; i < count; i++) {
7292                         EM_SAFE_FREE(p[i].mailbox_name);
7293                         EM_SAFE_FREE(p[i].server_uid);
7294                 }
7295
7296                 EM_SAFE_FREE(p); *read_mail_uid = NULL;
7297         }
7298
7299         ret = true;
7300
7301 FINISH_OFF:
7302         if (err_code != NULL)
7303                 *err_code = error;
7304
7305         EM_DEBUG_FUNC_END("ret [%d]", ret);
7306         return ret;
7307 }
7308
7309 INTERNAL_FUNC int emstorage_get_rule_count_by_account_id(char *multi_user_name, int account_id, int *count, int transaction, int *err_code)
7310 {
7311         EM_DEBUG_FUNC_BEGIN("account_id [%d], count[%p], transaction[%d], err_code[%p]", count, transaction, err_code);
7312
7313         if (!count) {
7314                 EM_DEBUG_EXCEPTION("count[%p]", count);
7315
7316                 if (err_code != NULL)
7317                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7318                 return false;
7319         }
7320
7321         int rc = -1, ret = false;
7322         int error =  EMAIL_ERROR_NONE;
7323         char sql_query_string[QUERY_SIZE] = {0, };
7324
7325         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7326         EMSTORAGE_START_READ_TRANSACTION(transaction);
7327
7328         if (account_id != ALL_ACCOUNT)
7329                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl where account_id = %d", account_id);
7330         else
7331                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl");
7332
7333         char **result;
7334
7335         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7336         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7337                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7338
7339         *count = atoi(result[1]);
7340         sqlite3_free_table(result);
7341
7342         ret = true;
7343
7344 FINISH_OFF:
7345         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7346
7347         if (err_code != NULL)
7348                 *err_code = error;
7349
7350         EM_DEBUG_FUNC_END("ret [%d]", ret);
7351         return ret;
7352 }
7353
7354 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)
7355 {
7356         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);
7357
7358         if (!select_num || !is_completed || !rule_list) {               /*  only global rule supported. */
7359                 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);
7360
7361                 if (err_code != NULL)
7362                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7363                 return false;
7364         }
7365
7366         int ret = false;
7367         int error = EMAIL_ERROR_NONE;
7368
7369         emstorage_rule_tbl_t* p_data_tbl = NULL;
7370         int i = 0, count = 0;
7371         DB_STMT hStmt = NULL;
7372         char sql_query_string[QUERY_SIZE] = {0, };
7373
7374         int rc;
7375
7376         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7377         EMSTORAGE_START_READ_TRANSACTION(transaction);
7378
7379         if (account_id != ALL_ACCOUNT) {
7380                 if (type)
7381                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d AND type = %d", account_id, type);
7382                 else
7383                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d ORDER BY type", account_id);
7384         } else {
7385                 if (type)
7386                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE type = %d", type);
7387                 else
7388                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl ORDER BY type");
7389         }
7390
7391         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7392 /*      EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt); */
7393         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7394                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7395
7396
7397         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7398         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7399                 ("sqlite3_step fail:%d", rc));
7400
7401         char **result;
7402         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL); */
7403         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
7404         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7405                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7406
7407         sqlite3_free_table(result);
7408
7409         if (count == 0) {
7410                 EM_DEBUG_LOG_DEV("No matching rule found...");
7411                 ret = true;
7412                 error = EMAIL_ERROR_FILTER_NOT_FOUND; /*there is no matched rule*/
7413                 goto FINISH_OFF;
7414         }
7415
7416
7417         if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t) * count))) {
7418                 EM_DEBUG_EXCEPTION(" malloc failed...");
7419
7420                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7421                 goto FINISH_OFF;
7422         }
7423
7424         memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t) * count);
7425
7426         for (i = 0; i < count; i++) {
7427                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7428                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7429                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7430                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].type), TYPE_IDX_IN_MAIL_RULE_TBL);
7431                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7432                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7433                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7434                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7435                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7436                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7437
7438                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7439                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7440                         ("sqlite3_step fail:%d", rc));
7441         }
7442
7443         ret = true;
7444
7445 FINISH_OFF:
7446
7447         EM_DEBUG_LOG("[%d] rules found.", count);
7448
7449         if (ret == true) {
7450                 *rule_list = p_data_tbl;
7451                 *select_num = count;
7452         } else if (p_data_tbl != NULL)
7453                 emstorage_free_rule(&p_data_tbl, count, NULL); /* CID FIX */
7454
7455         if (hStmt != NULL) {
7456                 rc = sqlite3_finalize(hStmt);
7457                 if (rc != SQLITE_OK) {
7458                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7459                         error = EMAIL_ERROR_DB_FAILURE;
7460                 }
7461         }
7462
7463         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7464
7465         if (err_code != NULL)
7466                 *err_code = error;
7467
7468         EM_DEBUG_FUNC_END("ret [%d]", ret);
7469         return ret;
7470 }
7471
7472 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)
7473 {
7474         EM_DEBUG_FUNC_BEGIN("rule_id[%d], rule[%p], transaction[%d], err_code[%p]", rule_id, rule, transaction, err_code);
7475         int error = EMAIL_ERROR_NONE;
7476         int ret = false;
7477         DB_STMT hStmt = NULL;
7478
7479         if (rule_id <= 0) {
7480                 EM_DEBUG_EXCEPTION("Invalid parameter");
7481                 error = EMAIL_ERROR_INVALID_PARAM;
7482                 goto FINISH_OFF;
7483         }
7484
7485         if (!rule) {
7486                 EM_DEBUG_EXCEPTION("rule_id[%d], rule[%p]", rule_id, rule);
7487                 error = EMAIL_ERROR_INVALID_PARAM;
7488                 goto FINISH_OFF;
7489         }
7490
7491         emstorage_rule_tbl_t* p_data_tbl = NULL;
7492         int rc;
7493
7494         char sql_query_string[QUERY_SIZE] = {0, };
7495         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7496         EMSTORAGE_START_READ_TRANSACTION(transaction);
7497
7498         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7499
7500         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7501         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7502                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7503
7504
7505         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7506         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7507                 ("sqlite3_step fail:%d", rc));
7508
7509         if (rc == SQLITE_DONE) {
7510                 EM_DEBUG_EXCEPTION(" no matched rule found...");
7511                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7512                 goto FINISH_OFF;
7513         }
7514
7515         if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t)))) {
7516                 EM_DEBUG_EXCEPTION(" malloc failed...");
7517                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7518                 goto FINISH_OFF;
7519         }
7520
7521         memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t));
7522         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7523         _get_stmt_field_data_int(hStmt, &(p_data_tbl->rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7524         _get_stmt_field_data_string(hStmt, &(p_data_tbl->filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7525         _get_stmt_field_data_int(hStmt, &(p_data_tbl->type), TYPE_IDX_IN_MAIL_RULE_TBL);
7526         _get_stmt_field_data_string(hStmt, &(p_data_tbl->value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7527         _get_stmt_field_data_string(hStmt, &(p_data_tbl->value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7528         _get_stmt_field_data_int(hStmt, &(p_data_tbl->action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7529         _get_stmt_field_data_int(hStmt, &(p_data_tbl->target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7530         _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7531         _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7532
7533         ret = true;
7534
7535 FINISH_OFF:
7536
7537         if (ret == true)
7538                 *rule = p_data_tbl;
7539
7540         if (hStmt != NULL) {
7541                 rc = sqlite3_finalize(hStmt);
7542                 if (rc != SQLITE_OK) {
7543                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7544                         error = EMAIL_ERROR_DB_FAILURE;
7545                 }
7546         }
7547
7548         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7549
7550         if (err_code != NULL)
7551                 *err_code = error;
7552
7553         EM_DEBUG_FUNC_END("ret [%d]", ret);
7554         return ret;
7555 }
7556
7557 INTERNAL_FUNC int emstorage_change_rule(char *multi_user_name, int rule_id, emstorage_rule_tbl_t* new_rule, int transaction, int *err_code)
7558 {
7559         EM_DEBUG_FUNC_BEGIN("rule_id[%d], new_rule[%p], transaction[%d], err_code[%p]", rule_id, new_rule, transaction, err_code);
7560
7561         if (!new_rule) {                /*  only global rule supported. */
7562                 EM_DEBUG_EXCEPTION("rule_id[%d], new_rule[%p]", rule_id, new_rule);
7563
7564                 if (err_code != NULL)
7565                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7566                 return false;
7567         }
7568
7569         int rc, ret = false;
7570         int error = EMAIL_ERROR_NONE;
7571
7572         DB_STMT hStmt = NULL;
7573         char sql_query_string[QUERY_SIZE] = {0, };
7574         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7575         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7576
7577         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7578                 "UPDATE mail_rule_tbl SET"
7579                 "  filter_name = ?"
7580                 ", type = ?"
7581                 ", value = ?"
7582                 ", value2 = ?"
7583                 ", action_type = ?"
7584                 ", target_mailbox_id = ?"
7585                 ", flag1 = ?"
7586                 ", flag2 = ?"
7587                 ", account_id = ?"
7588                 ", rule_id = ?"
7589                 " WHERE rule_id = %d"
7590                 , rule_id);
7591
7592
7593         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7594         EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
7595         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7596                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7597
7598         int i = 0;
7599
7600         _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->filter_name, 0, FILTER_NAME_LEN_IN_MAIL_RULE_TBL);
7601         _bind_stmt_field_data_int(hStmt, i++, new_rule->type);
7602         _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7603         _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7604         _bind_stmt_field_data_int(hStmt, i++, new_rule->action_type);
7605         _bind_stmt_field_data_int(hStmt, i++, new_rule->target_mailbox_id);
7606         _bind_stmt_field_data_int(hStmt, i++, new_rule->flag1);
7607         _bind_stmt_field_data_int(hStmt, i++, new_rule->flag2);
7608         _bind_stmt_field_data_int(hStmt, i++, new_rule->account_id);
7609         _bind_stmt_field_data_int(hStmt, i++, rule_id);
7610
7611
7612         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7613         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7614                 ("sqlite3_step fail:%d", rc));
7615
7616         ret = true;
7617
7618 FINISH_OFF:
7619         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7620         if (hStmt != NULL) {
7621                 rc = sqlite3_finalize(hStmt);
7622                 if (rc != SQLITE_OK) {
7623                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7624                         error = EMAIL_ERROR_DB_FAILURE;
7625                 }
7626         }
7627
7628         if (err_code != NULL)
7629                 *err_code = error;
7630
7631         EM_DEBUG_FUNC_END("ret [%d]", ret);
7632         return ret;
7633 }
7634
7635 INTERNAL_FUNC int emstorage_find_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7636 {
7637         EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7638
7639         if (!rule) {
7640                 EM_DEBUG_LOG("rule is NULL");
7641                 if (err_code != NULL)
7642                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7643                 return false;
7644         }
7645
7646         DB_STMT hStmt = NULL;
7647         char sql_query_string[QUERY_SIZE] = {0,};
7648         int error = EMAIL_ERROR_NONE;
7649         int rc = 0;
7650         int ret = false;
7651
7652         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7653         EMSTORAGE_START_READ_TRANSACTION(transaction);
7654
7655         switch (rule->action_type) {
7656         case EMAIL_FILTER_MOVE:
7657                 if (rule->type == EMAIL_PRIORITY_SENDER) {
7658                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7659                                 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7660                                 rule->action_type, rule->type, rule->value2);
7661                 } else {
7662                                 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7663                                         "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(filter_name) = UPPER(\'%q\')", rule->action_type, rule->type, rule->filter_name);
7664                 }
7665                 break;
7666         case EMAIL_FILTER_BLOCK:
7667                 if (rule->type == EMAIL_FILTER_FROM)
7668                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7669                                 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7670                                 rule->action_type, rule->type, rule->value2);
7671                 else if (rule->type == EMAIL_FILTER_SUBJECT)
7672                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7673                                 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value) = UPPER(\'%q\')",
7674                                 rule->action_type, rule->type, rule->value);
7675                 else if (rule->type == (EMAIL_FILTER_SUBJECT | EMAIL_FILTER_FROM))
7676                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7677                                 "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\'))",
7678                                 rule->action_type, EMAIL_FILTER_SUBJECT, rule->value, EMAIL_FILTER_FROM, rule->value2);
7679                 break;
7680
7681         default:
7682                 EM_DEBUG_EXCEPTION("Invalid parameter : rule->action_type[%d]", rule->action_type);
7683                 error = EMAIL_ERROR_INVALID_PARAM;
7684                 goto FINISH_OFF;
7685                 break;
7686         }
7687
7688         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7689         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7690                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7691
7692         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7693         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7694                 ("sqlite3_step fail:%d", rc));
7695
7696         if (rc == SQLITE_DONE) {
7697                 EM_DEBUG_EXCEPTION(" no matched rule found...");
7698                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7699         }
7700
7701         ret = true;
7702
7703 FINISH_OFF:
7704
7705         if (hStmt != NULL) {
7706                 rc = sqlite3_finalize(hStmt);
7707                 if (rc != SQLITE_OK) {
7708                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7709                         error = EMAIL_ERROR_DB_FAILURE;
7710                 }
7711         }
7712
7713         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7714
7715         if (err_code)
7716                 *err_code = error;
7717
7718         EM_DEBUG_FUNC_END("ret [%d]", ret);
7719         return ret;
7720 }
7721
7722 INTERNAL_FUNC int emstorage_add_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7723 {
7724         EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7725
7726         if (!rule) {    /*  only global rule supported. */
7727                 EM_DEBUG_LOG("rule is NULL");
7728                 if (err_code != NULL)
7729                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7730                 return false;
7731         }
7732
7733         int rc, rc_2, ret = false;
7734         int error = EMAIL_ERROR_NONE;
7735         DB_STMT hStmt = NULL;
7736         char sql_query_string[QUERY_SIZE] = {0, };
7737
7738         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7739
7740         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7741
7742         char *sql;
7743         char **result;
7744         sql = "SELECT max(rowid) FROM mail_rule_tbl;";
7745
7746         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7747         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7748                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7749
7750         if (NULL == result[1])
7751                 rc = 1;
7752         else
7753                 rc = atoi(result[1])+1;
7754
7755         sqlite3_free_table(result);
7756
7757         rule->rule_id = rc;
7758
7759         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7760                 "INSERT INTO mail_rule_tbl VALUES "
7761                 "(?"            /*  account id */
7762                 ", ?"           /*  rule_id */
7763                 ", ?"           /*  filter_name */
7764                 ", ?"           /*  type */
7765                 ", ?"           /*  value */
7766                 ", ?"           /*  value2 */
7767                 ", ?"           /*  action_type */
7768                 ", ?"           /*  target_mailbox_id */
7769                 ", ?"           /*  flag1 */
7770                 ", ?)");        /*  flag2 */
7771
7772         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc_2);
7773         if (rc_2 != SQLITE_OK) {
7774                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc_2, sqlite3_errmsg(local_db_handle));
7775                 error = EMAIL_ERROR_DB_FAILURE;
7776                 goto FINISH_OFF;
7777         }
7778
7779         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL, rule->account_id);
7780         _bind_stmt_field_data_int(hStmt, RULE_ID_IDX_IN_MAIL_RULE_TBL, rule->rule_id);
7781         _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);
7782         _bind_stmt_field_data_int(hStmt, TYPE_IDX_IN_MAIL_RULE_TBL, rule->type);
7783         _bind_stmt_field_data_string(hStmt, VALUE_IDX_IN_MAIL_RULE_TBL, (char*)rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7784         _bind_stmt_field_data_string(hStmt, VALUE2_IDX_IN_MAIL_RULE_TBL, (char*)rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7785         _bind_stmt_field_data_int(hStmt, ACTION_TYPE_IDX_IN_MAIL_RULE_TBL, rule->action_type);
7786         _bind_stmt_field_data_int(hStmt, TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL, rule->target_mailbox_id);
7787         _bind_stmt_field_data_int(hStmt, FLAG1_IDX_IN_MAIL_RULE_TBL, rule->flag1);
7788         _bind_stmt_field_data_int(hStmt, FLAG2_IDX_IN_MAIL_RULE_TBL, rule->flag2);
7789
7790         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7791         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7792                 ("sqlite3_step fail:%d", rc));
7793
7794         ret = true;
7795
7796 FINISH_OFF:
7797         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7798         if (hStmt != NULL) {
7799                 rc = sqlite3_finalize(hStmt);
7800                 if (rc != SQLITE_OK) {
7801                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7802                         error = EMAIL_ERROR_DB_FAILURE;
7803                 }
7804         }
7805
7806         if (err_code != NULL)
7807                 *err_code = error;
7808
7809         EM_DEBUG_FUNC_END("ret [%d]", ret);
7810         return ret;
7811 }
7812
7813 INTERNAL_FUNC int emstorage_delete_rule(char *multi_user_name, int rule_id, int transaction, int *err_code)
7814 {
7815         EM_DEBUG_FUNC_BEGIN("rule_id[%d], transaction[%d], err_code[%p]", rule_id, transaction, err_code);
7816
7817         if (rule_id <= 0) {             /*  only global rule supported. */
7818                 EM_DEBUG_EXCEPTION("rule_id[%d]", rule_id);
7819
7820                 if (err_code != NULL)
7821                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7822                 return false;
7823         }
7824
7825         int rc, ret = false;
7826         int error = EMAIL_ERROR_NONE;
7827         char sql_query_string[QUERY_SIZE] = {0, };
7828         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7829         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7830
7831         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7832         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7833         if (error != EMAIL_ERROR_NONE) {
7834                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7835                         goto FINISH_OFF;
7836         }
7837
7838         rc = sqlite3_changes(local_db_handle);
7839         if (rc == 0) {
7840                 EM_DEBUG_EXCEPTION(" no matched rule found...");
7841
7842                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7843                 goto FINISH_OFF;
7844         }
7845
7846         ret = true;
7847
7848 FINISH_OFF:
7849         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7850
7851         if (err_code != NULL)
7852                 *err_code = error;
7853
7854         EM_DEBUG_FUNC_END("ret [%d]", ret);
7855         return ret;
7856 }
7857
7858 INTERNAL_FUNC int emstorage_free_rule(emstorage_rule_tbl_t** rule_list, int count, int *err_code)
7859 {
7860         EM_DEBUG_FUNC_BEGIN("rule_list[%p], conut[%d], err_code[%p]", rule_list, count, err_code);
7861
7862         int ret = false;
7863         int error = EMAIL_ERROR_NONE;
7864
7865         if (count > 0) {
7866                 if (!rule_list || !*rule_list) {
7867                         EM_DEBUG_EXCEPTION(" rule_list[%p], conut[%d]", rule_list, count);
7868
7869                         error = EMAIL_ERROR_INVALID_PARAM;
7870                         goto FINISH_OFF;
7871                 }
7872
7873                 emstorage_rule_tbl_t* p = *rule_list;
7874                 int i = 0;
7875
7876                 for (; i < count; i++) {
7877                         EM_SAFE_FREE(p[i].value);
7878                 }
7879
7880                 EM_SAFE_FREE(p); *rule_list = NULL;
7881         }
7882
7883 FINISH_OFF:
7884         if (err_code != NULL)
7885                 *err_code = error;
7886
7887         EM_DEBUG_FUNC_END("ret [%d]", ret);
7888         return ret;
7889 }
7890
7891 INTERNAL_FUNC int emstorage_get_mail_count(char *multi_user_name, int account_id, int mailbox_id, int *total, int *unseen, int transaction, int *err_code)
7892 {
7893         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);
7894
7895         if (!total && !unseen) {
7896                 EM_DEBUG_EXCEPTION(" accoun_id[%d], mailbox_id[%d], total[%p], unseen[%p]", account_id, mailbox_id, total, unseen);
7897                 if (err_code != NULL)
7898                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7899                 return false;
7900         }
7901
7902         int rc = -1, ret = false;
7903         int error = EMAIL_ERROR_NONE;
7904         DB_STMT hStmt = NULL;
7905         char sql_query_string[QUERY_SIZE] = {0, };
7906         char *replaced_mailbox_name = NULL;
7907
7908         memset(&sql_query_string, 0x00, sizeof(sql_query_string));
7909         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7910         EMSTORAGE_START_READ_TRANSACTION(transaction);
7911
7912         if (total) {
7913                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl");
7914
7915                 if (account_id != ALL_ACCOUNT) {
7916                         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);
7917                         if (mailbox_id)
7918                                 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);
7919                 } else if (mailbox_id)
7920                         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);
7921
7922 #ifdef USE_GET_RECORD_COUNT_API
7923                 char **result;
7924
7925                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7926                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF2; },
7927                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7928
7929                 *total = atoi(result[1]);
7930                 sqlite3_free_table(result);
7931 #else
7932
7933                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7934                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF2; },
7935                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7936
7937                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7938                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF2; },
7939                         ("sqlite3_step fail:%d", rc));
7940                 _get_stmt_field_data_int(hStmt, total, 0);
7941 #endif          /*  USE_GET_RECORD_COUNT_API */
7942         }
7943
7944         if (unseen) {
7945                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl WHERE flags_seen_field = 0");               /*  fSEEN = 0x01 */
7946
7947                 if (account_id != ALL_ACCOUNT)
7948                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND account_id = %d", account_id);
7949
7950                 if (mailbox_id) {
7951                         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);
7952                 } else
7953                         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)");
7954
7955                 char **result;
7956                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7957                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7958                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7959
7960                 *unseen = atoi(result[1]);
7961                 sqlite3_free_table(result);
7962
7963         }
7964 FINISH_OFF:
7965         ret = true;
7966
7967 FINISH_OFF2:
7968
7969 #ifndef USE_PREPARED_QUERY_
7970         if (hStmt != NULL) {
7971                 rc = sqlite3_finalize(hStmt);
7972                 if (rc != SQLITE_OK) {
7973                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7974                         error = EMAIL_ERROR_DB_FAILURE;
7975                 }
7976         }
7977 #endif
7978
7979         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7980
7981         EM_SAFE_FREE(replaced_mailbox_name);
7982
7983         if (err_code != NULL)
7984                 *err_code = error;
7985
7986         EM_DEBUG_FUNC_END("ret [%d]", ret);
7987         return ret;
7988 }
7989
7990 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)
7991 {
7992         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
7993
7994         if (mail_id <= 0 || !mail) {
7995                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
7996                 if (err_code != NULL)
7997                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7998                 return false;
7999         }
8000
8001         int col_index = 0;
8002         emstorage_mail_tbl_t* p_data_tbl = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
8003
8004         if (p_data_tbl == NULL) {
8005                 EM_DEBUG_EXCEPTION("malloc failed...");
8006                 if (err_code != NULL)
8007                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8008                 return false;
8009         }
8010
8011         memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8012         DB_STMT hStmt = NULL;
8013         char sql_query_string[QUERY_SIZE] = {0, };
8014
8015         int rc, ret = false;
8016         int error = EMAIL_ERROR_NONE;
8017
8018         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8019         EMSTORAGE_START_READ_TRANSACTION(transaction);
8020
8021         switch (type) {
8022                 case RETRIEVE_SUMMARY:
8023                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8024                                 "SELECT account_id, "
8025                                 "mail_id, "
8026                                 "mailbox_id, "
8027                                 "server_mail_status, "
8028                                 "server_mailbox_name, "
8029                                 "server_mail_id, "
8030                                 "file_path_plain, "
8031                                 "file_path_html,"
8032                                 "file_path_mime_entity, "
8033                                 "flags_seen_field, "
8034                                 "save_status, "
8035                                 "lock_status, "
8036                                 "thread_id, "
8037                                 "thread_item_count "
8038                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8039                         break;
8040
8041                 case RETRIEVE_FIELDS_FOR_DELETE:
8042                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8043                                 "SELECT account_id, "
8044                                 "mail_id, "
8045                                 "server_mail_status, "
8046                                 "server_mailbox_name, "
8047                                 "server_mail_id "
8048                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8049                         break;
8050
8051                 case RETRIEVE_ACCOUNT:
8052                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8053                                 "SELECT account_id "
8054                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8055                         break;
8056
8057                 case RETRIEVE_FLAG:
8058                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8059                                 "SELECT account_id, "
8060                                 "flags_seen_field, "
8061                                 "thread_id, "
8062                                 "mailbox_id "
8063                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8064                         break;
8065
8066                 default:
8067                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8068                         error = EMAIL_ERROR_INVALID_PARAM;
8069                         goto FINISH_OFF;
8070         }
8071
8072         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8073
8074         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8075         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8076                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8077
8078
8079         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8080         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8081                 ("sqlite3_step fail:%d", rc));
8082
8083         if (rc == SQLITE_DONE) {
8084                 EM_DEBUG_LOG("no matched mail found...");
8085                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8086                 goto FINISH_OFF;
8087         }
8088         switch (type) {
8089                 case RETRIEVE_SUMMARY:
8090                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8091                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8092                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8093                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8094                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8095                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8096                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, col_index++);
8097                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, col_index++);
8098                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, col_index++);
8099                         _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8100                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), col_index++);
8101                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), col_index++);
8102                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8103                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_item_count), col_index++);
8104                         break;
8105
8106                 case RETRIEVE_FIELDS_FOR_DELETE:
8107                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8108                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8109                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8110                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8111                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8112                         break;
8113
8114                 case RETRIEVE_ACCOUNT:
8115                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8116                         break;
8117
8118                 case RETRIEVE_FLAG:
8119                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8120                         _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8121                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8122                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8123                         break;
8124         }
8125
8126         ret = true;
8127
8128 FINISH_OFF:
8129         if (ret == true)
8130                 *mail = p_data_tbl;
8131         else if (p_data_tbl != NULL)
8132                 emstorage_free_mail(&p_data_tbl,  1, NULL);
8133
8134         if (hStmt != NULL) {
8135                 rc = sqlite3_finalize(hStmt);
8136                 if (rc != SQLITE_OK) {
8137                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8138                         error = EMAIL_ERROR_DB_FAILURE;
8139                 }
8140         }
8141
8142
8143         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8144
8145         if (err_code != NULL)
8146                 *err_code = error;
8147
8148         EM_DEBUG_FUNC_END("ret [%d]", ret);
8149         return ret;
8150 }
8151
8152 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)
8153 {
8154         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);
8155
8156         int ret = false;
8157         int error = EMAIL_ERROR_NONE;
8158         int query_string_length = 0;
8159         int i = 0, item_count = 0, rc = -1, field_count, col_index, cur_sql_query_string = 0;
8160         char **result = NULL;
8161         char *sql_query_string = NULL;
8162         emstorage_mail_tbl_t* p_data_tbl = NULL;
8163         sqlite3 *local_db_handle = NULL;
8164
8165         if (number_of_mails <= 0 || !mail_ids) {
8166                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8167                 if (err_code != NULL)
8168                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8169                 return false;
8170         }
8171
8172         p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * number_of_mails);
8173
8174         query_string_length = (sizeof(char) * 8 * number_of_mails) + 512;
8175         sql_query_string = (char*)em_malloc(query_string_length);
8176
8177         if (p_data_tbl == NULL || sql_query_string == NULL) {
8178                 EM_DEBUG_EXCEPTION("malloc failed...");
8179
8180                 EM_SAFE_FREE(p_data_tbl);
8181                 EM_SAFE_FREE(sql_query_string);
8182
8183                 if (err_code != NULL)
8184                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8185                 return false;
8186         }
8187
8188         local_db_handle = emstorage_get_db_connection(multi_user_name);
8189
8190         EMSTORAGE_START_READ_TRANSACTION(transaction);
8191
8192         switch (type) {
8193                 case RETRIEVE_SUMMARY:
8194                         cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8195                                 "SELECT account_id, "
8196                                 "mail_id, "
8197                                 "mailbox_id, "
8198                                 "server_mail_status, "
8199                                 "server_mailbox_name, "
8200                                 "server_mail_id, "
8201                                 "file_path_plain, "
8202                                 "file_path_html, "
8203                                 "file_path_mime_entity, "
8204                                 "subject, "
8205                                 "flags_seen_field, "
8206                                 "save_status, "
8207                                 "lock_status, "
8208                                 "thread_id, "
8209                                 "thread_item_count "
8210                                 "FROM mail_tbl WHERE mail_id in (");
8211                         field_count = 15;
8212                         break;
8213
8214                 case RETRIEVE_FIELDS_FOR_DELETE:
8215                         cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8216                                 "SELECT account_id, "
8217                                 "mail_id, "
8218                                 "server_mail_status, "
8219                                 "server_mailbox_name, "
8220                                 "server_mail_id "
8221                                 "FROM mail_tbl WHERE mail_id in (");
8222                         field_count = 5;
8223                         break;
8224
8225                 case RETRIEVE_ACCOUNT:
8226                         cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8227                                 "SELECT account_id FROM mail_tbl WHERE mail_id in (");
8228                         field_count = 1;
8229                         break;
8230
8231                 case RETRIEVE_FLAG:
8232                         cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8233                                 "SELECT account_id, "
8234                                 "mail_id, "
8235                                 "mailbox_id, "
8236                                 "flags_seen_field, "
8237                                 "thread_id "
8238                                 "FROM mail_tbl WHERE mail_id in (");
8239                         field_count = 5;
8240                         break;
8241
8242                 default:
8243                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8244                         error = EMAIL_ERROR_INVALID_PARAM;
8245                         goto FINISH_OFF;
8246         }
8247
8248         for (i = 0; i < number_of_mails; i++)
8249                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_string_length, "%d,", mail_ids[i]);
8250         sql_query_string[EM_SAFE_STRLEN(sql_query_string) - 1] = ')';
8251
8252         EM_DEBUG_LOG_SEC("Query [%s], Length [%d]", sql_query_string, EM_SAFE_STRLEN(sql_query_string));
8253
8254         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &item_count, 0, NULL), rc);
8255         if (SQLITE_OK != rc && -1 != rc) {
8256                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
8257                 error = EMAIL_ERROR_DB_FAILURE;
8258                 goto FINISH_OFF;
8259         }
8260
8261         if (rc == SQLITE_DONE) {
8262                 EM_DEBUG_LOG("no matched mail found...");
8263                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8264                 goto FINISH_OFF;
8265         }
8266
8267         EM_DEBUG_LOG("item_count [%d]", item_count);
8268
8269         if (number_of_mails != item_count) {
8270                 EM_DEBUG_EXCEPTION("Can't find all emails");
8271                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8272                 goto FINISH_OFF;
8273         }
8274
8275         col_index = field_count;
8276
8277         for (i = 0; i < item_count; i++)        {
8278                 switch (type) {
8279                         case RETRIEVE_SUMMARY:
8280                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8281                                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8282                                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8283                                 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8284                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8285                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8286                                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
8287                                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
8288                                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
8289                                 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 0, col_index++);
8290                                 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8291                                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
8292                                 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
8293                                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8294                                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
8295                                 break;
8296
8297                         case RETRIEVE_FIELDS_FOR_DELETE:
8298                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8299                                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8300                                 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8301                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8302                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8303                                 break;
8304
8305                         case RETRIEVE_ACCOUNT:
8306                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8307                                 break;
8308
8309                         case RETRIEVE_FLAG:
8310                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8311                                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8312                                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8313                                 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8314                                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8315                                 break;
8316                 }
8317         }
8318
8319         ret = true;
8320
8321 FINISH_OFF:
8322         if (ret == true)
8323                 *mail = p_data_tbl;
8324         else
8325                 emstorage_free_mail(&p_data_tbl, number_of_mails, NULL);
8326
8327         if (result)
8328                 sqlite3_free_table(result);
8329
8330         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8331
8332         EM_SAFE_FREE(sql_query_string);
8333
8334         if (err_code != NULL)
8335                 *err_code = error;
8336
8337         EM_DEBUG_FUNC_END("ret [%d]", ret);
8338         return ret;
8339 }
8340
8341 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)
8342 {
8343         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8344
8345         if (mail_id <= 0 || !mail) {
8346                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
8347                 if (err_code != NULL)
8348                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8349                 return false;
8350         }
8351
8352         int ret = false, error = EMAIL_ERROR_NONE, count;
8353         char conditional_clause[QUERY_SIZE] = {0, };
8354         emstorage_mail_tbl_t* p_data_tbl = NULL;
8355
8356         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8357         EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8358
8359         if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8360                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8361                 goto FINISH_OFF;
8362         }
8363
8364         ret = true;
8365
8366 FINISH_OFF:
8367         if (ret == true)
8368                 *mail = p_data_tbl;
8369         else if (p_data_tbl != NULL)
8370                 emstorage_free_mail(&p_data_tbl, 1, &error);
8371
8372         if (err_code != NULL)
8373                 *err_code = error;
8374
8375         EM_DEBUG_FUNC_END("ret [%d]", ret);
8376         return ret;
8377 }
8378
8379 #ifdef __FEATURE_BODY_SEARCH__
8380 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)
8381 {
8382         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
8383
8384         if (mail_id <= 0 || !mail_text) {
8385                 EM_DEBUG_EXCEPTION("mail_id[%d], mail_text[%p]", mail_id, mail_text);
8386                 if (err_code != NULL)
8387                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8388                 return false;
8389         }
8390
8391         int ret = false;
8392         int error = EMAIL_ERROR_NONE;
8393         int count = 0;
8394         char conditional_clause[QUERY_SIZE] = {0, };
8395         emstorage_mail_text_tbl_t *p_data_tbl = NULL;
8396
8397         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8398         EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8399
8400         if (!emstorage_query_mail_text_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8401                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8402                 goto FINISH_OFF;
8403         }
8404
8405         ret = true;
8406
8407 FINISH_OFF:
8408         if (ret == true)
8409                 *mail_text = p_data_tbl;
8410         else if (p_data_tbl != NULL)
8411                 emstorage_free_mail_text(&p_data_tbl, 1, &error);
8412
8413         if (err_code != NULL)
8414                 *err_code = error;
8415
8416         EM_DEBUG_FUNC_END("ret [%d]", ret);
8417         return ret;
8418 }
8419 #endif
8420
8421 INTERNAL_FUNC int emstorage_mail_search_start(char *multi_user_name,
8422                                                                                         emstorage_search_filter_t *search,
8423                                                                                         int account_id,
8424                                                                                         int mailbox_id,
8425                                                                                         int sorting,
8426                                                                                         DB_STMT *search_handle,
8427                                                                                         int *searched,
8428                                                                                         int transaction,
8429                                                                                         int *err_code)
8430 {
8431         EM_DEBUG_FUNC_BEGIN("search[%p], account_id[%d], mailbox_id[%d], sorting[%d], "
8432                                                 "search_handle[%p], searched[%p], transaction[%d], err_code[%p]",
8433                                                 search, account_id, mailbox_id, sorting, search_handle,
8434                                                 searched, transaction, err_code);
8435
8436         if (!search_handle || !searched) {
8437                 if (err_code != NULL)
8438                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8439                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8440                 EM_DEBUG_FUNC_END("false");
8441                 return false;
8442         }
8443
8444         emstorage_search_filter_t* p = search;
8445         int error = EMAIL_ERROR_NONE;
8446         DB_STMT hStmt = NULL;
8447         char sql_query_string[QUERY_SIZE] = {0, };
8448         int rc, ret = false;
8449         int and = false, mail_count = 0;
8450
8451         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8452         EMSTORAGE_START_READ_TRANSACTION(transaction);
8453
8454         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl");
8455
8456         if (account_id != ALL_ACCOUNT) {
8457                 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);
8458                 and = true;
8459         }
8460
8461         if (mailbox_id) {
8462                 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);
8463                 and = true;
8464         }
8465
8466         while (p) {
8467                 if (p->key_type) {
8468                         if (!strncmp(p->key_type, "subject", strlen("subject"))) {
8469                                 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);
8470                                 and = true;
8471                         } else if (!strncmp(p->key_type, "full_address_from", strlen("full_address_from"))) {
8472                                 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);
8473                                 and = true;
8474                         } else if (!strncmp(p->key_type, "full_address_to", strlen("full_address_to"))) {
8475                                 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);
8476                                 and = true;
8477                         } else if (!strncmp(p->key_type, "email_address", strlen("email_address"))) {
8478                                 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);
8479                                 and = true;
8480                         }
8481                         p = p->next;
8482                 }
8483         }
8484
8485         if (sorting)
8486                 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");
8487
8488         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
8489
8490
8491         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8492         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8493                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8494
8495
8496         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8497         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8498                 ("sqlite3_step fail:%d", rc));
8499
8500         char **result;
8501
8502         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &mail_count, NULL, NULL), rc);
8503         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
8504                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8505
8506         sqlite3_free_table(result);
8507
8508         ret = true;
8509
8510 FINISH_OFF:
8511         if (ret == true) {
8512                 *search_handle = hStmt;
8513                 *searched = mail_count;
8514                 EM_DEBUG_LOG("mail_count [%d]", mail_count);
8515         } else {
8516                 if (hStmt != NULL) {
8517                         rc = sqlite3_finalize(hStmt);
8518                         if (rc != SQLITE_OK) {
8519                                 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8520                                 error = EMAIL_ERROR_DB_FAILURE;
8521                         }
8522                 }
8523
8524                 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8525         }
8526
8527         if (err_code != NULL)
8528                 *err_code = error;
8529
8530         EM_DEBUG_FUNC_END("ret [%d]", ret);
8531         return ret;
8532 }
8533
8534 INTERNAL_FUNC int emstorage_mail_search_result(DB_STMT search_handle, emstorage_mail_field_type_t type, void** data, int transaction, int *err_code)
8535 {
8536         EM_DEBUG_FUNC_BEGIN("search_handle[%d], type[%d], data[%p], transaction[%d], err_code[%p]", search_handle, type, data, transaction, err_code);
8537
8538         if (search_handle == 0 || !data) {
8539                 EM_DEBUG_EXCEPTION(" search_handle[%d], type[%d], data[%p]", search_handle, type, data);
8540
8541                 if (err_code != NULL)
8542                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8543                 return false;
8544         }
8545
8546         emstorage_mail_tbl_t* p_data_tbl = NULL;
8547         DB_STMT hStmt = search_handle;
8548         int rc, ret = false;
8549         int error = EMAIL_ERROR_NONE;
8550
8551         switch (type) {
8552                 case RETRIEVE_ID:
8553                         _get_stmt_field_data_int(hStmt, (int *)data, MAIL_ID_IDX_IN_MAIL_TBL);
8554                         break;
8555
8556                 case RETRIEVE_ENVELOPE:
8557                 case RETRIEVE_ALL:
8558                         if (!(p_data_tbl = em_malloc(sizeof(emstorage_mail_tbl_t)))) {
8559                                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
8560                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
8561                                 goto FINISH_OFF;
8562                         }
8563
8564                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8565                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8566                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_size), MAIL_SIZE_IDX_IN_MAIL_TBL);
8567                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8568                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_from), 1, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL);
8569                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_to), 1, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL);
8570                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject), 1, SUBJECT_IDX_IN_MAIL_TBL);
8571                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->body_download_status), BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL);
8572                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL);
8573                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8574                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8575                         _get_stmt_field_data_time_t(hStmt, &(p_data_tbl->date_time), DATETIME_IDX_IN_MAIL_TBL);
8576                         _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL);
8577                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->DRM_status), DRM_STATUS_IDX_IN_MAIL_TBL);
8578                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->priority), PRIORITY_IDX_IN_MAIL_TBL);
8579                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), SAVE_STATUS_IDX_IN_MAIL_TBL);
8580                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), LOCK_STATUS_IDX_IN_MAIL_TBL);
8581                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->report_status), REPORT_STATUS_IDX_IN_MAIL_TBL);
8582                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8583                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->meeting_request_status), MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL);
8584                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->message_class), MESSAGE_CLASS_IDX_IN_MAIL_TBL);
8585                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->digest_type), DIGEST_TYPE_IDX_IN_MAIL_TBL);
8586                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->smime_type), SMIME_TYPE_IDX_IN_MAIL_TBL);
8587                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->scheduled_sending_time), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8588                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->remaining_resend_times), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8589                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->tag_id), TAG_ID_IDX_IN_MAIL_TBL);
8590                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->eas_data_length), EAS_DATA_LENGTH_IDX_IN_MAIL_TBL);
8591                         _get_stmt_field_data_blob(hStmt, (void**)&(p_data_tbl->eas_data), EAS_DATA_IDX_IN_MAIL_TBL);
8592
8593                         if (type == RETRIEVE_ALL) {
8594                                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8595                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8596                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_reply), 1, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL);
8597                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_cc), 1, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL);
8598                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_bcc), 1, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL);
8599                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_return), 1, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL);
8600                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->message_id), 0, MESSAGE_ID_IDX_IN_MAIL_TBL);
8601                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8602                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8603                                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_count), ATTACHMENT_COUNT_IDX_IN_MAIL_TBL);
8604                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8605                                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->replied_time), REPLIED_TIME_IDX_IN_MAIL_TBL);
8606                                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->forwarded_time), FORWARDED_TIME_IDX_IN_MAIL_TBL);
8607                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->default_charset), 0, DEFAULT_CHARSET_IDX_IN_MAIL_TBL);
8608                         }
8609
8610                         if (p_data_tbl->body_download_status) {
8611                                 struct stat buf;
8612
8613                                 if (p_data_tbl->file_path_html) {
8614                                         if (stat(p_data_tbl->file_path_html, &buf) == -1)
8615                                                 p_data_tbl->body_download_status = 0;
8616                                 } else if (p_data_tbl->file_path_plain) {
8617                                         if (stat(p_data_tbl->file_path_plain, &buf) == -1)
8618                                                 p_data_tbl->body_download_status = 0;
8619                                 } else
8620                                         p_data_tbl->body_download_status = 0;
8621                         }
8622
8623                         *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8624                         break;
8625
8626                 case RETRIEVE_SUMMARY:
8627                         if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8628                                 EM_DEBUG_EXCEPTION(" malloc failed...");
8629
8630                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
8631                                 goto FINISH_OFF;
8632                         }
8633
8634                         memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8635
8636                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8637                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8638                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8639                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8640                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8641
8642                         *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8643                         break;
8644
8645                 case RETRIEVE_ADDRESS:
8646                         if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8647                                 EM_DEBUG_EXCEPTION(" malloc failed...");
8648                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
8649                                 goto FINISH_OFF;
8650                         }
8651
8652                         memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8653                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8654                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8655                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8656                         *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8657                         break;
8658
8659                 default:
8660                         break;
8661         }
8662
8663         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8664         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8665                 ("sqlite3_step fail:%d", rc));
8666
8667         ret = true;
8668
8669 FINISH_OFF:
8670
8671         if (err_code != NULL)
8672                 *err_code = error;
8673
8674         if (ret == false && p_data_tbl)
8675                 emstorage_free_mail(&p_data_tbl, 1, NULL);
8676
8677         EM_DEBUG_FUNC_END("ret [%d]", ret);
8678         return ret;
8679 }
8680
8681 INTERNAL_FUNC int emstorage_mail_search_end(DB_STMT search_handle, int transaction, int *err_code)
8682 {
8683         EM_DEBUG_FUNC_BEGIN("search_handle[%d], transaction[%d], err_code[%p]", search_handle, transaction, err_code);
8684
8685         int error = EMAIL_ERROR_NONE;
8686         int rc, ret = false;
8687
8688         if (search_handle == 0) {
8689                 EM_DEBUG_EXCEPTION(" search_handle[%d]", search_handle);
8690                 error = EMAIL_ERROR_INVALID_PARAM;
8691                 goto FINISH_OFF;
8692         }
8693
8694         DB_STMT hStmt = search_handle;
8695
8696         rc = sqlite3_finalize(hStmt);
8697         if (rc != SQLITE_OK) {
8698                 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8699                 error = EMAIL_ERROR_DB_FAILURE;
8700         }
8701
8702         ret = true;
8703
8704 FINISH_OFF:
8705         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8706
8707         if (err_code != NULL)
8708                 *err_code = error;
8709
8710         EM_DEBUG_FUNC_END("ret [%d]", ret);
8711         return ret;
8712 }
8713
8714 INTERNAL_FUNC int emstorage_change_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t *mail, int transaction, int *err_code)
8715 {
8716         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8717
8718         if (mail_id <= 0 || !mail) {
8719                 EM_DEBUG_EXCEPTION(" mail_id[%d], mail[%p]", mail_id, mail);
8720
8721                 if (err_code != NULL)
8722                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8723                 return false;
8724         }
8725
8726         DB_STMT hStmt = NULL;
8727         char sql_query_string[QUERY_SIZE] = {0, };
8728         int rc = -1;
8729         int ret = false;
8730         int error = EMAIL_ERROR_NONE;
8731         int i = 0;
8732         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8733         char mailbox_id_param_string[10] = {0,};
8734
8735         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
8736
8737         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8738                 "UPDATE mail_tbl SET"
8739                 "  mail_id = ?"
8740                 ", account_id = ?"
8741                 ", mailbox_id = ?"
8742                 ", mail_size = ?"
8743                 ", server_mail_status = ?"
8744                 ", server_mailbox_name = ?"
8745                 ", server_mail_id = ?"
8746                 ", reference_mail_id = ?"
8747                 ", full_address_from = ?"
8748                 ", full_address_reply = ?"  /* 10 */
8749                 ", full_address_to = ?"
8750                 ", full_address_cc = ?"
8751                 ", full_address_bcc = ?"
8752                 ", full_address_return = ?"
8753                 ", subject = ?"
8754                 ", body_download_status = ?"
8755                 ", file_path_plain = ?"
8756                 ", file_path_html = ?"
8757                 ", file_path_mime_entity = ?"
8758                 ", date_time = ?"
8759                 ", flags_seen_field      = ?"
8760                 ", flags_deleted_field   = ?"
8761                 ", flags_flagged_field   = ?"
8762                 ", flags_answered_field  = ?"
8763                 ", flags_recent_field    = ?"
8764                 ", flags_draft_field     = ?"
8765                 ", flags_forwarded_field = ?"
8766                 ", DRM_status = ?"
8767                 ", priority = ?"
8768                 ", save_status = ?"
8769                 ", lock_status = ?"
8770                 ", message_id = ?"
8771                 ", report_status = ?"
8772                 ", preview_text = ?"
8773                 ", smime_type = ?"
8774                 ", scheduled_sending_time = ?"
8775                 ", remaining_resend_times = ?"
8776                 ", tag_id = ?"
8777                 ", replied_time = ?"
8778                 ", forwarded_time = ?"
8779                 ", default_charset = ?"
8780                 ", eas_data_length = ?"
8781                 ", eas_data = ?"
8782                 " WHERE mail_id = %d AND account_id != 0 "
8783                 , mail_id);
8784
8785
8786         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8787         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8788                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8789
8790         _bind_stmt_field_data_int(hStmt, i++, mail->mail_id);
8791         _bind_stmt_field_data_int(hStmt, i++, mail->account_id);
8792         _bind_stmt_field_data_int(hStmt, i++, mail->mailbox_id);
8793         _bind_stmt_field_data_int(hStmt, i++, mail->mail_size);
8794         _bind_stmt_field_data_int(hStmt, i++, mail->server_mail_status);
8795         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mailbox_name, 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
8796         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mail_id, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
8797         _bind_stmt_field_data_int(hStmt, i++, mail->reference_mail_id);
8798         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
8799         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
8800         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
8801         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
8802         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
8803         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
8804         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8805         _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8806         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8807         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8808         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
8809         _bind_stmt_field_data_int(hStmt, i++, mail->date_time);
8810         _bind_stmt_field_data_char(hStmt, i++, mail->flags_seen_field);
8811         _bind_stmt_field_data_char(hStmt, i++, mail->flags_deleted_field);
8812         _bind_stmt_field_data_char(hStmt, i++, mail->flags_flagged_field);
8813         _bind_stmt_field_data_char(hStmt, i++, mail->flags_answered_field);
8814         _bind_stmt_field_data_char(hStmt, i++, mail->flags_recent_field);
8815         _bind_stmt_field_data_char(hStmt, i++, mail->flags_draft_field);
8816         _bind_stmt_field_data_char(hStmt, i++, mail->flags_forwarded_field);
8817         _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
8818         _bind_stmt_field_data_int(hStmt, i++, mail->priority);
8819         _bind_stmt_field_data_int(hStmt, i++, mail->save_status);
8820         _bind_stmt_field_data_int(hStmt, i++, mail->lock_status);
8821         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->message_id, 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
8822         _bind_stmt_field_data_int(hStmt, i++, mail->report_status);
8823         _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8824         _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
8825         _bind_stmt_field_data_int(hStmt, i++, mail->scheduled_sending_time);
8826         _bind_stmt_field_data_int(hStmt, i++, mail->remaining_resend_times);
8827         _bind_stmt_field_data_int(hStmt, i++, mail->tag_id);
8828         _bind_stmt_field_data_int(hStmt, i++, mail->replied_time);
8829         _bind_stmt_field_data_int(hStmt, i++, mail->forwarded_time);
8830         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
8831         _bind_stmt_field_data_int(hStmt, i++, mail->eas_data_length);
8832         _bind_stmt_field_data_blob(hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
8833
8834         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8835         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8836                 ("sqlite3_step fail:%d", rc));
8837
8838         rc = sqlite3_changes(local_db_handle);
8839         if (rc == 0) {
8840                 EM_DEBUG_LOG(" no matched mail found...");
8841                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8842                 goto FINISH_OFF;
8843         }
8844
8845         ret = true;
8846
8847 FINISH_OFF:
8848         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
8849         if (hStmt != NULL) {
8850                 rc = sqlite3_finalize(hStmt);
8851                 if (rc != SQLITE_OK) {
8852                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8853                         error = EMAIL_ERROR_DB_FAILURE;
8854                 }
8855         }
8856
8857
8858         if (error == EMAIL_ERROR_NONE && mail) {
8859                 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
8860                 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail->mail_id, mailbox_id_param_string, 0))
8861                         EM_DEBUG_EXCEPTION("emcore_notify_storage_eventfailed [NOTI_MAIL_UPDATE]");
8862         }
8863
8864         if (err_code != NULL)
8865                 *err_code = error;
8866
8867         EM_DEBUG_FUNC_END("ret [%d]", ret);
8868         return ret;
8869 }
8870
8871 /**
8872   *  emstorage_clean_save_status(int save_status, int  *err_code) - set the all mail status to the set value
8873   *
8874   *
8875   **/
8876 INTERNAL_FUNC int emstorage_clean_save_status(char *multi_user_name, int save_status, int  *err_code)
8877 {
8878         EM_DEBUG_FUNC_BEGIN("save_status[%d], err_code[%p]", save_status, err_code);
8879
8880         EM_IF_NULL_RETURN_VALUE(err_code, false);
8881
8882         int ret = false;
8883         int error = EMAIL_ERROR_NONE;
8884         int rc = 0;
8885         char sql_query_string[QUERY_SIZE] = {0, };
8886         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8887
8888         memset(sql_query_string, 0x00, sizeof(sql_query_string));
8889         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);
8890
8891         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8892         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
8893         if (error != EMAIL_ERROR_NONE) {
8894                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
8895                         goto FINISH_OFF;
8896         }
8897
8898         rc = sqlite3_changes(local_db_handle);
8899         if (rc == 0) {
8900                 EM_DEBUG_LOG(" No Matched Mail Exists ");
8901                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8902         }
8903
8904         ret = true;
8905
8906 FINISH_OFF:
8907
8908         if (err_code != NULL)
8909                 *err_code = error;
8910
8911         EM_DEBUG_FUNC_END("ret [%d]", ret);
8912         return ret;
8913 }
8914
8915 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)
8916 {
8917         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);
8918         int i = 0;
8919         int error = EMAIL_ERROR_NONE;
8920         int ret = false;
8921         int query_size = 0;
8922         int cur_mail_id_string = 0;
8923         int mail_id_string_buffer_length = 0;
8924         int parameter_string_length = 0;
8925         char  *sql_query_string = NULL;
8926         char *mail_id_string_buffer = NULL;
8927         char *parameter_string = NULL;
8928         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8929         email_mail_attribute_type target_mail_attribute_type = 0;
8930
8931         if (!mail_ids  || !field_name || account_id == 0) {
8932                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8933                 if (err_code != NULL)
8934                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8935                 return false;
8936         }
8937
8938         if ((error = emcore_get_attribute_type_by_mail_field_name(field_name, &target_mail_attribute_type)) != EMAIL_ERROR_NONE) {
8939                 EM_DEBUG_EXCEPTION("emstorageemcore_get_attribute_type_by_mail_field_name failed [%d]", error);
8940                 if (err_code != NULL)
8941                         *err_code = error;
8942                 return false;
8943         }
8944
8945         /* Generating mail id list string */
8946         mail_id_string_buffer_length = MAIL_ID_STRING_LENGTH * mail_ids_count;
8947
8948         mail_id_string_buffer = em_malloc(mail_id_string_buffer_length);
8949
8950         if (!mail_id_string_buffer) {
8951                 EM_DEBUG_EXCEPTION("em_mallocfailed");
8952                 if (err_code != NULL)
8953                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8954                 return false;
8955         }
8956
8957         for (i = 0; i < mail_ids_count; i++)
8958                 cur_mail_id_string += SNPRINTF_OFFSET(mail_id_string_buffer, cur_mail_id_string, mail_id_string_buffer_length, "%d,", mail_ids[i]);
8959
8960         if (EM_SAFE_STRLEN(mail_id_string_buffer) > 1)
8961                 mail_id_string_buffer[EM_SAFE_STRLEN(mail_id_string_buffer) - 1] = NULL_CHAR;
8962
8963         /* Generating notification parameter string */
8964         parameter_string_length = mail_id_string_buffer_length + EM_SAFE_STRLEN(field_name) + 2;
8965         parameter_string = em_malloc(parameter_string_length);
8966
8967         if (!parameter_string) {
8968                 EM_DEBUG_EXCEPTION("em_mallocfailed");
8969                 if (err_code != NULL)
8970                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8971                 EM_SAFE_FREE(mail_id_string_buffer);
8972                 return false;
8973         }
8974
8975         SNPRINTF(parameter_string, parameter_string_length, "%s%c%s", field_name, 0x01, mail_id_string_buffer);
8976         query_size = EM_SAFE_STRLEN(mail_id_string_buffer) + EM_SAFE_STRLEN(field_name) + 250;
8977
8978         sql_query_string = em_malloc(query_size);
8979         if (sql_query_string == NULL) {
8980                 EM_DEBUG_EXCEPTION("em_mallocfailed");
8981                 error = EMAIL_ERROR_OUT_OF_MEMORY;
8982                 goto FINISH_OFF;
8983         }
8984         /* Write query string */
8985         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);
8986
8987         EM_DEBUG_LOG_DEV("sql_query_string [%s]", sql_query_string);
8988
8989         /* Execute query */
8990         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
8991         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
8992         if (error != EMAIL_ERROR_NONE) {
8993                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
8994                         goto FINISH_OFF;
8995         }
8996
8997         if (sqlite3_changes(local_db_handle) == 0)
8998                 EM_DEBUG_LOG("no mail matched...");
8999
9000         ret = true;
9001
9002 FINISH_OFF:
9003         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9004
9005         if (error == EMAIL_ERROR_NONE && parameter_string) {
9006                 if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, target_mail_attribute_type, parameter_string, value))
9007                         EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_MAIL_FIELD_UPDATE [%s,%d]", field_name, value);
9008         }
9009
9010         EM_SAFE_FREE(mail_id_string_buffer);
9011         EM_SAFE_FREE(parameter_string);
9012         EM_SAFE_FREE(sql_query_string);
9013
9014
9015         if (err_code != NULL)
9016                 *err_code = error;
9017
9018         EM_DEBUG_FUNC_END("error [%d]", error);
9019         return ret;
9020 }
9021
9022 #ifdef __FEATURE_BODY_SEARCH__
9023 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)
9024 {
9025         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
9026
9027         if (mail_id <= 0 || !mail_text) {
9028                 EM_DEBUG_EXCEPTION(" mail_id[%d], mail_text[%p]", mail_id, mail_text);
9029                 if (err_code != NULL)
9030                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9031                 return false;
9032         }
9033
9034         int ret = false;
9035         int error = EMAIL_ERROR_NONE;
9036         DB_STMT hStmt = NULL;
9037         char sql_query_string[QUERY_SIZE] = {0, };
9038
9039         int i = 0;
9040         int rc = 0;
9041
9042         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9043         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9044
9045         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9046                 "UPDATE mail_text_tbl SET"
9047                 " body_text = ?"
9048                 " WHERE mail_id = %d AND account_id != 0"
9049                 , mail_id);
9050         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9051
9052         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9053         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9054                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9055
9056         i = 0;
9057         _bind_stmt_field_data_string(hStmt, i++, (char *)mail_text->body_text, 0, -1);
9058
9059         if (hStmt != NULL) {
9060                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9061                 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9062                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9063
9064                 rc = sqlite3_changes(local_db_handle);
9065                 if (rc == 0) {
9066                         EM_DEBUG_LOG(" no matched mail found...");
9067                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
9068                         goto FINISH_OFF;
9069                 }
9070         }
9071
9072         ret = true;
9073
9074 FINISH_OFF:
9075         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9076
9077         if (hStmt != NULL) {
9078                 rc = sqlite3_finalize(hStmt);
9079                 if (rc != SQLITE_OK) {
9080                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9081                         error = EMAIL_ERROR_DB_FAILURE;
9082                 }
9083                 hStmt = NULL;
9084         }
9085
9086         if (err_code != NULL)
9087                 *err_code = error;
9088
9089         EM_DEBUG_FUNC_END("ret [%d]", ret);
9090         return ret;
9091 }
9092 #endif
9093
9094 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)
9095 {
9096         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
9097
9098         int ret = false;
9099         int error = EMAIL_ERROR_NONE;
9100         int move_flag = 0;
9101         int rc = 0;
9102         int i = 0;
9103         int mailbox_id = 0;
9104         DB_STMT hStmt = NULL;
9105         char sql_query_string[QUERY_SIZE] = {0, };
9106         char mailbox_id_param_string[10] = {0,};
9107         sqlite3 *local_db_handle = NULL;
9108
9109         if (mail_id <= 0 || !mail) {
9110                 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], mail[%p]", mail_id, type, mail);
9111                 if (err_code != NULL)
9112                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9113                 return false;
9114         }
9115
9116         local_db_handle = emstorage_get_db_connection(multi_user_name);
9117
9118         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9119
9120         switch (type) {
9121                 case APPEND_BODY:
9122                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9123                                 "UPDATE mail_tbl SET"
9124                                 "  body_download_status = ?"
9125                                 ", file_path_plain = ?"
9126                                 ", file_path_html = ?"
9127                                 ", file_path_mime_entity = ?"
9128                                 ", flags_seen_field      = ?"
9129                                 ", flags_deleted_field   = ?"
9130                                 ", flags_flagged_field   = ?"
9131                                 ", flags_answered_field  = ?"
9132                                 ", flags_recent_field    = ?"
9133                                 ", flags_draft_field     = ?"
9134                                 ", flags_forwarded_field = ?"
9135                                 ", DRM_status = ?"
9136                                 ", attachment_count = ?"
9137                                 ", preview_text= ?"
9138                                 ", meeting_request_status = ? "
9139                                 ", message_class = ? "
9140                                 ", digest_type = ? "
9141                                 ", smime_type = ? "
9142                                 " WHERE mail_id = %d AND account_id != 0"
9143                                 , mail_id);
9144
9145
9146                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9147                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9148                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9149                          i = 0;
9150
9151                         _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9152                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9153                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9154                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9155                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_seen_field);
9156                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_deleted_field);
9157                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_flagged_field);
9158                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_answered_field);
9159                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_recent_field);
9160                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_draft_field);
9161                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_forwarded_field);
9162                         _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9163                         _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9164                         _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9165                         _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9166                         _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9167                         _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9168                         _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9169                         break;
9170
9171                 case UPDATE_MAILBOX: {
9172                                 int err;
9173                                 emstorage_mailbox_tbl_t *mailbox_tbl;
9174
9175                                 if ((err = emstorage_get_mailbox_by_id(multi_user_name, mail->mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
9176                                         EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_id failed [%d]", err);
9177                                         goto FINISH_OFF;
9178                                 }
9179
9180                                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9181                                         "UPDATE mail_tbl SET"
9182                                         " mailbox_id = '%d'"
9183                                         ",mailbox_type = '%d'"
9184                                         " WHERE mail_id = %d AND account_id != 0"
9185                                         , mailbox_tbl->mailbox_id
9186                                         , mailbox_tbl->mailbox_type
9187                                         , mail_id);
9188                                         move_flag = 1;
9189
9190
9191                                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9192                                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9193                                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9194
9195                                 i = 0;
9196                                 _bind_stmt_field_data_string(hStmt, i++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
9197
9198                                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL); /*prevent 26251*/
9199
9200                         }
9201                         break;
9202
9203                 case UPDATE_FLAG:
9204                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9205                                 "UPDATE mail_tbl SET"
9206                                 " flags_seen_field      = %d"
9207                                 ",flags_deleted_field   = %d"
9208                                 ",flags_flagged_field   = %d"
9209                                 ",flags_answered_field  = %d"
9210                                 ",flags_recent_field    = %d"
9211                                 ",flags_draft_field     = %d"
9212                                 ",flags_forwarded_field = %d"
9213                                 "  WHERE mail_id = %d AND account_id != 0"
9214                                 , mail->flags_seen_field
9215                                 , mail->flags_deleted_field
9216                                 , mail->flags_flagged_field
9217                                 , mail->flags_answered_field
9218                                 , mail->flags_recent_field
9219                                 , mail->flags_draft_field
9220                                 , mail->flags_forwarded_field
9221                                 , mail_id);
9222                         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9223
9224
9225                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9226                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9227                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9228
9229                         break;
9230
9231                 case UPDATE_EXTRA_FLAG:
9232                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9233                                 "UPDATE mail_tbl SET"
9234                                 "  priority = %d"
9235                                 ", save_status = %d"
9236                                 ", lock_status = %d"
9237                                 ", report_status = %d"
9238                                 ", DRM_status = %d"
9239                                 " WHERE mail_id = %d AND account_id != 0"
9240                                 , mail->priority
9241                                 , mail->save_status
9242                                 , mail->lock_status
9243                                 , mail->report_status
9244                                 , mail->DRM_status
9245                                 , mail_id);
9246                         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9247
9248
9249                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9250                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9251                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9252                         break;
9253
9254                 case UPDATE_STICKY_EXTRA_FLAG:
9255                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9256                                 "UPDATE mail_tbl SET"
9257                                 "  lock_status = %d"
9258                                 "  WHERE mail_id = %d AND account_id != 0"
9259                                 , mail->lock_status
9260                                 , mail_id);
9261                         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9262
9263
9264                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9265                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9266                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9267                         break;
9268
9269                 case UPDATE_MAIL:
9270                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9271                                 "UPDATE mail_tbl SET"
9272                                 "  full_address_from = ?"
9273                                 ", full_address_reply = ?"
9274                                 ", full_address_to = ?"
9275                                 ", full_address_cc = ?"
9276                                 ", full_address_bcc = ?"
9277                                 ", full_address_return = ?"
9278                                 ", subject = ?"
9279                                 ", file_path_plain = ?"
9280                                 ", date_time = ?"
9281                                 ", flags_seen_field = ?"
9282                                 ", flags_deleted_field = ?"
9283                                 ", flags_flagged_field = ?"
9284                                 ", flags_answered_field = ?"
9285                                 ", flags_recent_field = ?"
9286                                 ", flags_draft_field = ?"
9287                                 ", flags_forwarded_field = ?"
9288                                 ", priority = ?"
9289                                 ", save_status = ?"
9290                                 ", lock_status = ?"
9291                                 ", report_status = ?"
9292                                 ", DRM_status = ?"
9293                                 ", file_path_html = ?"
9294                                 ", file_path_mime_entity = ?"
9295                                 ", mail_size = ?"
9296                                 ", preview_text = ?"
9297                                 ", body_download_status = ?"
9298                                 ", attachment_count = ?"
9299                                 ", inline_content_count = ?"
9300                                 ", meeting_request_status = ?"
9301                                 ", message_class = ?"
9302                                 ", digest_type = ?"
9303                                 ", smime_type = ?"
9304                                 ", scheduled_sending_time = ?"
9305                                 ", remaining_resend_times = ?"
9306                                 ", tag_id = ?"
9307                                 ", replied_time = ?"
9308                                 ", forwarded_time = ?"
9309                                 ", default_charset = ?"
9310                                 ", eas_data_length = ?"
9311                                 ", eas_data = ?"
9312                                 " WHERE mail_id = %d AND account_id != 0"
9313                                 , mail_id);
9314
9315
9316                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9317                         EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
9318                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9319                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9320                         i = 0;
9321                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
9322                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
9323                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
9324                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
9325                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
9326                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
9327                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9328                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9329                         _bind_stmt_field_data_time_t(hStmt, i++, mail->date_time);
9330                         _bind_stmt_field_data_char(hStmt, i++, mail->flags_seen_field);
9331                         _bind_stmt_field_data_char(hStmt, i++, mail->flags_deleted_field);
9332                         _bind_stmt_field_data_char(hStmt, i++, mail->flags_flagged_field);
9333                         _bind_stmt_field_data_char(hStmt, i++, mail->flags_answered_field);
9334                         _bind_stmt_field_data_char(hStmt, i++, mail->flags_recent_field);
9335                         _bind_stmt_field_data_char(hStmt, i++, mail->flags_draft_field);
9336                         _bind_stmt_field_data_char(hStmt, i++, mail->flags_forwarded_field);
9337                         _bind_stmt_field_data_int(hStmt, i++, mail->priority);
9338                         _bind_stmt_field_data_int(hStmt, i++, mail->save_status);
9339                         _bind_stmt_field_data_int(hStmt, i++, mail->lock_status);
9340                         _bind_stmt_field_data_int(hStmt, i++, mail->report_status);
9341                         _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9342                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9343                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9344                         _bind_stmt_field_data_int(hStmt, i++, mail->mail_size);
9345                         _bind_stmt_field_data_nstring(hStmt, i++, (char*)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9346                         _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9347                         _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9348                         _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9349                         _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9350                         _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9351                         _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9352                         _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9353                         _bind_stmt_field_data_int(hStmt, i++, mail->scheduled_sending_time);
9354                         _bind_stmt_field_data_int(hStmt, i++, mail->remaining_resend_times);
9355                         _bind_stmt_field_data_int(hStmt, i++, mail->tag_id);
9356                         _bind_stmt_field_data_int(hStmt, i++, mail->replied_time);
9357                         _bind_stmt_field_data_int(hStmt, i++, mail->forwarded_time);
9358                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
9359                         _bind_stmt_field_data_int(hStmt, i++, mail->eas_data_length);
9360                         _bind_stmt_field_data_blob(hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
9361                         break;
9362
9363                 case UPDATE_DATETIME: {
9364                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9365                                 "UPDATE mail_tbl SET"
9366                                 " date_time = '%ld'"
9367                                 " WHERE mail_id = %d AND account_id != 0"
9368                                 , mail->date_time
9369                                 , mail_id);
9370
9371                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9372                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9373                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9374                         break;
9375                 }
9376
9377                 case UPDATE_FROM_CONTACT_INFO:
9378                         EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_FROM_CONTACT_INFO");
9379                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9380                                 "UPDATE mail_tbl SET"
9381                                 " email_address_sender = ?,"
9382                                 " WHERE mail_id = %d",
9383                                 mail_id);
9384
9385                         hStmt = NULL;
9386
9387                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9388                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9389                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9390                         i = 0;
9391                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9392                         break;
9393
9394                 case UPDATE_TO_CONTACT_INFO:
9395                         EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_TO_CONTACT_INFO");
9396                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9397                                 "UPDATE mail_tbl SET"
9398                                 " email_address_recipient = ?,"
9399                                 " WHERE mail_id = %d",
9400                                 mail_id);
9401
9402                         hStmt = NULL;
9403
9404                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9405                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9406                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9407                         i = 0;
9408                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9409                         break;
9410
9411                         case UPDATE_ALL_CONTACT_INFO:
9412                                 EM_DEBUG_LOG("emstorage_change_mail_field - mail change type is UPDATE_ALL_CONTACT_INFO");
9413                                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9414                                         "UPDATE mail_tbl SET"
9415                                         " email_address_sender = ?,"
9416                                         " email_address_recipient = ?,"
9417                                         " WHERE mail_id = %d",
9418                                         mail_id);
9419
9420                                 hStmt = NULL;
9421
9422                                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9423                                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9424                                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9425                                 i = 0;
9426                                 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9427                                 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9428                                 break;
9429
9430
9431 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
9432                         case UPDATE_PARTIAL_BODY_DOWNLOAD:
9433
9434                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9435                         "UPDATE mail_tbl SET"
9436                         "  body_download_status = ?"
9437                         ", file_path_plain = ?"
9438                         ", file_path_html = ?"
9439                         ", file_path_mime_entity = ?"
9440                         ", attachment_count = ?"
9441                         ", inline_content_count = ?"
9442                         ", preview_text = ?"
9443                         ", digest_type = ?"
9444                         ", smime_type = ?"
9445                         " WHERE mail_id = %d"
9446                         , mail_id);
9447
9448
9449                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9450                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9451                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9452                          i = 0;
9453
9454                         _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9455                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9456                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html,  0, TEXT_2_LEN_IN_MAIL_TBL);
9457                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9458                         _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9459                         _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9460                         _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text,    0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9461                         _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9462                         _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9463
9464                         break;
9465
9466 #endif
9467                 case UPDATE_FILE_PATH:
9468                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9469                                 "UPDATE mail_tbl SET"
9470                                 ", file_path_plain = ?"
9471                                 ", file_path_html = ?"
9472                                 ", file_path_mime_entity = ?"
9473                                 " WHERE mail_id = %d"
9474                                 , mail_id);
9475
9476
9477                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9478                         EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
9479                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9480                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9481                         i = 0;
9482                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9483                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9484                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9485                         break;
9486
9487                 default:
9488                         EM_DEBUG_LOG(" type[%d]", type);
9489
9490                         error = EMAIL_ERROR_INVALID_PARAM;
9491                         goto FINISH_OFF;
9492         }
9493
9494         if (hStmt != NULL) {
9495
9496         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9497                 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
9498                 ("sqlite3_step fail:%d", rc));
9499                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9500                         ("sqlite3_step fail:%d", rc));
9501                 rc = sqlite3_changes(local_db_handle);
9502                 if (rc == 0) {
9503                         EM_DEBUG_LOG(" no matched mail found...");
9504                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
9505                         goto FINISH_OFF;
9506                 }
9507         }
9508
9509         if (mail->account_id == 0) {
9510                 emstorage_mail_tbl_t* mail_for_account_tbl = NULL;
9511                 if (!emstorage_get_mail_field_by_id(multi_user_name, mail_id, RETRIEVE_ACCOUNT, &mail_for_account_tbl, true, &error) || !mail_for_account_tbl) {
9512                         EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id error [%d]", error);
9513                         goto FINISH_OFF;
9514                 }
9515                 mail->account_id = mail_for_account_tbl->account_id;
9516                 if (mail_for_account_tbl)
9517                         emstorage_free_mail(&mail_for_account_tbl, 1, NULL);
9518         }
9519
9520         ret = true;
9521
9522 FINISH_OFF:
9523         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9524
9525         if (hStmt != NULL) {
9526                 rc = sqlite3_finalize(hStmt);
9527                 if (rc != SQLITE_OK) {
9528                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9529                         error = EMAIL_ERROR_DB_FAILURE;
9530                 }
9531                 hStmt = NULL;
9532         }
9533
9534         if (error == EMAIL_ERROR_NONE &&  move_flag != 1 && transaction) {
9535                 if (!emstorage_get_mailbox_id_by_mailbox_type(multi_user_name, mail->account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &mailbox_id, false, &error))
9536                         EM_DEBUG_EXCEPTION("emstorage_get_mailbox_id_by_mailbox_type error [%d]", error);
9537
9538                 if (mail->mailbox_id == mailbox_id) {
9539                         SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
9540                         if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, mailbox_id_param_string, type))
9541                                 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventerror [ NOTI_MAIL_UPDATE ] >>>> ");
9542                 } else {
9543                         /* h.gahlaut@samsung.com: Jan 10, 2011 Publishing noti to refresh outbox when email sending status changes */
9544                         if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, NULL, type))
9545                                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventerror [ NOTI_MAIL_UPDATE ]");
9546                 }
9547         }
9548
9549         if (err_code != NULL)
9550                 *err_code = error;
9551
9552         EM_DEBUG_FUNC_END("ret [%d]", ret);
9553         return ret;
9554 }
9555
9556 INTERNAL_FUNC int emstorage_increase_mail_id(char *multi_user_name, int *mail_id, int transaction, int *err_code)
9557 {
9558         EM_DEBUG_FUNC_BEGIN("mail_id[%p], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9559
9560         int rc, ret = false;
9561         int error = EMAIL_ERROR_NONE;
9562         int latest_mail_id = 0;
9563         sqlite3 *local_db_handle = NULL;
9564         char *sql = "SELECT MAX(mail_id) FROM mail_tbl;";
9565         char **result = NULL;
9566
9567 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9568         _timedlock_shm_mutex(mapped_for_generating_mail_id, 2);
9569 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9570
9571         ret = vconf_get_int(VCONF_KEY_LATEST_MAIL_ID, &latest_mail_id);
9572         if (ret < 0 || latest_mail_id == 0) {
9573                 EM_DEBUG_LOG("vconf_get_int() failed [%d] or latest_mail_id is zero", ret);
9574
9575         local_db_handle = emstorage_get_db_connection(multi_user_name);
9576
9577                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9578                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
9579                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9580                 if (NULL == result[1])
9581                         rc = 1;
9582                 else
9583                         rc = atoi(result[1]) + 1;
9584
9585                 sqlite3_free_table(result);
9586                 latest_mail_id = rc;
9587         }
9588
9589         latest_mail_id++;
9590
9591         ret = vconf_set_int(VCONF_KEY_LATEST_MAIL_ID, latest_mail_id);
9592
9593         if (mail_id)
9594                 *mail_id = latest_mail_id;
9595
9596 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9597         _unlockshm_mutex(mapped_for_generating_mail_id);
9598 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9599
9600         ret = true;
9601
9602 FINISH_OFF:
9603
9604         if (err_code != NULL)
9605                 *err_code = error;
9606
9607         EM_DEBUG_FUNC_END("ret [%d]", ret);
9608         return ret;
9609 }
9610
9611 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)
9612 {
9613         EM_PROFILE_BEGIN(profile_emstorage_add_mail);
9614         EM_DEBUG_FUNC_BEGIN("mail_tbl_data[%p], get_id[%d], transaction[%d], err_code[%p]", mail_tbl_data, get_id, transaction, err_code);
9615
9616         if (!mail_tbl_data) {
9617                 EM_DEBUG_EXCEPTION("mail_tbl_data[%p], get_id[%d]", mail_tbl_data, get_id);
9618                 if (err_code != NULL)
9619                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9620                 return false;
9621         }
9622
9623         int rc, ret = false;
9624         int error = EMAIL_ERROR_NONE;
9625         char sql_query_string[QUERY_SIZE] = {0, };
9626         DB_STMT hStmt = NULL;
9627
9628         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9629
9630         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9631
9632         if (get_id) {
9633                 /*  increase unique id */
9634                 char *sql = "SELECT max(rowid) FROM mail_tbl;";
9635                 char **result;
9636
9637                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9638 /*              EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
9639                         ("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle))); */
9640                 if (rc != SQLITE_OK) {
9641                         EM_DEBUG_EXCEPTION("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle));
9642                         error = EMAIL_ERROR_DB_FAILURE;
9643                         sqlite3_free_table(result);
9644                         goto FINISH_OFF;
9645                 }
9646
9647                 if (NULL == result[1])
9648                         rc = 1;
9649                 else
9650                         rc = atoi(result[1])+1;
9651
9652                 sqlite3_free_table(result);
9653
9654                 mail_tbl_data->mail_id   = rc;
9655                 mail_tbl_data->thread_id = rc;
9656         }
9657
9658         if (mail_tbl_data->date_time == 0)
9659                 mail_tbl_data->date_time = time(NULL);
9660
9661         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9662                 "INSERT INTO mail_tbl VALUES "
9663                 "(?" /*  mail_id */
9664                 ", ?" /*  account_id */
9665                 ", ?" /*  mailbox_id */
9666                 ", ?" /*  mailbox_type */
9667                 ", ?" /*  subject */
9668
9669                 ", ?" /*  date_time */
9670                 ", ?" /*  server_mail_status */
9671                 ", ?" /*  server_mailbox_name */
9672                 ", ?" /*  server_mail_id */
9673                 ", ?" /*  message_id */
9674
9675                 ", ?" /*  reference_mail_id */
9676                 ", ?" /*  full_address_from */
9677                 ", ?" /*  full_address_reply */
9678                 ", ?" /*  full_address_to */
9679                 ", ?" /*  full_address_cc */
9680
9681                 ", ?" /*  full_address_bcc */
9682                 ", ?" /*  full_address_return */
9683                 ", ?" /*  email_address_sender */
9684                 ", ?" /*  email_address_recipient */
9685                 ", ?" /*  alias_sender */
9686
9687                 ", ?" /*  alias_recipient */
9688                 ", ?" /*  body_download_status */
9689                 ", ?" /*  file_path_plain */
9690                 ", ?" /*  file_path_html */
9691                 ", ?" /*  file_path_mime_entity */
9692
9693                 ", ?" /*  mail_size */
9694                 ", ?" /*  flags_seen_field */
9695                 ", ?" /*  flags_deleted_field */
9696                 ", ?" /*  flags_flagged_field */
9697                 ", ?" /*  flags_answered_field */
9698
9699                 ", ?" /*  flags_recent_field */
9700                 ", ?" /*  flags_draft_field */
9701                 ", ?" /*  flags_forwarded_field */
9702                 ", ?" /*  DRM_status */
9703                 ", ?" /*  priority */
9704
9705                 ", ?" /*  save_status */
9706                 ", ?" /*  lock_status */
9707                 ", ?" /*  report_status */
9708                 ", ?" /*  attachment_count */
9709                 ", ?" /*  inline_content_count */
9710
9711                 ", ?" /*  thread_id */
9712                 ", ?" /*  thread_item_count */
9713                 ", ?" /*  preview_text */
9714                 ", ?" /*  meeting_request_status */
9715                 ", ?" /*  message_class */
9716
9717                 ", ?" /*  digest_type */
9718                 ", ?" /*  smime_type */
9719                 ", ?" /*  scheduled_sending_time */
9720                 ", ?" /*  remaining_resend_times */
9721                 ", ?" /*  tag_id */
9722
9723                 ", ?" /*  replied_time */
9724                 ", ?" /*  forwarded_time */
9725                 ", ?" /*  default charset */
9726                 ", ?" /*  eas_data_length */
9727                 ", ?" /*  eas_data */
9728                 ", ?" /*  user_name */
9729                 ")");
9730
9731         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle,
9732                                                    sql_query_string,
9733                                                    EM_SAFE_STRLEN(sql_query_string),
9734                                                    &hStmt,
9735                                                    NULL),
9736                                     rc);
9737         if (rc != SQLITE_OK) {
9738                 EM_DEBUG_EXCEPTION("sqlite3_prepare error [%d] [%s] SQL(%s) ",
9739                              rc, sql_query_string, sqlite3_errmsg(local_db_handle));
9740                 error = EMAIL_ERROR_DB_FAILURE;
9741                 goto FINISH_OFF;
9742         }
9743
9744         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mail_id);
9745         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, mail_tbl_data->account_id);
9746         _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_id);
9747         _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_type);
9748         _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9749
9750         _bind_stmt_field_data_int(hStmt, DATETIME_IDX_IN_MAIL_TBL, mail_tbl_data->date_time);
9751         _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->server_mail_status);
9752         _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);
9753         _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);
9754         _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);
9755
9756         _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, mail_tbl_data->reference_mail_id);
9757         _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);
9758         _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);
9759         _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);
9760         _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);
9761
9762         _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);
9763         _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);
9764         _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);
9765         _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);
9766         _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);
9767
9768         _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);
9769         _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->body_download_status);
9770         _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);
9771         _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);
9772         _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);
9773
9774         _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, mail_tbl_data->mail_size);
9775         _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_seen_field);
9776         _bind_stmt_field_data_int(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_deleted_field);
9777         _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_flagged_field);
9778         _bind_stmt_field_data_int(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_answered_field);
9779
9780         _bind_stmt_field_data_int(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_recent_field);
9781         _bind_stmt_field_data_int(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_draft_field);
9782         _bind_stmt_field_data_int(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_forwarded_field);
9783         _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->DRM_status);
9784         _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, mail_tbl_data->priority);
9785
9786         _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->save_status);
9787         _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->lock_status);
9788         _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->report_status);
9789         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->attachment_count);
9790         _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->inline_content_count);
9791
9792         _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, mail_tbl_data->thread_id);
9793         _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->thread_item_count);
9794         _bind_stmt_field_data_nstring(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9795         _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->meeting_request_status);
9796         _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, mail_tbl_data->message_class);
9797
9798         _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->digest_type);
9799         _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->smime_type);
9800         _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->scheduled_sending_time);
9801         _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, mail_tbl_data->remaining_resend_times);
9802         _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, mail_tbl_data->tag_id);
9803
9804         _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->replied_time);
9805         _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->forwarded_time);
9806         _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);
9807         _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, mail_tbl_data->eas_data_length);
9808         _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, (void*)mail_tbl_data->eas_data, mail_tbl_data->eas_data_length);
9809         _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);
9810
9811         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9812         if (rc == SQLITE_FULL) {
9813                 EM_DEBUG_EXCEPTION("sqlite3_step error [%d]", rc);
9814                 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
9815                 goto FINISH_OFF;
9816         }
9817         if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
9818                 EM_DEBUG_EXCEPTION("sqlite3_step error [%d]", rc);
9819                 error = EMAIL_ERROR_DB_FAILURE;
9820                 goto FINISH_OFF;
9821         }
9822         ret = true;
9823
9824 FINISH_OFF:
9825         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9826
9827         if (hStmt != NULL) {
9828                 rc = sqlite3_finalize(hStmt);
9829                 if (rc != SQLITE_OK) {
9830                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9831                         error = EMAIL_ERROR_DB_FAILURE;
9832                 }
9833         }
9834
9835         if (err_code != NULL)
9836                 *err_code = error;
9837
9838         EM_PROFILE_END(profile_emstorage_add_mail);
9839         EM_DEBUG_FUNC_END("ret [%d]", ret);
9840         return ret;
9841 }
9842
9843 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)
9844 {
9845         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);
9846
9847         int ret = false, i, cur_conditional_clause = 0;
9848         int error = EMAIL_ERROR_NONE;
9849         int target_account_id;
9850         int conditional_clause_len = 0;
9851         char *sql_query_string = NULL, *conditional_clause = NULL;
9852         emstorage_mailbox_tbl_t *result_mailbox = NULL;
9853         email_mailbox_type_e target_mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED;
9854         char* target_mailbox_name = NULL;
9855
9856         if (!mail_ids || input_mailbox_id <= 0) {
9857                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9858                 if (err_code != NULL)
9859                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9860                 return false;
9861         }
9862
9863         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9864
9865         if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &result_mailbox)) != EMAIL_ERROR_NONE || !result_mailbox) {
9866                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", error);
9867                 if (err_code != NULL)
9868                         *err_code = error;
9869                 return false;
9870         }
9871
9872         if (result_mailbox->mailbox_name) {
9873                 if (strstr(result_mailbox->mailbox_name, "'")) {
9874                         target_mailbox_name = em_replace_all_string(result_mailbox->mailbox_name, "'", "''");
9875                 } else {
9876                         target_mailbox_name = strdup(result_mailbox->mailbox_name);
9877                 }
9878         }
9879
9880         target_mailbox_type = result_mailbox->mailbox_type;
9881         target_account_id   = result_mailbox->account_id;
9882         emstorage_free_mailbox(&result_mailbox, 1, NULL);
9883
9884         conditional_clause_len =  (sizeof(char) * 8 * number_of_mails) + 512;
9885         conditional_clause = em_malloc(conditional_clause_len);
9886         if (conditional_clause == NULL) {
9887                 EM_DEBUG_EXCEPTION("em_mallocfailed");
9888                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9889                 goto FINISH_OFF;
9890         }
9891         cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE mail_id in (");
9892
9893         for (i = 0; i < number_of_mails; i++)
9894                 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
9895
9896         /* prevent 34415 */
9897         char *last_comma = rindex(conditional_clause, ',');
9898         if (last_comma) *last_comma = ')'; /* replace , with) */
9899
9900         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9901
9902         /* Updating a mail_tbl */
9903
9904         sql_query_string = em_malloc(conditional_clause_len);
9905         if (sql_query_string == NULL) {
9906                 EM_DEBUG_EXCEPTION("em_mallocfailed");
9907                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9908                 goto FINISH_OFF;
9909         }
9910         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);
9911         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9912
9913         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9914         if (error != EMAIL_ERROR_NONE) {
9915                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9916                         goto FINISH_OFF;
9917         }
9918
9919         /* Updating a mail_attachment_tbl */
9920         memset(sql_query_string, 0x00, conditional_clause_len);
9921         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);
9922         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9923         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9924         if (error != EMAIL_ERROR_NONE) {
9925                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9926                         goto FINISH_OFF;
9927         }
9928
9929         /* Updating a mail_meeting_tbl */
9930         memset(sql_query_string, 0x00, conditional_clause_len);
9931         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);
9932         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9933         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9934         if (error != EMAIL_ERROR_NONE) {
9935                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9936                         goto FINISH_OFF;
9937         }
9938
9939 #ifdef __FEATURE_BODY_SEARCH__
9940         /* Updating mail_text_tbl */
9941         memset(sql_query_string, 0x00, conditional_clause_len);
9942         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);
9943         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9944         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9945         if (error != EMAIL_ERROR_NONE) {
9946                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9947                         goto FINISH_OFF;
9948         }
9949 #endif
9950
9951         /* Updating a mail_read_mail_uid_tbl */
9952         memset(conditional_clause, 0x00, conditional_clause_len);
9953         cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE local_uid in (");
9954
9955         for (i = 0; i < number_of_mails; i++)
9956                 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
9957
9958         /* prevent 34415 */
9959         last_comma = rindex(conditional_clause, ',');
9960         if (last_comma) *last_comma = ')'; /* replace , with) */
9961
9962         memset(sql_query_string, 0x00, conditional_clause_len);
9963         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);
9964         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9965         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9966         if (error != EMAIL_ERROR_NONE) {
9967                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9968                         goto FINISH_OFF;
9969         }
9970
9971         ret = true;
9972
9973 FINISH_OFF:
9974         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9975
9976         EM_SAFE_FREE(target_mailbox_name);
9977         EM_SAFE_FREE(conditional_clause);
9978         EM_SAFE_FREE(sql_query_string);
9979
9980         if (err_code != NULL)
9981                 *err_code = error;
9982
9983         EM_DEBUG_FUNC_END("ret [%d]", ret);
9984         return ret;
9985 }
9986
9987 INTERNAL_FUNC int emstorage_delete_mail(char *multi_user_name, int mail_id, int from_server, int transaction, int *err_code)
9988 {
9989         EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9990
9991         if (!mail_id) {
9992                 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
9993                 if (err_code != NULL)
9994                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9995                 return false;
9996         }
9997
9998         int ret = false;
9999         int error = EMAIL_ERROR_NONE;
10000         char sql_query_string[QUERY_SIZE] = {0, };
10001         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10002         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10003
10004         memset(sql_query_string, 0x00, sizeof(sql_query_string));
10005
10006         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id = %d ", mail_id);
10007         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10008         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10009         if (error != EMAIL_ERROR_NONE) {
10010                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10011                         goto FINISH_OFF;
10012         }
10013
10014         ret = true;
10015
10016 FINISH_OFF:
10017         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10018
10019         if (err_code != NULL)
10020                 *err_code = error;
10021
10022         EM_DEBUG_FUNC_END("ret [%d]", ret);
10023         return ret;
10024 }
10025
10026 INTERNAL_FUNC int emstorage_delete_multiple_mails(char *multi_user_name, int mail_ids[], int number_of_mails, int transaction, int *err_code)
10027 {
10028         EM_DEBUG_FUNC_BEGIN("mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", mail_ids, number_of_mails, transaction, err_code);
10029
10030         int ret = false, i, cur_sql_query_string = 0;
10031         int error = EMAIL_ERROR_NONE;
10032         int query_size = 0;
10033         char *sql_query_string = NULL;
10034
10035         if (!mail_ids) {
10036                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10037                 if (err_code != NULL)
10038                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10039                 return false;
10040         }
10041
10042         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10043         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10044
10045         query_size = (sizeof(char) * 8 * number_of_mails) + 512;
10046         sql_query_string =  em_malloc(query_size);
10047         if (sql_query_string == NULL) {
10048                 EM_DEBUG_EXCEPTION("em_mallocfailed");
10049                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10050                 goto FINISH_OFF;
10051         }
10052
10053         cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_tbl WHERE mail_id in (");
10054
10055         for (i = 0; i < number_of_mails; i++)
10056                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
10057
10058         /* prevent 34414 */
10059         char *last_comma = rindex(sql_query_string, ',');
10060         if (last_comma != NULL) *last_comma = ')'; /* replace , with) */
10061
10062         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10063         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10064         if (error != EMAIL_ERROR_NONE) {
10065                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10066                         goto FINISH_OFF;
10067         }
10068
10069 #ifdef __FEATURE_BODY_SEARCH__
10070         /* delete mail_text from mail_text_tbl */
10071         cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_text_tbl WHERE mail_id in (");
10072
10073         for (i = 0; i < number_of_mails; i++)
10074                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
10075
10076         last_comma = rindex(sql_query_string, ',');
10077         *last_comma = ')'; /* replace , with) */
10078
10079         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10080         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10081         if (error != EMAIL_ERROR_NONE) {
10082                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10083                         goto FINISH_OFF;
10084         }
10085 #endif
10086
10087         ret = true;
10088
10089 FINISH_OFF:
10090         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10091
10092         EM_SAFE_FREE(sql_query_string);
10093         if (err_code != NULL)
10094                 *err_code = error;
10095
10096         EM_DEBUG_FUNC_END("ret [%d]", ret);
10097         return ret;
10098 }
10099
10100 INTERNAL_FUNC int emstorage_delete_mail_by_account(char *multi_user_name, int account_id, int transaction, int *err_code)
10101 {
10102         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
10103
10104         if (account_id < FIRST_ACCOUNT_ID) {
10105                 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
10106
10107                 if (err_code != NULL)
10108                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10109                 return false;
10110         }
10111
10112         int rc, ret = false;
10113         int error = EMAIL_ERROR_NONE;
10114         char sql_query_string[QUERY_SIZE] = {0, };
10115
10116         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10117         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10118
10119         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d", account_id);
10120         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10121         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10122         if (error != EMAIL_ERROR_NONE) {
10123                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10124                         goto FINISH_OFF;
10125         }
10126
10127         rc = sqlite3_changes(local_db_handle);
10128         if (rc == 0) {
10129                 EM_DEBUG_LOG(" no mail found...");
10130                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10131         }
10132
10133         /* Delete all mails  mail_read_mail_uid_tbl table based on account id */
10134         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
10135         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10136         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10137         if (error != EMAIL_ERROR_NONE) {
10138                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10139                         goto FINISH_OFF;
10140         }
10141
10142         rc = sqlite3_changes(local_db_handle);
10143         if (rc == 0) {
10144                 EM_DEBUG_LOG("No mail found...");
10145                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10146         }
10147
10148 #ifdef __FEATURE_BODY_SEARCH__
10149         /* Delete all mail_text in mail_text_tbl table based on account id */
10150         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE account_id = %d", account_id);
10151         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10152         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10153         if (error != EMAIL_ERROR_NONE) {
10154                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10155                         goto FINISH_OFF;
10156         }
10157
10158         rc = sqlite3_changes(local_db_handle);
10159         if (rc == 0) {
10160                 EM_DEBUG_LOG("No mail found...");
10161                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10162         }
10163 #endif
10164
10165         ret = true;
10166
10167 FINISH_OFF:
10168         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10169
10170         if (error == EMAIL_ERROR_NONE) {
10171                 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_WITH_ACCOUNT, account_id, 0 , NULL, 0))
10172                         EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAIL_DELETE_ALL ]");
10173         }
10174
10175         if (err_code != NULL)
10176                 *err_code = error;
10177
10178         EM_DEBUG_FUNC_END("ret [%d]", ret);
10179         return ret;
10180 }
10181
10182 INTERNAL_FUNC int emstorage_delete_mail_by_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox, int transaction, int *err_code)
10183 {
10184         EM_DEBUG_FUNC_BEGIN("mailbox[%p], transaction[%d], err_code[%p]", mailbox, transaction, err_code);
10185
10186         if (mailbox == NULL) {
10187                 EM_DEBUG_EXCEPTION("mailbox [%p]", mailbox);
10188                 if (err_code != NULL)
10189                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10190                 return false;
10191         }
10192
10193         int ret = false;
10194         int error = EMAIL_ERROR_NONE;
10195         char sql_query_string[QUERY_SIZE] = {0, };
10196         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10197         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10198
10199         if (strcmp(mailbox->mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
10200                 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);
10201                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10202                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10203                 if (error != EMAIL_ERROR_NONE) {
10204                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10205                         goto FINISH_OFF;
10206                 }
10207
10208                 /* Delete Mails from mail_read_mail_uid_tbl */
10209                 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);
10210                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10211                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10212                 if (error != EMAIL_ERROR_NONE) {
10213                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10214                         goto FINISH_OFF;
10215                 }
10216         } else {
10217                 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);
10218                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10219                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10220                 if (error != EMAIL_ERROR_NONE) {
10221                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10222                         goto FINISH_OFF;
10223                 }
10224
10225                 /* Delete Mails from mail_read_mail_uid_tbl */
10226                 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);
10227                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10228                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10229                 if (error != EMAIL_ERROR_NONE) {
10230                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10231                         goto FINISH_OFF;
10232                 }
10233
10234 #ifdef __FEATURE_BODY_SEARCH__
10235                 /* Delete Mails from mail_text_tbl */
10236                 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);
10237                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10238                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10239                 if (error != EMAIL_ERROR_NONE) {
10240                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10241                         goto FINISH_OFF;
10242                 }
10243 #endif
10244         }
10245         ret = true;
10246
10247 FINISH_OFF:
10248         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10249
10250         if (error == EMAIL_ERROR_NONE) {
10251                 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_ALL, mailbox->account_id, mailbox->mailbox_id , mailbox->mailbox_name, 0))
10252                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventFailed [ NOTI_MAIL_DELETE_ALL ] >>>> ");
10253         }
10254
10255         if (err_code != NULL)
10256                 *err_code = error;
10257
10258         EM_DEBUG_FUNC_END("ret [%d]", ret);
10259         return ret;
10260 }
10261
10262 INTERNAL_FUNC int emstorage_free_mail(emstorage_mail_tbl_t** mail_list, int count, int *err_code)
10263 {
10264         EM_DEBUG_FUNC_BEGIN("mail_list[%p], count[%d], err_code[%p]", mail_list, count, err_code);
10265
10266         if (count > 0) {
10267                 if ((mail_list == NULL) || (*mail_list == NULL)) {
10268                         EM_DEBUG_EXCEPTION("mail_ilst[%p], count[%d]", mail_list, count);
10269
10270                         if (err_code)
10271                                 *err_code = EMAIL_ERROR_INVALID_PARAM;
10272                         return false;
10273                 }
10274
10275                 emstorage_mail_tbl_t* p = *mail_list;
10276                 int i = 0;
10277
10278                 for (; i < count; i++, p++) {
10279                         EM_SAFE_FREE(p->server_mailbox_name);
10280                         EM_SAFE_FREE(p->server_mail_id);
10281                         EM_SAFE_FREE(p->full_address_from);
10282                         EM_SAFE_FREE(p->full_address_reply);
10283                         EM_SAFE_FREE(p->full_address_to);
10284                         EM_SAFE_FREE(p->full_address_cc);
10285                         EM_SAFE_FREE(p->full_address_bcc);
10286                         EM_SAFE_FREE(p->full_address_return);
10287                         EM_SAFE_FREE(p->subject);
10288                         EM_SAFE_FREE(p->file_path_plain);
10289                         EM_SAFE_FREE(p->file_path_html);
10290                         EM_SAFE_FREE(p->file_path_mime_entity);
10291                         EM_SAFE_FREE(p->message_id);
10292                         EM_SAFE_FREE(p->email_address_sender);
10293                         EM_SAFE_FREE(p->email_address_recipient);
10294                         EM_SAFE_FREE(p->preview_text);
10295                         EM_SAFE_FREE(p->alias_sender);
10296                         EM_SAFE_FREE(p->alias_recipient);
10297                         EM_SAFE_FREE(p->default_charset);
10298                         EM_SAFE_FREE(p->pgp_password);
10299                         EM_SAFE_FREE(p->eas_data);
10300                         EM_SAFE_FREE(p->user_name);
10301                 }
10302                 EM_SAFE_FREE(*mail_list);
10303         }
10304
10305         if (err_code != NULL)
10306                 *err_code = EMAIL_ERROR_NONE;
10307
10308         EM_DEBUG_FUNC_END();
10309         return true;
10310 }
10311
10312 #ifdef __FEATURE_BODY_SEARCH__
10313 INTERNAL_FUNC void emstorage_free_mail_text(emstorage_mail_text_tbl_t** mail_text_list, int count, int *err_code)
10314 {
10315         EM_DEBUG_FUNC_BEGIN("mail_text_list[%p], count[%d], err_code[%p]", mail_text_list, count, err_code);
10316
10317         if (count > 0) {
10318                 if ((mail_text_list == NULL) || (*mail_text_list == NULL)) {
10319                         EM_DEBUG_LOG("Nothing to free: mail_text_list[%p]", mail_text_list);
10320                         return;
10321                 }
10322
10323                 emstorage_mail_text_tbl_t *p = *mail_text_list;
10324                 int i = 0;
10325
10326                 for (; i < count; i++, p++) {
10327                         EM_SAFE_FREE(p->body_text);
10328                 }
10329                 EM_SAFE_FREE(*mail_text_list);
10330         }
10331
10332         EM_DEBUG_FUNC_END();
10333 }
10334 #endif
10335
10336 INTERNAL_FUNC int emstorage_get_attachment_count(char *multi_user_name, int mail_id, int *count, int transaction, int *err_code)
10337 {
10338         EM_DEBUG_FUNC_BEGIN("mail_id[%d], count[%p], transaction[%d], err_code[%p]", mail_id, count, transaction, err_code);
10339
10340         if (mail_id <= 0 || !count) {
10341                 EM_DEBUG_EXCEPTION("mail_id[%d], count[%p]", mail_id, count);
10342                 if (err_code != NULL)
10343                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10344                 return false;
10345         }
10346
10347         int rc = -1, ret = false;
10348         int error = EMAIL_ERROR_NONE;
10349         char sql_query_string[QUERY_SIZE] = {0, };
10350
10351         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10352         EMSTORAGE_START_READ_TRANSACTION(transaction);
10353
10354         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
10355
10356         char **result;
10357
10358         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10359         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10360                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10361
10362         *count = atoi(result[1]);
10363         sqlite3_free_table(result);
10364
10365         ret = true;
10366
10367 FINISH_OFF:
10368         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10369
10370         if (err_code != NULL)
10371                 *err_code = error;
10372
10373         EM_DEBUG_FUNC_END("ret [%d]", ret);
10374         return ret;
10375 }
10376
10377 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)
10378 {
10379         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);
10380
10381         if (input_mail_id <= 0 || !output_attachment_list || !output_attachment_count) {
10382                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10383                 return EMAIL_ERROR_INVALID_PARAM;
10384         }
10385
10386         int                         error = EMAIL_ERROR_NONE;
10387         int                         i = 0;
10388         int                         rc = -1;
10389         char                      **result = NULL;
10390         char                        sql_query_string[QUERY_SIZE] = {0, };
10391         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10392         DB_STMT hStmt = NULL;
10393         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10394
10395         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
10396         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", input_mail_id);
10397         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10398         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10399                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10400
10401         *output_attachment_count = atoi(result[1]);
10402         sqlite3_free_table(result);
10403
10404         if (*output_attachment_count == 0) {
10405                 error = EMAIL_ERROR_NONE;
10406                 goto FINISH_OFF;
10407         }
10408
10409         p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * (*output_attachment_count));
10410
10411         if (!p_data_tbl) {
10412                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
10413                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10414                 goto FINISH_OFF;
10415         }
10416
10417         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE mail_id = %d ORDER BY attachment_id", input_mail_id);
10418         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
10419
10420         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10421         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)));
10422
10423         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10424         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },  ("sqlite3_step fail:%d", rc));
10425
10426         if (rc == SQLITE_DONE) {
10427                 EM_DEBUG_EXCEPTION("no matched attachment found...");
10428                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10429                 goto FINISH_OFF;
10430         }
10431         for (i = 0; i < *output_attachment_count; i++) {
10432                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10433                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10434                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10435                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10436                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10437                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10438                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10439                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10440                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10441                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10442                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10443                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10444                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10445                 EM_DEBUG_LOG("attachment[%d].attachment_id : %d", i, p_data_tbl[i].attachment_id);
10446                 EM_DEBUG_LOG("attachment_mime_type : %s", p_data_tbl[i].attachment_mime_type);
10447                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10448                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
10449         }
10450
10451 FINISH_OFF:
10452
10453         if (error == EMAIL_ERROR_NONE)
10454                 *output_attachment_list = p_data_tbl;
10455         else if (p_data_tbl != NULL)
10456                 emstorage_free_attachment(&p_data_tbl, *output_attachment_count, NULL);
10457
10458         if (hStmt) {
10459                 rc = sqlite3_finalize(hStmt);
10460                 if (rc != SQLITE_OK) {
10461                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10462                         error = EMAIL_ERROR_DB_FAILURE;
10463                         if (*output_attachment_list)
10464                                 emstorage_free_attachment(output_attachment_list, *output_attachment_count, NULL); /* prevent */
10465                 }
10466         }
10467
10468         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
10469
10470         EM_DEBUG_FUNC_END("error [%d]", error);
10471         return error;
10472 }
10473
10474 INTERNAL_FUNC int emstorage_get_attachment(char *multi_user_name, int attachment_id, emstorage_attachment_tbl_t **attachment, int transaction, int *err_code)
10475 {
10476         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], transaction[%d], err_code[%p]", attachment_id, attachment, transaction, err_code);
10477
10478         if (attachment_id <= 0 || !attachment) {
10479                 EM_DEBUG_EXCEPTION("attachment_id[%d], attachment[%p]", attachment_id, attachment);
10480                 if (err_code != NULL)
10481                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10482                 return false;
10483         }
10484
10485         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10486         int rc, ret = false;
10487         int error = EMAIL_ERROR_NONE;
10488         char sql_query_string[QUERY_SIZE] = {0, };
10489
10490         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10491         EMSTORAGE_START_READ_TRANSACTION(transaction);
10492
10493         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_id = %d",  attachment_id);
10494
10495         sqlite3_stmt* hStmt = NULL;
10496
10497         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10498         EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
10499
10500         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10501                 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10502
10503
10504         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10505         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10506                 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10507
10508         if (rc == SQLITE_DONE) {
10509                 EM_DEBUG_LOG("no matched attachment found...");
10510                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10511                 goto FINISH_OFF;
10512         }
10513
10514         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10515                 EM_DEBUG_EXCEPTION("malloc failed...");
10516                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10517                 goto FINISH_OFF;
10518         }
10519
10520         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10521         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10522         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10523         _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10524         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10525         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10526         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10527         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10528         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10529         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10530         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10531         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10532         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10533
10534 #ifdef __ATTACHMENT_OPTI__
10535                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10536                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10537 #endif
10538
10539         ret = true;
10540
10541 FINISH_OFF:
10542         if (ret == true)
10543                 *attachment = p_data_tbl;
10544
10545         if (hStmt != NULL) {
10546                 rc = sqlite3_finalize(hStmt);
10547                 if (rc != SQLITE_OK) {
10548                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10549                         error = EMAIL_ERROR_DB_FAILURE;
10550                 }
10551         }
10552
10553         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10554
10555         if (err_code != NULL)
10556                 *err_code = error;
10557
10558         EM_DEBUG_FUNC_END("ret [%d]", ret);
10559         return ret;
10560 }
10561
10562 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)
10563 {
10564         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);
10565
10566         if (mail_id <= 0 || nth <= 0 || !attachment_tbl) {
10567                 EM_DEBUG_EXCEPTION(" mail_id[%d], nth[%d], attachment[%p]", mail_id, nth, attachment_tbl);
10568
10569                 if (err_code != NULL)
10570                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10571                 return false;
10572         }
10573
10574         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10575         char *p = NULL;
10576         int rc, ret = false;
10577         int error = EMAIL_ERROR_NONE;
10578         char sql_query_string[QUERY_SIZE] = {0, };
10579
10580         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10581         EMSTORAGE_START_READ_TRANSACTION(transaction);
10582
10583         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));
10584         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10585
10586         DB_STMT hStmt = NULL;
10587
10588         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10589         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10590                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10591
10592
10593         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10594         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10595                 ("sqlite3_step fail:%d", rc));
10596
10597         if (rc == SQLITE_DONE) {
10598                 EM_DEBUG_EXCEPTION("no matched attachment found: mail_id[%d] nth[%d]", mail_id, nth);
10599                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10600                 goto FINISH_OFF;
10601         }
10602
10603         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10604                 EM_DEBUG_EXCEPTION(" malloc failed...");
10605                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10606                 goto FINISH_OFF;
10607         }
10608
10609         p_data_tbl->attachment_id = sqlite3_column_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10610         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10611                 p_data_tbl->attachment_name = cpy_str(p);
10612         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10613                 p_data_tbl->attachment_path = cpy_str(p);
10614         if ((p = (char *)sqlite3_column_text(hStmt, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10615                 p_data_tbl->content_id = cpy_str(p);
10616         p_data_tbl->attachment_size = sqlite3_column_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10617         p_data_tbl->mail_id = sqlite3_column_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10618         p_data_tbl->account_id = sqlite3_column_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10619         p_data_tbl->mailbox_id = sqlite3_column_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10620         p_data_tbl->attachment_save_status = sqlite3_column_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10621         p_data_tbl->attachment_drm_type = sqlite3_column_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10622         p_data_tbl->attachment_drm_method = sqlite3_column_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10623         p_data_tbl->attachment_inline_content_status = sqlite3_column_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10624         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10625                 p_data_tbl->attachment_mime_type = cpy_str(p);
10626 #ifdef __ATTACHMENT_OPTI__
10627                 p_data_tbl->encoding = sqlite3_column_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10628                 if ((p = (char *)sqlite3_column_text(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10629                         p_data_tbl->section = cpy_str(p);
10630 #endif
10631         ret = true;
10632
10633 FINISH_OFF:
10634         if (ret == true)
10635                 *attachment_tbl = p_data_tbl;
10636
10637         if (hStmt != NULL) {
10638                 rc = sqlite3_finalize(hStmt);
10639                 if (rc != SQLITE_OK) {
10640                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10641                         error = EMAIL_ERROR_DB_FAILURE;
10642                 }
10643         }
10644
10645         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10646
10647         if (err_code != NULL)
10648                 *err_code = error;
10649
10650         EM_DEBUG_FUNC_END("ret [%d]", ret);
10651         return ret;
10652 }
10653
10654 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)
10655 {
10656         EM_DEBUG_FUNC_BEGIN("attachment_path[%p], attachment[%p], transaction[%d], err_code[%p]", attachment_path, attachment, transaction, err_code);
10657
10658         if (attachment_path == NULL || !attachment) {
10659                 EM_DEBUG_EXCEPTION("attachment_path[%p], attachment[%p]", attachment_path, attachment);
10660                 if (err_code != NULL)
10661                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10662                 return false;
10663         }
10664
10665         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10666         int rc, ret = false;
10667         int error = EMAIL_ERROR_NONE;
10668         char sql_query_string[QUERY_SIZE] = {0, };
10669
10670         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10671         EMSTORAGE_START_READ_TRANSACTION(transaction);
10672
10673         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_path = '%s'", attachment_path);
10674
10675         sqlite3_stmt* hStmt = NULL;
10676
10677         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10678         EM_DEBUG_LOG_DEV("before sqlite3_prepare hStmt = %p", hStmt);
10679
10680         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10681                 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10682
10683
10684         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10685         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10686                 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10687
10688         if (rc == SQLITE_DONE) {
10689                 EM_DEBUG_LOG("no matched attachment found...");
10690                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10691                 goto FINISH_OFF;
10692         }
10693
10694         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10695                 EM_DEBUG_EXCEPTION("malloc failed...");
10696                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10697                 goto FINISH_OFF;
10698         }
10699
10700         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10701         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10702         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10703         _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10704         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10705         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10706         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10707         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10708         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10709         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10710         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10711         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10712         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10713
10714 #ifdef __ATTACHMENT_OPTI__
10715                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10716                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10717 #endif
10718
10719         ret = true;
10720
10721 FINISH_OFF:
10722         if (ret == true)
10723                 *attachment = p_data_tbl;
10724
10725         if (hStmt != NULL) {
10726                 rc = sqlite3_finalize(hStmt);
10727                 if (rc != SQLITE_OK) {
10728                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10729                         error = EMAIL_ERROR_DB_FAILURE;
10730                 }
10731         }
10732
10733         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10734
10735         if (err_code != NULL)
10736                 *err_code = error;
10737
10738         EM_DEBUG_FUNC_END("ret [%d]", ret);
10739         return ret;
10740 }
10741
10742 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)
10743 {
10744         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], attachment[%p], transaction[%d], err_code[%p]", mail_id, type, attachment, transaction, err_code);
10745
10746         if (mail_id <= 0 || !attachment) {
10747                 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], attachment[%p]", mail_id, type, attachment);
10748                 if (err_code != NULL)
10749                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10750                 return false;;
10751         }
10752
10753         int rc, ret = false;
10754         int error = EMAIL_ERROR_NONE;
10755         DB_STMT hStmt = NULL;
10756         char sql_query_string[QUERY_SIZE] = {0, };
10757
10758         int i = 0;
10759
10760         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10761
10762         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10763
10764         switch (type) {
10765                 case UPDATE_MAILBOX:
10766                                 EM_DEBUG_LOG("UPDATE_MAILBOX");
10767                         if (!attachment->mailbox_id) {
10768                                 EM_DEBUG_EXCEPTION(" attachment->mailbox_id[%d]", attachment->mailbox_id);
10769                                 error = EMAIL_ERROR_INVALID_PARAM;
10770                                 goto FINISH_OFF;
10771                         }
10772                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
10773                                 "UPDATE mail_attachment_tbl SET mailbox_id = ? WHERE mail_id = %d", mail_id);
10774
10775                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10776                         EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10777                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10778                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10779
10780                         _bind_stmt_field_data_int(hStmt, i++, attachment->mailbox_id);
10781                         break;
10782
10783                 case UPDATE_SAVENAME:
10784                         EM_DEBUG_LOG("UPDATE_SAVENAME");
10785                         if (!attachment->attachment_path) {
10786                                 EM_DEBUG_EXCEPTION(" attachment->attachment_path[%p]", attachment->attachment_path);
10787                                 error = EMAIL_ERROR_INVALID_PARAM;
10788                                 goto FINISH_OFF;
10789                         }
10790
10791                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
10792                                 "UPDATE mail_attachment_tbl SET"
10793                                 "  attachment_size = ?"
10794                                 ", attachment_save_status = ?"
10795                                 ", attachment_path = ?"
10796                                 " WHERE mail_id = %d"
10797                                 " AND attachment_id = %d"
10798                                 , attachment->mail_id
10799                                 , attachment->attachment_id);
10800
10801
10802                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10803                         EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10804                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10805                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10806
10807                         _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_size);
10808                         _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_save_status);
10809                         _bind_stmt_field_data_string(hStmt, i++, (char *)attachment->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10810                         break;
10811
10812                 default:
10813                         EM_DEBUG_LOG("type[%d]", type);
10814                         error = EMAIL_ERROR_INVALID_PARAM;
10815                         goto FINISH_OFF;
10816         }
10817         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10818
10819         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10820         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10821                 ("sqlite3_step fail:%d", rc));
10822         ret = true;
10823
10824 FINISH_OFF:
10825
10826         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10827
10828         if (hStmt != NULL) {
10829                 rc = sqlite3_finalize(hStmt);
10830                 if (rc != SQLITE_OK) {
10831                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10832                         error = EMAIL_ERROR_DB_FAILURE;
10833                 }
10834         }
10835
10836         if (err_code != NULL)
10837                 *err_code = error;
10838         EM_DEBUG_FUNC_END("ret [%d]", ret);
10839         return ret;
10840 }
10841
10842 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)
10843 {
10844         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);
10845
10846         int rc = 0;
10847         int ret = false;
10848         int error = EMAIL_ERROR_NONE;
10849         int field_idx = 0;
10850         int account_id = 0;
10851         char sql_query_string[QUERY_SIZE] = {0, };
10852         char *replaced_mailbox_name = NULL;
10853         char *replaced_alias = NULL;
10854         sqlite3 *local_db_handle = NULL;
10855         DB_STMT hStmt = NULL;
10856         emstorage_mailbox_tbl_t *old_mailbox_data = NULL;
10857
10858         if (input_mailbox_id <= 0 || !input_new_mailbox_name || !input_new_mailbox_alias) {
10859                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10860                 return EMAIL_ERROR_INVALID_PARAM;
10861         }
10862
10863         if (strstr(input_new_mailbox_name, "'")) {
10864                 replaced_mailbox_name = em_replace_all_string(input_new_mailbox_name, "'", "''");
10865         } else {
10866                 replaced_mailbox_name = strdup(input_new_mailbox_name);
10867         }
10868
10869         if (strstr(input_new_mailbox_alias, "'")) {
10870                 replaced_alias = em_replace_all_string(input_new_mailbox_alias, "'", "''");
10871         } else {
10872                 replaced_alias = strdup(input_new_mailbox_alias);
10873         }
10874
10875         local_db_handle = emstorage_get_db_connection(multi_user_name);
10876
10877         if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
10878                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", error);
10879                 EM_SAFE_FREE(replaced_mailbox_name);
10880                 EM_SAFE_FREE(replaced_alias);
10881                 return error;
10882         }
10883
10884     if (old_mailbox_data == NULL) {
10885         EM_DEBUG_LOG("old_mailbox_data is NULL");
10886         error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
10887         goto FINISH_OFF;
10888     }
10889
10890         account_id = old_mailbox_data->account_id;
10891
10892         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
10893
10894         if (input_eas_data && input_eas_data_length > 0) {
10895                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10896                         "UPDATE mail_box_tbl SET"
10897                         " mailbox_name = ?"
10898                         ",alias = ?"
10899                         ",eas_data = ?"
10900                         ",eas_data_length = ?"
10901                         " WHERE mailbox_id = ?");
10902
10903                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10904
10905                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10906                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10907
10908                 _bind_stmt_field_data_string(hStmt, field_idx++, input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10909                 _bind_stmt_field_data_string(hStmt, field_idx++, input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10910                 _bind_stmt_field_data_blob(hStmt, field_idx++, input_eas_data, input_eas_data_length);
10911                 _bind_stmt_field_data_int(hStmt, field_idx++, input_eas_data_length);
10912                 _bind_stmt_field_data_int(hStmt, field_idx++, input_mailbox_id);
10913         } else {
10914                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10915                                 "UPDATE mail_box_tbl SET"
10916                                 " mailbox_name = ?"
10917                                 ",alias = ?"
10918                                 " WHERE mailbox_id = ?");
10919
10920                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10921
10922                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10923                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10924
10925                 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10926                 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10927                 _bind_stmt_field_data_int(hStmt, field_idx++ , input_mailbox_id);
10928         }
10929
10930
10931         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10932         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
10933                 ("sqlite3_step fail:%d", rc));
10934         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10935                 ("sqlite3_step fail:%d", rc));
10936
10937         if (sqlite3_changes(local_db_handle) == 0)
10938                 EM_DEBUG_LOG("no mail_meeting_tbl matched...");
10939
10940         ret = true;
10941
10942 FINISH_OFF:
10943         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, error);
10944         if (error == EMAIL_ERROR_NONE) {
10945                 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME, account_id, input_mailbox_id, input_new_mailbox_name, 0))
10946                         EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAILBOX_RENAME ] >>>> ");
10947         } else {
10948                 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME_FAIL, account_id, input_mailbox_id, input_new_mailbox_name, error))
10949                         EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAILBOX_RENAME_FAIL ] >>>> ");
10950         }
10951
10952         EM_SAFE_FREE(replaced_mailbox_name);
10953         EM_SAFE_FREE(replaced_alias);
10954
10955         if (old_mailbox_data)
10956                 emstorage_free_mailbox(&old_mailbox_data, 1, NULL);
10957
10958         if (hStmt != NULL) {
10959                 rc = sqlite3_finalize(hStmt);
10960                 if (rc != SQLITE_OK) {
10961                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10962                         error = EMAIL_ERROR_DB_FAILURE;
10963                 }
10964         }
10965
10966         EM_DEBUG_FUNC_END("error [%d]", error);
10967         return error;
10968 }
10969
10970 INTERNAL_FUNC int emstorage_get_new_attachment_no(char *multi_user_name, int *attachment_no, int *err_code)
10971 {
10972         EM_DEBUG_FUNC_BEGIN("attachment_no [%p], err_code[%p]", attachment_no, err_code);
10973         int rc, ret = false;
10974         int error = EMAIL_ERROR_NONE;
10975         char *sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
10976         char **result;
10977
10978         if (!attachment_no) {
10979                 EM_DEBUG_EXCEPTION("Invalid attachment");
10980                 if (err_code != NULL)
10981                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10982                 return false;
10983         }
10984
10985         *attachment_no = -1;
10986
10987         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10988
10989
10990         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
10991         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10992                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
10993
10994         if (NULL == result[1])
10995                 rc = 1;
10996         else
10997                 rc = atoi(result[1])+1;
10998
10999         sqlite3_free_table(result);
11000
11001         *attachment_no = rc;
11002         EM_DEBUG_LOG("attachment_no [%d]", *attachment_no);
11003         ret = true;
11004
11005 FINISH_OFF:
11006
11007         if (err_code != NULL)
11008                 *err_code = error;
11009
11010         EM_DEBUG_FUNC_END("ret [%d]", ret);
11011         return ret;
11012 }
11013
11014 INTERNAL_FUNC int emstorage_add_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int iscopy, int transaction, int *err_code)
11015 {
11016         EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], iscopy[%d], transaction[%d], err_code[%p]", attachment_tbl, iscopy, transaction, err_code);
11017
11018         char *sql = NULL;
11019         char **result;
11020         int rc, ret = false;
11021         int error = EMAIL_ERROR_NONE;
11022         char sql_query_string[QUERY_SIZE] = {0, };
11023
11024         DB_STMT hStmt = NULL;
11025         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11026
11027         if (!attachment_tbl) {
11028                 EM_DEBUG_EXCEPTION("attachment_tbl[%p], iscopy[%d]", attachment_tbl, iscopy);
11029                 if (err_code != NULL)
11030                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11031                 return false;
11032         }
11033
11034         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11035
11036         sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
11037
11038         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
11039         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
11040                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
11041
11042         if (NULL == result[1]) rc = 1;
11043         else rc = atoi(result[1]) + 1;
11044         sqlite3_free_table(result);
11045
11046         attachment_tbl->attachment_id = rc;
11047
11048         SNPRINTF(sql_query_string, sizeof(sql_query_string),
11049                 "INSERT INTO mail_attachment_tbl VALUES "
11050                 "(?"    /* attachment_id */
11051                 ", ?"   /* attachment_name */
11052                 ", ?"   /* attachment_path */
11053                 ", ?"   /* content_id */
11054                 ", ?"   /* attachment_size */
11055                 ", ?"   /* mail_id */
11056                 ", ?"   /* account_id */
11057                 ", ?"   /* mailbox_id */
11058                 ", ?"   /* attachment_save_status */
11059                 ", ?"   /* attachment_drm_type */
11060                 ", ?"   /* attachment_drm_method */
11061                 ", ?"   /* attachment_inline_content_status */
11062                 ", ?"   /* attachment_mime_type */
11063 #ifdef __ATTACHMENT_OPTI__
11064                 ", ?"
11065                 ", ?"
11066 #endif
11067                 ")");
11068
11069
11070         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11071         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11072                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11073
11074         _bind_stmt_field_data_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_id);
11075         _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);
11076         _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);
11077         _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);
11078         _bind_stmt_field_data_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_size);
11079         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mail_id);
11080         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->account_id);
11081         _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mailbox_id);
11082         _bind_stmt_field_data_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_save_status);
11083         _bind_stmt_field_data_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_type);
11084         _bind_stmt_field_data_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_method);
11085         _bind_stmt_field_data_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_inline_content_status);
11086         _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);
11087 #ifdef __ATTACHMENT_OPTI__
11088         _bind_stmt_field_data_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->encoding);
11089         _bind_stmt_field_data_string(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->section, 0, ATTACHMENT_LEN_IN_MAIL_ATTACHMENT_TBL);
11090 #endif
11091
11092
11093         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11094         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11095                 ("sqlite3_step fail:%d", rc));
11096         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11097                 ("sqlite3_step fail:%d", rc));
11098
11099         rc = sqlite3_changes(local_db_handle);
11100         if (rc == 0) {
11101                 EM_DEBUG_LOG(" no matched mail found...");
11102                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11103                 goto FINISH_OFF;
11104         }
11105
11106         ret = true;
11107
11108 FINISH_OFF:
11109         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11110
11111         if (hStmt != NULL) {
11112                 rc = sqlite3_finalize(hStmt);
11113                 if (rc != SQLITE_OK) {
11114                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11115                         error = EMAIL_ERROR_DB_FAILURE;
11116                 }
11117         }
11118
11119         if (err_code != NULL)
11120         *err_code = error;
11121
11122         EM_DEBUG_FUNC_END("ret [%d]", ret);
11123         return ret;
11124 }
11125
11126 INTERNAL_FUNC int emstorage_update_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int transaction, int *err_code)
11127 {
11128         EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], transaction[%d], err_code[%p]", attachment_tbl, transaction, err_code);
11129
11130         int rc, ret = false, field_idx = 0;
11131         int error = EMAIL_ERROR_NONE;
11132         DB_STMT hStmt = NULL;
11133         char sql_query_string[QUERY_SIZE] = {0, };
11134
11135         if (!attachment_tbl) {
11136                 EM_DEBUG_EXCEPTION(" attachment_tbl[%p] ", attachment_tbl);
11137                 if (err_code)
11138                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11139                 return false;
11140         }
11141
11142         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11143
11144         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11145
11146         SNPRINTF(sql_query_string, sizeof(sql_query_string),
11147                 "UPDATE mail_attachment_tbl SET  "
11148                 "  attachment_name = ?"
11149                 ", attachment_path =  ?"
11150                 ", content_id = ?"
11151                 ", attachment_size = ?"
11152                 ", mail_id = ?"
11153                 ", account_id = ?"
11154                 ", mailbox_id = ?"
11155                 ", attachment_save_status = ?"
11156                 ", attachment_drm_type = ?"
11157                 ", attachment_drm_method = ?"
11158                 ", attachment_inline_content_status = ? "
11159                 ", attachment_mime_type = ? "
11160                 " WHERE attachment_id = ?;");
11161
11162
11163         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11164
11165         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11166                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11167
11168         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
11169         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
11170         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->content_id, 0, CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL);
11171         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_size);
11172         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->mail_id);
11173         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->account_id);
11174         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->mailbox_id);
11175         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_save_status);
11176         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_drm_type);
11177         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_drm_method);
11178         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_inline_content_status);
11179         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_mime_type, 0, ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL);
11180         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_id);
11181
11182
11183         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11184         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11185                 ("sqlite3_step fail:%d", rc));
11186         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11187                 ("sqlite3_step fail:%d", rc));
11188
11189         rc = sqlite3_changes(local_db_handle);
11190         if (rc == 0) {
11191                 EM_DEBUG_LOG(" no matched mail found...");
11192                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11193                 goto FINISH_OFF;
11194         }
11195
11196         ret = true;
11197
11198 FINISH_OFF:
11199         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11200         if (hStmt != NULL) {
11201                 rc = sqlite3_finalize(hStmt);
11202                 if (rc != SQLITE_OK) {
11203                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11204                         error = EMAIL_ERROR_DB_FAILURE;
11205                 }
11206         }
11207
11208         if (err_code != NULL)
11209         *err_code = error;
11210
11211         EM_DEBUG_FUNC_END("ret [%d]", ret);
11212         return ret;
11213 }
11214
11215 INTERNAL_FUNC int emstorage_delete_attachment_on_db(char *multi_user_name, int attachment_id, int transaction, int *err_code)
11216 {
11217         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], transaction[%d], err_code[%p]", attachment_id, transaction, err_code);
11218
11219         if (attachment_id < 0) {
11220                 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
11221                 if (err_code != NULL)
11222                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11223                 return false;
11224         }
11225
11226         int ret = false;
11227         int error = EMAIL_ERROR_NONE;
11228         char sql_query_string[QUERY_SIZE] = {0, };
11229         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11230
11231         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11232
11233         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
11234
11235         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11236         if (error != EMAIL_ERROR_NONE) {
11237                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11238                         goto FINISH_OFF;
11239         }
11240
11241         ret = true;
11242
11243 FINISH_OFF:
11244         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11245
11246         if (err_code)
11247                 *err_code = error;
11248
11249         EM_DEBUG_FUNC_END("ret [%d]", ret);
11250         return ret;
11251 }
11252
11253 INTERNAL_FUNC int emstorage_delete_all_attachments_of_mail(char *multi_user_name, int mail_id, int transaction, int *err_code)
11254 {
11255         EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
11256         int ret = false;
11257         int error = EMAIL_ERROR_NONE;
11258         char sql_query_string[QUERY_SIZE] = {0, };
11259         sqlite3 *local_db_handle = NULL;
11260
11261         if (mail_id <= 0) {
11262                 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
11263                 if (err_code != NULL)
11264                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11265                 return false;
11266         }
11267
11268         local_db_handle = emstorage_get_db_connection(multi_user_name);
11269
11270         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11271
11272         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
11273         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11274         if (error != EMAIL_ERROR_NONE) {
11275                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11276                         goto FINISH_OFF;
11277         }
11278
11279         ret = true;
11280
11281 FINISH_OFF:
11282         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11283
11284         if (err_code)
11285                 *err_code = error;
11286
11287         EM_DEBUG_FUNC_END("ret [%d]", ret);
11288         return ret;
11289 }
11290
11291 INTERNAL_FUNC int emstorage_delete_attachment_all_on_db(char *multi_user_name, int account_id, char *mailbox, int transaction, int *err_code)
11292 {
11293         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox, transaction, err_code);
11294
11295         int error = EMAIL_ERROR_NONE;
11296         int ret = false;
11297         char sql_query_string[QUERY_SIZE] = {0, };
11298         char *replaced_mailbox = NULL;
11299         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11300
11301         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11302
11303         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl");
11304
11305         if (account_id != ALL_ACCOUNT) /*  '0' means all account */
11306                 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);
11307
11308         if (mailbox)    /*  NULL means all mailbox_name */ {
11309                 if (strstr(mailbox, "'")) {
11310                         replaced_mailbox = em_replace_all_string(mailbox, "'", "''");
11311                 } else {
11312                         replaced_mailbox = strdup(mailbox);
11313                 }
11314
11315                 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);
11316                 EM_SAFE_FREE(replaced_mailbox); /*prevent 49434*/
11317         }
11318
11319         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11320         if (error != EMAIL_ERROR_NONE) {
11321                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11322                         goto FINISH_OFF;
11323         }
11324
11325         ret = true;
11326
11327 FINISH_OFF:
11328         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11329
11330         if (err_code != NULL)
11331                 *err_code = error;
11332
11333         EM_DEBUG_FUNC_END("ret [%d]", ret);
11334         return ret;
11335 }
11336
11337 INTERNAL_FUNC int emstorage_free_attachment(emstorage_attachment_tbl_t** attachment_tbl_list, int count, int *err_code)
11338 {
11339         EM_DEBUG_FUNC_BEGIN("attachment_tbl_list[%p], count[%d], err_code[%p]", attachment_tbl_list, count, err_code);
11340
11341         if (count > 0) {
11342                 if ((attachment_tbl_list == NULL) || (*attachment_tbl_list == NULL)) {
11343                         EM_DEBUG_LOG("Nothing to free: attachment_tbl_list[%p], count[%d]", attachment_tbl_list, count);
11344                         if (err_code != NULL)
11345                                 *err_code = EMAIL_ERROR_INVALID_PARAM;
11346                         return false;
11347                 }
11348
11349                 emstorage_attachment_tbl_t* p = *attachment_tbl_list;
11350                 int i;
11351
11352                 for (i = 0; i < count; i++) {
11353                         EM_SAFE_FREE(p[i].attachment_name);
11354                         EM_SAFE_FREE(p[i].attachment_path);
11355                         EM_SAFE_FREE(p[i].content_id);
11356                         EM_SAFE_FREE(p[i].attachment_mime_type);
11357 #ifdef __ATTACHMENT_OPTI__
11358                         EM_SAFE_FREE(p[i].section);
11359 #endif
11360                 }
11361
11362                 EM_SAFE_FREE(p);
11363                 *attachment_tbl_list = NULL;
11364         }
11365
11366         if (err_code != NULL)
11367                 *err_code = EMAIL_ERROR_NONE;
11368         EM_DEBUG_FUNC_END();
11369         return true;
11370 }
11371
11372 INTERNAL_FUNC int emstorage_begin_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11373 {
11374         EM_DEBUG_FUNC_BEGIN();
11375         EM_PROFILE_BEGIN(emStorageBeginTransaction);
11376         int ret = true;
11377
11378         _timedlock_shm_mutex(mapped_for_db_lock, 2);
11379
11380         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11381
11382         int rc;
11383         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN immediate;", NULL, NULL, NULL), rc);
11384         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11385                 ("SQL(BEGIN) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11386
11387         if (ret == false) {
11388                 if (err_code != NULL) *err_code = EMAIL_ERROR_DB_FAILURE;
11389         }
11390
11391         EM_PROFILE_END(emStorageBeginTransaction);
11392         EM_DEBUG_FUNC_END("ret [%d]", ret);
11393         return ret;
11394 }
11395
11396 INTERNAL_FUNC int emstorage_commit_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11397 {
11398         EM_DEBUG_FUNC_BEGIN();
11399         int ret = true;
11400         int rc;
11401
11402         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11403
11404         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
11405         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; }, ("SQL(END) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11406
11407         if (ret == false && err_code != NULL)
11408                 *err_code = EMAIL_ERROR_DB_FAILURE;
11409
11410         _unlockshm_mutex(mapped_for_db_lock);
11411
11412         EM_DEBUG_FUNC_END("ret [%d]", ret);
11413         return ret;
11414 }
11415
11416 INTERNAL_FUNC int emstorage_rollback_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11417 {
11418         EM_DEBUG_FUNC_BEGIN();
11419         int ret = true;
11420         int rc;
11421         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11422
11423         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "ROLLBACK;", NULL, NULL, NULL), rc);
11424         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11425                 ("SQL(ROLLBACK) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11426
11427         if (ret == false && err_code != NULL)
11428                 *err_code = EMAIL_ERROR_DB_FAILURE;
11429
11430         _unlockshm_mutex(mapped_for_db_lock);
11431
11432         EM_DEBUG_FUNC_END("ret [%d]", ret);
11433         return ret;
11434 }
11435
11436 INTERNAL_FUNC int emstorage_is_mailbox_full(char *multi_user_name, int account_id, email_mailbox_t *mailbox, int *result, int *err_code)
11437 {
11438         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], result[%p], err_code[%p]", account_id, mailbox, result, err_code);
11439
11440         if (account_id < FIRST_ACCOUNT_ID || !mailbox || !result) {
11441                 if (mailbox)
11442                         EM_DEBUG_EXCEPTION("Invalid Parameter. accoun_id[%d], mailbox[%p]", account_id, mailbox);
11443
11444                 if (err_code != NULL)
11445                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11446
11447                 return false;
11448         }
11449
11450         int ret = false;
11451         int error = EMAIL_ERROR_NONE;
11452         int mail_count = 0;
11453
11454         if (!emstorage_get_mail_count(multi_user_name, account_id, mailbox->mailbox_id, &mail_count, NULL, true, &error)) {
11455                 EM_DEBUG_EXCEPTION("emstorage_get_mail_count failed [%d]", error);
11456                 goto FINISH_OFF;
11457         }
11458
11459         if (mailbox) {
11460                 EM_DEBUG_LOG("mail_count[%d] mail_slot_size[%d]", mail_count, mailbox->mail_slot_size);
11461                 if (mail_count >= mailbox->mail_slot_size)
11462                         *result = true;
11463                 else
11464                         *result = false;
11465
11466                 ret = true;
11467         }
11468
11469         ret = true;
11470 FINISH_OFF:
11471
11472         if (err_code != NULL)
11473                 *err_code = error;
11474
11475         EM_DEBUG_FUNC_END("ret [%d]", ret);
11476         return ret;
11477 }
11478
11479 INTERNAL_FUNC int emstorage_clear_mail_data(char *multi_user_name, int transaction, int *err_code)
11480 {
11481         EM_DEBUG_FUNC_BEGIN("transaction[%d], err_code[%p]", transaction, err_code);
11482
11483         int ret = false;
11484         int error = EMAIL_ERROR_NONE;
11485         char sql_query_string[QUERY_SIZE] = {0, };
11486         const email_db_object_t* tables = _g_db_tables;
11487         const email_db_object_t* indexes = _g_db_indexes;
11488
11489         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11490         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11491
11492         if (!emstorage_delete_dir((char *)MAIL_HOME, &error)) {
11493                 EM_DEBUG_EXCEPTION(" emstorage_delete_dir failed - %d", error);
11494
11495                 goto FINISH_OFF;
11496         }
11497
11498         mkdir(MAIL_HOME, DIRECTORY_PERMISSION);
11499         mkdir(MAIL_TEMP, DIRECTORY_PERMISSION);
11500         chmod(MAIL_TEMP, 0777);
11501
11502         /*  first clear index. */
11503         while (indexes->object_name) {
11504                 if (indexes->data_flag) {
11505                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP index %s", indexes->object_name);
11506                         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11507                         if (error != EMAIL_ERROR_NONE) {
11508                                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11509                                         goto FINISH_OFF;
11510                         }
11511                 }
11512                 indexes++;
11513         }
11514
11515         while (tables->object_name) {
11516                 if (tables->data_flag) {
11517                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP table %s", tables->object_name);
11518                         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11519                         if (error != EMAIL_ERROR_NONE) {
11520                                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11521                                         goto FINISH_OFF;
11522                         }
11523                 }
11524
11525                 tables++;
11526         }
11527         ret = true;
11528
11529 FINISH_OFF:
11530         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11531
11532         if (err_code != NULL)
11533                 *err_code = error;
11534
11535         EM_DEBUG_FUNC_END("ret [%d]", ret);
11536         return ret;
11537 }
11538 /*======================= DB File Utils =============================================*/
11539 #include <dirent.h>
11540 #include <sys/types.h>
11541 #define  DIR_SEPERATOR "/"
11542
11543 INTERNAL_FUNC char *emstorage_make_directory_path_from_file_path(char *file_name)
11544 {
11545         EM_DEBUG_FUNC_BEGIN("Filename [ %p ]", file_name);
11546         char delims[] = "/";
11547         char *result = NULL;
11548         gchar **token = NULL;
11549
11550         token = g_strsplit_set(file_name, delims, 1);
11551
11552         if (token && token[0]) {
11553                 EM_DEBUG_LOG_SEC(">>>> Directory_name [ %s ]", token[0]);
11554                 result = EM_SAFE_STRDUP(token[0]);
11555         } else
11556                 EM_DEBUG_LOG(">>>> No Need to create Directory");
11557
11558         g_strfreev(token);
11559
11560         return result;
11561 }
11562
11563 INTERNAL_FUNC int emstorage_get_save_name(char *multi_user_name, int account_id, int mail_id, int atch_id,
11564                                                                                         char *fname, char *move_buf, char *path_buf, int maxlen, int *err_code)
11565 {
11566         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);
11567         EM_PROFILE_BEGIN(profile_emstorage_get_save_name);
11568
11569         int ret = false;
11570         int error = EMAIL_ERROR_NONE;
11571         char *dir_name = NULL;
11572         char create_dir[1024] = {0};
11573         char *temp_file = NULL;
11574         char *prefix_path = NULL;
11575         char *modified_fname = NULL;
11576
11577         if (!move_buf || !path_buf || account_id < FIRST_ACCOUNT_ID || mail_id < 0 || atch_id < 0) {
11578                 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);
11579                 error = EMAIL_ERROR_INVALID_PARAM;
11580                 goto FINISH_OFF;
11581         }
11582
11583         snprintf(path_buf, 512, "%s", MAIL_HOME);
11584         snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, account_id);
11585
11586         if (mail_id > 0)
11587                 snprintf(path_buf+EM_SAFE_STRLEN(path_buf),   512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, mail_id);
11588
11589         if (atch_id > 0)
11590                 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, atch_id);
11591
11592         if (fname) {
11593                 temp_file = EM_SAFE_STRDUP(fname);
11594                 if (temp_file && strstr(temp_file, "/")) {
11595                         dir_name = emstorage_make_directory_path_from_file_path(temp_file);
11596                 }
11597         }
11598
11599         if (dir_name) {
11600                 snprintf(create_dir, sizeof(create_dir), "%s%s%s", path_buf, DIR_SEPERATOR, dir_name);
11601                 EM_DEBUG_LOG(">>>>> DIR PATH [%s]", create_dir);
11602                 mkdir(create_dir, DIRECTORY_PERMISSION);
11603                 EM_SAFE_FREE(dir_name);
11604         }
11605
11606         if (fname) {
11607                 EM_DEBUG_LOG_DEV(">>>>> fname [%s]", fname);
11608
11609                 /* Did not allow the slash */
11610                 modified_fname = reg_replace_new(fname, "/", "_");
11611                 EM_DEBUG_LOG_SEC("modified_fname : [%s]", modified_fname);
11612
11613                 if (modified_fname == NULL) modified_fname = g_strdup(fname);
11614
11615                 if (EM_SAFE_STRLEN(modified_fname) + EM_SAFE_STRLEN(path_buf) + strlen(DIR_SEPERATOR) > maxlen - 1) {
11616                         char *modified_name = NULL;
11617                         int remain_len  = (maxlen - 1) - EM_SAFE_STRLEN(path_buf) - strlen(DIR_SEPERATOR);
11618
11619                         if (remain_len <= 0) {
11620                                 error = EMAIL_ERROR_MAX_EXCEEDED;
11621                                 goto FINISH_OFF;
11622                         }
11623
11624                         if (remain_len > MAX_FILENAME) {
11625                                 remain_len = MAX_FILENAME;
11626                         }
11627
11628                         modified_name = em_shrink_filename(modified_fname, remain_len);
11629
11630                         if (!modified_name) {
11631                                 error = EMAIL_ERROR_MAX_EXCEEDED;
11632                                 goto FINISH_OFF;
11633                         }
11634
11635                         snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),"%s%s", DIR_SEPERATOR, modified_name);
11636                         EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11637                         EM_SAFE_FREE(modified_name);
11638                 } else {
11639                         if (EM_SAFE_STRLEN(modified_fname) > MAX_FILENAME - 1) {
11640                                 char *modified_name = NULL;
11641
11642                                 modified_name = em_shrink_filename(modified_fname, MAX_FILENAME);
11643                                 if (!modified_name) {
11644                                         error = EMAIL_ERROR_MAX_EXCEEDED;
11645                                         goto FINISH_OFF;
11646                                 }
11647
11648                                 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),"%s%s", DIR_SEPERATOR, modified_name);
11649                                 EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11650                                 EM_SAFE_FREE(modified_name);
11651                         } else {
11652                                 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),"%s%s", DIR_SEPERATOR, modified_fname);
11653                         }
11654                 }
11655         }
11656
11657         EM_DEBUG_LOG_SEC(">>>>> path_buf [%s]", path_buf);
11658
11659     if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11660                 error = emcore_get_container_path(multi_user_name, &prefix_path);
11661                 if (error != EMAIL_ERROR_NONE) {
11662                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11663                         goto FINISH_OFF;
11664                 }
11665         snprintf(move_buf, 512, "%s/%s", prefix_path, path_buf);
11666         EM_DEBUG_LOG_SEC("move_buf : [%s]", move_buf);
11667     } else {
11668         snprintf(move_buf, 512, "%s", path_buf);
11669         EM_DEBUG_LOG_SEC("move_buf : [%s]", move_buf);
11670     }
11671
11672         ret = true;
11673
11674 FINISH_OFF:
11675
11676         EM_SAFE_FREE(temp_file);
11677         EM_SAFE_FREE(prefix_path);
11678         EM_SAFE_FREE(modified_fname);
11679
11680         if (err_code != NULL)
11681                 *err_code = error;
11682
11683         EM_PROFILE_END(profile_emstorage_get_save_name);
11684         EM_DEBUG_FUNC_END("ret [%d]", ret);
11685         return ret;
11686 }
11687
11688 /*
11689 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)
11690 {
11691         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);
11692
11693         if (!name_buf || account_id < FIRST_ACCOUNT_ID) {
11694                 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);
11695                 if (err_code != NULL)
11696                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11697                 return false;
11698         }
11699
11700         sprintf(name_buf+EM_SAFE_STRLEN(name_buf),      "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
11701
11702         if (mail_id > 0)
11703                 sprintf(name_buf+EM_SAFE_STRLEN(name_buf),      "%s%d", DIR_SEPERATOR, mail_id);
11704         else
11705                 goto FINISH_OFF;
11706
11707         if (atch_id > 0)
11708                 sprintf(name_buf+EM_SAFE_STRLEN(name_buf),      "%s%d", DIR_SEPERATOR, atch_id);
11709         else
11710                 goto FINISH_OFF;
11711
11712 FINISH_OFF:
11713         sprintf(name_buf+EM_SAFE_STRLEN(name_buf),      ".DELE");
11714
11715         EM_DEBUG_FUNC_END();
11716         return true;
11717 }
11718 */
11719
11720 INTERNAL_FUNC int emstorage_create_dir(char *multi_user_name, int account_id, int mail_id, int atch_id, int *err_code)
11721 {
11722         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], err_code[%p]", account_id, mail_id, atch_id, err_code);
11723         EM_PROFILE_BEGIN(profile_emcore_save_create_dir);
11724         int ret = false;
11725         int error = EMAIL_ERROR_NONE;
11726         char buf[512];
11727         struct stat sbuf;
11728         char *prefix_path = NULL;
11729         char errno_buf[ERRNO_BUF_SIZE] = {0};
11730
11731         memset(buf, 0x00, sizeof(buf));
11732
11733     if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11734                 error = emcore_get_container_path(multi_user_name, &prefix_path);
11735                 if (error != EMAIL_ERROR_NONE) {
11736                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11737                         goto FINISH_OFF;
11738                 }
11739         } else {
11740                 prefix_path = strdup("");
11741         }
11742
11743         if (account_id >= FIRST_ACCOUNT_ID) {
11744                 SNPRINTF(buf, sizeof(buf), "%s%s%s%s%d", prefix_path,
11745                                                                                                 DIR_SEPERATOR,
11746                                                                                                 MAIL_HOME,
11747                                                                                                 DIR_SEPERATOR,
11748                                                                                                 account_id);
11749
11750                 if (stat(buf, &sbuf) == 0) {
11751                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11752                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11753                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11754                                 goto FINISH_OFF;
11755                         }
11756                 } else {
11757                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11758                                 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11759                                 EM_DEBUG_EXCEPTION("mkdir failed: %s", EM_STRERROR(errno_buf));
11760                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11761                                 if (errno == 28)
11762                                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11763                                 goto FINISH_OFF;
11764                         }
11765
11766                         if (account_id == EML_FOLDER) {
11767                                 chmod(buf, 0777);
11768                         }
11769                 }
11770         }
11771
11772         if (mail_id > 0) {
11773                 int space_left_in_buffer = sizeof(buf) - EM_SAFE_STRLEN(buf);
11774
11775                 if (account_id < FIRST_ACCOUNT_ID) {
11776                         EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11777                         error = EMAIL_ERROR_INVALID_PARAM;
11778                         goto FINISH_OFF;
11779                 }
11780
11781                 if (space_left_in_buffer + 10 > sizeof(buf)) {
11782                         EM_DEBUG_EXCEPTION("Buffer overflowed");
11783                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11784                         goto FINISH_OFF;
11785                 }
11786
11787                 SNPRINTF(buf+EM_SAFE_STRLEN(buf), space_left_in_buffer, "%s%d", DIR_SEPERATOR, mail_id);
11788
11789                 if (stat(buf, &sbuf) == 0) {
11790                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11791                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11792                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11793                                 goto FINISH_OFF;
11794                         }
11795                 } else {
11796                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11797                                 EM_DEBUG_EXCEPTION("mkdir failed [%s]", buf);
11798                                 EM_DEBUG_EXCEPTION("mkdir failed [%d][%s]", errno, EM_STRERROR(errno_buf));
11799                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11800                                 if (errno == 28)
11801                                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11802                                 goto FINISH_OFF;
11803                         }
11804
11805                         if (account_id == EML_FOLDER) {
11806                                 chmod(buf, 0777);
11807                         }
11808                 }
11809         }
11810
11811         if (atch_id > 0) {
11812                 if (account_id < FIRST_ACCOUNT_ID || mail_id <= 0) {
11813                         EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11814                         error = EMAIL_ERROR_INVALID_PARAM;
11815                         goto FINISH_OFF;
11816                 }
11817
11818                 SNPRINTF(buf+EM_SAFE_STRLEN(buf), sizeof(buf)-(EM_SAFE_STRLEN(buf)+1), "%s%d", DIR_SEPERATOR, atch_id);
11819
11820                 if (stat(buf, &sbuf) == 0) {
11821                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11822                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11823                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11824                                 goto FINISH_OFF;
11825                         }
11826                 } else {
11827                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11828                                 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11829                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11830                                 if (errno == 28)
11831                                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11832                                 goto FINISH_OFF;
11833                         }
11834
11835                         if (account_id == EML_FOLDER) {
11836                                 chmod(buf, 0777);
11837                         }
11838                 }
11839         }
11840
11841         ret = true;
11842
11843 FINISH_OFF:
11844
11845         EM_SAFE_FREE(prefix_path);
11846
11847         if (err_code != NULL)
11848                 *err_code = error;
11849
11850         EM_PROFILE_END(profile_emcore_save_create_dir);
11851         EM_DEBUG_FUNC_END("ret [%d]", ret);
11852         return ret;
11853 }
11854
11855 INTERNAL_FUNC int emstorage_copy_file(char *src_file, char *dst_file, int sync_status, int *err_code)
11856 {
11857         EM_DEBUG_FUNC_BEGIN_SEC("src_file[%s], dst_file[%s], err_code[%p]", src_file, dst_file, err_code);
11858         EM_DEBUG_LOG("Using the fsync function");
11859         int ret = false;
11860         int error = EMAIL_ERROR_NONE;
11861         struct stat st_buf;
11862
11863         int fp_src = 0;
11864         int fp_dst = 0;
11865         int nread = 0;
11866         char buf[FILE_MAX_BUFFER_SIZE] = {0};
11867         char errno_buf[ERRNO_BUF_SIZE] = {0};
11868
11869         if (!src_file || !dst_file) {
11870                 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
11871
11872                 error = EMAIL_ERROR_INVALID_PARAM;
11873                 goto FINISH_OFF;
11874         }
11875
11876         if (stat(src_file, &st_buf) < 0) {
11877                 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", src_file);
11878
11879                 error = EMAIL_ERROR_SYSTEM_FAILURE;             /* EMAIL_ERROR_INVALID_PATH; */
11880                 goto FINISH_OFF;
11881         }
11882
11883         error = em_open(src_file, O_RDONLY, 0, &fp_src);
11884         if (error != EMAIL_ERROR_NONE) {
11885                         EM_DEBUG_EXCEPTION(">>>> Source Fail em_open %s Failed: %d", src_file, error);
11886                         goto FINISH_OFF;
11887         }
11888
11889         error = em_open(dst_file, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, &fp_dst); /*prevent 24474*/
11890         if (error != EMAIL_ERROR_NONE) {
11891                         EM_DEBUG_EXCEPTION(">>>> Destination Fail em_open %s:  %d", dst_file, error);
11892                         goto FINISH_OFF;
11893         }
11894
11895         memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
11896
11897         while ((nread = read(fp_src, buf, FILE_MAX_BUFFER_SIZE)) > 0) {
11898                 if (nread > 0 && nread <= FILE_MAX_BUFFER_SIZE) {
11899                         EM_DEBUG_LOG("Nread Value [%d]", nread);
11900                         char *buf_ptr;
11901                         ssize_t byte_written = 0;
11902                         size_t remain_byte = nread;
11903                         buf_ptr = buf;
11904                         errno = 0;
11905
11906                         while (remain_byte > 0 && buf_ptr && errno == 0) {
11907                                 byte_written = write(fp_dst, buf_ptr, remain_byte);
11908
11909                                 if (byte_written < 0) {
11910                                         /* interrupted by a signal */
11911                                         if (errno == EINTR) {
11912                                                 errno = 0;
11913                                                 continue;
11914                                         }
11915
11916                                         EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
11917                                         error = EMAIL_ERROR_UNKNOWN;
11918                                         goto FINISH_OFF;
11919                                 }
11920                                 EM_DEBUG_LOG("NWRITTEN [%d]", byte_written);
11921                                 remain_byte -= byte_written;
11922                                 buf_ptr += byte_written;
11923                         }
11924                 }
11925
11926                 memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
11927         }
11928
11929         ret = true;
11930
11931 FINISH_OFF:
11932         EM_SAFE_CLOSE(fp_src);
11933
11934         if (fp_dst >= 0) { /*prevent 24474*/
11935                 if (sync_status) {
11936                         EM_DEBUG_LOG("Before fsync");
11937                         fsync(fp_dst);
11938                 }
11939                 close(fp_dst);
11940         }
11941
11942         if (nread < 0 || error == EMAIL_ERROR_UNKNOWN)
11943                 remove(dst_file);
11944
11945         if (err_code != NULL)
11946                 *err_code = error;
11947         EM_DEBUG_FUNC_END("ret [%d]", ret);
11948         return ret;
11949 }
11950 /* create Directory if user has deleted [deepam.p@samsung.com] */
11951 INTERNAL_FUNC void emstorage_create_dir_if_delete()
11952 {
11953         EM_DEBUG_FUNC_BEGIN();
11954
11955         mkdir(DATA_PATH, DIRECTORY_PERMISSION);
11956         mkdir(EMAIL_PATH, DIRECTORY_PERMISSION);
11957         mkdir(MAIL_HOME, DIRECTORY_PERMISSION);
11958         mkdir(MAIL_TEMP, DIRECTORY_PERMISSION);
11959         chmod(MAIL_TEMP, 0777);
11960
11961         EM_DEBUG_FUNC_END();
11962 }
11963
11964 static int _get_temp_file_name(char **filename, int *err_code)
11965 {
11966         EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
11967
11968         int ret = false;
11969         int error = EMAIL_ERROR_NONE;
11970
11971         if (filename == NULL) {
11972                 EM_DEBUG_EXCEPTION(" filename[%p]", filename);
11973                 error = EMAIL_ERROR_INVALID_PARAM;
11974                 goto FINISH_OFF;
11975         }
11976
11977         char tempname[512] = {0x00, };
11978         struct timeval tv;
11979
11980         gettimeofday(&tv, NULL);
11981         srand(tv.tv_usec);
11982         unsigned int seed = time(NULL);
11983         SNPRINTF(tempname, sizeof(tempname), "%s%c%d", MAIL_TEMP, '/', rand_r(&seed));
11984
11985         char *p = EM_SAFE_STRDUP(tempname);
11986         if (p == NULL) {
11987                 EM_DEBUG_EXCEPTION(" strdup failed...");
11988                 error = EMAIL_ERROR_OUT_OF_MEMORY;
11989                 goto FINISH_OFF;
11990         }
11991
11992         *filename = p;
11993
11994         ret = true;
11995
11996 FINISH_OFF:
11997         if (err_code != NULL)
11998                 *err_code = error;
11999
12000         EM_DEBUG_FUNC_END("ret [%d]", ret);
12001         return ret;
12002 }
12003
12004 INTERNAL_FUNC int emstorage_add_content_type(char *file_path, char *char_set, int *err_code)
12005 {
12006         EM_DEBUG_FUNC_BEGIN("File path [ %p ]  Character Set [ %p ] err_code [ %p]", file_path, char_set, err_code);
12007
12008         EM_IF_NULL_RETURN_VALUE(file_path, false);
12009         EM_IF_NULL_RETURN_VALUE(char_set, false);
12010         EM_IF_NULL_RETURN_VALUE(err_code, false);
12011
12012         char *buf =  NULL;
12013         char *buf1 = NULL;
12014         struct stat st_buf;
12015         int buf_size = 0;
12016         char *low_char_set = NULL;
12017         char *match_str = NULL;
12018         int nwritten = 0;
12019         int ret = false;
12020         int error = EMAIL_ERROR_NONE;
12021         int data_count_to_written = 0;
12022         char *temp_file_name = NULL;
12023         int err = 0;
12024         FILE* fp_src = NULL;
12025         FILE* fp_dest = NULL;
12026         int nread = 0;
12027
12028
12029         if (stat(file_path, &st_buf) < 0) {
12030                 EM_DEBUG_EXCEPTION_SEC(" stat(\"%s\") failed...", file_path);
12031                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12032                 goto FINISH_OFF;
12033         }
12034
12035         buf_size =  st_buf.st_size;
12036         EM_DEBUG_LOG(">>>> File Size [ %d ] ", buf_size);
12037         buf = (char *)calloc(1, buf_size+1);
12038
12039         if (!buf) {
12040                 EM_DEBUG_LOG(">>> Memory cannot be allocated ");
12041                 goto FINISH_OFF;
12042         }
12043
12044         error = em_fopen(file_path, "rb", &fp_src);
12045         if (error != EMAIL_ERROR_NONE || fp_src == NULL) {
12046                 EM_DEBUG_EXCEPTION_SEC(" file_path fopen failed - %s [%d]", file_path, error);
12047                 goto FINISH_OFF;
12048         }
12049
12050         if ((nread = fread(buf, 1, buf_size, fp_src)) > 0) {
12051                 if (nread > 0 && nread <= buf_size) {
12052                         EM_DEBUG_LOG(">>>> Nread Value [ %d ] ", nread);
12053
12054                         /**
12055                           *   1.Add check for whether content type is there.
12056                           *   2. If not based on the character set, Append it in File
12057                           **/
12058
12059                         low_char_set = calloc(1, EM_SAFE_STRLEN(char_set) + strlen(" \" /></head>") +1); /*prevent 34359*/
12060                         if (low_char_set == NULL) {
12061                                 EM_DEBUG_EXCEPTION("calloc failed");
12062                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12063                                 goto FINISH_OFF;
12064                         }
12065
12066                         strncat(low_char_set, char_set, EM_SAFE_STRLEN(char_set));
12067                         EM_DEBUG_LOG(">>>> CHAR SET [ %s ] ", low_char_set);
12068                         strncat(low_char_set, " \" /></head>", strlen(" \" /></head>")); /*prevent 34359*/
12069                         EM_DEBUG_LOG(">>> CHARSET [ %s ] ", low_char_set);
12070                         match_str = strstr(buf, CONTENT_TYPE_DATA);
12071
12072                         if (match_str == NULL) {
12073                                 EM_DEBUG_LOG(">>>>emstorage_add_content_type 3 ");
12074                                 if (fp_src != NULL) {
12075                                         fclose(fp_src);
12076                                         fp_src = NULL;
12077                                 }
12078                                 data_count_to_written = EM_SAFE_STRLEN(low_char_set)+strlen(CONTENT_DATA)+1; /*prevent 34359*/
12079                                 buf1 = (char *)calloc(1, data_count_to_written);
12080
12081                                 if (buf1) {
12082                                         strncat(buf1, CONTENT_DATA, strlen(CONTENT_DATA)); /*prevent 34359*/
12083                                         EM_DEBUG_LOG(">>>>> BUF 1 [ %s ] ", buf1);
12084                                         strncat(buf1, low_char_set, EM_SAFE_STRLEN(low_char_set));
12085                                         EM_DEBUG_LOG(">>>> HTML TAG DATA  [ %s ] ", buf1);
12086
12087                                         /* 1. Create a temporary file name */
12088                                         if (!_get_temp_file_name(&temp_file_name, &err)) {
12089                                                         EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed - %d", err);
12090                                                         if (err_code != NULL) *err_code = err;
12091                                                         goto FINISH_OFF;
12092                                         }
12093                                         EM_DEBUG_LOG_SEC(">>>>>>> TEMP APPEND FILE PATH [ %s ] ", temp_file_name);
12094
12095                                         /* Open the Temp file in Append mode */
12096                                         error = em_fopen(temp_file_name, "ab", &fp_dest);
12097                                         if (error != EMAIL_ERROR_NONE) {
12098                                                 EM_DEBUG_EXCEPTION_SEC(" fopen failed - %s [%d]", temp_file_name, error);
12099                                                 goto FINISH_OFF;
12100                                         }
12101
12102                                         /* 2. write the Latest data */
12103                                         nwritten = fwrite(buf1, data_count_to_written-1, 1, fp_dest);
12104
12105                                         if (nwritten > 0) {
12106                                                 EM_DEBUG_LOG(" Latest Data  : [%d ] bytes written ", nwritten);
12107                                                 nwritten = 0;
12108                                                 /* 3. Append old data */
12109                                                 nwritten = fwrite(buf, nread-1, 1, fp_dest);
12110
12111                                                 if (nwritten <= 0) {
12112                                                         EM_DEBUG_EXCEPTION(" Error Occured while writing Old data : [%d ] bytes written ", nwritten);
12113                                                         error = EMAIL_ERROR_SYSTEM_FAILURE;
12114                                                         goto FINISH_OFF;
12115                                                 } else {
12116                                                         EM_DEBUG_LOG(">>>> OLD data appended [ %d ] ", nwritten);
12117
12118                                                         if (!emstorage_move_file(temp_file_name, file_path, false, &err)) {
12119                                                                 EM_DEBUG_EXCEPTION(" emstorage_move_file failed - %d", err);
12120                                                                 goto FINISH_OFF;
12121                                                         }
12122                                                 }
12123
12124                                         } else {
12125                                                 EM_DEBUG_EXCEPTION(" Error Occured while writing New data : [%d ] bytes written ", nwritten);
12126                                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12127                                                 goto FINISH_OFF;
12128                                         }
12129                                 }
12130                         }
12131                 }
12132         }
12133
12134         ret = true;
12135 FINISH_OFF:
12136
12137         EM_SAFE_FREE(buf);
12138         EM_SAFE_FREE(buf1);
12139         EM_SAFE_FREE(low_char_set);
12140         EM_SAFE_FREE(temp_file_name);
12141
12142         if (fp_src != NULL) {
12143                 fclose(fp_src);
12144                 fp_src = NULL;
12145         }
12146
12147         if (fp_dest != NULL) {
12148                 fclose(fp_dest);
12149                 fp_dest = NULL;
12150         }
12151
12152         if (err_code)
12153                 *err_code = error;
12154
12155         EM_DEBUG_FUNC_END("ret [%d]", ret);
12156         return ret;
12157
12158 }
12159
12160 INTERNAL_FUNC int emstorage_move_file(char *src_file, char *dst_file, int sync_status, int *err_code)
12161 {
12162         EM_DEBUG_FUNC_BEGIN("src_file[%p], dst_file[%p], err_code[%p]", src_file, dst_file, err_code);
12163
12164         int ret = false;
12165         int error = EMAIL_ERROR_NONE;
12166         char errno_buf[ERRNO_BUF_SIZE] = {0};
12167
12168         if (src_file == NULL || dst_file == NULL) {
12169                 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
12170                 error = EMAIL_ERROR_INVALID_PARAM;
12171                 goto FINISH_OFF;
12172         }
12173
12174         EM_DEBUG_LOG_SEC("src_file[%s], dst_file[%s]", src_file, dst_file);
12175
12176         if (strcmp(src_file, dst_file) != 0) {
12177                                 EM_DEBUG_LOG("oldpath and newpath are not on the same mounted file system.");
12178                                 if (!emstorage_copy_file(src_file, dst_file, sync_status, &error)) {
12179                                         EM_DEBUG_EXCEPTION("emstorage_copy_file failed - %d", error);
12180
12181                                         struct stat temp_file_stat;
12182                                         if (stat(src_file, &temp_file_stat) < 0) {
12183                                                 EM_DEBUG_EXCEPTION("no src file found [%s] : %s", src_file, EM_STRERROR(errno_buf));
12184                                         }
12185                                         if (stat(dst_file, &temp_file_stat) < 0)
12186                                                 EM_DEBUG_EXCEPTION("no dst file found [%s] : %s", dst_file, EM_STRERROR(errno_buf));
12187
12188                                         error = EMAIL_ERROR_FILE_NOT_FOUND;
12189                                         goto FINISH_OFF;
12190
12191
12192                                 }
12193                                 remove(src_file);
12194                                 EM_DEBUG_LOG("src[%s] removed", src_file);
12195         } else {
12196                 EM_DEBUG_LOG("src[%s] = dst[%s]", src_file, dst_file);
12197         }
12198
12199         ret = true;
12200
12201 FINISH_OFF:
12202         if (err_code != NULL)
12203                 *err_code = error;
12204
12205         EM_DEBUG_FUNC_END("ret [%d]", ret);
12206         return ret;
12207 }
12208
12209 INTERNAL_FUNC int emstorage_delete_file(char *src_file, int *err_code)
12210 {
12211         EM_DEBUG_FUNC_BEGIN("src_file[%p], err_code[%p]", src_file, err_code);
12212
12213         int ret = false;
12214         int error = EMAIL_ERROR_NONE;
12215
12216         if (src_file == NULL) {
12217                 EM_DEBUG_EXCEPTION(" src_file[%p]", src_file);
12218
12219                 error = EMAIL_ERROR_INVALID_PARAM;
12220                 goto FINISH_OFF;
12221         }
12222
12223         if (remove(src_file) != 0) {
12224                 if (errno != ENOENT) {
12225                         EM_DEBUG_EXCEPTION(" remove failed - %d", errno);
12226
12227                         error = EMAIL_ERROR_SYSTEM_FAILURE;
12228                         goto FINISH_OFF;
12229                 } else {
12230                         EM_DEBUG_EXCEPTION(" no file found...");
12231
12232                         error = EMAIL_ERROR_FILE_NOT_FOUND;
12233                 }
12234         }
12235
12236         ret = true;
12237
12238 FINISH_OFF:
12239         if (err_code != NULL)
12240                 *err_code = error;
12241
12242         EM_DEBUG_FUNC_END("ret [%d]", ret);
12243         return ret;
12244 }
12245
12246
12247 INTERNAL_FUNC int emstorage_delete_dir(char *src_dir, int *err_code)
12248 {
12249         EM_DEBUG_FUNC_BEGIN("src_dir[%p], err_code[%p]", src_dir, err_code);
12250
12251         if (src_dir == NULL) {
12252                 EM_DEBUG_EXCEPTION("src_dir[%p]", src_dir);
12253
12254                 if (err_code != NULL)
12255                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12256                 return false;
12257         }
12258
12259         int error = EMAIL_ERROR_NONE;
12260
12261         DIR *dirp;
12262         struct dirent *dp;
12263         struct stat sbuf;
12264         char buf[512];
12265
12266         dirp = opendir(src_dir);
12267
12268         if (dirp == NULL) {
12269                 if (errno == ENOENT) {
12270                         EM_DEBUG_EXCEPTION("directory[%s] does not exist...", src_dir);
12271                         if (err_code != NULL)
12272                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12273                         return true;
12274                 } else {
12275                         EM_DEBUG_EXCEPTION("opendir failed [%s] [%d]", src_dir, errno);
12276                         if (err_code != NULL)
12277                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12278                         return false;
12279                 }
12280         }
12281
12282         while ((dp = readdir(dirp))) {
12283                 if (strncmp(dp->d_name, ".", 1) == 0 || strncmp(dp->d_name, "..", 2) == 0) /* prevent 34360 */
12284                         continue;
12285
12286                 SNPRINTF(buf, sizeof(buf), "%s/%s", src_dir, dp->d_name);
12287
12288                 if (lstat(buf, &sbuf) == 0 || stat(buf, &sbuf) == 0) {
12289                         /*  check directory */
12290                         if ((sbuf.st_mode & S_IFMT) == S_IFDIR) {       /*  directory */
12291                                 /*  recursive call */
12292                                 if (!emstorage_delete_dir(buf, &error)) {
12293                                         closedir(dirp);
12294                                         if (err_code != NULL)
12295                                                 *err_code = error;
12296                                         return false;
12297                                 }
12298                         } else {        /*  file */
12299                                 if (remove(buf) < 0) {
12300                                         EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", buf, errno);
12301                                         closedir(dirp);
12302                                         if (err_code != NULL)
12303                                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12304                                         return false;
12305                                 }
12306                         }
12307                 } else
12308                         EM_DEBUG_EXCEPTION("content does not exist...");
12309         }
12310
12311         closedir(dirp);
12312
12313         EM_DEBUG_LOG_DEV("remove direcotory [%s]", src_dir);
12314
12315         /* EM_DEBUG_FUNC_BEGIN(); */
12316
12317         if (remove(src_dir) < 0) {
12318                 EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", src_dir, errno);
12319                 if (err_code != NULL)
12320                         *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12321                 return false;
12322         }
12323
12324         if (err_code != NULL)
12325                 *err_code = error;
12326
12327         return true;
12328 }
12329
12330 /* faizan.h@samsung.com */
12331 INTERNAL_FUNC int emstorage_update_server_uid(char *multi_user_name,
12332                                                                                                 int mail_id,
12333                                                                                                 char *old_server_uid,
12334                                                                                                 char *new_server_uid,
12335                                                                                                 int *err_code)
12336 {
12337         EM_DEBUG_FUNC_BEGIN("new_server_uid[%s], old_server_uid[%s]", new_server_uid, old_server_uid);
12338         int ret = false;
12339         int error = EMAIL_ERROR_NONE;
12340         int transaction = true;
12341         int temp_strlen = 0;
12342         int and_operation = 0;
12343         char sql_query_string[QUERY_SIZE] = {0, };
12344         char conditional_clause_string[QUERY_SIZE] = {0};
12345
12346         if ((mail_id <= 0 || !old_server_uid) && !new_server_uid) {
12347                 EM_DEBUG_EXCEPTION("Invalid parameters");
12348                 if (err_code)
12349                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12350                 return false;
12351         }
12352
12353         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12354         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12355
12356         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE ");
12357
12358         if (mail_id > 0) {
12359                 temp_strlen = strlen(conditional_clause_string);
12360                 SNPRINTF(conditional_clause_string + temp_strlen, sizeof(conditional_clause_string) - temp_strlen,
12361                                         "mail_id = %d ", mail_id);
12362                 and_operation = 1;
12363         }
12364
12365         if (old_server_uid) {
12366                 temp_strlen = strlen(conditional_clause_string);
12367                 if (!and_operation) {
12368                         sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
12369                                                         "server_mail_id = '%q'", old_server_uid);
12370                 } else {
12371                         sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
12372                                                         "and server_mail_id = '%q'", old_server_uid);
12373                 }
12374         }
12375
12376         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
12377                                                 "UPDATE mail_tbl SET server_mail_id = '%q' %s", new_server_uid, conditional_clause_string);
12378
12379         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12380         if (error != EMAIL_ERROR_NONE) {
12381                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12382                 goto FINISH_OFF;
12383         }
12384
12385         ret = true;
12386
12387 FINISH_OFF:
12388
12389         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12390
12391         if (err_code != NULL)
12392                 *err_code = error;
12393
12394         EM_DEBUG_FUNC_END("ret [%d]", ret);
12395         return ret;
12396
12397 }
12398
12399 INTERNAL_FUNC int emstorage_update_read_mail_uid(char *multi_user_name, int mail_id, char *new_server_uid, char *mbox_name, int *err_code)
12400 {
12401         EM_DEBUG_FUNC_BEGIN_SEC("mail_id[%d], new_server_uid[%s], mbox_name[%s]", mail_id, new_server_uid, mbox_name);
12402
12403         int ret = false;
12404         int error = EMAIL_ERROR_NONE;
12405         char sql_query_string[QUERY_SIZE] = {0, };
12406         int transaction = true;
12407
12408         if (!mail_id || !new_server_uid || !mbox_name) {
12409                 EM_DEBUG_EXCEPTION("Invalid parameters");
12410                 if (err_code != NULL)
12411                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12412                 return false;
12413         }
12414
12415         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12416         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12417
12418
12419         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12420                  "UPDATE mail_read_mail_uid_tbl SET server_uid=\'%s\', mailbox_id=\'%s\', mailbox_name=\'%s\' WHERE local_uid=%d ", new_server_uid, mbox_name, mbox_name, mail_id);
12421
12422         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12423         if (error != EMAIL_ERROR_NONE) {
12424                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12425                         goto FINISH_OFF;
12426         }
12427
12428         ret     = true;
12429
12430 FINISH_OFF:
12431
12432         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12433
12434         if (err_code != NULL)
12435                 *err_code = error;
12436
12437         EM_DEBUG_FUNC_END("ret [%d]", ret);
12438         return ret;
12439
12440 }
12441
12442 INTERNAL_FUNC int emstorage_update_save_status(char *multi_user_name, int account_id, int *err_code)
12443 {
12444         EM_DEBUG_FUNC_BEGIN();
12445
12446         int ret = false;
12447         int transaction = true;
12448         int error = EMAIL_ERROR_NONE;
12449         char sql_query_string[QUERY_SIZE] = {0,};
12450
12451         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12452
12453         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12454
12455         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12456
12457         if (account_id <= ALL_ACCOUNT)
12458                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET save_status = %d WHERE (save_status = %d or save_status = %d)", EMAIL_MAIL_STATUS_NONE, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12459         else
12460                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET save_status = %d WHERE (save_status = %d or save_status = %d) and account_id = %d ", EMAIL_MAIL_STATUS_NONE, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED, account_id);
12461
12462         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12463         if (error != EMAIL_ERROR_NONE) {
12464                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12465                         goto FINISH_OFF;
12466         }
12467
12468         ret = true;
12469
12470 FINISH_OFF:
12471         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12472
12473         if (err_code != NULL)
12474                 *err_code = error;
12475
12476         EM_DEBUG_FUNC_END("ret [%d]", ret);
12477         return ret;
12478
12479 }
12480
12481 int emstorage_get_unread_mailid(char *multi_user_name, int account_id, int vip_mode, int **mail_ids, int *mail_number, int *err_code)
12482 {
12483         EM_DEBUG_FUNC_BEGIN();
12484
12485         if ((!mail_ids) || (account_id <= 0 &&  account_id != -1)) {
12486                 EM_DEBUG_EXCEPTION(" mail_id[%p], account_id[%d] ", mail_ids, account_id);
12487                 if (err_code != NULL)
12488                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12489                 return false;
12490         }
12491
12492         int ret = false;
12493         int rc = -1;
12494         int error = EMAIL_ERROR_NONE;
12495         int count = 0;
12496         int i = 0;
12497         int col_index = 0;
12498         int *p_mail_ids = NULL;
12499         int transaction = false;
12500         char **result = NULL;
12501         char sql_query_string[QUERY_SIZE] = {0, };
12502         char temp_query_string[QUERY_SIZE] = {0,};
12503         char sql_select_query_string[QUERY_SIZE] = {0, };
12504
12505         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12506         EMSTORAGE_START_READ_TRANSACTION(transaction);
12507
12508         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12509
12510         if (account_id == -1)
12511                 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string), "SELECT mail_id FROM mail_tbl WHERE flags_seen_field = 0 AND (save_status = %d or save_status = %d)", EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12512         else
12513                 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string), "SELECT mail_id FROM mail_tbl WHERE account_id = %d AND flags_seen_field = 0 AND (save_status = %d or save_status = %d)", account_id, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12514
12515         if (vip_mode) {
12516                 SNPRINTF(temp_query_string, sizeof(temp_query_string), "%s AND tag_id < 0", sql_select_query_string);
12517         } else {
12518                 SNPRINTF(temp_query_string, sizeof(temp_query_string), "%s", sql_select_query_string);
12519         }
12520
12521         SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s ORDER BY date_time ASC", temp_query_string);
12522
12523         EM_DEBUG_LOG_SEC("query: [%s]", sql_query_string);
12524
12525         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
12526         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
12527                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12528
12529         EM_DEBUG_LOG("Count : %d", count);
12530
12531         if (count == 0) {
12532                 EM_DEBUG_EXCEPTION("no Mails found...");
12533                 ret = false;
12534                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
12535                 goto FINISH_OFF;
12536         }
12537
12538         p_mail_ids = em_malloc(count * sizeof(int));
12539         if (p_mail_ids == NULL) {
12540                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
12541                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12542                 goto FINISH_OFF;
12543         }
12544
12545         col_index = 1;
12546
12547         for (i = 0; i < count; i++) {
12548                 _get_table_field_data_int(result, &(p_mail_ids[i]), col_index++);
12549         }
12550
12551         ret = true;
12552
12553 FINISH_OFF:
12554
12555         if (result)
12556                 sqlite3_free_table(result);
12557
12558         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12559
12560 //      sqlite3_db_release_memory(local_db_handle);
12561
12562         if (ret == true) {
12563                 if (mail_ids != NULL)
12564                         *mail_ids = p_mail_ids;
12565
12566                 if (mail_number != NULL)
12567                         *mail_number = count;
12568         } else {
12569                 EM_SAFE_FREE(p_mail_ids);
12570         }
12571
12572         if (err_code != NULL)
12573                 *err_code = error;
12574
12575         EM_DEBUG_FUNC_END("ret [%d]", ret);
12576         return ret;
12577 }
12578
12579 int setting_system_command(const char *command)
12580
12581  {
12582         int pid = 0, status = 0;
12583         char *const environ[] = { NULL };
12584
12585         if (command == 0)
12586                 return 1;
12587
12588         pid = fork();
12589
12590         if (pid == -1)
12591                 return -1;
12592
12593         if (pid == 0) {
12594                 char *argv[4];
12595
12596                 argv[0] = "sh";
12597                 argv[1] = "-c";
12598                 argv[2] = (char *)command;
12599                 argv[3] = 0;
12600
12601                 execve("/bin/sh", argv, environ);
12602                 abort();
12603         }
12604
12605         do {
12606                 if (waitpid(pid, &status, 0) == -1) {
12607                         if (errno != EINTR)
12608                                 return -1;
12609                 } else {
12610                         return status;
12611                 }
12612         } while (1);
12613
12614         return 0;
12615 }
12616
12617
12618 INTERNAL_FUNC int emstorage_mail_get_total_diskspace_usage(unsigned long *total_usage,  int transaction, int *err_code)
12619 {
12620         EM_DEBUG_FUNC_BEGIN("total_usage[%p],  transaction[%d], err_code[%p]", total_usage, transaction, err_code);
12621
12622         if (!total_usage) {
12623                 EM_DEBUG_EXCEPTION("total_usage[%p]", total_usage);
12624
12625                 if (err_code != NULL)
12626                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12627                 return false;
12628         }
12629
12630         int   ret = false;
12631         int   error = EMAIL_ERROR_NONE;
12632         char  syscmd[256] = {0, };
12633         char  line[256] = {0, };
12634         char *line_from_file = NULL;
12635         FILE *fp = NULL;
12636         unsigned long total_diskusage = 0;
12637
12638         SNPRINTF(syscmd, sizeof(syscmd), "touch %s", SETTING_MEMORY_TEMP_FILE_PATH);
12639         if (setting_system_command(syscmd) == -1) {
12640                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
12641
12642                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12643                 goto FINISH_OFF;
12644         }
12645
12646         SNPRINTF(syscmd, sizeof(syscmd), "du -hsk %s > %s", EMAIL_PATH, SETTING_MEMORY_TEMP_FILE_PATH);
12647         EM_DEBUG_LOG(" cmd : %s", syscmd);
12648         if (setting_system_command(syscmd) == -1) {
12649                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : Setting > Memory] System Command [%s] is failed", syscmd);
12650
12651                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12652                 goto FINISH_OFF;
12653         }
12654
12655         error = em_fopen(SETTING_MEMORY_TEMP_FILE_PATH, "r", &fp);
12656         if (error != EMAIL_ERROR_NONE) {
12657                 perror(SETTING_MEMORY_TEMP_FILE_PATH);
12658                 goto FINISH_OFF;
12659         }
12660
12661         line_from_file = fgets(line, sizeof(line), fp);
12662
12663         if (line_from_file == NULL) {
12664                 EM_DEBUG_EXCEPTION("fgets failed");
12665                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12666                 goto FINISH_OFF;
12667         }
12668         total_diskusage = strtoul(line, NULL, 10);
12669
12670         memset(syscmd, 0, sizeof(syscmd));
12671         SNPRINTF(syscmd, sizeof(syscmd), "rm -f %s", SETTING_MEMORY_TEMP_FILE_PATH);
12672         if (setting_system_command(syscmd) == -1) {
12673                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage :  [Setting > Memory] System Command [%s] is failed", syscmd);
12674                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12675                 goto FINISH_OFF;
12676         }
12677
12678         EM_DEBUG_LOG("[Setting > Memory] @@@@@ Size of Directory [%s] is %ld KB", EMAIL_PATH, total_diskusage);
12679
12680         ret = true;
12681
12682 FINISH_OFF:
12683         if (err_code != NULL)
12684                 *err_code = error;
12685
12686         if (ret)
12687                 *total_usage = total_diskusage;
12688         else
12689                 *total_usage = 0;
12690
12691         if (fp) fclose(fp); /* prevent 32730 */
12692
12693         EM_DEBUG_FUNC_END("ret [%d]", ret);
12694         return ret;
12695 }
12696 #define MAILHOME_UTF8    tzplatform_mkpath(TZ_USER_DATA, "email/.email_data/7/348/UTF-8")
12697
12698 INTERNAL_FUNC int emstorage_test(char *multi_user_name, int mail_id, int account_id, char *full_address_to, char *full_address_cc, char *full_address_bcc, int *err_code)
12699 {
12700         DB_STMT hStmt = NULL;
12701         int ret = false;
12702         int error = EMAIL_ERROR_NONE;
12703         int rc = 0;
12704         char sql_query_string[QUERY_SIZE] = {0, };
12705
12706         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12707                 "INSERT INTO mail_tbl VALUES "
12708                 "(?" /*  mail_id */
12709                 ", ?" /*  account_id */
12710                 ", ?" /*  mail_size */
12711                 ", ?" /*  server_mail_status */
12712                 ", ?" /*  server_mailbox_name */
12713                 ", ?" /*  server_mail_id */
12714                 ", ?" /*  reference_mail_id */
12715                 ", ?" /*  full_address_from */
12716                 ", ?" /*  full_address_reply */
12717                 ", ?" /*  full_address_to */
12718                 ", ?" /*  full_address_cc */
12719                 ", ?" /*  full_address_bcc */
12720                 ", ?" /*  full_address_return */
12721                 ", ?" /*  subject */
12722                 ", ?" /*  body_download_status */
12723                 ", ?" /*  file_path_plain */
12724                 ", ?" /*  file_path_html */
12725                 ", ?" /*  date_time */
12726                 ", ?" /*  flags_seen_field */
12727                 ", ?" /*  flags_deleted_field */
12728                 ", ?" /*  flags_flagged_field */
12729                 ", ?" /*  flags_answered_field */
12730                 ", ?" /*  flags_recent_field */
12731                 ", ?" /*  flags_draft_field */
12732                 ", ?" /*  flags_forwarded_field */
12733                 ", ?" /*  DRM_status */
12734                 ", ?" /*  priority */
12735                 ", ?" /*  save_status */
12736                 ", ?" /*  lock_status */
12737                 ", ?" /*  message_id */
12738                 ", ?" /*  report_status */
12739                 ", ?" /*  email_address_sender */
12740                 ", ?" /*  email_address_recipient */
12741                 ", ?" /*  attachment_count */
12742                 ", ?" /*  inline_content_count */
12743                 ", ?" /*  preview_text */
12744                 ", ?" /*  thread_id */
12745                 ", ?" /*  mailbox_type */
12746                 ", ?" /*  alias_sender */
12747                 ", ?" /*  alias_recipient */
12748                 ", ?" /*  thread_item_count */
12749                 ", ?" /*  meeting_request_status */
12750                 ", ?" /*  message_class */
12751                 ", ?" /*  digest_type */
12752                 ", ?" /*  smime_type */
12753                 ", ?" /*  scheduled_sending_time */
12754                 ", ?" /*  remaining_resend_times */
12755                 ", ?" /*  tag_id   */
12756                 ", ?" /*  replied_time */
12757                 ", ?" /*  forwarded_time */
12758                 ", ?" /*  default_charset */
12759                 ", ?" /*  eas_data_length */
12760                 ", ?" /*  eas_data */
12761                 ")");
12762
12763         int transaction = true;
12764         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12765
12766         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12767
12768
12769         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12770         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
12771                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12772
12773         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_id);
12774         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, account_id);
12775         _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, EMAIL_MAILBOX_TYPE_OUTBOX);
12776         _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, "save test - long", 1, SUBJECT_LEN_IN_MAIL_TBL);
12777         _bind_stmt_field_data_string(hStmt, DATETIME_IDX_IN_MAIL_TBL, "20100316052908", 0, DATETIME_LEN_IN_MAIL_TBL);
12778         _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, 0);
12779         _bind_stmt_field_data_string(hStmt, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL, "", 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
12780         _bind_stmt_field_data_string(hStmt, SERVER_MAIL_ID_IDX_IN_MAIL_TBL, "", 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
12781         _bind_stmt_field_data_string(hStmt, MESSAGE_ID_IDX_IN_MAIL_TBL, "", 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
12782         _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, 0);
12783         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL, "<test08@streaming.s3glab.net>", 1, FROM_LEN_IN_MAIL_TBL);
12784         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL, "", 1, REPLY_TO_LEN_IN_MAIL_TBL);
12785         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL, full_address_to, 1, TO_LEN_IN_MAIL_TBL);
12786         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL, full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
12787         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL, full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
12788         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL, "", 1, RETURN_PATH_LEN_IN_MAIL_TBL);
12789         _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL, "<sender_name@sender_host.com>", 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12790         _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL, "<recipient_name@recipient_host.com>", 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12791         _bind_stmt_field_data_string(hStmt, ALIAS_SENDER_IDX_IN_MAIL_TBL, "send_alias", 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12792         _bind_stmt_field_data_string(hStmt, ALIAS_RECIPIENT_IDX_IN_MAIL_TBL, "recipient_alias", 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12793         _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, 1);
12794         _bind_stmt_field_data_string(hStmt, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL, (char *)MAILHOME_UTF8, 0, TEXT_1_LEN_IN_MAIL_TBL);
12795         _bind_stmt_field_data_string(hStmt, FILE_PATH_HTML_IDX_IN_MAIL_TBL, "", 0, TEXT_2_LEN_IN_MAIL_TBL);
12796         _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, 4);
12797         _bind_stmt_field_data_char(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, 0);
12798         _bind_stmt_field_data_char(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, 0);
12799         _bind_stmt_field_data_char(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, 0);
12800         _bind_stmt_field_data_char(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, 0);
12801         _bind_stmt_field_data_char(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, 0);
12802         _bind_stmt_field_data_char(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, 0);
12803         _bind_stmt_field_data_char(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, 0);
12804         _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, 0);
12805         _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, 0);
12806         _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, 0);
12807         _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, 0);
12808         _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, 0);
12809         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12810         _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, 0);
12811         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12812         _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, 0);
12813         _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, 0);
12814         _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, "preview body", 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
12815         _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, 0);
12816         _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, 0);
12817         _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, 0);
12818         _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, 0);
12819         _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, 0);
12820         _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, 0);
12821         _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, 0);
12822         _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, 0);
12823         _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, 0);
12824         _bind_stmt_field_data_string(hStmt, DEFAULT_CHARSET_IDX_IN_MAIL_TBL, "UTF-8", 0, TEXT_2_LEN_IN_MAIL_TBL);
12825         _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, 0);
12826         _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, NULL, 0);
12827
12828         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12829         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
12830                 ("sqlite3_step fail:%d", rc));
12831         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
12832                 ("sqlite3_step fail:%d", rc));
12833         ret = true;
12834
12835 FINISH_OFF:
12836         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12837         if (hStmt != NULL) {
12838                 rc = sqlite3_finalize(hStmt);
12839                 if (rc != SQLITE_OK) {
12840                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
12841                         error = EMAIL_ERROR_DB_FAILURE;
12842                 }
12843         }
12844
12845         if (err_code != NULL)
12846                 *err_code = error;
12847
12848         EM_DEBUG_FUNC_END("ret [%d]", ret);
12849         return ret;
12850 }
12851
12852
12853 INTERNAL_FUNC int emstorage_get_max_mail_count()
12854 {
12855         return EMAIL_MAIL_MAX_COUNT;
12856 }
12857
12858 #define STRIPPED_SUBJECT_BUFFER_SIZE 4086
12859
12860 INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(char *multi_user_name,
12861                                                                                                                         emstorage_mail_tbl_t *mail_tbl,
12862                                                                                                                         int *thread_id,
12863                                                                                                                         int *result_latest_mail_id_in_thread,
12864                                                                                                                         int *thread_item_count)
12865 {
12866         EM_DEBUG_FUNC_BEGIN("mail_tbl [%p], thread_id [%p], "
12867                                                 "result_latest_mail_id_in_thread [%p], thread_item_count [%p]",
12868                                                 mail_tbl, thread_id, result_latest_mail_id_in_thread, thread_item_count);
12869         EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
12870
12871         int rc = 0, query_size = 0, query_size_account = 0;
12872         int i = 0;
12873         int search_thread = false;
12874         int account_id = 0;
12875         int err_code = EMAIL_ERROR_NONE;
12876         int count = 0, result_thread_id = -1, latest_mail_id_in_thread = -1;
12877         time_t latest_date_time = 0;
12878         char *subject = NULL;
12879         char *p_subject = NULL;
12880         char *sql_query_string = NULL, *sql_account = NULL;
12881         int col_index = 4;
12882         int temp_thread_id = -1;
12883         char *sql_format = "SELECT mail_id, thread_id, date_time, subject "
12884                                                 "FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
12885         char *sql_format_account = " AND account_id = %d ";
12886         char *sql_format_order_by = " ORDER BY thread_id, date_time DESC ";
12887         char **result = NULL;
12888         char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
12889         char stripped_subject2[STRIPPED_SUBJECT_BUFFER_SIZE];
12890
12891         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12892
12893         EM_DEBUG_LOG("subject: [%p], mail_id: [%d]", subject, mail_tbl->mail_id);
12894
12895         EM_IF_NULL_RETURN_VALUE(mail_tbl, EMAIL_ERROR_INVALID_PARAM);
12896         EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
12897         EM_IF_NULL_RETURN_VALUE(result_latest_mail_id_in_thread, EMAIL_ERROR_INVALID_PARAM);
12898         EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
12899
12900         account_id   = mail_tbl->account_id;
12901         subject      = mail_tbl->subject;
12902
12903         EM_DEBUG_LOG_SEC("subject: [%s]", subject);
12904
12905         if (EM_SAFE_STRLEN(subject) == 0 && mail_tbl->mail_id != 0) {
12906                 result_thread_id = mail_tbl->mail_id;
12907                 count = 1;
12908                 goto FINISH_OFF;
12909         }
12910
12911         if (em_find_pos_stripped_subject_for_thread_view(subject,
12912                                                                                                                 stripped_subject,
12913                                                                                                                 STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE)      {
12914                 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
12915                 err_code =  EMAIL_ERROR_UNKNOWN;
12916                 result_thread_id = -1;
12917                 goto FINISH_OFF;
12918         }
12919
12920         EM_DEBUG_LOG_SEC("stripped_subject: [%s]", stripped_subject);
12921
12922         if (EM_SAFE_STRLEN(stripped_subject) == 0) {
12923                 result_thread_id = -1;
12924                 goto FINISH_OFF;
12925         }
12926
12927         EM_DEBUG_LOG_SEC("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s",
12928                                                 EM_SAFE_STRLEN(stripped_subject), stripped_subject);
12929
12930         if (account_id > 0)     {
12931                 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
12932                 sql_account = malloc(query_size_account);
12933                 if (sql_account == NULL) {
12934                         EM_DEBUG_EXCEPTION("malloc for sql_account  is failed %d", query_size_account);
12935                         err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
12936                         goto FINISH_OFF;
12937                 }
12938                 snprintf(sql_account, query_size_account, sql_format_account, account_id);
12939         }
12940
12941         /* prevent 34362 */
12942         query_size = strlen(sql_format) + strlen(stripped_subject)*2 + 50 + query_size_account + strlen(sql_format_order_by); /*  + query_size_mailbox; */
12943         sql_query_string = malloc(query_size);
12944
12945         if (sql_query_string == NULL) {
12946                 EM_DEBUG_EXCEPTION("malloc for sql  is failed %d", query_size);
12947                 err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
12948                 goto FINISH_OFF;
12949         }
12950
12951         sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mail_tbl->mailbox_id);
12952
12953         if (account_id > 0)
12954                 strcat(sql_query_string, sql_account);
12955
12956         strcat(sql_query_string, sql_format_order_by);
12957         strcat(sql_query_string, ";");
12958
12959         EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
12960
12961         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL),
12962                                                                         rc);
12963
12964         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
12965                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12966
12967         EM_DEBUG_LOG("Result rows count : %d", count);
12968
12969         if (count == 0)
12970                 result_thread_id = -1;
12971         else {
12972                 for (i = 0; i < count; i++) {
12973                         EM_SAFE_FREE(p_subject);
12974
12975                         _get_table_field_data_int(result, &latest_mail_id_in_thread, col_index++);
12976                         _get_table_field_data_int(result, &result_thread_id, col_index++);
12977                         _get_table_field_data_time_t(result, &latest_date_time, col_index++);
12978                         _get_table_field_data_string(result, &p_subject, 0, col_index++);
12979
12980                         if (temp_thread_id == result_thread_id)
12981                                 continue;
12982
12983                         temp_thread_id = result_thread_id;
12984
12985                         if (em_find_pos_stripped_subject_for_thread_view(p_subject,
12986                                                                                                                                 stripped_subject2,
12987                                                                                                                                 STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE)      {
12988                                 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
12989                                 err_code = EMAIL_ERROR_UNKNOWN;
12990                                 result_thread_id = -1;
12991                                 goto FINISH_OFF;
12992                         }
12993
12994                         if (g_strcmp0(stripped_subject2, stripped_subject) == 0) {
12995                                 if (latest_date_time < mail_tbl->date_time)
12996                                         *result_latest_mail_id_in_thread = latest_mail_id_in_thread;
12997                                 else
12998                                         *result_latest_mail_id_in_thread = mail_tbl->mail_id;
12999
13000                                 search_thread = true;
13001                                 break;
13002                         }
13003
13004                         if (search_thread) {
13005                                 EM_DEBUG_LOG("latest_mail_id_in_thread [%d], mail_id [%d]",
13006                                                                 latest_mail_id_in_thread, mail_tbl->mail_id);
13007                         } else {
13008                                 result_thread_id = -1;
13009                                 count = 0;
13010                         }
13011                 }
13012
13013         }
13014
13015 FINISH_OFF:
13016
13017         *thread_id = result_thread_id;
13018         *thread_item_count = count;
13019
13020         EM_DEBUG_LOG("Result thread id : %d", *thread_id);
13021         EM_DEBUG_LOG("Result count : %d", *thread_item_count);
13022         EM_DEBUG_LOG("err_code : %d", err_code);
13023
13024         EM_SAFE_FREE(sql_account);
13025         EM_SAFE_FREE(sql_query_string);
13026         EM_SAFE_FREE(p_subject);
13027
13028         sqlite3_free_table(result);
13029
13030         EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
13031
13032         return err_code;
13033 }
13034
13035 INTERNAL_FUNC int emstorage_get_thread_id_from_mailbox(char *multi_user_name, int account_id, int mailbox_id, char *mail_subject, int *thread_id, int *thread_item_count)
13036 {
13037         EM_DEBUG_FUNC_BEGIN("mailbox_id [%d], subject [%p], thread_id [%p], thread_item_count [%p]", mailbox_id, mail_subject, thread_id, thread_item_count);
13038         EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
13039
13040         int rc = 0;
13041         int query_size = 0;
13042         int query_size_account = 0;
13043         int err_code = EMAIL_ERROR_NONE;
13044         int count = 0;
13045         int result_thread_id = -1;
13046         char *sql_query_string = NULL;
13047         char *sql_account = NULL;
13048         char *sql_format = "SELECT thread_id FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
13049         char *sql_format_account = " AND account_id = %d ";
13050         char *sql_format_order_by = " ORDER BY date_time DESC ";
13051         char **result = NULL;
13052         char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
13053         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13054
13055         EM_IF_NULL_RETURN_VALUE(mail_subject, EMAIL_ERROR_INVALID_PARAM);
13056         EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
13057         EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
13058
13059         EM_DEBUG_LOG_SEC("subject: [%s]", mail_subject);
13060
13061         if (em_find_pos_stripped_subject_for_thread_view(mail_subject, stripped_subject, STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE)     {
13062                 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view  is failed");
13063                 err_code =  EMAIL_ERROR_UNKNOWN;
13064                 result_thread_id = -1;
13065                 goto FINISH_OFF;
13066         }
13067
13068         if (EM_SAFE_STRLEN(stripped_subject) == 0) {
13069                 result_thread_id = -1;
13070                 goto FINISH_OFF;
13071         }
13072
13073         EM_DEBUG_LOG("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s", EM_SAFE_STRLEN(stripped_subject), stripped_subject);
13074
13075         if (account_id > 0) {
13076                 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
13077                 sql_account = malloc(query_size_account);
13078                 if (sql_account == NULL) {
13079                         EM_DEBUG_EXCEPTION("malloc for sql_account  is failed %d", query_size_account);
13080                         err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
13081                         goto FINISH_OFF;
13082                 }
13083                 snprintf(sql_account, query_size_account, sql_format_account, account_id);
13084         }
13085
13086         query_size = EM_SAFE_STRLEN(sql_format) + EM_SAFE_STRLEN(stripped_subject)*2 + 50 + query_size_account + EM_SAFE_STRLEN(sql_format_order_by); /*  + query_size_mailbox; */
13087         sql_query_string = malloc(query_size);
13088
13089         if (sql_query_string == NULL) {
13090                 EM_DEBUG_EXCEPTION("malloc for sql  is failed %d", query_size);
13091                 err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
13092                 goto FINISH_OFF;
13093         }
13094
13095         sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mailbox_id);
13096
13097         if (account_id > 0)
13098                 strcat(sql_query_string, sql_account);
13099
13100         strcat(sql_query_string, sql_format_order_by);
13101         strcat(sql_query_string, ";");
13102
13103         EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
13104
13105         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
13106
13107         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13108                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13109
13110         EM_DEBUG_LOG("Result rows count : %d", count);
13111
13112         if (count == 0)
13113                 result_thread_id = -1;
13114         else {
13115                 _get_table_field_data_int(result, &result_thread_id, 1);
13116         }
13117
13118 FINISH_OFF:
13119         *thread_id = result_thread_id;
13120         *thread_item_count = count;
13121
13122         EM_DEBUG_LOG("Result thread id : %d", *thread_id);
13123         EM_DEBUG_LOG("Result count : %d", *thread_item_count);
13124         EM_DEBUG_LOG("err_code : %d", err_code);
13125
13126         EM_SAFE_FREE(sql_account);
13127         EM_SAFE_FREE(sql_query_string);
13128
13129         sqlite3_free_table(result);
13130
13131         EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
13132
13133         return err_code;
13134 }
13135
13136 INTERNAL_FUNC int emstorage_get_thread_information(char *multi_user_name, int thread_id, emstorage_mail_tbl_t** mail_tbl, int transaction, int *err_code)
13137 {
13138         EM_DEBUG_FUNC_BEGIN();
13139
13140         int count = 0, ret = false;
13141         int error = EMAIL_ERROR_NONE;
13142         emstorage_mail_tbl_t *p_data_tbl = NULL;
13143         char conditional_clause[QUERY_SIZE] = {0, };
13144
13145         EM_IF_NULL_RETURN_VALUE(mail_tbl, false);
13146
13147         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE thread_id = %d AND thread_item_count > 0", thread_id);
13148         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
13149
13150         if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
13151                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
13152                 goto FINISH_OFF;
13153         }
13154
13155         if (p_data_tbl)
13156                 EM_DEBUG_LOG("thread_id : %d, thread_item_count : %d", p_data_tbl[0].thread_id, p_data_tbl[0].thread_item_count);
13157
13158         ret = true;
13159
13160 FINISH_OFF:
13161         if (ret == true)
13162                 *mail_tbl = p_data_tbl;
13163         else if (p_data_tbl != NULL)
13164                 emstorage_free_mail(&p_data_tbl, 1, NULL);
13165
13166         if (err_code != NULL)
13167                 *err_code = error;
13168
13169         EM_DEBUG_FUNC_END("ret [%d]", ret);
13170         return ret;
13171 }
13172
13173 INTERNAL_FUNC int emstorage_get_sender_list(char *multi_user_name, int account_id, int mailbox_id, int search_type, const char *search_value, email_sort_type_t sorting, email_sender_list_t** sender_list, int *sender_count,  int *err_code)
13174 {
13175         EM_DEBUG_FUNC_BEGIN_SEC("account_id [%d], mailbox_id [%d], search_type [%d], search_value [%p], sorting [%d], sender_list[%p], sender_count[%p] err_code[%p]"
13176                 , account_id , mailbox_id , search_type , search_value , sorting , sender_list, sender_count, err_code);
13177
13178         if ((!sender_list) || (!sender_count)) {
13179                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
13180                 if (err_code != NULL)
13181                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13182                 return false;
13183         }
13184
13185         int rc = -1, ret = false;
13186         int error = EMAIL_ERROR_NONE;
13187         int count = 0;
13188         int i, col_index = 0;
13189         int read_count = 0;
13190         email_sender_list_t *p_sender_list = NULL;
13191         char sql_query_string[QUERY_SIZE] = {0, };
13192         char **result = NULL;
13193         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13194
13195         SNPRINTF(sql_query_string, sizeof(sql_query_string),
13196                 "SELECT email_address_sender, alias_sender, COUNT(email_address_sender), SUM(flags_seen_field = 1) "
13197                 "FROM mail_tbl ");
13198
13199         /*  mailbox_id */
13200         if (mailbox_id)
13201                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE mailbox_id = %d ", mailbox_id);
13202         else    /*  NULL  means all mailbox_name. but except for trash(3), spambox(5), all emails(for GMail, 7) */
13203                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE mailbox_type not in (3, 5, 7, 8) ");
13204
13205         /*  account id */
13206         /*  '0' (ALL_ACCOUNT) means all account */
13207         if (account_id > ALL_ACCOUNT)
13208                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1), " AND account_id = %d ", account_id);
13209
13210         if (search_value) {
13211                 switch (search_type) {
13212                         case EMAIL_SEARCH_FILTER_SUBJECT:
13213                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13214                                         " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\')) ", search_value);
13215                                 break;
13216                         case EMAIL_SEARCH_FILTER_SENDER:
13217                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13218                                         " AND  ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13219                                         ") ", search_value);
13220                                 break;
13221                         case EMAIL_SEARCH_FILTER_RECIPIENT:
13222                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13223                                         " AND ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13224                                         "       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13225                                         "       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13226                                         ") ", search_value, search_value, search_value);
13227                                 break;
13228                         case EMAIL_SEARCH_FILTER_ALL:
13229                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13230                                         " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') "
13231                                         "       OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13232                                         "                       OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13233                                         "                       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13234                                         "                       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13235                                         "               ) "
13236                                         "       )"
13237                                         ")", search_value, search_value, search_value, search_value, search_value);
13238                                 break;
13239                 }
13240         }
13241
13242
13243         /*  sorting option is not available now. The order of sender list is ascending order by display name */
13244         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
13245                 "GROUP BY email_address_sender "
13246                 "ORDER BY UPPER(alias_sender) ");
13247
13248         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
13249
13250         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
13251         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13252                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13253
13254         EM_DEBUG_LOG("Count of Sender [%d]", count);
13255
13256         if (!(p_sender_list = (email_sender_list_t*)em_malloc(sizeof(email_sender_list_t) * count))) {
13257                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
13258                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13259                 goto FINISH_OFF;
13260         }
13261
13262         col_index = 4;
13263
13264         EM_DEBUG_LOG(">>>> DATA ASSIGN START >>");
13265         for (i = 0; i < count; i++) {
13266                 _get_table_field_data_string(result, &(p_sender_list[i].address), 1, col_index++);
13267                 _get_table_field_data_string(result, &(p_sender_list[i].display_name), 1, col_index++);
13268                 _get_table_field_data_int(result, &(p_sender_list[i].total_count), col_index++);
13269                 _get_table_field_data_int(result, &(read_count), col_index++);
13270                 p_sender_list[i].unread_count = p_sender_list[i].total_count - read_count;              /*  unread count = total - read          */
13271         }
13272         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >>", count);
13273
13274         sqlite3_free_table(result);
13275         result = NULL;
13276
13277         ret = true;
13278
13279 FINISH_OFF:
13280         if (ret == true) {
13281                 *sender_list = p_sender_list;
13282                 *sender_count = count;
13283                 EM_DEBUG_LOG(">>>> COUNT : %d >>", count);
13284         }
13285
13286
13287         if (err_code != NULL)
13288                 *err_code = error;
13289
13290         EM_DEBUG_FUNC_END("ret [%d]", ret);
13291         return ret;
13292 }
13293
13294 INTERNAL_FUNC int emstorage_free_sender_list(email_sender_list_t **sender_list, int count)
13295 {
13296         EM_DEBUG_FUNC_BEGIN("sender_list[%p], count[%d]", sender_list, count);
13297
13298         int err = EMAIL_ERROR_NONE;
13299
13300         if (count > 0) {
13301                 if (!sender_list || !*sender_list) {
13302                         EM_DEBUG_EXCEPTION("sender_list[%p], count[%d]", sender_list, count);
13303                         err = EMAIL_ERROR_INVALID_PARAM;
13304                         return err;
13305                 }
13306
13307                 email_sender_list_t* p = *sender_list;
13308                 int i = 0;
13309
13310                 for (; i < count; i++) {
13311                         EM_SAFE_FREE(p[i].address);
13312                         EM_SAFE_FREE(p[i].display_name);
13313                 }
13314
13315                 EM_SAFE_FREE(p);
13316                 *sender_list = NULL;
13317         }
13318
13319         return err;
13320 }
13321
13322
13323 INTERNAL_FUNC int emstorage_free_address_info_list(email_address_info_list_t **address_info_list)
13324 {
13325         EM_DEBUG_FUNC_BEGIN("address_info_list[%p]", address_info_list);
13326
13327         int err = EMAIL_ERROR_NONE;
13328         email_address_info_t *p_address_info = NULL;
13329         GList *list = NULL;
13330         GList *node = NULL;
13331         int i = 0;
13332
13333         if (!address_info_list || !*address_info_list) {
13334                 EM_DEBUG_EXCEPTION("address_info_list[%p]", address_info_list);
13335                 err = EMAIL_ERROR_INVALID_PARAM;
13336                 return err;
13337         }
13338
13339         /*  delete GLists */
13340         for (i = EMAIL_ADDRESS_TYPE_FROM; i <= EMAIL_ADDRESS_TYPE_BCC; i++) {
13341                 switch (i) {
13342                         case EMAIL_ADDRESS_TYPE_FROM:
13343                                 list = (*address_info_list)->from;
13344                                 break;
13345                         case EMAIL_ADDRESS_TYPE_TO:
13346                                 list = (*address_info_list)->to;
13347                                 break;
13348                         case EMAIL_ADDRESS_TYPE_CC:
13349                                 list = (*address_info_list)->cc;
13350                                 break;
13351                         case EMAIL_ADDRESS_TYPE_BCC:
13352                                 list = (*address_info_list)->bcc;
13353                                 break;
13354                 }
13355
13356                 /*  delete dynamic-allocated memory for each item */
13357                 node = g_list_first(list);
13358                 while (node != NULL) {
13359                         p_address_info = (email_address_info_t*)node->data;
13360                         EM_SAFE_FREE(p_address_info->address);
13361                         EM_SAFE_FREE(p_address_info->display_name);
13362                         EM_SAFE_FREE(node->data);
13363
13364                         node = g_list_next(node);
13365                 }
13366                 g_list_free(list);
13367         }
13368
13369         EM_SAFE_FREE(*address_info_list);
13370         *address_info_list = NULL;
13371
13372         EM_DEBUG_FUNC_END("err [%d]", err);
13373         return err;
13374 }
13375 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
13376
13377 INTERNAL_FUNC int emstorage_add_pbd_activity(char *multi_user_name, email_event_partial_body_thd* local_activity, int *activity_id, int transaction, int *err_code)
13378 {
13379         EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
13380
13381         if (!local_activity || !activity_id) {
13382                 EM_DEBUG_EXCEPTION("local_activity[%p], transaction[%d], activity_id[%p], err_code[%p]", local_activity, activity_id, transaction, err_code);
13383                 if (err_code != NULL)
13384                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13385                 return false;
13386         }
13387
13388         int rc = -1;
13389         int ret = false;
13390         int error = EMAIL_ERROR_NONE;
13391         int i = 0;
13392
13393         char sql_query_string[QUERY_SIZE] = {0, };
13394         DB_STMT hStmt = NULL;
13395         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13396
13397         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13398         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13399         SNPRINTF(sql_query_string, sizeof(sql_query_string),
13400                 "INSERT INTO mail_partial_body_activity_tbl VALUES "
13401                 "("
13402                 "? "  /* Account ID */
13403                 ",?"  /* Local Mail ID */
13404                 ",?"  /* Server mail ID */
13405                 ",?"  /* Activity ID */
13406                 ",?"  /* Activity type*/
13407                 ",?"  /* Mailbox ID*/
13408                 ",?"  /* Mailbox name*/
13409                 ",?"  /* Multi User Name */
13410                 ") ");
13411
13412         char *sql = "SELECT max(rowid) FROM mail_partial_body_activity_tbl;";
13413         char **result = NULL;
13414
13415
13416         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13417
13418         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13419                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13420
13421         if (NULL == result[1]) rc = 1;
13422         else rc = atoi(result[1])+1;
13423         sqlite3_free_table(result);
13424         result = NULL;
13425
13426         *activity_id = local_activity->activity_id = rc;
13427
13428         EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [ %d ], MAIL ID [ %d ], ACTIVITY TYPE [ %d ], SERVER MAIL ID [ %lu ]", \
13429                 local_activity->activity_id, local_activity->mail_id, local_activity->activity_type, local_activity->server_mail_id);
13430
13431         if (local_activity->mailbox_id)
13432                 EM_DEBUG_LOG(" MAILBOX ID [ %d ]", local_activity->mailbox_id);
13433
13434
13435         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13436         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13437                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13438
13439
13440         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
13441         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
13442         _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
13443         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
13444         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
13445         _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
13446         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->mailbox_name, 0, 3999);
13447         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
13448
13449
13450         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13451
13452         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
13453                 ("sqlite3_step fail:%d", rc));
13454         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13455                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
13456
13457         ret = true;
13458
13459 FINISH_OFF:
13460         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13461         if (hStmt != NULL) {
13462                 rc = sqlite3_finalize(hStmt);
13463                 hStmt = NULL;
13464                 if (rc != SQLITE_OK) {
13465                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13466                         error = EMAIL_ERROR_DB_FAILURE;
13467                 }
13468         }
13469
13470         if (err_code != NULL)
13471                 *err_code = error;
13472
13473         EM_DEBUG_FUNC_END("ret [%d]", ret);
13474         return ret;
13475 }
13476
13477 INTERNAL_FUNC int emstorage_get_pbd_mailbox_list(char *multi_user_name, int account_id, int **mailbox_list, int *count, int transaction, int *err_code)
13478 {
13479         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13480
13481         if (account_id < FIRST_ACCOUNT_ID || NULL == mailbox_list || *mailbox_list == NULL || NULL == count) {
13482                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13483                 if (err_code != NULL)
13484                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13485                 return false;
13486         }
13487
13488         int ret = false;
13489         int error = EMAIL_ERROR_NONE;
13490         char **result;
13491         int i = 0, rc = -1;
13492         int *mbox_list = NULL;
13493         DB_STMT hStmt = NULL;
13494         char sql_query_string[QUERY_SIZE] = {0, };
13495
13496         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13497         EMSTORAGE_START_READ_TRANSACTION(transaction);
13498
13499         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13500         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(distinct mailbox_id) FROM mail_partial_body_activity_tbl WHERE account_id = %d order by mailbox_id", account_id);
13501
13502
13503         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13504         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13505                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13506
13507         *count = atoi(result[1]);
13508         sqlite3_free_table(result);
13509
13510         if (*count <= 0) {
13511                 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
13512                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13513                 ret = true;
13514                 goto FINISH_OFF;
13515         }
13516         EM_DEBUG_LOG("Mailbox count = %d", *count);
13517
13518         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13519
13520         /* SNPRINTF(g_sql_query, sizeof(g_sql_query), "SELECT distinct mailbox_name FROM mail_partial_body_activity_tbl WHERE account_id = %d order by activity_id", account_id); */
13521         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct mailbox_id FROM mail_partial_body_activity_tbl WHERE account_id = %d order by mailbox_id", account_id);
13522
13523         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13524
13525
13526         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13527
13528
13529         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13530                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13531
13532
13533         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13534         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13535                 ("sqlite3_step fail:%d", rc));
13536
13537         mbox_list = (int *)em_malloc(sizeof(int) * (*count));
13538         if (NULL == mbox_list) {
13539                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
13540                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13541                 goto FINISH_OFF;
13542         }
13543
13544         memset(mbox_list, 0x00, sizeof(int) * (*count));
13545
13546         for (i = 0; i < (*count); i++) {
13547                 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
13548                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13549                 /* EM_DEBUG_LOG("In emstorage_get_pdb_mailbox_list() loop, After sqlite3_step(), , i = %d, rc = %d.", i,  rc); */
13550                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13551                         ("sqlite3_step fail:%d", rc));
13552                 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
13553         }
13554
13555         ret = true;
13556
13557 FINISH_OFF:
13558         if (ret == true)
13559                 *mailbox_list = mbox_list;
13560         else
13561                 EM_SAFE_FREE(mbox_list);
13562
13563         if (hStmt != NULL) {
13564                 rc = sqlite3_finalize(hStmt);
13565                 hStmt = NULL;
13566                 if (rc != SQLITE_OK) {
13567                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13568                         error = EMAIL_ERROR_DB_FAILURE;
13569                 }
13570         }
13571
13572         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13573         if (err_code != NULL)
13574                 *err_code = error;
13575         EM_DEBUG_FUNC_END("ret [%d]", ret);
13576         return ret;
13577 }
13578
13579 INTERNAL_FUNC int emstorage_get_pbd_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
13580 {
13581         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13582
13583         if (NULL == account_list || NULL == count) {
13584                 EM_DEBUG_EXCEPTION("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13585                 if (err_code != NULL)
13586                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13587                 return false;
13588         }
13589
13590         int ret = false;
13591         int error = EMAIL_ERROR_NONE;
13592         char *sql = "SELECT count(distinct account_id) FROM mail_partial_body_activity_tbl";
13593         char **result;
13594         int i = 0, rc = -1;
13595         int *result_account_list = NULL;
13596         DB_STMT hStmt = NULL;
13597         char sql_query_string[QUERY_SIZE] = {0, };
13598
13599         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13600
13601         EMSTORAGE_START_READ_TRANSACTION(transaction);
13602
13603         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13604         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13605                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13606
13607         *count = atoi(result[1]);
13608         sqlite3_free_table(result);
13609
13610         if (*count <= 0) {
13611                 EM_DEBUG_EXCEPTION("no account found...");
13612                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13613                 ret = true;
13614                 goto FINISH_OFF;
13615         }
13616
13617         EM_DEBUG_LOG("Account count [%d]", *count);
13618
13619         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13620
13621         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_partial_body_activity_tbl");
13622
13623         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13624
13625
13626         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13627
13628         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
13629         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13630                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13631
13632
13633         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13634         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13635                 ("sqlite3_step fail:%d", rc));
13636
13637         if (NULL == (result_account_list = (int *)em_malloc(sizeof(int) * (*count)))) {
13638                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
13639                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13640                 goto FINISH_OFF;
13641         }
13642
13643         memset(result_account_list, 0x00, sizeof(int) * (*count));
13644
13645         for (i = 0; i < (*count); i++) {
13646                 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
13647
13648                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13649                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13650                         ("sqlite3_step fail:%d", rc));
13651                 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
13652         }
13653
13654         ret = true;
13655
13656 FINISH_OFF:
13657         if (ret == true)
13658                 *account_list = result_account_list;
13659         else
13660                 EM_SAFE_FREE(result_account_list);
13661
13662         if (hStmt != NULL) {
13663                 rc = sqlite3_finalize(hStmt);
13664                 hStmt = NULL;
13665                 if (rc != SQLITE_OK) {
13666                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13667                         error = EMAIL_ERROR_DB_FAILURE;
13668                 }
13669         }
13670
13671         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13672         if (err_code != NULL)
13673                 *err_code = error;
13674         EM_DEBUG_FUNC_END("ret [%d]", ret);
13675         return ret;
13676 }
13677
13678 INTERNAL_FUNC int emstorage_get_pbd_activity_data(char *multi_user_name, int account_id, int input_mailbox_id, email_event_partial_body_thd** event_start, int *count, int transaction, int *err_code)
13679 {
13680         EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
13681
13682         if (account_id < FIRST_ACCOUNT_ID || NULL == event_start || 0 == input_mailbox_id || NULL == count) {
13683                 EM_DEBUG_EXCEPTION("account_id[%d], email_event_partial_body_thd[%p], input_mailbox_id[%d], count[%p], err_code[%p]", account_id, event_start, input_mailbox_id, count, err_code);
13684
13685                 if (err_code != NULL)
13686                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13687                 return false;
13688         }
13689
13690         int rc = -1;
13691         int ret = false;
13692         char **result;
13693         int error = EMAIL_ERROR_NONE;
13694         int i = 0;
13695         DB_STMT hStmt = NULL;
13696         email_event_partial_body_thd* event_list = NULL;
13697         char sql_query_string[QUERY_SIZE] = {0, };
13698
13699         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13700
13701         EMSTORAGE_START_READ_TRANSACTION(transaction);
13702
13703         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13704         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl WHERE account_id = %d AND mailbox_id = '%d' order by activity_id", account_id, input_mailbox_id);
13705
13706
13707         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13708         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13709                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13710
13711         *count = atoi(result[1]);
13712         sqlite3_free_table(result);
13713
13714         EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
13715
13716         if (*count <= 0) {
13717                 EM_DEBUG_LOG("No matched activity found in mail_partial_body_activity_tbl");
13718                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
13719                 ret = true;
13720                 goto FINISH_OFF;
13721         }
13722         EM_DEBUG_LOG("Activity Count = %d", *count);
13723
13724         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13725         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_partial_body_activity_tbl WHERE account_id = %d AND mailbox_id = '%d' order by activity_id", account_id, input_mailbox_id);
13726
13727         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13728
13729
13730         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13731
13732         EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
13733         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13734                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13735
13736
13737         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13738         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13739                 ("sqlite3_step fail:%d", rc));
13740
13741         if (!(event_list = (email_event_partial_body_thd*)em_malloc(sizeof(email_event_partial_body_thd) * (*count)))) {
13742                 EM_DEBUG_EXCEPTION("Malloc failed");
13743
13744                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13745                 goto FINISH_OFF;
13746         }
13747         memset(event_list, 0x00, sizeof(email_event_partial_body_thd) * (*count));
13748
13749         for (i = 0; i < (*count); i++) {
13750                 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_IDX_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13751                 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13752                 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13753                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13754                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_type), ACTIVITY_TYPE_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13755                 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13756                 _get_stmt_field_data_string(hStmt, &(event_list[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13757                 _get_stmt_field_data_string(hStmt, &(event_list[i].multi_user_name), 0, MULTI_USER_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13758
13759                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13760                 /* EM_DEBUG_LOG("In emstorage_get_pbd_activity_data() loop, After sqlite3_step(), , i = %d, rc = %d.", i,  rc); */
13761                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13762                         ("sqlite3_step fail:%d", rc));
13763
13764                 event_list[i].event_type = 0;
13765         }
13766
13767         ret = true;
13768
13769 FINISH_OFF:
13770         if (true == ret)
13771           *event_start = event_list;
13772         else {
13773                 for (i = 0; i < (*count); i++)
13774                         emcore_free_partial_body_thd_event(event_list, NULL);
13775                 EM_SAFE_FREE(event_list); /*prevent 54559*/
13776                 *event_start = NULL;
13777                 *count = 0;
13778         }
13779
13780         if (hStmt != NULL) {
13781                 rc = sqlite3_finalize(hStmt);
13782                 hStmt = NULL;
13783                 if (rc != SQLITE_OK) {
13784                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13785                         error = EMAIL_ERROR_DB_FAILURE;
13786                 }
13787         }
13788
13789         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13790         if (err_code != NULL)
13791                 *err_code = error;
13792
13793         EM_DEBUG_FUNC_END("ret [%d]", ret);
13794         return ret;
13795 }
13796
13797 INTERNAL_FUNC int emstorage_delete_pbd_activity(char *multi_user_name, int account_id, int mail_id, int activity_id, int transaction, int *err_code)
13798 {
13799         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d] , activity_id[%d], transaction[%d], err_code[%p]", account_id, mail_id, activity_id, transaction, err_code);
13800
13801
13802         if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
13803                 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], activity_id[%d], transaction[%d], err_code[%p]", account_id, mail_id, activity_id, transaction, err_code);
13804
13805                 if (err_code != NULL)
13806                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13807                 return false;
13808         }
13809
13810         int rc = -1;
13811         int ret = false;
13812         int error = EMAIL_ERROR_NONE;
13813         char sql_query_string[QUERY_SIZE] = {0, };
13814         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13815
13816         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13817         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13818
13819         if (activity_id == 0)
13820                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d AND mail_id = %d", account_id, mail_id);
13821         else
13822                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d AND activity_id = %d", account_id, activity_id);
13823
13824         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13825         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
13826         if (error != EMAIL_ERROR_NONE) {
13827                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
13828                         goto FINISH_OFF;
13829         }
13830
13831         /*  validate activity existence */
13832         rc = sqlite3_changes(local_db_handle);
13833         if (rc == 0) {
13834                 EM_DEBUG_EXCEPTION("No matching activity found");
13835                 error = EMAIL_ERROR_DATA_NOT_FOUND;
13836                 ret = true;
13837                 goto FINISH_OFF;
13838         }
13839
13840         ret = true;
13841
13842 FINISH_OFF:
13843         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13844
13845         if (err_code != NULL)
13846                 *err_code = error;
13847
13848         EM_DEBUG_FUNC_END("ret [%d]", ret);
13849         return ret;
13850 }
13851
13852 INTERNAL_FUNC int emstorage_get_mailbox_pbd_activity_count(char *multi_user_name, int account_id, int input_mailbox_id, int *activity_count, int transaction, int *err_code)
13853 {
13854         EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
13855
13856         if (account_id < FIRST_ACCOUNT_ID || NULL == activity_count || NULL == err_code) {
13857                 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
13858                 if (err_code != NULL)
13859                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13860                 return false;
13861         }
13862         int rc = -1;
13863         int ret = false;
13864         int error = EMAIL_ERROR_NONE;
13865         char sql_query_string[QUERY_SIZE] = {0, };
13866
13867         DB_STMT hStmt = NULL;
13868
13869         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13870
13871         EMSTORAGE_START_READ_TRANSACTION(transaction);
13872         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13873
13874         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl WHERE account_id = %d and mailbox_id = '%d'", account_id, input_mailbox_id);
13875
13876         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13877
13878
13879         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13880         EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
13881         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13882                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13883
13884
13885         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13886         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13887                 ("sqlite3_step fail:%d", rc));
13888
13889         _get_stmt_field_data_int(hStmt, activity_count, 0);
13890
13891         EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
13892
13893         ret = true;
13894
13895 FINISH_OFF:
13896
13897         if (hStmt != NULL) {
13898                 rc = sqlite3_finalize(hStmt);
13899                 hStmt = NULL;
13900                 if (rc != SQLITE_OK) {
13901                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13902                         error = EMAIL_ERROR_DB_FAILURE;
13903                 }
13904         }
13905
13906         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13907
13908         if (err_code != NULL)
13909                 *err_code = error;
13910
13911         EM_DEBUG_FUNC_END("ret [%d]", ret);
13912         return ret;
13913 }
13914
13915 INTERNAL_FUNC int emstorage_get_pbd_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
13916 {
13917         EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
13918
13919         if (NULL == activity_count || NULL == err_code) {
13920                 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
13921                 if (err_code != NULL)
13922                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13923                 return false;
13924         }
13925         int rc = -1;
13926         int ret = false;
13927         int error = EMAIL_ERROR_NONE;
13928         DB_STMT hStmt = NULL;
13929         char sql_query_string[QUERY_SIZE] = {0, };
13930
13931         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13932
13933         EMSTORAGE_START_READ_TRANSACTION(transaction);
13934         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13935
13936         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl;");
13937
13938         EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
13939
13940         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13941         EM_DEBUG_LOG_DEV("  before sqlite3_prepare hStmt = %p", hStmt);
13942         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13943                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13944
13945         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13946         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13947                 ("sqlite3_step fail:%d", rc));
13948
13949         _get_stmt_field_data_int(hStmt, activity_count, 0);
13950
13951         EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
13952
13953         ret = true;
13954
13955 FINISH_OFF:
13956
13957
13958         if (hStmt != NULL) {
13959                 rc = sqlite3_finalize(hStmt);
13960                 hStmt = NULL;
13961                 if (rc != SQLITE_OK) {
13962                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13963                         error = EMAIL_ERROR_DB_FAILURE;
13964                 }
13965         }
13966
13967         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13968         if (err_code != NULL)
13969                 *err_code = error;
13970
13971         EM_DEBUG_FUNC_END("ret [%d]", ret);
13972         return ret;
13973 }
13974
13975 INTERNAL_FUNC int emstorage_delete_full_pbd_activity_data(char *multi_user_name, int account_id, int transaction, int *err_code)
13976 {
13977         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
13978         if (account_id < FIRST_ACCOUNT_ID) {
13979                 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
13980                 if (err_code != NULL)
13981                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13982                 return false;
13983         }
13984
13985         int rc = -1;
13986         int ret = false;
13987         int error = EMAIL_ERROR_NONE;
13988         char sql_query_string[QUERY_SIZE] = {0, };
13989
13990         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13991
13992         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13993         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13994         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d", account_id);
13995
13996         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13997         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
13998         if (error != EMAIL_ERROR_NONE) {
13999                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14000                         goto FINISH_OFF;
14001         }
14002
14003         rc = sqlite3_changes(local_db_handle);
14004         if (rc == 0) {
14005                 EM_DEBUG_EXCEPTION("No matching activities found in mail_partial_body_activity_tbl");
14006                 error = EMAIL_ERROR_DATA_NOT_FOUND;
14007                 ret = true;
14008                 goto FINISH_OFF;
14009         }
14010
14011         ret = true;
14012
14013 FINISH_OFF:
14014
14015         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14016
14017         if (err_code != NULL)
14018                 *err_code = error;
14019
14020         EM_DEBUG_FUNC_END("ret [%d]", ret);
14021         return ret;
14022 }
14023
14024 /*Himanshu[h.gahlaut]-> Added below API to update mail_partial_body_activity_tbl
14025 if a mail is moved before its partial body is downloaded.Currently not used but should be used if mail move from server is enabled*/
14026
14027 INTERNAL_FUNC int emstorage_update_pbd_activity(char *multi_user_name, char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code)
14028 {
14029         EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
14030
14031         int rc = -1, ret = false;
14032         int error = EMAIL_ERROR_NONE;
14033         char sql_query_string[QUERY_SIZE] = {0, };
14034
14035         int transaction = true;
14036
14037         if (!old_server_uid || !new_server_uid || !mbox_name) {
14038                 EM_DEBUG_EXCEPTION("Invalid parameters");
14039                 error = EMAIL_ERROR_INVALID_PARAM;
14040                 return false;
14041         }
14042
14043         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14044
14045         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14046         memset(sql_query_string, 0x00, sizeof(sql_query_string));
14047         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14048                  "UPDATE mail_partial_body_activity_tbl SET server_mail_id = %s , mailbox_name=\'%s\' WHERE server_mail_id = %s ", new_server_uid, mbox_name, old_server_uid);
14049
14050         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14051         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14052         if (error != EMAIL_ERROR_NONE) {
14053                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14054                         goto FINISH_OFF;
14055         }
14056
14057         rc = sqlite3_changes(local_db_handle);
14058         if (rc == 0) {
14059                 EM_DEBUG_LOG("No matching found in mail_partial_body_activity_tbl");
14060         }
14061
14062         ret = true;
14063
14064 FINISH_OFF:
14065         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14066
14067         if (err_code != NULL)
14068                 *err_code = error;
14069
14070         EM_DEBUG_FUNC_END("ret [%d]", ret);
14071         return ret;
14072 }
14073
14074
14075 INTERNAL_FUNC int emstorage_create_file(char *data_string, size_t file_size, char *dst_file_name, int *err_code)
14076 {
14077         EM_DEBUG_FUNC_BEGIN_SEC("file_size[%d] , dst_file_name[%s], err_code[%p]", file_size, dst_file_name, err_code);
14078
14079         int ret = false;
14080         int error = EMAIL_ERROR_NONE;
14081         FILE* fp_dst = NULL;
14082         char errno_buf[ERRNO_BUF_SIZE] = {0};
14083
14084         if (!data_string || !dst_file_name) {
14085                 EM_DEBUG_LOG("data_string[%p], dst_file_name[%p]", data_string, dst_file_name);
14086                 error = EMAIL_ERROR_INVALID_PARAM;
14087                 goto FINISH_OFF;
14088         }
14089
14090         error = em_fopen(dst_file_name, "w", &fp_dst);
14091         if (error != EMAIL_ERROR_NONE) {
14092                 EM_DEBUG_EXCEPTION_SEC("em_fopen failed - %s: %d", dst_file_name, error);
14093                 goto FINISH_OFF;
14094         }
14095
14096         if (fwrite(data_string, 1, file_size, fp_dst) == 0) {
14097                 EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
14098                 error = EMAIL_ERROR_UNKNOWN;
14099                 goto FINISH_OFF;
14100         }
14101
14102         ret = true;
14103
14104 FINISH_OFF:
14105
14106         if (fp_dst != NULL)
14107                 fclose(fp_dst);
14108
14109         if (err_code != NULL)
14110                 *err_code = error;
14111
14112         EM_DEBUG_FUNC_END("ret [%d]", ret);
14113         return ret;
14114 }
14115
14116 #endif /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
14117
14118
14119
14120 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
14121 INTERNAL_FUNC int emstorage_update_read_mail_uid_by_server_uid(char *multi_user_name, char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code)
14122 {
14123         EM_DEBUG_FUNC_BEGIN();
14124         int rc = -1;
14125         int ret = false;
14126         int error = EMAIL_ERROR_NONE;
14127         char sql_query_string[QUERY_SIZE] = {0, };
14128
14129         int transaction = true;
14130
14131         if (!old_server_uid || !new_server_uid || !mbox_name) {
14132                 EM_DEBUG_EXCEPTION("Invalid parameters");
14133                 if (err_code != NULL)
14134                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14135                 return false;
14136         }
14137
14138         EM_DEBUG_LOG_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
14139
14140         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14141         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14142
14143
14144         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14145                  "UPDATE mail_read_mail_uid_tbl SET server_uid=\'%s\' , mailbox_name=\'%s\' WHERE server_uid=%s ", new_server_uid, mbox_name, old_server_uid);
14146
14147         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
14148         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14149         if (error != EMAIL_ERROR_NONE) {
14150                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14151                         goto FINISH_OFF;
14152         }
14153
14154         rc = sqlite3_changes(local_db_handle);
14155         if (rc == 0) {
14156                 EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
14157                 error = EMAIL_ERROR_DATA_NOT_FOUND;
14158                 goto FINISH_OFF;
14159         }
14160
14161         ret = true;
14162
14163 FINISH_OFF:
14164         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14165
14166         if (err_code != NULL)
14167                 *err_code = error;
14168
14169         EM_DEBUG_FUNC_END("ret [%d]", ret);
14170         return ret;
14171
14172 }
14173
14174
14175 /**
14176  * @fn emstorage_get_id_set_from_mail_ids(int mail_ids[], int mail_id_count, email_id_set_t** server_uids, int *id_set_count, int *err_code);
14177  * Prepare an array of mail_id and corresponding server mail id.
14178  *
14179  *@author                                       h.gahlaut@samsung.com
14180  * @param[in] mail_ids                  Specifies the comma separated string of mail_ids. Maximaum size of string should be less than or equal to (QUERY_SIZE - 88)
14181  *                                                      where 88 is the length of fixed keywords including ending null character in the QUERY to be formed
14182  * @param[out] idset                    Returns the array of mail_id and corresponding server_mail_id sorted by server_mail_ids
14183  * @param[out] id_set_count             Returns the no. of cells in idset array i.e. no. of sets of mail_ids and server_mail_ids
14184  * @param[out] err_code         Returns the error code.
14185  * @remarks                                     An Example of Query to be exexuted in this API:
14186  *                                                      SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (12, 13, 56, 78);
14187  * @return This function returns true on success or false on failure.
14188  */
14189 INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *multi_user_name,
14190                                                                                                                 char *mail_ids,
14191                                                                                                                 email_id_set_t** idset,
14192                                                                                                                 int *id_set_count,
14193                                                                                                                 int *err_code)
14194 {
14195         EM_DEBUG_FUNC_BEGIN();
14196         EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds);
14197
14198         int error = EMAIL_ERROR_NONE;
14199         int ret = false;
14200         email_id_set_t* p_id_set = NULL;
14201         int count = 0;
14202         const int buf_size = QUERY_SIZE;
14203         char sql_query_string[QUERY_SIZE] = {0, };
14204         int space_left_in_query_buffer = buf_size;
14205         int i = 0;
14206         int rc = -1;
14207         char *server_mail_id = NULL;
14208         char **result = NULL;
14209         int col_index = 0;
14210
14211
14212         if (NULL == mail_ids || NULL == idset || NULL == id_set_count) {
14213                 EM_DEBUG_EXCEPTION("Invalid Parameters mail_ids[%p] idset[%p] id_set_count [%p]",
14214                                                         mail_ids, idset, id_set_count);
14215                 if (err_code != NULL)
14216                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14217                 return false;
14218         }
14219
14220         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14221
14222         SNPRINTF(sql_query_string, space_left_in_query_buffer,
14223                         "SELECT local_uid, server_uid FROM mail_read_mail_uid_tbl WHERE local_uid in (%s) ORDER BY server_uid",
14224                         mail_ids);
14225
14226         EM_DEBUG_LOG_SEC("SQL Query formed [%s] ", sql_query_string);
14227
14228         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14229         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14230         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result);
14231                                                 goto FINISH_OFF; },     ("SQL(%s) sqlite3_get_table fail:%d -%s",
14232                                                 sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14233
14234         EM_DEBUG_LOG(" Count of mails [%d ]", count);
14235
14236         if (count <= 0) {
14237                 EM_DEBUG_EXCEPTION("Can't find proper mail");
14238                 error = EMAIL_ERROR_DATA_NOT_FOUND;
14239                 goto FINISH_OFF;
14240         }
14241
14242         if (NULL == (p_id_set = (email_id_set_t*)em_malloc(sizeof(email_id_set_t) * count))) {
14243                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
14244                 error = EMAIL_ERROR_OUT_OF_MEMORY;
14245                 goto FINISH_OFF;
14246         }
14247
14248         col_index = 2;
14249
14250         EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds_Loop);
14251         EM_DEBUG_LOG(">>>> DATA ASSIGN START");
14252         for (i = 0; i < count; i++) {
14253                 _get_table_field_data_int(result, &(p_id_set[i].mail_id), col_index++);
14254                 _get_table_field_data_string(result, &server_mail_id, 1, col_index++);
14255                 if (server_mail_id) {
14256                         p_id_set[i].server_mail_id = strtoul(server_mail_id, NULL, 10);
14257                         EM_SAFE_FREE(server_mail_id);
14258                 }
14259         }
14260         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d]", count);
14261         EM_PROFILE_END(EmStorageGetIdSetFromMailIds_Loop);
14262
14263         sqlite3_free_table(result);
14264         result = NULL;
14265
14266         ret = true;
14267
14268 FINISH_OFF:
14269
14270         if (ret == true) {
14271                 *idset = p_id_set;
14272                 *id_set_count = count;
14273                 EM_DEBUG_LOG(" idset[%p] id_set_count [%d]", *idset, *id_set_count);
14274         } else
14275                 EM_SAFE_FREE(p_id_set);
14276
14277
14278         if (err_code != NULL)
14279                 *err_code = error;
14280
14281         EM_PROFILE_END(EmStorageGetIdSetFromMailIds);
14282
14283         EM_DEBUG_FUNC_END("ret [%d]", ret);
14284         return ret;
14285 }
14286
14287
14288
14289 #endif
14290
14291 INTERNAL_FUNC int emstorage_delete_triggers_from_lucene(char *multi_user_name)
14292 {
14293         EM_DEBUG_FUNC_BEGIN();
14294         int ret = true, transaction = true;
14295         int error = EMAIL_ERROR_NONE;
14296         char sql_query_string[QUERY_SIZE] = {0, };
14297         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14298         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14299
14300         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerDelete;");
14301         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14302         if (error != EMAIL_ERROR_NONE) {
14303                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14304                         goto FINISH_OFF;
14305         }
14306
14307         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerInsert;");
14308         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14309         if (error != EMAIL_ERROR_NONE) {
14310                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14311                         goto FINISH_OFF;
14312         }
14313
14314         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerUpdate;");
14315         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14316         if (error != EMAIL_ERROR_NONE) {
14317                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14318                         goto FINISH_OFF;
14319         }
14320
14321         ret = true;
14322
14323 FINISH_OFF:
14324         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14325
14326         EM_DEBUG_FUNC_END("ret [%d]", ret);
14327         return ret;
14328 }
14329
14330 INTERNAL_FUNC int emstorage_update_tag_id(char *multi_user_name, int old_filter_id, int new_filter_id, int *err_code)
14331 {
14332         EM_DEBUG_FUNC_BEGIN("new_filter_id[%d], old_filter_id[%d]", new_filter_id, old_filter_id);
14333         int ret = false;
14334         int error = EMAIL_ERROR_NONE;
14335         char sql_query_string[QUERY_SIZE] = {0, };
14336         int transaction = true;
14337
14338         if (old_filter_id < 0 || new_filter_id < 0) {
14339                 EM_DEBUG_EXCEPTION("Invalid parameters");
14340                 if (err_code != NULL)
14341                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14342                 return false;
14343         }
14344
14345         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14346
14347         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14348
14349         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14350                  "UPDATE mail_tbl SET tag_id=%d WHERE tag_id=%d ", new_filter_id, old_filter_id);
14351
14352         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14353         if (error != EMAIL_ERROR_NONE) {
14354                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14355                         goto FINISH_OFF;
14356         }
14357
14358         ret = true;
14359
14360 FINISH_OFF:
14361         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14362
14363         if (err_code != NULL)
14364                 *err_code = error;
14365
14366         EM_DEBUG_FUNC_END("ret [%d]", ret);
14367         return ret;
14368 }
14369
14370 INTERNAL_FUNC int emstorage_filter_mails_by_rule(char *multi_user_name, int account_id, int dest_mailbox_id, int dest_mailbox_type, int reset, emstorage_rule_tbl_t *rule, int ** filtered_mail_id_list, int *count_of_mails, int *err_code)
14371 {
14372         EM_DEBUG_FUNC_BEGIN("account_id[%d], dest_mailbox_id[%d] rule[%p], filtered_mail_id_list[%p], count_of_mails[%p], err_code[%p]", account_id, dest_mailbox_id, rule, filtered_mail_id_list, count_of_mails, err_code);
14373
14374         if ((account_id < 0) || (dest_mailbox_id < 0) || (!rule)) {
14375                 EM_DEBUG_EXCEPTION("Invalid Parameter");
14376
14377                 if (err_code != NULL)
14378                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14379                 return false;
14380         }
14381
14382         int rc = -1, ret = false, error = EMAIL_ERROR_NONE;
14383         int count = 0, col_index = 0, i = 0, where_pararaph_length = 0, *mail_list = NULL;
14384         int tag_id = rule->rule_id;
14385         char **result = NULL, *where_pararaph = NULL;
14386         char sql_query_string[QUERY_SIZE] = {0, };
14387         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14388
14389         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl ");
14390
14391         where_pararaph_length = EM_SAFE_STRLEN(rule->value) + 2 * (EM_SAFE_STRLEN(rule->value2)) + 100;
14392         where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
14393         if (where_pararaph == NULL) {
14394                 EM_DEBUG_EXCEPTION("malloc failed for where_pararaph.");
14395                 error = EMAIL_ERROR_OUT_OF_MEMORY;
14396                 goto FINISH_OFF;
14397         }
14398
14399         if (account_id != ALL_ACCOUNT)
14400                 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (0)", account_id);
14401         else
14402                 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_type NOT in (0)");
14403
14404         if (rule->type & EMAIL_FILTER_SUBJECT) {
14405                 if (rule->flag2 == RULE_TYPE_INCLUDES)
14406                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject like \'%%%q%%\'", rule->value);
14407                 else /*  RULE_TYPE_EXACTLY */
14408                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject = \'%q\'", rule->value);
14409         }
14410
14411         if (rule->type & EMAIL_FILTER_FROM) {
14412                 if (rule->flag2 == RULE_TYPE_INCLUDES)
14413                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND full_address_from like \'%%%q%%\'", rule->value2);
14414 #ifdef __FEATURE_COMPARE_DOMAIN__
14415                 else if (rule->flag2 == RULE_TYPE_COMPARE_DOMAIN)
14416                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND (full_address_from like \'@%%%q\' OR full_address_from like \'@%%%q>%%\')", rule->value2, rule->value2);
14417 #endif /*__FEATURE_COMPARE_DOMAIN__ */
14418                 else /*  RULE_TYPE_EXACTLY */
14419                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND full_address_from = \'%q\'", rule->value2);
14420         }
14421
14422         if (rule->type == EMAIL_PRIORITY_SENDER) {
14423                 if (rule->flag2 == RULE_TYPE_INCLUDES)
14424                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND full_address_from like \'%%%q%%\'", rule->value2);
14425                 else /*  RULE_TYPE_EXACTLY */
14426                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND full_address_from = \'%q\' OR email_address_sender = \'%q\'", rule->value2, rule->value2);
14427
14428                 tag_id = PRIORITY_SENDER_TAG_ID;
14429         }
14430
14431         /* prevent 34361 */
14432         if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14433                 strcat(sql_query_string, where_pararaph);
14434
14435         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14436
14437         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14438         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14439         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
14440                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14441
14442         EM_DEBUG_LOG("Count of mails [%d]", count);
14443
14444         if (count) {
14445                 mail_list = malloc(sizeof(int) * count);
14446                 if (mail_list == NULL) {
14447                         EM_DEBUG_EXCEPTION("malloc failed for mail_list.");
14448                         error = EMAIL_ERROR_OUT_OF_MEMORY;
14449                         goto FINISH_OFF;
14450                 }
14451
14452                 col_index = 1;
14453
14454                 for (i = 0; i < count; i++)
14455                         _get_table_field_data_int(result, &(mail_list[i]), col_index++);
14456
14457                 memset(sql_query_string, 0x00, QUERY_SIZE);
14458                 if (reset) {
14459                         switch (rule->action_type) {
14460                         case EMAIL_FILTER_MOVE:
14461                                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = 0 ");
14462                                 break;
14463                         case EMAIL_FILTER_BLOCK:
14464                         default:
14465                                 EM_DEBUG_LOG("Not support : action_type[%d]", rule->action_type);
14466                                 ret = true;
14467                                 goto FINISH_OFF;
14468                         }
14469                 } else {
14470                         switch (rule->action_type) {
14471                         case EMAIL_FILTER_MOVE:
14472                                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = %d ", tag_id);
14473                                 break;
14474                         case EMAIL_FILTER_BLOCK:
14475                                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET mailbox_id = %d, mailbox_type = %d ", dest_mailbox_id, dest_mailbox_type);
14476                                 break;
14477                         default:
14478                                 EM_DEBUG_LOG("Not support");
14479                                 ret = true;
14480                                 goto FINISH_OFF;
14481                         }
14482                 }
14483                 /* prevent 34361 */
14484                 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14485                         strcat(sql_query_string, where_pararaph);
14486
14487                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14488                 if (error != EMAIL_ERROR_NONE) {
14489                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14490                                 goto FINISH_OFF;
14491                 }
14492
14493 #ifdef __FEATURE_BODY_SEARCH__
14494                 /* Updating mail_text_tbl */
14495                 if (rule->action_type == EMAIL_FILTER_BLOCK) {
14496                         memset(sql_query_string, 0x00, QUERY_SIZE);
14497                         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_text_tbl SET mailbox_id = %d ", dest_mailbox_id);
14498                         if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14499                                 strcat(sql_query_string, where_pararaph);
14500
14501                         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14502                         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14503                         if (error != EMAIL_ERROR_NONE) {
14504                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14505                                 goto FINISH_OFF;
14506                         }
14507                 }
14508 #endif
14509         }
14510
14511         ret = true;
14512
14513 FINISH_OFF:
14514
14515         if (ret) {
14516                 if (filtered_mail_id_list)
14517                         *filtered_mail_id_list = mail_list;
14518
14519                 if (count_of_mails)
14520                         *count_of_mails = count;
14521         } else
14522                 EM_SAFE_FREE(mail_list);
14523
14524         sqlite3_free_table(result);
14525         result = NULL;
14526
14527
14528         EM_SAFE_FREE(where_pararaph);
14529
14530         if (err_code != NULL)
14531                 *err_code = error;
14532
14533         EM_DEBUG_FUNC_END("ret [%d]", ret);
14534         return ret;
14535 }
14536
14537 #define EMAIL_SLOT_UNIT 25
14538 INTERNAL_FUNC int emstorage_set_mail_slot_size(char *multi_user_name, int account_id, int mailbox_id, int new_slot_size, int transaction, int *err_code)
14539 {
14540         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%p] new_slot_size[%d], err_code[%p]", account_id, mailbox_id, new_slot_size, err_code);
14541         int ret = false, err = EMAIL_ERROR_NONE;
14542         int where_pararaph_length = 0;
14543         char *where_pararaph = NULL;
14544         char sql_query_string[QUERY_SIZE] = {0, };
14545         int and = 0;
14546         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14547
14548         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
14549
14550         if (new_slot_size > 0)
14551                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = %d ", new_slot_size);
14552         else if (new_slot_size == 0)
14553                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", EMAIL_SLOT_UNIT);
14554         else
14555                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", new_slot_size * -1);
14556
14557
14558         if (mailbox_id)
14559                 where_pararaph_length = 80;
14560         else
14561                 where_pararaph_length = 50;
14562
14563         if (new_slot_size == 0)
14564                 where_pararaph_length += 70;
14565
14566         where_pararaph = malloc(sizeof(char) * where_pararaph_length);
14567         if (where_pararaph == NULL) {
14568                 EM_DEBUG_EXCEPTION("Memory allocation failed for where_pararaph");
14569                 err = EMAIL_ERROR_OUT_OF_MEMORY;
14570                 goto FINISH_OFF;
14571         }
14572         memset(where_pararaph, 0x00, where_pararaph_length);
14573
14574         if (account_id > ALL_ACCOUNT) {
14575                 and = 1;
14576                 if (mailbox_id)
14577                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_id = %d ", mailbox_id);
14578                 else
14579                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d ", account_id);
14580         }
14581
14582         if (new_slot_size == 0)
14583                 SNPRINTF(where_pararaph + EM_SAFE_STRLEN(where_pararaph), where_pararaph_length - EM_SAFE_STRLEN(where_pararaph), " %s total_mail_count_on_server > mail_slot_size ", (and ? "AND" : "WHERE"));
14584
14585         if (strlen(sql_query_string) + EM_SAFE_STRLEN(where_pararaph) < QUERY_SIZE) /* prevent 34363 */
14586                 strcat(sql_query_string, where_pararaph);
14587         else {
14588                 EM_DEBUG_EXCEPTION("Query buffer overflowed !!!");
14589                 err = EMAIL_ERROR_OUT_OF_MEMORY;
14590                 goto FINISH_OFF;
14591         }
14592
14593         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14594         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14595         if (err != EMAIL_ERROR_NONE) {
14596                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
14597                         goto FINISH_OFF;
14598         }
14599
14600         ret = true;
14601
14602 FINISH_OFF:
14603         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
14604
14605         EM_SAFE_FREE(where_pararaph);
14606
14607         if (err_code != NULL)
14608                 *err_code = err;
14609
14610         EM_DEBUG_FUNC_END("ret [%d]", ret);
14611         return ret;
14612 }
14613
14614 INTERNAL_FUNC int emstorage_add_meeting_request(char *multi_user_name, int account_id, int input_mailbox_id, email_meeting_request_t* meeting_req, int transaction, int *err_code)
14615 {
14616         EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], meeting_req[%p], transaction[%d], err_code[%p]", account_id, input_mailbox_id, meeting_req, transaction, err_code);
14617
14618         if (!meeting_req || meeting_req->mail_id <= 0) {
14619                 if (meeting_req)
14620                 EM_DEBUG_EXCEPTION("mail_id[%]d", meeting_req->mail_id);
14621
14622                 if (err_code != NULL)
14623                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14624
14625                 return false;
14626         }
14627
14628         int rc = -1;
14629         int ret = false;
14630         int error = EMAIL_ERROR_NONE;
14631         DB_STMT hStmt = NULL;
14632         char sql_query_string[QUERY_SIZE] = {0, };
14633         int col_index = 0;
14634         time_t temp_unix_time = 0;
14635
14636         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14637         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14638
14639         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14640                 "INSERT INTO mail_meeting_tbl VALUES "
14641                 "(?"            /*  mail_id */
14642                 ", ?"           /*  account_id */
14643                 ", ?"           /*  mailbox_id */
14644                 ", ?"           /*  meeting_response */
14645                 ", ?"           /*  start_time */
14646                 ", ?"           /*  end_time */
14647                 ", ?"           /*  location */
14648                 ", ?"           /*  global_object_id */
14649                 ", ?"           /*  offset */
14650                 ", ?"           /*  standard_name */
14651                 ", ?"           /*  standard_time_start_date */
14652                 ", ?"           /*  standard_biad */
14653                 ", ?"           /*  daylight_name */
14654                 ", ?"           /*  daylight_time_start_date */
14655                 ", ?"           /*  daylight_bias */
14656                 ")");
14657
14658         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14659         if (rc != SQLITE_OK) {
14660                 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
14661                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
14662
14663                 error = EMAIL_ERROR_DB_FAILURE;
14664                 goto FINISH_OFF;
14665         }
14666
14667         col_index = 0;
14668         /*
14669         EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14670         EM_DEBUG_LOG_SEC(">>>>> account_id[%d]", account_id);
14671         EM_DEBUG_LOG_SEC(">>>>> mailbox_name[%s]", mailbox_name);
14672         EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14673         EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14674         EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14675         EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14676         EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14677         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14678         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14679         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14680         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14681         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14682         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14683         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14684         */
14685         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->mail_id);
14686         _bind_stmt_field_data_int(hStmt, col_index++, account_id);
14687         _bind_stmt_field_data_int(hStmt, col_index++, input_mailbox_id);
14688         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14689
14690         temp_unix_time = timegm(&(meeting_req->start_time));
14691         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14692         temp_unix_time = timegm(&(meeting_req->end_time));
14693         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14694
14695         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->location, 0, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14696         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->global_object_id, 0, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14697
14698         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14699         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.standard_name, 0, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14700         temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14701         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14702         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14703
14704         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.daylight_name, 0, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14705         temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14706         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14707         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
14708
14709
14710         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14711         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
14712                 ("sqlite3_step fail:%d", rc));
14713         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14714                 ("sqlite3_step fail:%d", rc));
14715
14716         ret = true;
14717
14718 FINISH_OFF:
14719         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14720         if (hStmt != NULL) {
14721                 rc = sqlite3_finalize(hStmt);
14722                 if (rc != SQLITE_OK) {
14723                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14724                         error = EMAIL_ERROR_DB_FAILURE;
14725                 }
14726         }
14727
14728         if (err_code != NULL)
14729                 *err_code = error;
14730
14731         EM_DEBUG_FUNC_END("ret [%d]", ret);
14732         return ret;
14733 }
14734
14735 INTERNAL_FUNC int emstorage_query_meeting_request(char *multi_user_name, const char *conditional_clause, email_meeting_request_t **output_meeting_req, int *output_result_count, int transaction)
14736 {
14737         EM_DEBUG_FUNC_BEGIN("conditional_clause[%s] output_meeting_req[%p] output_result_count[%p] transaction[%d]", conditional_clause, output_meeting_req, output_result_count, transaction);
14738
14739         int i = 0;
14740         int col_index = 0;
14741         int rc;
14742         int count = 0;
14743         int dummy = 0;
14744         int err = EMAIL_ERROR_NONE;
14745         char **result = NULL;
14746         char sql_query_string[QUERY_SIZE] = {0, };
14747         email_meeting_request_t* p_temp_meeting_req = NULL;
14748         sqlite3 *local_db_handle = NULL;
14749         time_t temp_unix_time;
14750
14751         if (conditional_clause == NULL || output_meeting_req == NULL || output_result_count == NULL) {
14752                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14753                 err = EMAIL_ERROR_INVALID_PARAM;
14754                 goto FINISH_OFF;
14755         }
14756
14757         col_index = _field_count_of_table[CREATE_TABLE_MAIL_MEETING_TBL];
14758         EM_DEBUG_LOG("col_index [%d]", col_index);
14759
14760         local_db_handle = emstorage_get_db_connection(multi_user_name);
14761
14762         EMSTORAGE_START_READ_TRANSACTION(transaction);
14763
14764         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_meeting_tbl %s", conditional_clause);
14765
14766         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14767
14768         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14769         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
14770                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14771
14772         if (!count) {
14773                 EM_DEBUG_EXCEPTION("No meeting_request found...");
14774                 err = EMAIL_ERROR_DATA_NOT_FOUND;
14775                 goto FINISH_OFF;
14776         }
14777
14778         EM_DEBUG_LOG("There are [%d] meeting requests.", count);
14779         if (!(p_temp_meeting_req = (email_meeting_request_t*)em_malloc(sizeof(email_meeting_request_t) * count))) {
14780                 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
14781                 err = EMAIL_ERROR_OUT_OF_MEMORY;
14782                 goto FINISH_OFF;
14783         }
14784
14785         EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
14786
14787         for (i = 0; i < count; i++) {
14788                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].mail_id), col_index++);
14789                 _get_table_field_data_int(result, &dummy, col_index++); /* account_id. but why should this field exist in DB table? */
14790                 _get_table_field_data_int(result, &dummy, col_index++); /* mailbox_id */
14791                 _get_table_field_data_int(result, (int*)&(p_temp_meeting_req[i].meeting_response), col_index++);
14792                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* start time */
14793                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].start_time));
14794                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* end time */
14795                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].end_time));
14796                 _get_table_field_data_string(result, &p_temp_meeting_req[i].location, 1, col_index++);
14797                 _get_table_field_data_string(result, &p_temp_meeting_req[i].global_object_id, 1, col_index++);
14798                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.offset_from_GMT), col_index++);
14799                 _get_table_field_data_string_without_allocation(result, p_temp_meeting_req[i].time_zone.standard_name, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL, 1, col_index++);
14800                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14801                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.standard_time_start_date));
14802                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.standard_bias), col_index++);
14803                 _get_table_field_data_string_without_allocation(result, p_temp_meeting_req[i].time_zone.daylight_name, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL, 1, col_index++);
14804                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14805                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.daylight_time_start_date));
14806                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.daylight_bias), col_index++);
14807         }
14808
14809
14810 FINISH_OFF:
14811         if (result)
14812                 sqlite3_free_table(result);
14813
14814         if (err == EMAIL_ERROR_NONE) {
14815                 if (p_temp_meeting_req)
14816                         *output_meeting_req = p_temp_meeting_req;
14817                 *output_result_count = count;
14818         } else
14819                 EM_SAFE_FREE(p_temp_meeting_req);
14820
14821         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14822
14823 //      sqlite3_db_release_memory(local_db_handle);
14824
14825         EM_DEBUG_FUNC_END("err [%d]", err);
14826         return err;
14827 }
14828
14829 INTERNAL_FUNC int emstorage_get_meeting_request(char *multi_user_name, int mail_id, email_meeting_request_t ** meeting_req, int transaction, int *err_code)
14830 {
14831         EM_DEBUG_FUNC_BEGIN();
14832
14833         int count = 0;
14834         int ret = false;
14835         int error = EMAIL_ERROR_NONE;
14836         char conditional_clause[QUERY_SIZE] = {0, };
14837
14838         EM_IF_NULL_RETURN_VALUE(meeting_req, false);
14839
14840
14841         SNPRINTF(conditional_clause, QUERY_SIZE, " WHERE mail_id = %d", mail_id);
14842         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
14843
14844         if ((error = emstorage_query_meeting_request(multi_user_name, conditional_clause, meeting_req, &count, transaction)) != EMAIL_ERROR_NONE) {
14845                 EM_DEBUG_EXCEPTION("emstorage_query_meeting_request failed. [%d]", error);
14846                 goto FINISH_OFF;
14847         }
14848
14849         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->mail_id[%d]", (*meeting_req)->mail_id);
14850         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->meeting_response[%d]", (*meeting_req)->meeting_response);
14851         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->start_time[%s]", asctime(&((*meeting_req)->start_time)));
14852         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->end_time[%s]", asctime(&((*meeting_req)->end_time)));
14853         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->location[%s]", (*meeting_req)->location);
14854         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->global_object_id[%s]", (*meeting_req)->global_object_id);
14855         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.offset_from_GMT[%d]", (*meeting_req)->time_zone.offset_from_GMT);
14856         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_name[%s]", (*meeting_req)->time_zone.standard_name);
14857         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.standard_time_start_date)));
14858         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_bias[%d]", (*meeting_req)->time_zone.standard_bias);
14859         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_name[%s]", (*meeting_req)->time_zone.daylight_name);
14860         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.daylight_time_start_date)));
14861         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_bias[%d]", (*meeting_req)->time_zone.daylight_bias);
14862         ret = true;
14863
14864 FINISH_OFF:
14865
14866         if (err_code != NULL)
14867                 *err_code = error;
14868
14869         EM_DEBUG_FUNC_END("ret [%d]", ret);
14870         return ret;
14871 }
14872
14873 INTERNAL_FUNC int emstorage_update_meeting_request(char *multi_user_name, email_meeting_request_t* meeting_req, int transaction, int *err_code)
14874 {
14875         EM_DEBUG_FUNC_BEGIN("meeting_req[%p], transaction[%d], err_code[%p]", meeting_req, transaction, err_code);
14876
14877         int ret = false;
14878         int error = EMAIL_ERROR_NONE;
14879         int rc;
14880         DB_STMT hStmt = NULL;
14881         char sql_query_string[QUERY_SIZE] = {0, };
14882         time_t temp_unix_time = 0;
14883
14884         if (!meeting_req) {
14885                 EM_DEBUG_EXCEPTION("Invalid Parameter!");
14886                 if (err_code != NULL)
14887                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14888                 return false;
14889         }
14890
14891         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14892         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14893
14894         memset(sql_query_string, 0x00, sizeof(sql_query_string));
14895         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14896                 "UPDATE mail_meeting_tbl "
14897                 "SET "
14898                 /* "  account_id = ?, "         //  not update here, this can be changed when move or copy */
14899                 /* "  mailbox_name = ?, "               //  not update here, this can be changed when move or copy */
14900                 "  meeting_response = ?, "
14901                 "  start_time = ?, "
14902                 "  end_time = ?, "
14903                 "  location = ?, "
14904                 "  global_object_id = ?, "
14905                 "  offset = ?, "
14906                 "  standard_name = ?, "
14907                 "  standard_time_start_date = ?, "
14908                 "  standard_bias = ?, "
14909                 "  daylight_name = ?, "
14910                 "  daylight_time_start_date = ?, "
14911                 "  daylight_bias = ? "
14912                 "WHERE mail_id = %d",
14913                 meeting_req->mail_id);
14914
14915         EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
14916
14917         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14918         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14919                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14920 /*
14921         EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14922         EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14923         EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14924         EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14925         EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14926         EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14927         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14928         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14929         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14930         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14931         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14932         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14933         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14934 */
14935         int col_index = 0;
14936
14937         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14938         temp_unix_time = timegm(&(meeting_req->start_time));
14939         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14940         temp_unix_time = timegm(&(meeting_req->end_time));
14941         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14942         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->location, 1, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14943         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->global_object_id, 1, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14944         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14945         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.standard_name, 1, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14946         temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14947         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14948         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14949         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.daylight_name, 1, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14950         temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14951         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14952         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
14953
14954
14955         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14956         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14957                 ("sqlite3_step fail:%d", rc));
14958         ret = true;
14959
14960
14961 FINISH_OFF:
14962         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14963
14964         if (hStmt != NULL) {
14965                 rc = sqlite3_finalize(hStmt);
14966                 if (rc != SQLITE_OK) {
14967                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14968                         error = EMAIL_ERROR_DB_FAILURE;
14969                 }
14970         }
14971
14972         if (err_code != NULL)
14973                 *err_code = error;
14974
14975         EM_DEBUG_FUNC_END("ret [%d]", ret);
14976         return ret;
14977 }
14978
14979 INTERNAL_FUNC int emstorage_delete_meeting_request(char *multi_user_name, int account_id, int mail_id, int input_mailbox_id, int transaction, int *err_code)
14980 {
14981         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], input_mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mail_id, input_mailbox_id, transaction, err_code);
14982
14983         if (account_id < ALL_ACCOUNT || mail_id < 0) {
14984                 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d]", account_id, mail_id);
14985
14986                 if (err_code != NULL)
14987                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14988                 return false;
14989         }
14990
14991         int ret = false;
14992         int error = EMAIL_ERROR_NONE;
14993         int and = false;
14994         char sql_query_string[QUERY_SIZE] = {0, };
14995
14996         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14997         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14998
14999         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_meeting_tbl ");
15000
15001         if (account_id != ALL_ACCOUNT) {                /*  NOT '0' means a specific account. '0' means all account */
15002                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE account_id = %d", account_id);
15003                 and = true;
15004         }
15005         if (mail_id > 0) {
15006                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s mail_id = %d", (and ? "AND" : "WHERE"), mail_id);
15007                 and = true;
15008         }
15009         if (input_mailbox_id > 0) {             /*  0 means all mailbox_id */
15010                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s mailbox_id = '%d'",  (and ? "AND" : "WHERE"), input_mailbox_id);
15011         }
15012
15013         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15014         if (error != EMAIL_ERROR_NONE) {
15015                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
15016                         goto FINISH_OFF;
15017         }
15018
15019         ret = true;
15020
15021 FINISH_OFF:
15022         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15023
15024         if (err_code)
15025                 *err_code = error;
15026
15027         EM_DEBUG_FUNC_END("ret [%d]", ret);
15028         return ret;
15029 }
15030
15031
15032 INTERNAL_FUNC void emstorage_free_meeting_request(email_meeting_request_t *meeting_req)
15033 {
15034         EM_DEBUG_FUNC_BEGIN();
15035
15036         if (!meeting_req) return;
15037
15038         EM_SAFE_FREE(meeting_req->location);
15039         EM_SAFE_FREE(meeting_req->global_object_id);
15040
15041         EM_DEBUG_FUNC_END();
15042 }
15043
15044 INTERNAL_FUNC int emstorage_get_overflowed_mail_id_list(char *multi_user_name, int account_id, int input_mailbox_id, int mail_slot_size, int **mail_id_list, int *mail_id_count, int transaction, int *err_code)
15045 {
15046         EM_DEBUG_FUNC_BEGIN("account_id [%d], input_mailbox_id [%d], mail_slot_size [%d], mail_id_list [%p], mail_id_count [%p], transaction [%d], err_code [%p]", account_id, input_mailbox_id, mail_slot_size, mail_id_list, mail_id_count, transaction, err_code);
15047         EM_PROFILE_BEGIN(profile_emstorage_get_overflowed_mail_id_list);
15048         char sql_query_string[QUERY_SIZE] = {0, };
15049         char **result = NULL;
15050         int rc = -1, ret = false;
15051         int error = EMAIL_ERROR_NONE;
15052         int counter = 0, col_index = 0;
15053         int result_mail_id_count = 0;
15054         int *result_mail_id_list = NULL;
15055
15056         if (input_mailbox_id <= 0 || !mail_id_list || !mail_id_count || account_id < 1) {
15057                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15058                 if (err_code)
15059                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15060                 return false;
15061         }
15062
15063         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl WHERE account_id = %d AND mailbox_id = %d ORDER BY date_time DESC LIMIT %d, 10000", account_id, input_mailbox_id, mail_slot_size);
15064
15065         EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
15066
15067         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15068         EMSTORAGE_START_READ_TRANSACTION(transaction);
15069
15070         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_mail_id_count, 0, NULL), rc);
15071         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15072                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15073
15074         if (!result_mail_id_count) {
15075                 EM_DEBUG_LOG("No mail found...");
15076                 ret = false;
15077                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
15078                 goto FINISH_OFF;
15079         }
15080
15081         EM_DEBUG_LOG("There are [%d] overflowed mails in mailbox_id [%d]", result_mail_id_count, input_mailbox_id);
15082
15083         if (!(result_mail_id_list = (int *)malloc(sizeof(int) * result_mail_id_count))) {
15084                 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
15085                 error = EMAIL_ERROR_OUT_OF_MEMORY;
15086                 sqlite3_free_table(result);
15087                 goto FINISH_OFF;
15088         }
15089
15090         memset(result_mail_id_list, 0x00, sizeof(int) * result_mail_id_count);
15091
15092         col_index = 1;
15093
15094         for (counter = 0; counter < result_mail_id_count; counter++)
15095                 _get_table_field_data_int(result, result_mail_id_list + counter, col_index++);
15096
15097         ret = true;
15098
15099 FINISH_OFF:
15100         EM_DEBUG_LOG("finish off [%d]", ret);
15101
15102         if (result)
15103                 sqlite3_free_table(result);
15104
15105         if (ret == true) {
15106                 *mail_id_list = result_mail_id_list;
15107                 *mail_id_count = result_mail_id_count;
15108         } else
15109                 EM_SAFE_FREE(result_mail_id_list);
15110
15111         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15112
15113         if (err_code != NULL)
15114                 *err_code = error;
15115
15116         EM_PROFILE_END(profile_emstorage_get_overflowed_mail_id_list);
15117         EM_DEBUG_FUNC_END("ret [%d]", ret);
15118         return ret;
15119 }
15120
15121 INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(char *multi_user_name, int mail_id, int *thread_id, int *err_code)
15122 {
15123         EM_DEBUG_FUNC_BEGIN("mail_id[%d], thread_id[%p], err_code[%p]", mail_id, thread_id, err_code);
15124
15125         int rc = -1, ret = false;
15126         int err = EMAIL_ERROR_NONE;
15127         char sql_query_string[QUERY_SIZE] = {0, };
15128         char **result;
15129         int result_count = 0;
15130
15131         if (mail_id == 0 || thread_id == NULL) {
15132                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15133                 if (err_code)
15134                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15135                 return false;
15136         }
15137
15138         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15139
15140         memset(sql_query_string, 0, QUERY_SIZE);
15141         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT thread_id FROM mail_tbl WHERE mail_id = %d", mail_id);
15142
15143         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15144         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15145         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15146                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15147
15148         if (!result_count) {
15149                 EM_DEBUG_LOG("No mail found...");
15150                 ret = false;
15151                 err = EMAIL_ERROR_MAIL_NOT_FOUND;
15152                 /* sqlite3_free_table(result); */
15153                 goto FINISH_OFF;
15154         }
15155
15156         _get_table_field_data_int(result, thread_id, 1);
15157
15158         sqlite3_free_table(result);
15159
15160         ret = true;
15161
15162 FINISH_OFF:
15163
15164         if (err_code != NULL)
15165                 *err_code = err;
15166
15167         EM_DEBUG_FUNC_END("ret [%d]", ret);
15168         return ret;
15169 }
15170
15171 INTERNAL_FUNC int emstorage_update_latest_thread_mail(char *multi_user_name,
15172                                                                                                                 int account_id,
15173                                                                                                                 int mailbox_id,
15174                                                                                                                 int mailbox_type,
15175                                                                                                                 int thread_id,
15176                                                                                                                 int *updated_thread_id,
15177                                                                                                                 int latest_mail_id,
15178                                                                                                                 int thread_item_count,
15179                                                                                                                 int noti_type,
15180                                                                                                                 int transaction,
15181                                                                                                                 int *err_code)
15182 {
15183         EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id [%d], thread_id[%d], updated_thread_id[%p], "
15184                                                 "latest_mail_id [%d], thread_item_count[%d], err_code[%p]",
15185                                                 account_id, mailbox_id, thread_id, updated_thread_id,
15186                                                 latest_mail_id, thread_item_count, err_code);
15187
15188         int rc = -1, ret = false;
15189         int err = EMAIL_ERROR_NONE;
15190         char sql_query_string[QUERY_SIZE] = {0, };
15191         char **result = NULL;
15192         int result_count = 0;
15193
15194         if (thread_id == 0) {
15195                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15196                 if (err_code)
15197                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15198                 return false;
15199         }
15200
15201         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15202
15203         if (thread_item_count == 0 && latest_mail_id == 0) {
15204                 memset(sql_query_string, 0, QUERY_SIZE);
15205                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id, count(*) FROM (SELECT account_id, mail_id, thread_id, mailbox_id FROM mail_tbl ORDER BY date_time) WHERE account_id = %d AND thread_id = %d AND mailbox_id = %d", account_id, thread_id, mailbox_id);
15206
15207                 /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15208                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15209                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15210                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15211                 EM_DEBUG_LOG("result_count[%d]", result_count);
15212                 if (result_count == 0) {
15213                         EM_DEBUG_LOG("No mail found...");
15214                         ret = false;
15215                         if (err_code)
15216                                 *err_code =  EMAIL_ERROR_MAIL_NOT_FOUND;
15217                         sqlite3_free_table(result);
15218                         return false;
15219                 }
15220
15221                 _get_table_field_data_int(result, &latest_mail_id, 2);
15222                 _get_table_field_data_int(result, &thread_item_count, 3);
15223
15224                 EM_DEBUG_LOG("latest_mail_id[%d]", latest_mail_id);
15225                 EM_DEBUG_LOG("thread_item_count[%d]", thread_item_count);
15226
15227                 sqlite3_free_table(result);
15228         }
15229
15230         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15231
15232         if (thread_item_count < 0) {
15233                 memset(sql_query_string, 0, QUERY_SIZE);
15234                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = 0 WHERE account_id = %d AND thread_id = %d", account_id, thread_id);
15235                 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15236                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15237                 if (err != EMAIL_ERROR_NONE) {
15238                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15239                                 goto FINISH_OFF;
15240                 }
15241         } else if (thread_id != latest_mail_id) {
15242                 /* Initialize the thread id */
15243                 memset(sql_query_string, 0, QUERY_SIZE);
15244                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = 0, thread_id = %d WHERE account_id = %d AND mailbox_id = %d AND thread_id = %d", latest_mail_id, account_id, mailbox_id, thread_id);
15245                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15246                 if (err != EMAIL_ERROR_NONE) {
15247                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15248                                 goto FINISH_OFF;
15249                 }
15250
15251                 /* update the thread item count */
15252                 memset(sql_query_string, 0, QUERY_SIZE);
15253                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = %d WHERE account_id = %d AND mail_id = %d ", thread_item_count, account_id, latest_mail_id);
15254                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15255                 if (err != EMAIL_ERROR_NONE) {
15256                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15257                                 goto FINISH_OFF;
15258                 }
15259         } else {
15260                 memset(sql_query_string, 0, QUERY_SIZE);
15261                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = %d WHERE account_id = %d AND mail_id = %d ", thread_item_count, account_id, latest_mail_id);
15262                 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15263                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15264                 if (err != EMAIL_ERROR_NONE) {
15265                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15266                                 goto FINISH_OFF;
15267                 }
15268         }
15269         ret = true;
15270
15271 FINISH_OFF:
15272         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15273
15274         if (thread_id != latest_mail_id) {
15275                 if (err == EMAIL_ERROR_NONE) {
15276                         EM_DEBUG_LOG("noti_type[%d]", noti_type);
15277
15278                         if (latest_mail_id > 0 && thread_id > 0 && noti_type > 0) {
15279                                 char mailbox_id_str[25] = {0,};
15280                                 snprintf(mailbox_id_str, sizeof(mailbox_id_str), "%d", mailbox_id);
15281                                 if (!emcore_notify_storage_event(noti_type, thread_id, latest_mail_id, mailbox_id_str, account_id))
15282                                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventfailed [NOTI_THREAD_ID_CHANGED] >>>> ");
15283
15284                                 if (updated_thread_id) *updated_thread_id = latest_mail_id;
15285                         }
15286                 }
15287         } else if (thread_item_count >= 0) {
15288                 if (err == EMAIL_ERROR_NONE) {
15289                         char parameter_string[500] = {0,};
15290                         SNPRINTF(parameter_string, sizeof(parameter_string), "%s%c%d", "thread_item_count", 0x01, latest_mail_id);
15291                         if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT, parameter_string, thread_item_count))
15292                                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventfailed [NOTI_MAIL_FIELD_UPDATE] >>>> ");
15293                 }
15294         }
15295
15296         if (err_code != NULL)
15297                 *err_code = err;
15298
15299         EM_DEBUG_FUNC_END("ret [%d]", ret);
15300         return ret;
15301 }
15302
15303 INTERNAL_FUNC int emstorage_update_thread_id_of_mail(char *multi_user_name, int account_id, int mailbox_id, int mail_id, int thread_id, int thread_item_count, int transaction, int *err_code)
15304 {
15305         EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id [%d], mail_id[%d], thread_id[%d], thread_item_count[%d], err_code[%p]", account_id, mailbox_id, mail_id, thread_id, thread_item_count, err_code);
15306
15307         int ret = false;
15308         int err = EMAIL_ERROR_NONE;
15309         char sql_query_string[QUERY_SIZE] = {0, };
15310
15311         if (thread_id == 0) {
15312                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15313                 if (err_code)
15314                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15315                 return false;
15316         }
15317
15318         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15319
15320         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15321
15322         memset(sql_query_string, 0, QUERY_SIZE);
15323         SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = %d, thread_id = %d WHERE account_id = %d AND mail_id = %d", thread_item_count, thread_id, account_id, mail_id);
15324         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15325         if (err != EMAIL_ERROR_NONE) {
15326                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15327                 goto FINISH_OFF;
15328         }
15329
15330         ret = true;
15331
15332 FINISH_OFF:
15333         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15334
15335         if (err_code != NULL)
15336                 *err_code = err;
15337
15338         EM_DEBUG_FUNC_END("ret [%d]", ret);
15339         return ret;
15340 }
15341
15342 #ifdef __FEATURE_LOCAL_ACTIVITY__
15343 /**
15344   *     emstorage_add_activity - Add Email Local activity during OFFLINE mode
15345   *
15346   */
15347 INTERNAL_FUNC int emstorage_add_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15348 {
15349         EM_DEBUG_FUNC_BEGIN();
15350
15351         EM_DEBUG_LOG(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15352
15353         int rc = -1, ret = false;
15354         int error = EMAIL_ERROR_NONE;
15355         DB_STMT hStmt = NULL;
15356         char sql_query_string[8192] = { 0x00, };
15357         int i = 0;
15358
15359         if (!local_activity) {
15360                 EM_DEBUG_EXCEPTION(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15361                 if (err_code != NULL)
15362                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15363                 return false;
15364         }
15365
15366         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15367
15368         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15369
15370         memset(sql_query_string, 0x00 , sizeof(sql_query_string));
15371         SNPRINTF(sql_query_string, sizeof(sql_query_string), "INSERT INTO mail_local_activity_tbl VALUES (?, ?, ?, ?, ?, ?, ?)");
15372
15373         EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ] ", local_activity->activity_id);
15374         EM_DEBUG_LOG(">>>>> MAIL ID [ %d ] ", local_activity->mail_id);
15375         EM_DEBUG_LOG(">>>>> ACCOUNT ID [ %d ] ", local_activity->account_id);
15376         EM_DEBUG_LOG(">>>>> ACTIVITY TYPE [ %d ] ", local_activity->activity_type);
15377         EM_DEBUG_LOG_SEC(">>>>> SERVER MAIL ID [ %s ] ", local_activity->server_mailid);
15378         EM_DEBUG_LOG(">>>>> SOURCE MAILBOX [ %s ] ", local_activity->src_mbox);
15379         EM_DEBUG_LOG(">>>>> DEST MAILBOX   [ %s ] ", local_activity->dest_mbox);
15380
15381         EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15382
15383
15384         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15385         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15386                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15387
15388         EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15389
15390         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
15391         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
15392         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
15393         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
15394         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->server_mailid, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
15395         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->src_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15396         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->dest_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15397
15398
15399         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15400
15401         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
15402                 ("sqlite3_step fail:%d", rc));
15403         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15404                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
15405
15406         ret = true;
15407
15408 FINISH_OFF:
15409         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15410
15411         if (hStmt != NULL) {
15412                 rc = sqlite3_finalize(hStmt);
15413                 if (rc != SQLITE_OK) {
15414                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15415                         error = EMAIL_ERROR_DB_FAILURE;
15416                 }
15417         }
15418
15419         if (err_code != NULL)
15420                 *err_code = error;
15421
15422         EM_DEBUG_FUNC_END("ret [%d]", ret);
15423         return ret;
15424 }
15425
15426 /**
15427   *     emstorage_get_activity - Get the Local activity Information
15428   *
15429   *
15430   */
15431 INTERNAL_FUNC int emstorage_get_activity(int account_id, int activityid, emstorage_activity_tbl_t** activity_list, int *select_num, int transaction, int *err_code)
15432 {
15433         EM_DEBUG_FUNC_BEGIN();
15434
15435         int i = 0, count = 0, rc = -1, ret = false;
15436         int error = EMAIL_ERROR_NONE;
15437         emstorage_activity_tbl_t *p_activity_tbl = NULL;
15438         char sql_query_string[1024] = {0x00, };
15439         char **result = NULL;
15440         int col_index ;
15441
15442         EM_IF_NULL_RETURN_VALUE(activity_list, false);
15443         EM_IF_NULL_RETURN_VALUE(select_num, false);
15444
15445
15446         if (!select_num || !activity_list || account_id <= 0 || activityid < 0) {
15447                 EM_DEBUG_LOG(" select_num[%p], activity_list[%p] account_id [%d] activityid [%d] ", select_num, activity_list, account_id, activityid);
15448                 if (err_code)
15449                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15450                 return false;
15451         }
15452
15453         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15454
15455         EMSTORAGE_START_READ_TRANSACTION(transaction);
15456
15457         memset(sql_query_string, 0x00, sizeof(sql_query_string));
15458
15459         if (activityid == ALL_ACTIVITIES) {
15460                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d order by activity_id", account_id);
15461         } else {
15462                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d AND activity_id = %d ", account_id, activityid);
15463         }
15464
15465         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
15466
15467
15468
15469         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15470         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15471                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15472
15473         col_index = 7;
15474
15475         if (!(p_activity_tbl = (emstorage_activity_tbl_t*)em_malloc(sizeof(emstorage_activity_tbl_t) * count))) {
15476                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
15477                 error = EMAIL_ERROR_OUT_OF_MEMORY;
15478                 goto FINISH_OFF;
15479         }
15480
15481
15482         for (i = 0; i < count; i++) {
15483                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15484                 if (result[col_index])
15485                         p_activity_tbl[i].activity_id = atoi(result[col_index++]);
15486
15487                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15488                 if (result[col_index])
15489                         p_activity_tbl[i].account_id = atoi(result[col_index++]);
15490
15491                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15492                 if (result[col_index])
15493                         p_activity_tbl[i].mail_id = atoi(result[col_index++]);
15494
15495                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15496                 if (result[col_index])
15497                         p_activity_tbl[i].activity_type = atoi(result[col_index++]);
15498
15499
15500                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15501                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15502                         p_activity_tbl[i].server_mailid = EM_SAFE_STRDUP(result[col_index++]);
15503                 else
15504                         col_index++;
15505
15506                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15507                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15508                         p_activity_tbl[i].src_mbox = EM_SAFE_STRDUP(result[col_index++]);
15509                 else
15510                         col_index++;
15511
15512                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15513                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15514                         p_activity_tbl[i].dest_mbox = EM_SAFE_STRDUP(result[col_index++]);
15515                 else
15516                         col_index++;
15517
15518         }
15519
15520         if (result)
15521                 sqlite3_free_table(result);
15522
15523         ret = true;
15524
15525 FINISH_OFF:
15526
15527         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15528
15529         if (ret == true) {
15530                 *activity_list = p_activity_tbl;
15531                 *select_num = count;
15532                 EM_DEBUG_LOG(">>>> COUNT : %d >> ", count);
15533         } else if (p_activity_tbl != NULL) {
15534                 emstorage_free_local_activity(&p_activity_tbl, count, NULL);
15535         }
15536
15537
15538         if (err_code != NULL)
15539                 *err_code = error;
15540
15541         EM_DEBUG_FUNC_END("ret [%d]", ret);
15542         return ret;
15543 }
15544
15545
15546 INTERNAL_FUNC int emstorage_get_next_activity_id(int *activity_id, int *err_code)
15547 {
15548
15549         EM_DEBUG_FUNC_BEGIN();
15550
15551         int ret = false;
15552         int err = EMAIL_ERROR_NONE;
15553         int rc = -1;
15554         char *sql = NULL;
15555         char **result = NULL;
15556
15557         if (NULL == activity_id) {
15558                 EM_DEBUG_EXCEPTION(" activity_id[%p]", activity_id);
15559                 if (err_code)
15560                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15561                 return false;
15562         }
15563
15564         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15565
15566         /*  increase unique id */
15567
15568         sql = "SELECT max(rowid) FROM mail_local_activity_tbl;";
15569
15570         /*  rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); n EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc); */
15571         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15572                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
15573
15574         if (NULL == result[1])
15575                 rc = 1;
15576         else
15577                 rc = atoi(result[1])+1;
15578
15579         *activity_id = rc;
15580
15581         if (result)
15582                 sqlite3_free_table(result);
15583
15584         ret = true;
15585
15586         FINISH_OFF:
15587
15588         if (err_code)
15589                 *err_code = err;
15590
15591         EM_DEBUG_FUNC_END("ret [%d]", ret);
15592         return ret;
15593
15594 }
15595
15596 INTERNAL_FUNC int emstorage_get_activity_id_list(char *multi_user_name, int account_id, int ** activity_id_list, int *activity_id_count, int lowest_activity_type, int highest_activity_type, int transaction, int *err_code)
15597 {
15598
15599         EM_DEBUG_FUNC_BEGIN();
15600
15601         EM_DEBUG_LOG(" account_id[%d], activity_id_list[%p], activity_id_count[%p] err_code[%p]", account_id,  activity_id_list, activity_id_count, err_code);
15602
15603         if (account_id <= 0 || NULL == activity_id_list || NULL == activity_id_count || lowest_activity_type <= 0 || highest_activity_type <= 0) {
15604                 if (err_code != NULL)
15605                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15606                 return false;
15607         }
15608
15609         int ret = false;
15610         int error = EMAIL_ERROR_NONE;
15611         int i = 0, rc = -1, count = 0;
15612         char sql_query_string[1024] = {0x00, };
15613         int *activity_ids = NULL;
15614         int col_index = 0;
15615         char **result = NULL;
15616         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15617         EMSTORAGE_START_READ_TRANSACTION(transaction);
15618
15619         memset(sql_query_string, 0x00, sizeof(sql_query_string));
15620
15621         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct activity_id FROM mail_local_activity_tbl WHERE account_id = %d AND activity_type >= %d AND activity_type <= %d order by activity_id", account_id, lowest_activity_type, highest_activity_type);
15622
15623         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
15624
15625
15626         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15627         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15628                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15629
15630         col_index = 1;
15631
15632         EM_DEBUG_LOG(" Activity COUNT : %d ... ", count);
15633
15634         if (NULL == (activity_ids = (int *)em_malloc(sizeof(int) * count))) {
15635                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
15636                 error = EMAIL_ERROR_OUT_OF_MEMORY;
15637                 goto FINISH_OFF;
15638         }
15639
15640         for (i = 0; i < count; i++) {
15641                 activity_ids[i] = atoi(result[col_index]);
15642                 col_index++;
15643                 EM_DEBUG_LOG("activity_id %d", activity_ids[i]);
15644         }
15645
15646         ret = true;
15647
15648 FINISH_OFF:
15649
15650
15651         if (ret == true) {
15652                 *activity_id_count = count;
15653                 *activity_id_list = activity_ids;
15654         } else if (activity_ids != NULL) /* Prevent defect - 216566 */
15655                 EM_SAFE_FREE(activity_ids);
15656
15657
15658         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15659         if (err_code != NULL) {
15660                 *err_code = error;
15661         }
15662
15663         EM_DEBUG_FUNC_END("ret [%d]", ret);
15664         return ret;
15665 }
15666
15667 INTERNAL_FUNC int emstorage_free_activity_id_list(int *activity_id_list, int *error_code)
15668 {
15669         EM_DEBUG_FUNC_BEGIN();
15670
15671         int error = EMAIL_ERROR_NONE;
15672         int ret = false;
15673
15674         EM_DEBUG_LOG(" activity_id_list [%p]", activity_id_list);
15675
15676         if (NULL == activity_id_list) {
15677                 error = EMAIL_ERROR_INVALID_PARAM;
15678                 goto FINISH_OFF;
15679         } else {
15680                 EM_SAFE_FREE(activity_id_list);
15681         }
15682
15683
15684         ret = true;
15685
15686         FINISH_OFF:
15687
15688         if (NULL != error_code) {
15689                 *error_code = error;
15690         }
15691
15692         EM_DEBUG_FUNC_END("ret [%d]", ret);
15693         return ret;
15694 }
15695
15696 /**
15697  * emstorage_delete_local_activity - Deletes the Local acitivity Generated based on activity_type
15698  * or based on server mail id
15699  *
15700  */
15701 INTERNAL_FUNC int emstorage_delete_local_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15702 {
15703         EM_DEBUG_FUNC_BEGIN();
15704
15705
15706         EM_DEBUG_LOG(" local_activity[%p] ", local_activity);
15707
15708         if (!local_activity) {
15709                 EM_DEBUG_EXCEPTION(" local_activity[%p] ", local_activity);
15710                 if (err_code != NULL)
15711                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15712                 return false;
15713         }
15714
15715         int rc = -1, ret = false;                       /* Prevent_FIX  */
15716         int err = EMAIL_ERROR_NONE;
15717         int query_and = 0;
15718         int query_where = 0;
15719         char sql_query_string[8192] = { 0x00, };
15720         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15721         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15722
15723         memset(sql_query_string, 0x00, sizeof(sql_query_string));
15724
15725         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_local_activity_tbl ");
15726
15727         EM_DEBUG_LOG_SEC(">>> Query [ %s ] ", sql_query_string);
15728
15729         if (local_activity->account_id) {
15730                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15731                 " WHERE account_id = %d ", local_activity->account_id);
15732                 query_and = 1;
15733                 query_where = 1;
15734         }
15735
15736         if (local_activity->server_mailid) {
15737                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15738                 " %s %s server_mailid = '%s' ", query_where ? "" : "WHERE", query_and ? "AND" : "", local_activity->server_mailid);
15739                 query_and = 1;
15740                 query_where = 1;
15741         }
15742
15743
15744         if (local_activity->mail_id) {
15745                 EM_DEBUG_LOG(">>>> MAIL ID [ %d ] , ACTIVITY TYPE [%d ]", local_activity->mail_id, local_activity->activity_type);
15746
15747                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15748                 " %s %s mail_id = %d  ", query_where ? "" : "WHERE", query_and ? "AND" : "", local_activity->mail_id);
15749
15750                 query_and = 1;
15751                 query_where = 1;
15752
15753         }
15754
15755         if (local_activity->activity_type > 0) {
15756                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15757                 " %s %s activity_type = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "" , local_activity->activity_type);
15758         }
15759
15760         if (local_activity->activity_id > 0) {
15761                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15762                 " %s %s activity_id = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "" , local_activity->activity_id);
15763
15764         }
15765
15766         EM_DEBUG_LOG_SEC(">>>>> Query [ %s ] ", sql_query_string);
15767         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15768         if (error != EMAIL_ERROR_NONE) {
15769                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
15770                         goto FINISH_OFF;
15771         }
15772
15773         rc = sqlite3_changes(local_db_handle);
15774         if (rc == 0) {
15775                 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
15776                 err = EMAIL_ERROR_MAILBOX_NOT_FOUND;
15777         }
15778
15779         ret = true;
15780
15781 FINISH_OFF:
15782         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15783
15784         if (hStmt != NULL) {
15785                 rc = sqlite3_finalize(hStmt);
15786                 if (rc != SQLITE_OK) {
15787                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15788                         error = EMAIL_ERROR_DB_FAILURE;
15789                 }
15790         }
15791
15792         if (err_code != NULL)
15793                 *err_code = err;
15794         EM_DEBUG_FUNC_END("ret [%d]", ret);
15795         return ret;
15796 }
15797
15798 /**
15799 *       emstorage_free_local_activity - Free the Local Activity data
15800 */
15801 INTERNAL_FUNC int emstorage_free_local_activity(emstorage_activity_tbl_t **local_activity_list, int count, int *err_code)
15802 {
15803         EM_DEBUG_FUNC_BEGIN();
15804
15805         EM_DEBUG_LOG(" local_activity_list[%p], count[%d], err_code[%p]", local_activity_list, count, err_code);
15806
15807         int ret = false;
15808         int error = EMAIL_ERROR_INVALID_PARAM;
15809
15810         if (count > 0) {
15811                 if (!local_activity_list || !*local_activity_list) {
15812                         EM_DEBUG_EXCEPTION(" local_activity_list[%p], count[%d]", local_activity_list, count);
15813
15814                         error = EMAIL_ERROR_INVALID_PARAM;
15815                         goto FINISH_OFF;
15816                 }
15817
15818                 emstorage_activity_tbl_t* p = *local_activity_list;
15819                 int i = 0;
15820                 if (p) {
15821                         for (; i < count; i++) {
15822                                 EM_SAFE_FREE(p[i].dest_mbox);
15823                                 EM_SAFE_FREE(p[i].src_mbox);
15824                                 EM_SAFE_FREE(p[i].server_mailid);
15825                         }
15826
15827                         EM_SAFE_FREE(p);
15828                         *local_activity_list = NULL;
15829                 }
15830         }
15831
15832         ret = true;
15833
15834 FINISH_OFF:
15835
15836         if (err_code != NULL)
15837                 *err_code = error;
15838
15839         EM_DEBUG_FUNC_END("ret [%d]", ret);
15840         return ret;
15841
15842 }
15843 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
15844
15845
15846 static int _get_key_value_string_for_list_filter_rule(email_list_filter_rule_t *input_list_filter_rule, char **output_key_value_string)
15847 {
15848         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_key_value_string [%p]", input_list_filter_rule, output_key_value_string);
15849
15850         int  ret = EMAIL_ERROR_NONE;
15851         char key_value_string[QUERY_SIZE] = { 0, };
15852         char *temp_key_value_1 = NULL;
15853         char *temp_key_value_2 = NULL;
15854
15855         if (input_list_filter_rule == NULL || output_key_value_string == NULL) {
15856                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15857                 return EMAIL_ERROR_INVALID_PARAM;
15858         }
15859
15860         switch (input_list_filter_rule->target_attribute) {
15861         case EMAIL_MAIL_ATTRIBUTE_MAIL_ID:
15862         case EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID:
15863         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID:
15864         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE:
15865         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_STATUS:
15866         case EMAIL_MAIL_ATTRIBUTE_REFERENCE_MAIL_ID:
15867         case EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS:
15868         case EMAIL_MAIL_ATTRIBUTE_MAIL_SIZE:
15869         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN:
15870         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML:
15871         case EMAIL_MAIL_ATTRIBUTE_FILE_SIZE:
15872         case EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD:
15873         case EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD:
15874         case EMAIL_MAIL_ATTRIBUTE_FLAGS_FLAGGED_FIELD:
15875         case EMAIL_MAIL_ATTRIBUTE_FLAGS_ANSWERED_FIELD:
15876         case EMAIL_MAIL_ATTRIBUTE_FLAGS_RECENT_FIELD:
15877         case EMAIL_MAIL_ATTRIBUTE_FLAGS_DRAFT_FIELD:
15878         case EMAIL_MAIL_ATTRIBUTE_FLAGS_FORWARDED_FIELD:
15879         case EMAIL_MAIL_ATTRIBUTE_DRM_STATUS:
15880         case EMAIL_MAIL_ATTRIBUTE_PRIORITY:
15881         case EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS:
15882         case EMAIL_MAIL_ATTRIBUTE_LOCK_STATUS:
15883         case EMAIL_MAIL_ATTRIBUTE_REPORT_STATUS:
15884         case EMAIL_MAIL_ATTRIBUTE_ATTACHMENT_COUNT:
15885         case EMAIL_MAIL_ATTRIBUTE_INLINE_CONTENT_COUNT:
15886         case EMAIL_MAIL_ATTRIBUTE_THREAD_ID:
15887         case EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT:
15888         case EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS:
15889         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_CLASS:
15890         case EMAIL_MAIL_ATTRIBUTE_DIGEST_TYPE:
15891         case EMAIL_MAIL_ATTRIBUTE_SMIME_TYPE:
15892         case EMAIL_MAIL_ATTRIBUTE_REMAINING_RESEND_TIMES:
15893         case EMAIL_MAIL_ATTRIBUTE_TAG_ID:
15894         case EMAIL_MAIL_ATTRIBUTE_EAS_DATA_LENGTH_TYPE:
15895                 SNPRINTF(key_value_string, QUERY_SIZE, "%d", input_list_filter_rule->key_value.integer_type_value);
15896                 break;
15897
15898         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
15899         case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
15900         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
15901         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
15902         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
15903         case EMAIL_MAIL_ATTRIBUTE_FROM:
15904         case EMAIL_MAIL_ATTRIBUTE_TO:
15905         case EMAIL_MAIL_ATTRIBUTE_CC:
15906         case EMAIL_MAIL_ATTRIBUTE_BCC:
15907         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
15908                 if (input_list_filter_rule->key_value.string_type_value == NULL) {
15909                         EM_DEBUG_EXCEPTION("Invalid string_type_value [%p]", input_list_filter_rule->key_value.string_type_value);
15910                         ret = EMAIL_ERROR_INVALID_PARAM;
15911                         goto FINISH_OFF;
15912                 }
15913
15914                 temp_key_value_1 = input_list_filter_rule->key_value.string_type_value;
15915
15916                 temp_key_value_2 = em_replace_all_string(temp_key_value_1, "_", "\\_");
15917                 temp_key_value_1 = em_replace_all_string(temp_key_value_2, "%", "\\%");
15918
15919                 if (input_list_filter_rule->rule_type == EMAIL_LIST_FILTER_RULE_INCLUDE)
15920                         SNPRINTF(key_value_string, QUERY_SIZE, "\'%%%s%%\'", temp_key_value_1);
15921                 else
15922                         SNPRINTF(key_value_string, QUERY_SIZE, "\'%s\'", temp_key_value_1);
15923                 break;
15924
15925         case EMAIL_MAIL_ATTRIBUTE_DATE_TIME:
15926         case EMAIL_MAIL_ATTRIBUTE_SCHEDULED_SENDING_TIME:
15927         case EMAIL_MAIL_ATTRIBUTE_REPLIED_TIME:
15928         case EMAIL_MAIL_ATTRIBUTE_FORWARDED_TIME:
15929                 SNPRINTF(key_value_string, QUERY_SIZE, "%d", (int)input_list_filter_rule->key_value.datetime_type_value);
15930                 break;
15931
15932         default:
15933                 ret = EMAIL_ERROR_INVALID_PARAM;
15934                 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
15935                 break;
15936         }
15937
15938         if (ret == EMAIL_ERROR_NONE && EM_SAFE_STRLEN(key_value_string) > 0) {
15939                 *output_key_value_string = strdup(key_value_string);
15940         }
15941
15942 FINISH_OFF:
15943
15944         EM_SAFE_FREE(temp_key_value_1);
15945         EM_SAFE_FREE(temp_key_value_2);
15946
15947         EM_DEBUG_FUNC_END("ret [%d]", ret);
15948         return ret;
15949 }
15950
15951 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15952 static int _get_cert_password_file_name(int index, char *cert_password_file_name)
15953 {
15954         EM_DEBUG_FUNC_BEGIN("index : [%d]", index);
15955
15956         if (index <= 0 || !cert_password_file_name) {
15957                 EM_DEBUG_EXCEPTION("Invalid parameter");
15958                 return EMAIL_ERROR_INVALID_PARAM;
15959         }
15960
15961         sprintf(cert_password_file_name, ".email_cert_%d", index);
15962
15963         EM_DEBUG_FUNC_END();
15964         return EMAIL_ERROR_NONE;
15965 }
15966 #endif
15967
15968 static int _make_filter_rule_string(email_list_filter_rule_t *input_list_filter_rule, char **output_string)
15969 {
15970         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
15971
15972         int   ret = EMAIL_ERROR_NONE;
15973         int   is_alpha = 0;
15974         int   length_field_name = 0;
15975         int   length_value = 0;
15976         char  result_rule_string[QUERY_SIZE] = { 0 , };
15977         char *mod_field_name_string = NULL;
15978         char *mod_value_string = NULL;
15979         char *temp_field_name_string = NULL;
15980         char *temp_key_value_string = NULL;
15981
15982         if (input_list_filter_rule == NULL || output_string == NULL) {
15983                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15984                 return  EMAIL_ERROR_INVALID_PARAM;
15985         }
15986
15987         temp_field_name_string = emcore_get_mail_field_name_by_attribute_type(input_list_filter_rule->target_attribute);
15988
15989         if (temp_field_name_string == NULL) {
15990                 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
15991                 return EMAIL_ERROR_INVALID_PARAM;
15992         }
15993
15994         if (_get_key_value_string_for_list_filter_rule(input_list_filter_rule, &temp_key_value_string) != EMAIL_ERROR_NONE || temp_key_value_string == NULL) {
15995                 EM_DEBUG_EXCEPTION("_get_key_value_string_for_list_filter_rule failed");
15996                 return EMAIL_ERROR_INVALID_PARAM;
15997         }
15998
15999         length_field_name = EM_SAFE_STRLEN(temp_field_name_string);
16000         length_value      = EM_SAFE_STRLEN(temp_key_value_string);
16001
16002         switch (input_list_filter_rule->target_attribute) {
16003         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
16004         case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
16005         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
16006         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
16007         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
16008         case EMAIL_MAIL_ATTRIBUTE_FROM:
16009         case EMAIL_MAIL_ATTRIBUTE_TO:
16010         case EMAIL_MAIL_ATTRIBUTE_CC:
16011         case EMAIL_MAIL_ATTRIBUTE_BCC:
16012         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
16013                 is_alpha = 1;
16014                 break;
16015         default:
16016                 is_alpha = 0;
16017                 break;
16018         }
16019
16020         if (is_alpha == 1 && input_list_filter_rule->case_sensitivity == false) {
16021                 length_field_name += strlen("UPPER() ");
16022                 length_value      += strlen("UPPER() ");
16023                 mod_field_name_string = em_malloc(sizeof(char) * length_field_name);
16024                 if (mod_field_name_string == NULL) {
16025                         EM_DEBUG_EXCEPTION("em_mallocfailed");
16026                         EM_SAFE_FREE(temp_field_name_string);
16027                         ret = EMAIL_ERROR_OUT_OF_MEMORY;
16028                         goto FINISH_OFF;
16029                 }
16030
16031                 mod_value_string = em_malloc(sizeof(char) * length_value);
16032                 if (mod_value_string == NULL) {
16033                         EM_DEBUG_EXCEPTION("em_mallocfailed");
16034                         EM_SAFE_FREE(temp_field_name_string);
16035                         ret = EMAIL_ERROR_OUT_OF_MEMORY;
16036                         goto FINISH_OFF;
16037                 }
16038
16039                 SNPRINTF(mod_field_name_string, length_field_name, "UPPER(%s)", temp_field_name_string);
16040                 SNPRINTF(mod_value_string,      length_value, "UPPER(%s)", temp_key_value_string);
16041                 EM_SAFE_FREE(temp_key_value_string);
16042         } else {
16043                 mod_field_name_string = strdup(temp_field_name_string);
16044                 mod_value_string      = temp_key_value_string;
16045         }
16046
16047         switch (input_list_filter_rule->rule_type) {
16048         case EMAIL_LIST_FILTER_RULE_EQUAL:
16049                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = %s ", mod_field_name_string, mod_value_string);
16050                 break;
16051         case EMAIL_LIST_FILTER_RULE_NOT_EQUAL:
16052                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s != %s ", mod_field_name_string, mod_value_string);
16053                 break;
16054         case EMAIL_LIST_FILTER_RULE_LESS_THAN:
16055                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s < %s ", mod_field_name_string, mod_value_string);
16056                 break;
16057         case EMAIL_LIST_FILTER_RULE_GREATER_THAN:
16058                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s > %s ", mod_field_name_string, mod_value_string);
16059                 break;
16060         case EMAIL_LIST_FILTER_RULE_LESS_THAN_OR_EQUAL:
16061                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s <= %s ", mod_field_name_string, mod_value_string);
16062                 break;
16063         case EMAIL_LIST_FILTER_RULE_GREATER_THAN_OR_EQUAL:
16064                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s >= %s ", mod_field_name_string, mod_value_string);
16065                 break;
16066         case EMAIL_LIST_FILTER_RULE_INCLUDE:
16067                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s LIKE %s ", mod_field_name_string, mod_value_string);
16068                 break;
16069         case EMAIL_LIST_FILTER_RULE_IN:
16070                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s IN (%s) ", mod_field_name_string, mod_value_string);
16071                 break;
16072         case EMAIL_LIST_FILTER_RULE_NOT_IN:
16073                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s NOT IN (%s) ", mod_field_name_string, mod_value_string);
16074                 break;
16075         default:
16076                 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16077                 ret = EMAIL_ERROR_INVALID_PARAM;
16078                 goto FINISH_OFF;
16079         }
16080
16081         *output_string = strdup(result_rule_string);
16082
16083 FINISH_OFF:
16084         EM_SAFE_FREE(mod_field_name_string);
16085         EM_SAFE_FREE(mod_value_string);
16086
16087         EM_DEBUG_FUNC_END("ret [%d]", ret);
16088         return ret;
16089 }
16090
16091 static int _make_filter_attach_rule_string(char *multi_user_name, email_list_filter_rule_attach_t *input_list_filter_rule, char **output_string)
16092 {
16093         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16094
16095         char *field_name_string = NULL;
16096         char  key_value_string[QUERY_SIZE] = {0,};
16097         char  result_rule_string[QUERY_SIZE] = {0,};
16098         int rc = -1;
16099         int count = 0;
16100         int query_size = 0;
16101         int cur_query = 0;
16102         int col_index = 0;
16103         int error = EMAIL_ERROR_NONE;
16104         char **result = NULL;
16105         char sql_query_string[QUERY_SIZE] = {0,};
16106         char *sql_query_string2 = NULL;
16107         sqlite3 *local_db_handle = NULL;
16108         int *mail_ids = NULL;
16109
16110         if (input_list_filter_rule == NULL || output_string == NULL) {
16111                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16112                 return  EMAIL_ERROR_INVALID_PARAM;
16113         }
16114
16115         field_name_string = EM_SAFE_STRDUP("attachment_name");
16116         SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16117
16118         switch (input_list_filter_rule->rule_type) {
16119
16120         case EMAIL_LIST_FILTER_RULE_INCLUDE:
16121                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16122                 break;
16123
16124         case EMAIL_LIST_FILTER_RULE_MATCH:
16125                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16126                 break;
16127
16128         default:
16129                 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16130                 error = EMAIL_ERROR_INVALID_PARAM;
16131                 goto FINISH_OFF;
16132         }
16133
16134         local_db_handle = emstorage_get_db_connection(multi_user_name);
16135
16136         EMSTORAGE_START_READ_TRANSACTION(true);
16137         SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_attachment_tbl %s", result_rule_string);
16138
16139         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16140         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16141                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16142
16143         col_index = 1;
16144
16145         if (!count) {
16146                 EM_DEBUG_LOG("No mail found...");
16147                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
16148                 *output_string = strdup("mail_id IN () ");
16149                 goto FINISH_OFF;
16150         }
16151
16152         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
16153         int i = 0;
16154         if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16155                 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16156                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16157                 goto FINISH_OFF;
16158         }
16159
16160         for (i = 0; i < count; i++) {
16161                 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16162                 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16163         }
16164
16165         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >>", count);
16166         sqlite3_free_table(result);
16167         EMSTORAGE_FINISH_READ_TRANSACTION(true);
16168
16169 //      sqlite3_db_release_memory(local_db_handle);
16170
16171
16172         query_size = (10 * count) + strlen("mail_id IN ()  ");
16173
16174         sql_query_string2 = em_malloc(query_size);
16175         if (sql_query_string2 == NULL) {
16176                 EM_DEBUG_EXCEPTION("em_mallocfailed");
16177                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16178                 goto FINISH_OFF;
16179         }
16180
16181         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN (");
16182         for (i = 0; i < count-1; i++) {
16183                 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16184         }
16185         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d) ", mail_ids[count-1]);
16186
16187         *output_string = strdup(sql_query_string2);
16188 FINISH_OFF:
16189
16190         EM_SAFE_FREE(mail_ids); /* prevent */
16191         EM_SAFE_FREE(sql_query_string2);
16192         EM_SAFE_FREE(field_name_string);
16193         EM_DEBUG_FUNC_END("error [%d]", error);
16194         return error;
16195 }
16196
16197 static int _make_filter_fts_rule_string(char *multi_user_name, email_list_filter_rule_fts_t *input_list_filter_rule, char **output_string)
16198 {
16199         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16200         char *field_name_string = NULL;
16201         char key_value_string[QUERY_SIZE] = {0,};
16202         char  result_rule_string[QUERY_SIZE] = {0,};
16203         int rc = -1;
16204         int count = 0;
16205         int col_index = 0;
16206         int query_size = 0;
16207         int error = EMAIL_ERROR_NONE;
16208         char **result = NULL;
16209         char sql_query_string[QUERY_SIZE] = {0,};
16210         char *sql_query_string2 = NULL;
16211         sqlite3 *local_db_handle = NULL;
16212         int *mail_ids = NULL;
16213
16214         if (input_list_filter_rule == NULL || output_string == NULL) {
16215                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16216                 return  EMAIL_ERROR_INVALID_PARAM;
16217         }
16218
16219         field_name_string = EM_SAFE_STRDUP("body_text");
16220         SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16221
16222         switch (input_list_filter_rule->rule_type) {
16223
16224         case EMAIL_LIST_FILTER_RULE_INCLUDE:
16225                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16226                 break;
16227
16228         case EMAIL_LIST_FILTER_RULE_MATCH:
16229                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16230                 break;
16231
16232         default:
16233                 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16234                 error = EMAIL_ERROR_INVALID_PARAM;
16235                 goto FINISH_OFF;
16236         }
16237
16238         local_db_handle = emstorage_get_db_connection(multi_user_name);
16239
16240         EMSTORAGE_START_READ_TRANSACTION(true);
16241         SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_text_tbl %s", result_rule_string);
16242
16243         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16244         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16245                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16246
16247         col_index = 1;
16248
16249         if (!count) {
16250                 EM_DEBUG_LOG("No mail found...");
16251                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
16252                 *output_string = strdup("mail_id IN () ");
16253                 goto FINISH_OFF;
16254         }
16255
16256         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
16257         int i = 0;
16258
16259         if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16260                 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16261                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16262                 goto FINISH_OFF;
16263         }
16264
16265         for (i = 0; i < count; i++) {
16266                 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16267                 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16268         }
16269
16270         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >>", count);
16271         sqlite3_free_table(result);
16272         EMSTORAGE_FINISH_READ_TRANSACTION(true);
16273
16274 //      sqlite3_db_release_memory(local_db_handle);
16275
16276
16277         query_size = (10 * count) + strlen("mail_id IN ()  ");
16278         sql_query_string2 = em_malloc(query_size);
16279         if (sql_query_string2 == NULL) {
16280                 EM_DEBUG_EXCEPTION("em_mallocfailed");
16281                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16282                 goto FINISH_OFF;
16283         }
16284         int cur_query = 0;
16285         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN (");
16286         for (i = 0; i < count-1; i++) {
16287                 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16288         }
16289         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d) ", mail_ids[count-1]);
16290
16291         *output_string = strdup(sql_query_string2);
16292
16293 FINISH_OFF:
16294         EM_SAFE_FREE(mail_ids); /* prevent */
16295         EM_SAFE_FREE(sql_query_string2);
16296         EM_SAFE_FREE(field_name_string);
16297         EM_DEBUG_FUNC_END("error [%d]", error);
16298         return error;
16299 }
16300
16301 static int _make_order_rule_string(char *multi_user_name, email_list_sorting_rule_t *input_sorting_rule, char **output_string)
16302 {
16303         EM_DEBUG_FUNC_BEGIN("input_sorting_rule [%p], output_string [%p]", input_sorting_rule, output_string);
16304
16305         char  result_rule_string[QUERY_SIZE] = { 0 , };
16306         int   ret = EMAIL_ERROR_NONE;
16307
16308         emstorage_account_tbl_t *account_tbl_array = NULL;
16309         int count = 0;
16310         int i = 0;
16311         char *result_str = NULL;
16312         char *tmp_str1 = NULL;
16313         char *tmp_str2 = NULL;
16314         char query_per_account[QUERY_SIZE] = { 0 , };
16315
16316         if (input_sorting_rule->force_boolean_check) {
16317                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = 0 ", emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16318         } else
16319                 EM_SAFE_STRCPY(result_rule_string, emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16320
16321         switch (input_sorting_rule->sort_order) {
16322                 case EMAIL_SORT_ORDER_ASCEND:
16323                         EM_SAFE_STRCAT(result_rule_string, " ASC ");
16324                         break;
16325
16326                 case EMAIL_SORT_ORDER_DESCEND:
16327                         EM_SAFE_STRCAT(result_rule_string, " DESC ");
16328                         break;
16329
16330                 case EMAIL_SORT_ORDER_NOCASE_ASCEND:
16331                         EM_SAFE_STRCAT(result_rule_string, " COLLATE NOCASE ASC ");
16332                         break;
16333
16334                 case EMAIL_SORT_ORDER_NOCASE_DESCEND:
16335                         EM_SAFE_STRCAT(result_rule_string, " COLLATE NOCASE DESC ");
16336                         break;
16337
16338                 case EMAIL_SORT_ORDER_TO_CCBCC:
16339                         memset(result_rule_string, 0, QUERY_SIZE);
16340                         if (input_sorting_rule->key_value.string_type_value)
16341                                 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16342                                                 " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 ELSE 2 END ",
16343                                                 input_sorting_rule->key_value.string_type_value);
16344                         break;
16345
16346                 case EMAIL_SORT_ORDER_TO_CC_BCC:
16347                         memset(result_rule_string, 0, QUERY_SIZE);
16348                         if (input_sorting_rule->key_value.string_type_value)
16349                                 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16350                                                 " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 WHEN full_address_cc LIKE \'%%%q%%\' THEN 2 ELSE 3 END ",
16351                                                 input_sorting_rule->key_value.string_type_value, input_sorting_rule->key_value.string_type_value);
16352                         break;
16353
16354                 case EMAIL_SORT_ORDER_TO_CCBCC_ALL:
16355                         if (!emstorage_get_account_list(multi_user_name, &count, &account_tbl_array, true, false, NULL)) {
16356                                 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed");
16357                                 goto FINISH_OFF;
16358                         }
16359
16360                         if (!count) {
16361                                 EM_DEBUG_LOG("No account exist");
16362                                 ret = EMAIL_ERROR_INVALID_PARAM;
16363                                 goto FINISH_OFF;
16364                         }
16365
16366                         for (i = 0; i < count; i++) {
16367                                 if (i > 0 && result_str) {
16368                                         tmp_str2 = result_str;
16369                                         result_str = g_strconcat(tmp_str2, " OR ", NULL);
16370                                         EM_SAFE_FREE(tmp_str2);
16371                                 }
16372
16373                                 memset(query_per_account, 0, QUERY_SIZE);
16374                                 snprintf(query_per_account, QUERY_SIZE,
16375                                                 "(account_id = %d AND full_address_to LIKE \'%%%s%%\')",
16376                                                 account_tbl_array[i].account_id, account_tbl_array[i].user_email_address);
16377
16378                                 tmp_str1 = result_str;
16379                                 if (tmp_str1)
16380                                         result_str = g_strconcat(tmp_str1, query_per_account, NULL);
16381                                 else
16382                                         result_str = g_strdup(query_per_account);
16383                                 EM_SAFE_FREE(tmp_str1);
16384                         }
16385
16386                         snprintf(result_rule_string, QUERY_SIZE,
16387                                         " CASE WHEN %s THEN 1 ELSE 2 END ", result_str);
16388
16389                         EM_SAFE_FREE(result_str);
16390                         if (account_tbl_array)
16391                                 emstorage_free_account(&account_tbl_array, count, NULL);
16392                         break;
16393
16394                 case EMAIL_SORT_ORDER_LOCALIZE_ASCEND:
16395                         memset(result_rule_string, 0, QUERY_SIZE);
16396                         sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16397                                 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END ASC, %s COLLATE NOCASE ASC ",
16398                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16399                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16400                         break;
16401
16402                 case EMAIL_SORT_ORDER_LOCALIZE_DESCEND:
16403                         memset(result_rule_string, 0, QUERY_SIZE);
16404                         sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16405                                 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END DESC, %s COLLATE NOCASE DESC ",
16406                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16407                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16408                         break;
16409
16410                 default:
16411                         EM_DEBUG_EXCEPTION("Invalid sort_order [%d]", input_sorting_rule->sort_order);
16412                         ret = EMAIL_ERROR_INVALID_PARAM;
16413                         goto FINISH_OFF;
16414         }
16415
16416         *output_string = strdup(result_rule_string);
16417
16418 FINISH_OFF:
16419         EM_DEBUG_FUNC_END("ret [%d]", ret);
16420         return ret;
16421 }
16422
16423 INTERNAL_FUNC int emstorage_write_conditional_clause_for_getting_mail_list(char *multi_user_name, email_list_filter_t *input_filter_list, int input_filter_count, email_list_sorting_rule_t *input_sorting_rule_list, int input_sorting_rule_count, int input_start_index, int input_limit_count, char **output_conditional_clause)
16424 {
16425         EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d], input_sorting_rule_list[%p], input_sorting_rule_count [%d], input_start_index [%d], input_limit_count [%d], output_conditional_clause [%p]", input_filter_list, input_filter_count, input_sorting_rule_list, input_sorting_rule_count, input_start_index, input_limit_count, output_conditional_clause);
16426         int ret = EMAIL_ERROR_NONE;
16427         int i = 0;
16428         int string_offset = 0;
16429         int query_size = 0;
16430         int new_query_size = 0;
16431         char *conditional_clause_string = NULL;
16432         char *result_string_for_a_item = NULL;
16433
16434         if ((input_filter_count > 0 && !input_filter_list) || (input_sorting_rule_count > 0 && !input_sorting_rule_list) || output_conditional_clause == NULL) {
16435                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16436                 return EMAIL_ERROR_INVALID_PARAM;
16437         }
16438
16439         conditional_clause_string = em_malloc(QUERY_SIZE);
16440         if (conditional_clause_string == NULL) {
16441                 EM_DEBUG_EXCEPTION("Memory is full");
16442                 return EMAIL_ERROR_OUT_OF_MEMORY;
16443         }
16444
16445         if (input_filter_count > 0) {
16446                 query_size = QUERY_SIZE;
16447                 g_strlcpy(conditional_clause_string, " WHERE ", QUERY_SIZE);
16448
16449                 for (i = 0; i < input_filter_count; i++) {
16450                         switch (input_filter_list[i].list_filter_item_type) {
16451                         case EMAIL_LIST_FILTER_ITEM_RULE:
16452                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE", i);
16453                                 _make_filter_rule_string(&(input_filter_list[i].list_filter_item.rule), &result_string_for_a_item);
16454                                 break;
16455
16456                         case EMAIL_LIST_FILTER_ITEM_RULE_FTS:
16457                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_FTS", i);
16458                                 _make_filter_fts_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_fts), &result_string_for_a_item);
16459                                 break;
16460
16461                         case EMAIL_LIST_FILTER_ITEM_RULE_ATTACH:
16462                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_ATTACH", i);
16463                                 _make_filter_attach_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_attach), &result_string_for_a_item);
16464                                 break;
16465
16466                         case EMAIL_LIST_FILTER_ITEM_OPERATOR:
16467                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_OPERATOR", i);
16468                                 switch (input_filter_list[i].list_filter_item.operator_type) {
16469                                 case EMAIL_LIST_FILTER_OPERATOR_AND:
16470                                         result_string_for_a_item = strdup("AND ");
16471                                         break;
16472                                 case EMAIL_LIST_FILTER_OPERATOR_OR:
16473                                         result_string_for_a_item = strdup("OR ");
16474                                         break;
16475                                 case EMAIL_LIST_FILTER_OPERATOR_LEFT_PARENTHESIS:
16476                                         result_string_for_a_item = strdup(" (");
16477                                         break;
16478                                 case EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS:
16479                                         result_string_for_a_item = strdup(") ");
16480                                         break;
16481                                 }
16482                                 break;
16483
16484                         default:
16485                                 EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", input_filter_list[i].list_filter_item_type);
16486                                 ret = EMAIL_ERROR_INVALID_PARAM;
16487                                 goto FINISH_OFF;
16488                         }
16489
16490                         if (result_string_for_a_item == NULL) {
16491                                 EM_DEBUG_EXCEPTION("result_string_for_a_item is null");
16492                                 ret = EMAIL_ERROR_INVALID_PARAM;
16493                                 goto FINISH_OFF;
16494                         }
16495
16496                         if (strlen(conditional_clause_string) + EM_SAFE_STRLEN(result_string_for_a_item) >= query_size) { /* prevent 34364 */
16497                                 EM_DEBUG_LOG("QUERY is too long");
16498                                 new_query_size = EM_SAFE_STRLEN(result_string_for_a_item) + EM_SAFE_STRLEN(conditional_clause_string) + QUERY_SIZE;
16499                                 conditional_clause_string = realloc(conditional_clause_string, new_query_size);
16500                                 if (conditional_clause_string == NULL) {
16501                                         EM_DEBUG_EXCEPTION("realloc failed");
16502                                         ret = EMAIL_ERROR_OUT_OF_MEMORY;
16503                                         goto FINISH_OFF;
16504                                 }
16505
16506                                 query_size = new_query_size;
16507                         }
16508
16509                         strcat(conditional_clause_string, result_string_for_a_item);
16510                         EM_SAFE_FREE(result_string_for_a_item);
16511                 }
16512         }
16513
16514         if (input_sorting_rule_count > 0) {
16515                 strcat(conditional_clause_string, "ORDER BY ");
16516
16517                 for (i = 0; i < input_sorting_rule_count; i++) {
16518                         if ((ret = _make_order_rule_string(multi_user_name, &input_sorting_rule_list[i], &result_string_for_a_item)) != EMAIL_ERROR_NONE) {
16519                                 EM_DEBUG_EXCEPTION("_make_order_rule_string failed. [%d]", ret);
16520                                 goto FINISH_OFF;
16521                         }
16522                         if (i > 0)
16523                                 strcat(conditional_clause_string, ", ");
16524                         strcat(conditional_clause_string, result_string_for_a_item);
16525                         EM_SAFE_FREE(result_string_for_a_item);
16526                 }
16527         }
16528
16529         if (input_start_index != -1 && input_limit_count != -1) {
16530                 string_offset = strlen(conditional_clause_string);
16531                 SNPRINTF_OFFSET(conditional_clause_string, string_offset, query_size, " LIMIT %d, %d", input_start_index, input_limit_count);
16532         }
16533
16534         *output_conditional_clause = strdup(conditional_clause_string);
16535
16536 FINISH_OFF:
16537         EM_SAFE_FREE(result_string_for_a_item);
16538         EM_SAFE_FREE(conditional_clause_string);
16539
16540         EM_DEBUG_FUNC_END("ret [%d]", ret);
16541         return ret;
16542 }
16543
16544 INTERNAL_FUNC int emstorage_free_list_filter(email_list_filter_t **input_filter_list, int input_filter_count)
16545 {
16546         EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d]", input_filter_list, input_filter_count);
16547         int err = EMAIL_ERROR_NONE;
16548         int i = 0;
16549         email_list_filter_t *temp_filter_list = NULL;
16550
16551         EM_IF_NULL_RETURN_VALUE(input_filter_list, EMAIL_ERROR_INVALID_PARAM);
16552
16553         for (i = 0; i < input_filter_count; i++) {
16554                 temp_filter_list = (*input_filter_list) + i;
16555                 if (!temp_filter_list) {
16556                         continue;
16557                 }
16558
16559                 if (temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE) {
16560                         switch (temp_filter_list->list_filter_item.rule.target_attribute) {
16561                         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
16562                         case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
16563                         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
16564                         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
16565                         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
16566                         case EMAIL_MAIL_ATTRIBUTE_FROM:
16567                         case EMAIL_MAIL_ATTRIBUTE_TO:
16568                         case EMAIL_MAIL_ATTRIBUTE_CC:
16569                         case EMAIL_MAIL_ATTRIBUTE_BCC:
16570                         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN:
16571                         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML:
16572                         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
16573                                 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule.key_value.string_type_value);
16574                                 break;
16575                         default:
16576                                 break;
16577                         }
16578                 } else if (temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE_FTS && temp_filter_list->list_filter_item.rule_fts.target_attribute == EMAIL_MAIL_TEXT_ATTRIBUTE_FULL_TEXT) {
16579                                 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_fts.key_value.string_type_value);
16580                 } else if (temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE_ATTACH && temp_filter_list->list_filter_item.rule_attach.target_attribute == EMAIL_MAIL_ATTACH_ATTRIBUTE_ATTACHMENT_NAME) {
16581                                 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_attach.key_value.string_type_value);
16582                 }
16583         }
16584
16585         EM_SAFE_FREE(*input_filter_list);
16586
16587         EM_DEBUG_FUNC_END("err [%d]", err);
16588         return err;
16589 }
16590
16591 /* Tasks --------------------------------------------------------------------------*/
16592 INTERNAL_FUNC int emstorage_add_task(char *multi_user_name, email_task_type_t input_task_type, email_task_priority_t input_task_priority, char *input_task_parameter, int input_task_parameter_length, int input_transaction, int *output_task_id)
16593 {
16594         EM_DEBUG_FUNC_BEGIN("input_task_type [%d] input_task_priority[%p], input_task_parameter[%p] input_task_parameter_length[%d] input_transaction[%d] output_task_id[%p]", input_task_type, input_task_priority, input_task_parameter, input_task_parameter_length, input_transaction, output_task_id);
16595         int ret = 0;
16596         int i = 0;
16597         int task_id = 0;
16598         int err = EMAIL_ERROR_NONE;
16599         int rc = -1;
16600         DB_STMT hStmt = NULL;
16601         char sql_query_string[QUERY_SIZE] = {0, };
16602         sqlite3 *local_db_handle = NULL;
16603         char *sql = "SELECT max(rowid) FROM mail_task_tbl;";
16604         char **result = NULL;
16605
16606         if (input_task_parameter == NULL || output_task_id == NULL) {
16607                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16608                 return EMAIL_ERROR_INVALID_PARAM;
16609         }
16610
16611         local_db_handle = emstorage_get_db_connection(multi_user_name);
16612         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, err);
16613
16614         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
16615         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16616                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
16617
16618         if (NULL == result[1])
16619                 task_id = 1;
16620         else
16621                 task_id = atoi(result[1])+1;
16622
16623         *output_task_id = task_id;
16624
16625         sqlite3_free_table(result);
16626         result = NULL;
16627
16628         SNPRINTF(sql_query_string, sizeof(sql_query_string),
16629                 "INSERT INTO mail_task_tbl VALUES "
16630                 "(        "
16631                 "    ? "  /*   task_id */
16632                 "  , ? "  /*   task_type */
16633                 "  , ? "  /*   task_status */
16634                 "  , ? "  /*   task_priority */
16635                 "  , ? "  /*   task_parameter_length */
16636                 "  , ? "  /*   task_parameter */
16637                 "  , ? "  /*   date_time */
16638                 ") ");
16639
16640         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16641         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16642                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16643
16644         EM_DEBUG_LOG_SEC(">>>> SQL STMT [%s] ", sql_query_string);
16645
16646
16647         _bind_stmt_field_data_int(hStmt, i++, task_id);
16648         _bind_stmt_field_data_int(hStmt, i++, input_task_type);
16649         _bind_stmt_field_data_int(hStmt, i++, EMAIL_TASK_STATUS_WAIT);
16650         _bind_stmt_field_data_int(hStmt, i++, input_task_priority);
16651         _bind_stmt_field_data_int(hStmt, i++, input_task_parameter_length);
16652         _bind_stmt_field_data_blob(hStmt, i++, input_task_parameter, input_task_parameter_length);
16653         _bind_stmt_field_data_int(hStmt, i++, time(NULL));
16654
16655         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16656
16657         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {err = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
16658                 ("sqlite3_step fail:%d", rc));
16659         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16660                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
16661
16662         ret = (err == EMAIL_ERROR_NONE);
16663
16664 FINISH_OFF:
16665         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, err);
16666
16667         if (hStmt != NULL) {
16668                 rc = sqlite3_finalize(hStmt);
16669                 if (rc != SQLITE_OK) {
16670                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16671                         err = EMAIL_ERROR_DB_FAILURE;
16672                 }
16673         }
16674
16675         EM_DEBUG_FUNC_END("err [%d]", err);
16676         return err;
16677 }
16678
16679 INTERNAL_FUNC int emstorage_delete_task(char *multi_user_name, int task_id, int transaction)
16680 {
16681         EM_DEBUG_FUNC_BEGIN("task_id[%d], transaction[%d]", task_id, transaction);
16682         int ret = false;
16683         int err = EMAIL_ERROR_NONE;
16684         char sql_query_string[QUERY_SIZE] = {0, };
16685
16686         sqlite3 *local_db_handle = NULL;
16687
16688         if (task_id < 0) {
16689                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16690                 return EMAIL_ERROR_INVALID_PARAM;
16691         }
16692
16693         local_db_handle = emstorage_get_db_connection(multi_user_name);
16694
16695         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
16696
16697         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_task_tbl WHERE task_id = %d", task_id);
16698         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16699         if (err != EMAIL_ERROR_NONE) {
16700                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16701                         goto FINISH_OFF;
16702         }
16703
16704         ret = true;
16705
16706 FINISH_OFF:
16707         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
16708
16709         EM_DEBUG_FUNC_END("err [%d]", err);
16710         return err;
16711 }
16712
16713 INTERNAL_FUNC int emstorage_update_task_status(char *multi_user_name, int task_id, email_task_status_type_t task_status, int transaction)
16714 {
16715         EM_DEBUG_FUNC_BEGIN("task_id[%d] task_status[%d] transaction[%d]", task_id, task_status, transaction);
16716         int ret = false;
16717         int err = EMAIL_ERROR_NONE;
16718         char sql_query_string[QUERY_SIZE] = {0, };
16719
16720         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16721         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
16722
16723         SNPRINTF(sql_query_string, sizeof(sql_query_string),
16724                 "UPDATE mail_task_tbl SET"
16725                 " task_status = %d"
16726                 " WHERE task_id = %d"
16727                 , task_status
16728                 , task_id);
16729         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16730         if (err != EMAIL_ERROR_NONE) {
16731                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16732                         goto FINISH_OFF;
16733         }
16734
16735         ret = true;
16736
16737 FINISH_OFF:
16738         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
16739
16740         EM_DEBUG_FUNC_END("err [%d]", err);
16741         return err;
16742 }
16743
16744 INTERNAL_FUNC int emstorage_query_task(char *multi_user_name,
16745                                                                         const char *input_conditional_clause,
16746                                                                         const char *input_ordering_clause,
16747                                                                         email_task_t **output_task_list,
16748                                                                         int *output_task_count)
16749 {
16750         EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_ordering_clause [%p], "
16751                                                 "output_task_list[%p], output_task_count[%d]",
16752                                                 input_conditional_clause, input_ordering_clause, output_task_list, output_task_count);
16753         int i = 0, count = 0, rc = -1;
16754         int cur_query = 0;
16755         int field_index = 0;
16756         int err = EMAIL_ERROR_NONE;
16757         email_task_t *task_item_from_tbl = NULL;
16758         char sql_query_string[QUERY_SIZE] = {0, };
16759         char *field_list = "task_id, task_type, task_status, task_priority, task_parameter_length, task_parameter ";
16760         char **result;
16761         sqlite3 *local_db_handle = NULL;
16762         DB_STMT hStmt = NULL;
16763
16764         EM_IF_NULL_RETURN_VALUE(input_conditional_clause, false);
16765         EM_IF_NULL_RETURN_VALUE(output_task_count, false);
16766
16767         local_db_handle = emstorage_get_db_connection(multi_user_name);
16768
16769         SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
16770                                         "SELECT COUNT(*) FROM mail_task_tbl %s", input_conditional_clause);
16771         EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
16772
16773         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
16774         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16775                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16776
16777         count = atoi(result[1]);
16778         sqlite3_free_table(result);
16779
16780         EM_DEBUG_LOG("count = %d", rc);
16781
16782         if (count == 0) {
16783                 EM_DEBUG_EXCEPTION("no task found...");
16784                 err = EMAIL_ERROR_TASK_NOT_FOUND;
16785                 goto FINISH_OFF;
16786         }
16787
16788         SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
16789                                         "SELECT %s FROM mail_task_tbl %s %s", field_list, input_conditional_clause, input_ordering_clause);
16790         EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
16791
16792         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16793
16794         EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
16795         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16796                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16797
16798         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16799         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16800                 ("sqlite3_step fail:%d", rc));
16801
16802         if (rc == SQLITE_DONE) {
16803                 EM_DEBUG_EXCEPTION("no task found...");
16804                 err = EMAIL_ERROR_TASK_NOT_FOUND;
16805                 count = 0;
16806                 goto FINISH_OFF;
16807         }
16808
16809         if (!(task_item_from_tbl = (email_task_t*)em_malloc(sizeof(email_task_t) * count))) {
16810                 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
16811                 err = EMAIL_ERROR_OUT_OF_MEMORY;
16812                 goto FINISH_OFF;
16813         }
16814
16815         for (i = 0; i < count; i++) {
16816                 /*  get recordset */
16817                 field_index = 0;
16818
16819                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_id), field_index++);
16820                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_type), field_index++);
16821                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_status), field_index++);
16822                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_priority), field_index++);
16823                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_parameter_length), field_index++);
16824                 _get_stmt_field_data_blob(hStmt, (void**)&(task_item_from_tbl[i].task_parameter), field_index++);
16825
16826                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16827                 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i,  rc);
16828                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16829                         ("sqlite3_step fail:%d", rc));
16830         }
16831
16832 FINISH_OFF:
16833
16834         if (err == EMAIL_ERROR_NONE) {
16835                 if (output_task_list)
16836                         *output_task_list = task_item_from_tbl;
16837                 *output_task_count = count;
16838         } else {
16839                 if (task_item_from_tbl) {
16840                         for (i = 0; i < count; i++)
16841                                 EM_SAFE_FREE(task_item_from_tbl[i].task_parameter);
16842
16843                         free(task_item_from_tbl);
16844                 }
16845         }
16846
16847         if (hStmt != NULL) {
16848                 rc = sqlite3_finalize(hStmt);
16849                 hStmt = NULL;
16850                 if (rc != SQLITE_OK) {
16851                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16852                         err = EMAIL_ERROR_DB_FAILURE;
16853                 }
16854         }
16855
16856         EM_DEBUG_FUNC_END("err [%d]", err);
16857         return err;
16858 }
16859
16860 INTERNAL_FUNC int emstorage_check_and_update_server_uid_by_message_id(char *multi_user_name, int account_id, email_mailbox_type_e input_mailbox_type, char *message_id, char *server_uid, int *mail_id)
16861 {
16862         EM_DEBUG_FUNC_BEGIN("account_id:[%d], mailbox_type:[%d], message_id:[%s], server_uid:[%s]", account_id, input_mailbox_type, message_id, server_uid);
16863         int err = EMAIL_ERROR_NONE;
16864
16865         if (message_id == NULL) {
16866                 EM_DEBUG_EXCEPTION("Invalid parameter");
16867                 err = EMAIL_ERROR_INVALID_PARAM;
16868                 return err;
16869         }
16870
16871         int rc = -1;
16872         int count = 0;
16873         int temp_mail_id = 0;
16874         int where_pararaph_length = 0;
16875         char *where_pararaph = NULL;
16876         char sql_query_string[QUERY_SIZE] = {0, };
16877         char **result = NULL;
16878         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16879
16880         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id from mail_tbl ");
16881
16882         where_pararaph_length = EM_SAFE_STRLEN(message_id) + 100;
16883         where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
16884         if (where_pararaph == NULL) {
16885                 EM_DEBUG_EXCEPTION("em_mallocfailed");
16886                 err = EMAIL_ERROR_OUT_OF_MEMORY;
16887                 goto FINISH_OFF;
16888         }
16889
16890         if (account_id != ALL_ACCOUNT)
16891                 sqlite3_snprintf(where_pararaph_length, where_pararaph, "WHERE account_id = %d AND mailbox_type = %d AND message_id like '%q'", account_id, input_mailbox_type, message_id);
16892         else
16893                 sqlite3_snprintf(where_pararaph_length, where_pararaph, "WHERE mailbox_type = %d AND message_id like '%q'", input_mailbox_type, message_id);
16894
16895         if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
16896                 strcat(sql_query_string, where_pararaph);
16897
16898         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
16899
16900         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16901         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16902                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16903
16904
16905         EM_DEBUG_LOG("Count of mails [%d]", count);
16906
16907         if (count) {
16908                 _get_table_field_data_int(result, &temp_mail_id, 1);
16909                 EM_DEBUG_LOG("Searched mail_id [%d]", temp_mail_id);
16910
16911                 memset(sql_query_string, 0x00, QUERY_SIZE);
16912                 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string, "UPDATE mail_tbl set server_mail_id = '%q'", server_uid);
16913
16914                 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
16915                         strcat(sql_query_string, where_pararaph);
16916
16917                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16918                 if (err != EMAIL_ERROR_NONE) {
16919                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16920                         goto FINISH_OFF;
16921                 }
16922
16923         } else {
16924                 err = EMAIL_ERROR_MAIL_NOT_FOUND;
16925         }
16926
16927 FINISH_OFF:
16928
16929         sqlite3_free_table(result);
16930         result = NULL;
16931
16932
16933         EM_SAFE_FREE(where_pararaph);
16934
16935         if (mail_id != NULL)
16936                 *mail_id = temp_mail_id;
16937
16938         EM_DEBUG_FUNC_END("err : [%d]", err);
16939         return err;
16940 }
16941 /* Tasks --------------------------------------------------------------------------*/
16942
16943 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
16944 INTERNAL_FUNC int emstorage_add_auto_download_activity(char *multi_user_name, email_event_auto_download *local_activity, int *activity_id, int transaction, int *err_code)
16945 {
16946         EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
16947
16948         if (!local_activity || !activity_id) {
16949                 EM_DEBUG_EXCEPTION("local_activity[%p], activity_id[%p]", local_activity, activity_id);
16950                 if (err_code != NULL)
16951                         *err_code = EMAIL_ERROR_INVALID_PARAM;
16952                 return false;
16953         }
16954
16955         int rc = -1;
16956         int ret = false;
16957         int error = EMAIL_ERROR_NONE;
16958         int i = 0;
16959
16960         char sql_query_string[QUERY_SIZE] = {0, };
16961         DB_STMT hStmt = NULL;
16962
16963         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16964         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
16965
16966         memset(sql_query_string, 0x00, sizeof(sql_query_string));
16967         SNPRINTF(sql_query_string, sizeof(sql_query_string),
16968                 "INSERT INTO mail_auto_download_activity_tbl VALUES "
16969                 "("
16970                 "? "  /* Activity ID */
16971                 ",?"  /* Status */
16972                 ",?"  /* Account ID */
16973                 ",?"  /* Local Mail ID */
16974                 ",?"  /* Server mail ID */
16975                 ",?"  /* Mailbox ID*/
16976                 ",?"  /* Multi USER NAME */
16977                 ") ");
16978
16979         char *sql = "SELECT max(rowid) FROM mail_auto_download_activity_tbl;";
16980         char **result = NULL;
16981
16982
16983         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
16984
16985         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16986                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
16987
16988         if (NULL == result[1]) rc = 1;
16989         else rc = atoi(result[1])+1;
16990         sqlite3_free_table(result);
16991         result = NULL;
16992
16993         *activity_id = local_activity->activity_id = rc;
16994
16995         EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [%d], MAIL ID [%d], SERVER MAIL ID [%lu]",
16996                         local_activity->activity_id, local_activity->mail_id, local_activity->server_mail_id);
16997
16998         if (local_activity->mailbox_id)
16999                 EM_DEBUG_LOG(" MAILBOX ID [%d]", local_activity->mailbox_id);
17000
17001
17002         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17003         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17004                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17005
17006
17007         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
17008         _bind_stmt_field_data_int(hStmt, i++, local_activity->status);
17009         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
17010         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
17011         _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
17012         _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
17013         _bind_stmt_field_data_string(hStmt, i++, (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
17014
17015
17016         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17017
17018         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
17019                 ("sqlite3_step fail:%d", rc));
17020         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17021                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
17022
17023         ret = true;
17024
17025 FINISH_OFF:
17026         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17027         if (hStmt != NULL) {
17028                 rc = sqlite3_finalize(hStmt);
17029                 hStmt = NULL;
17030                 if (rc != SQLITE_OK) {
17031                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17032                         error = EMAIL_ERROR_DB_FAILURE;
17033                 }
17034         }
17035
17036         if (err_code != NULL)
17037                 *err_code = error;
17038
17039         EM_DEBUG_FUNC_END("ret [%d]", ret);
17040         return ret;
17041 }
17042
17043
17044 INTERNAL_FUNC int emstorage_delete_auto_download_activity(char *multi_user_name, int account_id, int mail_id, int activity_id, int transaction, int *err_code)
17045 {
17046         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d] , activity_id[%d], transaction[%d], err_code[%p]", account_id, mail_id, activity_id, transaction, err_code);
17047
17048         if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
17049                 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], activity_id[%d], transaction[%d], err_code[%p]", account_id, mail_id, activity_id, transaction, err_code);
17050
17051                 if (err_code != NULL)
17052                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17053                 return false;
17054         }
17055
17056         int rc = -1;
17057         int ret = false;
17058         int error = EMAIL_ERROR_NONE;
17059         char sql_query_string[QUERY_SIZE] = {0, };
17060         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17061
17062         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17063         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17064
17065         if (activity_id == 0)
17066                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d AND mail_id = %d", account_id, mail_id);
17067         else
17068                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d AND activity_id = %d", account_id, activity_id);
17069
17070         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17071         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17072         if (error != EMAIL_ERROR_NONE) {
17073                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17074                 goto FINISH_OFF;
17075         }
17076
17077         /*  validate activity existence */
17078         rc = sqlite3_changes(local_db_handle);
17079         if (rc == 0) {
17080                 EM_DEBUG_EXCEPTION("No matching activity found");
17081                 error = EMAIL_ERROR_DATA_NOT_FOUND;
17082                 ret = true;
17083                 goto FINISH_OFF;
17084         }
17085
17086         ret = true;
17087
17088 FINISH_OFF:
17089         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17090
17091         if (err_code != NULL)
17092                 *err_code = error;
17093
17094         EM_DEBUG_FUNC_END("ret [%d]", ret);
17095         return ret;
17096 }
17097
17098 INTERNAL_FUNC int emstorage_delete_all_auto_download_activity(char *multi_user_name, int account_id, int transaction, int *err_code)
17099 {
17100         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17101
17102         if (account_id < FIRST_ACCOUNT_ID) {
17103                 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17104                 if (err_code != NULL)
17105                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17106                 return false;
17107         }
17108
17109         int rc = -1;
17110         int ret = false;
17111         int error = EMAIL_ERROR_NONE;
17112         char sql_query_string[QUERY_SIZE] = {0, };
17113
17114         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17115
17116         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17117         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17118         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d", account_id);
17119
17120         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17121         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17122         if (error != EMAIL_ERROR_NONE) {
17123                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17124                         goto FINISH_OFF;
17125         }
17126
17127         rc = sqlite3_changes(local_db_handle);
17128         if (rc == 0) {
17129                 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17130                 error = EMAIL_ERROR_DATA_NOT_FOUND;
17131                 ret = true;
17132                 goto FINISH_OFF;
17133         }
17134
17135         ret = true;
17136
17137 FINISH_OFF:
17138         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17139
17140         if (err_code != NULL)
17141                 *err_code = error;
17142
17143         EM_DEBUG_FUNC_END("ret [%d]", ret);
17144         return ret;
17145 }
17146
17147
17148 INTERNAL_FUNC int emstorage_delete_auto_download_activity_by_mailbox(char *multi_user_name, int account_id, int mailbox_id, int transaction, int *err_code)
17149 {
17150         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17151
17152         if (account_id < FIRST_ACCOUNT_ID || mailbox_id < 0) {
17153                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17154                 if (err_code != NULL)
17155                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17156                 return false;
17157         }
17158
17159         int rc = -1;
17160         int ret = false;
17161         int error = EMAIL_ERROR_NONE;
17162         char sql_query_string[QUERY_SIZE] = {0, };
17163
17164         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17165
17166         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17167         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17168         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d AND mailbox_id = %d", account_id, mailbox_id);
17169
17170         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17171         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17172         if (error != EMAIL_ERROR_NONE) {
17173                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17174                         goto FINISH_OFF;
17175         }
17176
17177         rc = sqlite3_changes(local_db_handle);
17178         if (rc == 0) {
17179                 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17180                 error = EMAIL_ERROR_DATA_NOT_FOUND;
17181                 ret = true;
17182                 goto FINISH_OFF;
17183         }
17184
17185         ret = true;
17186
17187 FINISH_OFF:
17188         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17189
17190         if (err_code != NULL)
17191                 *err_code = error;
17192
17193         EM_DEBUG_FUNC_END("ret [%d]", ret);
17194         return ret;
17195 }
17196
17197
17198 INTERNAL_FUNC int emstorage_get_auto_download_activity(char *multi_user_name, int account_id, int input_mailbox_id, email_event_auto_download **event_start, int *count, int transaction, int *err_code)
17199 {
17200         EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
17201
17202         if (account_id < FIRST_ACCOUNT_ID || !event_start || input_mailbox_id <= 0 || !count) {
17203                 EM_DEBUG_EXCEPTION("account_id[%d], event_start[%p], input_mailbox_id[%d], count[%p], err_code[%p]", account_id, event_start, input_mailbox_id, count, err_code);
17204
17205                 if (err_code != NULL)
17206                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17207                 return false;
17208         }
17209
17210         int rc = -1;
17211         int ret = false;
17212         char **result;
17213         int error = EMAIL_ERROR_NONE;
17214         int i = 0;
17215         DB_STMT hStmt = NULL;
17216         email_event_auto_download *event_list = NULL;
17217         char sql_query_string[QUERY_SIZE] = {0, };
17218
17219         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17220
17221         EMSTORAGE_START_READ_TRANSACTION(transaction);
17222
17223         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17224         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl WHERE account_id = %d AND mailbox_id = '%d' order by activity_id", account_id, input_mailbox_id);
17225
17226
17227         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17228         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17229                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17230
17231         *count = atoi(result[1]);
17232         sqlite3_free_table(result);
17233
17234         EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
17235
17236         if (!*count) {
17237                 EM_DEBUG_LOG("No matched activity found in mail_auto_download_activity_tbl");
17238                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
17239                 ret = true;
17240                 goto FINISH_OFF;
17241         }
17242         EM_DEBUG_LOG("Activity Count = %d", *count);
17243
17244         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17245         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_auto_download_activity_tbl WHERE account_id = %d AND mailbox_id = '%d' order by activity_id", account_id, input_mailbox_id);
17246
17247         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17248
17249
17250         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17251
17252         EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
17253         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17254                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17255
17256
17257         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17258         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17259                 ("sqlite3_step fail:%d", rc));
17260
17261         if (!(event_list = (email_event_auto_download *)em_malloc(sizeof(email_event_auto_download)*(*count)))) {
17262                 EM_DEBUG_EXCEPTION("Malloc failed");
17263
17264                 error = EMAIL_ERROR_OUT_OF_MEMORY;
17265                 goto FINISH_OFF;
17266         }
17267
17268         for (i = 0; i < (*count); i++) {
17269                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17270                 _get_stmt_field_data_int(hStmt, &(event_list[i].status), STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17271                 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17272                 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17273                 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17274                 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17275
17276                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17277                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17278                         ("sqlite3_step fail:%d", rc));
17279         }
17280
17281         ret = true;
17282
17283 FINISH_OFF:
17284
17285         if (true == ret)
17286           *event_start = event_list;
17287         else {
17288                 EM_SAFE_FREE(event_list);
17289                 *event_start = NULL;
17290                 *count = 0;
17291         }
17292
17293         if (hStmt != NULL) {
17294                 rc = sqlite3_finalize(hStmt);
17295                 hStmt = NULL;
17296                 if (rc != SQLITE_OK) {
17297                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17298                         error = EMAIL_ERROR_DB_FAILURE;
17299                 }
17300         }
17301
17302         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17303         if (err_code != NULL)
17304                 *err_code = error;
17305
17306         EM_DEBUG_FUNC_END("ret [%d]", ret);
17307         return ret;
17308 }
17309
17310
17311 INTERNAL_FUNC int emstorage_get_auto_download_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
17312 {
17313         EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
17314
17315         if (!activity_count || !err_code) {
17316                 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
17317                 if (err_code != NULL)
17318                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17319                 return false;
17320         }
17321
17322         int rc = -1;
17323         int ret = false;
17324         int error = EMAIL_ERROR_NONE;
17325         DB_STMT hStmt = NULL;
17326         char sql_query_string[QUERY_SIZE] = {0, };
17327
17328         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17329
17330         EMSTORAGE_START_READ_TRANSACTION(transaction);
17331         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17332
17333         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl;");
17334
17335         EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
17336
17337
17338         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17339         EM_DEBUG_LOG_DEV("before sqlite3_prepare hStmt = %p", hStmt);
17340         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17341                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17342
17343
17344         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17345         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17346                 ("sqlite3_step fail:%d", rc));
17347
17348         _get_stmt_field_data_int(hStmt, activity_count, 0);
17349
17350         EM_DEBUG_LOG("counts of activities in activity table [%d]", *activity_count);
17351
17352         ret = true;
17353
17354 FINISH_OFF:
17355
17356         if (hStmt != NULL) {
17357                 rc = sqlite3_finalize(hStmt);
17358                 hStmt = NULL;
17359                 if (rc != SQLITE_OK) {
17360                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17361                         error = EMAIL_ERROR_DB_FAILURE;
17362                 }
17363         }
17364
17365         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17366         if (err_code != NULL)
17367                 *err_code = error;
17368
17369         EM_DEBUG_FUNC_END("ret [%d]", ret);
17370         return ret;
17371 }
17372
17373
17374 INTERNAL_FUNC int emstorage_get_auto_download_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
17375 {
17376         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
17377
17378         if (!account_list || !count) {
17379                 EM_DEBUG_EXCEPTION("account_list[%p], count[%p]", account_list, count);
17380                 if (err_code != NULL)
17381                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17382                 return false;
17383         }
17384
17385         int ret = false;
17386         int error = EMAIL_ERROR_NONE;
17387         char *sql = "SELECT count(distinct account_id) FROM mail_auto_download_activity_tbl";
17388         char **result;
17389         int i = 0, rc = -1;
17390         int *result_account_list = NULL;
17391         DB_STMT hStmt = NULL;
17392         char sql_query_string[QUERY_SIZE] = {0, };
17393         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17394
17395         EMSTORAGE_START_READ_TRANSACTION(transaction);
17396
17397         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
17398         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17399                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
17400
17401         *count = atoi(result[1]);
17402         sqlite3_free_table(result);
17403
17404         if (!*count) {
17405                 EM_DEBUG_EXCEPTION("no account found...");
17406                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17407                 ret = true;
17408                 goto FINISH_OFF;
17409         }
17410
17411         EM_DEBUG_LOG("Account count [%d]", *count);
17412
17413         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17414
17415         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_auto_download_activity_tbl");
17416
17417         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17418
17419
17420         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17421
17422         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
17423         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17424                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17425
17426
17427         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17428         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17429                 ("sqlite3_step fail:%d", rc));
17430
17431         if (NULL == (result_account_list = (int *)em_malloc(sizeof(int)*(*count)))) {
17432                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
17433                 error = EMAIL_ERROR_OUT_OF_MEMORY;
17434                 goto FINISH_OFF;
17435         }
17436
17437         for (i = 0; i < (*count); i++) {
17438                 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
17439
17440                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17441                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17442                         ("sqlite3_step fail:%d", rc));
17443                 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
17444         }
17445
17446         ret = true;
17447
17448 FINISH_OFF:
17449
17450         if (ret == true)
17451                 *account_list = result_account_list;
17452         else
17453                 EM_SAFE_FREE(result_account_list);
17454
17455         if (hStmt != NULL) {
17456                 rc = sqlite3_finalize(hStmt);
17457                 hStmt = NULL;
17458                 if (rc != SQLITE_OK) {
17459                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17460                         error = EMAIL_ERROR_DB_FAILURE;
17461                 }
17462         }
17463
17464         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17465         if (err_code != NULL)
17466                 *err_code = error;
17467         EM_DEBUG_FUNC_END("ret [%d]", ret);
17468         return ret;
17469 }
17470
17471
17472 INTERNAL_FUNC int emstorage_get_auto_download_mailbox_list(char *multi_user_name, int account_id, int **mailbox_list, int *count, int transaction, int *err_code)
17473 {
17474         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
17475
17476         if (account_id < FIRST_ACCOUNT_ID || !mailbox_list || !count) {
17477                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p]", account_id, mailbox_list, count);
17478                 if (err_code != NULL)
17479                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17480                 return false;
17481         }
17482
17483         int ret = false;
17484         int error = EMAIL_ERROR_NONE;
17485         char **result;
17486         int i = 0, rc = -1;
17487         int *mbox_list = NULL;
17488         DB_STMT hStmt = NULL;
17489         char sql_query_string[QUERY_SIZE] = {0, };
17490
17491         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17492
17493         EMSTORAGE_START_READ_TRANSACTION(transaction);
17494
17495         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17496         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(distinct mailbox_id) FROM mail_auto_download_activity_tbl WHERE account_id = %d order by mailbox_id", account_id);
17497
17498
17499         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17500         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17501                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17502
17503         *count = atoi(result[1]);
17504         sqlite3_free_table(result);
17505
17506         if (!*count) {
17507                 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
17508                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17509                 ret = true;
17510                 goto FINISH_OFF;
17511         }
17512         EM_DEBUG_LOG("Mailbox count = %d", *count);
17513
17514         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17515
17516         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct mailbox_id FROM mail_auto_download_activity_tbl WHERE account_id = %d order by mailbox_id", account_id);
17517
17518         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
17519
17520
17521         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17522
17523
17524         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17525                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17526
17527
17528         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17529         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17530                 ("sqlite3_step fail:%d", rc));
17531
17532         mbox_list = (int *)em_malloc(sizeof(int)*(*count)); /* prevent */
17533         if (mbox_list == NULL) {
17534                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
17535                 error = EMAIL_ERROR_OUT_OF_MEMORY;
17536                 goto FINISH_OFF;
17537         }
17538
17539         for (i = 0; i < (*count); i++) {
17540                 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
17541                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17542
17543                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17544                         ("sqlite3_step fail:%d", rc));
17545                 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
17546         }
17547
17548         ret = true;
17549
17550 FINISH_OFF:
17551
17552         if (ret == true)
17553                 *mailbox_list = mbox_list;
17554         else
17555                 EM_SAFE_FREE(mbox_list);
17556
17557         if (hStmt != NULL) {
17558                 rc = sqlite3_finalize(hStmt);
17559                 hStmt = NULL;
17560                 if (rc != SQLITE_OK) {
17561                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17562                         error = EMAIL_ERROR_DB_FAILURE;
17563                 }
17564         }
17565
17566         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17567         if (err_code != NULL)
17568                 *err_code = error;
17569         EM_DEBUG_FUNC_END("ret [%d]", ret);
17570         return ret;
17571 }
17572
17573
17574 INTERNAL_FUNC int emstorage_get_auto_download_activity_count_by_mailbox(char *multi_user_name, int account_id, int input_mailbox_id, int *activity_count, int transaction, int *err_code)
17575 {
17576         EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
17577
17578         if (account_id < FIRST_ACCOUNT_ID || !activity_count || !err_code) {
17579                 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
17580                 if (err_code != NULL)
17581                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17582                 return false;
17583         }
17584
17585         int rc = -1;
17586         int ret = false;
17587         int error = EMAIL_ERROR_NONE;
17588         char sql_query_string[QUERY_SIZE] = {0, };
17589         DB_STMT hStmt = NULL;
17590
17591         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17592
17593         EMSTORAGE_START_READ_TRANSACTION(transaction);
17594         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17595         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl WHERE account_id = %d and mailbox_id = '%d'", account_id, input_mailbox_id);
17596
17597         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
17598
17599         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17600         EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
17601         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17602                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17603
17604         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17605         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17606                 ("sqlite3_step fail:%d", rc));
17607
17608         _get_stmt_field_data_int(hStmt, activity_count, 0);
17609
17610         EM_DEBUG_LOG("count of activities in activity table [%d]", *activity_count);
17611
17612         ret = true;
17613
17614 FINISH_OFF:
17615
17616         if (hStmt != NULL) {
17617                 rc = sqlite3_finalize(hStmt);
17618                 hStmt = NULL;
17619                 if (rc != SQLITE_OK) {
17620                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17621                         error = EMAIL_ERROR_DB_FAILURE;
17622                 }
17623         }
17624
17625         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17626
17627         if (err_code != NULL)
17628                 *err_code = error;
17629
17630         EM_DEBUG_FUNC_END("ret [%d]", ret);
17631         return ret;
17632 }
17633
17634
17635 INTERNAL_FUNC int emstorage_update_auto_download_activity(char *multi_user_name, char *old_server_uid, char *new_server_uid, char *mailbox_name, int mailbox_id, int *err_code)
17636 {
17637         EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mailbox_id[%d]", old_server_uid, new_server_uid, mailbox_id);
17638
17639         int rc = -1, ret = false;
17640         int error = EMAIL_ERROR_NONE;
17641         char sql_query_string[QUERY_SIZE] = {0, };
17642         int transaction = true;
17643
17644         if (!old_server_uid || !new_server_uid || (!mailbox_name && mailbox_id < 0)) {
17645                 EM_DEBUG_EXCEPTION("Invalid parameters");
17646                 error = EMAIL_ERROR_INVALID_PARAM;
17647                 return false;
17648         }
17649
17650         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17651
17652         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17653         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17654
17655         if (mailbox_id > 0)
17656                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17657                          "UPDATE mail_auto_download_activity_tbl SET server_mail_id = %s , mailbox_id ='%d' WHERE server_mail_id = %s ", new_server_uid, mailbox_id, old_server_uid);
17658         else if (mailbox_name)
17659                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17660                          "UPDATE mail_auto_download_activity_tbl SET server_mail_id = %s WHERE server_mail_id = %s ", new_server_uid, old_server_uid);
17661
17662         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17663         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17664         if (error != EMAIL_ERROR_NONE) {
17665                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17666                         goto FINISH_OFF;
17667         }
17668
17669         rc = sqlite3_changes(local_db_handle);
17670         if (rc == 0) {
17671                 EM_DEBUG_LOG("No matching found in mail_auto_download_activity_tbl");
17672         }
17673
17674         ret = true;
17675
17676 FINISH_OFF:
17677
17678         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17679
17680         if (err_code != NULL)
17681                 *err_code = error;
17682
17683         EM_DEBUG_FUNC_END("ret [%d]", ret);
17684         return ret;
17685 }
17686
17687 #endif
17688
17689 #ifdef __FEATURE_UPDATE_DB_TABLE_SCHEMA__
17690
17691 typedef struct {
17692         char *column_name;
17693         char *column_type;
17694 } email_column_info_t;
17695
17696 static int get_column_information_from_table_callback(void *arg1, int argc, char **argv, char **input_column_name)
17697 {
17698         EM_DEBUG_FUNC_BEGIN("arg1[%p] argc[%d] argv[%p] column_name[%p]", arg1, argc, argv, input_column_name);
17699
17700         int i = 0;
17701         int validated = 0;
17702         char *column_name = NULL;
17703         char *column_type = NULL;
17704         GList *new_list = *((GList**)arg1);
17705         email_column_info_t *column_info_item = NULL;
17706
17707         for (i = 0; i < argc; ++i) {
17708                 /* EM_DEBUG_LOG("%s = %s", input_column_name[i], argv[i]); */
17709                 if (EM_SAFE_STRCMP(input_column_name[i], "name") == 0) {
17710                         if (column_name)
17711                                 EM_SAFE_FREE(column_name);
17712
17713                         column_name = EM_SAFE_STRDUP(argv[i]);
17714                         validated = 1;
17715                 } else if (EM_SAFE_STRCMP(input_column_name[i], "type") == 0) {
17716                         if (column_type)
17717                                 EM_SAFE_FREE(column_type);
17718
17719                         column_type = EM_SAFE_STRDUP(argv[i]);
17720                 }
17721         }
17722
17723         if (validated) {
17724                 EM_DEBUG_LOG("column_name[%s] column_type[%s]", column_name, column_type);
17725                 column_info_item = em_malloc(sizeof(email_column_info_t));
17726         if (column_info_item == NULL) {
17727             EM_DEBUG_EXCEPTION("em_mallocfailed");
17728             goto FINISH_OFF;
17729         }
17730
17731                 column_info_item->column_name = EM_SAFE_STRDUP(column_name);
17732                 column_info_item->column_type = EM_SAFE_STRDUP(column_type);
17733                 new_list = g_list_append(new_list, (gpointer)column_info_item);
17734                 *((GList**)arg1) = new_list;
17735         }
17736
17737 FINISH_OFF:
17738
17739     EM_SAFE_FREE(column_name);
17740     EM_SAFE_FREE(column_type);
17741
17742         EM_DEBUG_FUNC_END();
17743         return 0;
17744 }
17745
17746 static int emstorage_get_column_information_from_table(char *multi_user_name, const char *input_table_name, GList **output_column_info)
17747 {
17748         EM_DEBUG_FUNC_BEGIN("input_table_name[%p] output_column_info[%p]", input_table_name, output_column_info);
17749         int err = EMAIL_ERROR_NONE;
17750         int result_from_sqlite = 0;
17751         char *error_message_from_sqlite = NULL;
17752         char sql_query_string[QUERY_SIZE] = {0, };
17753         GList *new_list = NULL;
17754         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17755
17756         SNPRINTF(sql_query_string, QUERY_SIZE, "pragma table_info(%s);", input_table_name);
17757
17758         result_from_sqlite = sqlite3_exec(local_db_handle, sql_query_string, get_column_information_from_table_callback, &new_list, &error_message_from_sqlite);
17759
17760         if (result_from_sqlite != SQLITE_OK)
17761                 EM_DEBUG_EXCEPTION("sqlite3_exec returns [%d]", result_from_sqlite);
17762
17763         EM_DEBUG_LOG("new_list[%p] output_column_info[%p]", new_list, output_column_info);
17764
17765         if (new_list && output_column_info) {
17766                 EM_DEBUG_LOG("g_list_length[%d]", g_list_length(new_list));
17767                 *output_column_info = new_list;
17768         }
17769
17770         EM_DEBUG_FUNC_END("err [%d]", err);
17771         return err;
17772 }
17773
17774 static  int emstorage_create_renamed_table(char *multi_user_name, char **input_full_query, int input_query_index, char *input_source_table_name, char *input_new_table_name)
17775 {
17776         EM_DEBUG_FUNC_BEGIN("input_full_query [%p] input_query_index[%d] input_source_table_name[%p] input_new_table_name[%p]", input_full_query, input_query_index, input_source_table_name, input_new_table_name);
17777         int error = EMAIL_ERROR_NONE;
17778         int rc = -1;
17779         sqlite3 *local_db_handle = NULL;
17780         char sql_query_string[QUERY_SIZE] = {0, };
17781
17782         if (input_full_query == NULL || input_source_table_name == NULL || input_new_table_name == NULL) {
17783                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17784                 error = EMAIL_ERROR_INVALID_PARAM;
17785                 goto FINISH_OFF;
17786         }
17787
17788         local_db_handle = emstorage_get_db_connection(multi_user_name);
17789
17790         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17791         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17792
17793         EM_DEBUG_LOG("[%s] will be replaced by [%s]", input_source_table_name, input_new_table_name);
17794
17795         EM_SAFE_STRNCPY(sql_query_string, input_full_query[input_query_index], sizeof(sql_query_string)-1); /*prevent 21984*/
17796         reg_replace(sql_query_string, input_source_table_name, input_new_table_name);
17797
17798         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17799 FINISH_OFF:
17800
17801         if (error == EMAIL_ERROR_NONE) {
17802                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17803         } else {
17804                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17805         }
17806
17807         EM_DEBUG_FUNC_END("error [%d]", error);
17808         return error;
17809 }
17810
17811 static int emstorage_add_column(char *multi_user_name, char *input_table_name, email_column_info_t *input_new_column)
17812 {
17813         EM_DEBUG_FUNC_BEGIN("input_table_name[%p] input_new_column[%p]", input_table_name, input_new_column);
17814         int error = EMAIL_ERROR_NONE;
17815         int rc = -1;
17816         sqlite3 *local_db_handle = NULL;
17817         char sql_query_string[QUERY_SIZE] = {0, };
17818
17819         if (input_table_name == NULL || input_new_column == NULL) {
17820                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17821                 error = EMAIL_ERROR_INVALID_PARAM;
17822                 goto FINISH_OFF;
17823         }
17824
17825         local_db_handle = emstorage_get_db_connection(multi_user_name);
17826
17827         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17828         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17829         SNPRINTF(sql_query_string, QUERY_SIZE, "ALTER TABLE %s ADD COLUMN %s %s;", input_table_name, input_new_column->column_name, input_new_column->column_type);
17830         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17831 FINISH_OFF:
17832
17833         if (error == EMAIL_ERROR_NONE) {
17834                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17835         } else {
17836                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17837         }
17838
17839         EM_DEBUG_FUNC_END("error [%d]", error);
17840         return error;
17841 }
17842
17843 static int emstorage_drop_table(char *multi_user_name, char *input_table_name)
17844 {
17845         EM_DEBUG_FUNC_BEGIN("input_table_name[%p]", input_table_name);
17846         int error = EMAIL_ERROR_NONE;
17847         int rc = -1;
17848         sqlite3 *local_db_handle = NULL;
17849         char sql_query_string[QUERY_SIZE] = {0, };
17850
17851         if (input_table_name == NULL) {
17852                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17853                 error = EMAIL_ERROR_INVALID_PARAM;
17854                 goto FINISH_OFF;
17855         }
17856
17857         local_db_handle = emstorage_get_db_connection(multi_user_name);
17858
17859         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17860         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17861         SNPRINTF(sql_query_string, QUERY_SIZE, "DROP TABLE %s;", input_table_name);
17862         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17863 FINISH_OFF:
17864
17865         if (error == EMAIL_ERROR_NONE) {
17866                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17867         } else {
17868                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17869         }
17870
17871         EM_DEBUG_FUNC_END("error [%d]", error);
17872         return error;
17873 }
17874
17875 gint glist_compare_column_name(gconstpointer old_column_info, gconstpointer new_column_info)
17876 {
17877         EM_DEBUG_FUNC_BEGIN("old_column_info[%p] new_column_info[%p]", old_column_info, new_column_info);
17878         email_column_info_t *left_one  = (email_column_info_t*)old_column_info;
17879         email_column_info_t *right_one = (email_column_info_t*)new_column_info;
17880
17881         if (old_column_info == NULL || new_column_info == NULL)
17882                 return -1;
17883
17884         return EM_SAFE_STRCMP((char*)left_one->column_name, (char*)right_one->column_name);
17885 }
17886
17887 INTERNAL_FUNC int emstorage_update_db_table_schema(char *multi_user_name)
17888 {
17889         EM_DEBUG_FUNC_BEGIN();
17890         int i = 0;
17891         int j = 0;
17892         int error = EMAIL_ERROR_NONE;
17893         int query_len = 0;
17894         email_column_info_t *new_column_info = NULL;
17895         email_column_info_t *p_column_info = NULL;
17896         char **create_table_query = NULL;
17897         GList *found_data = NULL;
17898         GList *column_list_of_old_table = NULL;
17899         GList *column_list_of_new_table = NULL;
17900         char table_names[CREATE_TABLE_MAX][2][50] = { { "mail_account_tbl", "mail_account_tbl_new" },
17901                 { "mail_box_tbl", "mail_box_tbl_new" },
17902                 { "mail_read_mail_uid_tbl", "mail_read_mail_uid_tbl_new" },
17903                 { "mail_rule_tbl", "mail_rule_tbl_new" },
17904                 { "mail_tbl", "mail_tbl_new" },
17905                 { "mail_attachment_tbl", "mail_attachment_tbl_new" },
17906 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
17907                 { "mail_partial_body_activity_tbl", "mail_partial_body_activity_tbl_new" },
17908 #else
17909                 { "", "" },
17910 #endif
17911                 { "mail_meeting_tbl", "mail_meeting_tbl_new" },
17912 #ifdef __FEATURE_LOCAL_ACTIVITY__
17913                 { "mail_local_activity_tbl", "mail_local_activity_tbl_new" },
17914 #else
17915                 { "", "" },
17916 #endif
17917                 { "mail_certificate_tbl", "mail_certificate_tbl_new" },
17918                 { "mail_task_tbl", "mail_task_tbl_new" },
17919 #ifdef __FEATURE_BODY_SEARCH__
17920                 { "mail_text_tbl", "mail_text_tbl_new" },
17921 #else
17922                 { "", "" },
17923 #endif
17924
17925 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
17926                 { "mail_auto_download_activity_tbl", "mail_auto_download_activity_tbl_new" }
17927 #else
17928                 { "", "" }
17929 #endif
17930
17931         };
17932
17933         error = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
17934
17935         if (error != EMAIL_ERROR_NONE) {
17936                 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
17937                 goto FINISH_OFF;
17938         }
17939
17940         if (query_len < CREATE_TABLE_MAX) {
17941                 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
17942                 error = EMAIL_ERROR_SYSTEM_FAILURE;
17943                 goto FINISH_OFF;
17944         }
17945
17946         for (i = CREATE_TABLE_MAIL_ACCOUNT_TBL; i < CREATE_TABLE_MAX; i++) {
17947                 EM_DEBUG_LOG("table [%s] new_table [%s]", table_names[i][0], table_names[i][1]);
17948                 if (EM_SAFE_STRLEN(table_names[i][0]) && EM_SAFE_STRLEN(table_names[i][1])) {
17949                         /* Check existing of _new table */
17950                         emstorage_drop_table(multi_user_name, table_names[i][1]);
17951                         error = emstorage_create_renamed_table(multi_user_name, create_table_query, i, table_names[i][0], table_names[i][1]);
17952                         if (error != EMAIL_ERROR_NONE) {
17953                                 EM_DEBUG_EXCEPTION("emstorage_create_renamed_table failed [%d]", error);
17954                                 goto FINISH_OFF;
17955                         }
17956
17957                         emstorage_get_column_information_from_table(multi_user_name, table_names[i][0], &column_list_of_old_table);
17958                         emstorage_get_column_information_from_table(multi_user_name, table_names[i][1], &column_list_of_new_table);
17959
17960                         /* Compare fields and add new field */
17961                         for (j = 0; j < g_list_length(column_list_of_new_table); j++) {
17962                                 new_column_info = (email_column_info_t*)g_list_nth_data(column_list_of_new_table, j);
17963                                 found_data = g_list_find_custom(column_list_of_old_table, (gconstpointer)new_column_info, glist_compare_column_name);
17964                                 if (found_data == NULL) {
17965                                         /* add new field*/
17966                                         emstorage_add_column(multi_user_name, table_names[i][0], new_column_info);
17967                                 }
17968                         }
17969
17970                         emstorage_drop_table(multi_user_name, table_names[i][1]);
17971                 } else
17972                         EM_DEBUG_LOG("Skipped");
17973         }
17974
17975 FINISH_OFF:
17976         if (create_table_query) {
17977                 int i = 0;
17978                 for (i = 0; i < query_len; i++) {
17979                         if (create_table_query[i]) {
17980                                 EM_SAFE_FREE(create_table_query[i]);
17981                         }
17982                 }
17983                 EM_SAFE_FREE(create_table_query);
17984         }
17985
17986         found_data = g_list_first(column_list_of_old_table);
17987         while (found_data != NULL) {
17988                 p_column_info = (email_column_info_t *)found_data->data;
17989                 EM_SAFE_FREE(p_column_info->column_name);
17990                 EM_SAFE_FREE(p_column_info->column_type);
17991                 EM_SAFE_FREE(p_column_info);
17992
17993                 found_data = g_list_next(found_data);
17994         }
17995         g_list_free(column_list_of_old_table);
17996
17997         found_data = g_list_first(column_list_of_new_table);
17998         while (found_data != NULL) {
17999                 p_column_info = (email_column_info_t *)found_data->data;
18000                 EM_SAFE_FREE(p_column_info->column_name);
18001                 EM_SAFE_FREE(p_column_info->column_type);
18002                 EM_SAFE_FREE(p_column_info);
18003
18004                 found_data = g_list_next(found_data);
18005         }
18006         g_list_free(column_list_of_new_table);
18007
18008         EM_DEBUG_FUNC_END("error [%d]", error);
18009         return error;
18010 }
18011 #endif /* __FEATURE_UPDATE_DB_TABLE_SCHEMA__ */
18012
18013 /*EOF*/