9931add3c3768142cd69cd29bb0f689b78d99b58
[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 <ss_manager.h>
49 #include <fcntl.h>
50 #include <db-util.h>
51
52 #define __USE_UNIX98
53 #define __USE_GNU
54 #include <pthread.h>
55
56 #include "email-internal-types.h"
57 #include "email-convert.h"
58 #include "email-core-utils.h"
59 #include "email-utilities.h"
60 #include "email-storage.h"
61 #include "email-debug-log.h"
62 #include "email-types.h"
63 #include "email-convert.h"
64 #include "email-core-signal.h"
65
66 #define DB_STMT sqlite3_stmt *
67
68 #define SETTING_MEMORY_TEMP_FILE_PATH "/tmp/email_tmp_file"
69
70 #define CONTENT_DATA                  "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset="
71 #define CONTENT_TYPE_DATA             "<meta http-equiv=\"Content-Type\" content=\"text/html; charset="
72
73 #define FLAG_SEEN       0x01
74 #define FLAG_DELETED    0x02
75 #define FLAG_FLAGGED    0x04
76 #define FLAG_ANSWERED   0x08
77 #define FLAG_OLD        0x10
78 #define FLAG_DRAFT      0x20
79
80 #undef close
81
82 #define ISSUE_ORGANIZATION_LEN_IN_MAIL_CERTIFICATE_TBL  256
83 #define EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL       128
84 #define SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL      1027
85 #define FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL           256
86
87 #define ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL            50
88 #define RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL   50
89 #define EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL              128
90 #define USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL               50
91 #define PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL                50
92 #define SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL     50
93 #define SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL            50
94 #define SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL        50
95 #define DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL            30
96 #define REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL           128
97 #define RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL             128
98 #define LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL          256
99 #define DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL       256
100 #define SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL               256
101 #define MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL                128
102 #define ALIAS_LEN_IN_MAIL_BOX_TBL                       128
103 #define LOCAL_MBOX_LEN_IN_MAIL_READ_MAIL_UID_TBL        128
104 #define MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL      128
105 #define S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL             128
106 #define DATA2_LEN_IN_MAIL_READ_MAIL_UID_TBL             256
107 #define VALUE_LEN_IN_MAIL_RULE_TBL                      256
108 #define DEST_MAILBOX_LEN_IN_MAIL_RULE_TBL               128
109 #define MAILBOX_NAME_LEN_IN_MAIL_ATTACHMENT_TBL         128
110 #define ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL      256
111 #define ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL      256
112 #define CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL           256
113 #define ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL 128
114 #define MAILBOX_LEN_IN_MAIL_TBL                         128
115 #define SERVER_MAILBOX_LEN_IN_MAIL_TBL                  128
116 #define SERVER_MAIL_ID_LEN_IN_MAIL_TBL                  128
117 #define FROM_LEN_IN_MAIL_TBL                            256
118 #define SENDER_LEN_IN_MAIL_TBL                          256
119 #define REPLY_TO_LEN_IN_MAIL_TBL                        256
120 #define TO_LEN_IN_MAIL_TBL                              3999
121 #define CC_LEN_IN_MAIL_TBL                              3999
122 #define BCC_LEN_IN_MAIL_TBL                             3999
123 #define RETURN_PATH_LEN_IN_MAIL_TBL                     3999
124 #define SUBJECT_LEN_IN_MAIL_TBL                         1027
125 #define THREAD_TOPIC_LEN_IN_MAIL_TBL                    256
126 #define TEXT_1_LEN_IN_MAIL_TBL                          256
127 #define TEXT_2_LEN_IN_MAIL_TBL                          256
128 #define MIME_ENTITY_LEN_IN_MAIL_TBL                     256
129 #define DATETIME_LEN_IN_MAIL_TBL                        128
130 #define MESSAGE_ID_LEN_IN_MAIL_TBL                      256
131 #define FROM_CONTACT_NAME_LEN_IN_MAIL_TBL               3999
132 #define FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL              3999
133 #define TO_CONTACT_NAME_LEN_IN_MAIL_TBL                 3999
134 #define TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL                3999
135 #define MAILBOX_LEN_IN_MAIL_MEETING_TBL                 128
136 #define LOCATION_LEN_IN_MAIL_MEETING_TBL                1024
137 #define GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL        512
138 #define STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL           32
139 #define DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL           32
140 #define PREVIEWBODY_LEN_IN_MAIL_TBL                     512
141 #define CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL        256
142
143 /*  this define is used for query to change data (delete, insert, update) */
144 #define EMSTORAGE_START_WRITE_TRANSACTION(transaction_flag, error_code) \
145         if (transaction_flag)\
146         {\
147                 _timedlock_shm_mutex(&mapped_for_db_lock, 2);\
148                 if (emstorage_begin_transaction(NULL, NULL, &error_code) == false) \
149                 {\
150                         EM_DEBUG_EXCEPTION("emstorage_begin_transaction() error[%d]", error_code);\
151                         goto FINISH_OFF;\
152                 }\
153         }
154
155 /*  this define is used for query to change data (delete, insert, update) */
156 #define EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction_flag, result_code, error_code) \
157         if (transaction_flag)\
158         {\
159                 if (result_code == true)\
160                 {\
161                         if (emstorage_commit_transaction(NULL, NULL, NULL) == false)\
162                         {\
163                                 error_code = EMAIL_ERROR_DB_FAILURE;\
164                                 result_code = false;\
165                         }\
166                 }\
167                 else\
168                 {\
169                         if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)\
170                                 error_code = EMAIL_ERROR_DB_FAILURE;\
171                 }\
172                 _unlockshm_mutex(&mapped_for_db_lock);\
173         }
174
175 /*  this define is used for query to read (select) */
176 #define EMSTORAGE_START_READ_TRANSACTION(transaction_flag) \
177         if (transaction_flag)\
178         {\
179                 /*_timedlock_shm_mutex(&mapped_for_db_lock, 2);*/\
180         }
181
182 /*  this define is used for query to read (select) */
183 #define EMSTORAGE_FINISH_READ_TRANSACTION(transaction_flag) \
184         if (transaction_flag)\
185         {\
186                 /*_unlockshm_mutex(&mapped_for_db_lock);*/\
187         }
188
189 /*  for safety DB operation */
190 static pthread_mutex_t _transactionBeginLock = PTHREAD_MUTEX_INITIALIZER;
191 static pthread_mutex_t _transactionEndLock = PTHREAD_MUTEX_INITIALIZER;
192 static pthread_mutex_t _db_handle_lock = PTHREAD_MUTEX_INITIALIZER;
193
194 #define _MULTIPLE_DB_HANDLE
195
196 #ifdef _MULTIPLE_DB_HANDLE
197
198 #define _DISCONNECT_DB                  /* db_util_close(_db_handle); */
199
200 typedef struct
201 {
202         pthread_t       thread_id;
203         sqlite3 *db_handle;
204 } db_handle_t;
205
206 #define MAX_DB_CLIENT 100
207
208 /* static int _db_handle_count = 0; */
209 db_handle_t _db_handle_list[MAX_DB_CLIENT] = {{0, 0}, };
210
211 sqlite3 *emstorage_get_db_handle()
212 {
213         EM_DEBUG_FUNC_BEGIN();
214         int i;
215         pthread_t current_thread_id = THREAD_SELF();
216         sqlite3 *result_db_handle = NULL;
217
218         ENTER_CRITICAL_SECTION(_db_handle_lock);
219         for (i = 0; i < MAX_DB_CLIENT; i++) {
220                 if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
221                         EM_DEBUG_LOG("found db handle at [%d]", i);
222                         result_db_handle = _db_handle_list[i].db_handle;
223                         break;
224                 }
225         }
226         LEAVE_CRITICAL_SECTION(_db_handle_lock);
227
228         if (!result_db_handle)
229                 EM_DEBUG_EXCEPTION("Can't find proper handle for [%d]", current_thread_id);
230
231         EM_DEBUG_FUNC_END();
232         return result_db_handle;
233 }
234
235 int emstorage_set_db_handle(sqlite3 *db_handle)
236 {
237         EM_DEBUG_FUNC_BEGIN();
238         int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
239         pthread_t current_thread_id = THREAD_SELF();
240
241         ENTER_CRITICAL_SECTION(_db_handle_lock);
242         for (i = 0; i < MAX_DB_CLIENT; i++)     {
243                 if (_db_handle_list[i].thread_id == 0) {
244                         _db_handle_list[i].thread_id = current_thread_id;
245                         _db_handle_list[i].db_handle = db_handle;
246                         EM_DEBUG_LOG("current_thread_id [%d], index [%d]", current_thread_id, i);
247                         error_code =  EMAIL_ERROR_NONE;
248                         break;
249                 }
250         }
251         LEAVE_CRITICAL_SECTION(_db_handle_lock);
252
253         if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
254                 EM_DEBUG_EXCEPTION("Exceeded the limitation of db client. Can't find empty slot in _db_handle_list.");
255
256         EM_DEBUG_FUNC_END("error_code [%d]", error_code);
257         return error_code;
258 }
259
260 int emstorage_remove_db_handle()
261 {
262         EM_DEBUG_FUNC_BEGIN();
263         int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
264         ENTER_CRITICAL_SECTION(_db_handle_lock);
265         for (i = 0; i < MAX_DB_CLIENT; i++)
266         {
267                 if (_db_handle_list[i].thread_id == THREAD_SELF())
268                 {
269                         _db_handle_list[i].thread_id = 0;
270                         _db_handle_list[i].db_handle = NULL;
271                         EM_DEBUG_LOG("index [%d]", i);
272                         error_code = EMAIL_ERROR_NONE;
273                         break;
274                 }
275         }
276         LEAVE_CRITICAL_SECTION(_db_handle_lock);
277
278         if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
279                 EM_DEBUG_EXCEPTION("Can't find proper thread_id");
280
281         EM_DEBUG_FUNC_END("error_code [%d]", error_code);
282         return error_code;
283 }
284
285
286 int emstorage_reset_db_handle_list()
287 {
288         EM_DEBUG_FUNC_BEGIN();
289         int i;
290
291         ENTER_CRITICAL_SECTION(_db_handle_lock);
292         for (i = 0; i < MAX_DB_CLIENT; i++)
293         {
294                 _db_handle_list[i].thread_id = 0;
295                 _db_handle_list[i].db_handle = NULL;
296         }
297         LEAVE_CRITICAL_SECTION(_db_handle_lock)
298
299         EM_DEBUG_FUNC_END();
300         return EMAIL_ERROR_NONE;
301 }
302
303 sqlite3 *emstorage_get_db_connection()
304 {
305         sqlite3 *tmp_db_handle = emstorage_get_db_handle();
306         if (NULL == tmp_db_handle)
307                 tmp_db_handle = emstorage_db_open(NULL);
308         return tmp_db_handle;
309 }
310
311
312 #else   /*  _MULTIPLE_DB_HANDLE */
313 #define _DISCONNECT_DB                  /* db_util_close(_db_handle); */
314
315 sqlite3 *_db_handle = NULL;
316
317 sqlite3 *emstorage_get_db_connection()
318 {
319         if (NULL == _db_handle)
320                 emstorage_db_open(NULL);
321         return _db_handle;
322 }
323 #endif  /*  _MULTIPLE_DB_HANDLE */
324
325 /* ------------------------------------------------------------------------------ */
326 /*  Mutex using shared memory */
327 typedef struct
328 {
329         pthread_mutex_t mutex;
330         int data;
331 } mmapped_t;
332
333 mmapped_t       *mapped_for_db_lock = NULL;
334 int              shm_fd_for_db_lock = 0;
335
336 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
337 mmapped_t       *mapped_for_generating_mail_id = NULL;
338 int              shm_fd_for_generating_mail_id = 0;
339 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
340
341 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__
342 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
343         {  _timedlock_shm_mutex(&mapped_for_db_lock, 2); return_value = function_call; _unlockshm_mutex(&mapped_for_db_lock); }
344 #else /*  __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
345 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
346         {  return_value = function_call; }
347 #endif /*  __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
348
349 INTERNAL_FUNC int emstorage_shm_file_init(const char *shm_file_name)
350 {
351         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
352
353         if(!shm_file_name) {
354                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
355                 return EMAIL_ERROR_INVALID_PARAM;
356         }
357
358         int fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); /*  note: permission is not working */
359
360         if (fd > 0) {
361                 fchmod(fd, 0666);
362                 EM_DEBUG_LOG("** Create SHM FILE **");
363                 if (ftruncate(fd, sizeof(mmapped_t)) != 0) {
364                         EM_DEBUG_EXCEPTION("ftruncate failed: %s", strerror(errno));
365                         return EMAIL_ERROR_SYSTEM_FAILURE;
366                 }
367
368                 mmapped_t *m = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
369                 if (m == MAP_FAILED) {
370                         EM_DEBUG_EXCEPTION("mmap failed: %s", strerror(errno));
371                         return EMAIL_ERROR_SYSTEM_FAILURE;
372                 }
373
374                 m->data = 0;
375
376                 pthread_mutexattr_t mattr;
377                 pthread_mutexattr_init(&mattr);
378                 pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
379                 pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
380                 pthread_mutex_init(&(m->mutex), &mattr);
381                 pthread_mutexattr_destroy(&mattr);
382         }
383         else {
384                 EM_DEBUG_EXCEPTION("shm_open failed: %s", strerror(errno));
385                 return EMAIL_ERROR_SYSTEM_FAILURE;
386         }
387         close(fd);
388         EM_DEBUG_FUNC_END();
389         return EMAIL_ERROR_NONE;
390 }
391
392 int emstorage_shm_file_destroy(const char *shm_file_name)
393 {
394         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
395         if(!shm_file_name) {
396                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
397                 return EMAIL_ERROR_INVALID_PARAM;
398         }
399
400         if (shm_unlink(shm_file_name) != 0)
401                 EM_DEBUG_EXCEPTION("shm_unlink failed: %s", strerror(errno));
402         EM_DEBUG_FUNC_END();
403         return EMAIL_ERROR_NONE;
404 }
405
406 static int _initialize_shm_mutex(const char *shm_file_name, int *param_shm_fd, mmapped_t **param_mapped)
407 {
408         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p] param_shm_fd [%p], param_mapped [%p]", shm_file_name, param_shm_fd, param_mapped);
409
410         if(!shm_file_name || !param_shm_fd || !param_mapped) {
411                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
412                 return EMAIL_ERROR_INVALID_PARAM;
413         }
414
415         if (!(*param_mapped)) {
416                 EM_DEBUG_LOG("** mapping begin **");
417
418                 if (!(*param_shm_fd)) { /*  open shm_file_name at first. Otherwise, the num of files in /proc/pid/fd will be increasing  */
419                         *param_shm_fd = shm_open(shm_file_name, O_RDWR, 0);
420                         if ((*param_shm_fd) == -1) {
421                                 EM_DEBUG_EXCEPTION("FAIL: shm_open(): %s", strerror(errno));
422                                 return EMAIL_ERROR_SYSTEM_FAILURE;
423                         }
424                 }
425                 mmapped_t *tmp = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ|PROT_WRITE, MAP_SHARED, (*param_shm_fd), 0);
426
427                 if (tmp == MAP_FAILED) {
428                         EM_DEBUG_EXCEPTION("mmap failed: %s", strerror(errno));
429                         return EMAIL_ERROR_SYSTEM_FAILURE;
430                 }
431                 *param_mapped = tmp;
432         }
433
434         EM_DEBUG_FUNC_END();
435         return EMAIL_ERROR_NONE;
436 }
437
438 static int _timedlock_shm_mutex(mmapped_t **param_mapped, int sec)
439 {
440         EM_DEBUG_FUNC_BEGIN("param_mapped [%p], sec [%d]", param_mapped, sec);
441
442         if(!param_mapped) {
443                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
444                 return EMAIL_ERROR_INVALID_PARAM;
445         }
446
447         struct timespec abs_time;
448         clock_gettime(CLOCK_REALTIME, &abs_time);
449         abs_time.tv_sec += sec;
450
451         int err = pthread_mutex_timedlock(&((*param_mapped)->mutex), &abs_time);
452
453         if (err == EOWNERDEAD) {
454                 err = pthread_mutex_consistent(&((*param_mapped)->mutex));
455                 EM_DEBUG_EXCEPTION("Previous owner is dead with lock. Fix mutex : %s", EM_STRERROR(err));
456         }
457         else if (err != 0) {
458                 EM_DEBUG_EXCEPTION("ERROR : %s", EM_STRERROR(err));
459                 return err;
460         }
461
462         EM_DEBUG_FUNC_END();
463         return EMAIL_ERROR_NONE;
464 }
465
466 void _unlockshm_mutex(mmapped_t **param_mapped)
467 {
468         EM_DEBUG_FUNC_BEGIN();
469         pthread_mutex_unlock(&((*param_mapped)->mutex));
470         EM_DEBUG_FUNC_END();
471 }
472 /* ------------------------------------------------------------------------------ */
473
474
475 static int _open_counter = 0;
476 static int g_transaction = false;
477
478 static int _get_password_file_name(int account_id, char *recv_password_file_name, char *send_password_file_name);
479 static int _read_password_from_secure_storage(char *file_name, char **password);
480
481 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
482 static int _get_cert_password_file_name(int index, char *cert_password_file_name);
483 #endif
484
485 typedef struct {
486         const char *object_name;
487         unsigned int data_flag;
488 } email_db_object_t;
489
490 static const email_db_object_t _g_db_tables[] =
491 {
492         { "mail_read_mail_uid_tbl", 1},
493         { "mail_tbl", 1},
494         { "mail_attachment_tbl", 1},
495         { NULL,  0},
496 };
497
498 static const email_db_object_t _g_db_indexes[] =
499 {
500         { "mail_read_mail_uid_idx1", 1},
501         { "mail_idx1", 1},
502         { "mail_attachment_idx1", 1},
503         { NULL,  1},
504 };
505
506 enum
507 {
508         CREATE_TABLE_MAIL_ACCOUNT_TBL,
509         CREATE_TABLE_MAIL_BOX_TBL,
510         CREATE_TABLE_MAIL_READ_MAIL_UID_TBL,
511         CREATE_TABLE_MAIL_RULE_TBL,
512         CREATE_TABLE_MAIL_TBL,
513         CREATE_TABLE_MAIL_ATTACHMENT_TBL,
514 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
515         CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL,
516 #endif
517         CREATE_TABLE_MAIL_MEETING_TBL,
518 #ifdef __FEATURE_LOCAL_ACTIVITY__
519         CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL,
520 #endif
521         CREATE_TABLE_MAIL_CERTIFICATE_TBL,
522         CREATE_TABLE_MAIL_TASK_TBL,
523 #ifdef __FEATURE_BODY_SEARCH__
524         CREATE_TABLE_MAIL_TEXT_TBL,
525 #endif
526
527         /*CREATE INDEX*/
528         CREATE_TABLE_MAIL_ACCOUNT_IDX,
529         CREATE_TABLE_MAIL_BOX_IDX,
530         CREATE_TABLE_MAIL_READ_MAIL_UID_IDX,
531         CREATE_TABLE_MAIL_IDX,
532         CREATE_TABLE_MAIL_ATTACHMENT_IDX,
533         CREATE_TABLE_MAIL_MEETING_IDX,
534         CREATE_TABLE_MAIL_TASK_IDX,
535         CREATE_TABLE_MAIL_DATETIME_IDX,
536         CREATE_TABLE_MAIL_THREAD_IDX,
537         CREATE_TABLE_MAX,
538 };
539
540 enum
541 {
542         DATA1_IDX_IN_MAIL_ACTIVITY_TBL = 0,
543         TRANSTYPE_IDX_IN_MAIL_ACTIVITY_TBL,
544         FLAG_IDX_IN_MAIL_ACTIVITY_TBL,
545 };
546
547 enum
548 {
549         CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL = 0,
550         ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
551         ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
552         ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
553         EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
554         EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
555         EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
556         ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL,
557         EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL,
558         SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL,
559         FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL,
560 };
561
562 enum
563 {
564         TO_RECIPIENT = 0,
565         CC_RECIPIENT,
566         BCC_RECIPIENT,
567 };
568 enum
569 {
570         ACCOUNT_ID_IDX_IN_MAIL_BOX_TBL = 0,
571         LOCAL_YN_IDX_IN_MAIL_BOX_TBL,
572         MAILBOX_NAME_IDX_IN_MAIL_BOX_TBL,
573         MAILBOX_TYPE_IDX_IN_MAIL_BOX_TBL,
574         ALIAS_IDX_IN_MAIL_BOX_TBL,
575         SYNC_WITH_SERVER_YN_IDX_IN_MAIL_BOX_TBL,
576         MODIFIABLE_YN_IDX_IN_MAIL_BOX_TBL,
577         TOTAL_MAIL_COUNT_ON_SERVER_IDX_IN_MAIL_BOX_TBL,
578         ARCHIVE_IDX_IN_MAIL_BOX_TBL,
579         MAIL_SLOT_SIZE_IDX_IN_MAIL_BOX_TBL,
580 };
581
582 enum
583 {
584         ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL = 0,
585         LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
586         LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
587         MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL,
588         S_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
589         DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL,
590         DATA2_IDX_IN_MAIL_READ_MAIL_UID_TBL,
591         FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL,
592         IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL,          /* unused */
593 };
594
595 #ifdef __FEATURE_BODY_SEARCH__
596 enum
597 {
598         MAIL_ID_IDX_IN_MAIL_TEXT_TBL = 0,
599         ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL,
600         MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL,
601         BODY_TEXT_IDX_IN_MAIL_TEXT_TBL,
602         FIELD_COUNT_OF_MAIL_TEXT_TBL,
603 };
604 #endif
605
606 enum
607 {
608         ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL = 0,
609         RULE_ID_IDX_IN_MAIL_RULE_TBL,
610         TYPE_IDX_IN_MAIL_RULE_TBL,
611         VALUE_IDX_IN_MAIL_RULE_TBL,
612         ACTION_TYPE_IDX_IN_MAIL_RULE_TBL,
613         TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL,
614         FLAG1_IDX_IN_MAIL_RULE_TBL,
615         FLAG2_IDX_IN_MAIL_RULE_TBL,
616 };
617
618 enum
619 {
620         MAIL_ID_IDX_IN_MAIL_TBL = 0,
621         ACCOUNT_ID_IDX_IN_MAIL_TBL,
622         MAILBOX_ID_IDX_IN_MAIL_TBL,
623         MAILBOX_TYPE_IDX_IN_MAIL_TBL,
624         SUBJECT_IDX_IN_MAIL_TBL,
625         DATETIME_IDX_IN_MAIL_TBL,
626         SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL,
627         SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL,
628         SERVER_MAIL_ID_IDX_IN_MAIL_TBL,
629         MESSAGE_ID_IDX_IN_MAIL_TBL,
630         REFERENCE_ID_IDX_IN_MAIL_TBL,
631         FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL,
632         FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL,
633         FULL_ADDRESS_TO_IDX_IN_MAIL_TBL,
634         FULL_ADDRESS_CC_IDX_IN_MAIL_TBL,
635         FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL,
636         FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL,
637         EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL,
638         EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL,
639         ALIAS_SENDER_IDX_IN_MAIL_TBL,
640         ALIAS_RECIPIENT_IDX_IN_MAIL_TBL,
641         BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL,
642         FILE_PATH_PLAIN_IDX_IN_MAIL_TBL,
643         FILE_PATH_HTML_IDX_IN_MAIL_TBL,
644         FILE_PATH_MIME_ENTITY_IDX_IN_MAIL_TBL,
645         MAIL_SIZE_IDX_IN_MAIL_TBL,
646         FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL,
647         FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL,
648         FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL,
649         FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL,
650         FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL,
651         FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL,
652         FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL,
653         DRM_STATUS_IDX_IN_MAIL_TBL,
654         PRIORITY_IDX_IN_MAIL_TBL,
655         SAVE_STATUS_IDX_IN_MAIL_TBL,
656         LOCK_STATUS_IDX_IN_MAIL_TBL,
657         REPORT_STATUS_IDX_IN_MAIL_TBL,
658         ATTACHMENT_COUNT_IDX_IN_MAIL_TBL,
659         INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL,
660         THREAD_ID_IDX_IN_MAIL_TBL,
661         THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL,
662         PREVIEW_TEXT_IDX_IN_MAIL_TBL,
663         MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL,
664         MESSAGE_CLASS_IDX_IN_MAIL_TBL,
665         DIGEST_TYPE_IDX_IN_MAIL_TBL,
666         SMIME_TYPE_IDX_IN_MAIL_TBL,
667         SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL,
668         EAS_DATA_LENGTH_IDX_IN_MAIL_TBL,
669         EAS_DATA_IDX_IN_MAIL_TBL,
670         FIELD_COUNT_OF_MAIL_TBL,  /* End of mail_tbl */
671 };
672
673 enum
674 {
675         ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL = 0,
676         ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL,
677         ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL,
678         ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL,
679         MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
680         ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
681         MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
682         ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
683         ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
684         ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL,
685         ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
686         ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
687 #ifdef __ATTACHMENT_OPTI__
688         ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL,
689         SECTION_IDX_IN_MAIL_ATTACHMENT_TBL,
690 #endif
691 };
692
693 enum {
694         IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL = 0,
695 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
696         MAIL_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
697         ACCOUNT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
698         ADDRESS_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
699         ADDRESS_IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL,
700 #endif
701         ADDRESS_IDX_IN_MAIL_CONTACT_SYNC_TBL,
702         CONTACT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
703         STORAGE_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
704         CONTACT_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
705 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
706         DISPLAY_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
707         FLAG1_IDX_IN_MAIL_CONTACT_SYNC_TBL,
708 #endif
709 };
710
711 /* sowmya.kr 03032010, changes for get list of mails for given addr list */
712 typedef struct _em_mail_id_list {
713         int mail_id;
714         struct _em_mail_id_list *next;
715 } em_mail_id_list;
716
717 static char *g_test_query[] = {
718                 /*  1. select mail_account_tbl */
719                 "SELECT"
720                 " account_name, "
721                 " incoming_server_type, "
722                 " incoming_server_address, "
723                 " user_email_address, "
724                 " incoming_server_user_name, "
725                 " incoming_server_password, "
726                 " retrieval_mode, "
727                 " incoming_server_port_number, "
728                 " incoming_server_secure_connection, "
729                 " outgoing_server_type, "
730                 " outgoing_server_address, "
731                 " outgoing_server_port_number, "
732                 " outgoing_server_need_authentication, "
733                 " outgoing_server_secure_connection, "
734                 " outgoing_server_user_name, "
735                 " outgoing_server_password, "
736                 " display_name, "
737                 " reply_to_addr, "
738                 " return_addr, "
739                 " account_id, "
740                 " keep_mails_on_pop_server_after_download, "
741                 " flag1, "
742                 " flag2, "
743                 " pop_before_smtp, "
744                 " incoming_server_requires_apop"
745                 ", logo_icon_path, "
746                 " is_preset_account, "
747                 " check_interval, "
748                 " priority, "
749                 " keep_local_copy, "
750                 " req_delivery_receipt, "
751                 " req_read_receipt, "
752                 " download_limit, "
753                 " block_address, "
754                 " block_subject, "
755                 " display_name_from, "
756                 " reply_with_body, "
757                 " forward_with_files, "
758                 " add_myname_card, "
759                 " add_signature, "
760                 " signature"
761                 ", add_my_address_to_bcc"
762                 ", account_svc_id "
763                 ", index_color "
764                 ", sync_status "
765                 ", sync_disabled "
766                 ", smime_type"
767                 ", certificate_path"
768                 ", cipher_type"
769                 ", digest_type"
770                 " FROM mail_account_tbl",
771                 /*  2. select mail_box_tbl */
772                 "SELECT "
773                 "   mailbox_id, "
774                 "   account_id, "
775                 "   local_yn,  "
776                 "   mailbox_name,  "
777                 "   mailbox_type,   "
778                 "   alias,  "
779                 "   deleted_flag,  "
780                 "   modifiable_yn,  "
781                 "   total_mail_count_on_server,  "
782                 "   has_archived_mails, "
783                 "   mail_slot_size, "
784                 "   no_select, "
785                 "   last_sync_time "
786                 " FROM mail_box_tbl ",
787                 /*  3. select mail_read_mail_uid_tbl */
788                 "SELECT  "
789                 "   account_id,  "
790                 "   mailbox_id,  "
791                 "   local_uid,  "
792                 "   mailbox_name,  "
793                 "   s_uid,  "
794                 "   data1 ,  "
795                 "   data2,  "
796                 "   flag,  "
797                 "   idx_num "
798                 " FROM mail_read_mail_uid_tbl ",
799                 /*  4. select mail_rule_tbl */
800                 "SELECT "
801                 "   account_id, "
802                 "   rule_id, "
803                 "       type, "
804                 "       value, "
805                 "       action_type, "
806                 "       target_mailbox_id,      "
807                 "       flag1, "
808                 "       flag2 "
809                 " FROM mail_rule_tbl    ",
810                 /*  5. select mail_tbl */
811                 "SELECT"
812                 "       mail_id, "
813                 "       account_id, "
814                 "       mailbox_id, "
815                 "       mailbox_name, "
816                 "   mailbox_type, "
817                 "   subject, "
818                 "       date_time, "
819                 "       server_mail_status, "
820                 "       server_mailbox_name, "
821                 "       server_mail_id, "
822                 "   message_id, "
823                 "       reference_mail_id, "
824                 "   full_address_from, "
825                 "   full_address_reply, "
826                 "   full_address_to, "
827                 "   full_address_cc, "
828                 "   full_address_bcc, "
829                 "   full_address_return, "
830                 "   email_address_sender, "
831                 "   email_address_recipient, "
832                 "   alias_sender, "
833                 "   alias_recipient, "
834                 "       body_download_status, "
835                 "       file_path_plain, "
836                 "       file_path_html, "
837                 "   file_path_mime_entity, "
838                 "       mail_size, "
839                 "   flags_seen_field     ,"
840                 "   flags_deleted_field  ,"
841                 "   flags_flagged_field  ,"
842                 "   flags_answered_field ,"
843                 "   flags_recent_field   ,"
844                 "   flags_draft_field    ,"
845                 "   flags_forwarded_field,"
846                 "       DRM_status, "
847                 "       priority, "
848                 "       save_status, "
849                 "       lock_status, "
850                 "       report_status, "
851                 "   attachment_count, "
852                 "       inline_content_count, "
853                 "       thread_id, "
854                 "       thread_item_count, "
855                 "   preview_text, "
856                 "       meeting_request_status, "
857                 "   message_class, "
858                 "   digest_type, "
859                 "   smime_type "
860                 " FROM mail_tbl",
861                 /*  6. select mail_attachment_tbl */
862                 "SELECT "
863                 "       attachment_id, "
864                 "       attachment_name, "
865                 "       attachment_path, "
866                 "       attachment_size, "
867                 "       mail_id,  "
868                 "       account_id, "
869                 "       mailbox_id, "
870                 "       attachment_save_status,  "
871                 "       attachment_drm_type,  "
872                 "       attachment_drm_method,  "
873                 "       attachment_inline_content_status,  "
874                 "       attachment_mime_type  "
875                 " FROM mail_attachment_tbl ",
876
877 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
878                 "SELECT  "
879                 "   account_id, "
880                 "   mail_id, "
881                 "   server_mail_id, "
882                 "   activity_id, "
883                 "   activity_type, "
884                 "   mailbox_id, "
885                 "   mailbox_name "
886                 " FROM mail_partial_body_activity_tbl ",
887 #endif
888
889                 "SELECT  "
890                 "   mail_id, "
891                 "   account_id, "
892                 "   mailbox_id, "
893                 "   meeting_response, "
894                 "   start_time, "
895                 "   end_time, "
896                 "   location, "
897                 "   global_object_id, "
898                 "   offset, "
899                 "   standard_name, "
900                 "   standard_time_start_date, "
901                 "   standard_bias, "
902                 "   daylight_name, "
903                 "   daylight_time_start_date, "
904                 "   daylight_bias "
905                 " FROM mail_meeting_tbl ",
906
907 #ifdef __FEATURE_LOCAL_ACTIVITY__
908                 "SELECT "
909                 "       activity_id, "
910                 "   account_id, "
911                 "   mail_id, "
912                 "   activity_type, "
913                 "   server_mailid, "
914                 "   src_mbox , "
915                 "       dest_mbox "
916                 " FROM mail_local_activity_tbl  ",
917 #endif
918                 "SELECT "
919                 "       certificate_id, "
920                 "   issue_year, "
921                 "   issue_month, "
922                 "   issue_day, "
923                 "   expiration_year, "
924                 "   expiration_month, "
925                 "   expiration_day, "
926                 "   issue_organization_name, "
927                 "   email_address, "
928                 "   subject_str, "
929                 "   filepath, "
930                 "   password "
931                 " FROM mail_certificate_tbl     ",
932 #ifdef __FEATURE_BODY_SEARCH__
933                 "SELECT "
934                 "       mail_id, "
935                 "   account_id, "
936                 "   mailbox_id, "
937                 "   body_text "
938                 " FROM mail_text_tbl    ",
939 #endif
940                 "SELECT "
941                 "       task_id, "
942                 "   task_type, "
943                 "   task_status, "
944                 "   task_priority, "
945                 "   task_parameter_length, "
946                 "   task_parameter , "
947                 "       date_time "
948                 " FROM mail_task_tbl    ",
949                 NULL,
950 };
951
952
953 static int _get_table_field_data_char(char  **table, char *buf, int index)
954 {
955         if ((table == NULL) || (buf == NULL) || (index < 0))  {
956                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
957                 return false;
958         }
959
960         if (table[index] != NULL) {
961                 *buf = (char)atoi(table[index]);
962                 return true;
963         }
964
965         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
966
967         *buf = 0;
968         return false;
969 }
970
971 static int _get_table_field_data_int(char  **table, int *buf, int index)
972 {
973         if ((table == NULL) || (buf == NULL) || (index < 0))  {
974                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
975                 return false;
976         }
977
978         if (table[index] != NULL) {
979                 *buf = atoi(table[index]);
980                 return true;
981         }
982
983         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
984
985         *buf = 0;
986         return false;
987 }
988
989 static int _get_table_field_data_time_t(char  **table, time_t *buf, int index)
990 {
991         if ((table == NULL) || (buf == NULL) || (index < 0))  {
992                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
993                 return false;
994         }
995
996         if (table[index] != NULL) {
997                 *buf = (time_t)atol(table[index]);
998                 return true;
999         }
1000
1001         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
1002
1003         *buf = 0;
1004         return false;
1005 }
1006
1007 static int _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
1008 {
1009         int ret = false;
1010
1011         if ((table == NULL) || (buf == NULL) || (index < 0))  {
1012                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1013                 return false;
1014         }
1015
1016         char *pTemp = table[index];
1017         int sLen = 0;
1018         if (pTemp == NULL)
1019                 *buf = NULL;
1020         else {
1021                 sLen = EM_SAFE_STRLEN(pTemp);
1022                 if(sLen) {
1023                         *buf = (char *) em_malloc(sLen + 1);
1024                         if (*buf == NULL) {
1025                                 EM_DEBUG_EXCEPTION("malloc is failed");
1026                                 goto FINISH_OFF;
1027                         }
1028                         strncpy(*buf, pTemp, sLen);
1029                 }
1030                 else
1031                         *buf = NULL;
1032         }
1033 #ifdef _PRINT_STORAGE_LOG_
1034         if (*buf)
1035                 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", *buf, index);
1036         else
1037                 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1038 #endif
1039         ret = true;
1040 FINISH_OFF:
1041
1042         return ret;
1043 }
1044
1045 static int _get_table_field_data_string_without_allocation(char **table, char *buf, int buffer_size, int ucs2, int index)
1046 {
1047         if ((table == NULL) || (buf == NULL) || (index < 0))  {
1048                 EM_DEBUG_EXCEPTION(" table[%p], buf[%p], index[%d]", table, buf, index);
1049                         return false;
1050         }
1051
1052         char *pTemp = table[index];
1053
1054         if (pTemp == NULL)
1055                 buf = NULL;
1056         else {
1057                 memset(buf, 0, buffer_size);
1058                 strncpy(buf, pTemp, buffer_size - 1);
1059         }
1060 #ifdef _PRINT_STORAGE_LOG_
1061         if (buf)
1062                 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", buf, index);
1063         else
1064                 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1065 #endif
1066
1067         return true;
1068 }
1069
1070 static int _get_table_field_data_blob(char **table, void **buffer, int buffer_size, int index)
1071 {
1072         if ((table == NULL) || (buffer == NULL) || (index < 0))  {
1073                 EM_DEBUG_EXCEPTION(" table[%p], buffer[%p], buffer_size [%d], index[%d]", table, buffer, buffer_size, index);
1074                 return false;
1075         }
1076
1077         char *temp_buffer = table[index];
1078
1079         if (temp_buffer == NULL)
1080                 buffer = NULL;
1081         else {
1082                 *buffer = malloc(buffer_size);
1083                 if(*buffer == NULL) {
1084                         EM_DEBUG_EXCEPTION("allocation failed.");
1085                         return false;
1086                 }
1087                 memset(*buffer, 0, buffer_size);
1088                 memcpy(*buffer, temp_buffer, buffer_size);
1089         }
1090 #ifdef _PRINT_STORAGE_LOG_
1091         if (buf)
1092                 EM_DEBUG_LOG("_get_table_field_data_string - buffer[%s], index[%d]", buffer, index);
1093         else
1094                 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1095 #endif
1096
1097         return true;
1098 }
1099
1100 static int _get_stmt_field_data_char(DB_STMT hStmt, char *buf, int index)
1101 {
1102         if ((hStmt == NULL) || (buf == NULL) || (index < 0))  {
1103                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1104                 return false;
1105         }
1106
1107         if (sqlite3_column_text(hStmt, index) != NULL) {
1108                 *buf = (char)sqlite3_column_int(hStmt, index);
1109 #ifdef _PRINT_STORAGE_LOG_
1110                         EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1111 #endif
1112                 return true;
1113         }
1114
1115         EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1116
1117         return false;
1118 }
1119
1120 static int _get_stmt_field_data_int(DB_STMT hStmt, int *buf, int index)
1121 {
1122         if ((hStmt == NULL) || (buf == NULL) || (index < 0))  {
1123                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1124                 return false;
1125         }
1126
1127         if (sqlite3_column_text(hStmt, index) != NULL) {
1128                 *buf = sqlite3_column_int(hStmt, index);
1129 #ifdef _PRINT_STORAGE_LOG_
1130                         EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1131 #endif
1132                 return true;
1133         }
1134
1135         EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1136
1137         return false;
1138 }
1139
1140 static int _get_stmt_field_data_time_t(DB_STMT hStmt, time_t *buf, int index)
1141 {
1142         if ((hStmt == NULL) || (buf == NULL) || (index < 0))  {
1143                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1144                 return false;
1145         }
1146
1147         if (sqlite3_column_text(hStmt, index) != NULL) {
1148                 *buf = (time_t)sqlite3_column_int(hStmt, index);
1149 #ifdef _PRINT_STORAGE_LOG_
1150                 EM_DEBUG_LOG("_get_stmt_field_data_time_t [%d]", *buf);
1151 #endif
1152                 return true;
1153         }
1154
1155         EM_DEBUG_LOG("_get_stmt_field_data_time_t fail. index [%d]", index);
1156         return false;
1157 }
1158
1159 static int _get_stmt_field_data_string(DB_STMT hStmt, char **buf, int ucs2, int index)
1160 {
1161         if (!hStmt || !buf || (index < 0))  { /*prevent 39619*/
1162                 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1163                 return false;
1164         }
1165
1166         int sLen = 0;
1167         sLen = sqlite3_column_bytes(hStmt, index);
1168
1169 #ifdef _PRINT_STORAGE_LOG_
1170                 EM_DEBUG_LOG("_get_stmt_field_data_string sqlite3_column_bytes sLen[%d]", sLen);
1171 #endif
1172
1173         if (sLen > 0) {
1174                 *buf = (char *) em_malloc(sLen + 1);
1175                 strncpy(*buf, (char *)sqlite3_column_text(hStmt, index), sLen);
1176         }
1177         else
1178                 *buf = NULL;
1179
1180 #ifdef _PRINT_STORAGE_LOG_
1181         if (*buf)
1182                 EM_DEBUG_LOG("buf[%s], index[%d]", *buf, index);
1183         else
1184                 EM_DEBUG_LOG("_get_stmt_field_data_string - No string got");
1185 #endif
1186
1187         return false;
1188 }
1189
1190 static void _get_stmt_field_data_blob(DB_STMT hStmt, void **buf, int index)
1191 {
1192         if( !hStmt || !buf || (index < 0))  { /*prevent 39618*/
1193                 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1194                 return;
1195         }
1196
1197         int sLen = 0;
1198         sLen = sqlite3_column_bytes(hStmt, index);
1199
1200 #ifdef _PRINT_STORAGE_LOG_
1201         EM_DEBUG_LOG("_get_stmt_field_data_blob sqlite3_column_bytes sLen[%d]", sLen);
1202 #endif
1203
1204         if (sLen > 0) {
1205                 *buf = (char *) em_malloc(sLen);
1206                 memcpy(*buf, (void *)sqlite3_column_blob(hStmt, index), sLen);
1207         }
1208         else
1209                 *buf = NULL;
1210
1211 }
1212
1213 static int _get_stmt_field_data_string_without_allocation(DB_STMT hStmt, char *buf, int buffer_size, int ucs2, int index)
1214 {
1215         if (!hStmt || !buf || (index < 0))  { /*prevent 39620*/
1216                 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], buffer_size[%d], index[%d]", hStmt, buf, buffer_size, index);
1217                 return false;
1218         }
1219
1220         int sLen = 0;
1221         sLen = sqlite3_column_bytes(hStmt, index);
1222
1223 #ifdef _PRINT_STORAGE_LOG_
1224                 EM_DEBUG_LOG("_get_stmt_field_data_string_without_allocation sqlite3_column_bytes sLen[%d]", sLen);
1225 #endif
1226
1227         if (sLen > 0) {
1228                 memset(buf, 0, buffer_size);
1229                 strncpy(buf, (char *)sqlite3_column_text(hStmt, index), buffer_size - 1);
1230         }
1231         else
1232                 strcpy(buf, "");
1233
1234 #ifdef _PRINT_STORAGE_LOG_
1235         EM_DEBUG_LOG("buf[%s], index[%d]", buf, index);
1236 #endif
1237
1238         return false;
1239 }
1240
1241 static int _bind_stmt_field_data_char(DB_STMT hStmt, int index, char value)
1242 {
1243         if ((hStmt == NULL) || (index < 0)) {
1244                 EM_DEBUG_EXCEPTION("index[%d]", index);
1245                 return false;
1246         }
1247
1248         int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1249
1250         if (ret != SQLITE_OK)  {
1251                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1252                 return false;
1253         }
1254
1255         return true;
1256 }
1257
1258 static int _bind_stmt_field_data_int(DB_STMT hStmt, int index, int value)
1259 {
1260         if ((hStmt == NULL) || (index < 0)) {
1261                 EM_DEBUG_EXCEPTION("index[%d]", index);
1262                 return false;
1263         }
1264
1265         int ret = sqlite3_bind_int(hStmt, index+1, value);
1266
1267         if (ret != SQLITE_OK)  {
1268                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1269                 return false;
1270         }
1271
1272         return true;
1273 }
1274
1275 static int _bind_stmt_field_data_time_t(DB_STMT hStmt, int index, time_t value)
1276 {
1277         if ((hStmt == NULL) || (index < 0)) {
1278                 EM_DEBUG_EXCEPTION("index[%d]", index);
1279                 return false;
1280         }
1281
1282         int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1283
1284         if (ret != SQLITE_OK)  {
1285                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1286                 return false;
1287         }
1288
1289         return true;
1290 }
1291
1292 static int _bind_stmt_field_data_string(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1293 {
1294         if ((hStmt == NULL) || (index < 0)) {
1295                 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1296                 return false;
1297         }
1298
1299 #ifdef _PRINT_STORAGE_LOG_
1300         EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1301 #endif
1302
1303         int ret = 0;
1304         if (value != NULL)
1305                 ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1306         else
1307                 ret = sqlite3_bind_text(hStmt, index+1, "", -1, NULL);
1308
1309         if (ret != SQLITE_OK)  {
1310                 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1311                 return false;
1312         }
1313         return true;
1314 }
1315
1316
1317 static int _bind_stmt_field_data_blob(DB_STMT hStmt, int index, void *blob, int blob_size)
1318 {
1319         if ((hStmt == NULL) || (index < 0)) {
1320                 EM_DEBUG_EXCEPTION("index[%d], blob_size[%d]", index, blob_size);
1321                 return false;
1322         }
1323
1324 #ifdef _PRINT_STORAGE_LOG_
1325         EM_DEBUG_LOG("hStmt = %p, index = %d, blob_size = %d, blob = [%p]", hStmt, index, blob_size, blob);
1326 #endif
1327
1328         int ret = 0;
1329         if (blob_size>0)
1330                 ret = sqlite3_bind_blob(hStmt, index+1, blob, blob_size, SQLITE_STATIC);
1331         else
1332                 ret = sqlite3_bind_null(hStmt, index+1);
1333
1334         if (ret != SQLITE_OK)  {
1335                 EM_DEBUG_EXCEPTION("sqlite3_bind_blob fail [%d]", ret);
1336                 return false;
1337         }
1338         return true;
1339 }
1340
1341
1342 static int _delete_temp_file(const char *path)
1343 {
1344         EM_DEBUG_FUNC_BEGIN("path[%p]", path);
1345
1346         DIR *dp = NULL;
1347         struct dirent *entry = NULL;
1348
1349         char buf[1024] = {0x00, };
1350
1351         if ((dp = opendir(path)) == NULL)  {
1352                 EM_DEBUG_EXCEPTION("opendir(\"%s\") failed...", path);
1353                 return false;
1354         }
1355
1356         while ((entry = readdir(dp)) != NULL)  {
1357                 SNPRINTF(buf, sizeof(buf), "%s/%s", path, entry->d_name);
1358                 remove(buf);
1359         }
1360
1361         closedir(dp);
1362         EM_DEBUG_FUNC_END();
1363         return true;
1364 }
1365
1366 char *cpy_str(char *src)
1367 {
1368         char *p = NULL;
1369
1370         if (src)  {
1371                 if (!(p = em_malloc((int)EM_SAFE_STRLEN(src) + 1)))  {
1372                         EM_DEBUG_EXCEPTION("mailoc failed...");
1373                         return NULL;
1374                 }
1375                 strncpy(p, src, EM_SAFE_STRLEN(src));
1376         }
1377
1378         return p;
1379 }
1380
1381 static void _emstorage_close_once(void)
1382 {
1383         EM_DEBUG_FUNC_BEGIN();
1384
1385         DELETE_CRITICAL_SECTION(_transactionBeginLock);
1386         DELETE_CRITICAL_SECTION(_transactionEndLock);
1387
1388         EM_DEBUG_FUNC_END();
1389 }
1390
1391 INTERNAL_FUNC int emstorage_close(int *err_code)
1392 {
1393         EM_DEBUG_FUNC_BEGIN();
1394
1395         int ret = false;
1396         int error = EMAIL_ERROR_NONE;
1397
1398         emstorage_db_close(&error);
1399
1400         if (--_open_counter == 0)
1401                 _emstorage_close_once();
1402
1403         ret = true;
1404
1405         if (err_code != NULL)
1406                 *err_code = error;
1407
1408         EM_DEBUG_FUNC_END("ret [%d]", ret);
1409         return ret;
1410 }
1411
1412 static void *_emstorage_open_once(int *err_code)
1413 {
1414         EM_DEBUG_FUNC_BEGIN();
1415
1416         int error = EMAIL_ERROR_NONE;
1417
1418         mkdir(USERDATA_PATH, DIRECTORY_PERMISSION);
1419         mkdir(DATA_PATH, DIRECTORY_PERMISSION);
1420         mkdir(EMAILPATH, DIRECTORY_PERMISSION);
1421         mkdir(MAILHOME, DIRECTORY_PERMISSION);
1422         mkdir(MAILTEMP, DIRECTORY_PERMISSION);
1423
1424         _delete_temp_file(MAILTEMP);
1425
1426         g_transaction = false;
1427
1428         if (!emstorage_create_table(EMAIL_CREATE_DB_NORMAL, &error)) {
1429                 EM_DEBUG_EXCEPTION(" emstorage_create_table failed - %d", error);
1430                 goto FINISH_OFF;
1431         }
1432
1433 FINISH_OFF:
1434         if (err_code != NULL)
1435                 *err_code = error;
1436
1437
1438         return NULL;
1439 }
1440
1441  /*  pData : a parameter which is registered when busy handler is registerd */
1442  /*  count : retry count */
1443 #define EMAIL_STORAGE_MAX_RETRY_COUNT   20
1444 static int _callback_sqlite_busy_handler(void *pData, int count)
1445 {
1446         EM_DEBUG_LOG("Busy Handler Called!!: [%d]", count);
1447         usleep(200000);   /*   sleep time when SQLITE_LOCK */
1448
1449         /*  retry will be stopped if  busy handler return 0 */
1450         return EMAIL_STORAGE_MAX_RETRY_COUNT - count;
1451 }
1452
1453 static int _delete_all_files_and_directories(int *err_code)
1454 {
1455         EM_DEBUG_FUNC_BEGIN();
1456
1457         int error = EMAIL_ERROR_NONE;
1458         int ret = false;
1459
1460         if (!emstorage_delete_file(EMAIL_SERVICE_DB_FILE_PATH, &error)) {
1461                 if (error != EMAIL_ERROR_FILE_NOT_FOUND) {
1462                         EM_DEBUG_EXCEPTION("remove failed - %s", EMAIL_SERVICE_DB_FILE_PATH);
1463                         goto FINISH_OFF;
1464                 }
1465         }
1466
1467         if (!emstorage_delete_dir(MAILHOME, &error)) {
1468                 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed");
1469                 goto FINISH_OFF;
1470         }
1471
1472         ret = true;
1473
1474 FINISH_OFF:
1475         if (err_code)
1476                 *err_code = error;
1477         EM_DEBUG_FUNC_END();
1478         return ret;
1479 }
1480
1481 static int _recovery_from_malformed_db_file(int *err_code)
1482 {
1483         EM_DEBUG_FUNC_BEGIN();
1484
1485         int error = EMAIL_ERROR_NONE;
1486         int ret = false;
1487
1488         /* Delete all files and directories */
1489         if (!_delete_all_files_and_directories(&error)) {
1490                 EM_DEBUG_EXCEPTION("_delete_all_files_and_directories failed [%d]", error);
1491                 goto FINISH_OFF;
1492         }
1493
1494         /* Delete all accounts on MyAccount */
1495
1496         /* Delete all managed connection to DB */
1497         emstorage_reset_db_handle_list();
1498
1499         ret = true;
1500
1501 FINISH_OFF:
1502         if (err_code)
1503                 *err_code = error;
1504         EM_DEBUG_FUNC_END();
1505         return ret;
1506 }
1507
1508
1509 INTERNAL_FUNC int em_db_open(sqlite3 **sqlite_handle, int *err_code)
1510 {
1511         EM_DEBUG_FUNC_BEGIN();
1512         int rc = 0;
1513         int error = EMAIL_ERROR_NONE;
1514         int ret = false;
1515
1516         EM_DEBUG_LOG("*sqlite_handle[%p]", *sqlite_handle);
1517
1518         if (*sqlite_handle)  { /*prevent 33351*/
1519                 EM_DEBUG_LOG(">>>>> DB Already Opened......");
1520                 if (err_code != NULL)
1521                         *err_code = error;
1522                 return true;
1523         }
1524
1525         /*  db open */
1526         EM_DEBUG_LOG("Open DB");
1527         EMSTORAGE_PROTECTED_FUNC_CALL(db_util_open(EMAIL_SERVICE_DB_FILE_PATH, sqlite_handle, DB_UTIL_REGISTER_HOOK_METHOD), rc);
1528         if (SQLITE_OK != rc) {
1529                 EM_DEBUG_EXCEPTION("db_util_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1530                 error = EMAIL_ERROR_DB_FAILURE;
1531                 db_util_close(*sqlite_handle);
1532                 *sqlite_handle = NULL;
1533
1534                 if (SQLITE_CORRUPT == rc) /* SQLITE_CORRUPT : The database disk image is malformed */ {/* Recovery DB file */
1535                         EM_DEBUG_LOG("The database disk image is malformed. Trying to remove and create database disk image and directories");
1536                         if (!_recovery_from_malformed_db_file(&error)) {
1537                                 EM_DEBUG_EXCEPTION("_recovery_from_malformed_db_file failed [%d]", error);
1538                                 goto FINISH_OFF;
1539                         }
1540
1541                         EM_DEBUG_LOG("Open DB again");
1542                         EMSTORAGE_PROTECTED_FUNC_CALL(db_util_open(EMAIL_SERVICE_DB_FILE_PATH, sqlite_handle, DB_UTIL_REGISTER_HOOK_METHOD), rc);
1543                         if (SQLITE_OK != rc) {
1544                                 EM_DEBUG_EXCEPTION("db_util_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1545                                 error = EMAIL_ERROR_DB_FAILURE;
1546                                 db_util_close(*sqlite_handle);
1547                                 *sqlite_handle = NULL;
1548                                 goto FINISH_OFF; /*prevent 33351*/
1549                         }
1550                 }
1551                 else
1552                         goto FINISH_OFF;
1553         }
1554         EM_DEBUG_LOG(">>>>> DB Handle : *sqlite_handle[%p]", *sqlite_handle);
1555
1556         /* register busy handler */
1557         EM_DEBUG_LOG(">>>>> Register busy handler.....");
1558         rc = sqlite3_busy_handler(*sqlite_handle, _callback_sqlite_busy_handler, NULL);  /*  Busy Handler registration, NULL is a parameter which will be passed to handler */
1559         if (SQLITE_OK != rc) {
1560                 EM_DEBUG_EXCEPTION("sqlite3_busy_handler fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1561                 error = EMAIL_ERROR_DB_FAILURE;
1562                 db_util_close(*sqlite_handle);
1563                 *sqlite_handle = NULL;
1564                 goto FINISH_OFF;
1565         }
1566
1567
1568         ret = true;
1569
1570 FINISH_OFF:
1571         if (err_code != NULL)
1572                 *err_code = error;
1573
1574         EM_DEBUG_FUNC_END("ret [%d]", ret);
1575         return ret;
1576 }
1577
1578 INTERNAL_FUNC sqlite3* emstorage_db_open(int *err_code)
1579 {
1580         EM_DEBUG_FUNC_BEGIN();
1581 #ifdef _MULTIPLE_DB_HANDLE
1582         sqlite3 *_db_handle = NULL;
1583 #endif
1584
1585         int error = EMAIL_ERROR_NONE;
1586
1587         _initialize_shm_mutex(SHM_FILE_FOR_DB_LOCK, &shm_fd_for_db_lock, &mapped_for_db_lock);
1588
1589 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
1590         _initialize_shm_mutex(SHM_FILE_FOR_MAIL_ID_LOCK, &shm_fd_for_generating_mail_id, &mapped_for_generating_mail_id);
1591 #endif /*__FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
1592
1593         if (!em_db_open(&_db_handle, &error)) {
1594                 EM_DEBUG_EXCEPTION("em_db_open failed[%d]", error);
1595                 goto FINISH_OFF;
1596         }
1597
1598 #ifdef _MULTIPLE_DB_HANDLE
1599         emstorage_set_db_handle(_db_handle);
1600 #endif
1601
1602 FINISH_OFF:
1603         if (err_code != NULL)
1604                 *err_code = error;
1605
1606         EM_DEBUG_FUNC_END("ret [%p]", _db_handle);
1607         return _db_handle;
1608 }
1609
1610 INTERNAL_FUNC int emstorage_db_close(int *err_code)
1611 {
1612         EM_DEBUG_FUNC_BEGIN();
1613 #ifdef _MULTIPLE_DB_HANDLE
1614         sqlite3 *_db_handle = emstorage_get_db_handle();
1615 #endif
1616
1617         int error = EMAIL_ERROR_NONE;
1618         int ret = false;
1619
1620         DELETE_CRITICAL_SECTION(_transactionBeginLock);
1621         DELETE_CRITICAL_SECTION(_transactionEndLock);
1622
1623         if (_db_handle) {
1624                 ret = db_util_close(_db_handle);
1625
1626                 if (ret != SQLITE_OK) {
1627                         EM_DEBUG_EXCEPTION(" db_util_close fail - %d", ret);
1628                         error = EMAIL_ERROR_DB_FAILURE;
1629                         ret = false;
1630                         goto FINISH_OFF;
1631                 }
1632 #ifdef _MULTIPLE_DB_HANDLE
1633                 emstorage_remove_db_handle();
1634 #endif
1635                 _db_handle = NULL;
1636         }
1637
1638         ret = true;
1639
1640 FINISH_OFF:
1641         if (err_code != NULL)
1642                 *err_code = error;
1643
1644         EM_DEBUG_FUNC_END("ret [%d]", ret);
1645         return ret;
1646 }
1647
1648
1649 INTERNAL_FUNC int emstorage_open(int *err_code)
1650 {
1651         EM_DEBUG_FUNC_BEGIN();
1652
1653         int ret = false;
1654         int error = EMAIL_ERROR_NONE;
1655
1656         int retValue;
1657
1658         retValue = mkdir(DB_PATH, DIRECTORY_PERMISSION);
1659
1660         EM_DEBUG_LOG("mkdir return- %d", retValue);
1661         EM_DEBUG_LOG("emstorage_open - before db_util_open - pid = %d", getpid());
1662
1663         if (emstorage_db_open(&error) == NULL) {
1664                 EM_DEBUG_EXCEPTION("emstorage_db_open failed[%d]", error);
1665                 goto FINISH_OFF;
1666         }
1667
1668
1669         if (_open_counter++ == 0)
1670                 _emstorage_open_once(&error);
1671
1672         ret = true;
1673
1674 FINISH_OFF:
1675         if (err_code != NULL)
1676                 *err_code = error;
1677
1678         EM_DEBUG_FUNC_END("ret [%d]", ret);
1679         return ret;
1680 }
1681
1682 INTERNAL_FUNC int emstorage_create_table(emstorage_create_db_type_t type, int *err_code)
1683 {
1684         EM_DEBUG_FUNC_BEGIN();
1685
1686         int error = EMAIL_ERROR_NONE;
1687         int rc = -1, ret = false;
1688         int query_len = 0;
1689         char sql_query_string[QUERY_SIZE] = {0, };
1690         char **create_table_query = NULL;
1691
1692         error = emcore_load_query_from_file(EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
1693         if (error != EMAIL_ERROR_NONE) {
1694                 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
1695                 goto FINISH_OFF2;
1696         }
1697
1698         if (query_len < CREATE_TABLE_MAX) {
1699                 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
1700                 error = EMAIL_ERROR_SYSTEM_FAILURE;
1701                 goto FINISH_OFF2;
1702         }
1703
1704         sqlite3 *local_db_handle = emstorage_get_db_connection();
1705         EM_DEBUG_LOG("local_db_handle = %p.", local_db_handle);
1706
1707         char *sql;
1708         char **result;
1709
1710         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_account_tbl';";
1711         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
1712         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1713                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
1714
1715         EM_DEBUG_LOG("emstorage_create_table - result[1] = %s %c", result[1], result[1]);
1716
1717         if (atoi(result[1]) < 1) {
1718                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
1719                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1720                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
1721
1722                 EM_DEBUG_LOG("CREATE TABLE mail_account_tbl");
1723
1724                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
1725
1726                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1727                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1728
1729                 /*  create mail_account_tbl unique index */
1730                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ACCOUNT_IDX]);
1731
1732                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1733                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1734
1735                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
1736
1737         } /*  mail_account_tbl */
1738         else if (type == EMAIL_CREATE_DB_CHECK)  {
1739                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], NULL, NULL, NULL), rc);
1740                 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)));
1741         }
1742
1743         sqlite3_free_table(result);
1744
1745
1746         /*  2. create mail_box_tbl */
1747         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_box_tbl';";
1748
1749         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
1750         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1751                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
1752
1753         if (atoi(result[1]) < 1) {
1754                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
1755                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1756                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
1757
1758                 EM_DEBUG_LOG("CREATE TABLE mail_box_tbl");
1759
1760                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_BOX_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
1761
1762                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1763                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1764                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1765
1766                 /*  create mail_local_mailbox_tbl unique index */
1767                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_BOX_IDX]);
1768
1769                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1770                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1771                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1772
1773                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
1774
1775         } /*  mail_box_tbl */
1776         else if (type == EMAIL_CREATE_DB_CHECK)  {
1777                 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_BOX_TBL], NULL, NULL, NULL);
1778                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1779                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_BOX_TBL], rc, sqlite3_errmsg(local_db_handle)));
1780         }
1781         sqlite3_free_table(result);
1782
1783         /*  3. create mail_read_mail_uid_tbl */
1784         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_read_mail_uid_tbl';";
1785         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
1786         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1787                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
1788
1789
1790         if (atoi(result[1]) < 1) {
1791                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
1792                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1793                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
1794
1795                 EM_DEBUG_LOG("CREATE TABLE mail_read_mail_uid_tbl");
1796
1797                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
1798
1799                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1800                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1801                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1802
1803                 /*  create mail_read_mail_uid_tbl unique index */
1804                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_IDX]);
1805
1806                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1807                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1808                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1809                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
1810
1811         } /*  mail_read_mail_uid_tbl */
1812         else if (type == EMAIL_CREATE_DB_CHECK)  {
1813                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], NULL, NULL, NULL), rc);
1814                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1815                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], rc, sqlite3_errmsg(local_db_handle)));
1816         }
1817         sqlite3_free_table(result);
1818
1819
1820         /*  4. create mail_rule_tbl */
1821         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_rule_tbl';";
1822
1823         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
1824         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1825                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
1826
1827         if (atoi(result[1]) < 1) {
1828                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
1829                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1830                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
1831
1832                 EM_DEBUG_LOG("CREATE TABLE mail_rule_tbl");
1833
1834                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_RULE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
1835
1836                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1837                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1838                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1839                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
1840
1841         } /*  mail_rule_tbl */
1842         else if (type == EMAIL_CREATE_DB_CHECK)  {
1843                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_RULE_TBL], NULL, NULL, NULL), rc);
1844                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1845                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_RULE_TBL], rc, sqlite3_errmsg(local_db_handle)));
1846         }
1847         sqlite3_free_table(result);
1848
1849
1850         /*  5. create mail_tbl */
1851         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_tbl';";
1852         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
1853         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1854                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
1855
1856         if (atoi(result[1]) < 1) {
1857                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
1858                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1859                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
1860                 EM_DEBUG_LOG("CREATE TABLE mail_tbl");
1861
1862                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
1863
1864                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1865                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1866                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1867
1868                 /*  create mail_tbl unique index */
1869                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_IDX]);
1870                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1871                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1872                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1873
1874                 /*  create mail_tbl index for date_time */
1875                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_DATETIME_IDX]);
1876                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1877                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1878                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1879
1880                 /*  create mail_tbl index for thread_item_count */
1881                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_THREAD_IDX]);
1882                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1883                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1884                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1885
1886                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
1887
1888                 /*  just one time call */
1889 /*              EFTSInitFTSIndex(FTS_EMAIL_IDX); */
1890         } /*  mail_tbl */
1891         else if (type == EMAIL_CREATE_DB_CHECK)  {
1892                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TBL], NULL, NULL, NULL), rc);
1893                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1894                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TBL], rc, sqlite3_errmsg(local_db_handle)));
1895         }
1896         sqlite3_free_table(result);
1897
1898
1899         /*  6. create mail_attachment_tbl */
1900         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_attachment_tbl';";
1901         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
1902         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1903                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
1904
1905         if (atoi(result[1]) < 1) {
1906                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
1907                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1908                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
1909                 EM_DEBUG_LOG("CREATE TABLE mail_attachment_tbl");
1910
1911                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
1912
1913                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1914                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1915                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1916
1917                 /*  create mail_attachment_tbl unique index */
1918                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_IDX]);
1919
1920                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1921                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1922                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1923                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
1924
1925         } /*  mail_attachment_tbl */
1926         else if (type == EMAIL_CREATE_DB_CHECK)  {
1927                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], NULL, NULL, NULL), rc);
1928                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1929                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], rc, sqlite3_errmsg(local_db_handle)));
1930         }
1931         sqlite3_free_table(result);
1932
1933 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
1934
1935         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_partial_body_activity_tbl';";
1936         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
1937         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1938                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
1939
1940         if (atoi(result[1]) < 1) {
1941
1942                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
1943                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1944                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
1945
1946                 EM_DEBUG_LOG("CREATE TABLE mail_partial_body_activity_tbl");
1947
1948                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
1949
1950                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1951                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1952                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1953                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
1954
1955         } /*  mail_rule_tbl */
1956         else if (type == EMAIL_CREATE_DB_CHECK)  {
1957                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], NULL, NULL, NULL), rc);
1958                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1959                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
1960         }
1961         sqlite3_free_table(result);
1962
1963 #endif /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
1964
1965         /*  create mail_meeting_tbl */
1966         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_meeting_tbl';";
1967         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
1968         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1969                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
1970
1971         if (atoi(result[1]) < 1) {
1972                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
1973                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1974                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
1975
1976                 EM_DEBUG_LOG("CREATE TABLE mail_meeting_tbl");
1977
1978                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_MEETING_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
1979
1980                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1981                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1982                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1983                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_MEETING_IDX]);
1984
1985                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1986                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1987                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1988                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
1989
1990         } /*  mail_contact_sync_tbl */
1991         else if (type == EMAIL_CREATE_DB_CHECK)  {
1992                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], NULL, NULL, NULL), rc);
1993                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1994                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], rc, sqlite3_errmsg(local_db_handle)));
1995         }
1996         sqlite3_free_table(result);
1997
1998 #ifdef __FEATURE_LOCAL_ACTIVITY__
1999
2000                 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_local_activity_tbl';";
2001                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2002                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2003                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2004
2005                 if (atoi(result[1]) < 1) {
2006
2007                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2008                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2009                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2010
2011                         EM_DEBUG_LOG(" CREATE TABLE mail_local_activity_tbl");
2012
2013                         SNPRINTF(sql_query_string, sizeof(sql_query_string), create_table_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL]);
2014
2015                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2016                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2017                                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2018                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2019
2020                 } /*  mail_rule_tbl */
2021                 else if (type == EMAIL_CREATE_DB_CHECK)  {
2022                         rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL);
2023                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2024                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2025                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2026                 }
2027                 sqlite3_free_table(result);
2028
2029 #endif /*  __FEATURE_LOCAL_ACTIVITY__ */
2030         /*  create mail_certificate_tbl */
2031         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_certificate_tbl';";
2032         /*  rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL);   */
2033         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2034         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)));
2035
2036         if (atoi(result[1]) < 1) {
2037                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2038                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2039
2040                 EM_DEBUG_LOG("CREATE TABLE mail_certificate_tbl");
2041
2042                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2043
2044                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2045                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2046                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2047         } /*  mail_contact_sync_tbl */
2048         else if (type == EMAIL_CREATE_DB_CHECK)  {
2049                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], NULL, NULL, NULL), rc);
2050                 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)));
2051         }
2052
2053         /*  create mail_task_tbl */
2054         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_task_tbl';";
2055         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2056         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)));
2057
2058         if (atoi(result[1]) < 1) {
2059                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2060                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2061
2062                 EM_DEBUG_LOG("CREATE TABLE mail_task_tbl");
2063
2064                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TASK_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2065
2066                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2067                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2068                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_TASK_IDX]);
2069
2070                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2071                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2072                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2073         } /*  mail_task_tbl */
2074         else if (type == EMAIL_CREATE_DB_CHECK)  {
2075                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TASK_TBL], NULL, NULL, NULL), rc);
2076                 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)));
2077         }
2078
2079 #ifdef __FEATURE_BODY_SEARCH__
2080         /*  create mail_text_tbl */
2081         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_text_tbl';";
2082         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2083         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)));
2084
2085         if (atoi(result[1]) < 1) {
2086                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2087                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2088
2089                 EM_DEBUG_LOG("CREATE TABLE mail_text_tbl");
2090
2091                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TEXT_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2092
2093                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2094                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2095
2096                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2097         } /*  mail_text_tbl */
2098         else if (type == EMAIL_CREATE_DB_CHECK)  {
2099                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TEXT_TBL], NULL, NULL, NULL), rc);
2100                 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)));
2101         }
2102 #endif
2103
2104         sqlite3_free_table(result);
2105
2106         ret = true;
2107
2108 FINISH_OFF:
2109         if (ret == true) {
2110                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2111         }
2112         else {
2113                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
2114         }
2115
2116         _DISCONNECT_DB;
2117
2118 FINISH_OFF2:
2119         if (create_table_query) {
2120                 int i = 0;
2121                 for (i = 0; i < query_len; i++) {
2122                         if (create_table_query[i]) {
2123                                 EM_SAFE_FREE(create_table_query[i]);
2124                         }
2125                 }
2126                 EM_SAFE_FREE(create_table_query);
2127         }
2128
2129         if (err_code != NULL)
2130                 *err_code = error;
2131
2132         EM_DEBUG_FUNC_END("ret [%d]", ret);
2133         return ret;
2134 }
2135
2136 /* Query series --------------------------------------------------------------*/
2137
2138 INTERNAL_FUNC int emstorage_query_mail_count(const char *input_conditional_clause, int input_transaction, int *output_total_mail_count, int *output_unseen_mail_count)
2139 {
2140         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);
2141         int rc = -1;
2142         int error = EMAIL_ERROR_NONE;
2143         DB_STMT hStmt = NULL;
2144         char sql_query_string[QUERY_SIZE] = {0, };
2145         char err_msg[1024];
2146         char **result;
2147         sqlite3 *local_db_handle = NULL;
2148
2149         if (!input_conditional_clause || (!output_total_mail_count && !output_unseen_mail_count)) {
2150                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2151                 return EMAIL_ERROR_INVALID_PARAM;
2152         }
2153
2154         memset(&sql_query_string, 0x00, sizeof(sql_query_string));
2155         local_db_handle = emstorage_get_db_connection();
2156
2157         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2158
2159         SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT COUNT(*) FROM mail_tbl");
2160         EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
2161
2162         if (output_total_mail_count)  {
2163                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
2164                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2165                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2166
2167                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2168                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2169                         ("sqlite3_step fail:%d", rc));
2170                 _get_stmt_field_data_int(hStmt, output_total_mail_count, 0);
2171         }
2172
2173         if (output_unseen_mail_count)  {
2174                 EM_SAFE_STRCAT(sql_query_string, " AND flags_seen_field = 0 ");
2175
2176                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
2177                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2178                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2179
2180                 *output_unseen_mail_count = atoi(result[1]);
2181                 sqlite3_free_table(result);
2182         }
2183
2184 FINISH_OFF:
2185
2186         if (hStmt != NULL)  {
2187                 rc = sqlite3_finalize(hStmt);
2188                 if (rc != SQLITE_OK)  {
2189                         EM_DEBUG_LOG("sqlite3_finalize failed [%d] : %s", rc, err_msg);
2190                         error = EMAIL_ERROR_DB_FAILURE;
2191                 }
2192         }
2193
2194         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2195         _DISCONNECT_DB;
2196
2197         EM_DEBUG_FUNC_END("error [%d]", error);
2198         return error;
2199 }
2200
2201 INTERNAL_FUNC int emstorage_query_mail_id_list(const char *input_conditional_clause, int input_transaction, int **output_mail_id_list, int *output_mail_id_count)
2202 {
2203         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);
2204
2205         int      i = 0;
2206         int      count = 0;
2207         int      rc = -1;
2208         int      cur_query = 0;
2209         int      col_index;
2210         int      error = EMAIL_ERROR_NONE;
2211         int     *result_mail_id_list = NULL;
2212         char   **result = NULL;
2213         char     sql_query_string[QUERY_SIZE] = {0, };
2214         sqlite3 *local_db_handle = emstorage_get_db_connection();
2215
2216         EM_IF_NULL_RETURN_VALUE(input_conditional_clause, EMAIL_ERROR_INVALID_PARAM);
2217         EM_IF_NULL_RETURN_VALUE(output_mail_id_list, EMAIL_ERROR_INVALID_PARAM);
2218         EM_IF_NULL_RETURN_VALUE(output_mail_id_count, EMAIL_ERROR_INVALID_PARAM);
2219
2220         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2221
2222         /* Composing query */
2223         SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, "SELECT mail_id FROM mail_tbl ");
2224         EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
2225
2226         EM_DEBUG_LOG("query[%s].", sql_query_string);
2227
2228         /* Performing query */
2229         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2230         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2231                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2232
2233         col_index = 1;
2234
2235         /*  to get mail list */
2236         if (count == 0) {
2237                 EM_DEBUG_EXCEPTION("No mail found...");
2238                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2239                 goto FINISH_OFF;
2240         }
2241
2242         EM_DEBUG_LOG("There are [%d] mails.", count);
2243
2244         if (!(result_mail_id_list = (int*)em_malloc(sizeof(int) * count))) {
2245                 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
2246                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2247                 goto FINISH_OFF;
2248         }
2249
2250         EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2251
2252         for (i = 0; i < count; i++)
2253                 _get_table_field_data_int(result, result_mail_id_list + i, col_index++);
2254
2255         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >> ", count);
2256
2257         *output_mail_id_list  = result_mail_id_list;
2258         *output_mail_id_count = count;
2259
2260 FINISH_OFF:
2261
2262         if(result)
2263                 sqlite3_free_table(result);
2264
2265         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2266         _DISCONNECT_DB;
2267
2268         if(error != EMAIL_ERROR_NONE)
2269                 EM_SAFE_FREE(result_mail_id_list);
2270
2271         EM_DEBUG_FUNC_END("error [%d]", error);
2272         return error;
2273 }
2274
2275 INTERNAL_FUNC int emstorage_query_mail_list(const char *conditional_clause, int transaction, email_mail_list_item_t** result_mail_list,  int *result_count,  int *err_code)
2276 {
2277         EM_DEBUG_FUNC_BEGIN();
2278         EM_PROFILE_BEGIN(emstorage_query_mail_list_func);
2279
2280         int i = 0, count = 0, rc = -1, to_get_count = (result_mail_list)?0:1;
2281         int local_inline_content_count = 0, local_attachment_count = 0;
2282         int cur_query = 0, base_count = 0, col_index;
2283         int ret = false, error = EMAIL_ERROR_NONE;
2284         char *date_time_string = NULL;
2285         char **result = NULL, sql_query_string[QUERY_SIZE] = {0, };
2286         char *field_list = "mail_id, account_id, mailbox_id, full_address_from, email_address_sender, full_address_to, subject, body_download_status, 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, eas_data_length, eas_data ";
2287         email_mail_list_item_t *mail_list_item_from_tbl = NULL;
2288         sqlite3 *local_db_handle = emstorage_get_db_connection();
2289
2290         EM_IF_NULL_RETURN_VALUE(conditional_clause, false);
2291         EM_IF_NULL_RETURN_VALUE(result_count, false);
2292
2293         EMSTORAGE_START_READ_TRANSACTION(transaction);
2294
2295         /*  select clause */
2296         if (to_get_count) /*  count only */
2297                 cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, "SELECT mail_id FROM mail_tbl");
2298         else /*  mail list in plain form */
2299                 cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, "SELECT %s FROM mail_tbl ", field_list);
2300
2301         /* cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, conditional_clause); This code caused some crashes.*/
2302         strncat(sql_query_string, conditional_clause, QUERY_SIZE - cur_query);
2303
2304         EM_DEBUG_LOG("emstorage_query_mail_list : query[%s].", sql_query_string);
2305
2306         /*  performing query            */
2307         EM_PROFILE_BEGIN(emstorage_query_mail_list_performing_query);
2308         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2309         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2310                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2311         EM_PROFILE_END(emstorage_query_mail_list_performing_query);
2312
2313         if (!base_count)
2314                 base_count = ({ int i=0; char *tmp = field_list; for (i=0; tmp && *(tmp + 1); tmp = index(tmp + 1, ','), i++); i; });
2315
2316         col_index = base_count;
2317
2318         EM_DEBUG_LOG("base_count [%d]", base_count);
2319
2320         if (to_get_count) {
2321                 /*  to get count */
2322                 if (!count) {
2323                         EM_DEBUG_EXCEPTION("No mail found...");
2324                         ret = false;
2325                         error= EMAIL_ERROR_MAIL_NOT_FOUND;
2326                         goto FINISH_OFF;
2327                 }
2328                 EM_DEBUG_LOG("There are [%d] mails.", count);
2329         }
2330         else {
2331                 /*  to get mail list */
2332                 if (!count) {
2333                         EM_DEBUG_EXCEPTION("No mail found...");
2334                         ret = false;
2335                         error= EMAIL_ERROR_MAIL_NOT_FOUND;
2336                         goto FINISH_OFF;
2337                 }
2338
2339                 EM_DEBUG_LOG("There are [%d] mails.", count);
2340                 if (!(mail_list_item_from_tbl = (email_mail_list_item_t*)em_malloc(sizeof(email_mail_list_item_t) * count))) {
2341                         EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
2342                         error = EMAIL_ERROR_OUT_OF_MEMORY;
2343                         goto FINISH_OFF;
2344                 }
2345
2346                 EM_PROFILE_BEGIN(emstorage_query_mail_list_loop);
2347                 EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2348                 for (i = 0; i < count; i++) {
2349                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_id), col_index++);
2350                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].account_id), col_index++);
2351                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mailbox_id), col_index++);
2352                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].full_address_from, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2353                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_sender, MAX_EMAIL_ADDRESS_LENGTH, 1, col_index++);
2354                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_recipient, STRING_LENGTH_FOR_DISPLAY,  1, col_index++);
2355                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].subject, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2356                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].body_download_status), col_index++);
2357                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_seen_field), col_index++);
2358                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_deleted_field), col_index++);
2359                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_flagged_field), col_index++);
2360                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_answered_field), col_index++);
2361                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_recent_field), col_index++);
2362                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_draft_field), col_index++);
2363                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_forwarded_field), col_index++);
2364                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].DRM_status), col_index++);
2365                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].priority), col_index++);
2366                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].save_status), col_index++);
2367                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].lock_status), col_index++);
2368                         _get_table_field_data_int(result, &local_attachment_count, col_index++);
2369                         _get_table_field_data_int(result, &local_inline_content_count, col_index++);
2370                         _get_table_field_data_time_t(result, &(mail_list_item_from_tbl[i].date_time), col_index++);
2371                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].preview_text, MAX_PREVIEW_TEXT_LENGTH, 1, col_index++);
2372                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_id), col_index++);
2373                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_item_count), col_index++);
2374                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].meeting_request_status), col_index++);
2375                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].message_class), col_index++);
2376                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].smime_type), col_index++);
2377                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].scheduled_sending_time), col_index++);
2378                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].eas_data_length), col_index++);
2379                         _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++);
2380
2381                         mail_list_item_from_tbl[i].attachment_count = ((local_attachment_count - local_inline_content_count)>0)?1:0;
2382                 }
2383                 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >> ", count);
2384                 EM_PROFILE_END(emstorage_query_mail_list_loop);
2385         }
2386
2387         sqlite3_free_table(result);
2388         result = NULL;
2389         ret = true;
2390
2391 FINISH_OFF:
2392         EM_DEBUG_LOG("COUNT [%d]", count);
2393
2394         if (to_get_count)
2395                 *result_count = count;
2396         else {
2397                 if (ret == true)  {
2398                         if (result_mail_list)
2399                                 *result_mail_list = mail_list_item_from_tbl;
2400                         *result_count = count;
2401                 }
2402                 else
2403                         EM_SAFE_FREE(mail_list_item_from_tbl);
2404         }
2405
2406         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2407
2408         sqlite3_release_memory(-1);
2409
2410         _DISCONNECT_DB;
2411
2412         EM_SAFE_FREE(date_time_string);
2413
2414         if (err_code != NULL)
2415                 *err_code = error;
2416
2417         EM_PROFILE_END(emstorage_query_mail_list_func);
2418         EM_DEBUG_FUNC_END("ret [%d]", ret);
2419         return ret;
2420 }
2421
2422
2423 INTERNAL_FUNC int emstorage_query_mail_tbl(const char *conditional_clause, int transaction, emstorage_mail_tbl_t** result_mail_tbl, int *result_count, int *err_code)
2424 {
2425         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);
2426
2427         if (!conditional_clause) {
2428                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2429
2430                 if (err_code != NULL)
2431                         *err_code = EMAIL_ERROR_INVALID_PARAM;
2432                 return false;
2433         }
2434
2435         int i, col_index = FIELD_COUNT_OF_MAIL_TBL, rc, ret = false, count;
2436         int error = EMAIL_ERROR_NONE;
2437         char **result = NULL, sql_query_string[QUERY_SIZE] = {0, };
2438         emstorage_mail_tbl_t* p_data_tbl = NULL;
2439         sqlite3 *local_db_handle = emstorage_get_db_connection();
2440
2441         EMSTORAGE_START_READ_TRANSACTION(transaction);
2442
2443         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl %s", conditional_clause);
2444
2445         EM_DEBUG_LOG("Query [%s]", sql_query_string);
2446
2447         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2448         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2449                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2450
2451         if (!count) {
2452                 EM_DEBUG_EXCEPTION("No mail found...");
2453                 ret = false;
2454                 error= EMAIL_ERROR_MAIL_NOT_FOUND;
2455                 goto FINISH_OFF;
2456         }
2457
2458         EM_DEBUG_LOG("There are [%d] mails.", count);
2459         if (!(p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * count))) {
2460                 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
2461                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2462                 goto FINISH_OFF;
2463         }
2464
2465         EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2466         for (i = 0; i < count; i++) {
2467                 _get_table_field_data_int   (result, &(p_data_tbl[i].mail_id), col_index++);
2468                 _get_table_field_data_int   (result, &(p_data_tbl[i].account_id), col_index++);
2469                 _get_table_field_data_int   (result, &(p_data_tbl[i].mailbox_id), col_index++);
2470                 _get_table_field_data_int   (result, &(p_data_tbl[i].mailbox_type), col_index++);
2471                 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 1, col_index++);
2472                 _get_table_field_data_time_t (result, &(p_data_tbl[i].date_time), col_index++);
2473                 _get_table_field_data_int   (result, &(p_data_tbl[i].server_mail_status), col_index++);
2474                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
2475                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
2476                 _get_table_field_data_string(result, &(p_data_tbl[i].message_id), 0, col_index++);
2477                 _get_table_field_data_int   (result, &(p_data_tbl[i].reference_mail_id), col_index++);
2478                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_from), 1, col_index++);
2479                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_reply), 1, col_index++);
2480                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_to), 1, col_index++);
2481                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_cc), 1, col_index++);
2482                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_bcc), 1, col_index++);
2483                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_return), 1, col_index++);
2484                 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_sender), 1, col_index++);
2485                 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_recipient), 1, col_index++);
2486                 _get_table_field_data_string(result, &(p_data_tbl[i].alias_sender), 1, col_index++);
2487                 _get_table_field_data_string(result, &(p_data_tbl[i].alias_recipient), 1, col_index++);
2488                 _get_table_field_data_int   (result, &(p_data_tbl[i].body_download_status), col_index++);
2489                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
2490                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
2491                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
2492                 _get_table_field_data_int   (result, &(p_data_tbl[i].mail_size), col_index++);
2493                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_seen_field), col_index++);
2494                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_deleted_field), col_index++);
2495                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_flagged_field), col_index++);
2496                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_answered_field), col_index++);
2497                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_recent_field), col_index++);
2498                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_draft_field), col_index++);
2499                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_forwarded_field), col_index++);
2500                 _get_table_field_data_int   (result, &(p_data_tbl[i].DRM_status), col_index++);
2501                 _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].priority), col_index++);
2502                 _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].save_status), col_index++);
2503                 _get_table_field_data_int   (result, &(p_data_tbl[i].lock_status), col_index++);
2504                 _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].report_status), col_index++);
2505                 _get_table_field_data_int   (result, &(p_data_tbl[i].attachment_count), col_index++);
2506                 _get_table_field_data_int   (result, &(p_data_tbl[i].inline_content_count), col_index++);
2507                 _get_table_field_data_int   (result, &(p_data_tbl[i].thread_id), col_index++);
2508                 _get_table_field_data_int   (result, &(p_data_tbl[i].thread_item_count), col_index++);
2509                 _get_table_field_data_string(result, &(p_data_tbl[i].preview_text), 1, col_index++);
2510                 _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].meeting_request_status), col_index++);
2511                 _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].message_class), col_index++);
2512                 _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].digest_type), col_index++);
2513                 _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].smime_type), col_index++);
2514                 _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].scheduled_sending_time), col_index++);
2515                 _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
2516                 _get_table_field_data_blob  (result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
2517                 /*  check real body file... */
2518                 if (p_data_tbl[i].body_download_status) {
2519                         struct stat buf;
2520
2521                         if (p_data_tbl[i].file_path_html && EM_SAFE_STRLEN(p_data_tbl[i].file_path_html) > 0) {
2522                                 if (stat(p_data_tbl[i].file_path_html, &buf) == -1) {
2523                                         EM_DEBUG_LINE;
2524                                         p_data_tbl[i].body_download_status = 0;
2525                                 }
2526                         }
2527                         else if (p_data_tbl[i].file_path_plain && EM_SAFE_STRLEN(p_data_tbl[i].file_path_plain) > 0) {
2528                                 if (stat(p_data_tbl[i].file_path_plain, &buf) == -1){
2529                                         EM_DEBUG_LINE;
2530                                         p_data_tbl[i].body_download_status = 0;
2531                                 }
2532                         }
2533                         else
2534                                 p_data_tbl[i].body_download_status = 0;
2535                 }
2536         }
2537
2538         ret = true;
2539
2540 FINISH_OFF:
2541         if(result)
2542                 sqlite3_free_table(result);
2543
2544         if (ret == true)  {
2545                 if (result_mail_tbl)
2546                         *result_mail_tbl = p_data_tbl;
2547                 *result_count = count;
2548         }
2549         else
2550                 EM_SAFE_FREE(p_data_tbl);
2551
2552         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2553
2554         sqlite3_release_memory(-1);
2555
2556         _DISCONNECT_DB;
2557
2558         if (err_code != NULL)
2559                 *err_code = error;
2560
2561         EM_DEBUG_FUNC_END("ret [%d]", ret);
2562         return ret;
2563 }
2564
2565 #ifdef __FEATURE_BODY_SEARCH__
2566 INTERNAL_FUNC int emstorage_query_mail_text_tbl(const char *conditional_clause, int transaction, emstorage_mail_text_tbl_t** result_mail_text_tbl, int *result_count, int *err_code)
2567 {
2568         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);
2569
2570         if (!conditional_clause) {
2571                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2572                 if (err_code != NULL)
2573                         *err_code = EMAIL_ERROR_INVALID_PARAM;
2574                 return false;
2575         }
2576
2577         int i = 0;
2578         int rc = 0;
2579         int ret = false;
2580         int count = 0;
2581         int col_index = FIELD_COUNT_OF_MAIL_TEXT_TBL;
2582         int error = EMAIL_ERROR_NONE;
2583         char **result = NULL;
2584         char sql_query_string[QUERY_SIZE] = {0, };
2585         emstorage_mail_text_tbl_t* p_data_tbl = NULL;
2586         sqlite3 *local_db_handle = emstorage_get_db_connection();
2587
2588         EMSTORAGE_START_READ_TRANSACTION(transaction);
2589
2590         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_text_tbl %s", conditional_clause);
2591
2592         EM_DEBUG_LOG("Query [%s]", sql_query_string);
2593
2594         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2595         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2596                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2597
2598         if (!count) {
2599                 EM_DEBUG_EXCEPTION("No mail found...");
2600                 ret = false;
2601                 error= EMAIL_ERROR_MAIL_NOT_FOUND;
2602                 goto FINISH_OFF;
2603         }
2604
2605         EM_DEBUG_LOG("There are [%d] mails.", count);
2606         if (!(p_data_tbl = (emstorage_mail_text_tbl_t *)em_malloc(sizeof(emstorage_mail_text_tbl_t) * count))) {
2607                 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_text_tbl_t failed...");
2608                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2609                 goto FINISH_OFF;
2610         }
2611
2612         EM_DEBUG_LOG(">>>> DATA ASSIGN START >>");
2613         for (i = 0; i < count; i++) {
2614                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
2615                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
2616                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
2617                 _get_table_field_data_string(result, &(p_data_tbl[i].body_text), 0, col_index++);
2618         }
2619
2620         ret = true;
2621
2622 FINISH_OFF:
2623         if(result)
2624                 sqlite3_free_table(result);
2625
2626         if (ret == true) {
2627                 if (result_mail_text_tbl)
2628                         *result_mail_text_tbl = p_data_tbl;
2629                 *result_count = count;
2630         }
2631         else
2632                 EM_SAFE_FREE(p_data_tbl);
2633
2634         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2635
2636         sqlite3_release_memory(-1);
2637
2638         _DISCONNECT_DB;
2639
2640         if (err_code != NULL)
2641                 *err_code = error;
2642
2643         EM_DEBUG_FUNC_END("ret [%d]", ret);
2644         return ret;
2645 }
2646 #endif
2647
2648 INTERNAL_FUNC int emstorage_query_mailbox_tbl(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)
2649 {
2650         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);
2651
2652         int i = 0;
2653         int rc;
2654         int count = 0;
2655         int col_index = 0;
2656         int error = EMAIL_ERROR_NONE;
2657         int read_count = 0;
2658         int total_count = 0;
2659         char **result;
2660         char sql_query_string[QUERY_SIZE] = {0, };
2661         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 ";
2662         emstorage_mailbox_tbl_t* p_data_tbl = NULL;
2663         sqlite3 *local_db_handle = emstorage_get_db_connection();
2664
2665         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2666
2667         if (input_get_mail_count == 0) {        /* without mail count */
2668                 col_index = 13;
2669                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT %s FROM mail_box_tbl AS MBT %s", fields, input_conditional_clause);
2670         }
2671         else {  /* with read count and total count */
2672                 col_index = 15;
2673                 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 %s GROUP BY mailbox_id) AS MT ON MBT.mailbox_id = MT.mailbox_id %s %s", fields, input_conditional_clause, input_conditional_clause, input_ordering_clause);
2674         }
2675
2676         EM_DEBUG_LOG("query[%s]", sql_query_string);
2677
2678         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
2679         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2680                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)))
2681
2682         EM_DEBUG_LOG("result count [%d]", count);
2683
2684         if(count == 0) {
2685                 EM_DEBUG_EXCEPTION("Can't find mailbox");
2686                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
2687                 goto FINISH_OFF;
2688         }
2689
2690         if ((p_data_tbl = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t) * count)) == NULL) {
2691                 EM_DEBUG_EXCEPTION("em_malloc failed...");
2692                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2693                 goto FINISH_OFF;
2694         }
2695
2696         for (i = 0; i < count; i++)  {
2697                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
2698                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
2699                 _get_table_field_data_int(result, &(p_data_tbl[i].local_yn), col_index++);
2700                 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
2701                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].mailbox_type), col_index++);
2702                 _get_table_field_data_string(result, &(p_data_tbl[i].alias), 0, col_index++);
2703                 _get_table_field_data_int(result, &(p_data_tbl[i].deleted_flag), col_index++);
2704                 _get_table_field_data_int(result, &(p_data_tbl[i].modifiable_yn), col_index++);
2705                 _get_table_field_data_int(result, &(p_data_tbl[i].total_mail_count_on_server), col_index++);
2706                 _get_table_field_data_int(result, &(p_data_tbl[i].has_archived_mails), col_index++);
2707                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_slot_size), col_index++);
2708                 _get_table_field_data_int(result, &(p_data_tbl[i].no_select), col_index++);
2709                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].last_sync_time), col_index++);
2710
2711                 if (input_get_mail_count == 1) {
2712                         _get_table_field_data_int(result, &(total_count), col_index++);
2713                         p_data_tbl[i].total_mail_count_on_local = total_count;
2714                         _get_table_field_data_int(result, &(read_count), col_index++);
2715                         p_data_tbl[i].unread_count = total_count - read_count;                  /*  return unread count, NOT  */
2716                 }
2717         }
2718
2719
2720 FINISH_OFF:
2721         if (result)
2722                 sqlite3_free_table(result);
2723
2724         if (error == EMAIL_ERROR_NONE)  {
2725                 *output_mailbox_list = p_data_tbl;
2726                 *output_mailbox_count = count;
2727                 EM_DEBUG_LOG("Mailbox Count [ %d]", count);
2728         }
2729
2730         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2731
2732         sqlite3_release_memory(-1);
2733
2734         _DISCONNECT_DB;
2735
2736         EM_DEBUG_FUNC_END("error [%d]", error);
2737         return error;
2738 }
2739
2740 /* Query series --------------------------------------------------------------*/
2741
2742 INTERNAL_FUNC int emstorage_check_duplicated_account(email_account_t* account, int transaction, int *err_code)
2743 {
2744         EM_DEBUG_FUNC_BEGIN();
2745
2746         if (!account)  {
2747                 if (err_code != NULL)
2748                         *err_code = EMAIL_ERROR_INVALID_PARAM;
2749                 return false;
2750         }
2751
2752         int rc = -1, ret = false;
2753         int error = EMAIL_ERROR_NONE;
2754         char **result;
2755         int count;
2756         char sql_query_string[QUERY_SIZE] = {0, };
2757
2758         sqlite3 *local_db_handle = emstorage_get_db_connection();
2759         EMSTORAGE_START_READ_TRANSACTION(transaction);
2760
2761         SNPRINTF(sql_query_string, sizeof(sql_query_string),
2762                 "SELECT COUNT(*) FROM mail_account_tbl "
2763                 " WHERE "
2764                 " user_email_address = '%s' AND "
2765                 " incoming_server_user_name = '%s' AND "
2766                 " incoming_server_type = %d AND "
2767                 " incoming_server_address = '%s' AND "
2768                 " outgoing_server_user_name = '%s' AND "
2769                 " outgoing_server_type = %d AND "
2770                 " outgoing_server_address = '%s'; ",
2771                 account->user_email_address,
2772                 account->incoming_server_user_name, account->incoming_server_type, account->incoming_server_address,
2773                 account->outgoing_server_user_name, account->outgoing_server_type, account->outgoing_server_address
2774         );
2775         EM_DEBUG_LOG("Query[%s]", sql_query_string);
2776         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
2777         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2778                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2779
2780         count = atoi(result[1]);
2781         sqlite3_free_table(result);
2782
2783         EM_DEBUG_LOG("Count of Duplicated Account Information: [%d]", count);
2784
2785         if (count == 0) {       /*  not duplicated account */
2786                 ret = true;
2787                 EM_DEBUG_LOG("NOT duplicated account: user_email_address[%s]", account->user_email_address);
2788         }
2789         else {  /*  duplicated account */
2790                 ret = false;
2791                 EM_DEBUG_LOG("The same account already exists. Duplicated account: user_email_address[%s]", account->user_email_address);
2792                 error = EMAIL_ERROR_ALREADY_EXISTS;
2793         }
2794
2795 FINISH_OFF:
2796
2797         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2798         _DISCONNECT_DB;
2799
2800         if (err_code != NULL)
2801                 *err_code = error;
2802
2803         EM_DEBUG_FUNC_END("ret [%d]", ret);
2804         return ret;
2805
2806 }
2807
2808 INTERNAL_FUNC int emstorage_get_account_count(int *count, int transaction, int *err_code)
2809 {
2810         EM_DEBUG_FUNC_BEGIN();
2811
2812         if (!count)  {
2813                 if (err_code != NULL)
2814                         *err_code = EMAIL_ERROR_INVALID_PARAM;
2815                 return false;
2816         }
2817
2818         int rc = -1, ret = false;
2819         int error = EMAIL_ERROR_NONE;
2820
2821         DB_STMT hStmt = NULL;
2822         char sql_query_string[QUERY_SIZE] = {0, };
2823         char err_msg[1024];
2824
2825         sqlite3 *local_db_handle = emstorage_get_db_connection();
2826         EMSTORAGE_START_READ_TRANSACTION(transaction);
2827         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_account_tbl");
2828         EM_DEBUG_LOG("SQL STMT [ %s ]", sql_query_string);
2829         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
2830         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
2831         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2832                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2833
2834         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2835         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2836                 ("sqlite3_step fail:%d", rc));
2837
2838         *count = sqlite3_column_int(hStmt, 0);
2839
2840         ret = true;
2841
2842 FINISH_OFF:
2843
2844         if (hStmt != NULL)  {
2845                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
2846
2847                 rc = sqlite3_finalize(hStmt);
2848                 hStmt=NULL;
2849                 if (rc != SQLITE_OK)  {
2850                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d: %s", rc, err_msg);
2851                         error = EMAIL_ERROR_DB_FAILURE;
2852                 }
2853                 EM_DEBUG_LOG("sqlite3_finalize- %d", rc);
2854         }
2855
2856         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2857         _DISCONNECT_DB;
2858
2859         if (err_code != NULL)
2860                 *err_code = error;
2861
2862         EM_DEBUG_FUNC_END("ret [%d]", ret);
2863         return ret;
2864 }
2865
2866
2867 INTERNAL_FUNC int emstorage_get_account_list(int *select_num, emstorage_account_tbl_t** account_list, int transaction, int with_password, int *err_code)
2868 {
2869         EM_DEBUG_FUNC_BEGIN();
2870
2871         int i = 0, count = 0, rc = -1, ret = false;
2872         int field_index = 0;
2873         int error = EMAIL_ERROR_NONE;
2874         emstorage_account_tbl_t *p_data_tbl = NULL;
2875         char sql_query_string[QUERY_SIZE] = {0, };
2876         char *sql = "SELECT count(*) FROM mail_account_tbl;";
2877         char **result;
2878         sqlite3 *local_db_handle = NULL;
2879         DB_STMT hStmt = NULL;
2880
2881         if (!select_num || !account_list)  {
2882                 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, account_list);
2883                 if (err_code != NULL)
2884                         *err_code = EMAIL_ERROR_INVALID_PARAM;
2885                 return false;
2886         }
2887
2888         local_db_handle = emstorage_get_db_connection();
2889         EMSTORAGE_START_READ_TRANSACTION(transaction);
2890
2891         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2892         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2893                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2894
2895         count = atoi(result[1]);
2896         sqlite3_free_table(result);
2897
2898         EM_DEBUG_LOG("count = %d", rc);
2899
2900         if (count == 0) {
2901                 EM_DEBUG_EXCEPTION("no account found...");
2902                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
2903                 ret = true;
2904                 goto FINISH_OFF;
2905         }
2906         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_account_tbl ORDER BY account_id");
2907
2908         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
2909
2910         EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
2911         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2912                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2913
2914         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2915         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2916                 ("sqlite3_step fail:%d", rc));
2917         if (rc == SQLITE_DONE)  {
2918                 EM_DEBUG_EXCEPTION("no account found...");
2919
2920                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
2921                 count = 0;
2922                 ret = true;
2923                 goto FINISH_OFF;
2924         }
2925
2926         if (!(p_data_tbl = (emstorage_account_tbl_t*)malloc(sizeof(emstorage_account_tbl_t) * count)))  {
2927                 EM_DEBUG_EXCEPTION("malloc failed...");
2928                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2929                 goto FINISH_OFF;
2930         }
2931         memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * count);
2932         for (i = 0; i < count; i++)  {
2933                 /*  get recordset */
2934                 field_index = 0;
2935                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_id), field_index++);
2936                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].account_name), 0, field_index++);
2937                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].logo_icon_path), 0, field_index++);
2938                 _get_stmt_field_data_blob(hStmt, &(p_data_tbl[i].user_data), field_index++);
2939                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].user_data_length), field_index++);
2940                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_svc_id), field_index++);
2941                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_status), field_index++);
2942                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_disabled), field_index++);
2943                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].default_mail_slot_size), field_index++);
2944                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_display_name), 0, field_index++);
2945                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_email_address), 0, field_index++);
2946                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].reply_to_address), 0, field_index++);
2947                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].return_address), 0, field_index++);
2948                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].incoming_server_type), field_index++);
2949                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_address), 0, field_index++);
2950                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_port_number), field_index++);
2951                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_user_name), 0, field_index++);
2952                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_password), 0, field_index++);
2953                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_secure_connection), field_index++);
2954                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].retrieval_mode), field_index++);
2955                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].keep_mails_on_pop_server_after_download), field_index++);
2956                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].check_interval), field_index++);
2957                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_download_size), field_index++);
2958                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].outgoing_server_type), field_index++);
2959                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_address), 0, field_index++);
2960                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_port_number), field_index++);
2961                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_user_name), 0, field_index++);
2962                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_password), 0, field_index++);
2963                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_secure_connection), field_index++);
2964                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_need_authentication), field_index++);
2965                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_use_same_authenticator), field_index++);
2966                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.priority), field_index++);
2967                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.keep_local_copy), field_index++);
2968                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_delivery_receipt), field_index++);
2969                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_read_receipt), field_index++);
2970                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.download_limit), field_index++);
2971                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_address), field_index++);
2972                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_subject), field_index++);
2973                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.display_name_from), 0, field_index++);
2974                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.reply_with_body), field_index++);
2975                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.forward_with_files), field_index++);
2976                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_myname_card), field_index++);
2977                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_signature), field_index++);
2978                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.signature), 0, field_index++);
2979                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.add_my_address_to_bcc), field_index++);
2980                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].pop_before_smtp), field_index++);
2981                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_requires_apop), field_index++);
2982                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].smime_type), field_index++);
2983                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].certificate_path), 0, field_index++);
2984                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].cipher_type), field_index++);
2985                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].digest_type), field_index++);
2986
2987                 if (with_password == true) {
2988                         /*  get password from the secure storage */
2989                         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
2990                         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
2991
2992                         EM_SAFE_FREE(p_data_tbl[i].incoming_server_password);
2993                         EM_SAFE_FREE(p_data_tbl[i].outgoing_server_password);
2994
2995                         /*  get password file name */
2996                         if ((error = _get_password_file_name(p_data_tbl[i].account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
2997                                 EM_DEBUG_EXCEPTION("_get_password_file_name failed. [%d]", error);
2998                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
2999                                 goto FINISH_OFF;
3000                         }
3001
3002                         /*  read password from secure storage */
3003                         if ((error = _read_password_from_secure_storage(recv_password_file_name, &(p_data_tbl[i].incoming_server_password))) < 0 ) {
3004                                 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()  failed. [%d]", error);
3005                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3006                                 goto FINISH_OFF;
3007                         }
3008
3009                         if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl[i].outgoing_server_password))) < 0) {
3010                                 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()  failed. [%d]", error);
3011                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3012                                 goto FINISH_OFF;
3013                         }
3014                 }
3015
3016                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3017                 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i,  rc);
3018                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3019                         ("sqlite3_step fail:%d", rc));
3020         }
3021
3022         ret = true;
3023
3024 FINISH_OFF:
3025         if (ret == true)  {
3026                 *account_list = p_data_tbl;
3027                 *select_num = count;
3028                 EM_DEBUG_LOG("COUNT : %d", count);
3029         }
3030         else if (p_data_tbl != NULL)
3031                 emstorage_free_account(&p_data_tbl, count, NULL);
3032
3033         if (hStmt != NULL)  {
3034                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
3035
3036                 rc = sqlite3_finalize(hStmt);
3037                 hStmt = NULL;
3038                 if (rc != SQLITE_OK)  {
3039                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
3040                         error = EMAIL_ERROR_DB_FAILURE;
3041                 }
3042         }
3043
3044         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3045         _DISCONNECT_DB;
3046
3047         if (err_code != NULL)
3048                 *err_code = error;
3049
3050         EM_DEBUG_FUNC_END("ret [%d]", ret);
3051         return ret;
3052 }
3053
3054
3055 INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(int account_id, char *server_mail_id, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
3056 {
3057         EM_DEBUG_FUNC_BEGIN("account_id [%d], server_mail_id[%s], mail[%p], transaction[%d], err_code[%p]", account_id, server_mail_id, mail, transaction, err_code);
3058
3059         int ret = false, error = EMAIL_ERROR_NONE, result_count;
3060         char conditional_clause[QUERY_SIZE] = {0, };
3061         emstorage_mail_tbl_t* p_data_tbl = NULL;
3062
3063         if (!server_mail_id || !mail) {
3064                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3065                 error = EMAIL_ERROR_INVALID_PARAM;
3066                 goto FINISH_OFF;
3067         }
3068
3069         if (account_id == ALL_ACCOUNT)
3070                 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE UPPER(server_mail_id) =UPPER('%s')", server_mail_id);
3071         else
3072                 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE UPPER(server_mail_id) =UPPER('%s') AND account_id = %d", server_mail_id, account_id);
3073
3074         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
3075
3076         if(!emstorage_query_mail_tbl(conditional_clause, transaction, &p_data_tbl, &result_count, &error)) {
3077                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3078                 goto FINISH_OFF;
3079         }
3080
3081         ret = true;
3082
3083 FINISH_OFF:
3084         if (ret == true)
3085                 *mail = p_data_tbl;
3086
3087         if (err_code != NULL)
3088                 *err_code = error;
3089
3090         EM_DEBUG_FUNC_END("ret [%d]", ret);
3091         return ret;
3092 }
3093
3094
3095 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)
3096 {
3097         int cur_clause = 0, conditional_clause_count = 0, i = 0;
3098
3099         if (account_id < ALL_ACCOUNT) {
3100                 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3101                 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3102         }
3103
3104         /*  where clause */
3105         if (account_id == ALL_ACCOUNT) {
3106                 cur_clause += (conditional_clause_count++ == 0)?
3107                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_type not in (3, 5, 7, 8)"):
3108                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_type not in (3, 5, 7, 8)");
3109         }
3110         else {
3111                 cur_clause += (conditional_clause_count++ == 0)?
3112                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE account_id = %d", account_id):
3113                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND account_id = %d", account_id);
3114         }
3115
3116         if (mailbox_id > 0) {
3117                 cur_clause += (conditional_clause_count++ == 0)?
3118                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_id = %d", mailbox_id):
3119                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_id = %d", mailbox_id);
3120         }
3121         else if(account_id != ALL_ACCOUNT) {
3122                 cur_clause += (conditional_clause_count++ == 0)?
3123                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_type not in (3, 5, 7, 8)"):
3124                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_type not in (3, 5, 7, 8)");
3125         }
3126
3127         if (thread_id > 0) {
3128                 cur_clause += (conditional_clause_count++ == 0)?
3129                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_id = %d ", thread_id):
3130                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_id = %d ", thread_id);
3131         }
3132         else if (thread_id == EMAIL_LIST_TYPE_THREAD)   {
3133                 cur_clause += (conditional_clause_count++ == 0)?
3134                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_item_count > 0"):
3135                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_item_count > 0");
3136         }
3137         else if (thread_id == EMAIL_LIST_TYPE_LOCAL) {
3138                 cur_clause += (conditional_clause_count++ == 0)?
3139                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE server_mail_status = 0"):
3140                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND server_mail_status = 0");
3141         }
3142         else if (thread_id == EMAIL_LIST_TYPE_UNREAD) {
3143                 cur_clause += (conditional_clause_count++ == 0)?
3144                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_seen_field == 0"):
3145                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_seen_field == 0");
3146         }
3147
3148         /*  EM_DEBUG_LOG("where clause added [%s]", conditional_clause_string); */
3149         if (addr_list && addr_list->address_count > 0) {
3150                 if (!addr_list->address_type) {
3151                         cur_clause += (conditional_clause_count++ == 0)?
3152                                 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE email_address_sender IN(\"%s\"", addr_list->address_list[0]):
3153                                 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND email_address_sender IN(\"%s\"", addr_list->address_list[0]);
3154
3155                         for (i = 1; i < addr_list->address_count; i++)
3156                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3157
3158                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3159                 } else {
3160                         cur_clause += (conditional_clause_count++ == 0)?
3161                                 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]):
3162                                 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]);
3163
3164                         for (i = 1; i < addr_list->address_count; i++)
3165                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3166
3167                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3168                 }
3169         }
3170
3171         if (input_except_delete_flagged_mails) {
3172                 cur_clause += (conditional_clause_count++ == 0)?
3173                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_deleted_field = 0"):
3174                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_deleted_field = 0");
3175         }
3176
3177         if (search_value) {
3178                 switch (search_type) {
3179                         case EMAIL_SEARCH_FILTER_SUBJECT:
3180                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3181                                         " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3182                                 break;
3183                         case EMAIL_SEARCH_FILTER_SENDER:
3184                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3185                                         " %s  ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3186                                         ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3187                                 break;
3188                         case EMAIL_SEARCH_FILTER_RECIPIENT:
3189                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3190                                         " %s ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3191                                         "       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3192                                         "       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3193                                         ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value);
3194                                 break;
3195                         case EMAIL_SEARCH_FILTER_ALL:
3196                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3197                                         " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\' "
3198                                         "       OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3199                                         "                       OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3200                                         "                       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3201                                         "                       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3202                                         "               ) "
3203                                         "       )"
3204                                         ")", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value, search_value, search_value);
3205                                 break;
3206                 }
3207         }
3208
3209         /*  EM_DEBUG_LOG("where clause [%s]", conditional_clause_string); */
3210         static char sorting_str[][50] = {
3211                          " ORDER BY date_time DESC",                                             /* case EMAIL_SORT_DATETIME_HIGH: */
3212                          " ORDER BY date_time ASC",                                               /* case EMAIL_SORT_DATETIME_LOW: */
3213                          " ORDER BY full_address_from DESC, date_time DESC", /* case EMAIL_SORT_SENDER_HIGH: */
3214                          " ORDER BY full_address_from ASC, date_time DESC",  /* case EMAIL_SORT_SENDER_LOW: */
3215                          " ORDER BY full_address_to DESC, date_time DESC",   /* case EMAIL_SORT_RCPT_HIGH: */
3216                          " ORDER BY full_address_to ASC, date_time DESC",       /* case EMAIL_SORT_RCPT_LOW: */
3217                          " ORDER BY subject DESC, date_time DESC",                 /* case EMAIL_SORT_SUBJECT_HIGH: */
3218                          " ORDER BY subject ASC, date_time DESC",                       /* case EMAIL_SORT_SUBJECT_LOW: */
3219                          " ORDER BY priority DESC, date_time DESC",               /* case EMAIL_SORT_PRIORITY_HIGH: */
3220                          " ORDER BY priority ASC, date_time DESC",                 /* case EMAIL_SORT_PRIORITY_LOW: */
3221                          " ORDER BY attachment_count DESC, date_time DESC",  /* case EMAIL_SORT_ATTACHMENT_HIGH: */
3222                          " ORDER BY attachment_count ASC, date_time DESC",   /* case EMAIL_SORT_ATTACHMENT_LOW: */
3223                          " ORDER BY lock_status DESC, date_time DESC",     /* case EMAIL_SORT_FAVORITE_HIGH: */
3224                          " ORDER BY lock_status ASC, date_time DESC",           /* case EMAIL_SORT_FAVORITE_LOW: */
3225                          " ORDER BY mailbox_name DESC, date_time DESC",   /* case EMAIL_SORT_MAILBOX_NAME_HIGH: */
3226                          " ORDER BY mailbox_name ASC, date_time DESC"           /* case EMAIL_SORT_MAILBOX_NAME_LOW: */
3227                          };
3228
3229         if (sorting < EMAIL_SORT_END && sorting >= 0)
3230                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " %s", sorting_str[sorting]);
3231         else
3232                 EM_DEBUG_LOG(" Invalid Sorting order ");
3233
3234         /*  limit clause */
3235         if (start_index != -1 && limit_count != -1)
3236                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " LIMIT %d, %d", start_index, limit_count);
3237
3238         return true;
3239 }
3240
3241
3242 /**
3243   *     emstorage_get_mail_list - Get the mail list information.
3244   *
3245   *
3246   */
3247 INTERNAL_FUNC int emstorage_get_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, int transaction, email_mail_list_item_t** mail_list,  int *result_count,  int *err_code)
3248 {
3249         EM_DEBUG_FUNC_BEGIN();
3250         EM_PROFILE_BEGIN(emstorage_get_mail_list_func);
3251
3252         int ret = false, error = EMAIL_ERROR_NONE;
3253         char conditional_clause_string[QUERY_SIZE] = { 0, };
3254
3255         if (account_id < ALL_ACCOUNT) {
3256                 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3257                 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3258         }
3259         EM_IF_NULL_RETURN_VALUE(result_count, false);
3260
3261         _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);
3262
3263         EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
3264
3265         if(!emstorage_query_mail_list(conditional_clause_string, transaction, mail_list, result_count, &error)) {
3266                 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3267                 goto FINISH_OFF;
3268         }
3269
3270         ret = true;
3271
3272 FINISH_OFF:
3273         if (err_code != NULL)
3274                 *err_code = error;
3275
3276         EM_PROFILE_END(emstorage_get_mail_list_func);
3277         EM_DEBUG_FUNC_END("ret [%d]", ret);
3278         return ret;
3279 }
3280
3281
3282 /**
3283   *     emstorage_get_mails - Get the Mail list information based on mailbox_name name
3284   *
3285   *
3286   */
3287 INTERNAL_FUNC int emstorage_get_mails(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)
3288 {
3289         EM_DEBUG_FUNC_BEGIN();
3290         EM_PROFILE_BEGIN(emStorageGetMails);
3291
3292         int count = 0, ret = false, error = EMAIL_ERROR_NONE;
3293         emstorage_mail_tbl_t *p_data_tbl = NULL;
3294         char conditional_clause_string[QUERY_SIZE] = {0, };
3295
3296         EM_IF_NULL_RETURN_VALUE(mail_list, false);
3297         EM_IF_NULL_RETURN_VALUE(result_count, false);
3298
3299         if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3300                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3301                 if (err_code != NULL)
3302                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3303                 return false;
3304         }
3305
3306         _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);
3307
3308         EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
3309
3310         if(!emstorage_query_mail_tbl(conditional_clause_string, transaction, &p_data_tbl, &count,  &error)) {
3311                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3312                 goto FINISH_OFF;
3313         }
3314
3315         ret = true;
3316
3317 FINISH_OFF:
3318         if (ret == true) {
3319                 *mail_list = p_data_tbl;
3320                 *result_count = count;
3321                 EM_DEBUG_LOG("COUNT : %d", count);
3322         }
3323         else if (p_data_tbl != NULL)
3324                 emstorage_free_mail(&p_data_tbl, count, NULL);
3325
3326         if (err_code != NULL)
3327                 *err_code = error;
3328
3329         EM_PROFILE_END(emStorageGetMails);
3330         EM_DEBUG_FUNC_END("ret [%d]", ret);
3331         return ret;
3332 }
3333
3334
3335
3336 /**
3337   *     emstorage_get_searched_mail_list - Get the mail list information after filtering
3338   *
3339   *
3340   */
3341 INTERNAL_FUNC int emstorage_get_searched_mail_list(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)
3342 {
3343         EM_DEBUG_FUNC_BEGIN();
3344
3345         int ret = false, error = EMAIL_ERROR_NONE;
3346         char conditional_clause[QUERY_SIZE] = {0, };
3347         char *temp_search_value = NULL;
3348         char *temp_search_value2 = NULL;
3349
3350         EM_IF_NULL_RETURN_VALUE(mail_list, false);
3351         EM_IF_NULL_RETURN_VALUE(result_count, false);
3352
3353         if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3354                 EM_DEBUG_EXCEPTION("select_num[%p], Mail_list[%p]", result_count, mail_list);
3355                 error = EMAIL_ERROR_INVALID_PARAM;
3356                 goto FINISH_OFF;
3357         }
3358
3359         temp_search_value = em_replace_all_string((char*)search_value, "_", "\\_");
3360         temp_search_value2 = em_replace_all_string(temp_search_value, "%", "\\%");
3361
3362         _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);
3363
3364         EM_DEBUG_LOG("conditional_clause[%s]", conditional_clause);
3365
3366         if(!emstorage_query_mail_list(conditional_clause, transaction, mail_list, result_count, &error)) {
3367                 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3368                 goto FINISH_OFF;
3369         }
3370
3371         ret = true;
3372
3373 FINISH_OFF:
3374         EM_DEBUG_LOG("emstorage_get_searched_mail_list finish off");
3375
3376         if (err_code != NULL)
3377                 *err_code = error;
3378
3379         EM_SAFE_FREE(temp_search_value);
3380         EM_SAFE_FREE(temp_search_value2);
3381
3382         EM_DEBUG_FUNC_END("ret [%d]", ret);
3383         return ret;
3384 }
3385
3386
3387 static int _get_password_file_name(int account_id, char *recv_password_file_name, char *send_password_file_name)
3388 {
3389         EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
3390
3391         if (account_id <= 0 || !recv_password_file_name || !send_password_file_name)  {
3392                 EM_DEBUG_EXCEPTION("Invalid parameter");
3393                 return EMAIL_ERROR_INVALID_PARAM;
3394         }
3395
3396         sprintf(recv_password_file_name, ".email_account_%d_recv", account_id);
3397         sprintf(send_password_file_name, ".email_account_%d_send", account_id);
3398         EM_DEBUG_FUNC_END();
3399         return EMAIL_ERROR_NONE;
3400 }
3401
3402 static int _read_password_from_secure_storage(char *file_name, char **password)
3403 {
3404         EM_DEBUG_FUNC_BEGIN("file_name[%s], password[%p]", file_name, password);
3405
3406         if (!file_name || !password) {
3407                 EM_DEBUG_EXCEPTION("Invalid Parameter");
3408                 return EMAIL_ERROR_INVALID_PARAM;
3409         }
3410
3411         size_t buf_len = 0, read_len = 0;
3412         ssm_file_info_t sfi;
3413         char *temp_password = NULL;
3414         int error_code_from_ssm = 0;
3415         int ret = EMAIL_ERROR_NONE;
3416
3417         if ( (error_code_from_ssm = ssm_getinfo(file_name, &sfi, SSM_FLAG_SECRET_OPERATION, NULL)) < 0) {
3418                 EM_DEBUG_EXCEPTION("ssm_getinfo() failed. [%d]", error_code_from_ssm);
3419                 ret = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3420                 goto FINISH_OFF;
3421         }
3422
3423         buf_len = sfi.originSize;
3424         EM_DEBUG_LOG("password buf_len[%d]", buf_len);
3425         if ((temp_password = (char *)malloc(buf_len + 1)) == NULL) {
3426                 EM_DEBUG_EXCEPTION("malloc failed...");
3427                 ret = EMAIL_ERROR_OUT_OF_MEMORY;
3428                 goto FINISH_OFF;
3429         }
3430         memset(temp_password, 0x00, buf_len + 1);
3431
3432         if ( (error_code_from_ssm = ssm_read(file_name, temp_password, buf_len, &read_len, SSM_FLAG_SECRET_OPERATION, NULL)) < 0) {
3433                 EM_DEBUG_EXCEPTION("ssm_read() failed. [%d]", error_code_from_ssm);
3434                 ret = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3435                 goto FINISH_OFF;
3436         }
3437
3438         EM_DEBUG_LOG("password_file_name[%s], password[%s], originSize[%d], read len[%d]", file_name,  temp_password, sfi.originSize, read_len);
3439
3440         *password = temp_password;
3441         temp_password = NULL;
3442
3443 FINISH_OFF:
3444         EM_SAFE_FREE(temp_password);
3445
3446         EM_DEBUG_FUNC_END("ret [%d]", ret);
3447         return ret;
3448 }
3449
3450
3451 INTERNAL_FUNC int emstorage_get_account_by_id(int account_id, int pulloption, emstorage_account_tbl_t **account, int transaction, int *err_code)
3452 {
3453         EM_DEBUG_FUNC_BEGIN("account_id[%d], pulloption[%d], account[%p], transaction[%d], err_code[%p]", account_id, pulloption, account, transaction, err_code);
3454
3455         if (!account)  {
3456                 EM_DEBUG_EXCEPTION("account_id[%d], account[%p]", account_id, account);
3457                 if (err_code != NULL)
3458                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3459                 return false;
3460         }
3461
3462         int ret = false;
3463         int error = EMAIL_ERROR_NONE;
3464         emstorage_account_tbl_t* p_data_tbl = NULL;
3465         DB_STMT hStmt = NULL;
3466         char sql_query_string[QUERY_SIZE] = {0, };
3467         int rc = -1;
3468         int sql_len = 0;
3469         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3470         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3471
3472         sqlite3 *local_db_handle = emstorage_get_db_connection();
3473         EMSTORAGE_START_READ_TRANSACTION(transaction);
3474
3475         /*  Make query string */
3476         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
3477         sql_len = EM_SAFE_STRLEN(sql_query_string);
3478
3479         if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
3480                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
3481                         "incoming_server_type,"
3482                         "incoming_server_address,"
3483                         "user_email_address,"
3484                         "incoming_server_user_name,"
3485                         "retrieval_mode,"
3486                         "incoming_server_port_number,"
3487                         "incoming_server_secure_connection,"
3488                         "outgoing_server_type,"
3489                         "outgoing_server_address,"
3490                         "outgoing_server_port_number,"
3491                         "outgoing_server_need_authentication,"
3492                         "outgoing_server_secure_connection,"
3493                         "outgoing_server_user_name,"
3494                         "user_display_name,"
3495                         "reply_to_address,"
3496                         "return_address,"
3497                         "account_id,"
3498                         "keep_mails_on_pop_server_after_download,"
3499                         "auto_download_size,"
3500                         "outgoing_server_use_same_authenticator,"
3501                         "pop_before_smtp,"
3502                         "incoming_server_requires_apop,"
3503                         "logo_icon_path,"
3504                         "user_data,"
3505                         "user_data_length,"
3506                         "check_interval,"
3507                         "sync_status,");
3508                 sql_len = EM_SAFE_STRLEN(sql_query_string);
3509         }
3510
3511         if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME) {
3512                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " account_name, ");
3513                 sql_len = EM_SAFE_STRLEN(sql_query_string);
3514         }
3515
3516         /*  get from secure storage, not from db */
3517         if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
3518                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
3519                         "priority,"
3520                         "keep_local_copy,"
3521                         "req_delivery_receipt,"
3522                         "req_read_receipt,"
3523                         "download_limit,"
3524                         "block_address,"
3525                         "block_subject,"
3526                         "display_name_from,"
3527                         "reply_with_body,"
3528                         "forward_with_files,"
3529                         "add_myname_card,"
3530                         "add_signature,"
3531                         "signature,"
3532                         "add_my_address_to_bcc,"
3533                         "account_svc_id,"
3534                         "sync_disabled,"
3535                         "default_mail_slot_size,"
3536                         "smime_type,"
3537                         "certificate_path,"
3538                         "cipher_type,"
3539                         "digest_type,");
3540
3541                 sql_len = EM_SAFE_STRLEN(sql_query_string);
3542         }
3543
3544         /*  dummy value, FROM WHERE clause */
3545         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "0 FROM mail_account_tbl WHERE account_id = %d", account_id);
3546
3547         /*  FROM clause */
3548         EM_DEBUG_LOG("query = [%s]", sql_query_string);
3549
3550         /*  execute a sql and count rows */
3551         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3552         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3553                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3554
3555         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3556         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3557                 ("sqlite3_step fail:%d", rc));
3558
3559         if (rc == SQLITE_DONE)  {
3560                 EM_DEBUG_EXCEPTION("no matched account found...");
3561                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3562                 goto FINISH_OFF;
3563         }
3564
3565         /*  Assign query result to structure */
3566         if (!(p_data_tbl = (emstorage_account_tbl_t *)malloc(sizeof(emstorage_account_tbl_t) * 1)))  {
3567                 EM_DEBUG_EXCEPTION("malloc failed...");
3568                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3569                 goto FINISH_OFF;
3570         }
3571
3572         memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * 1);
3573         int col_index = 0;
3574
3575         if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
3576                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_type), col_index++);
3577                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_address),  0, col_index++);
3578                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_email_address), 0, col_index++);
3579                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_user_name), 0, col_index++);
3580                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->retrieval_mode), col_index++);
3581                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_port_number), col_index++);
3582                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_secure_connection), col_index++);
3583                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->outgoing_server_type), col_index++);
3584                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_address), 0, col_index++);
3585                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_port_number), col_index++);
3586                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_need_authentication), col_index++);
3587                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_secure_connection), col_index++);
3588                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_user_name), 0, col_index++);
3589                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_display_name), 0, col_index++);
3590                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->reply_to_address), 0, col_index++);
3591                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->return_address), 0, col_index++);
3592                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
3593                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->keep_mails_on_pop_server_after_download), col_index++);
3594                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_download_size), col_index++);
3595                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_use_same_authenticator), col_index++);
3596                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->pop_before_smtp), col_index++);
3597                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_requires_apop), col_index++);
3598                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->logo_icon_path), 0, col_index++);
3599                 _get_stmt_field_data_blob(hStmt, &p_data_tbl->user_data, col_index++);
3600                 _get_stmt_field_data_int(hStmt, &p_data_tbl->user_data_length, col_index++);
3601                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->check_interval), col_index++);
3602                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->sync_status), col_index++);
3603         }
3604
3605         if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME)
3606                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->account_name), 0, col_index++);
3607
3608         if (pulloption & EMAIL_ACC_GET_OPT_PASSWORD) {
3609                 /*  get password file name */
3610                 if ((error = _get_password_file_name(p_data_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
3611                         EM_DEBUG_EXCEPTION("_get_password_file_name failed. [%d]", error);
3612                         goto FINISH_OFF;
3613                 }
3614
3615                 /*  read password from secure storage */
3616                 if ((error = _read_password_from_secure_storage(recv_password_file_name, &(p_data_tbl->incoming_server_password))) < 0) {
3617                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed. [%d]", error);
3618                         goto FINISH_OFF;
3619                 }
3620
3621                 EM_DEBUG_LOG("recv_password_file_name[%s], password[%s]", recv_password_file_name,  p_data_tbl->incoming_server_password);
3622
3623                 if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl->outgoing_server_password))) < 0) {
3624                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed. [%d]", error);
3625                         goto FINISH_OFF;
3626                 }
3627                 EM_DEBUG_LOG("send_password_file_name[%s], password[%s]", send_password_file_name,  p_data_tbl->outgoing_server_password);
3628         }
3629
3630         if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
3631                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.priority), col_index++);
3632                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.keep_local_copy), col_index++);
3633                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_delivery_receipt), col_index++);
3634                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_read_receipt), col_index++);
3635                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.download_limit), col_index++);
3636                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_address), col_index++);
3637                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_subject), col_index++);
3638                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.display_name_from), 0, col_index++);
3639                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.reply_with_body), col_index++);
3640                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.forward_with_files), col_index++);
3641                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_myname_card), col_index++);
3642                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_signature), col_index++);
3643                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.signature), 0, col_index++);
3644                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.add_my_address_to_bcc), col_index++);
3645                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_svc_id), col_index++);
3646                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->sync_disabled), col_index++);
3647                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->default_mail_slot_size), col_index++);
3648                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->smime_type), col_index++);
3649                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->certificate_path), 0, col_index++);
3650                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->cipher_type), col_index++);
3651                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->digest_type), col_index++);
3652         }
3653
3654         ret = true;
3655
3656 FINISH_OFF:
3657         if (ret == true)
3658                 *account = p_data_tbl;
3659         else {
3660                 if (p_data_tbl)
3661                         emstorage_free_account((emstorage_account_tbl_t **)&p_data_tbl, 1, NULL);
3662         }
3663         if (hStmt != NULL)  {
3664                 rc = sqlite3_finalize(hStmt);
3665                 if (rc != SQLITE_OK)  {
3666                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
3667                         error = EMAIL_ERROR_DB_FAILURE;
3668                 }
3669         }
3670
3671         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3672         _DISCONNECT_DB;
3673
3674         if (err_code != NULL)
3675                 *err_code = error;
3676
3677         EM_DEBUG_FUNC_END("ret [%d]", ret);
3678         return ret;
3679 }
3680
3681 INTERNAL_FUNC int emstorage_get_password_length_of_account(int account_id, int *password_length, int *err_code)
3682 {
3683         EM_DEBUG_FUNC_BEGIN("account_id[%d], password_length[%p], err_code[%p]", account_id, password_length, err_code);
3684
3685         if (account_id <= 0 || password_length == NULL)  {
3686                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3687                 if (err_code != NULL)
3688                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3689                 return false;
3690         }
3691
3692         int ret = false;
3693         int error = EMAIL_ERROR_NONE;
3694         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3695         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3696         char *temp_password = NULL;
3697
3698
3699         /*  get password file name */
3700         if ((error = _get_password_file_name(account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
3701                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
3702                 goto FINISH_OFF;
3703         }
3704
3705         /*  read password from secure storage */
3706         if ((error = _read_password_from_secure_storage(recv_password_file_name, &temp_password)) < 0 || !temp_password) {
3707                 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed...");
3708                 goto FINISH_OFF;
3709         }
3710
3711         *password_length = EM_SAFE_STRLEN(temp_password);
3712
3713         EM_DEBUG_LOG("recv_password_file_name[%s], *password_length[%d]", recv_password_file_name,  *password_length);
3714
3715         ret = true;
3716
3717 FINISH_OFF:
3718         EM_SAFE_FREE(temp_password);
3719
3720         if (err_code != NULL)
3721                 *err_code = error;
3722
3723         EM_DEBUG_FUNC_END("ret [%d]", ret);
3724         return ret;
3725 }
3726
3727 INTERNAL_FUNC int emstorage_update_account(int account_id, emstorage_account_tbl_t* account_tbl, int transaction, int *err_code)
3728 {
3729         EM_DEBUG_FUNC_BEGIN("account_id[%d], account[%p], transaction[%d], err_code[%p]", account_id, account_tbl, transaction, err_code);
3730
3731         if (account_id < FIRST_ACCOUNT_ID || !account_tbl)  {
3732                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3733                 if (err_code != NULL)
3734                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3735                 return false;
3736         }
3737
3738         int error = EMAIL_ERROR_NONE;
3739         int rc, ret = false;
3740
3741         DB_STMT hStmt = NULL;
3742         char sql_query_string[QUERY_SIZE] = {0, };
3743         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3744         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3745
3746         sqlite3 *local_db_handle = emstorage_get_db_connection();
3747         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
3748
3749         SNPRINTF(sql_query_string, sizeof(sql_query_string),
3750                 "UPDATE mail_account_tbl SET"
3751                 "  account_name = ?"
3752                 ", logo_icon_path = ?"
3753                 ", user_data = ?"
3754                 ", user_data_length = ?"
3755                 ", account_svc_id = ?"
3756                 ", sync_status = ?"
3757                 ", sync_disabled = ?"
3758                 ", default_mail_slot_size = ?"
3759                 ", user_display_name = ?"
3760                 ", user_email_address = ?"
3761                 ", reply_to_address = ?"
3762                 ", return_address = ?"
3763                 ", incoming_server_type = ?"
3764                 ", incoming_server_address = ?"
3765                 ", incoming_server_port_number = ?"
3766                 ", incoming_server_user_name = ?"
3767                 ", incoming_server_secure_connection = ?"
3768                 ", retrieval_mode = ?"
3769                 ", keep_mails_on_pop_server_after_download = ?"
3770                 ", check_interval = ?"
3771                 ", auto_download_size = ?"
3772                 ", outgoing_server_type = ?"
3773                 ", outgoing_server_address = ?"
3774                 ", outgoing_server_port_number = ?"
3775                 ", outgoing_server_user_name = ?"
3776                 ", outgoing_server_secure_connection = ?"
3777                 ", outgoing_server_need_authentication = ?"
3778                 ", outgoing_server_use_same_authenticator = ?"
3779                 ", priority = ?"
3780                 ", keep_local_copy = ?"
3781                 ", req_delivery_receipt = ?"
3782                 ", req_read_receipt = ?"
3783                 ", download_limit = ?"
3784                 ", block_address = ?"
3785                 ", block_subject = ?"
3786                 ", display_name_from = ?"
3787                 ", reply_with_body = ?"
3788                 ", forward_with_files = ?"
3789                 ", add_myname_card = ?"
3790                 ", add_signature = ?"
3791                 ", signature = ?"
3792                 ", add_my_address_to_bcc = ?"
3793                 ", pop_before_smtp = ?"
3794                 ", incoming_server_requires_apop = ?"
3795                 ", smime_type = ?"
3796                 ", certificate_path = ?"
3797                 ", cipher_type = ?"
3798                 ", digest_type = ?"
3799                 " WHERE account_id = ?");
3800
3801         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3802         EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
3803         EM_DEBUG_LOG("SQL[%s]", sql_query_string);
3804         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3805                 ("sqlite3_prepare fail:(%d) %s", rc, sqlite3_errmsg(local_db_handle)));
3806
3807         int i = 0;
3808
3809         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
3810         _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
3811         _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
3812         _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
3813         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
3814         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
3815         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
3816         _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
3817         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
3818         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3819         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3820         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3821         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
3822         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3823         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
3824         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
3825         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
3826         _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
3827         _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
3828         _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
3829         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
3830         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
3831         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3832         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
3833         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
3834         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
3835         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
3836         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
3837         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
3838         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
3839         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
3840         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
3841         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
3842         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
3843         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
3844         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
3845         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
3846         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
3847         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
3848         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
3849         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
3850         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
3851         _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
3852         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
3853         _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
3854         _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
3855         _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
3856         _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
3857         _bind_stmt_field_data_int(hStmt, i++, account_id);
3858
3859         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3860         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
3861                 ("sqlite3_step fail:%d", rc));
3862         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3863                 ("sqlite3_step fail:%d", rc));
3864
3865         /*  validate account existence */
3866         rc = sqlite3_changes(local_db_handle);
3867         if (rc == 0) {
3868                 EM_DEBUG_EXCEPTION(" no matched account found...");
3869
3870                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3871                 goto FINISH_OFF;
3872         }
3873
3874         /*  get password file name */
3875         if ((error = _get_password_file_name(account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
3876                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
3877                 goto FINISH_OFF;
3878         }
3879
3880         /*  save passwords to the secure storage */
3881         EM_DEBUG_LOG("save to the secure storage : recv_file[%s], recv_pass[%s], send_file[%s], send_pass[%s]", recv_password_file_name, account_tbl->incoming_server_password, send_password_file_name, account_tbl->outgoing_server_password);
3882         if (account_tbl->incoming_server_password && (EM_SAFE_STRLEN(account_tbl->incoming_server_password) > 0)) {
3883                 if (ssm_write_buffer(account_tbl->incoming_server_password, EM_SAFE_STRLEN(account_tbl->incoming_server_password), recv_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
3884                         EM_DEBUG_EXCEPTION(" ssm_write_buffer failed -recv incoming_server_password : file[%s]", recv_password_file_name);
3885                         error = EMAIL_ERROR_SYSTEM_FAILURE;
3886                         goto FINISH_OFF;
3887                 }
3888         }
3889
3890         if (account_tbl->outgoing_server_password && (EM_SAFE_STRLEN(account_tbl->outgoing_server_password) > 0)) {
3891                 if (ssm_write_buffer(account_tbl->outgoing_server_password, EM_SAFE_STRLEN(account_tbl->outgoing_server_password), send_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
3892                         EM_DEBUG_EXCEPTION(" ssm_write_buffer failed -send password : file[%s]", send_password_file_name);
3893                         error = EMAIL_ERROR_SYSTEM_FAILURE;
3894                         goto FINISH_OFF;
3895                 }
3896         }
3897
3898         if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_tbl->account_id, 0, NULL, 0))
3899                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_ACCOUNT_UPDATE] : Notification Failed >>> ");
3900         ret = true;
3901
3902 FINISH_OFF:
3903
3904         if (hStmt != NULL)  {
3905                 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
3906
3907                 rc = sqlite3_finalize(hStmt);
3908                 if (rc != SQLITE_OK)  {
3909                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
3910                         error = EMAIL_ERROR_DB_FAILURE;
3911                 }
3912         }
3913
3914         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
3915
3916         _DISCONNECT_DB;
3917
3918         if (err_code != NULL)
3919                 *err_code = error;
3920
3921         EM_DEBUG_FUNC_END("ret [%d]", ret);
3922         return ret;
3923 }
3924
3925 INTERNAL_FUNC int emstorage_set_field_of_accounts_with_integer_value(int account_id, char *field_name, int value, int transaction)
3926 {
3927         EM_DEBUG_FUNC_BEGIN("account_id[%d], field_name[%s], value[%d], transaction[%d]", account_id, field_name, value, transaction);
3928         int error = EMAIL_ERROR_NONE;
3929         int rc = 0;
3930         int result = 0;
3931         char sql_query_string[QUERY_SIZE] = {0, };
3932         sqlite3 *local_db_handle = NULL;
3933
3934         if (!account_id  || !field_name) {
3935                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3936                 return EMAIL_ERROR_INVALID_PARAM;
3937         }
3938
3939         local_db_handle = emstorage_get_db_connection();
3940
3941         /* Write query string */
3942         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_account_tbl SET %s = %d WHERE account_id = %d", field_name, value, account_id);
3943
3944         EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
3945
3946         /* Execute query */
3947         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
3948         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
3949         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3950                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3951         if (sqlite3_changes(local_db_handle) == 0)
3952                 EM_DEBUG_LOG("no mail matched...");
3953
3954
3955 FINISH_OFF:
3956         result = (error == EMAIL_ERROR_NONE) ? true : false;
3957         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, result, error);
3958         _DISCONNECT_DB;
3959
3960         if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_id, 0, field_name, value))
3961                 EM_DEBUG_EXCEPTION("emcore_notify_storage_event failed : NOTI_ACCOUNT_UPDATE [%s,%d]", field_name, value);
3962
3963         EM_DEBUG_FUNC_END("error [%d]", error);
3964         return error;
3965 }
3966
3967 INTERNAL_FUNC int emstorage_get_sync_status_of_account(int account_id, int *result_sync_status,int *err_code)
3968 {
3969         EM_DEBUG_FUNC_BEGIN("account_id[%d], result_sync_status [%p], err_code[%p]", account_id, result_sync_status, err_code);
3970
3971         if(!result_sync_status) {
3972                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3973                 if (err_code != NULL)
3974                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3975                 return false;
3976         }
3977
3978         int error = EMAIL_ERROR_NONE, rc, ret = false, sync_status, count, i, col_index;
3979         char sql_query_string[QUERY_SIZE] = {0, };
3980         char **result = NULL;
3981         sqlite3 *local_db_handle = emstorage_get_db_connection();
3982
3983         if(account_id)
3984                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl WHERE account_id = %d", account_id);
3985         else
3986                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl");
3987
3988         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3989         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
3990                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3991
3992         if (!count) {
3993                 EM_DEBUG_EXCEPTION("no matched account found...");
3994                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3995                 goto FINISH_OFF;
3996         }
3997
3998         col_index = 1;
3999         *result_sync_status = 0;
4000
4001         for(i = 0; i < count; i++) {
4002                 _get_table_field_data_int(result, &sync_status, col_index++);
4003                 *result_sync_status |= sync_status;
4004         }
4005
4006         EM_DEBUG_LOG("sync_status [%d]", sync_status);
4007
4008         sqlite3_free_table(result);
4009
4010         ret = true;
4011
4012 FINISH_OFF:
4013
4014         _DISCONNECT_DB;
4015
4016         if (err_code != NULL)
4017                 *err_code = error;
4018
4019         EM_DEBUG_FUNC_END("ret [%d]", ret);
4020         return ret;
4021 }
4022
4023 INTERNAL_FUNC int emstorage_update_sync_status_of_account(int account_id, email_set_type_t set_operator, int sync_status, int transaction, int *err_code)
4024 {
4025         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);
4026
4027         int error = EMAIL_ERROR_NONE, rc, ret = false, set_value = sync_status, result_sync_status;
4028         char sql_query_string[QUERY_SIZE] = {0, };
4029         sqlite3 *local_db_handle = emstorage_get_db_connection();
4030
4031         if(set_operator != SET_TYPE_SET && account_id) {
4032                 if(!emstorage_get_sync_status_of_account(account_id, &result_sync_status, &error)) {
4033                         EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", error);
4034                         goto FINISH_OFF;
4035                 }
4036                 switch(set_operator) {
4037                         case SET_TYPE_UNION :
4038                                 set_value = result_sync_status | set_value;
4039                                 break;
4040                         case SET_TYPE_MINUS :
4041                                 set_value = result_sync_status & (~set_value);
4042                                 break;
4043                         default:
4044                                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_NOT_SUPPORTED [%d]", set_operator);
4045                                 error = EMAIL_ERROR_NOT_SUPPORTED;
4046                                 break;
4047                 }
4048                 EM_DEBUG_LOG("set_value [%d]", set_value);
4049         }
4050
4051         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
4052
4053         if(account_id)
4054                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE account_id = %d", set_value, account_id);
4055         else
4056                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE incoming_server_type <> 5", set_value);
4057
4058         EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
4059
4060         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
4061         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4062                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4063
4064         rc = sqlite3_changes(local_db_handle);
4065
4066         if (rc == 0) {
4067                 EM_DEBUG_EXCEPTION("no matched account found...");
4068                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4069                 goto FINISH_OFF;
4070         }
4071
4072         if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE_SYNC_STATUS, account_id, 0, NULL, 0))
4073                 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_UPDATE_SYNC_STATUS] : Notification failed");
4074         ret = true;
4075
4076 FINISH_OFF:
4077
4078         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
4079
4080         _DISCONNECT_DB;
4081
4082         if (err_code != NULL)
4083                 *err_code = error;
4084
4085         EM_DEBUG_FUNC_END("ret [%d]", ret);
4086         return ret;
4087 }
4088
4089 INTERNAL_FUNC int emstorage_add_account(emstorage_account_tbl_t* account_tbl, int transaction, int *err_code)
4090 {
4091         EM_DEBUG_FUNC_BEGIN("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4092
4093         if (!account_tbl)  {
4094                 EM_DEBUG_EXCEPTION("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4095                 if (err_code != NULL)
4096                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4097                 return false;
4098         }
4099
4100         int rc = -1, ret = false;
4101         int error = EMAIL_ERROR_NONE;
4102         int error_from_ssm = 0;
4103         DB_STMT hStmt = NULL;
4104         char sql_query_string[QUERY_SIZE] = {0, };
4105         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4106         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4107
4108         sqlite3 *local_db_handle = emstorage_get_db_connection();
4109
4110         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
4111
4112         char *sql = "SELECT max(rowid) FROM mail_account_tbl;";
4113         char **result = NULL;
4114
4115         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
4116         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
4117                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
4118
4119         if (NULL==result[1]) rc = 1;
4120         else rc = atoi(result[1])+1;
4121         sqlite3_free_table(result);
4122         result = NULL;
4123
4124         account_tbl->account_id = rc;
4125
4126         if ((error = _get_password_file_name(account_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4127                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4128                 goto FINISH_OFF;
4129         }
4130
4131         EM_DEBUG_LOG(" >>>> ACCOUNT_ID [ %d ] ", account_tbl->account_id);
4132         SNPRINTF(sql_query_string, sizeof(sql_query_string),
4133                 "INSERT INTO mail_account_tbl VALUES "
4134                 "(        "
4135                 "    ? "  /*   account_id */
4136                 "  , ? "  /*   account_name */
4137                 "  , ? "  /*   logo_icon_path */
4138                 "  , ? "  /*   user_data */
4139                 "  , ? "  /*   user_data_length */
4140                 "  , ? "  /*   account_svc_id */
4141                 "  , ? "  /*   sync_status */
4142                 "  , ? "  /*   sync_disabled */
4143                 "  , ? "  /*   default_mail_slot_size */
4144                 "  , ? "  /*   user_display_name */
4145                 "  , ? "  /*   user_email_address */
4146                 "  , ? "  /*   reply_to_address */
4147                 "  , ? "  /*   return_address */
4148                 "  , ? "  /*   incoming_server_type */
4149                 "  , ? "  /*   incoming_server_address */
4150                 "  , ? "  /*   incoming_server_port_number */
4151                 "  , ? "  /*   incoming_server_user_name */
4152                 "  , ? "  /*   incoming_server_password */
4153                 "  , ? "  /*   incoming_server_secure_connection */
4154                 "  , ? "  /*   retrieval_mode */
4155                 "  , ? "  /*   keep_mails_on_pop_server_after_download */
4156                 "  , ? "  /*   check_interval */
4157                 "  , ? "  /*   auto_download_size */
4158                 "  , ? "  /*   outgoing_server_type */
4159                 "  , ? "  /*   outgoing_server_address */
4160                 "  , ? "  /*   outgoing_server_port_number */
4161                 "  , ? "  /*   outgoing_server_user_name */
4162                 "  , ? "  /*   outgoing_server_password */
4163                 "  , ? "  /*   outgoing_server_secure_connection */
4164                 "  , ? "  /*   outgoing_server_need_authentication */
4165                 "  , ? "  /*   outgoing_server_use_same_authenticator */
4166                 "  , ? "  /*   priority */
4167                 "  , ? "  /*   keep_local_copy */
4168                 "  , ? "  /*   req_delivery_receipt */
4169                 "  , ? "  /*   req_read_receipt */
4170                 "  , ? "  /*   download_limit */
4171                 "  , ? "  /*   block_address */
4172                 "  , ? "  /*   block_subject */
4173                 "  , ? "  /*   display_name_from */
4174                 "  , ? "  /*   reply_with_body */
4175                 "  , ? "  /*   forward_with_files */
4176                 "  , ? "  /*   add_myname_card */
4177                 "  , ? "  /*   add_signature */
4178                 "  , ? "  /*   signature */
4179                 "  , ? "  /*   add_my_address_to_bcc */
4180                 "  , ? "  /*   pop_before_smtp */
4181                 "  , ? "  /*   incoming_server_requires_apop */
4182                 "  , ? "  /*   smime_type */
4183                 "  , ? "  /*   certificate_path */
4184                 "  , ? "  /*   cipher_type */
4185                 "  , ? "  /*   digest_type */
4186                 ") ");
4187
4188         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4189         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4190                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4191
4192         EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
4193         int i = 0;
4194
4195         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_id);
4196         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4197         _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4198         _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4199         _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4200         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4201         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4202         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4203         _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4204         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4205         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4206         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4207         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4208         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4209         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4210         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4211         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4212         _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
4213         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4214         _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
4215         _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
4216         _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
4217         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
4218         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
4219         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4220         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
4221         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
4222         _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
4223         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
4224         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
4225         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
4226         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
4227         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
4228         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
4229         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
4230         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
4231         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
4232         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
4233         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4234         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
4235         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
4236         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
4237         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
4238         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
4239         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
4240         _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
4241         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
4242         _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
4243         _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
4244         _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
4245         _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
4246
4247         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4248
4249         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
4250                 ("sqlite3_step fail:%d", rc));
4251         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4252                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
4253
4254
4255         /*  save passwords to the secure storage */
4256         EM_DEBUG_LOG("save to the secure storage : recv_file[%s], send_file[%s]", recv_password_file_name, send_password_file_name);
4257         if ( (error_from_ssm = ssm_write_buffer(account_tbl->incoming_server_password, EM_SAFE_STRLEN(account_tbl->incoming_server_password), recv_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL)) < 0) {
4258                 EM_DEBUG_EXCEPTION("ssm_write_buffer failed [%d] - recv password : file[%s]", error_from_ssm, recv_password_file_name);
4259                 error = EMAIL_ERROR_SYSTEM_FAILURE;
4260                 goto FINISH_OFF;
4261         }
4262         if ( (error_from_ssm = ssm_write_buffer(account_tbl->outgoing_server_password, EM_SAFE_STRLEN(account_tbl->outgoing_server_password), send_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL)) < 0) {
4263                 EM_DEBUG_EXCEPTION("ssm_write_buffer failed [%d] - send password : file[%s]", error_from_ssm, send_password_file_name);
4264                 error = EMAIL_ERROR_SYSTEM_FAILURE;
4265                 goto FINISH_OFF;
4266         }
4267
4268         ret = true;
4269
4270         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
4271
4272         if (!emcore_notify_storage_event(NOTI_ACCOUNT_ADD, account_tbl->account_id, 0, NULL, 0))
4273                 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_ADD] : Notification failed");
4274
4275 FINISH_OFF:
4276
4277         if (hStmt != NULL)  {
4278                 rc = sqlite3_finalize(hStmt);
4279                 if (rc != SQLITE_OK)  {
4280                         EM_DEBUG_LOG("sqlite3_finalize failed [%d]", rc);
4281                         error = EMAIL_ERROR_DB_FAILURE;
4282                 }
4283         }
4284         else
4285                 EM_DEBUG_LOG("hStmt is NULL!!!");
4286
4287         _DISCONNECT_DB;
4288
4289         if (err_code != NULL)
4290                 *err_code = error;
4291
4292         EM_DEBUG_FUNC_END("ret [%d]", ret);
4293         return ret;
4294 }
4295
4296
4297 INTERNAL_FUNC int emstorage_delete_account(int account_id, int transaction, int *err_code)
4298 {
4299         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
4300
4301         if (account_id < FIRST_ACCOUNT_ID)  {
4302                 EM_DEBUG_EXCEPTION(" account_id[%d]", account_id);
4303
4304                 if (err_code != NULL)
4305                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4306                 return false;
4307         }
4308
4309         int rc = -1, ret = false;
4310         int error = EMAIL_ERROR_NONE;
4311         sqlite3 *local_db_handle = emstorage_get_db_connection();
4312         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
4313
4314         /*  TODO : delete password files - file names can be obtained from db or a rule that makes a name */
4315         DB_STMT hStmt = NULL;
4316         char sql_query_string[QUERY_SIZE] = {0, };
4317         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4318         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4319
4320         /*  get password file name */
4321         if ((error = _get_password_file_name(account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4322                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4323                 goto FINISH_OFF;
4324         }
4325
4326         /*  delete from db */
4327         memset(sql_query_string, 0x00, sizeof(sql_query_string));
4328         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_account_tbl WHERE account_id = %d", account_id);
4329
4330         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
4331         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
4332                 ("sqlite3_exec fail:%d", rc));
4333         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4334                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4335
4336         /*  validate account existence */
4337         rc = sqlite3_changes(local_db_handle);
4338         if (rc == 0)  {
4339                 EM_DEBUG_EXCEPTION(" no matched account found...");
4340                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4341                 goto FINISH_OFF;
4342         }
4343
4344                 /*  delete from secure storage */
4345         if (ssm_delete_file(recv_password_file_name,  SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
4346                 EM_DEBUG_EXCEPTION(" ssm_delete_file failed -recv password : file[%s]", recv_password_file_name);
4347                 error = EMAIL_ERROR_SYSTEM_FAILURE;
4348                 goto FINISH_OFF;
4349         }
4350         if (ssm_delete_file(send_password_file_name,  SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
4351                 EM_DEBUG_EXCEPTION(" ssm_delete_file failed -send password : file[%s]", send_password_file_name);
4352                 error = EMAIL_ERROR_SYSTEM_FAILURE;
4353                 goto FINISH_OFF;
4354         }
4355         ret = true;
4356
4357 FINISH_OFF:
4358
4359         if (hStmt != NULL)  {
4360                 rc = sqlite3_finalize(hStmt);
4361                 if (rc != SQLITE_OK)  {
4362                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
4363                         error = EMAIL_ERROR_DB_FAILURE;
4364                 }
4365         }
4366
4367         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
4368         _DISCONNECT_DB;
4369
4370         if (err_code != NULL)
4371                 *err_code = error;
4372
4373         EM_DEBUG_FUNC_END("ret [%d]", ret);
4374         return ret;
4375 }
4376
4377
4378 INTERNAL_FUNC int emstorage_free_account(emstorage_account_tbl_t** account_list, int count, int *err_code)
4379 {
4380         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%d], err_code[%p]", account_list, count, err_code);
4381
4382         int ret = false;
4383         int error = EMAIL_ERROR_NONE;
4384
4385         if (count > 0)  {
4386                 if (!account_list || !*account_list)  {
4387                         EM_DEBUG_EXCEPTION("account_list[%p], count[%d]", account_list, count);
4388                         error = EMAIL_ERROR_INVALID_PARAM;
4389                         goto FINISH_OFF;
4390                 }
4391
4392                 emstorage_account_tbl_t* p = *account_list;
4393                 int i = 0;
4394
4395                 for (; i < count; i++)  {
4396                         EM_SAFE_FREE(p[i].account_name);
4397                         EM_SAFE_FREE(p[i].incoming_server_address);
4398                         EM_SAFE_FREE(p[i].user_email_address);
4399                         EM_SAFE_FREE(p[i].user_data);
4400                         EM_SAFE_FREE(p[i].incoming_server_user_name);
4401                         EM_SAFE_FREE(p[i].incoming_server_password);
4402                         EM_SAFE_FREE(p[i].outgoing_server_address);
4403                         EM_SAFE_FREE(p[i].outgoing_server_user_name);
4404                         EM_SAFE_FREE(p[i].outgoing_server_password);
4405                         EM_SAFE_FREE(p[i].user_display_name);
4406                         EM_SAFE_FREE(p[i].reply_to_address);
4407                         EM_SAFE_FREE(p[i].return_address);
4408                         EM_SAFE_FREE(p[i].logo_icon_path);
4409                         EM_SAFE_FREE(p[i].options.display_name_from);
4410                         EM_SAFE_FREE(p[i].options.signature);
4411                         EM_SAFE_FREE(p[i].certificate_path);
4412                 }
4413
4414                 EM_SAFE_FREE(p);
4415                 *account_list = NULL;
4416         }
4417
4418         ret = true;
4419
4420 FINISH_OFF:
4421         if (err_code != NULL)
4422                 *err_code = error;
4423
4424         EM_DEBUG_FUNC_END("ret [%d]", ret);
4425         return ret;
4426 }
4427
4428 INTERNAL_FUNC int emstorage_get_mailbox_count(int account_id, int local_yn, int *count, int transaction, int *err_code)
4429 {
4430         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);
4431
4432         if ((account_id < FIRST_ACCOUNT_ID) || (count == NULL))  {
4433                 EM_DEBUG_EXCEPTION(" account_list[%d], local_yn[%d], count[%p]", account_id, local_yn, count);
4434
4435                 if (err_code != NULL)
4436                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4437                 return false;
4438         }
4439
4440         int rc = -1, ret = false;
4441         int error = EMAIL_ERROR_NONE;
4442         char sql_query_string[QUERY_SIZE] = {0, };
4443
4444         sqlite3 *local_db_handle = emstorage_get_db_connection();
4445         EMSTORAGE_START_READ_TRANSACTION(transaction);
4446
4447         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);
4448
4449         char **result;
4450
4451         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
4452         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
4453                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4454
4455         *count = atoi(result[1]);
4456         sqlite3_free_table(result);
4457
4458
4459         ret = true;
4460
4461 FINISH_OFF:
4462
4463         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4464         _DISCONNECT_DB;
4465
4466         if (err_code != NULL)
4467                 *err_code = error;
4468
4469         EM_DEBUG_FUNC_END("ret [%d]", ret);
4470         return ret;
4471 }
4472
4473 INTERNAL_FUNC int emstorage_get_mailbox_list(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)
4474 {
4475         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);
4476
4477         if (!select_num || !mailbox_list) {
4478                 EM_DEBUG_EXCEPTION("Invalid parameters");
4479
4480                 if (err_code != NULL)
4481                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4482
4483                 return false;
4484         }
4485
4486         int ret = false;
4487         int error = EMAIL_ERROR_NONE;
4488         char conditional_clause_string[QUERY_SIZE] = {0, };
4489         char ordering_clause_string[QUERY_SIZE] = {0, };
4490
4491         if (account_id == ALL_ACCOUNT) {
4492                 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
4493                         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);
4494         }
4495         else {
4496                 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d  ", account_id);
4497                 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
4498                         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);
4499         }
4500
4501         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
4502
4503         switch (sort_type) {
4504                 case EMAIL_MAILBOX_SORT_BY_NAME_ASC :
4505                         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name ASC");
4506                         break;
4507
4508                 case EMAIL_MAILBOX_SORT_BY_NAME_DSC :
4509                         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name DESC");
4510                         break;
4511
4512                 case EMAIL_MAILBOX_SORT_BY_TYPE_ASC :
4513                         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type ASC");
4514                         break;
4515
4516                 case EMAIL_MAILBOX_SORT_BY_TYPE_DSC :
4517                         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type DEC");
4518                         break;
4519         }
4520
4521         EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
4522
4523         if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, ordering_clause_string, 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
4524                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4525                 goto FINISH_OFF;
4526         }
4527
4528         ret = true;
4529
4530 FINISH_OFF:
4531
4532         if (err_code != NULL)
4533                 *err_code = error;
4534
4535         EM_DEBUG_FUNC_END("ret [%d]", ret);
4536         return ret;
4537 }
4538
4539 INTERNAL_FUNC int emstorage_get_mailbox_list_ex(int account_id, int local_yn, int with_count, int *select_num, emstorage_mailbox_tbl_t** mailbox_list, int transaction, int *err_code)
4540 {
4541         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);
4542
4543         int ret = false;
4544         int error = EMAIL_ERROR_NONE;
4545         int where_clause_count = 0;
4546         char conditional_clause_string[QUERY_SIZE] = {0, };
4547         char ordering_clause_string[QUERY_SIZE] = {0, };
4548
4549         if (!select_num || !mailbox_list) {
4550                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4551                 error = EMAIL_ERROR_INVALID_PARAM;
4552                 goto FINISH_OFF;
4553         }
4554
4555         if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL) {
4556                 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);
4557                 where_clause_count++;
4558         }
4559
4560         if (account_id > 0) {
4561                 if (where_clause_count == 0) {
4562                         SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " WHERE ");
4563                         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);
4564                 }
4565                 else
4566                         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);
4567         }
4568
4569         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY MBT.mailbox_name ");
4570         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
4571         EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
4572
4573         if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, ordering_clause_string, 1, 1, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
4574                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4575                 goto FINISH_OFF;
4576         }
4577
4578         ret = true;
4579
4580 FINISH_OFF:
4581
4582         if (err_code != NULL)
4583                 *err_code = error;
4584
4585         EM_DEBUG_FUNC_END("ret [%d]", ret);
4586         return ret;
4587 }
4588
4589 INTERNAL_FUNC int emstorage_get_child_mailbox_list(int account_id, char *parent_mailbox_name, int *select_num, emstorage_mailbox_tbl_t** mailbox_list, int transaction, int *err_code)
4590 {
4591         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);
4592         if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list)  {
4593                 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);
4594
4595                 if (err_code != NULL)
4596                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4597                 return false;
4598         }
4599
4600         int ret = false;
4601         int error = EMAIL_ERROR_NONE;
4602         char conditional_clause_string[QUERY_SIZE] = {0, };
4603
4604         if (parent_mailbox_name)
4605                 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d  AND UPPER(mailbox_name) LIKE UPPER('%s/%%')", account_id, parent_mailbox_name);
4606         else
4607                 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d  AND (mailbox_name NOT LIKE '%%/%%')", account_id);
4608
4609         EM_DEBUG_LOG("conditional_clause_string", conditional_clause_string);
4610
4611         if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, " ORDER BY mailbox_name", 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
4612                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4613                 goto FINISH_OFF;
4614         }
4615
4616         ret = true;
4617
4618 FINISH_OFF:
4619
4620         if (err_code != NULL)
4621                 *err_code = error;
4622
4623         EM_DEBUG_FUNC_END("ret [%d]", ret);
4624         return ret;
4625 }
4626
4627 INTERNAL_FUNC int emstorage_get_mailbox_by_modifiable_yn(int account_id, int local_yn, int *select_num, emstorage_mailbox_tbl_t** mailbox_list, int transaction, int *err_code)
4628 {
4629         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);
4630         if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list)  {
4631                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4632
4633                 if (err_code != NULL)
4634                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4635
4636                 return false;
4637         }
4638
4639         int ret = false;
4640         int error = EMAIL_ERROR_NONE;
4641         char conditional_clause_string[QUERY_SIZE] = {0, };
4642
4643         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND modifiable_yn = 0", account_id);
4644         EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
4645
4646         if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, " ORDER BY mailbox_name", 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
4647                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4648                 goto FINISH_OFF;
4649         }
4650
4651         ret = true;
4652
4653 FINISH_OFF:
4654
4655         if (err_code != NULL)
4656                 *err_code = error;
4657
4658         EM_DEBUG_FUNC_END("ret [%d]", ret);
4659         return ret;
4660 }
4661
4662 INTERNAL_FUNC int emstorage_stamp_last_sync_time_of_mailbox(int input_mailbox_id, int input_transaction)
4663 {
4664         EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_transaction [%d]", input_mailbox_id, input_transaction);
4665
4666         int      result_code = false;
4667         int      error = EMAIL_ERROR_NONE;
4668         int      rc;
4669         time_t   current_time = 0;
4670         char     sql_query_string[QUERY_SIZE] = {0, };
4671         sqlite3 *local_db_handle = NULL;
4672
4673         if (!input_mailbox_id )  {
4674                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4675                 return EMAIL_ERROR_INVALID_PARAM;
4676         }
4677
4678         time(&current_time);
4679
4680         local_db_handle = emstorage_get_db_connection();
4681
4682         EMSTORAGE_START_WRITE_TRANSACTION(input_transaction, error);
4683
4684         SNPRINTF(sql_query_string, sizeof(sql_query_string),
4685                 "UPDATE mail_box_tbl SET"
4686                 " last_sync_time = %d"
4687                 " WHERE mailbox_id = %d"
4688                 , (int)current_time
4689                 , input_mailbox_id);
4690
4691         EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
4692
4693         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
4694         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4695                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4696
4697 FINISH_OFF:
4698
4699         if(error == EMAIL_ERROR_NONE)
4700                 result_code = true;
4701
4702         EMSTORAGE_FINISH_WRITE_TRANSACTION(input_transaction, result_code, error);
4703         _DISCONNECT_DB;
4704
4705         EM_DEBUG_FUNC_END("error [%d]", error);
4706         return error;
4707 }
4708
4709 INTERNAL_FUNC int emstorage_get_mailbox_by_name(int account_id, int local_yn, char *mailbox_name, emstorage_mailbox_tbl_t** result_mailbox, int transaction, int *err_code)
4710 {
4711         EM_DEBUG_FUNC_BEGIN("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);
4712         EM_PROFILE_BEGIN(profile_emstorage_get_mailbox_by_name);
4713
4714         if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !result_mailbox)  {
4715                 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], mailbox_name[%s], result_mailbox[%p]", account_id, local_yn, mailbox_name, result_mailbox);
4716
4717                 if (err_code != NULL)
4718                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4719                 return false;
4720         }
4721
4722         int ret = false;
4723         int error = EMAIL_ERROR_NONE;
4724         int result_count = 0;
4725         char conditional_clause_string[QUERY_SIZE] = {0, };
4726         char *replaced_mailbox_name = NULL;
4727
4728         if (strstr(mailbox_name, "'")) {
4729                 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
4730                 EM_DEBUG_LOG("replaced_mailbox_name : [%s]", replaced_mailbox_name);
4731         } else {
4732                 replaced_mailbox_name = strdup(mailbox_name);
4733         }
4734
4735         if(strcmp(mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
4736                 if (!(*result_mailbox = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t))))  {
4737                         EM_DEBUG_EXCEPTION("malloc failed...");
4738                         error = EMAIL_ERROR_OUT_OF_MEMORY;
4739                         goto FINISH_OFF;
4740                 }
4741
4742                 (*result_mailbox)->mailbox_id                 = 0;
4743                 (*result_mailbox)->account_id                 = account_id;
4744                 (*result_mailbox)->local_yn                   = 1;
4745                 (*result_mailbox)->mailbox_name               = EM_SAFE_STRDUP(mailbox_name);
4746                 (*result_mailbox)->mailbox_type               = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
4747                 (*result_mailbox)->alias                      = EM_SAFE_STRDUP(mailbox_name);
4748                 (*result_mailbox)->deleted_flag               = 0;
4749                 (*result_mailbox)->modifiable_yn              = 1;
4750                 (*result_mailbox)->total_mail_count_on_server = 1;
4751                 (*result_mailbox)->has_archived_mails         = 0;
4752                 (*result_mailbox)->mail_slot_size             = 0x0FFFFFFF;
4753                 (*result_mailbox)->no_select                  = 0;
4754         }
4755         else {
4756
4757                 if (local_yn == -1)
4758                         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND mailbox_name = '%s'", account_id, replaced_mailbox_name);
4759                 else
4760                         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND local_yn = %d AND mailbox_name = '%s'", account_id, local_yn, replaced_mailbox_name);
4761
4762                 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
4763
4764                 if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, "", 0, transaction, result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
4765                         EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4766                         goto FINISH_OFF;
4767                 }
4768         }
4769
4770         ret = true;
4771
4772 FINISH_OFF:
4773
4774         EM_SAFE_FREE(replaced_mailbox_name);
4775
4776         if (err_code != NULL)
4777                 *err_code = error;
4778
4779         EM_PROFILE_END(profile_emstorage_get_mailbox_by_name);
4780         EM_DEBUG_FUNC_END("ret [%d]", ret);
4781         return ret;
4782 }
4783
4784 INTERNAL_FUNC int emstorage_get_mailbox_by_mailbox_type(int account_id, email_mailbox_type_e mailbox_type, emstorage_mailbox_tbl_t** mailbox_name, int transaction, int *err_code)
4785 {
4786         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);
4787
4788         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_USER_DEFINED) || !mailbox_name) {
4789
4790                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
4791
4792                 if (err_code != NULL)
4793                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4794                 return false;
4795         }
4796
4797         int rc, ret = false;
4798         int error = EMAIL_ERROR_NONE;
4799         emstorage_mailbox_tbl_t *p_data_tbl = NULL;
4800         emstorage_account_tbl_t *account = NULL;
4801         DB_STMT hStmt = NULL;
4802         char sql_query_string[QUERY_SIZE] = {0,};
4803         char *fields = "mailbox_id, account_id, local_yn, mailbox_name, mailbox_type, alias, deleted_flag, modifiable_yn, total_mail_count_on_server, has_archived_mails, mail_slot_size, no_select, last_sync_time ";
4804
4805         sqlite3 *local_db_handle = emstorage_get_db_connection();
4806         EMSTORAGE_START_READ_TRANSACTION(transaction);
4807
4808         /* validate account */
4809         /*  Check whether the account exists. */
4810         if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
4811                 EM_DEBUG_EXCEPTION(" emstorage_get_account_by_id failed - %d", error);
4812                 goto FINISH_OFF;
4813         }
4814
4815         if (account)
4816                 emstorage_free_account(&account, 1, NULL);
4817
4818         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT %s FROM mail_box_tbl WHERE account_id = %d AND mailbox_type = %d ", fields, account_id, mailbox_type);
4819
4820         EM_DEBUG_LOG("query = [%s]", sql_query_string);
4821
4822
4823         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4824         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)));
4825
4826
4827         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4828         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
4829
4830         if (rc == SQLITE_DONE) {
4831                 EM_DEBUG_EXCEPTION(" no matched mailbox_name found...");
4832                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
4833                 goto FINISH_OFF;
4834         }
4835
4836
4837         if (!(p_data_tbl = (emstorage_mailbox_tbl_t*)malloc(sizeof(emstorage_mailbox_tbl_t))))  {
4838                 EM_DEBUG_EXCEPTION(" malloc failed...");
4839                 error = EMAIL_ERROR_OUT_OF_MEMORY;
4840                 goto FINISH_OFF;
4841         }
4842
4843         memset(p_data_tbl, 0x00, sizeof(emstorage_mailbox_tbl_t));
4844
4845         int col_index = 0;
4846
4847         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
4848         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
4849         _get_stmt_field_data_int(hStmt, &(p_data_tbl->local_yn), col_index++);
4850         _get_stmt_field_data_string(hStmt, &(p_data_tbl->mailbox_name), 0, col_index++);
4851         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->mailbox_type), col_index++);
4852         _get_stmt_field_data_string(hStmt, &(p_data_tbl->alias), 0, col_index++);
4853         _get_stmt_field_data_int(hStmt, &(p_data_tbl->deleted_flag), col_index++);
4854         _get_stmt_field_data_int(hStmt, &(p_data_tbl->modifiable_yn), col_index++);
4855         _get_stmt_field_data_int(hStmt, &(p_data_tbl->total_mail_count_on_server), col_index++);
4856         _get_stmt_field_data_int(hStmt, &(p_data_tbl->has_archived_mails), col_index++);
4857         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_slot_size), col_index++);
4858         _get_stmt_field_data_int(hStmt, &(p_data_tbl->no_select), col_index++);
4859         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->last_sync_time), col_index++);
4860
4861         ret = true;
4862
4863 FINISH_OFF:
4864         if (ret == true)
4865                 *mailbox_name = p_data_tbl;
4866
4867         if (hStmt != NULL)  {
4868                 rc = sqlite3_finalize(hStmt);
4869                 if (rc != SQLITE_OK)  {
4870                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
4871                         error = EMAIL_ERROR_DB_FAILURE;
4872                 }
4873         }
4874
4875         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4876         _DISCONNECT_DB;
4877
4878         if (err_code != NULL)
4879                 *err_code = error;
4880
4881         EM_DEBUG_FUNC_END("ret [%d]", ret);
4882         return ret;
4883 }
4884
4885 INTERNAL_FUNC int emstorage_get_mailbox_by_id(int input_mailbox_id, emstorage_mailbox_tbl_t** output_mailbox)
4886 {
4887         EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_mailbox[%p]", input_mailbox_id, output_mailbox);
4888
4889         if (input_mailbox_id <= 0 || !output_mailbox)  {
4890                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4891                 return  EMAIL_ERROR_INVALID_PARAM;
4892         }
4893
4894         int  ret = EMAIL_ERROR_NONE;
4895         int  result_count = 0;
4896         char conditional_clause_string[QUERY_SIZE] = {0, };
4897
4898         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE mailbox_id = %d", input_mailbox_id);
4899
4900         EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
4901
4902         if( (ret = emstorage_query_mailbox_tbl(conditional_clause_string, "", false, false, output_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
4903                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", ret);
4904                 goto FINISH_OFF;
4905         }
4906
4907 FINISH_OFF:
4908
4909         EM_DEBUG_FUNC_END("ret [%d]", ret);
4910         return ret;
4911 }
4912
4913 INTERNAL_FUNC int emstorage_get_mailbox_id_by_mailbox_type(int account_id, email_mailbox_type_e mailbox_type, int *mailbox_id, int transaction, int *err_code)
4914 {
4915         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);
4916         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_id)  {
4917                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_id[%p]", account_id, mailbox_type, mailbox_id);
4918                 if (err_code != NULL)
4919                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4920                 return false;
4921         }
4922
4923         int rc, ret = false;
4924         int error = EMAIL_ERROR_NONE;
4925         emstorage_account_tbl_t* account = NULL;
4926         DB_STMT hStmt = NULL;
4927         char sql_query_string[QUERY_SIZE] = {0, };
4928
4929         sqlite3 *local_db_handle = emstorage_get_db_connection();
4930         EMSTORAGE_START_READ_TRANSACTION(transaction);
4931
4932         /*  Check whether the account exists. */
4933         if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
4934                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
4935                 goto FINISH_OFF;
4936         }
4937
4938         if (account )
4939                 emstorage_free_account(&account, 1, NULL);
4940
4941         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);
4942
4943         EM_DEBUG_LOG("query = [%s]", sql_query_string);
4944
4945
4946         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4947
4948         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4949                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4950
4951
4952         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4953         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4954                 ("sqlite3_step fail:%d", rc));
4955
4956         if (rc == SQLITE_DONE) {
4957                 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
4958                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
4959                 goto FINISH_OFF;
4960         }
4961
4962         _get_stmt_field_data_int(hStmt, mailbox_id, 0);
4963
4964         ret = true;
4965
4966 FINISH_OFF:
4967         if (hStmt != NULL)  {
4968                 rc = sqlite3_finalize(hStmt);
4969                 if (rc != SQLITE_OK)  {
4970                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
4971                         error = EMAIL_ERROR_DB_FAILURE;
4972                 }
4973         }
4974
4975         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4976         _DISCONNECT_DB;
4977
4978         if (err_code != NULL)
4979                 *err_code = error;
4980
4981         EM_DEBUG_FUNC_END("ret [%d]", ret);
4982         return ret;
4983 }
4984
4985 INTERNAL_FUNC int emstorage_get_mailbox_name_by_mailbox_type(int account_id, email_mailbox_type_e mailbox_type, char **mailbox_name, int transaction, int *err_code)
4986 {
4987         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);
4988         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_name)  {
4989                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
4990                 if (err_code != NULL)
4991                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4992                 return false;
4993         }
4994
4995         int rc, ret = false;
4996         int error = EMAIL_ERROR_NONE;
4997         emstorage_account_tbl_t* account = NULL;
4998         DB_STMT hStmt = NULL;
4999         char sql_query_string[QUERY_SIZE] = {0, };
5000
5001         sqlite3 *local_db_handle = emstorage_get_db_connection();
5002         EMSTORAGE_START_READ_TRANSACTION(transaction);
5003
5004         /*  Check whether the account exists. */
5005         if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
5006                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5007                 goto FINISH_OFF;
5008         }
5009
5010         if (account )
5011                 emstorage_free_account(&account, 1, NULL);
5012
5013         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);
5014
5015         EM_DEBUG_LOG("query = [%s]", sql_query_string);
5016
5017
5018         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5019
5020         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5021                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5022
5023
5024         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5025         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5026                 ("sqlite3_step fail:%d", rc));
5027
5028         if (rc == SQLITE_DONE) {
5029                 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5030                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5031                 goto FINISH_OFF;
5032         }
5033
5034         _get_stmt_field_data_string(hStmt, mailbox_name, 0, 0);
5035
5036         ret = true;
5037
5038 FINISH_OFF:
5039         if (hStmt != NULL)  {
5040                 rc = sqlite3_finalize(hStmt);
5041                 if (rc != SQLITE_OK)  {
5042                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5043                         error = EMAIL_ERROR_DB_FAILURE;
5044                 }
5045         }
5046
5047         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5048         _DISCONNECT_DB;
5049
5050         if (err_code != NULL)
5051                 *err_code = error;
5052
5053         EM_DEBUG_FUNC_END("ret [%d]", ret);
5054         return ret;
5055 }
5056
5057 INTERNAL_FUNC int emstorage_update_mailbox_modifiable_yn(int account_id, int local_yn, char *mailbox_name, int modifiable_yn, int transaction, int *err_code)
5058 {
5059         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);
5060         int rc, ret = false;
5061         int error = EMAIL_ERROR_NONE;
5062         char sql_query_string[QUERY_SIZE] = {0, };
5063         char *replaced_mailbox_name = NULL;
5064
5065         if (mailbox_name) {     
5066                 if (strstr(mailbox_name, "'")) {
5067                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5068                 } else {
5069                         replaced_mailbox_name = strdup(mailbox_name);
5070                 }
5071         }
5072
5073         sqlite3 *local_db_handle = emstorage_get_db_connection();
5074
5075         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5076
5077         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5078                 "UPDATE mail_box_tbl SET"
5079                 " modifiable_yn = %d"
5080                 " WHERE account_id = %d"
5081                 " AND local_yn = %d"
5082                 " AND mailbox_name = '%s'"
5083                 , modifiable_yn
5084                 , account_id
5085                 , local_yn
5086                 , replaced_mailbox_name);
5087         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5088         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5089                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5090
5091         ret = true;
5092
5093 FINISH_OFF:
5094         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5095         _DISCONNECT_DB;
5096
5097         EM_SAFE_FREE(replaced_mailbox_name);
5098
5099         if (err_code != NULL)
5100                 *err_code = error;
5101
5102         EM_DEBUG_FUNC_END("ret [%d]", ret);
5103         return ret;
5104
5105 }
5106
5107
5108 INTERNAL_FUNC int emstorage_update_mailbox_total_count(int account_id, int input_mailbox_id, int total_count_on_server, int transaction, int *err_code)
5109 {
5110         EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], total_count_on_server[%d], transaction[%d], err_code[%p]", account_id, input_mailbox_id, total_count_on_server,  transaction, err_code);
5111         int rc, ret = false;
5112         int error = EMAIL_ERROR_NONE;
5113         char sql_query_string[QUERY_SIZE] = {0, };
5114
5115         if (account_id <= 0 || input_mailbox_id <= 0)  {
5116                 EM_DEBUG_EXCEPTION("account_id[%d], input_mailbox_id[%d]", account_id, input_mailbox_id);
5117
5118                 if (err_code != NULL)
5119                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5120                 return false;
5121         }
5122         sqlite3 *local_db_handle = emstorage_get_db_connection();
5123
5124         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5125
5126         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5127                 "UPDATE mail_box_tbl SET"
5128                 " total_mail_count_on_server = %d"
5129                 " WHERE account_id = %d"
5130                 " AND mailbox_id = %d"
5131                 , total_count_on_server
5132                 , account_id
5133                 , input_mailbox_id);
5134         EM_DEBUG_LOG("query[%s]", sql_query_string);
5135         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5136         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5137                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5138
5139         ret = true;
5140
5141 FINISH_OFF:
5142         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5143         _DISCONNECT_DB;
5144
5145         if (err_code != NULL)
5146                 *err_code = error;
5147
5148         EM_DEBUG_FUNC_END("ret [%d]", ret);
5149         return ret;
5150
5151 }
5152
5153
5154 INTERNAL_FUNC int emstorage_update_mailbox(int account_id, int local_yn, int input_mailbox_id, emstorage_mailbox_tbl_t* result_mailbox, int transaction, int *err_code)
5155 {
5156         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);
5157
5158         if (account_id < FIRST_ACCOUNT_ID || input_mailbox_id <= 0 || !result_mailbox)  {
5159                 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);
5160
5161                 if (err_code != NULL)
5162                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5163                 return false;
5164         }
5165
5166         int rc, ret = false;
5167         int error = EMAIL_ERROR_NONE;
5168         char sql_query_string[QUERY_SIZE] = {0, };
5169         DB_STMT hStmt = NULL;
5170         int i = 0;
5171
5172         sqlite3 *local_db_handle = emstorage_get_db_connection();
5173         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5174
5175         if (local_yn != -1) {
5176                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5177                         "UPDATE mail_box_tbl SET"
5178                         "  mailbox_id = ?"
5179                         ", mailbox_name = ?"
5180                         ", mailbox_type = ?"
5181                         ", alias = ?"
5182                         ", deleted_flag = ?"
5183                         ", modifiable_yn= ?"
5184                         ", mail_slot_size= ?"
5185                         ", total_mail_count_on_server = ?"
5186                         " WHERE account_id = %d"
5187                         " AND local_yn = %d"
5188                         " AND mailbox_id = '%d'"
5189                         , account_id
5190                         , local_yn
5191                         , input_mailbox_id);
5192         }
5193         else {
5194                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5195                         "UPDATE mail_box_tbl SET"
5196                         "  mailbox_id = ?"
5197                         ", mailbox_name = ?"
5198                         ", mailbox_type = ?"
5199                         ", alias = ?"
5200                         ", deleted_flag = ?"
5201                         ", modifiable_yn= ?"
5202                         ", mail_slot_size= ?"
5203                         ", total_mail_count_on_server = ?"
5204                         " WHERE account_id = %d"
5205                         " AND mailbox_id = '%d'"
5206                         , account_id
5207                         , input_mailbox_id);
5208         }
5209
5210
5211
5212         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5213         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5214                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5215
5216         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_id);
5217         _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);
5218         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_type);
5219         _bind_stmt_field_data_string(hStmt, i++, (char *)result_mailbox->alias ? result_mailbox->alias : "", 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
5220         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->deleted_flag);
5221         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->modifiable_yn);
5222         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mail_slot_size);
5223         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->total_mail_count_on_server);
5224
5225
5226         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5227         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
5228                 ("sqlite3_step fail:%d", rc));
5229         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5230                 ("sqlite3_step fail:%d", rc));
5231
5232         ret = true;
5233
5234 FINISH_OFF:
5235         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5236         _DISCONNECT_DB;
5237
5238         if (hStmt != NULL)  {
5239                 rc = sqlite3_finalize(hStmt);
5240                 if (rc != SQLITE_OK)  {
5241                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5242                         error = EMAIL_ERROR_DB_FAILURE;
5243                 }
5244         }
5245
5246         if (err_code != NULL)
5247                 *err_code = error;
5248
5249         EM_DEBUG_FUNC_END("ret [%d]", ret);
5250         return ret;
5251 }
5252
5253
5254 INTERNAL_FUNC int emstorage_update_mailbox_type(int account_id, int local_yn, char *mailbox_name, email_mailbox_type_e new_mailbox_type, int transaction, int *err_code)
5255 {
5256         EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], mailbox_name[%s], new_mailbox_type[%d], transaction[%d], err_code[%p]", account_id, local_yn, mailbox_name, new_mailbox_type, transaction, err_code);
5257
5258         if (account_id < FIRST_ACCOUNT_ID || !mailbox_name)  {
5259                 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], mailbox_name[%s]", account_id, local_yn, mailbox_name);
5260
5261                 if (err_code != NULL)
5262                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5263                 return false;
5264         }
5265
5266         int rc, ret = false;
5267         int error = EMAIL_ERROR_NONE;
5268         char sql_query_string[QUERY_SIZE] = {0, };
5269         char *replaced_mailbox_name = NULL;
5270
5271         if (strstr(mailbox_name, "'")) {
5272                 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5273         } else {
5274                 replaced_mailbox_name = strdup(mailbox_name);
5275         }
5276
5277         EM_DEBUG_LOG("replaced_mailbox_name : [%s]", replaced_mailbox_name);
5278
5279         sqlite3 *local_db_handle = emstorage_get_db_connection();
5280         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5281
5282         EM_DEBUG_LOG("emstorage_update_mailbox_type");
5283
5284         DB_STMT hStmt_box_tbl = NULL;
5285         DB_STMT hStmt_mail_tbl = NULL;
5286         int i = 0;
5287
5288         /*  Update mail_box_tbl */
5289         if (local_yn != -1) {
5290                 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
5291                         "UPDATE mail_box_tbl SET"
5292                         " mailbox_type = ?"
5293                         " WHERE account_id = %d"
5294                         " AND local_yn = %d"
5295                         " AND mailbox_name = '%s'"
5296                         , account_id
5297                         , local_yn
5298                         , replaced_mailbox_name);
5299         }
5300         else {
5301                 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
5302                         "UPDATE mail_box_tbl SET"
5303                         " mailbox_type = ?"
5304                         " WHERE account_id = %d"
5305                         " AND mailbox_name = '%s'"
5306                         , account_id
5307                         , replaced_mailbox_name);
5308         }
5309
5310         EM_DEBUG_LOG("SQL(%s)", sql_query_string);
5311
5312         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_box_tbl, NULL), rc);
5313
5314         if(SQLITE_OK != rc) {
5315                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
5316                 error = EMAIL_ERROR_DB_FAILURE;
5317                 goto FINISH_OFF; 
5318         }
5319
5320         _bind_stmt_field_data_int(hStmt_box_tbl, i++, new_mailbox_type);
5321
5322
5323         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_box_tbl), rc);
5324
5325         if(rc == SQLITE_FULL) {
5326                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
5327                 error   = EMAIL_ERROR_MAIL_MEMORY_FULL;
5328                 goto FINISH_OFF; 
5329         }
5330         
5331         if(rc != SQLITE_ROW && rc != SQLITE_DONE) {
5332                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
5333                 error = EMAIL_ERROR_DB_FAILURE;
5334                 goto FINISH_OFF; 
5335         }
5336         
5337
5338         /*  Update mail_tbl */
5339         i = 0;
5340         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5341                         "UPDATE mail_tbl SET"
5342                         " mailbox_type = ?"
5343                         " WHERE account_id = %d"
5344                         " AND mailbox_name = '%s'"
5345                         , account_id
5346                         , replaced_mailbox_name);
5347
5348         EM_DEBUG_LOG("SQL[%s]", sql_query_string);
5349
5350         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_mail_tbl, NULL), rc);
5351         if(SQLITE_OK != rc) {
5352                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
5353                 error = EMAIL_ERROR_DB_FAILURE;
5354                 goto FINISH_OFF; 
5355         }
5356         
5357         _bind_stmt_field_data_int(hStmt_mail_tbl, i++, new_mailbox_type);
5358
5359         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_mail_tbl), rc);
5360         if(rc == SQLITE_FULL) {
5361                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
5362                 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
5363                 goto FINISH_OFF; 
5364         }
5365         
5366         if(rc != SQLITE_ROW && rc != SQLITE_DONE){
5367                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
5368                 error = EMAIL_ERROR_DB_FAILURE;
5369                 goto FINISH_OFF; 
5370         }
5371
5372         ret = true;
5373
5374 FINISH_OFF:
5375         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5376         _DISCONNECT_DB;
5377
5378         EM_SAFE_FREE(replaced_mailbox_name);
5379
5380         if (hStmt_box_tbl != NULL)  {
5381                 rc = sqlite3_finalize(hStmt_box_tbl);
5382                 if (rc != SQLITE_OK)  {
5383                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5384                         error = EMAIL_ERROR_DB_FAILURE;
5385                 }
5386         }
5387
5388         if (hStmt_mail_tbl != NULL)  {
5389                 rc = sqlite3_finalize(hStmt_mail_tbl);
5390                 if (rc != SQLITE_OK)  {
5391                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5392                         error = EMAIL_ERROR_DB_FAILURE;
5393                 }
5394         }
5395
5396         if (err_code != NULL)
5397                 *err_code = error;
5398
5399         EM_DEBUG_FUNC_END("ret [%d]", ret);
5400         return ret;
5401 }
5402
5403 INTERNAL_FUNC int emstorage_set_local_mailbox(int input_mailbox_id, int input_is_local_mailbox, int transaction)
5404 {
5405         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);
5406
5407         int rc, ret = false;
5408         int error = EMAIL_ERROR_NONE;
5409         char sql_query_string[QUERY_SIZE] = {0, };
5410
5411         if (input_mailbox_id < 0)  {
5412                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5413                 return EMAIL_ERROR_INVALID_PARAM;
5414         }
5415
5416         sqlite3 *local_db_handle = emstorage_get_db_connection();
5417
5418         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5419
5420         EM_DEBUG_LOG("emstorage_update_mailbox_type");
5421
5422         DB_STMT hStmt = NULL;
5423         int i = 0;
5424
5425         /*  Update mail_box_tbl */
5426         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5427                 "UPDATE mail_box_tbl SET"
5428                 " local_yn = ?"
5429                 " WHERE mailbox_id = %d"
5430                 , input_mailbox_id);
5431
5432         EM_DEBUG_LOG("SQL(%s)", sql_query_string);
5433
5434         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5435         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5436                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5437
5438         _bind_stmt_field_data_int(hStmt, i++, input_is_local_mailbox);
5439
5440         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5441         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
5442                 ("sqlite3_step fail:%d", rc));
5443         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5444                 ("sqlite3_step fail:%d", rc));
5445
5446         if (hStmt != NULL)  {
5447                 rc = sqlite3_finalize(hStmt);
5448                 if (rc != SQLITE_OK)  {
5449                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5450                         error = EMAIL_ERROR_DB_FAILURE;
5451                 }
5452                 hStmt = NULL;
5453         }
5454
5455         ret = true;
5456
5457 FINISH_OFF:
5458         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5459         _DISCONNECT_DB;
5460
5461         if (hStmt != NULL)  {
5462                 rc = sqlite3_finalize(hStmt);
5463                 if (rc != SQLITE_OK)  {
5464                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5465                         error = EMAIL_ERROR_DB_FAILURE;
5466                 }
5467         }
5468
5469         EM_DEBUG_FUNC_END("error [%d]", error);
5470         return error;
5471 }
5472
5473 INTERNAL_FUNC int emstorage_set_field_of_mailbox_with_integer_value(int input_account_id, int *input_mailbox_id_array, int input_mailbox_id_count, char *input_field_name, int input_value, int transaction)
5474 {
5475         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);
5476         int i = 0;
5477         int err = EMAIL_ERROR_NONE;
5478         int rc = 0;
5479         int result = false;
5480         int cur_mailbox_id_string = 0;
5481         int mailbox_id_string_buffer_length = 0;
5482         char  sql_query_string[QUERY_SIZE] = {0, };
5483         char *mailbox_id_string_buffer = NULL;
5484         char *parameter_string = NULL;
5485         sqlite3 *local_db_handle = NULL;
5486
5487         if (input_mailbox_id_array == NULL || input_mailbox_id_count == 0 || input_field_name == NULL) {
5488                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5489                 return EMAIL_ERROR_INVALID_PARAM;
5490         }
5491
5492         local_db_handle = emstorage_get_db_connection();
5493
5494         /* Generating mail id list string */
5495         mailbox_id_string_buffer_length = MAILBOX_ID_STRING_LENGTH * input_mailbox_id_count;
5496
5497         mailbox_id_string_buffer = em_malloc(mailbox_id_string_buffer_length);
5498
5499         if(!mailbox_id_string_buffer) {
5500                 EM_DEBUG_EXCEPTION("em_malloc failed");
5501                 err = EMAIL_ERROR_OUT_OF_MEMORY;
5502                 goto FINISH_OFF;
5503         }
5504
5505         for(i = 0; i < input_mailbox_id_count; i++)
5506                 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]);
5507
5508         if(EM_SAFE_STRLEN(mailbox_id_string_buffer) > 1)
5509                 mailbox_id_string_buffer[EM_SAFE_STRLEN(mailbox_id_string_buffer) - 1] = NULL_CHAR;
5510
5511         /* Generating notification parameter string */
5512         parameter_string = em_malloc(mailbox_id_string_buffer_length + EM_SAFE_STRLEN(input_field_name) + 2);
5513
5514         if(!parameter_string) {
5515                 EM_DEBUG_EXCEPTION("em_malloc failed");
5516                 err = EMAIL_ERROR_OUT_OF_MEMORY;
5517                 goto FINISH_OFF;
5518         }
5519
5520         SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", input_field_name, 0x01, mailbox_id_string_buffer);
5521
5522         /* Write query string */
5523         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);
5524
5525         EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
5526
5527         /* Execute query */
5528         EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
5529         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5530         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5531                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5532         if (sqlite3_changes(local_db_handle) == 0)
5533                 EM_DEBUG_LOG("no mail matched...");
5534
5535         result = true;
5536
5537 FINISH_OFF:
5538         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, result, err);
5539         _DISCONNECT_DB;
5540
5541         if (result && parameter_string && !emcore_notify_storage_event(NOTI_MAILBOX_FIELD_UPDATE, input_account_id, 0, parameter_string, input_value))
5542                 EM_DEBUG_EXCEPTION("emcore_notify_storage_event failed : NOTI_MAIL_FIELD_UPDATE [%s,%d]", input_field_name, input_value);
5543
5544         EM_SAFE_FREE(mailbox_id_string_buffer);
5545         EM_SAFE_FREE(parameter_string);
5546
5547         EM_DEBUG_FUNC_END("err [%d]", err);
5548         return err;
5549 }
5550
5551 INTERNAL_FUNC int emstorage_add_mailbox(emstorage_mailbox_tbl_t* mailbox_tbl, int transaction, int *err_code)
5552 {
5553         EM_DEBUG_FUNC_BEGIN("mailbox_tbl[%p], transaction[%d], err_code[%p]", mailbox_tbl, transaction, err_code);
5554
5555         if (!mailbox_tbl)  {
5556                 if (err_code != NULL)
5557                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5558                 return false;
5559         }
5560
5561         int rc, ret = false;
5562         int error = EMAIL_ERROR_NONE;
5563         DB_STMT hStmt = NULL;
5564         char sql_query_string[QUERY_SIZE] = {0,};
5565         char **result = NULL;
5566         time_t current_time;
5567         sqlite3 *local_db_handle = emstorage_get_db_connection();
5568         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5569
5570         EM_SAFE_STRCPY(sql_query_string, "SELECT max(rowid) FROM mail_box_tbl;");
5571
5572         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5573         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
5574                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5575
5576         time(&current_time);
5577
5578         if (NULL == result[1])
5579                 rc = 1;
5580         else
5581                 rc = atoi(result[1]) + 1;
5582         sqlite3_free_table(result);
5583
5584         memset(sql_query_string, 0, sizeof(char) * QUERY_SIZE);
5585
5586         mailbox_tbl->mailbox_id = rc;
5587
5588         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5589                 "INSERT INTO mail_box_tbl VALUES "
5590                 "( ?"    /* mailbox_id */
5591                 ", ?"    /* account_id */
5592                 ", ?"    /* local_yn */
5593                 ", ?"    /* mailbox_name */
5594                 ", ?"    /* mailbox_type */
5595                 ", ?"    /* alias */
5596                 ", ?"    /* deleted_flag */
5597                 ", ?"    /* modifiable_yn */
5598                 ", ?"    /* total_mail_count_on_server */
5599                 ", ?"    /* has_archived_mails */
5600                 ", ?"    /* mail_slot_size */
5601                 ", ?"    /* no_select */
5602                 ", ? )");/* last_sync_time */
5603
5604
5605         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5606         EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
5607         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5608                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5609
5610         int col_index = 0;
5611
5612         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_id);
5613         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->account_id);
5614         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->local_yn);
5615         _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
5616         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_type);
5617         _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->alias, 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
5618         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->deleted_flag);
5619         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->modifiable_yn);
5620         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->total_mail_count_on_server);
5621         _bind_stmt_field_data_int(hStmt, col_index++, 0);
5622         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mail_slot_size);
5623         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->no_select);
5624         _bind_stmt_field_data_int(hStmt, col_index++, current_time);
5625
5626
5627         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5628         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5629                 ("sqlite3_step fail:%dn", rc));
5630
5631         ret = true;
5632
5633 FINISH_OFF:
5634         if (hStmt != NULL)  {
5635                 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
5636
5637                 rc = sqlite3_finalize(hStmt);
5638                 if (rc != SQLITE_OK)  {
5639                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
5640                         error = EMAIL_ERROR_DB_FAILURE;
5641                 }
5642         }
5643
5644         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5645         _DISCONNECT_DB;
5646
5647         if (error == EMAIL_ERROR_NONE && !emcore_notify_storage_event(NOTI_MAILBOX_ADD, mailbox_tbl->account_id, mailbox_tbl->mailbox_id, mailbox_tbl->mailbox_name, mailbox_tbl->mailbox_type))
5648                 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
5649
5650         if (err_code != NULL)
5651                 *err_code = error;
5652
5653         EM_DEBUG_FUNC_END("ret [%d]", ret);
5654         return ret;
5655 }
5656
5657 INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(int account_id, int modifiable_yn, int transaction, int *err_code)
5658 {
5659         EM_DEBUG_FUNC_BEGIN("account_id[%d], modifiable_yn[%d], err_code[%p]", account_id, modifiable_yn, err_code);
5660
5661         if (account_id < FIRST_ACCOUNT_ID)  {
5662
5663                 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
5664
5665                 if (err_code != NULL)
5666                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5667                 return false;
5668         }
5669         int rc, ret = false;
5670         int error = EMAIL_ERROR_NONE;
5671         char sql_query_string[QUERY_SIZE] = {0,};
5672
5673         sqlite3 *local_db_handle = emstorage_get_db_connection();
5674         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5675
5676
5677         SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET modifiable_yn = %d WHERE account_id = %d", modifiable_yn, account_id);
5678
5679         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5680         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5681                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5682
5683         rc = sqlite3_changes(local_db_handle);
5684         if (rc == 0)
5685                 EM_DEBUG_EXCEPTION("All mailbox_name modifiable_yn set to 0 already");
5686
5687
5688         ret = true;
5689
5690 FINISH_OFF:
5691         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5692         _DISCONNECT_DB;
5693
5694         if (err_code != NULL)
5695                 *err_code = error;
5696         EM_DEBUG_FUNC_END("ret [%d]", ret);
5697         return ret;
5698
5699
5700 }
5701
5702 INTERNAL_FUNC int emstorage_delete_mailbox(int account_id, int local_yn, int input_mailbox_id, int transaction, int *err_code)
5703 {
5704         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);
5705
5706         if (account_id < FIRST_ACCOUNT_ID)  {
5707                 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
5708
5709                 if (err_code != NULL)
5710                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5711                 return false;
5712         }
5713
5714         int rc, ret = false;
5715         int error = EMAIL_ERROR_NONE;
5716         char sql_query_string[QUERY_SIZE] = {0, };
5717
5718         sqlite3 *local_db_handle = emstorage_get_db_connection();
5719         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5720
5721         if (local_yn == -1)
5722                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_box_tbl WHERE account_id = %d ", account_id);
5723         else
5724                 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);
5725
5726         if (input_mailbox_id > 0)  {            /* 0 means all mailbox */
5727                 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);
5728         }
5729
5730         EM_DEBUG_LOG("mailbox sql_query_string [%s]", sql_query_string);
5731         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5732         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5733                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5734
5735         rc = sqlite3_changes(local_db_handle);
5736         if (rc == 0)  {
5737                 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
5738                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5739                 ret = true;
5740         }
5741         ret = true;
5742
5743 FINISH_OFF:
5744
5745         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5746         _DISCONNECT_DB;
5747
5748         if(error == EMAIL_ERROR_NONE) {
5749                 if (!emcore_notify_storage_event(NOTI_MAILBOX_DELETE, account_id, input_mailbox_id, NULL, 0))
5750                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
5751         }
5752
5753         if (err_code != NULL)
5754                 *err_code = error;
5755
5756         EM_DEBUG_FUNC_END("ret [%d]", ret);
5757         return ret;
5758 }
5759
5760 INTERNAL_FUNC int emstorage_free_mailbox(emstorage_mailbox_tbl_t** mailbox_list, int count, int *err_code)
5761 {
5762         EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d], err_code[%p]", mailbox_list, count, err_code);
5763
5764         int ret = false;
5765         int error = EMAIL_ERROR_NONE;
5766
5767         if (count > 0)  {
5768                 if (!mailbox_list || !*mailbox_list)  {
5769                         EM_DEBUG_EXCEPTION(" mailbox_list[%p], count[%d]", mailbox_list, count);
5770
5771                         error = EMAIL_ERROR_INVALID_PARAM;
5772                         goto FINISH_OFF;
5773                 }
5774
5775                 emstorage_mailbox_tbl_t* p = *mailbox_list;
5776                 int i = 0;
5777
5778                 for (; i < count; i++) {
5779                         EM_SAFE_FREE(p[i].mailbox_name);
5780                         EM_SAFE_FREE(p[i].alias);
5781                 }
5782
5783                 EM_SAFE_FREE(p); *mailbox_list = NULL;
5784         }
5785
5786         ret = true;
5787
5788 FINISH_OFF:
5789         if (err_code != NULL)
5790                 *err_code = error;
5791
5792         EM_DEBUG_FUNC_END("ret [%d]", ret);
5793         return ret;
5794 }
5795
5796 INTERNAL_FUNC int emstorage_get_count_read_mail_uid(int account_id, char *mailbox_name, int *count, int transaction, int *err_code)
5797 {
5798         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);
5799
5800         if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !count)  {
5801                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], count[%p], exist[%p]", account_id, mailbox_name, count);
5802
5803                 if (err_code != NULL)
5804                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5805                 return false;
5806         }
5807
5808         int rc = -1, ret = false;
5809         int error = EMAIL_ERROR_NONE;
5810         char sql_query_string[QUERY_SIZE] = {0, };
5811         char *replaced_mailbox_name = NULL;
5812
5813         if (strstr(mailbox_name, "'")) {
5814                 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5815         } else {
5816                 replaced_mailbox_name = EM_SAFE_STRDUP(mailbox_name);
5817         }
5818
5819         EM_DEBUG_LOG("replaced_mailbox_name : [%s]", replaced_mailbox_name);
5820
5821
5822         sqlite3 *local_db_handle = emstorage_get_db_connection();
5823         EMSTORAGE_START_READ_TRANSACTION(transaction);
5824         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);
5825         EM_DEBUG_LOG(">>> SQL [ %s ] ", sql_query_string);
5826
5827         char **result;
5828
5829         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5830         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
5831                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5832
5833         *count = atoi(result[1]);
5834         sqlite3_free_table(result);
5835
5836         ret = true;
5837
5838 FINISH_OFF:
5839         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5840         _DISCONNECT_DB;
5841
5842         EM_SAFE_FREE(replaced_mailbox_name);
5843
5844         if (err_code != NULL)
5845                 *err_code = error;
5846
5847         EM_DEBUG_FUNC_END("ret [%d]", ret);
5848         return ret;
5849 }
5850
5851
5852
5853 INTERNAL_FUNC int emstorage_check_read_mail_uid(int account_id, char *mailbox_name, char *uid, int *exist, int transaction, int *err_code)
5854 {
5855         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);
5856
5857         if (account_id < FIRST_ACCOUNT_ID || !uid || !exist)  {
5858                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], uid[%p], exist[%p]", account_id, mailbox_name , uid, exist);
5859
5860                 if (err_code != NULL)
5861                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5862                 return false;
5863         }
5864
5865         int rc = -1, ret = false;
5866         int error = EMAIL_ERROR_NONE;
5867         char sql_query_string[QUERY_SIZE] = {0, };
5868         char *replaced_mailbox_name = NULL;
5869
5870         EM_DEBUG_LOG("replaced_mailbox_name : [%s]", replaced_mailbox_name);
5871
5872         sqlite3 *local_db_handle = emstorage_get_db_connection();
5873         EMSTORAGE_START_READ_TRANSACTION(transaction);
5874
5875         if (mailbox_name)  {
5876                 if (strstr(mailbox_name, "'")) {
5877                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5878                 } else {
5879                         replaced_mailbox_name = strdup(mailbox_name);
5880                 }
5881
5882                 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 s_uid = '%s' ", account_id, replaced_mailbox_name, uid);
5883         }
5884         else  {
5885                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_read_mail_uid_tbl WHERE account_id = %d AND s_uid = '%s' ", account_id, uid);
5886         }
5887
5888         char **result;
5889
5890         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5891         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
5892                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5893
5894         *exist = atoi(result[1]);
5895         sqlite3_free_table(result);
5896
5897         if (*exist > 0)
5898                 *exist = 1;
5899         else
5900                 *exist = 0;
5901
5902         ret = true;
5903
5904 FINISH_OFF:
5905         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5906         _DISCONNECT_DB;
5907
5908         EM_SAFE_FREE(replaced_mailbox_name);
5909
5910         if (err_code != NULL)
5911                 *err_code = error;
5912
5913         EM_DEBUG_FUNC_END("ret [%d]", ret);
5914         return ret;
5915 }
5916
5917 INTERNAL_FUNC int emstorage_get_downloaded_mail(int mail_id, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
5918 {
5919         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], err_code[%p]", mail_id, mail, err_code);
5920
5921         if (!mail || mail_id <= 0) {
5922                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
5923                 if (err_code != NULL)
5924                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5925                 return false;
5926         }
5927
5928         int rc, ret = false, temp_rule;
5929         int error = EMAIL_ERROR_NONE;
5930         DB_STMT hStmt = NULL;
5931         char sql_query_string[QUERY_SIZE] = {0, };
5932
5933         sqlite3 *local_db_handle = emstorage_get_db_connection();
5934         EMSTORAGE_START_READ_TRANSACTION(transaction);
5935
5936         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE local_uid = %d", mail_id);
5937
5938         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5939         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
5940
5941         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5942                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5943
5944
5945         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5946         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5947                 ("sqlite3_step fail:%d", rc));
5948
5949         *mail = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
5950         if (*mail == NULL) {
5951                 error = EMAIL_ERROR_OUT_OF_MEMORY;
5952                 EM_DEBUG_EXCEPTION("Memory allocation for mail failed.");
5953                 goto FINISH_OFF;
5954
5955         }
5956         memset(*mail, 0x00, sizeof(emstorage_mail_tbl_t));
5957
5958         _get_stmt_field_data_int(hStmt, &((*mail)->account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5959         _get_stmt_field_data_int(hStmt, &((*mail)->mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5960         _get_stmt_field_data_int(hStmt, &((*mail)->mail_id), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5961         _get_stmt_field_data_string(hStmt, &((*mail)->server_mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5962         _get_stmt_field_data_string(hStmt, &((*mail)->server_mail_id), 0, S_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5963         _get_stmt_field_data_int(hStmt, &((*mail)->mail_size), DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5964         _get_stmt_field_data_int(hStmt, &temp_rule, FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5965
5966         (*mail)->server_mail_status = 1;
5967
5968         ret = true;
5969
5970 FINISH_OFF:
5971         if (hStmt != NULL)  {
5972                 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
5973                 rc = sqlite3_finalize(hStmt);
5974                 if (rc != SQLITE_OK)  {
5975                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5976                         error = EMAIL_ERROR_DB_FAILURE;
5977                 }
5978         }
5979
5980         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5981         _DISCONNECT_DB;
5982
5983         if (err_code != NULL)
5984                 *err_code = error;
5985
5986         EM_DEBUG_FUNC_END("ret [%d]", ret);
5987         return ret;
5988 }
5989
5990 INTERNAL_FUNC int emstorage_get_downloaded_list(int account_id, int mailbox_id, emstorage_read_mail_uid_tbl_t** read_mail_uid, int *count, int transaction, int *err_code)
5991 {
5992         EM_PROFILE_BEGIN(emStorageGetDownloadList);
5993         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);
5994         if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid || !count)  {
5995                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_id[%s], read_mail_uid[%p], count[%p]", account_id, mailbox_id, read_mail_uid, count);
5996
5997                 if (err_code != NULL)
5998                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5999                 return false;
6000         }
6001
6002         int rc, ret = false;
6003         int error = EMAIL_ERROR_NONE;
6004
6005         DB_STMT hStmt = NULL;
6006         char sql_query_string[QUERY_SIZE] = {0, };
6007
6008         emstorage_read_mail_uid_tbl_t* p_data_tbl = NULL;
6009         int i = 0;
6010
6011         sqlite3 *local_db_handle = emstorage_get_db_connection();
6012         EMSTORAGE_START_READ_TRANSACTION(transaction);
6013
6014         if (mailbox_id)
6015                 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);
6016         else
6017                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
6018
6019         EM_DEBUG_LOG(" sql_query_string : %s", sql_query_string);
6020
6021
6022
6023         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6024         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6025         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6026                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6027
6028
6029         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6030         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6031                 ("sqlite3_step fail:%d", rc));
6032
6033         char **result;
6034         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL); */
6035         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL), rc);
6036         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6037                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6038
6039         sqlite3_free_table(result);
6040         if (*count == 0)  {
6041                 EM_DEBUG_LOG("No mail found in mail_read_mail_uid_tbl");
6042                 ret = true;
6043                 goto FINISH_OFF;
6044         }
6045
6046
6047         if (!(p_data_tbl = (emstorage_read_mail_uid_tbl_t*)malloc(sizeof(emstorage_read_mail_uid_tbl_t) * *count)))  {
6048                 EM_DEBUG_EXCEPTION(" malloc failed...");
6049                 error = EMAIL_ERROR_OUT_OF_MEMORY;
6050                 goto FINISH_OFF;
6051         }
6052
6053         memset(p_data_tbl, 0x00, sizeof(emstorage_read_mail_uid_tbl_t)* *count);
6054
6055         for (i = 0; i < *count; ++i)  {
6056                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6057                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id),LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6058                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].local_uid), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6059                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6060                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].s_uid), 0, S_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6061                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].data1), DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6062                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].data2), 0, DATA2_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6063                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag), FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6064
6065                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6066                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6067                         ("sqlite3_step fail:%d", rc));
6068         }
6069
6070         ret = true;
6071
6072 FINISH_OFF:
6073         if (ret == true)
6074                 *read_mail_uid = p_data_tbl;
6075         else if (p_data_tbl)
6076                 emstorage_free_read_mail_uid(&p_data_tbl, *count, NULL);
6077
6078         if (hStmt != NULL)  {
6079                 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
6080                 rc = sqlite3_finalize(hStmt);
6081                 if (rc != SQLITE_OK)  {
6082                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6083
6084                         error = EMAIL_ERROR_DB_FAILURE;
6085                 }
6086         }
6087
6088         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6089         _DISCONNECT_DB;
6090
6091         if (err_code != NULL)
6092                 *err_code = error;
6093
6094         EM_PROFILE_END(emStorageGetDownloadList);
6095         EM_DEBUG_FUNC_END("ret [%d]", ret);
6096         return ret;
6097 }
6098
6099 INTERNAL_FUNC int emstorage_get_downloaded_mail_size(int account_id, char *mailbox_id, int local_uid, char *mailbox_name, char *uid, int *mail_size, int transaction, int *err_code)
6100 {
6101         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);
6102
6103         if (account_id < FIRST_ACCOUNT_ID || !mail_size)  {
6104                 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);
6105
6106                 if (err_code != NULL)
6107                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6108                 return false;
6109         }
6110
6111         int rc, ret = false;
6112         int error = EMAIL_ERROR_NONE;
6113         DB_STMT hStmt = NULL;
6114         char sql_query_string[QUERY_SIZE] = {0, };
6115         char *replaced_mailbox_name = NULL;
6116
6117         sqlite3 *local_db_handle = emstorage_get_db_connection();
6118         EMSTORAGE_START_READ_TRANSACTION(transaction);
6119
6120         if (mailbox_name) {
6121                 if (strstr(mailbox_name, "'")) {
6122                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6123                 } else {
6124                         replaced_mailbox_name = strdup(mailbox_name);
6125                 }
6126
6127                 EM_DEBUG_LOG("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6128
6129                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6130                         "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6131                         "WHERE account_id = %d "
6132                         "AND mailbox_id = '%s' "
6133                         "AND local_uid = %d "
6134                         "AND mailbox_name = '%s' "
6135                         "AND s_uid = '%s'",
6136                         account_id, mailbox_id, local_uid, replaced_mailbox_name, uid);
6137         }
6138         else {
6139                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6140                         "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6141                         "WHERE account_id = %d "
6142                         "AND mailbox_id = '%s' "
6143                         "AND local_uid = %d "
6144                         "AND s_uid = '%s'",
6145                         account_id, mailbox_id, local_uid, uid);
6146         }
6147
6148
6149         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6150         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6151                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6152
6153
6154         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6155         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6156                 ("sqlite3_step fail:%d", rc));
6157
6158         if (rc == SQLITE_DONE) {
6159                 EM_DEBUG_EXCEPTION(" no matched mail found....");
6160                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
6161                 goto FINISH_OFF;
6162         }
6163
6164         _get_stmt_field_data_int(hStmt, mail_size, 0);
6165
6166         ret = true;
6167
6168 FINISH_OFF:
6169         EM_SAFE_FREE(replaced_mailbox_name);
6170
6171         if (hStmt != NULL)  {
6172                 rc = sqlite3_finalize(hStmt);
6173                 if (rc != SQLITE_OK)  {
6174                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6175
6176                         error = EMAIL_ERROR_DB_FAILURE;
6177                 }
6178         }
6179
6180         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6181         _DISCONNECT_DB;
6182
6183         if (err_code != NULL)
6184                 *err_code = error;
6185
6186         EM_DEBUG_FUNC_END("ret [%d]", ret);
6187         return ret;
6188 }
6189
6190 INTERNAL_FUNC int emstorage_add_downloaded_mail(emstorage_read_mail_uid_tbl_t* read_mail_uid, int transaction, int *err_code)
6191 {
6192         EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], transaction[%d], err_code[%p]", read_mail_uid, transaction, err_code);
6193
6194         if (!read_mail_uid)  {
6195                 EM_DEBUG_EXCEPTION("read_mail_uid[%p]", read_mail_uid);
6196                 if (err_code != NULL)
6197                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6198                 return false;
6199         }
6200
6201         int rc, rc2,  ret = false;
6202         int error = EMAIL_ERROR_NONE;
6203         DB_STMT hStmt = NULL;
6204         char sql_query_string[QUERY_SIZE] = {0, };
6205
6206         sqlite3 *local_db_handle = emstorage_get_db_connection();
6207         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6208
6209         char *sql = "SELECT max(rowid) FROM mail_read_mail_uid_tbl;";
6210         char **result = NULL;
6211
6212
6213         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6214         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6215                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6216
6217         if (NULL==result[1]) rc = 1;
6218         else rc = atoi(result[1])+1;
6219         sqlite3_free_table(result);
6220
6221         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6222                 "INSERT INTO mail_read_mail_uid_tbl VALUES "
6223                 "( ?"
6224                 ", ?"
6225                 ", ?"
6226                 ", ?"
6227                 ", ?"
6228                 ", ?"
6229                 ", ?"
6230                 ", ?"
6231                 ", ? )");
6232
6233
6234         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
6235         if (rc2 != SQLITE_OK)  {
6236                 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6237                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6238
6239                 error = EMAIL_ERROR_DB_FAILURE;
6240                 goto FINISH_OFF;
6241         }
6242
6243         EM_DEBUG_LOG("account_id VALUE [%d] ", read_mail_uid->account_id);
6244         EM_DEBUG_LOG("mailbox_id VALUE [%d] ", read_mail_uid->mailbox_id);
6245         EM_DEBUG_LOG("local_uid VALUE [%d] ", read_mail_uid->local_uid);
6246         EM_DEBUG_LOG("mailbox_name VALUE [%s] ", read_mail_uid->mailbox_name);
6247         EM_DEBUG_LOG("s_uid VALUE [%s] ", read_mail_uid->s_uid);
6248         EM_DEBUG_LOG("data1 VALUE [%d] ", read_mail_uid->data1);
6249         EM_DEBUG_LOG("data2 VALUE [%s] ", read_mail_uid->data2);
6250         EM_DEBUG_LOG("flag VALUE [%d] ", read_mail_uid->flag);
6251         EM_DEBUG_LOG("rc VALUE [%d] ", rc);
6252
6253         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->account_id);
6254         _bind_stmt_field_data_int(hStmt, LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->mailbox_id);
6255         _bind_stmt_field_data_int(hStmt, LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->local_uid);
6256         _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);
6257         _bind_stmt_field_data_string(hStmt, S_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, (char *)read_mail_uid->s_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6258         _bind_stmt_field_data_int(hStmt, DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->data1);
6259         _bind_stmt_field_data_string(hStmt, DATA2_IDX_IN_MAIL_READ_MAIL_UID_TBL, (char *)read_mail_uid->data2, 0, DATA2_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6260         _bind_stmt_field_data_int(hStmt, FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flag);
6261         _bind_stmt_field_data_int(hStmt, IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, rc);
6262
6263
6264         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6265         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
6266                 ("sqlite3_step fail:%d", rc));
6267         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6268                 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
6269
6270
6271         ret = true;
6272
6273 FINISH_OFF:
6274         if (hStmt != NULL)  {
6275                 EM_DEBUG_LOG(" sqlite3_finalize hStmt = %p", hStmt);
6276
6277                 rc = sqlite3_finalize(hStmt);
6278                 if (rc != SQLITE_OK)  {
6279                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6280                         error = EMAIL_ERROR_DB_FAILURE;
6281                 }
6282         }
6283
6284         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6285         _DISCONNECT_DB;
6286
6287         if (err_code != NULL)
6288                 *err_code = error;
6289
6290         EM_DEBUG_FUNC_END("ret [%d]", ret);
6291         return ret;
6292 }
6293
6294 #ifdef __FEATURE_BODY_SEARCH__
6295 INTERNAL_FUNC int emstorage_add_mail_text(emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
6296 {
6297         EM_DEBUG_FUNC_BEGIN("mail_text[%p], transaction[%d], err_code[%p]", mail_text, transaction, err_code);
6298
6299         if (!mail_text) {
6300                 EM_DEBUG_EXCEPTION("mail_text[%p]", mail_text);
6301                 if (err_code != NULL)
6302                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6303                 return false;
6304         }
6305
6306         int rc, rc2,  ret = false;
6307         int error = EMAIL_ERROR_NONE;
6308         DB_STMT hStmt = NULL;
6309         char sql_query_string[QUERY_SIZE] = {0, };
6310
6311         sqlite3 *local_db_handle = emstorage_get_db_connection();
6312         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6313
6314         char *sql = "SELECT max(rowid) FROM mail_text_tbl;";
6315         char **result = NULL;
6316
6317         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6318         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6319                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6320         sqlite3_free_table(result);
6321
6322         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6323                 "INSERT INTO mail_text_tbl VALUES "
6324                 "( ?"
6325                 ", ?"
6326                 ", ?"
6327                 ", ? )");
6328
6329         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
6330         if (rc2 != SQLITE_OK)  {
6331                 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6332                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc2, sqlite3_errmsg(local_db_handle));
6333
6334                 error = EMAIL_ERROR_DB_FAILURE;
6335                 goto FINISH_OFF;
6336         }
6337
6338         EM_DEBUG_LOG("mail_id VALUE [%d] ", mail_text->mail_id);
6339         EM_DEBUG_LOG("account_id VALUE [%d] ", mail_text->account_id);
6340         EM_DEBUG_LOG("mailbox_id VALUE [%d] ", mail_text->mailbox_id);
6341         EM_DEBUG_LOG("body_text VALUE [%s] ", mail_text->body_text);
6342
6343         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mail_id);
6344         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->account_id);
6345         _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mailbox_id);
6346         _bind_stmt_field_data_string(hStmt, BODY_TEXT_IDX_IN_MAIL_TEXT_TBL, (char *)mail_text->body_text, 0, -1);
6347
6348         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6349         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
6350                 ("sqlite3_step fail:%d", rc));
6351         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6352                 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
6353
6354         ret = true;
6355
6356 FINISH_OFF:
6357         if (hStmt != NULL) {
6358                 EM_DEBUG_LOG(" sqlite3_finalize hStmt = %p", hStmt);
6359
6360                 rc = sqlite3_finalize(hStmt);
6361                 if (rc != SQLITE_OK)  {
6362                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6363                         error = EMAIL_ERROR_DB_FAILURE;
6364                 }
6365         }
6366
6367         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6368         _DISCONNECT_DB;
6369
6370         if (err_code != NULL)
6371                 *err_code = error;
6372
6373         EM_DEBUG_FUNC_END("ret [%d]", ret);
6374         return ret;
6375 }
6376 #endif
6377
6378 INTERNAL_FUNC int emstorage_change_read_mail_uid(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)
6379 {
6380         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);
6381
6382         if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid)  {
6383                 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);
6384
6385                 if (err_code != NULL)
6386                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6387                 return false;
6388         }
6389
6390         int rc, ret = false;
6391         int error = EMAIL_ERROR_NONE;
6392         DB_STMT hStmt = NULL;
6393         char sql_query_string[QUERY_SIZE] = {0, };
6394
6395         sqlite3 *local_db_handle = emstorage_get_db_connection();
6396
6397         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6398
6399         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6400                 "UPDATE mail_read_mail_uid_tbl SET"
6401                 "  account_id = ?"
6402                 ", mailbox_id = ?"
6403                 ", local_uid  = ?"
6404                 ", mailbox_name = ?"
6405                 ", s_uid = ?"
6406                 ", data1 = ?"
6407                 ", data2 = ?"
6408                 ", flag  = ?"
6409                 " WHERE account_id = ?"
6410                 " AND mailbox_id  = ?"
6411                 " AND local_uid   = ?"
6412                 " AND mailbox_name= ?"
6413                 " AND s_uid = ?");
6414
6415
6416         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6417         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6418         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6419                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6420
6421
6422         int i = 0;
6423
6424         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->account_id);
6425         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->mailbox_id);
6426         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->local_uid);
6427         _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6428         _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->s_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6429         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->data1);
6430         _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->data2, 0, DATA2_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6431         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flag);
6432         _bind_stmt_field_data_int(hStmt, i++, account_id);
6433         _bind_stmt_field_data_int(hStmt, i++, mailbox_id);
6434         _bind_stmt_field_data_int(hStmt, i++, local_uid);
6435         _bind_stmt_field_data_string(hStmt, i++, (char*)mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6436         _bind_stmt_field_data_string(hStmt, i++, (char*)uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6437
6438
6439         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6440         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6441                 ("sqlite3_step fail:%d", rc));
6442
6443         ret = true;
6444
6445 FINISH_OFF:
6446         if (hStmt != NULL)  {
6447                 EM_DEBUG_LOG("sqlite3_finalize hStmt = %p", hStmt);
6448
6449                 rc = sqlite3_finalize(hStmt);
6450                 if (rc != SQLITE_OK)  {
6451                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6452
6453                         error = EMAIL_ERROR_DB_FAILURE;
6454                 }
6455         }
6456
6457         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6458         _DISCONNECT_DB;
6459
6460         if (err_code != NULL)
6461                 *err_code = error;
6462
6463         EM_DEBUG_FUNC_END("ret [%d]", ret);
6464         return ret;
6465 }
6466
6467 INTERNAL_FUNC int emstorage_remove_downloaded_mail(int account_id, char *mailbox_name, char *uid, int transaction, int *err_code)
6468 {
6469         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_name[%s], uid[%s], transaction[%d], err_code[%p]", account_id, mailbox_name, uid, transaction, err_code);
6470
6471         if (account_id < FIRST_ACCOUNT_ID)  {
6472                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%s], uid[%s]", account_id, mailbox_name, uid);
6473
6474                 if (err_code != NULL)
6475                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6476                 return false;
6477         }
6478
6479         int rc, ret = false;
6480         int error = EMAIL_ERROR_NONE;
6481         char sql_query_string[QUERY_SIZE] = {0, };
6482         char *replaced_mailbox_name = NULL;
6483
6484         sqlite3 *local_db_handle = emstorage_get_db_connection();
6485
6486         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6487
6488         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d ", account_id);
6489
6490         if (mailbox_name) {             /*  NULL means all mailbox_name */
6491                 if (strstr(mailbox_name, "'")) {
6492                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6493                 } else {
6494                         replaced_mailbox_name = strdup(mailbox_name);
6495                 }
6496
6497                 SNPRINTF(sql_query_string+EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)), "AND mailbox_name = '%s' ", replaced_mailbox_name);
6498         }
6499
6500         if (uid) {              /*  NULL means all mail */
6501                 SNPRINTF(sql_query_string+EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)), "AND s_uid='%s' ", uid);
6502         }
6503
6504         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
6505         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6506                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6507
6508
6509         ret = true;
6510
6511 FINISH_OFF:
6512
6513         EM_SAFE_FREE(replaced_mailbox_name);
6514
6515         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6516         _DISCONNECT_DB;
6517
6518         if (err_code != NULL)
6519                 *err_code = error;
6520
6521         EM_DEBUG_FUNC_END("ret [%d]", ret);
6522         return ret;
6523 }
6524
6525 INTERNAL_FUNC int emstorage_free_read_mail_uid(emstorage_read_mail_uid_tbl_t** read_mail_uid, int count, int *err_code)
6526 {
6527         EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], count[%d], err_code[%p]", read_mail_uid, count, err_code);
6528
6529         int ret = false;
6530         int error = EMAIL_ERROR_NONE;
6531
6532         if (count > 0)  {
6533                 if (!read_mail_uid || !*read_mail_uid)  {
6534                         EM_DEBUG_EXCEPTION(" read_mail_uid[%p], count[%d]", read_mail_uid, count);
6535
6536                         error = EMAIL_ERROR_INVALID_PARAM;
6537                         goto FINISH_OFF;
6538                 }
6539
6540                 emstorage_read_mail_uid_tbl_t* p = *read_mail_uid;
6541                 int i;
6542
6543                 for (i = 0; i < count; i++)  {
6544                         EM_SAFE_FREE(p[i].mailbox_name);
6545                         EM_SAFE_FREE(p[i].s_uid);
6546                         EM_SAFE_FREE(p[i].data2);
6547                 }
6548
6549                 EM_SAFE_FREE(p); *read_mail_uid = NULL;
6550         }
6551
6552         ret = true;
6553
6554 FINISH_OFF:
6555         if (err_code != NULL)
6556                 *err_code = error;
6557
6558         EM_DEBUG_FUNC_END("ret [%d]", ret);
6559         return ret;
6560 }
6561
6562 INTERNAL_FUNC int emstorage_get_rule_count_by_account_id(int account_id, int *count, int transaction, int *err_code)
6563 {
6564         EM_DEBUG_FUNC_BEGIN("account_id [%d], count[%p], transaction[%d], err_code[%p]", count, transaction, err_code);
6565
6566         if (!count) {           
6567                 EM_DEBUG_EXCEPTION("count[%p]", count);
6568
6569                 if (err_code != NULL)
6570                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6571                 return false;
6572         }
6573
6574         int rc = -1, ret = false;
6575         int error =  EMAIL_ERROR_NONE;
6576         char sql_query_string[QUERY_SIZE] = {0, };
6577
6578         sqlite3 *local_db_handle = emstorage_get_db_connection();
6579         EMSTORAGE_START_READ_TRANSACTION(transaction);
6580
6581         if (account_id != ALL_ACCOUNT) 
6582                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl where account_id = %d", account_id);
6583         else
6584                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl");
6585
6586         char **result;
6587
6588         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6589         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6590                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6591
6592         *count = atoi(result[1]);
6593         sqlite3_free_table(result);
6594
6595         ret = true;
6596
6597 FINISH_OFF:
6598         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6599         _DISCONNECT_DB;
6600
6601         if (err_code != NULL)
6602                 *err_code = error;
6603
6604         EM_DEBUG_FUNC_END("ret [%d]", ret);
6605         return ret;
6606 }
6607
6608 INTERNAL_FUNC int emstorage_get_rule(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)
6609 {
6610         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);
6611
6612         if (!select_num || !is_completed || !rule_list) {               /*  only global rule supported. */
6613                 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);
6614
6615                 if (err_code != NULL)
6616                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6617                 return false;
6618         }
6619
6620         int ret = false;
6621         int error = EMAIL_ERROR_NONE;
6622
6623         emstorage_rule_tbl_t* p_data_tbl = NULL;
6624         int i = 0, count = 0;
6625         DB_STMT hStmt = NULL;
6626         char sql_query_string[QUERY_SIZE] = {0, };
6627
6628         int rc;
6629
6630         sqlite3 *local_db_handle = emstorage_get_db_connection();
6631         EMSTORAGE_START_READ_TRANSACTION(transaction);
6632
6633         if (account_id != ALL_ACCOUNT) {
6634                 if (type)
6635                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d AND type = %d", account_id, type);
6636                 else
6637                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d ORDER BY type", account_id);
6638         } else {
6639                 if (type)
6640                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE type = %d", type);
6641                 else
6642                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl ORDER BY type");
6643         }
6644
6645         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6646         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6647         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6648                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6649
6650
6651         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6652         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6653                 ("sqlite3_step fail:%d", rc));
6654
6655         char **result;
6656         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL); */
6657         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
6658         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6659                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6660
6661         sqlite3_free_table(result);
6662
6663         if (count == 0)  {
6664                 EM_DEBUG_LOG(" no matched rule found...");
6665                 ret = true;
6666                 goto FINISH_OFF;
6667         }
6668
6669
6670         if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t) * count))) {
6671                 EM_DEBUG_EXCEPTION(" malloc failed...");
6672
6673                 error = EMAIL_ERROR_OUT_OF_MEMORY;
6674                 goto FINISH_OFF;
6675         }
6676
6677         memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t) * count);
6678
6679         for (i = 0; i < count; i++)  {
6680                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
6681                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
6682                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].type), TYPE_IDX_IN_MAIL_RULE_TBL);
6683                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
6684                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
6685                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
6686                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
6687                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
6688
6689                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6690                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6691                         ("sqlite3_step fail:%d", rc));
6692         }
6693
6694         ret = true;
6695
6696 FINISH_OFF:
6697
6698         EM_DEBUG_LOG("[%d] rules found.", count);
6699
6700         if (ret == true)  {
6701                 *rule_list = p_data_tbl;
6702                 *select_num = count;
6703         }
6704         else if (p_data_tbl != NULL)
6705                 emstorage_free_rule(&p_data_tbl, count, NULL); /* CID FIX */
6706
6707         if (hStmt != NULL)  {
6708                 EM_DEBUG_LOG("  sqlite3_finalize hStmt = %p", hStmt);
6709
6710                 rc = sqlite3_finalize(hStmt);
6711                 if (rc != SQLITE_OK)  {
6712                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6713                         error = EMAIL_ERROR_DB_FAILURE;
6714                 }
6715         }
6716
6717
6718         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6719         _DISCONNECT_DB;
6720
6721         if (err_code != NULL)
6722                 *err_code = error;
6723
6724         EM_DEBUG_FUNC_END("ret [%d]", ret);
6725         return ret;
6726 }
6727
6728 INTERNAL_FUNC int emstorage_get_rule_by_id(int rule_id, emstorage_rule_tbl_t** rule, int transaction, int *err_code)
6729 {
6730         EM_DEBUG_FUNC_BEGIN("rule_id[%d], rule[%p], transaction[%d], err_code[%p]", rule_id, rule, transaction, err_code);
6731
6732         if (!rule) {
6733                 EM_DEBUG_EXCEPTION("rule_id[%d], rule[%p]", rule_id, rule);
6734
6735                 if (err_code != NULL)
6736                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6737                 return false;
6738         }
6739
6740         emstorage_rule_tbl_t* p_data_tbl = NULL;
6741         int rc, ret = false;
6742         int error = EMAIL_ERROR_NONE;
6743
6744         DB_STMT hStmt = NULL;
6745         char sql_query_string[QUERY_SIZE] = {0, };
6746         sqlite3 *local_db_handle = emstorage_get_db_connection();
6747         EMSTORAGE_START_READ_TRANSACTION(transaction);
6748
6749         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
6750
6751         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6752         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6753                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6754
6755
6756         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6757         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6758                 ("sqlite3_step fail:%d", rc));
6759
6760         if (rc == SQLITE_DONE)  {
6761                 EM_DEBUG_EXCEPTION(" no matched rule found...");
6762                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
6763                 goto FINISH_OFF;
6764         }
6765
6766         if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t))))  {
6767                 EM_DEBUG_EXCEPTION(" malloc failed...");
6768                 error = EMAIL_ERROR_OUT_OF_MEMORY;
6769                 goto FINISH_OFF;
6770         }
6771
6772         memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t));
6773         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
6774         _get_stmt_field_data_int(hStmt, &(p_data_tbl->rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
6775         _get_stmt_field_data_int(hStmt, &(p_data_tbl->type), TYPE_IDX_IN_MAIL_RULE_TBL);
6776         _get_stmt_field_data_string(hStmt, &(p_data_tbl->value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
6777         _get_stmt_field_data_int(hStmt, &(p_data_tbl->action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
6778         _get_stmt_field_data_int(hStmt, &(p_data_tbl->target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
6779         _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
6780         _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
6781
6782         ret = true;
6783
6784 FINISH_OFF:
6785         if (ret == true)
6786                 *rule = p_data_tbl;
6787
6788         if (hStmt != NULL)  {
6789                 EM_DEBUG_LOG(" before sqlite3_finalize hStmt = %p", hStmt);
6790
6791                 rc = sqlite3_finalize(hStmt);
6792                 if (rc != SQLITE_OK)  {
6793                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6794
6795                         error = EMAIL_ERROR_DB_FAILURE;
6796                 }
6797         }
6798
6799         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6800         _DISCONNECT_DB;
6801
6802         if (err_code != NULL)
6803                 *err_code = error;
6804
6805         EM_DEBUG_FUNC_END("ret [%d]", ret);
6806         return ret;
6807 }
6808
6809 INTERNAL_FUNC int emstorage_change_rule(int rule_id, emstorage_rule_tbl_t* new_rule, int transaction, int *err_code)
6810 {
6811         EM_DEBUG_FUNC_BEGIN("rule_id[%d], new_rule[%p], transaction[%d], err_code[%p]", rule_id, new_rule, transaction, err_code);
6812
6813         if (!new_rule) {                /*  only global rule supported. */
6814                 EM_DEBUG_EXCEPTION("rule_id[%d], new_rule[%p]", rule_id, new_rule);
6815
6816                 if (err_code != NULL)
6817                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6818                 return false;
6819         }
6820
6821         int rc, ret = false;
6822         int error = EMAIL_ERROR_NONE;
6823
6824         DB_STMT hStmt = NULL;
6825         char sql_query_string[QUERY_SIZE] = {0, };
6826         sqlite3 *local_db_handle = emstorage_get_db_connection();
6827         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6828
6829         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6830                 "UPDATE mail_rule_tbl SET"
6831                 "  type = ?"
6832                 ", value = ?"
6833                 ", action_type = ?"
6834                 ", target_mailbox_id = ?"
6835                 ", flag1 = ?"
6836                 ", flag2 = ?"
6837                 " WHERE account_id = ?"
6838                 " AND rule_id = ?");
6839
6840
6841         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6842         EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
6843         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6844                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6845
6846         int i = 0;
6847
6848         _bind_stmt_field_data_int(hStmt, i++, new_rule->type);
6849         _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
6850         _bind_stmt_field_data_int(hStmt, i++, new_rule->action_type);
6851         _bind_stmt_field_data_int(hStmt, i++, new_rule->target_mailbox_id);
6852         _bind_stmt_field_data_int(hStmt, i++, new_rule->flag1);
6853         _bind_stmt_field_data_int(hStmt, i++, new_rule->flag2);
6854         _bind_stmt_field_data_int(hStmt, i++, new_rule->account_id);
6855         _bind_stmt_field_data_int(hStmt, i++, rule_id);
6856
6857
6858         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6859         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6860                 ("sqlite3_step fail:%d", rc));
6861
6862         ret = true;
6863
6864 FINISH_OFF:
6865         if (hStmt != NULL)  {
6866                 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
6867
6868                 rc = sqlite3_finalize(hStmt);
6869                 if (rc != SQLITE_OK)  {
6870                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6871
6872                         error = EMAIL_ERROR_DB_FAILURE;
6873                 }
6874         }
6875
6876         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6877         _DISCONNECT_DB;
6878
6879         if (err_code != NULL)
6880                 *err_code = error;
6881
6882         EM_DEBUG_FUNC_END("ret [%d]", ret);
6883         return ret;
6884 }
6885
6886 INTERNAL_FUNC int emstorage_find_rule(emstorage_rule_tbl_t* rule, int transaction, int *err_code)
6887 {
6888         EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
6889
6890         if (!rule) {            
6891                 if (rule != NULL)
6892                         EM_DEBUG_EXCEPTION(" rule->account_id[%d]", rule->account_id);
6893
6894                 if (err_code != NULL)
6895                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6896                 return false;
6897         }
6898
6899         DB_STMT hStmt = NULL;
6900         char sql_query_string[QUERY_SIZE] = {0, };
6901
6902         int error = EMAIL_ERROR_NONE;
6903         int rc, ret = false;
6904
6905         sqlite3 *local_db_handle = emstorage_get_db_connection();
6906         EMSTORAGE_START_READ_TRANSACTION(transaction);
6907
6908         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6909                 "SELECT rule_id FROM mail_rule_tbl WHERE type = %d AND UPPER(value) = UPPER('%s')",
6910                 rule->type, rule->value);
6911
6912
6913         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6914         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6915                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6916
6917
6918         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6919         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6920                 ("sqlite3_step fail:%d", rc));
6921
6922         if (rc == SQLITE_DONE)  {
6923                 EM_DEBUG_EXCEPTION(" no matched rule found...");
6924                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
6925                 goto FINISH_OFF;
6926         }
6927
6928
6929         ret = true;
6930
6931 FINISH_OFF:
6932         if (hStmt != NULL)  {
6933                 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
6934
6935                 rc = sqlite3_finalize(hStmt);
6936                 if (rc != SQLITE_OK)  {
6937                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
6938                         error = EMAIL_ERROR_DB_FAILURE;
6939                 }
6940         }
6941
6942
6943         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6944         _DISCONNECT_DB;
6945
6946         if (err_code)
6947                 *err_code = error;
6948
6949         EM_DEBUG_FUNC_END("ret [%d]", ret);
6950         return ret;
6951 }
6952
6953 INTERNAL_FUNC int emstorage_add_rule(emstorage_rule_tbl_t* rule, int transaction, int *err_code)
6954 {
6955         EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
6956
6957         if (!rule)  {   /*  only global rule supported. */
6958                 if (rule != NULL)
6959                         EM_DEBUG_EXCEPTION(" rule->account_id[%d]", rule->account_id);
6960
6961                 if (err_code != NULL)
6962                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6963                 return false;
6964         }
6965
6966         int rc, rc_2, ret = false;
6967         int error = EMAIL_ERROR_NONE;
6968         DB_STMT hStmt = NULL;
6969         char sql_query_string[QUERY_SIZE] = {0, };
6970
6971
6972         sqlite3 *local_db_handle = emstorage_get_db_connection();
6973
6974         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6975
6976         char *sql;
6977         char **result;
6978         sql = "SELECT max(rowid) FROM mail_rule_tbl;";
6979
6980         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6981         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6982                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6983
6984         if (NULL==result[1])
6985                 rc = 1;
6986         else
6987                 rc = atoi(result[1])+1;
6988
6989         sqlite3_free_table(result);
6990
6991         rule->rule_id = rc;
6992
6993         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6994                 "INSERT INTO mail_rule_tbl VALUES "
6995                 "( ?"           /*  account id */
6996                 ", ?"           /*  rule_id */
6997                 ", ?"           /*  type */
6998                 ", ?"           /*  value */
6999                 ", ?"           /*  action_type */
7000                 ", ?"           /*  target_mailbox_id */
7001                 ", ?"           /*  flag1 */
7002                 ", ?)");        /*  flag2 */
7003
7004         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc_2);
7005         if (rc_2 != SQLITE_OK)  {
7006                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc_2, sqlite3_errmsg(local_db_handle));
7007                 error = EMAIL_ERROR_DB_FAILURE;
7008                 goto FINISH_OFF;
7009         }
7010
7011         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL, rule->account_id);
7012         _bind_stmt_field_data_int(hStmt, RULE_ID_IDX_IN_MAIL_RULE_TBL, rule->rule_id);
7013         _bind_stmt_field_data_int(hStmt, TYPE_IDX_IN_MAIL_RULE_TBL, rule->type);
7014         _bind_stmt_field_data_string(hStmt, VALUE_IDX_IN_MAIL_RULE_TBL, (char*)rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7015         _bind_stmt_field_data_int(hStmt, ACTION_TYPE_IDX_IN_MAIL_RULE_TBL, rule->action_type);
7016         _bind_stmt_field_data_int(hStmt, TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL, rule->target_mailbox_id);
7017         _bind_stmt_field_data_int(hStmt, FLAG1_IDX_IN_MAIL_RULE_TBL, rule->flag1);
7018         _bind_stmt_field_data_int(hStmt, FLAG2_IDX_IN_MAIL_RULE_TBL, rule->flag2);
7019
7020         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7021         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7022                 ("sqlite3_step fail:%d", rc));
7023
7024         ret = true;
7025
7026 FINISH_OFF:
7027         if (hStmt != NULL)  {
7028                 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
7029
7030                 rc = sqlite3_finalize(hStmt);
7031                 if (rc != SQLITE_OK) {
7032                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
7033
7034                         error = EMAIL_ERROR_DB_FAILURE;
7035                 }
7036         }
7037
7038         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
7039         _DISCONNECT_DB;
7040
7041         if (err_code != NULL)
7042                 *err_code = error;
7043
7044         EM_DEBUG_FUNC_END("ret [%d]", ret);
7045         return ret;
7046 }
7047
7048 INTERNAL_FUNC int emstorage_delete_rule(int rule_id, int transaction, int *err_code)
7049 {
7050         EM_DEBUG_FUNC_BEGIN("rule_id[%d], transaction[%d], err_code[%p]", rule_id, transaction, err_code);
7051
7052         if (rule_id <= 0) {             /*  only global rule supported. */
7053                 EM_DEBUG_EXCEPTION("rule_id[%d]", rule_id);
7054
7055                 if (err_code != NULL)
7056                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7057                 return false;
7058         }
7059
7060         int rc, ret = false;
7061         int error = EMAIL_ERROR_NONE;
7062         char sql_query_string[QUERY_SIZE] = {0, };
7063
7064         sqlite3 *local_db_handle = emstorage_get_db_connection();
7065         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
7066
7067         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7068
7069         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
7070         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7071                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7072         rc = sqlite3_changes(local_db_handle);
7073         if (rc == 0) {
7074                 EM_DEBUG_EXCEPTION(" no matched rule found...");
7075
7076                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7077                 goto FINISH_OFF;
7078         }
7079
7080         ret = true;
7081
7082 FINISH_OFF:
7083         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
7084         _DISCONNECT_DB;
7085
7086         if (err_code != NULL)
7087                 *err_code = error;
7088
7089         EM_DEBUG_FUNC_END("ret [%d]", ret);
7090         return ret;
7091 }
7092
7093 INTERNAL_FUNC int emstorage_free_rule(emstorage_rule_tbl_t** rule_list, int count, int *err_code)
7094 {
7095         EM_DEBUG_FUNC_BEGIN("rule_list[%p], conut[%d], err_code[%p]", rule_list, count, err_code);
7096
7097         int ret = false;
7098         int error = EMAIL_ERROR_NONE;
7099
7100         if (count > 0) {
7101                 if (!rule_list || !*rule_list) {
7102                         EM_DEBUG_EXCEPTION(" rule_list[%p], conut[%d]", rule_list, count);
7103
7104                         error = EMAIL_ERROR_INVALID_PARAM;
7105                         goto FINISH_OFF;
7106                 }
7107
7108                 emstorage_rule_tbl_t* p = *rule_list;
7109                 int i = 0;
7110
7111                 for (; i < count; i++) {
7112                         EM_SAFE_FREE(p[i].value);
7113                 }
7114
7115                 EM_SAFE_FREE(p); *rule_list = NULL;
7116         }
7117
7118 FINISH_OFF:
7119         if (err_code != NULL)
7120                 *err_code = error;
7121
7122         EM_DEBUG_FUNC_END("ret [%d]", ret);
7123         return ret;
7124 }
7125
7126 INTERNAL_FUNC int emstorage_get_mail_count(int account_id, int mailbox_id, int *total, int *unseen, int transaction, int *err_code)
7127 {
7128         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);
7129
7130         if (!total && !unseen) {
7131                 EM_DEBUG_EXCEPTION(" accoun_id[%d], mailbox_id[%d], total[%p], unseen[%p]", account_id, mailbox_id, total, unseen);
7132                 if (err_code != NULL)
7133                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7134                 return false;
7135         }
7136
7137         int rc = -1, ret = false;
7138         int error = EMAIL_ERROR_NONE;
7139         DB_STMT hStmt = NULL;
7140         char sql_query_string[QUERY_SIZE] = {0, };
7141         char err_msg[1024];
7142         char *replaced_mailbox_name = NULL;
7143
7144         memset(&sql_query_string, 0x00, sizeof(sql_query_string));
7145         sqlite3 *local_db_handle = emstorage_get_db_connection();
7146         EMSTORAGE_START_READ_TRANSACTION(transaction);
7147
7148         if (total)  {
7149                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl");
7150
7151                 if (account_id != ALL_ACCOUNT)  {
7152                         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);
7153                         if (mailbox_id)
7154                                 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);
7155                 }
7156                 else if (mailbox_id)
7157                         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);
7158
7159 #ifdef USE_GET_RECORD_COUNT_API
7160                 char **result;
7161
7162                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7163                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF2; },
7164                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7165
7166                 *total = atoi(result[1]);
7167                 sqlite3_free_table(result);
7168 #else
7169
7170                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7171                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF2; },
7172                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7173
7174                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7175                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF2; },
7176                         ("sqlite3_step fail:%d", rc));
7177                 _get_stmt_field_data_int(hStmt, total, 0);
7178 #endif          /*  USE_GET_RECORD_COUNT_API */
7179         }
7180
7181         if (unseen)  {
7182                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl WHERE flags_seen_field = 0");               /*  fSEEN = 0x01 */
7183
7184                 if (account_id != ALL_ACCOUNT)
7185                         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);
7186
7187                 if (mailbox_id) {
7188                         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);
7189                 }
7190                 else
7191                         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)");
7192
7193                 char **result;
7194                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7195                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
7196                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7197
7198                 *unseen = atoi(result[1]);
7199                 sqlite3_free_table(result);
7200
7201         }
7202 FINISH_OFF:
7203         ret = true;
7204
7205 FINISH_OFF2:
7206
7207 #ifndef USE_PREPARED_QUERY_
7208         if (hStmt != NULL)  {
7209                 rc = sqlite3_finalize(hStmt);
7210                 if (rc != SQLITE_OK)  {
7211                         EM_DEBUG_LOG(" EDBStmtClearRow failed - %d: %s", rc, err_msg);
7212                         error = EMAIL_ERROR_DB_FAILURE;
7213                 }
7214         }
7215 #endif
7216
7217         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7218         _DISCONNECT_DB;
7219
7220         EM_SAFE_FREE(replaced_mailbox_name);
7221
7222         if (err_code != NULL)
7223                 *err_code = error;
7224
7225         EM_DEBUG_FUNC_END("ret [%d]", ret);
7226         return ret;
7227 }
7228
7229 INTERNAL_FUNC int emstorage_get_mail_field_by_id(int mail_id, int type, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
7230 {
7231         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
7232
7233         if (mail_id <= 0 || !mail)  {
7234                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
7235                 if (err_code != NULL)
7236                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7237                 return false;
7238         }
7239
7240         int col_index = 0;
7241         emstorage_mail_tbl_t* p_data_tbl = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
7242
7243         if (p_data_tbl == NULL)  {
7244                 EM_DEBUG_EXCEPTION("malloc failed...");
7245                 if (err_code != NULL)
7246                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
7247                 return false;
7248         }
7249
7250         memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
7251         DB_STMT hStmt = NULL;
7252         char sql_query_string[QUERY_SIZE] = {0, };
7253
7254         int rc, ret = false;
7255         int error = EMAIL_ERROR_NONE;
7256
7257         sqlite3 *local_db_handle = emstorage_get_db_connection();
7258         EMSTORAGE_START_READ_TRANSACTION(transaction);
7259
7260         switch (type)  {
7261                 case RETRIEVE_SUMMARY:
7262                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7263                                 "SELECT account_id, mail_id, mailbox_id, server_mail_status, server_mailbox_name, server_mail_id, file_path_plain, file_path_html, flags_seen_field, save_status, lock_status, thread_id, thread_item_count FROM mail_tbl WHERE mail_id = %d", mail_id);
7264                         break;
7265
7266                 case RETRIEVE_FIELDS_FOR_DELETE:
7267                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7268                                 "SELECT account_id, mail_id, server_mail_status, server_mailbox_name, server_mail_id FROM mail_tbl WHERE mail_id = %d", mail_id);
7269                         break;
7270
7271                 case RETRIEVE_ACCOUNT:
7272                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7273                                 "SELECT account_id FROM mail_tbl WHERE mail_id = %d", mail_id);
7274                         break;
7275
7276                 case RETRIEVE_FLAG:
7277                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7278                                 "SELECT account_id, flags_seen_field, thread_id, mailbox_id FROM mail_tbl WHERE mail_id = %d", mail_id);
7279                         break;
7280
7281                 default :
7282                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
7283                         error = EMAIL_ERROR_INVALID_PARAM;
7284                         goto FINISH_OFF;
7285         }
7286
7287         EM_DEBUG_LOG("Query [%s]", sql_query_string);
7288
7289         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7290         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7291                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7292
7293
7294         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7295         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7296                 ("sqlite3_step fail:%d", rc));
7297
7298         if (rc == SQLITE_DONE)  {
7299                 EM_DEBUG_EXCEPTION("no matched mail found...");
7300                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7301                 goto FINISH_OFF;
7302         }
7303         switch (type)  {
7304                 case RETRIEVE_SUMMARY:
7305                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
7306                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
7307                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
7308                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
7309                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
7310                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
7311                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, col_index++);
7312                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, col_index++);
7313                         _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
7314                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), col_index++);
7315                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), col_index++);
7316                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
7317                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_item_count), col_index++);
7318                         break;
7319
7320                 case RETRIEVE_FIELDS_FOR_DELETE:
7321                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
7322                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
7323                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
7324                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
7325                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
7326                         break;
7327
7328                 case RETRIEVE_ACCOUNT:
7329                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
7330                         break;
7331
7332                 case RETRIEVE_FLAG:
7333                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
7334                         _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
7335                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
7336                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
7337                         break;
7338         }
7339
7340         ret = true;
7341
7342 FINISH_OFF:
7343         if (ret == true)
7344                 *mail = p_data_tbl;
7345         else if (p_data_tbl != NULL)
7346                 emstorage_free_mail(&p_data_tbl,  1, NULL);
7347
7348         if (hStmt != NULL)  {
7349                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
7350
7351                 rc = sqlite3_finalize(hStmt);
7352                 if (rc != SQLITE_OK)  {
7353                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
7354                         error = EMAIL_ERROR_DB_FAILURE;
7355                 }
7356         }
7357
7358
7359         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7360         _DISCONNECT_DB;
7361
7362         if (err_code != NULL)
7363                 *err_code = error;
7364
7365         EM_DEBUG_FUNC_END("ret [%d]", ret);
7366         return ret;
7367 }
7368
7369 INTERNAL_FUNC int emstorage_get_mail_field_by_multiple_mail_id(int mail_ids[], int number_of_mails, int type, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
7370 {
7371         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);
7372
7373         int ret = false;
7374         int error = EMAIL_ERROR_NONE;
7375         int query_string_length = 0;
7376         int i = 0, item_count = 0, rc = -1, field_count, col_index, cur_sql_query_string = 0;
7377         char **result = NULL;
7378         char *sql_query_string = NULL;
7379         emstorage_mail_tbl_t* p_data_tbl = NULL;
7380         sqlite3 *local_db_handle = NULL;
7381
7382         if (number_of_mails <= 0 || !mail_ids)  {
7383                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
7384                 if (err_code != NULL)
7385                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7386                 return false;
7387         }
7388
7389         p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * number_of_mails);
7390
7391         query_string_length = (sizeof(char) * 8 * number_of_mails) + 512;
7392         sql_query_string = (char*)em_malloc(query_string_length);
7393
7394         if (p_data_tbl == NULL || sql_query_string == NULL)  {
7395                 EM_DEBUG_EXCEPTION("malloc failed...");
7396
7397                 EM_SAFE_FREE(p_data_tbl);
7398                 EM_SAFE_FREE(sql_query_string);
7399
7400                 if (err_code != NULL)
7401                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
7402                 return false;
7403         }
7404
7405         local_db_handle = emstorage_get_db_connection();
7406
7407         EMSTORAGE_START_READ_TRANSACTION(transaction);
7408
7409         switch (type) {
7410                 case RETRIEVE_SUMMARY:
7411                         cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
7412                                 "SELECT account_id, mail_id, server_mail_status, server_mailbox_name, server_mail_id, file_path_plain, file_path_html, flags_seen_field, save_status, lock_status, thread_id, thread_item_count FROM mail_tbl WHERE mail_id in (");
7413                         field_count = 12;
7414                         break;
7415
7416                 case RETRIEVE_FIELDS_FOR_DELETE:
7417                         cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
7418                                 "SELECT account_id, mail_id, server_mail_status, server_mailbox_name, server_mail_id FROM mail_tbl WHERE mail_id in (");
7419                         field_count = 5;
7420                         break;
7421
7422                 case RETRIEVE_ACCOUNT:
7423                         cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
7424                                 "SELECT account_id FROM mail_tbl WHERE mail_id in (");
7425                         field_count = 1;
7426                         break;
7427
7428                 case RETRIEVE_FLAG:
7429                         cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
7430                                 "SELECT account_id, mailbox_id, flags_seen_field, thread_id FROM mail_tbl WHERE mail_id in (");
7431                         field_count = 4;
7432                         break;
7433
7434                 default :
7435                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
7436                         error = EMAIL_ERROR_INVALID_PARAM;
7437                         goto FINISH_OFF;
7438         }
7439
7440         for(i = 0; i < number_of_mails; i++)
7441                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, QUERY_SIZE, "%d,", mail_ids[i]);
7442         sql_query_string[EM_SAFE_STRLEN(sql_query_string) - 1] = ')';
7443
7444         EM_DEBUG_LOG("Query [%s], Length [%d]", sql_query_string, EM_SAFE_STRLEN(sql_query_string));
7445
7446         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &item_count, 0, NULL), rc);
7447         if (SQLITE_OK != rc && -1 != rc) {
7448                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
7449                 error = EMAIL_ERROR_DB_FAILURE;
7450                 goto FINISH_OFF;
7451         }
7452
7453         if (rc == SQLITE_DONE)  {
7454                 EM_DEBUG_EXCEPTION("no matched mail found...");
7455                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7456                 goto FINISH_OFF;
7457         }
7458
7459         EM_DEBUG_LOG("item_count [%d]", item_count);
7460
7461         if(number_of_mails != item_count) {
7462                 EM_DEBUG_EXCEPTION("Can't find all emails");
7463                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7464                 goto FINISH_OFF;
7465         }
7466
7467         col_index = field_count;
7468
7469         for(i = 0; i < item_count; i++) {
7470                 switch (type) {
7471                         case RETRIEVE_SUMMARY:
7472                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7473                                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
7474                                 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
7475                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
7476                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
7477                                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
7478                                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
7479                                 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
7480                                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
7481                                 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
7482                                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
7483                                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
7484                                 break;
7485
7486                         case RETRIEVE_FIELDS_FOR_DELETE:
7487                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7488                                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
7489                                 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
7490                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
7491                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
7492                                 break;
7493
7494                         case RETRIEVE_ACCOUNT:
7495                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7496                                 break;
7497
7498                         case RETRIEVE_FLAG:
7499                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7500                                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
7501                                 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
7502                                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
7503                                 break;
7504                 }
7505         }
7506
7507         ret = true;
7508
7509 FINISH_OFF:
7510         if (ret == true)
7511                 *mail = p_data_tbl;
7512         else
7513                 emstorage_free_mail(&p_data_tbl, number_of_mails, NULL);
7514
7515         if (result)
7516                 sqlite3_free_table(result);
7517
7518         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7519         _DISCONNECT_DB;
7520
7521         EM_SAFE_FREE(sql_query_string);
7522
7523         if (err_code != NULL)
7524                 *err_code = error;
7525
7526         EM_DEBUG_FUNC_END("ret [%d]", ret);
7527         return ret;
7528 }
7529
7530 INTERNAL_FUNC int emstorage_get_mail_by_id(int mail_id, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
7531 {
7532         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
7533
7534         if (mail_id <= 0 || !mail) {
7535                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
7536                 if (err_code != NULL)
7537                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7538                 return false;
7539         }
7540
7541         int ret = false, error = EMAIL_ERROR_NONE, count;
7542         char conditional_clause[QUERY_SIZE] = {0, };
7543         emstorage_mail_tbl_t* p_data_tbl = NULL;
7544
7545         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
7546         EM_DEBUG_LOG("query = [%s]", conditional_clause);
7547
7548         if(!emstorage_query_mail_tbl(conditional_clause, transaction, &p_data_tbl, &count, &error)) {
7549                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
7550                 goto FINISH_OFF;
7551         }
7552
7553         ret = true;
7554
7555 FINISH_OFF:
7556         if (ret == true)
7557                 *mail = p_data_tbl;
7558         else if (p_data_tbl != NULL)
7559                 emstorage_free_mail(&p_data_tbl, 1, &error);
7560
7561         if (err_code != NULL)
7562                 *err_code = error;
7563
7564         EM_DEBUG_FUNC_END("ret [%d]", ret);
7565         return ret;
7566 }
7567
7568 #ifdef __FEATURE_BODY_SEARCH__
7569 INTERNAL_FUNC int emstorage_get_mail_text_by_id(int mail_id, emstorage_mail_text_tbl_t **mail_text, int transaction, int *err_code)
7570 {
7571         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
7572
7573         if (mail_id <= 0 || !mail_text) {
7574                 EM_DEBUG_EXCEPTION("mail_id[%d], mail_text[%p]", mail_id, mail_text);
7575                 if (err_code != NULL)
7576                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7577                 return false;
7578         }
7579
7580         int ret = false;
7581         int error = EMAIL_ERROR_NONE;
7582         int count = 0;
7583         char conditional_clause[QUERY_SIZE] = {0, };
7584         emstorage_mail_text_tbl_t *p_data_tbl = NULL;
7585
7586         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
7587         EM_DEBUG_LOG("query = [%s]", conditional_clause);
7588
7589         if(!emstorage_query_mail_text_tbl(conditional_clause, transaction, &p_data_tbl, &count, &error)) {
7590                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
7591                 goto FINISH_OFF;
7592         }
7593
7594         ret = true;
7595
7596 FINISH_OFF:
7597         if (ret == true)
7598                 *mail_text = p_data_tbl;
7599         else if (p_data_tbl != NULL)
7600                 emstorage_free_mail_text(&p_data_tbl, 1, &error);
7601
7602         if (err_code != NULL)
7603                 *err_code = error;
7604
7605         EM_DEBUG_FUNC_END("ret [%d]", ret);
7606         return ret;
7607 }
7608 #endif
7609
7610 INTERNAL_FUNC int emstorage_mail_search_start(emstorage_search_filter_t* search, int account_id, char *mailbox_name, int sorting, int *search_handle, int *searched, int transaction, int *err_code)
7611 {
7612         EM_DEBUG_FUNC_BEGIN("search[%p], account_id[%d], mailbox_name[%p], sorting[%d], search_handle[%p], searched[%p], transaction[%d], err_code[%p]", search, account_id, mailbox_name, sorting, search_handle, searched, transaction, err_code);
7613
7614         if (!search_handle || !searched)  {
7615                 if (err_code != NULL)
7616                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7617                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
7618                 EM_DEBUG_FUNC_END("false");
7619                 return false;
7620         }
7621
7622         emstorage_search_filter_t* p = search;
7623         int error = EMAIL_ERROR_NONE;
7624         DB_STMT hStmt = NULL;
7625         char sql_query_string[QUERY_SIZE] = {0, };
7626         int rc, ret = false;
7627         int and = false, mail_count = 0;
7628         char *replaced_mailbox_name = NULL;
7629
7630         sqlite3 *local_db_handle = emstorage_get_db_connection();
7631         EMSTORAGE_START_READ_TRANSACTION(transaction);
7632
7633         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl");
7634
7635         if (account_id != ALL_ACCOUNT)  {
7636                 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);
7637                 and = true;
7638         }
7639
7640         if (mailbox_name)  {
7641                 if (strstr(mailbox_name, "'")) {
7642                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
7643                 } else {
7644                         replaced_mailbox_name = strdup(mailbox_name);
7645                 }
7646
7647                 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'", and ? "AND" : "WHERE", replaced_mailbox_name);
7648                 and = true;
7649         }
7650
7651         while (p)  {
7652
7653                 if (p->key_type) {
7654                         if (!strncmp(p->key_type, "subject", strlen("subject"))) {
7655                                 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);
7656                                 and = true;
7657                         }
7658                         else if (!strncmp(p->key_type, "full_address_from", strlen("full_address_from"))) {
7659                                 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);
7660                                 and = true;
7661                         }
7662                         else if (!strncmp(p->key_type, "full_address_to", strlen("full_address_to"))) {
7663                                 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);
7664                                 and = true;
7665                         }
7666                         else if (!strncmp(p->key_type, "email_address", strlen("email_address"))) {
7667                                 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);
7668                                 and = true;
7669                         }
7670                         p = p->next;
7671                 }
7672         }
7673
7674         if (sorting)
7675                 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");
7676
7677         EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
7678
7679
7680         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7681         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7682                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7683
7684
7685         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7686         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7687                 ("sqlite3_step fail:%d", rc));
7688
7689         char **result;
7690
7691         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &mail_count, NULL, NULL), rc);
7692         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
7693                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7694
7695         sqlite3_free_table(result);
7696
7697         ret = true;
7698
7699 FINISH_OFF:
7700         if (ret == true)  {
7701                 *search_handle = (int)hStmt;
7702                 *searched = mail_count;
7703                 EM_DEBUG_LOG("mail_count [%d]", mail_count);
7704         }
7705         else  {
7706                 if (hStmt != NULL)  {
7707                         rc = sqlite3_finalize(hStmt);
7708                         if (rc != SQLITE_OK)  {
7709                                 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
7710                                 error = EMAIL_ERROR_DB_FAILURE;
7711                         }
7712                 }
7713
7714                 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7715                 _DISCONNECT_DB;
7716         }
7717
7718         EM_SAFE_FREE(replaced_mailbox_name);
7719
7720         if (err_code != NULL)
7721                 *err_code = error;
7722
7723         EM_DEBUG_FUNC_END("ret [%d]", ret);
7724         return ret;
7725 }
7726
7727 INTERNAL_FUNC int emstorage_mail_search_result(int search_handle, emstorage_mail_field_type_t type, void** data, int transaction, int *err_code)
7728 {
7729         EM_DEBUG_FUNC_BEGIN("search_handle[%d], type[%d], data[%p], transaction[%d], err_code[%p]", search_handle, type, data, transaction, err_code);
7730
7731         if (search_handle == 0 || !data) {
7732                 EM_DEBUG_EXCEPTION(" search_handle[%d], type[%d], data[%p]", search_handle, type, data);
7733
7734                 if (err_code != NULL)
7735                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7736                 return false;
7737         }
7738
7739         emstorage_mail_tbl_t* p_data_tbl = NULL;
7740         DB_STMT hStmt = (DB_STMT)search_handle;
7741         int rc, ret = false;
7742         int error = EMAIL_ERROR_NONE;
7743
7744         switch (type)  {
7745                 case RETRIEVE_ID:
7746                         _get_stmt_field_data_int(hStmt, (int *)data, MAIL_ID_IDX_IN_MAIL_TBL);
7747                         break;
7748
7749                 case RETRIEVE_ENVELOPE:
7750                 case RETRIEVE_ALL:
7751                         if (!(p_data_tbl = em_malloc(sizeof(emstorage_mail_tbl_t)))) {
7752                                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
7753
7754                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7755                                 goto FINISH_OFF;
7756                         }
7757
7758                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
7759                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
7760                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->mail_size), MAIL_SIZE_IDX_IN_MAIL_TBL);
7761                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
7762                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_from), 1, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL);
7763                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_to), 1, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL);
7764                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject), 1, SUBJECT_IDX_IN_MAIL_TBL);
7765                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->body_download_status), BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL);
7766                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL);
7767                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
7768                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
7769                         _get_stmt_field_data_time_t(hStmt, &(p_data_tbl->date_time), DATETIME_IDX_IN_MAIL_TBL);
7770                         _get_stmt_field_data_char  (hStmt, &(p_data_tbl->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL);
7771                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->DRM_status), DRM_STATUS_IDX_IN_MAIL_TBL);
7772                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->priority), PRIORITY_IDX_IN_MAIL_TBL);
7773                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->save_status), SAVE_STATUS_IDX_IN_MAIL_TBL);
7774                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->lock_status), LOCK_STATUS_IDX_IN_MAIL_TBL);
7775                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->report_status), REPORT_STATUS_IDX_IN_MAIL_TBL);
7776                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
7777                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->meeting_request_status), MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL);
7778                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->message_class), MESSAGE_CLASS_IDX_IN_MAIL_TBL);
7779                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->digest_type), DIGEST_TYPE_IDX_IN_MAIL_TBL);
7780                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->smime_type), SMIME_TYPE_IDX_IN_MAIL_TBL);
7781                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->scheduled_sending_time), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
7782                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->eas_data_length), EAS_DATA_LENGTH_IDX_IN_MAIL_TBL);
7783                         _get_stmt_field_data_blob   (hStmt, (void**)&(p_data_tbl->eas_data), EAS_DATA_IDX_IN_MAIL_TBL);
7784
7785                         if (type == RETRIEVE_ALL)  {
7786                                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
7787                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
7788                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_reply), 1, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL);
7789                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_cc), 1, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL);
7790                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_bcc), 1, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL);
7791                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_return), 1, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL);
7792                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->message_id), 0, MESSAGE_ID_IDX_IN_MAIL_TBL);
7793                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
7794                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
7795                                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl->attachment_count), ATTACHMENT_COUNT_IDX_IN_MAIL_TBL);
7796                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
7797
7798                         }
7799
7800                         if (p_data_tbl->body_download_status)  {
7801                                 struct stat buf;
7802
7803                                 if (p_data_tbl->file_path_html)  {
7804                                         if (stat(p_data_tbl->file_path_html, &buf) == -1)
7805                                                 p_data_tbl->body_download_status = 0;
7806                                 }
7807                                 else if (p_data_tbl->file_path_plain)  {
7808                                         if (stat(p_data_tbl->file_path_plain, &buf) == -1)
7809                                                 p_data_tbl->body_download_status = 0;
7810                                 }
7811                                 else
7812                                         p_data_tbl->body_download_status = 0;
7813                         }
7814
7815                         *((emstorage_mail_tbl_t**)data) = p_data_tbl;
7816                         break;
7817
7818                 case RETRIEVE_SUMMARY:
7819                         if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t))))  {
7820                                 EM_DEBUG_EXCEPTION(" malloc failed...");
7821
7822                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7823                                 goto FINISH_OFF;
7824                         }
7825
7826                         memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
7827
7828                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
7829                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
7830                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
7831                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
7832                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
7833
7834                         *((emstorage_mail_tbl_t**)data) = p_data_tbl;
7835                         break;
7836
7837                 case RETRIEVE_ADDRESS:
7838                         if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t))))  {
7839                                 EM_DEBUG_EXCEPTION(" malloc failed...");
7840                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7841                                 goto FINISH_OFF;
7842                         }
7843
7844                         memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
7845                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
7846                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
7847                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
7848                         *((emstorage_mail_tbl_t**)data) = p_data_tbl;
7849                         break;
7850
7851                 default:
7852                         break;
7853         }
7854
7855
7856         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7857         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7858                 ("sqlite3_step fail:%d", rc));
7859
7860         ret = true;
7861
7862 FINISH_OFF:
7863
7864         if (err_code != NULL)
7865                 *err_code = error;
7866
7867         EM_DEBUG_FUNC_END("ret [%d]", ret);
7868         return ret;
7869 }
7870
7871 INTERNAL_FUNC int emstorage_mail_search_end(int search_handle, int transaction, int *err_code)
7872 {
7873         EM_DEBUG_FUNC_BEGIN("search_handle[%d], transaction[%d], err_code[%p]", search_handle, transaction, err_code);
7874
7875         int error = EMAIL_ERROR_NONE;
7876         int rc, ret = false;
7877
7878         if (search_handle == 0)  {
7879                 EM_DEBUG_EXCEPTION(" search_handle[%d]", search_handle);
7880                 error = EMAIL_ERROR_INVALID_PARAM;
7881                 goto FINISH_OFF;
7882         }
7883
7884         DB_STMT hStmt = (DB_STMT)search_handle;
7885
7886         EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
7887
7888         rc = sqlite3_finalize(hStmt);
7889         if (rc != SQLITE_OK)  {
7890                 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
7891                 error = EMAIL_ERROR_DB_FAILURE;
7892         }
7893
7894         ret = true;
7895
7896 FINISH_OFF:
7897         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7898         _DISCONNECT_DB;
7899
7900         if (err_code != NULL)
7901                 *err_code = error;
7902
7903         EM_DEBUG_FUNC_END("ret [%d]", ret);
7904         return ret;
7905 }
7906
7907 INTERNAL_FUNC int emstorage_change_mail(int mail_id, emstorage_mail_tbl_t* mail, int transaction, int *err_code)
7908 {
7909         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
7910
7911         if (mail_id <= 0 || !mail) {
7912                 EM_DEBUG_EXCEPTION(" mail_id[%d], mail[%p]", mail_id, mail);
7913
7914                 if (err_code != NULL)
7915                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7916                 return false;
7917         }
7918
7919         DB_STMT hStmt = NULL;
7920         char sql_query_string[QUERY_SIZE] = {0, };
7921         int rc = -1;
7922         int ret = false;
7923         int error = EMAIL_ERROR_NONE;
7924         int i = 0;
7925         sqlite3 *local_db_handle = emstorage_get_db_connection();
7926         char mailbox_id_param_string[10] = {0,};
7927
7928         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
7929
7930         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7931                 "UPDATE mail_tbl SET"
7932                 "  mail_id = ?"
7933                 ", account_id = ?"
7934                 ", mailbox_id = ?"
7935                 ", mail_size = ?"
7936                 ", server_mail_status = ?"
7937                 ", server_mailbox_name = ?"
7938                 ", server_mail_id = ?"
7939                 ", reference_mail_id = ?"
7940                 ", full_address_from = ?"
7941                 ", full_address_reply = ?"  /* 10 */
7942                 ", full_address_to = ?"
7943                 ", full_address_cc = ?"
7944                 ", full_address_bcc = ?"
7945                 ", full_address_return = ?"
7946                 ", subject = ?"
7947                 ", body_download_status = ?"
7948                 ", file_path_plain = ?"
7949                 ", file_path_html = ?"
7950                 ", date_time = ?"
7951                 ", flags_seen_field      = ?"
7952                 ", flags_deleted_field   = ?"
7953                 ", flags_flagged_field   = ?"
7954                 ", flags_answered_field  = ?"
7955                 ", flags_recent_field    = ?"
7956                 ", flags_draft_field     = ?"
7957                 ", flags_forwarded_field = ?"
7958                 ", DRM_status = ?"
7959                 ", priority = ?"
7960                 ", save_status = ?"
7961                 ", lock_status = ?"
7962                 ", message_id = ?"
7963                 ", report_status = ?"
7964                 ", preview_text = ?"
7965                 ", smime_type = ?"
7966                 ", scheduled_sending_time = ?"
7967                 ", eas_data_length = ?"
7968                 ", eas_data = ?"
7969                 " WHERE mail_id = %d AND account_id != 0 "
7970                 , mail_id);
7971
7972
7973         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7974         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7975                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7976
7977         _bind_stmt_field_data_int   (hStmt, i++, mail->mail_id);
7978         _bind_stmt_field_data_int   (hStmt, i++, mail->account_id);
7979         _bind_stmt_field_data_int   (hStmt, i++, mail->mailbox_id);
7980         _bind_stmt_field_data_int   (hStmt, i++, mail->mail_size);
7981         _bind_stmt_field_data_int   (hStmt, i++, mail->server_mail_status);
7982         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mailbox_name, 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
7983         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mail_id, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
7984         _bind_stmt_field_data_int   (hStmt, i++, mail->reference_mail_id);
7985         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
7986         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
7987         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
7988         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
7989         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
7990         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
7991         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
7992         _bind_stmt_field_data_int   (hStmt, i++, mail->body_download_status);
7993         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
7994         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
7995         _bind_stmt_field_data_int   (hStmt, i++, mail->date_time);
7996         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_seen_field);
7997         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_deleted_field);
7998         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_flagged_field);
7999         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_answered_field);
8000         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_recent_field);
8001         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_draft_field);
8002         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_forwarded_field);
8003         _bind_stmt_field_data_int   (hStmt, i++, mail->DRM_status);
8004         _bind_stmt_field_data_int   (hStmt, i++, mail->priority);
8005         _bind_stmt_field_data_int   (hStmt, i++, mail->save_status);
8006         _bind_stmt_field_data_int   (hStmt, i++, mail->lock_status);
8007         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->message_id, 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
8008         _bind_stmt_field_data_int   (hStmt, i++, mail->report_status);
8009         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8010         _bind_stmt_field_data_int   (hStmt, i++, mail->smime_type);
8011         _bind_stmt_field_data_int   (hStmt, i++, mail->scheduled_sending_time);
8012         _bind_stmt_field_data_int   (hStmt, i++, mail->eas_data_length);
8013         _bind_stmt_field_data_blob  (hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
8014
8015         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8016         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8017                 ("sqlite3_step fail:%d", rc));
8018
8019         rc = sqlite3_changes(local_db_handle);
8020         if (rc == 0)  {
8021                 EM_DEBUG_EXCEPTION(" no matched mail found...");
8022                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8023                 goto FINISH_OFF;
8024         }
8025         SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
8026         if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail->mail_id, mailbox_id_param_string, 0))
8027                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_UPDATE ] >>>> ");
8028
8029         ret = true;
8030
8031 FINISH_OFF:
8032         if (hStmt != NULL)  {
8033                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
8034                 rc = sqlite3_finalize(hStmt);
8035                 if (rc != SQLITE_OK)  {
8036                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
8037                         error = EMAIL_ERROR_DB_FAILURE;
8038                 }
8039         }
8040
8041         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8042         _DISCONNECT_DB;
8043
8044         if (err_code != NULL)
8045                 *err_code = error;
8046
8047         EM_DEBUG_FUNC_END("ret [%d]", ret);
8048         return ret;
8049 }
8050
8051 /**
8052   *  emstorage_clean_save_status(int save_status, int  *err_code) - set the all mail status to the set value
8053   *
8054   *
8055   **/
8056 INTERNAL_FUNC int emstorage_clean_save_status(int save_status, int  *err_code)
8057 {
8058         EM_DEBUG_FUNC_BEGIN("save_status[%d], err_code[%p]", save_status, err_code);
8059
8060         EM_IF_NULL_RETURN_VALUE(err_code, false);
8061
8062         int ret = false;
8063         int error = EMAIL_ERROR_NONE;
8064         int rc = 0;
8065         char sql_query_string[QUERY_SIZE] = {0, };
8066         sqlite3 *local_db_handle = emstorage_get_db_connection();
8067
8068         memset(sql_query_string, 0x00, sizeof(sql_query_string));
8069         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);
8070         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8071
8072         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8073         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8074                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8075
8076         rc = sqlite3_changes(local_db_handle);
8077         if (rc == 0) {
8078                 EM_DEBUG_LOG(" No Matched Mail Exists ");
8079                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8080         }
8081
8082         ret = true;
8083
8084 FINISH_OFF:
8085
8086         if (err_code != NULL)
8087                 *err_code = error;
8088
8089         EM_DEBUG_FUNC_END("ret [%d]", ret);
8090         return ret;
8091 }
8092
8093 INTERNAL_FUNC int emstorage_set_field_of_mails_with_integer_value(int account_id, int mail_ids[], int mail_ids_count, char *field_name, int value, int transaction, int *err_code)
8094 {
8095         EM_DEBUG_FUNC_BEGIN("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);
8096         int i = 0;
8097         int error = EMAIL_ERROR_NONE;
8098         int rc = 0;
8099         int ret = false;
8100         int cur_mail_id_string = 0;
8101         int mail_id_string_buffer_length = 0;
8102         char  sql_query_string[QUERY_SIZE] = {0, };
8103         char *mail_id_string_buffer = NULL;
8104         char *parameter_string = NULL;
8105         sqlite3 *local_db_handle = emstorage_get_db_connection();
8106         email_mail_attribute_type target_mail_attribute_type = 0;
8107
8108         if (!mail_ids  || !field_name || account_id == 0) {
8109                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8110                 if (err_code != NULL)
8111                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8112                 return false;
8113         }
8114
8115         if( (error = emcore_get_attribute_type_by_mail_field_name(field_name, &target_mail_attribute_type)) != EMAIL_ERROR_NONE) {
8116                 EM_DEBUG_EXCEPTION("emstorageemcore_get_attribute_type_by_mail_field_name failed [%d]", error);
8117                 if (err_code != NULL)
8118                         *err_code = error;
8119                 return false;
8120         }
8121
8122         /* Generating mail id list string */
8123         mail_id_string_buffer_length = MAIL_ID_STRING_LENGTH * mail_ids_count;
8124
8125         mail_id_string_buffer = em_malloc(mail_id_string_buffer_length);
8126
8127         if(!mail_id_string_buffer) {
8128                 EM_DEBUG_EXCEPTION("em_malloc failed");
8129                 error = EMAIL_ERROR_OUT_OF_MEMORY;
8130                 goto FINISH_OFF;
8131         }
8132
8133         for(i = 0; i < mail_ids_count; i++)
8134                 cur_mail_id_string += SNPRINTF_OFFSET(mail_id_string_buffer, cur_mail_id_string, mail_id_string_buffer_length, "%d,", mail_ids[i]);
8135
8136         if(EM_SAFE_STRLEN(mail_id_string_buffer) > 1)
8137                 mail_id_string_buffer[EM_SAFE_STRLEN(mail_id_string_buffer) - 1] = NULL_CHAR;
8138
8139         /* Generating notification parameter string */
8140         parameter_string = em_malloc(mail_id_string_buffer_length + EM_SAFE_STRLEN(field_name) + 2);
8141
8142         if(!parameter_string) {
8143                 EM_DEBUG_EXCEPTION("em_malloc failed");
8144                 error = EMAIL_ERROR_OUT_OF_MEMORY;
8145                 goto FINISH_OFF;
8146         }
8147
8148         SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", field_name, 0x01, mail_id_string_buffer);
8149
8150         /* Write query string */
8151         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);
8152
8153         EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
8154
8155         /* Execute query */
8156         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8157         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8158         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8159                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8160         if (sqlite3_changes(local_db_handle) == 0)
8161                 EM_DEBUG_LOG("no mail matched...");
8162
8163         ret = true;
8164
8165 FINISH_OFF:
8166         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8167         _DISCONNECT_DB;
8168
8169         if (ret && parameter_string && !emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, target_mail_attribute_type, parameter_string, value))
8170                 EM_DEBUG_EXCEPTION("emcore_notify_storage_event failed : NOTI_MAIL_FIELD_UPDATE [%s,%d]", field_name, value);
8171
8172         EM_SAFE_FREE(mail_id_string_buffer);
8173         EM_SAFE_FREE(parameter_string);
8174
8175         if (err_code != NULL)
8176                 *err_code = error;
8177
8178         EM_DEBUG_FUNC_END("error [%d]", error);
8179         return ret;
8180 }
8181
8182 #ifdef __FEATURE_BODY_SEARCH__
8183 INTERNAL_FUNC int emstorage_change_mail_text_field(int mail_id, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
8184 {
8185         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
8186
8187         if (mail_id <= 0 || !mail_text) {
8188                 EM_DEBUG_EXCEPTION(" mail_id[%d], mail_text[%p]", mail_id, mail_text);
8189                 if (err_code != NULL)
8190                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8191                 return false;
8192         }
8193
8194         int ret = false;
8195         int error = EMAIL_ERROR_NONE;
8196         DB_STMT hStmt = NULL;
8197         char sql_query_string[QUERY_SIZE] = {0, };
8198
8199         int i = 0;
8200         int rc = 0;
8201
8202         sqlite3 *local_db_handle = emstorage_get_db_connection();
8203         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8204
8205         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8206                 "UPDATE mail_text_tbl SET"
8207                 " body_text = ?"
8208                 " WHERE mail_id = %d AND account_id != 0"
8209                 , mail_id);
8210         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8211
8212         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8213         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8214                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8215
8216         i = 0;
8217         _bind_stmt_field_data_string(hStmt, i++, (char *)mail_text->body_text, 0, -1);
8218
8219         if (hStmt != NULL) {
8220                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8221                 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF;}, ("sqlite3_step fail:%d", rc));
8222                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF;}, ("sqlite3_step fail:%d", rc));
8223
8224                 rc = sqlite3_changes(local_db_handle);
8225                 if (rc == 0)  {
8226                         EM_DEBUG_EXCEPTION(" no matched mail found...");
8227                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
8228                         goto FINISH_OFF;
8229                 }
8230         }
8231
8232         ret = true;
8233
8234 FINISH_OFF:
8235
8236         if (hStmt != NULL) {
8237                 rc = sqlite3_finalize(hStmt);
8238                 if (rc != SQLITE_OK) {
8239                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
8240                         error = EMAIL_ERROR_DB_FAILURE;
8241                 }
8242                 hStmt = NULL;
8243         }
8244
8245         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8246         _DISCONNECT_DB;
8247
8248         if (err_code != NULL)
8249                 *err_code = error;
8250
8251         EM_DEBUG_FUNC_END("ret [%d]", ret);
8252         return ret;
8253 }
8254 #endif
8255
8256 INTERNAL_FUNC int emstorage_change_mail_field(int mail_id, email_mail_change_type_t type, emstorage_mail_tbl_t* mail, int transaction, int *err_code)
8257 {
8258         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
8259
8260         int ret = false;
8261         int error = EMAIL_ERROR_NONE;
8262         int move_flag = 0;
8263         int rc = 0;
8264         int i = 0;
8265         int mailbox_id = 0;
8266         DB_STMT hStmt = NULL;
8267         char sql_query_string[QUERY_SIZE] = {0, };
8268         char mailbox_id_param_string[10] = {0,};
8269         sqlite3 *local_db_handle = NULL;
8270
8271         if (mail_id <= 0 || !mail)  {
8272                 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], mail[%p]", mail_id, type, mail);
8273                 if (err_code != NULL)
8274                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8275                 return false;
8276         }
8277
8278         local_db_handle = emstorage_get_db_connection();
8279
8280         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8281
8282         switch (type) {
8283                 case APPEND_BODY:
8284                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8285                                 "UPDATE mail_tbl SET"
8286                                 "  body_download_status = ?"
8287                                 ", file_path_plain = ?"
8288                                 ", file_path_html = ?"
8289                                 ", flags_seen_field      = ?"
8290                                 ", flags_deleted_field   = ?"
8291                                 ", flags_flagged_field   = ?"
8292                                 ", flags_answered_field  = ?"
8293                                 ", flags_recent_field    = ?"
8294                                 ", flags_draft_field     = ?"
8295                                 ", flags_forwarded_field = ?"
8296                                 ", DRM_status = ?"
8297                                 ", attachment_count = ?"
8298                                 ", preview_text= ?"
8299                                 ", meeting_request_status = ? "
8300                                 ", message_class = ? "
8301                                 ", digest_type = ? "
8302                                 ", smime_type = ? "
8303                                 " WHERE mail_id = %d AND account_id != 0"
8304                                 , mail_id);
8305
8306
8307                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8308                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8309                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8310                          i = 0;
8311
8312                         _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8313                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8314                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8315                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_seen_field);
8316                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_deleted_field);
8317                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_flagged_field);
8318                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_answered_field);
8319                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_recent_field);
8320                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_draft_field);
8321                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_forwarded_field);
8322                         _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
8323                         _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
8324                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
8325                         _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
8326                         _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
8327                         _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
8328                         _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
8329                         break;
8330
8331                 case UPDATE_MAILBOX: {
8332                                 int err;
8333                                 emstorage_mailbox_tbl_t *mailbox_tbl;
8334
8335                                 if ((err = emstorage_get_mailbox_by_id(mail->mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
8336                                         EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_id failed [%d]", err);
8337                                         goto FINISH_OFF;
8338                                 }
8339
8340                                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8341                                         "UPDATE mail_tbl SET"
8342                                         " mailbox_id = '%d'"
8343                                         ",mailbox_type = '%d'"
8344                                         " WHERE mail_id = %d AND account_id != 0"
8345                                         , mailbox_tbl->mailbox_id
8346                                         , mailbox_tbl->mailbox_type
8347                                         , mail_id);
8348                                         move_flag = 1;
8349
8350
8351                                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8352                                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8353                                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8354
8355                                 i = 0;
8356                                 _bind_stmt_field_data_string(hStmt, i++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
8357
8358                                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL); /*prevent 26251*/
8359
8360                         }
8361                         break;
8362
8363                 case UPDATE_FLAG:
8364                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8365                                 "UPDATE mail_tbl SET"
8366                                 " flags_seen_field      = %d"
8367                                 ",flags_deleted_field   = %d"
8368                                 ",flags_flagged_field   = %d"
8369                                 ",flags_answered_field  = %d"
8370                                 ",flags_recent_field    = %d"
8371                                 ",flags_draft_field     = %d"
8372                                 ",flags_forwarded_field = %d"
8373                                 "  WHERE mail_id = %d AND account_id != 0"
8374                                 , mail->flags_seen_field
8375                                 , mail->flags_deleted_field
8376                                 , mail->flags_flagged_field
8377                                 , mail->flags_answered_field
8378                                 , mail->flags_recent_field
8379                                 , mail->flags_draft_field
8380                                 , mail->flags_forwarded_field
8381                                 , mail_id);
8382                         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8383
8384
8385                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8386                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8387                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8388
8389                         break;
8390
8391                 case UPDATE_EXTRA_FLAG:
8392                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8393                                 "UPDATE mail_tbl SET"
8394                                 "  priority = %d"
8395                                 ", save_status = %d"
8396                                 ", lock_status = %d"
8397                                 ", report_status = %d"
8398                                 ", DRM_status = %d"
8399                                 " WHERE mail_id = %d AND account_id != 0"
8400                                 , mail->priority
8401                                 , mail->save_status
8402                                 , mail->lock_status
8403                                 , mail->report_status
8404                                 , mail->DRM_status
8405                                 , mail_id);
8406                         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8407
8408
8409                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8410                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8411                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8412                         break;
8413
8414                 case UPDATE_STICKY_EXTRA_FLAG:
8415                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8416                                 "UPDATE mail_tbl SET"
8417                                 "  lock_status = %d"
8418                                 "  WHERE mail_id = %d AND account_id != 0"
8419                                 , mail->lock_status
8420                                 , mail_id);
8421                         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8422
8423
8424                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8425                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8426                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8427                         break;
8428
8429                 case UPDATE_MAIL:
8430                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8431                                 "UPDATE mail_tbl SET"
8432                                 "  full_address_from = ?"
8433                                 ", full_address_reply = ?"
8434                                 ", full_address_to = ?"
8435                                 ", full_address_cc = ?"
8436                                 ", full_address_bcc = ?"
8437                                 ", full_address_return = ?"
8438                                 ", subject = ?"
8439                                 ", file_path_plain = ?"
8440                                 ", date_time = ?"
8441                                 ", flags_seen_field = ?"
8442                                 ", flags_deleted_field = ?"
8443                                 ", flags_flagged_field = ?"
8444                                 ", flags_answered_field = ?"
8445                                 ", flags_recent_field = ?"
8446                                 ", flags_draft_field = ?"
8447                                 ", flags_forwarded_field = ?"
8448                                 ", priority = ?"
8449                                 ", save_status = ?"
8450                                 ", lock_status = ?"
8451                                 ", report_status = ?"
8452                                 ", DRM_status = ?"
8453                                 ", file_path_html = ?"
8454                                 ", file_path_mime_entity = ?"
8455                                 ", mail_size = ?"
8456                                 ", preview_text = ?"
8457                                 ", body_download_status = ?"
8458                                 ", attachment_count = ?"
8459                                 ", inline_content_count = ?"
8460                                 ", meeting_request_status = ?"
8461                                 ", message_class = ?"
8462                                 ", digest_type = ?"
8463                                 ", smime_type = ?"
8464                                 ", scheduled_sending_time = ?"
8465                                 ", eas_data_length = ?"
8466                                 ", eas_data = ?"
8467                                 " WHERE mail_id = %d AND account_id != 0"
8468                                 , mail_id);
8469
8470
8471                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8472                         EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
8473                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8474                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8475                         i = 0;
8476                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
8477                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
8478                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
8479                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
8480                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
8481                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
8482                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8483                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8484                         _bind_stmt_field_data_time_t(hStmt, i++, mail->date_time);
8485                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_seen_field);
8486                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_deleted_field);
8487                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_flagged_field);
8488                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_answered_field);
8489                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_recent_field);
8490                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_draft_field);
8491                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_forwarded_field);
8492                         _bind_stmt_field_data_int   (hStmt, i++, mail->priority);
8493                         _bind_stmt_field_data_int   (hStmt, i++, mail->save_status);
8494                         _bind_stmt_field_data_int   (hStmt, i++, mail->lock_status);
8495                         _bind_stmt_field_data_int   (hStmt, i++, mail->report_status);
8496                         _bind_stmt_field_data_int   (hStmt, i++, mail->DRM_status);
8497                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8498                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
8499                         _bind_stmt_field_data_int   (hStmt, i++, mail->mail_size);
8500                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8501                         _bind_stmt_field_data_int   (hStmt, i++, mail->body_download_status);
8502                         _bind_stmt_field_data_int   (hStmt, i++, mail->attachment_count);
8503                         _bind_stmt_field_data_int   (hStmt, i++, mail->inline_content_count);
8504                         _bind_stmt_field_data_int   (hStmt, i++, mail->meeting_request_status);
8505                         _bind_stmt_field_data_int   (hStmt, i++, mail->message_class);
8506                         _bind_stmt_field_data_int   (hStmt, i++, mail->digest_type);
8507                         _bind_stmt_field_data_int   (hStmt, i++, mail->smime_type);
8508                         _bind_stmt_field_data_int   (hStmt, i++, mail->scheduled_sending_time);
8509                         _bind_stmt_field_data_int   (hStmt, i++, mail->eas_data_length);
8510                         _bind_stmt_field_data_blob  (hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
8511                         break;
8512
8513                 case UPDATE_DATETIME:  {
8514                         time_t now = time(NULL);
8515
8516                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8517                                 "UPDATE mail_tbl SET"
8518                                 " date_time = '%d'"
8519                                 " WHERE mail_id = %d AND account_id != 0"
8520                                 , (int)now
8521                                 , mail_id);
8522
8523                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8524                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8525                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8526                         break;
8527                 }
8528
8529                 case UPDATE_FROM_CONTACT_INFO:
8530                         EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_FROM_CONTACT_INFO");
8531                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8532                                 "UPDATE mail_tbl SET"
8533                                 " email_address_sender = ?,"
8534                                 " WHERE mail_id = %d",
8535                                 mail_id);
8536
8537                         hStmt = NULL;
8538
8539                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8540                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8541                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8542                         i = 0;
8543                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8544                         break;
8545
8546                 case UPDATE_TO_CONTACT_INFO:
8547                         EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_TO_CONTACT_INFO");
8548                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8549                                 "UPDATE mail_tbl SET"
8550                                 " email_address_recipient = ?,"
8551                                 " WHERE mail_id = %d",
8552                                 mail_id);
8553
8554                         hStmt = NULL;
8555
8556                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8557                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8558                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8559                         i = 0;
8560                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8561                         break;
8562
8563                         case UPDATE_ALL_CONTACT_INFO:
8564                                 EM_DEBUG_LOG("emstorage_change_mail_field - mail change type is UPDATE_ALL_CONTACT_INFO");
8565                                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8566                                         "UPDATE mail_tbl SET"
8567                                         " email_address_sender = ?,"
8568                                         " email_address_recipient = ?,"
8569                                         " WHERE mail_id = %d",
8570                                         mail_id);
8571
8572                                 hStmt = NULL;
8573
8574                                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8575                                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8576                                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8577                                 i = 0;
8578                                 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8579                                 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8580                                 break;
8581
8582
8583 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
8584                         case UPDATE_PARTIAL_BODY_DOWNLOAD:
8585
8586                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8587                         "UPDATE mail_tbl SET"
8588                         "  body_download_status = ?"
8589                         ", file_path_plain = ?"
8590                         ", file_path_html = ?"
8591                         ", attachment_count = ?"
8592                         ", inline_content_count = ?"
8593                         ", preview_text= ?"
8594                         " WHERE mail_id = %d"
8595                         , mail_id);
8596
8597
8598                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8599                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8600                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8601                          i = 0;
8602
8603                         _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8604                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8605                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html,  0, TEXT_2_LEN_IN_MAIL_TBL);
8606                         _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
8607                         _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
8608                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->preview_text,    0, PREVIEWBODY_LEN_IN_MAIL_TBL);
8609
8610                         break;
8611
8612 #endif
8613                 case UPDATE_FILE_PATH:
8614                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8615                                 "UPDATE mail_tbl SET"
8616                                 ", file_path_plain = ?"
8617                                 ", file_path_html = ?"
8618                                 ", file_path_mime_entity = ?"
8619                                 " WHERE mail_id = %d"
8620                                 , mail_id);
8621
8622
8623                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8624                         EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
8625                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8626                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8627                         i = 0;
8628                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8629                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8630                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
8631                         break;
8632
8633                 default:
8634                         EM_DEBUG_LOG(" type[%d]", type);
8635
8636                         error = EMAIL_ERROR_INVALID_PARAM;
8637                         goto FINISH_OFF;
8638         }
8639
8640         if (hStmt != NULL)  {
8641
8642         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8643                 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
8644                 ("sqlite3_step fail:%d", rc));
8645                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8646                         ("sqlite3_step fail:%d", rc));
8647                 rc = sqlite3_changes(local_db_handle);
8648                 if (rc == 0)  {
8649                         EM_DEBUG_EXCEPTION(" no matched mail found...");
8650
8651                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
8652                         goto FINISH_OFF;
8653                 }
8654         }
8655
8656         if (mail->account_id == 0) {
8657                 emstorage_mail_tbl_t* mail_for_account_tbl = NULL;
8658                 if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_ACCOUNT, &mail_for_account_tbl, true, &error) || !mail_for_account_tbl) {
8659                         EM_DEBUG_EXCEPTION(" emstorage_get_mail_field_by_id failed - %d", error);
8660 /*               */
8661                         goto FINISH_OFF;
8662                 }
8663                 mail->account_id = mail_for_account_tbl->account_id;
8664                 if (mail_for_account_tbl)
8665                         emstorage_free_mail(&mail_for_account_tbl, 1, NULL);
8666         }
8667
8668         ret = true;
8669
8670 FINISH_OFF:
8671
8672         if (hStmt != NULL)  {
8673                 rc = sqlite3_finalize(hStmt);
8674                 if (rc != SQLITE_OK)  {
8675                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
8676                         error = EMAIL_ERROR_DB_FAILURE;
8677                 }
8678                 hStmt = NULL;
8679         }
8680
8681         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8682         _DISCONNECT_DB;
8683
8684         /*h.gahlaut@samsung.com: Moving publication of notification after committing transaction to DB */
8685
8686         if (ret == true &&  move_flag != 1) {
8687                 if (!emstorage_get_mailbox_id_by_mailbox_type(mail->account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &mailbox_id, false, &error))
8688                         EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_id_by_mailbox_type failed - %d", error);
8689
8690                 if (mail->mailbox_id == mailbox_id) {
8691                         SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
8692                         if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, mailbox_id_param_string, type))
8693                                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_UPDATE ] >>>> ");
8694                 }
8695                 else {
8696                         /* h.gahlaut@samsung.com: Jan 10, 2011 Publishing noti to refresh outbox when email sending status changes */
8697                         if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, NULL, type))
8698                                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_UPDATE ] ");
8699                 }
8700         }
8701
8702         if (err_code != NULL)
8703                 *err_code = error;
8704
8705         EM_DEBUG_FUNC_END("ret [%d]", ret);
8706         return ret;
8707 }
8708 INTERNAL_FUNC int emstorage_increase_mail_id(int *mail_id, int transaction, int *err_code)
8709 {
8710         EM_DEBUG_FUNC_BEGIN("mail_id[%p], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
8711
8712         int rc, ret = false;
8713         int error = EMAIL_ERROR_NONE;
8714         int latest_mail_id = 0;
8715         sqlite3 *local_db_handle = NULL;
8716         char *sql = "SELECT MAX(mail_id) FROM mail_tbl;";
8717         char **result = NULL;
8718
8719 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
8720         _timedlock_shm_mutex(&mapped_for_generating_mail_id, 2);
8721 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
8722
8723         ret = vconf_get_int(VCONF_KEY_LATEST_MAIL_ID, &latest_mail_id);
8724         if (ret < 0 || latest_mail_id == 0) {
8725                 EM_DEBUG_LOG("vconf_get_int() failed [%d] or latest_mail_id is zero", ret);
8726
8727                 local_db_handle = emstorage_get_db_connection();
8728
8729                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
8730                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
8731                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
8732                 if (NULL == result[1])
8733                         rc = 1;
8734                 else
8735                         rc = atoi(result[1]) + 1;
8736
8737                 sqlite3_free_table(result);
8738                 latest_mail_id = rc;
8739         }
8740
8741         latest_mail_id++;
8742
8743         ret = vconf_set_int(VCONF_KEY_LATEST_MAIL_ID, latest_mail_id);
8744
8745         if (mail_id)
8746                 *mail_id = latest_mail_id;
8747
8748 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
8749         _unlockshm_mutex(&mapped_for_generating_mail_id);
8750 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
8751
8752         ret = true;
8753
8754 FINISH_OFF:
8755         _DISCONNECT_DB;
8756
8757         if (err_code != NULL)
8758                 *err_code = error;
8759
8760         EM_DEBUG_FUNC_END("ret [%d]", ret);
8761         return ret;
8762 }
8763
8764
8765 INTERNAL_FUNC int emstorage_add_mail(emstorage_mail_tbl_t *mail_tbl_data, int get_id, int transaction, int *err_code)
8766 {
8767         EM_PROFILE_BEGIN(profile_emstorage_add_mail);
8768         EM_DEBUG_FUNC_BEGIN("mail_tbl_data[%p], get_id[%d], transaction[%d], err_code[%p]", mail_tbl_data, get_id, transaction, err_code);
8769
8770         if (!mail_tbl_data)  {
8771                 EM_DEBUG_EXCEPTION("mail_tbl_data[%p], get_id[%d]", mail_tbl_data, get_id);
8772                 if (err_code != NULL)
8773                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8774                 return false;
8775         }
8776
8777         int rc, ret = false;
8778         int error = EMAIL_ERROR_NONE;
8779         char sql_query_string[QUERY_SIZE] = {0, };
8780         DB_STMT hStmt = NULL;
8781         sqlite3 *local_db_handle = emstorage_get_db_connection();
8782
8783         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8784
8785         if (get_id)  {
8786                 /*  increase unique id */
8787                 char *sql = "SELECT max(rowid) FROM mail_tbl;";
8788                 char **result;
8789
8790                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
8791                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
8792                         ("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle)));
8793
8794                 if (NULL == result[1])
8795                         rc = 1;
8796                 else
8797                         rc = atoi(result[1])+1;
8798
8799                 sqlite3_free_table(result);
8800
8801                 mail_tbl_data->mail_id   = rc;
8802                 mail_tbl_data->thread_id = rc;
8803         }
8804
8805         if (mail_tbl_data->date_time == 0)
8806                 mail_tbl_data->date_time = time(NULL);
8807
8808         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8809                 "INSERT INTO mail_tbl VALUES "
8810                 "( ?" /*  mail_id */
8811                 ", ?" /*  account_id */
8812                 ", ?" /*  mailbox_id */
8813                 ", ?" /*  mailbox_type */
8814                 ", ?" /*  subject */
8815                 ", ?" /*  date_time */
8816                 ", ?" /*  server_mail_status */
8817                 ", ?" /*  server_mailbox_name */
8818                 ", ?" /*  server_mail_id */
8819                 ", ?" /*  message_id */
8820                 ", ?" /*  reference_mail_id */
8821                 ", ?" /*  full_address_from */
8822                 ", ?" /*  full_address_reply */
8823                 ", ?" /*  full_address_to */
8824                 ", ?" /*  full_address_cc */
8825                 ", ?" /*  full_address_bcc */
8826                 ", ?" /*  full_address_return */
8827                 ", ?" /*  email_address_sender */
8828                 ", ?" /*  email_address_recipient */
8829                 ", ?" /*  alias_sender */
8830                 ", ?" /*  alias_recipient */
8831                 ", ?" /*  body_download_status */
8832                 ", ?" /*  file_path_plain */
8833                 ", ?" /*  file_path_html */
8834                 ", ?" /*  file_path_mime_entity */
8835                 ", ?" /*  mail_size */
8836                 ", ?" /*  flags_seen_field */
8837                 ", ?" /*  flags_deleted_field */
8838                 ", ?" /*  flags_flagged_field */
8839                 ", ?" /*  flags_answered_field */
8840                 ", ?" /*  flags_recent_field */
8841                 ", ?" /*  flags_draft_field */
8842                 ", ?" /*  flags_forwarded_field */
8843                 ", ?" /*  DRM_status */
8844                 ", ?" /*  priority */
8845                 ", ?" /*  save_status */
8846                 ", ?" /*  lock_status */
8847                 ", ?" /*  report_status */
8848                 ", ?" /*  attachment_count */
8849                 ", ?" /*  inline_content_count */
8850                 ", ?" /*  thread_id */
8851                 ", ?" /*  thread_item_count */
8852                 ", ?" /*  preview_text */
8853                 ", ?" /*  meeting_request_status */
8854                 ", ?" /*  message_class */
8855                 ", ?" /*  digest_type */
8856                 ", ?" /*  smime_type */
8857                 ", ?" /*  scheduled_sending_time */
8858                 ", ?" /*  eas_data_length */
8859                 ", ?" /*  eas_data */
8860                 ")");
8861
8862         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8863         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8864                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8865
8866         _bind_stmt_field_data_int   (hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mail_id);
8867         _bind_stmt_field_data_int   (hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, mail_tbl_data->account_id);
8868         _bind_stmt_field_data_int   (hStmt, MAILBOX_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_id);
8869         _bind_stmt_field_data_int   (hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_type);
8870         _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8871         _bind_stmt_field_data_int   (hStmt, DATETIME_IDX_IN_MAIL_TBL, mail_tbl_data->date_time);
8872         _bind_stmt_field_data_int   (hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->server_mail_status);
8873         _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);
8874         _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);
8875         _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);
8876         _bind_stmt_field_data_int   (hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, mail_tbl_data->reference_mail_id);
8877         _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);
8878         _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);
8879         _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);
8880         _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);
8881         _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);
8882         _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);
8883         _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);
8884         _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);
8885         _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);
8886         _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);
8887         _bind_stmt_field_data_int   (hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->body_download_status);
8888         _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);
8889         _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);
8890         _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);
8891         _bind_stmt_field_data_int   (hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, mail_tbl_data->mail_size);
8892         _bind_stmt_field_data_int   (hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_seen_field);
8893         _bind_stmt_field_data_int   (hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_deleted_field);
8894         _bind_stmt_field_data_int   (hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_flagged_field);
8895         _bind_stmt_field_data_int   (hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_answered_field);
8896         _bind_stmt_field_data_int   (hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_recent_field);
8897         _bind_stmt_field_data_int   (hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_draft_field);
8898         _bind_stmt_field_data_int   (hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_forwarded_field);
8899         _bind_stmt_field_data_int   (hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->DRM_status);
8900         _bind_stmt_field_data_int   (hStmt, PRIORITY_IDX_IN_MAIL_TBL, mail_tbl_data->priority);
8901         _bind_stmt_field_data_int   (hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->save_status);
8902         _bind_stmt_field_data_int   (hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->lock_status);
8903         _bind_stmt_field_data_int   (hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->report_status);
8904         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->attachment_count);
8905         _bind_stmt_field_data_int   (hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->inline_content_count);
8906         _bind_stmt_field_data_int   (hStmt, THREAD_ID_IDX_IN_MAIL_TBL, mail_tbl_data->thread_id);
8907         _bind_stmt_field_data_int   (hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->thread_item_count);
8908         _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8909         _bind_stmt_field_data_int   (hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->meeting_request_status);
8910         _bind_stmt_field_data_int   (hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, mail_tbl_data->message_class);
8911         _bind_stmt_field_data_int   (hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->digest_type);
8912         _bind_stmt_field_data_int   (hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->smime_type);
8913         _bind_stmt_field_data_int   (hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->scheduled_sending_time);
8914         _bind_stmt_field_data_int   (hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, mail_tbl_data->eas_data_length);
8915         _bind_stmt_field_data_blob  (hStmt, EAS_DATA_IDX_IN_MAIL_TBL, (void*)mail_tbl_data->eas_data, mail_tbl_data->eas_data_length);
8916
8917         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8918         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
8919         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
8920         ret = true;
8921
8922 FINISH_OFF:
8923
8924         if (hStmt != NULL)  {
8925                 rc = sqlite3_finalize(hStmt);
8926                 if (rc != SQLITE_OK)  {
8927                         EM_DEBUG_LOG("sqlite3_finalize failed [%d]", rc);
8928                         error = EMAIL_ERROR_DB_FAILURE;
8929                 }
8930         }
8931
8932         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8933         _DISCONNECT_DB;
8934
8935
8936         if (err_code != NULL)
8937                 *err_code = error;
8938
8939         EM_PROFILE_END(profile_emstorage_add_mail);
8940         EM_DEBUG_FUNC_END("ret [%d]", ret);
8941         return ret;
8942 }
8943
8944 INTERNAL_FUNC int emstorage_move_multiple_mails_on_db(int input_source_account_id, int input_mailbox_id, int mail_ids[], int number_of_mails, int transaction, int *err_code)
8945 {
8946         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);
8947
8948         int rc, ret = false, i, cur_conditional_clause = 0;
8949         int error = EMAIL_ERROR_NONE;
8950         int target_account_id;
8951         char sql_query_string[QUERY_SIZE] = {0, }, conditional_clause[QUERY_SIZE] = {0, };
8952         emstorage_mailbox_tbl_t *result_mailbox = NULL;
8953         email_mailbox_type_e target_mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED;
8954         char* target_mailbox_name = NULL;
8955
8956         if (!mail_ids || input_mailbox_id <= 0) {
8957                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8958                 if (err_code != NULL)
8959                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8960                 return false;
8961         }
8962
8963         sqlite3 *local_db_handle = emstorage_get_db_connection();
8964
8965         if ((error = emstorage_get_mailbox_by_id(input_mailbox_id, &result_mailbox)) != EMAIL_ERROR_NONE || !result_mailbox) {
8966                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", error);
8967                 goto FINISH_OFF;
8968         }
8969
8970         if (result_mailbox->mailbox_name) {
8971                 if (strstr(result_mailbox->mailbox_name, "'")) {
8972                         target_mailbox_name = em_replace_all_string(result_mailbox->mailbox_name, "'", "''");
8973                 } else {
8974                         target_mailbox_name = strdup(result_mailbox->mailbox_name);
8975                 }
8976         }
8977
8978         target_mailbox_type = result_mailbox->mailbox_type;
8979         target_account_id   = result_mailbox->account_id;
8980         emstorage_free_mailbox(&result_mailbox, 1, NULL);
8981
8982         cur_conditional_clause = SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id in (");
8983
8984         for(i = 0; i < number_of_mails; i++)
8985                 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, QUERY_SIZE, "%d,", mail_ids[i]);
8986
8987         /* prevent 34415 */
8988         char *last_comma = rindex(conditional_clause, ',');
8989         if(last_comma) *last_comma = ')'; /* replace , with ) */
8990
8991         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8992
8993         /* Updating a mail_tbl */
8994
8995         memset(sql_query_string, 0x00, QUERY_SIZE);
8996         SNPRINTF(sql_query_string, QUERY_SIZE, "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);
8997         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8998
8999         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9000         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9001                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9002
9003         /* Updating a mail_attachment_tbl */
9004         memset(sql_query_string, 0x00, QUERY_SIZE);
9005         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_attachment_tbl SET mailbox_id = '%d', account_id = %d %s", input_mailbox_id, target_account_id, conditional_clause);
9006         EM_DEBUG_LOG("Query [%s]", sql_query_string);
9007
9008         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9009         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9010                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9011
9012         /* Updating a mail_meeting_tbl */
9013         memset(sql_query_string, 0x00, QUERY_SIZE);
9014         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_meeting_tbl SET mailbox_id = %d, account_id = %d %s", input_mailbox_id, target_account_id, conditional_clause);
9015         EM_DEBUG_LOG("Query [%s]", sql_query_string);
9016
9017         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9018         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9019                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9020
9021 #ifdef __FEATURE_BODY_SEARCH__
9022         /* Updating mail_text_tbl */
9023         memset(sql_query_string, 0x00, QUERY_SIZE);
9024         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_text_tbl SET mailbox_id = %d, account_id = %d %s", input_mailbox_id, target_account_id, conditional_clause);
9025         EM_DEBUG_LOG("Query [%s]", sql_query_string);
9026
9027         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9028         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9029                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9030 #endif
9031
9032         /* Updating a mail_read_mail_uid_tbl */
9033         memset(conditional_clause, 0x00, QUERY_SIZE);
9034         cur_conditional_clause = SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE local_uid in (");
9035
9036         for(i = 0; i < number_of_mails; i++)
9037                 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, QUERY_SIZE, "%d,", mail_ids[i]);
9038
9039         /* prevent 34415 */
9040         last_comma = rindex(conditional_clause, ',');
9041         if(last_comma) *last_comma = ')'; /* replace , with ) */
9042
9043         memset(sql_query_string, 0x00, QUERY_SIZE);
9044         SNPRINTF(sql_query_string, QUERY_SIZE, "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);
9045         EM_DEBUG_LOG("Query [%s]", sql_query_string);
9046
9047         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9048         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9049                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9050
9051         ret = true;
9052
9053 FINISH_OFF:
9054         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9055         _DISCONNECT_DB;
9056
9057         EM_SAFE_FREE(target_mailbox_name);
9058
9059         if (err_code != NULL)
9060                 *err_code = error;
9061
9062         EM_DEBUG_FUNC_END("ret [%d]", ret);
9063         return ret;
9064 }
9065
9066 INTERNAL_FUNC int emstorage_delete_mail(int mail_id, int from_server, int transaction, int *err_code)
9067 {
9068         EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9069
9070         if (!mail_id)  {
9071                 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
9072                 if (err_code != NULL)
9073                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9074                 return false;
9075         }
9076
9077         int rc, ret = false;
9078         int error = EMAIL_ERROR_NONE;
9079         char sql_query_string[QUERY_SIZE] = {0, };
9080
9081         sqlite3 *local_db_handle = emstorage_get_db_connection();
9082         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9083
9084         memset(sql_query_string, 0x00, sizeof(sql_query_string));
9085
9086         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id = %d ", mail_id);
9087         EM_DEBUG_LOG("Query [%s]", sql_query_string);
9088
9089
9090         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9091         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9092                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9093
9094         ret = true;
9095
9096 FINISH_OFF:
9097         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9098         _DISCONNECT_DB;
9099
9100         if (err_code != NULL)
9101                 *err_code = error;
9102
9103         EM_DEBUG_FUNC_END("ret [%d]", ret);
9104         return ret;
9105 }
9106
9107 INTERNAL_FUNC int emstorage_delete_multiple_mails(int mail_ids[], int number_of_mails, int transaction, int *err_code)
9108 {
9109         EM_DEBUG_FUNC_BEGIN("mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", mail_ids, number_of_mails, transaction, err_code);
9110
9111         int rc, ret = false, i, cur_sql_query_string = 0;
9112         int error = EMAIL_ERROR_NONE;
9113         char sql_query_string[QUERY_SIZE] = {0, };
9114
9115         if (!mail_ids) {
9116                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9117                 if (err_code != NULL)
9118                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9119                 return false;
9120         }
9121
9122         sqlite3 *local_db_handle = emstorage_get_db_connection();
9123         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9124
9125         cur_sql_query_string = SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id in (");
9126
9127         for(i = 0; i < number_of_mails; i++)
9128                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, QUERY_SIZE, "%d,", mail_ids[i]);
9129
9130         /* prevent 34414 */
9131         char *last_comma = rindex(sql_query_string, ',');
9132         *last_comma = ')'; /* replace , with ) */
9133
9134         EM_DEBUG_LOG("Query [%s]", sql_query_string);
9135
9136         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9137         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9138                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9139
9140 #ifdef __FEATURE_BODY_SEARCH__
9141         /* delete mail_text from mail_text_tbl */
9142         cur_sql_query_string = SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE mail_id in (");
9143
9144         for(i = 0; i < number_of_mails; i++)
9145                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, QUERY_SIZE, "%d,", mail_ids[i]);
9146
9147         last_comma = rindex(sql_query_string, ',');
9148         *last_comma = ')'; /* replace , with ) */
9149
9150         EM_DEBUG_LOG("Query [%s]", sql_query_string);
9151
9152         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9153         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9154                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9155 #endif
9156
9157         ret = true;
9158
9159 FINISH_OFF:
9160         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9161         _DISCONNECT_DB;
9162
9163         if (err_code != NULL)
9164                 *err_code = error;
9165
9166         EM_DEBUG_FUNC_END("ret [%d]", ret);
9167         return ret;
9168 }
9169
9170 INTERNAL_FUNC int emstorage_delete_mail_by_account(int account_id, int transaction, int *err_code)
9171 {
9172         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
9173
9174         if (account_id < FIRST_ACCOUNT_ID)  {
9175                 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
9176
9177                 if (err_code != NULL)
9178                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9179                 return false;
9180         }
9181
9182         int rc, ret = false;
9183         int error = EMAIL_ERROR_NONE;
9184         char sql_query_string[QUERY_SIZE] = {0, };
9185
9186         sqlite3 *local_db_handle = emstorage_get_db_connection();
9187         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9188
9189         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d", account_id);
9190         EM_DEBUG_LOG("Query [%s]", sql_query_string);
9191
9192         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9193         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9194                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9195
9196         rc = sqlite3_changes(local_db_handle);
9197         if (rc == 0)  {
9198                 EM_DEBUG_EXCEPTION(" no mail found...");
9199                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9200         }
9201
9202         /* Delete all mails  mail_read_mail_uid_tbl table based on account id */
9203         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
9204         EM_DEBUG_LOG("Query [%s]", sql_query_string);
9205
9206         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9207         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9208                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9209
9210         rc = sqlite3_changes(local_db_handle);
9211         if (rc == 0)  {
9212                 EM_DEBUG_EXCEPTION("no mail found...");
9213                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9214         }
9215
9216 #ifdef __FEATURE_BODY_SEARCH__
9217         /* Delete all mail_text in mail_text_tbl table based on account id */
9218         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE account_id = %d", account_id);
9219         EM_DEBUG_LOG("Query [%s]", sql_query_string);
9220
9221         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9222         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9223                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9224
9225         rc = sqlite3_changes(local_db_handle);
9226         if (rc == 0) {
9227                 EM_DEBUG_EXCEPTION("no mail found...");
9228                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9229         }
9230 #endif
9231
9232         if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_WITH_ACCOUNT, account_id, 0 , NULL, 0))
9233                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_DELETE_ALL ]");
9234
9235         ret = true;
9236
9237 FINISH_OFF:
9238
9239         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9240         _DISCONNECT_DB;
9241
9242         if (err_code != NULL)
9243                 *err_code = error;
9244
9245         EM_DEBUG_FUNC_END("ret [%d]", ret);
9246         return ret;
9247 }
9248
9249 INTERNAL_FUNC int emstorage_delete_mail_by_mailbox(int account_id, int mailbox_id, int transaction, int *err_code)
9250 {
9251         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
9252
9253         if (account_id < FIRST_ACCOUNT_ID || mailbox_id == 0)  {
9254                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_id[%d]", account_id, mailbox_id);
9255                 if (err_code != NULL)
9256                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9257                 return false;
9258         }
9259
9260         int rc, ret = false;
9261         int error = EMAIL_ERROR_NONE;
9262         char sql_query_string[QUERY_SIZE] = {0, };
9263
9264         sqlite3 *local_db_handle = emstorage_get_db_connection();
9265
9266         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9267
9268         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d AND mailbox_id = '%d'", account_id, mailbox_id);
9269         EM_DEBUG_LOG("Query [%s]", sql_query_string);
9270
9271         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9272         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9273                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9274
9275
9276         /* Delete Mails from mail_read_mail_uid_tbl */
9277         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d AND mailbox_id = '%d'", account_id, mailbox_id);
9278         EM_DEBUG_LOG("Query [%s]", sql_query_string);
9279
9280         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9281         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9282                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9283
9284 #ifdef __FEATURE_BODY_SEARCH__
9285         /* Delete Mails from mail_text_tbl */
9286         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE account_id = %d AND mailbox_id = %d", account_id, mailbox_id);
9287         EM_DEBUG_LOG("Query [%s]", sql_query_string);
9288
9289         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9290         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9291                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9292 #endif
9293
9294         if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_ALL, account_id, mailbox_id , 0, 0))
9295                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_DELETE_ALL ] >>>> ");
9296
9297         ret = true;
9298
9299 FINISH_OFF:
9300         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9301         _DISCONNECT_DB;
9302
9303         if (err_code != NULL)
9304                 *err_code = error;
9305
9306         EM_DEBUG_FUNC_END("ret [%d]", ret);
9307         return ret;
9308 }
9309
9310 INTERNAL_FUNC int emstorage_free_mail(emstorage_mail_tbl_t** mail_list, int count, int *err_code)
9311 {
9312         EM_DEBUG_FUNC_BEGIN("mail_list[%p], count[%d], err_code[%p]", mail_list, count, err_code);
9313
9314         if (count > 0)  {
9315                 if ((mail_list == NULL) || (*mail_list == NULL))  {
9316                         EM_DEBUG_EXCEPTION("mail_ilst[%p], count[%d]", mail_list, count);
9317
9318                         if (err_code)
9319                                 *err_code = EMAIL_ERROR_INVALID_PARAM;
9320                         return false;
9321                 }
9322
9323                 emstorage_mail_tbl_t* p = *mail_list;
9324                 int i = 0;
9325
9326                 for (; i < count; i++, p++) {
9327                         EM_SAFE_FREE(p->server_mailbox_name);
9328                         EM_SAFE_FREE(p->server_mail_id);
9329                         EM_SAFE_FREE(p->full_address_from);
9330                         EM_SAFE_FREE(p->full_address_reply);
9331                         EM_SAFE_FREE(p->full_address_to);
9332                         EM_SAFE_FREE(p->full_address_cc);
9333                         EM_SAFE_FREE(p->full_address_bcc);
9334                         EM_SAFE_FREE(p->full_address_return);
9335                         EM_SAFE_FREE(p->subject);
9336                         EM_SAFE_FREE(p->file_path_plain);
9337                         EM_SAFE_FREE(p->file_path_html);
9338                         EM_SAFE_FREE(p->file_path_mime_entity);
9339                         EM_SAFE_FREE(p->message_id);
9340                         EM_SAFE_FREE(p->email_address_sender);
9341                         EM_SAFE_FREE(p->email_address_recipient);
9342                         EM_SAFE_FREE(p->preview_text);
9343                         EM_SAFE_FREE(p->alias_sender);
9344                         EM_SAFE_FREE(p->alias_recipient);
9345                         EM_SAFE_FREE(p->eas_data);
9346                 }
9347                 EM_SAFE_FREE(*mail_list);
9348         }
9349
9350         if (err_code != NULL)
9351                 *err_code = EMAIL_ERROR_NONE;
9352
9353         EM_DEBUG_FUNC_END();
9354         return true;
9355 }
9356
9357 #ifdef __FEATURE_BODY_SEARCH__
9358 INTERNAL_FUNC int emstorage_free_mail_text(emstorage_mail_text_tbl_t** mail_text_list, int count, int *err_code)
9359 {
9360         EM_DEBUG_FUNC_BEGIN("mail_text_list[%p], count[%d], err_code[%p]", mail_text_list, count, err_code);
9361
9362         if (count > 0) {
9363                 if ((mail_text_list == NULL) || (*mail_text_list == NULL)) {
9364                         EM_DEBUG_EXCEPTION("mail_text_list[%p], count[%d]", mail_text_list, count);
9365
9366                         if (err_code)
9367                                 *err_code = EMAIL_ERROR_INVALID_PARAM;
9368                         return false;
9369                 }
9370
9371                 emstorage_mail_text_tbl_t *p = *mail_text_list;
9372                 int i = 0;
9373
9374                 for (; i < count; i++, p++) {
9375                         EM_SAFE_FREE(p->body_text);
9376                 }
9377                 EM_SAFE_FREE(*mail_text_list);
9378         }
9379
9380         if (err_code != NULL)
9381                 *err_code = EMAIL_ERROR_NONE;
9382
9383         EM_DEBUG_FUNC_END();
9384         return true;
9385 }
9386 #endif
9387
9388
9389 INTERNAL_FUNC int emstorage_get_attachment_count(int mail_id, int *count, int transaction, int *err_code)
9390 {
9391         EM_DEBUG_FUNC_BEGIN("mail_id[%d], count[%p], transaction[%d], err_code[%p]", mail_id, count, transaction, err_code);
9392
9393         if (mail_id <= 0 || !count)  {
9394                 EM_DEBUG_EXCEPTION("mail_id[%d], count[%p]", mail_id, count);
9395                 if (err_code != NULL)
9396                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9397                 return false;
9398         }
9399
9400         int rc = -1, ret = false;
9401         int error = EMAIL_ERROR_NONE;
9402         char sql_query_string[QUERY_SIZE] = {0, };
9403
9404         sqlite3 *local_db_handle = emstorage_get_db_connection();
9405         EMSTORAGE_START_READ_TRANSACTION(transaction);
9406
9407         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
9408
9409         char **result;
9410
9411         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
9412         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9413                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9414
9415         *count = atoi(result[1]);
9416         sqlite3_free_table(result);
9417
9418         ret = true;
9419
9420 FINISH_OFF:
9421
9422         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
9423         _DISCONNECT_DB;
9424
9425         if (err_code != NULL)
9426                 *err_code = error;
9427
9428         EM_DEBUG_FUNC_END("ret [%d]", ret);
9429         return ret;
9430 }
9431
9432 INTERNAL_FUNC int emstorage_get_attachment_list(int input_mail_id, int input_transaction, emstorage_attachment_tbl_t** output_attachment_list, int *output_attachment_count)
9433 {
9434         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);
9435
9436         if (input_mail_id <= 0 || !output_attachment_list || !output_attachment_count)  {
9437                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9438                 return EMAIL_ERROR_INVALID_PARAM;
9439         }
9440
9441         int                         error = EMAIL_ERROR_NONE;
9442         int                         i = 0;
9443         int                         rc = -1;
9444         char                      **result = NULL;
9445         char                        sql_query_string[QUERY_SIZE] = {0, };
9446         emstorage_attachment_tbl_t* p_data_tbl = NULL;
9447         DB_STMT hStmt = NULL;
9448         sqlite3 *local_db_handle = emstorage_get_db_connection();
9449
9450         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
9451         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", input_mail_id);
9452         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
9453         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9454                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9455
9456         *output_attachment_count = atoi(result[1]);
9457         sqlite3_free_table(result);
9458
9459         if(*output_attachment_count == 0) {
9460                 error = EMAIL_ERROR_NONE;
9461                 goto FINISH_OFF;
9462         }
9463
9464         p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * (*output_attachment_count));
9465
9466         if (!p_data_tbl)  {
9467                 EM_DEBUG_EXCEPTION("em_malloc failed...");
9468                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9469                 goto FINISH_OFF;
9470         }
9471
9472         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE mail_id = %d ORDER BY attachment_id", input_mail_id);
9473         EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
9474
9475         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9476         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)));
9477
9478         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9479         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },   ("sqlite3_step fail:%d", rc));
9480
9481         if (rc == SQLITE_DONE)  {
9482                 EM_DEBUG_EXCEPTION("no matched attachment found...");
9483                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
9484                 goto FINISH_OFF;
9485         }
9486         for (i = 0; i < *output_attachment_count; i++)  {
9487                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9488                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
9489                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
9490                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
9491                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9492                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9493                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9494                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9495                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9496                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
9497                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9498                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9499
9500                 EM_DEBUG_LOG("attachment[%d].attachment_id : %d", i, p_data_tbl[i].attachment_id);
9501                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9502                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9503         }
9504
9505 FINISH_OFF:
9506
9507         if (error == EMAIL_ERROR_NONE)
9508                 *output_attachment_list = p_data_tbl;
9509         else if (p_data_tbl != NULL)
9510                 emstorage_free_attachment(&p_data_tbl, *output_attachment_count, NULL);
9511
9512         if (hStmt) {
9513                 rc = sqlite3_finalize(hStmt);
9514                 if (rc != SQLITE_OK)  {
9515                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
9516                         error = EMAIL_ERROR_DB_FAILURE;
9517                 }
9518         }
9519
9520         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
9521
9522         _DISCONNECT_DB;
9523
9524         EM_DEBUG_FUNC_END("error [%d]", error);
9525         return error;
9526 }
9527
9528 INTERNAL_FUNC int emstorage_get_attachment(int attachment_id, emstorage_attachment_tbl_t** attachment, int transaction, int *err_code)
9529 {
9530         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], transaction[%d], err_code[%p]", attachment_id, attachment, transaction, err_code);
9531
9532         if (attachment_id <= 0 || !attachment)  {
9533                 EM_DEBUG_EXCEPTION("attachment_id[%d], attachment[%p]", attachment_id, attachment);
9534                 if (err_code != NULL)
9535                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9536                 return false;
9537         }
9538
9539         emstorage_attachment_tbl_t* p_data_tbl = NULL;
9540         int rc, ret = false;
9541         int error = EMAIL_ERROR_NONE;
9542         char sql_query_string[QUERY_SIZE] = {0, };
9543
9544         sqlite3 *local_db_handle = emstorage_get_db_connection();
9545         EMSTORAGE_START_READ_TRANSACTION(transaction);
9546
9547         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_id = %d",  attachment_id);
9548
9549         sqlite3_stmt* hStmt = NULL;
9550
9551         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9552         EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
9553
9554         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9555                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9556
9557
9558         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9559         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9560                 ("sqlite3_step fail:%d", rc));
9561
9562         if (rc == SQLITE_DONE)  {
9563                 EM_DEBUG_EXCEPTION("no matched attachment found...");
9564                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
9565                 goto FINISH_OFF;
9566         }
9567
9568         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1)))  {
9569                 EM_DEBUG_EXCEPTION("malloc failed...");
9570                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9571                 goto FINISH_OFF;
9572         }
9573
9574         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9575         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
9576         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
9577         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
9578         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9579         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9580         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9581         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9582         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9583         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
9584         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9585         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9586
9587 #ifdef __ATTACHMENT_OPTI__
9588                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
9589                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
9590 #endif
9591
9592         ret = true;
9593
9594 FINISH_OFF:
9595         if (ret == true)
9596                 *attachment = p_data_tbl;
9597
9598         if (hStmt != NULL)  {
9599                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
9600
9601                 rc = sqlite3_finalize(hStmt);
9602                 if (rc != SQLITE_OK)  {
9603                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
9604                         error = EMAIL_ERROR_DB_FAILURE;
9605                 }
9606         }
9607
9608         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
9609         _DISCONNECT_DB;
9610
9611         if (err_code != NULL)
9612                 *err_code = error;
9613
9614         EM_DEBUG_FUNC_END("ret [%d]", ret);
9615         return ret;
9616 }
9617
9618 INTERNAL_FUNC int emstorage_get_attachment_nth(int mail_id, int nth, emstorage_attachment_tbl_t** attachment_tbl, int transaction, int *err_code)
9619 {
9620         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);
9621
9622         if (mail_id <= 0 || nth <= 0 || !attachment_tbl)  {
9623                 EM_DEBUG_EXCEPTION(" mail_id[%d], nth[%d], attachment[%p]", mail_id, nth, attachment_tbl);
9624
9625                 if (err_code != NULL)
9626                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9627                 return false;
9628         }
9629
9630         emstorage_attachment_tbl_t* p_data_tbl = NULL;
9631         char *p = NULL;
9632         int rc, ret = false;
9633         int error = EMAIL_ERROR_NONE;
9634         char sql_query_string[QUERY_SIZE] = {0, };
9635
9636         sqlite3 *local_db_handle = emstorage_get_db_connection();
9637         EMSTORAGE_START_READ_TRANSACTION(transaction);
9638
9639         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));
9640         EM_DEBUG_LOG("query = [%s]", sql_query_string);
9641
9642         DB_STMT hStmt = NULL;
9643
9644         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9645         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9646                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9647
9648
9649         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9650         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9651                 ("sqlite3_step fail:%d", rc));
9652
9653         if (rc == SQLITE_DONE)  {
9654                 EM_DEBUG_EXCEPTION(" no matched attachment found...");
9655                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
9656                 goto FINISH_OFF;
9657         }
9658
9659         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1)))  {
9660                 EM_DEBUG_EXCEPTION(" malloc failed...");
9661                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9662                 goto FINISH_OFF;
9663         }
9664
9665         p_data_tbl->attachment_id = sqlite3_column_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9666         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
9667                 p_data_tbl->attachment_name = cpy_str(p);
9668         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
9669                 p_data_tbl->attachment_path = cpy_str(p);
9670         p_data_tbl->attachment_size = sqlite3_column_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
9671         p_data_tbl->mail_id = sqlite3_column_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9672         p_data_tbl->account_id = sqlite3_column_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9673         p_data_tbl->mailbox_id = sqlite3_column_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9674         p_data_tbl->attachment_save_status = sqlite3_column_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9675         p_data_tbl->attachment_drm_type = sqlite3_column_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9676         p_data_tbl->attachment_drm_method = sqlite3_column_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
9677         p_data_tbl->attachment_inline_content_status = sqlite3_column_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9678         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
9679                 p_data_tbl->attachment_mime_type = cpy_str(p);
9680 #ifdef __ATTACHMENT_OPTI__
9681                 p_data_tbl->encoding = sqlite3_column_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
9682                 if ((p = (char *)sqlite3_column_text(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
9683                         p_data_tbl->section= cpy_str(p);
9684 #endif
9685         ret = true;
9686
9687 FINISH_OFF:
9688         if (ret == true)
9689                 *attachment_tbl = p_data_tbl;
9690
9691         if (hStmt != NULL)  {
9692                 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
9693
9694                 rc = sqlite3_finalize(hStmt);
9695                 if (rc != SQLITE_OK)  {
9696                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
9697                         error = EMAIL_ERROR_DB_FAILURE;
9698                 }
9699         }
9700
9701         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
9702         _DISCONNECT_DB;
9703
9704         if (err_code != NULL)
9705                 *err_code = error;
9706
9707         EM_DEBUG_FUNC_END("ret [%d]", ret);
9708         return ret;
9709 }
9710
9711 INTERNAL_FUNC int emstorage_change_attachment_field(int mail_id, email_mail_change_type_t type, emstorage_attachment_tbl_t* attachment, int transaction, int *err_code)
9712 {
9713         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], attachment[%p], transaction[%d], err_code[%p]", mail_id, type, attachment, transaction, err_code);
9714
9715         if (mail_id <= 0 || !attachment)  {
9716                 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], attachment[%p]", mail_id, type, attachment);
9717                 if (err_code != NULL)
9718                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9719                 return false;;
9720         }
9721
9722         int rc, ret = false;
9723         int error = EMAIL_ERROR_NONE;
9724         DB_STMT hStmt = NULL;
9725         char sql_query_string[QUERY_SIZE] = {0, };
9726
9727         int i = 0;
9728
9729         sqlite3 *local_db_handle = emstorage_get_db_connection();
9730
9731         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9732
9733         switch (type)  {
9734                 case UPDATE_MAILBOX:
9735                                 EM_DEBUG_LOG("UPDATE_MAILBOX");
9736                         if (!attachment->mailbox_id)  {
9737                                 EM_DEBUG_EXCEPTION(" attachment->mailbox_id[%d]", attachment->mailbox_id);
9738                                 error = EMAIL_ERROR_INVALID_PARAM;
9739                                 goto FINISH_OFF;
9740                         }
9741                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9742                                 "UPDATE mail_attachment_tbl SET mailbox_id = ? WHERE mail_id = %d", mail_id);
9743
9744                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9745                         EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
9746                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9747                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9748
9749                         _bind_stmt_field_data_int(hStmt, i++, attachment->mailbox_id);
9750                         break;
9751
9752                 case UPDATE_SAVENAME:
9753                         EM_DEBUG_LOG("UPDATE_SAVENAME");
9754                         if (!attachment->attachment_path)  {
9755                                 EM_DEBUG_EXCEPTION(" attachment->attachment_path[%p]", attachment->attachment_path);
9756                                 error = EMAIL_ERROR_INVALID_PARAM;
9757                                 goto FINISH_OFF;
9758                         }
9759
9760                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9761                                 "UPDATE mail_attachment_tbl SET"
9762                                 "  attachment_size = ?"
9763                                 ", attachment_save_status = 1"
9764                                 ", attachment_path = ?"
9765                                 " WHERE mail_id = %d"
9766                                 " AND attachment_id = %d"
9767                                 , attachment->mail_id
9768                                 , attachment->attachment_id);
9769
9770
9771                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9772                         EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
9773                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9774                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9775
9776                         _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_size);
9777                         _bind_stmt_field_data_string(hStmt, i++, (char *)attachment->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
9778                         break;
9779
9780                 default:
9781                         EM_DEBUG_LOG("type[%d]", type);
9782                         error = EMAIL_ERROR_INVALID_PARAM;
9783                         goto FINISH_OFF;
9784         }
9785         EM_DEBUG_LOG("query = [%s]", sql_query_string);
9786
9787         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9788         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9789                 ("sqlite3_step fail:%d", rc));
9790         ret = true;
9791
9792 FINISH_OFF:
9793         if (hStmt != NULL)  {
9794                 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
9795                 rc = sqlite3_finalize(hStmt);
9796                 if (rc != SQLITE_OK)  {
9797                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
9798                         error = EMAIL_ERROR_DB_FAILURE;
9799                 }
9800         }
9801
9802         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9803         _DISCONNECT_DB;
9804
9805         if (err_code != NULL)
9806                 *err_code = error;
9807         EM_DEBUG_FUNC_END("ret [%d]", ret);
9808         return ret;
9809 }
9810
9811
9812 INTERNAL_FUNC int emstorage_rename_mailbox(int input_mailbox_id, char *input_new_mailbox_name, char *input_new_mailbox_alias, int input_transaction)
9813 {
9814         EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], input_new_mailbox_name[%p], input_new_mailbox_alias [%p], input_transaction[%d]", input_mailbox_id, input_new_mailbox_name, input_new_mailbox_alias, input_transaction);
9815
9816         int rc = 0;
9817         int ret = false;
9818         int error = EMAIL_ERROR_NONE;
9819         char sql_query_string[QUERY_SIZE] = {0, };
9820         sqlite3 *local_db_handle = NULL;
9821         int account_id = 0;
9822         emstorage_mailbox_tbl_t *old_mailbox_data = NULL;
9823         char *replaced_mailbox_name = NULL;
9824         char *replaced_alias = NULL;
9825
9826         if (input_mailbox_id <= 0 || !input_new_mailbox_name || !input_new_mailbox_alias)  {
9827                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9828                 return EMAIL_ERROR_INVALID_PARAM;
9829         }
9830
9831         if (strstr(input_new_mailbox_name, "'")) {
9832                 replaced_mailbox_name = em_replace_all_string(input_new_mailbox_name, "'", "''");
9833         } else {
9834                 replaced_mailbox_name = strdup(input_new_mailbox_name);
9835         }
9836
9837         if (strstr(input_new_mailbox_alias, "'")) {
9838                 replaced_alias = em_replace_all_string(input_new_mailbox_alias, "'", "''");
9839         } else {
9840                 replaced_alias = strdup(input_new_mailbox_alias);
9841         }
9842
9843         local_db_handle = emstorage_get_db_connection();
9844
9845         if ((error = emstorage_get_mailbox_by_id(input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
9846                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", error);
9847                 goto FINISH_OFF;
9848         }
9849         account_id = old_mailbox_data->account_id;
9850
9851         EMSTORAGE_START_WRITE_TRANSACTION(input_transaction, error);
9852
9853         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9854                 "UPDATE mail_box_tbl SET"
9855                 " mailbox_name = '%s'"
9856                 ",alias = '%s'"
9857                 " WHERE mailbox_id = %d"
9858                 , replaced_mailbox_name
9859                 , replaced_alias
9860                 , input_mailbox_id);
9861
9862         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9863
9864         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9865                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9866
9867         if (sqlite3_changes(local_db_handle) == 0)
9868                 EM_DEBUG_LOG("no mail_meeting_tbl matched...");
9869
9870         ret = true;
9871
9872 FINISH_OFF:
9873
9874         EMSTORAGE_FINISH_WRITE_TRANSACTION(input_transaction, ret, error);
9875
9876         if (ret) {
9877                 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME, account_id, input_mailbox_id, input_new_mailbox_name, 0))
9878                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAILBOX_RENAME ] >>>> ");
9879         }
9880         else {
9881                 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME_FAIL, account_id, input_mailbox_id, input_new_mailbox_name, error))
9882                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAILBOX_RENAME_FAIL ] >>>> ");
9883         }
9884
9885         EM_SAFE_FREE(replaced_mailbox_name);
9886         EM_SAFE_FREE(replaced_alias);
9887
9888         if (old_mailbox_data)
9889                 emstorage_free_mailbox(&old_mailbox_data, 1, NULL);
9890
9891         _DISCONNECT_DB;
9892
9893         EM_DEBUG_FUNC_END("error [%d]", error);
9894         return error;
9895 }
9896
9897 INTERNAL_FUNC int emstorage_get_new_attachment_no(int *attachment_no, int *err_code)
9898 {
9899         EM_DEBUG_FUNC_BEGIN("attachment_no [%p], err_code[%p]", attachment_no, err_code);
9900         int rc, ret = false;
9901         int error = EMAIL_ERROR_NONE;
9902         char *sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
9903         char **result;
9904
9905         if (!attachment_no)  {
9906                 EM_DEBUG_EXCEPTION("Invalid attachment");
9907                 error = EMAIL_ERROR_INVALID_PARAM;
9908                 goto FINISH_OFF;
9909         }
9910
9911         *attachment_no = -1;
9912
9913         sqlite3 *local_db_handle = emstorage_get_db_connection();
9914
9915
9916         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9917         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9918                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9919
9920         if (NULL == result[1])
9921                 rc = 1;
9922         else
9923                 rc = atoi(result[1])+1;
9924
9925         sqlite3_free_table(result);
9926
9927         *attachment_no = rc;
9928         EM_DEBUG_LOG("attachment_no [%d]", *attachment_no);
9929         ret = true;
9930
9931 FINISH_OFF:
9932         _DISCONNECT_DB;
9933
9934         if (err_code != NULL)
9935                 *err_code = error;
9936
9937         EM_DEBUG_FUNC_END("ret [%d]", ret);
9938         return ret;
9939 }
9940
9941 INTERNAL_FUNC int emstorage_add_attachment(emstorage_attachment_tbl_t* attachment_tbl, int iscopy, int transaction, int *err_code)
9942 {
9943         EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], iscopy[%d], transaction[%d], err_code[%p]", attachment_tbl, iscopy, transaction, err_code);
9944
9945         char *sql = NULL;
9946         char **result;
9947         int rc, ret = false;
9948         int error = EMAIL_ERROR_NONE;
9949         DB_STMT hStmt = NULL;
9950         char sql_query_string[QUERY_SIZE] = {0, };
9951         sqlite3 *local_db_handle = emstorage_get_db_connection();
9952
9953         if (!attachment_tbl)  {
9954                 EM_DEBUG_EXCEPTION("attachment_tbl[%p], iscopy[%d]", attachment_tbl, iscopy);
9955                 if (err_code != NULL)
9956                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9957                 return false;
9958         }
9959
9960         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9961
9962         sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
9963
9964         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9965         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9966                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9967
9968         if (NULL==result[1]) rc = 1;
9969         else rc = atoi(result[1]) + 1;
9970         sqlite3_free_table(result);
9971
9972         attachment_tbl->attachment_id = rc;
9973
9974         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9975                 "INSERT INTO mail_attachment_tbl VALUES "
9976                 "( ?"   /* attachment_id */
9977                 ", ?"   /* attachment_name */
9978                 ", ?"   /* attachment_path */
9979                 ", ?"   /* attachment_size */
9980                 ", ?"   /* mail_id */
9981                 ", ?"   /* account_id */
9982                 ", ?"   /* mailbox_id */
9983                 ", ?"   /* attachment_save_status */
9984                 ", ?"   /* attachment_drm_type */
9985                 ", ?"   /* attachment_drm_method */
9986                 ", ?"   /* attachment_inline_content_status */
9987                 ", ?"   /* attachment_mime_type */
9988 #ifdef __ATTACHMENT_OPTI__
9989                 ", ?"
9990                 ", ?"
9991 #endif
9992                 ")");
9993
9994
9995         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9996         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9997                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9998
9999         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_id);
10000         _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);
10001         _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);
10002         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_size);
10003         _bind_stmt_field_data_int   (hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mail_id);
10004         _bind_stmt_field_data_int   (hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->account_id);
10005         _bind_stmt_field_data_int   (hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mailbox_id);
10006         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_save_status);
10007         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_type);
10008         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_method);
10009         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_inline_content_status);
10010         _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);
10011 #ifdef __ATTACHMENT_OPTI__
10012         _bind_stmt_field_data_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->encoding);
10013         _bind_stmt_field_data_string(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->section, 0, ATTACHMENT_LEN_IN_MAIL_ATTACHMENT_TBL);
10014 #endif
10015
10016
10017         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10018         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
10019                 ("sqlite3_step fail:%d", rc));
10020         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10021                 ("sqlite3_step fail:%d", rc));
10022 /*
10023         SNPRINTF(sql_query_string, sizeof(sql_query_string),
10024                 "UPDATE mail_tbl SET attachment_count = 1 WHERE mail_id = %d", attachment_tbl->mail_id);
10025
10026         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10027
10028         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
10029                 ("sqlite3_exec fail:%d", rc));
10030         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10031                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10032 */
10033         rc = sqlite3_changes(local_db_handle);
10034         if (rc == 0)  {
10035                 EM_DEBUG_EXCEPTION(" no matched mail found...");
10036                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10037                 goto FINISH_OFF;
10038         }
10039
10040         ret = true;
10041
10042 FINISH_OFF:
10043         if (hStmt != NULL)  {
10044                 rc = sqlite3_finalize(hStmt);
10045                 if (rc != SQLITE_OK)  {
10046                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
10047                         error = EMAIL_ERROR_DB_FAILURE;
10048                 }
10049         }
10050
10051         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10052         _DISCONNECT_DB;
10053
10054         if (err_code != NULL)
10055         *err_code = error;
10056
10057         EM_DEBUG_FUNC_END("ret [%d]", ret);
10058         return ret;
10059 }
10060
10061 INTERNAL_FUNC int emstorage_update_attachment(emstorage_attachment_tbl_t* attachment_tbl, int transaction, int *err_code)
10062 {
10063         EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], transaction[%d], err_code[%p]", attachment_tbl, transaction, err_code);
10064
10065         int rc, ret = false, field_idx = 0;
10066         int error = EMAIL_ERROR_NONE;
10067         DB_STMT hStmt = NULL;
10068         char sql_query_string[QUERY_SIZE] = {0, };
10069
10070         if (!attachment_tbl)  {
10071                 EM_DEBUG_EXCEPTION(" attachment_tbl[%p] ", attachment_tbl);
10072                 error = EMAIL_ERROR_INVALID_PARAM;
10073                 goto FINISH_OFF;
10074         }
10075
10076         sqlite3 *local_db_handle = emstorage_get_db_connection();
10077
10078         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10079
10080         SNPRINTF(sql_query_string, sizeof(sql_query_string),
10081                 "UPDATE mail_attachment_tbl SET  "
10082                 "  attachment_name = ?"
10083                 ", attachment_path =  ?"
10084                 ", attachment_size = ?"
10085                 ", mail_id = ?"
10086                 ", account_id = ?"
10087                 ", mailbox_id = ?"
10088                 ", attachment_save_status = ?"
10089                 ", attachment_drm_type = ?"
10090                 ", attachment_drm_method = ?"
10091                 ", attachment_inline_content_status = ? "
10092                 ", attachment_mime_type = ? "
10093                 " WHERE attachment_id = ?;");
10094
10095
10096         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10097
10098         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10099                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10100
10101         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10102         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10103         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_size);
10104         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->mail_id);
10105         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->account_id);
10106         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->mailbox_id);
10107         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_save_status);
10108         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_drm_type);
10109         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_drm_method);
10110         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_inline_content_status);
10111         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_mime_type, 0, ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL);
10112         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_id);
10113
10114
10115         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10116         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
10117                 ("sqlite3_step fail:%d", rc));
10118         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10119                 ("sqlite3_step fail:%d", rc));
10120 /*
10121         SNPRINTF(sql_query_string, sizeof(sql_query_string),
10122                 "UPDATE mail_tbl SET attachment_count = 1 WHERE mail_id = %d", attachment_tbl->mail_id);
10123
10124         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10125
10126         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
10127                 ("sqlite3_exec fail:%d", rc));
10128         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10129                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10130 */
10131         rc = sqlite3_changes(local_db_handle);
10132         if (rc == 0)  {
10133                 EM_DEBUG_EXCEPTION(" no matched mail found...");
10134                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10135                 goto FINISH_OFF;
10136         }
10137
10138         ret = true;
10139
10140 FINISH_OFF:
10141         if (hStmt != NULL)  {
10142                 rc = sqlite3_finalize(hStmt);
10143                 if (rc != SQLITE_OK)  {
10144                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
10145                         error = EMAIL_ERROR_DB_FAILURE;
10146                 }
10147         }
10148
10149         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10150         _DISCONNECT_DB;
10151
10152         if (err_code != NULL)
10153         *err_code = error;
10154
10155         EM_DEBUG_FUNC_END("ret [%d]", ret);
10156         return ret;
10157 }
10158
10159 INTERNAL_FUNC int emstorage_delete_attachment_on_db(int attachment_id, int transaction, int *err_code)
10160 {
10161         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], transaction[%d], err_code[%p]", attachment_id, transaction, err_code);
10162
10163         if (attachment_id < 0)  {
10164                 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
10165                 if (err_code != NULL)
10166                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10167                 return false;
10168         }
10169
10170         int rc, ret = false;
10171         int error = EMAIL_ERROR_NONE;
10172         char sql_query_string[QUERY_SIZE] = {0, };
10173
10174         sqlite3 *local_db_handle = emstorage_get_db_connection();
10175
10176         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10177
10178         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
10179
10180         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10181         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10182                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10183
10184         ret = true;
10185
10186 FINISH_OFF:
10187         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10188         _DISCONNECT_DB;
10189
10190         if (err_code)
10191                 *err_code = error;
10192
10193         EM_DEBUG_FUNC_END("ret [%d]", ret);
10194         return ret;
10195 }
10196
10197 INTERNAL_FUNC int emstorage_delete_all_attachments_of_mail(int mail_id, int transaction, int *err_code)
10198 {
10199         EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
10200         int rc, ret = false;
10201         int error = EMAIL_ERROR_NONE;
10202         char sql_query_string[QUERY_SIZE] = {0, };
10203         sqlite3 *local_db_handle = NULL;
10204
10205         if (mail_id <= 0)  {
10206                 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
10207                 if (err_code != NULL)
10208                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10209                 return false;
10210         }
10211
10212         local_db_handle = emstorage_get_db_connection();
10213
10214         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10215
10216         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
10217
10218         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10219         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10220                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10221
10222         ret = true;
10223
10224 FINISH_OFF:
10225         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10226         _DISCONNECT_DB;
10227
10228         if (err_code)
10229                 *err_code = error;
10230
10231         EM_DEBUG_FUNC_END("ret [%d]", ret);
10232         return ret;
10233 }
10234
10235 INTERNAL_FUNC int emstorage_delete_attachment_all_on_db(int account_id, char *mailbox, int transaction, int *err_code)
10236 {
10237         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox, transaction, err_code);
10238
10239         int error = EMAIL_ERROR_NONE;
10240         int rc, ret = false;
10241         char sql_query_string[QUERY_SIZE] = {0, };
10242         char *replaced_mailbox = NULL;
10243
10244         sqlite3 *local_db_handle = emstorage_get_db_connection();
10245
10246         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10247
10248         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl");
10249
10250         if (account_id != ALL_ACCOUNT) /*  '0' means all account */
10251                 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);
10252
10253         if (mailbox)    /*  NULL means all mailbox_name */ {
10254                 if (strstr(mailbox, "'")) {
10255                         replaced_mailbox = em_replace_all_string(mailbox, "'", "''");
10256                 } else {
10257                         replaced_mailbox = strdup(mailbox);
10258                 }
10259
10260                 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);
10261                 EM_SAFE_FREE(replaced_mailbox); /*prevent 49434*/
10262         }
10263         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10264         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10265                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10266
10267         ret = true;
10268
10269 FINISH_OFF:
10270         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10271         _DISCONNECT_DB;
10272
10273         if (err_code != NULL)
10274                 *err_code = error;
10275
10276         EM_DEBUG_FUNC_END("ret [%d]", ret);
10277         return ret;
10278 }
10279
10280 INTERNAL_FUNC int emstorage_free_attachment(emstorage_attachment_tbl_t** attachment_tbl_list, int count, int *err_code)
10281 {
10282         EM_DEBUG_FUNC_BEGIN("attachment_tbl_list[%p], count[%d], err_code[%p]", attachment_tbl_list, count, err_code);
10283
10284         if (count > 0)  {
10285                 if ((attachment_tbl_list == NULL) || (*attachment_tbl_list == NULL))  {
10286                         EM_DEBUG_EXCEPTION(" attachment_tbl_list[%p], count[%d]", attachment_tbl_list, count);
10287                         if (err_code != NULL)
10288                                 *err_code = EMAIL_ERROR_INVALID_PARAM;
10289                         return false;
10290                 }
10291
10292                 emstorage_attachment_tbl_t* p = *attachment_tbl_list;
10293                 int i;
10294
10295                 for (i = 0; i < count; i++)  {
10296                         EM_SAFE_FREE(p[i].attachment_name);
10297                         EM_SAFE_FREE(p[i].attachment_path);
10298                         EM_SAFE_FREE(p[i].attachment_mime_type);
10299 #ifdef __ATTACHMENT_OPTI__
10300                         EM_SAFE_FREE(p[i].section);
10301 #endif
10302                 }
10303
10304                 EM_SAFE_FREE(p);
10305                 *attachment_tbl_list = NULL;
10306         }
10307
10308         if (err_code != NULL)
10309                 *err_code = EMAIL_ERROR_NONE;
10310         EM_DEBUG_FUNC_END();
10311         return true;
10312 }
10313
10314
10315
10316 INTERNAL_FUNC int emstorage_begin_transaction(void *d1, void *d2, int *err_code)
10317 {
10318         EM_PROFILE_BEGIN(emStorageBeginTransaction);
10319         int ret = true;
10320
10321         ENTER_CRITICAL_SECTION(_transactionBeginLock);
10322
10323         /*  wait for the trnasaction authority to be changed. */
10324         while (g_transaction)  {
10325                 EM_DEBUG_LOG(">>>>>>>> Wait for the transaction authority to be changed");
10326                 usleep(50000);
10327         }
10328
10329         /*  take the transaction authority. */
10330         g_transaction = true;
10331
10332         LEAVE_CRITICAL_SECTION(_transactionBeginLock);
10333
10334         sqlite3 *local_db_handle = emstorage_get_db_connection();
10335         int rc;
10336         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN immediate;", NULL, NULL, NULL), rc);
10337         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
10338                 ("SQL(BEGIN) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
10339
10340         if (ret == false) {
10341                 if (err_code != NULL) *err_code = EMAIL_ERROR_DB_FAILURE;
10342                 ENTER_CRITICAL_SECTION(_transactionEndLock);
10343                 g_transaction = false;
10344                 LEAVE_CRITICAL_SECTION(_transactionEndLock);
10345         }
10346
10347         EM_PROFILE_END(emStorageBeginTransaction);
10348         EM_DEBUG_FUNC_END("ret [%d]", ret);
10349         return ret;
10350 }
10351
10352 INTERNAL_FUNC int emstorage_commit_transaction(void *d1, void *d2, int *err_code)
10353 {
10354         EM_DEBUG_FUNC_BEGIN();
10355         int ret = true;
10356         sqlite3 *local_db_handle = emstorage_get_db_connection();
10357
10358         ENTER_CRITICAL_SECTION(_transactionEndLock);
10359
10360         int rc;
10361         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
10362         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; }, ("SQL(END) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
10363         /*  release the transaction authority. */
10364         g_transaction = false;
10365
10366         LEAVE_CRITICAL_SECTION(_transactionEndLock);
10367         if (ret == false && err_code != NULL)
10368                 *err_code = EMAIL_ERROR_DB_FAILURE;
10369
10370         EM_DEBUG_FUNC_END("ret [%d]", ret);
10371         return ret;
10372 }
10373
10374 INTERNAL_FUNC int emstorage_rollback_transaction(void *d1, void *d2, int *err_code)
10375 {
10376         EM_DEBUG_FUNC_BEGIN();
10377         int ret = true;
10378         sqlite3 *local_db_handle = emstorage_get_db_connection();
10379         int rc;
10380
10381         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "ROLLBACK;", NULL, NULL, NULL), rc);
10382         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
10383                 ("SQL(ROLLBACK) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
10384
10385         ENTER_CRITICAL_SECTION(_transactionEndLock);
10386
10387         /*  release the transaction authority. */
10388         g_transaction = false;
10389
10390         LEAVE_CRITICAL_SECTION(_transactionEndLock);
10391
10392         if (ret == false && err_code != NULL)
10393                 *err_code = EMAIL_ERROR_DB_FAILURE;
10394
10395         EM_DEBUG_FUNC_END("ret [%d]", ret);
10396         return ret;
10397 }
10398
10399 INTERNAL_FUNC int emstorage_is_mailbox_full(int account_id, email_mailbox_t *mailbox, int *result, int *err_code)
10400 {
10401         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], result[%p], err_code[%p]", account_id, mailbox, result, err_code);
10402
10403         if (account_id < FIRST_ACCOUNT_ID || !mailbox || !result)  {
10404                 if (mailbox)
10405                         EM_DEBUG_EXCEPTION("Invalid Parameter. accoun_id[%d], mailbox[%p]", account_id, mailbox);
10406
10407                 if (err_code != NULL)
10408                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10409
10410                 return false;
10411         }
10412
10413         int ret = false;
10414         int error = EMAIL_ERROR_NONE;
10415         int mail_count = 0;
10416
10417         if (!emstorage_get_mail_count(account_id, mailbox->mailbox_id, &mail_count, NULL, true, &error)) {
10418                 EM_DEBUG_EXCEPTION("emstorage_get_mail_count failed [%d]", error);
10419                 goto FINISH_OFF;
10420         }
10421
10422         if (mailbox) {
10423                 EM_DEBUG_LOG("mail_count[%d] mail_slot_size[%d]", mail_count, mailbox->mail_slot_size);
10424                 if (mail_count >= mailbox->mail_slot_size)
10425                         *result = true;
10426                 else
10427                         *result = false;
10428
10429                 ret = true;
10430         }
10431
10432         ret = true;
10433 FINISH_OFF:
10434
10435         if (err_code != NULL)
10436                 *err_code = error;
10437
10438         EM_DEBUG_FUNC_END("ret [%d]", ret);
10439         return ret;
10440 }
10441
10442 INTERNAL_FUNC int emstorage_clear_mail_data(int transaction, int *err_code)
10443 {
10444         EM_DEBUG_FUNC_BEGIN("transaction[%d], err_code[%p]", transaction, err_code);
10445
10446         int rc, ret = false;
10447         int error = EMAIL_ERROR_NONE;
10448         char sql_query_string[QUERY_SIZE] = {0, };
10449
10450         const email_db_object_t* tables = _g_db_tables;
10451         const email_db_object_t* indexes = _g_db_indexes;
10452
10453         sqlite3 *local_db_handle = emstorage_get_db_connection();
10454         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10455
10456         if (!emstorage_delete_dir(MAILHOME, &error)) {
10457                 EM_DEBUG_EXCEPTION(" emstorage_delete_dir failed - %d", error);
10458
10459                 goto FINISH_OFF;
10460         }
10461
10462         mkdir(MAILHOME, DIRECTORY_PERMISSION);
10463         mkdir(MAILTEMP, DIRECTORY_PERMISSION);
10464
10465         /*  first clear index. */
10466         while (indexes->object_name)  {
10467                 if (indexes->data_flag)  {
10468                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP index %s", indexes->object_name);
10469                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10470                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10471                                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10472                 }
10473                 indexes++;
10474         }
10475
10476         while (tables->object_name)  {
10477                 if (tables->data_flag)  {
10478                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP table %s", tables->object_name);
10479                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10480                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10481                                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10482                 }
10483
10484                 tables++;
10485         }
10486         ret = true;
10487
10488 FINISH_OFF:
10489         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10490         _DISCONNECT_DB;
10491
10492         if (err_code != NULL)
10493                 *err_code = error;
10494
10495         EM_DEBUG_FUNC_END("ret [%d]", ret);
10496         return ret;
10497 }
10498 /*======================= DB File Utils =============================================*/
10499 #include <dirent.h>
10500 #include <sys/types.h>
10501 #define  DIR_SEPERATOR "/"
10502
10503 INTERNAL_FUNC char *emstorage_make_directory_path_from_file_path(char *file_name)
10504 {
10505         EM_DEBUG_FUNC_BEGIN("Filename [ %p ]", file_name);
10506         char delims[] = "/";
10507         char *result = NULL;
10508         gchar **token = NULL;
10509
10510         token = g_strsplit_set(file_name, delims, 1);
10511
10512         if (token && token[0]) {
10513                 EM_DEBUG_LOG(">>>> Directory_name [ %s ]", token[0]);
10514                 result = EM_SAFE_STRDUP(token[0]);
10515         } else
10516                 EM_DEBUG_LOG(">>>> No Need to create Directory");
10517
10518         g_strfreev(token);
10519
10520         return result;
10521 }
10522
10523 INTERNAL_FUNC int emstorage_get_save_name(int account_id, int mail_id, int atch_id, char *fname, char *name_buf, int *err_code)
10524 {
10525         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], fname[%s], name_buf[%p], err_code[%p]", account_id, mail_id, atch_id, fname, name_buf, err_code);
10526         EM_PROFILE_BEGIN(profile_emstorage_get_save_name);
10527
10528         int ret = false;
10529         int error = EMAIL_ERROR_NONE;
10530         char *dir_name = NULL;
10531         char create_dir[1024]={0};
10532         char *temp_file = NULL;
10533
10534         if (!name_buf || account_id < FIRST_ACCOUNT_ID || mail_id < 0 || atch_id < 0)  {
10535                 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);
10536                 error = EMAIL_ERROR_INVALID_PARAM;
10537                 goto FINISH_OFF;
10538         }
10539
10540         sprintf(name_buf, "%s", MAILHOME);
10541         sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, account_id);
10542
10543         if (mail_id > 0)
10544                 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, mail_id);
10545
10546         if (atch_id > 0)
10547                 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, atch_id);
10548
10549         if (fname) {
10550                 temp_file = EM_SAFE_STRDUP(fname);
10551                 if (strstr(temp_file, "/")) {
10552                         dir_name = emstorage_make_directory_path_from_file_path(temp_file);
10553                 }
10554         }
10555
10556         if (dir_name) {
10557                 sprintf(create_dir, "%s%s%s", name_buf, DIR_SEPERATOR, dir_name);
10558                 EM_DEBUG_LOG(">>>>> DIR PATH [ %s ]", create_dir);
10559                 mkdir(create_dir, DIRECTORY_PERMISSION);
10560                 EM_SAFE_FREE(dir_name);
10561         }
10562
10563         if (fname) {
10564                 EM_DEBUG_LOG(">>>>> fname [ %s ]", fname);
10565                 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%s", DIR_SEPERATOR, fname);
10566         }
10567
10568         EM_DEBUG_LOG(">>>>> name_buf [ %s ]", name_buf);
10569
10570         ret = true;
10571
10572 FINISH_OFF:
10573         EM_SAFE_FREE(temp_file);
10574
10575         if (err_code != NULL)
10576                 *err_code = error;
10577
10578         EM_PROFILE_END(profile_emstorage_get_save_name);
10579         EM_DEBUG_FUNC_END("ret [%d]", ret);
10580         return ret;
10581 }
10582
10583 INTERNAL_FUNC int emstorage_get_dele_name(int account_id, int mail_id, int atch_id, char *fname, char *name_buf, int *err_code)
10584 {
10585         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);
10586
10587         if (!name_buf || account_id < FIRST_ACCOUNT_ID)  {
10588                 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);
10589                 if (err_code != NULL)
10590                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10591                 return false;
10592         }
10593
10594         sprintf(name_buf+EM_SAFE_STRLEN(name_buf),      "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
10595
10596         if (mail_id > 0)
10597                 sprintf(name_buf+EM_SAFE_STRLEN(name_buf),      "%s%d", DIR_SEPERATOR, mail_id);
10598         else
10599                 goto FINISH_OFF;
10600
10601         if (atch_id > 0)
10602                 sprintf(name_buf+EM_SAFE_STRLEN(name_buf),      "%s%d", DIR_SEPERATOR, atch_id);
10603         else
10604                 goto FINISH_OFF;
10605
10606 FINISH_OFF:
10607         sprintf(name_buf+EM_SAFE_STRLEN(name_buf),      ".DELE");
10608
10609         EM_DEBUG_FUNC_END();
10610         return true;
10611 }
10612
10613 INTERNAL_FUNC int emstorage_create_dir(int account_id, int mail_id, int atch_id, int *err_code)
10614 {
10615         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], err_code[%p]", account_id, mail_id, atch_id, err_code);
10616         EM_PROFILE_BEGIN(profile_emcore_save_create_dir);
10617         int ret = false;
10618         int error = EMAIL_ERROR_NONE;
10619
10620         char buf[512];
10621         struct stat sbuf;
10622         if (account_id >= FIRST_ACCOUNT_ID)  {
10623                 SNPRINTF(buf, sizeof(buf), "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
10624
10625                 if (stat(buf, &sbuf) == 0) {
10626                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR)  {
10627                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
10628                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10629                                 goto FINISH_OFF;
10630                         }
10631                 }
10632                 else  {
10633                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
10634                                 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
10635                                 EM_DEBUG_EXCEPTION("mkdir failed l(Errno=%d)][ErrStr=%s]", errno, strerror(errno));
10636                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10637                                 goto FINISH_OFF;
10638                         }
10639                 }
10640         }
10641
10642         if (mail_id > 0)  {
10643                 if (account_id < FIRST_ACCOUNT_ID)  {
10644                         EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
10645                         error = EMAIL_ERROR_INVALID_PARAM;
10646                         goto FINISH_OFF;
10647                 }
10648
10649                 SNPRINTF(buf+EM_SAFE_STRLEN(buf), sizeof(buf), "%s%d", DIR_SEPERATOR, mail_id);
10650
10651                 if (stat(buf, &sbuf) == 0) {
10652                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR)  {
10653                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
10654
10655                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10656                                 goto FINISH_OFF;
10657                         }
10658                 }
10659                 else  {
10660                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
10661                                 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
10662                                 EM_DEBUG_EXCEPTION("mkdir failed l (Errno=%d)][ErrStr=%s]", errno, strerror(errno));
10663                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10664                                 goto FINISH_OFF;
10665                         }
10666                 }
10667         }
10668
10669         if (atch_id > 0)  {
10670                 if (account_id < FIRST_ACCOUNT_ID || mail_id <= 0)  {
10671                         EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
10672
10673                         error = EMAIL_ERROR_INVALID_PARAM;
10674                         goto FINISH_OFF;
10675                 }
10676
10677                 SNPRINTF(buf+EM_SAFE_STRLEN(buf), sizeof(buf)-(EM_SAFE_STRLEN(buf)+1), "%s%d", DIR_SEPERATOR, atch_id);
10678
10679                 if (stat(buf, &sbuf) == 0) {
10680                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR)  {
10681                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
10682
10683                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10684                                 goto FINISH_OFF;
10685                         }
10686                 }
10687                 else  {
10688                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
10689                                 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
10690                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10691                                 goto FINISH_OFF;
10692                         }
10693                 }
10694         }
10695
10696         ret = true;
10697
10698 FINISH_OFF:
10699         if (err_code != NULL)
10700                 *err_code = error;
10701
10702         EM_PROFILE_END(profile_emcore_save_create_dir);
10703         EM_DEBUG_FUNC_END("ret [%d]", ret);
10704         return ret;
10705 }
10706
10707 INTERNAL_FUNC int emstorage_copy_file(char *src_file, char *dst_file, int sync_status, int *err_code)
10708 {
10709         EM_DEBUG_FUNC_BEGIN("src_file[%s], dst_file[%s], err_code[%p]", src_file, dst_file, err_code);
10710         EM_DEBUG_LOG("Using the fsync function");
10711         int ret = false;
10712         int error = EMAIL_ERROR_NONE;
10713         struct stat st_buf;
10714
10715         int fp_src = 0;
10716         int fp_dst = 0;
10717         int nread = 0;
10718         int nwritten = 0;
10719         char *buf =  NULL;
10720         int buf_size = 0;
10721
10722         if (!src_file || !dst_file)  {
10723                 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
10724
10725                 error = EMAIL_ERROR_INVALID_PARAM;
10726                 goto FINISH_OFF;
10727         }
10728
10729         if (stat(src_file, &st_buf) < 0) {
10730                 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", src_file);
10731
10732                 error = EMAIL_ERROR_SYSTEM_FAILURE;             /* EMAIL_ERROR_INVALID_PATH; */
10733                 goto FINISH_OFF;
10734         }
10735
10736         buf_size =  st_buf.st_size;
10737         EM_DEBUG_LOG(">>>> File Size [ %d ]", buf_size);
10738         buf = (char *)calloc(1, buf_size+1);
10739
10740         if (!buf) {
10741                 EM_DEBUG_EXCEPTION(">>> Memory cannot be allocated");
10742                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10743                 goto FINISH_OFF;
10744         }
10745
10746         if (buf) {
10747                 if ((fp_src = open(src_file, O_RDONLY))<0) { /*prevent 24474*/
10748                         EM_DEBUG_EXCEPTION(">>>> Source Fail open %s Failed [ %d ] - Error [ %s ]", src_file, errno, strerror(errno));
10749                         error = EMAIL_ERROR_SYSTEM_FAILURE;
10750                         goto FINISH_OFF;
10751                 }
10752
10753                 if ((fp_dst = open(dst_file, O_CREAT | O_WRONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH))<0) { /*prevent 24474*/
10754                         EM_DEBUG_EXCEPTION(">>>> Destination Fail open %s Failed [ %d ] - Error [ %s ]", dst_file, errno, strerror(errno));
10755                         error = EMAIL_ERROR_SYSTEM_FAILURE;
10756                         goto FINISH_OFF;
10757                 }
10758
10759                 while ((nread = read(fp_src, buf, buf_size)) > 0) {
10760                         if (nread > 0 && nread <= buf_size)  {
10761                                 EM_DEBUG_LOG("Nread Value [%d]", nread);
10762                                 if ((nwritten = write(fp_dst, buf, nread)) != nread) {
10763                                         EM_DEBUG_EXCEPTION("fwrite failed...[%d] : [%s]", errno, strerror(errno));
10764                                         error = EMAIL_ERROR_UNKNOWN;
10765                                         goto FINISH_OFF;
10766                                 }
10767                                 EM_DEBUG_LOG("NWRITTEN [%d]", nwritten);
10768                         }
10769                 }
10770         }
10771
10772         ret = true;
10773
10774 FINISH_OFF:
10775         if (fp_src>0) /*prevent 24474*/
10776                 close(fp_src);
10777
10778         if (fp_dst>0) { /*prevent 24474*/
10779                 if (sync_status) {
10780                         EM_DEBUG_LOG("Before fsync");
10781                         fsync(fp_dst);
10782                 }
10783                 close(fp_dst);
10784         }
10785         EM_SAFE_FREE(buf);
10786         if (nread < 0 || error == EMAIL_ERROR_UNKNOWN)
10787                 remove(dst_file);
10788
10789         if (err_code != NULL)
10790                 *err_code = error;
10791         EM_DEBUG_FUNC_END("ret [%d]", ret);
10792         return ret;
10793 }
10794 /* create Directory if user has deleted [deepam.p@samsung.com] */
10795 INTERNAL_FUNC void emstorage_create_dir_if_delete()
10796 {
10797         EM_DEBUG_FUNC_BEGIN();
10798
10799         mkdir(DATA_PATH, DIRECTORY_PERMISSION);
10800         mkdir(EMAILPATH, DIRECTORY_PERMISSION);
10801         mkdir(MAILHOME, DIRECTORY_PERMISSION);
10802         mkdir(MAILTEMP, DIRECTORY_PERMISSION);
10803
10804         EM_DEBUG_FUNC_END();
10805 }
10806 static int _get_temp_file_name(char **filename, int *err_code)
10807 {
10808         EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
10809
10810         int ret = false;
10811         int error = EMAIL_ERROR_NONE;
10812
10813         if (filename == NULL) {
10814                 EM_DEBUG_EXCEPTION(" filename[%p]", filename);
10815                 error = EMAIL_ERROR_INVALID_PARAM;
10816                 goto FINISH_OFF;
10817         }
10818
10819         char tempname[512] = {0x00, };
10820         struct timeval tv;
10821
10822         gettimeofday(&tv, NULL);
10823         srand(tv.tv_usec);
10824
10825         SNPRINTF(tempname, sizeof(tempname), "%s%c%d", MAILTEMP, '/', rand());
10826
10827         char *p = EM_SAFE_STRDUP(tempname);
10828         if (p == NULL)  {
10829                 EM_DEBUG_EXCEPTION(" strdup failed...");
10830                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10831                 goto FINISH_OFF;
10832         }
10833
10834         *filename = p;
10835
10836         ret = true;
10837
10838 FINISH_OFF:
10839         if (err_code != NULL)
10840                 *err_code = error;
10841
10842         EM_DEBUG_FUNC_END("ret [%d]", ret);
10843         return ret;
10844 }
10845
10846 INTERNAL_FUNC int emstorage_add_content_type(char *file_path, char *char_set, int *err_code)
10847 {
10848         EM_DEBUG_FUNC_BEGIN("File path [ %p ]  Character Set [ %p ] err_code [ %p]", file_path, char_set, err_code);
10849
10850         EM_IF_NULL_RETURN_VALUE(file_path, false);
10851         EM_IF_NULL_RETURN_VALUE(char_set, false);
10852         EM_IF_NULL_RETURN_VALUE(err_code, false);
10853
10854         char *buf =  NULL;
10855         char *buf1 = NULL;
10856         struct stat st_buf;
10857         int buf_size = 0;
10858         char *low_char_set = NULL;
10859         char *match_str = NULL;
10860         int nwritten = 0;
10861         int ret = false;
10862         int error = EMAIL_ERROR_NONE;
10863         int data_count_to_written = 0;
10864         char *temp_file_name = NULL;
10865         int err = 0;
10866
10867         FILE* fp_src = NULL;
10868         FILE* fp_dest = NULL;
10869         int nread = 0;
10870
10871
10872         if (stat(file_path, &st_buf) < 0) {
10873                 EM_DEBUG_EXCEPTION(" stat(\"%s\") failed...", file_path);
10874
10875                 error = EMAIL_ERROR_SYSTEM_FAILURE;             /* EMAIL_ERROR_INVALID_PATH; */
10876                 goto FINISH_OFF;
10877         }
10878
10879         buf_size =  st_buf.st_size;
10880
10881         EM_DEBUG_LOG(">>>> File Size [ %d ] ", buf_size);
10882
10883         buf = (char *)calloc(1, buf_size+1);
10884
10885         if (!buf) {
10886                 EM_DEBUG_LOG(">>> Memory cannot be allocated ");
10887                 goto FINISH_OFF;
10888         }
10889
10890         if (!(fp_src = fopen(file_path, "rb"))) {
10891                 EM_DEBUG_EXCEPTION(" file_path fopen failed - %s", file_path);
10892
10893                         error = EMAIL_ERROR_SYSTEM_FAILURE;
10894                         goto FINISH_OFF;
10895                 }
10896
10897                 if ((nread = fread(buf, 1, buf_size, fp_src)) > 0) {
10898                         if (nread > 0 && nread <= buf_size)  {
10899                                 EM_DEBUG_LOG(">>>> Nread Value [ %d ] ", nread);
10900
10901                                 /**
10902                                   *   1.Add check for whether content type is there.
10903                                   *   2. If not based on the character set, Append it in File
10904                                   **/
10905
10906                                 low_char_set = calloc(1, EM_SAFE_STRLEN(char_set) + strlen(" \" /></head>") +1); /*prevent 34359*/
10907
10908                                 strncat(low_char_set, char_set, EM_SAFE_STRLEN(char_set));
10909
10910                                 EM_DEBUG_LOG(">>>> CHAR SET [ %s ] ", low_char_set);
10911
10912                                 strncat(low_char_set, " \" /></head>", strlen(" \" /></head>")); /*prevent 34359*/
10913
10914                                 EM_DEBUG_LOG(">>> CHARSET [ %s ] ", low_char_set);
10915
10916                                 EM_DEBUG_LOG(">>>>emstorage_add_content_type 1 ");
10917
10918                                 match_str = strstr(buf, CONTENT_TYPE_DATA);
10919                                 EM_DEBUG_LOG(">>>>emstorage_add_content_type 2 ");
10920
10921                                 if (match_str == NULL) {
10922                                         EM_DEBUG_LOG(">>>>emstorage_add_content_type 3 ");
10923                                         if (fp_src !=NULL) {
10924                                                 fclose(fp_src);fp_src = NULL;
10925                                         }
10926                                 data_count_to_written = EM_SAFE_STRLEN(low_char_set)+strlen(CONTENT_DATA)+1; /*prevent 34359*/
10927                                         EM_DEBUG_LOG(">>>>emstorage_add_content_type 4 ");
10928                                 buf1 = (char *)calloc(1, data_count_to_written);
10929                                         EM_DEBUG_LOG(">>>>emstorage_add_content_type 5 ");
10930
10931                                         if (buf1) {
10932                                                 EM_DEBUG_LOG(">>>>emstorage_add_content_type 6 ");
10933                                                 strncat(buf1, CONTENT_DATA, strlen(CONTENT_DATA)); /*prevent 34359*/
10934
10935                                                 EM_DEBUG_LOG(">>>>> BUF 1 [ %s ] ", buf1);
10936
10937                                                 strncat(buf1, low_char_set, EM_SAFE_STRLEN(low_char_set));
10938
10939                                                 EM_DEBUG_LOG(">>>> HTML TAG DATA  [ %s ] ", buf1);
10940
10941
10942                                         /* 1. Create a temporary file name */
10943                                         if (!_get_temp_file_name(&temp_file_name, &err)) {
10944                                                         EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed - %d", err);
10945                                                         if (err_code != NULL) *err_code = err;
10946                                                         EM_SAFE_FREE(temp_file_name);
10947                                                         goto FINISH_OFF;
10948                                         }
10949                                         EM_DEBUG_LOG(">>>>>>> TEMP APPEND FILE PATH [ %s ] ", temp_file_name);
10950
10951                                         /* Open the Temp file in Append mode */
10952                                         if (!(fp_dest = fopen(temp_file_name, "ab"))) {
10953                                                 EM_DEBUG_EXCEPTION(" fopen failed - %s", temp_file_name);
10954                                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10955                                                 goto FINISH_OFF;
10956                                         }
10957
10958                                         /* 2. write the Latest data */
10959                                         nwritten = fwrite(buf1, data_count_to_written-1, 1, fp_dest);
10960
10961                                         if (nwritten > 0) {
10962                                                 EM_DEBUG_LOG(" Latest Data  : [%d ] bytes written ", nwritten);
10963                                                 nwritten = 0;
10964                                                 /* 3. Append old data */
10965                                                 nwritten = fwrite(buf, nread-1, 1, fp_dest);
10966
10967                                                 if (nwritten <= 0) {
10968                                                         EM_DEBUG_EXCEPTION(" Error Occured while writing Old data : [%d ] bytes written ", nwritten);
10969                                                         error = EMAIL_ERROR_SYSTEM_FAILURE;
10970                                                         goto FINISH_OFF;
10971                                                 }
10972                                                 else {
10973                                                         EM_DEBUG_LOG(">>>> OLD data appended [ %d ] ", nwritten);
10974
10975                                                         if (!emstorage_move_file(temp_file_name, file_path, false, &err)) {
10976                                                                 EM_DEBUG_EXCEPTION(" emstorage_move_file failed - %d", err);
10977                                                                 goto FINISH_OFF;
10978                                                         }
10979                                                 }
10980
10981                                         }
10982                                         else {
10983                                                 EM_DEBUG_EXCEPTION(" Error Occured while writing New data : [%d ] bytes written ", nwritten);
10984                                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10985                                                 goto FINISH_OFF;
10986                                         }
10987
10988                                         }
10989
10990                                 }
10991                                 EM_DEBUG_LOG(">>>>emstorage_add_content_type 15 ");
10992
10993
10994                         }
10995                 }
10996
10997         ret = true;
10998 FINISH_OFF:
10999
11000         EM_SAFE_FREE(buf);
11001         EM_SAFE_FREE(buf1);
11002         EM_SAFE_FREE(low_char_set);
11003
11004         if (fp_src != NULL) {
11005                 fclose(fp_src);
11006                 fp_src = NULL;
11007         }
11008
11009         if (fp_dest != NULL) {
11010                 fclose(fp_dest);
11011                 fp_dest = NULL;
11012         }
11013         EM_DEBUG_FUNC_END("ret [%d]", ret);
11014         return ret;
11015
11016 }
11017
11018 INTERNAL_FUNC int emstorage_move_file(char *src_file, char *dst_file, int sync_status, int *err_code)
11019 {
11020         EM_DEBUG_FUNC_BEGIN("src_file[%p], dst_file[%p], err_code[%p]", src_file, dst_file, err_code);
11021
11022         int ret = false;
11023         int error = EMAIL_ERROR_NONE;
11024
11025         if (src_file == NULL || dst_file == NULL)  {
11026                 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
11027                 error = EMAIL_ERROR_INVALID_PARAM;
11028                 goto FINISH_OFF;
11029         }
11030
11031         EM_DEBUG_LOG("src_file[%s], dst_file[%s]", src_file, dst_file);
11032
11033         if (strcmp(src_file, dst_file) != 0) {
11034                 if (rename(src_file, dst_file) != 0) {
11035                         if (errno == EXDEV)  {  /* oldpath and newpath are not on the same mounted file system.  (Linux permits a file system to be mounted at multiple points,  but  rename() */
11036                                 /*  does not work across different mount points, even if the same file system is mounted on both.)       */
11037                                 EM_DEBUG_LOG("oldpath and newpath are not on the same mounted file system.");
11038                                 if (!emstorage_copy_file(src_file, dst_file, sync_status, &error)) {
11039                                         EM_DEBUG_EXCEPTION("emstorage_copy_file failed - %d", error);
11040                                         goto FINISH_OFF;
11041                                 }
11042                                 remove(src_file);
11043                                 EM_DEBUG_LOG("src[%s] removed", src_file);
11044
11045                         }
11046                         else  {
11047                                 if (errno == ENOENT)  {
11048                                         struct stat temp_file_stat;
11049                                         if (stat(src_file, &temp_file_stat) < 0)
11050                                                 EM_DEBUG_EXCEPTION("no src file found [%s]", src_file);
11051                                         if (stat(dst_file, &temp_file_stat) < 0)
11052                                                 EM_DEBUG_EXCEPTION("no dst file found [%s]", src_file);
11053
11054                                         EM_DEBUG_EXCEPTION("no file found [%d]", errno);
11055                                         error = EMAIL_ERROR_FILE_NOT_FOUND;
11056                                         goto FINISH_OFF;
11057
11058                                 }
11059                                 else  {
11060                                         EM_DEBUG_EXCEPTION("rename failed [%d]", errno);
11061                                         error = EMAIL_ERROR_SYSTEM_FAILURE;
11062                                         goto FINISH_OFF;
11063                                 }
11064                         }
11065                 }
11066         }
11067         else {
11068                 EM_DEBUG_LOG("src[%s] = dst[%d]", src_file, dst_file);
11069         }
11070
11071         ret = true;
11072
11073 FINISH_OFF:
11074         if (err_code != NULL)
11075                 *err_code = error;
11076
11077         EM_DEBUG_FUNC_END("ret [%d]", ret);
11078         return ret;
11079 }
11080
11081 INTERNAL_FUNC int emstorage_delete_file(char *src_file, int *err_code)
11082 {
11083         EM_DEBUG_FUNC_BEGIN("src_file[%p], err_code[%p]", src_file, err_code);
11084
11085         int ret = false;
11086         int error = EMAIL_ERROR_NONE;
11087
11088         if (src_file == NULL) {
11089                 EM_DEBUG_EXCEPTION(" src_file[%p]", src_file);
11090
11091                 error = EMAIL_ERROR_INVALID_PARAM;
11092                 goto FINISH_OFF;
11093         }
11094
11095         if (remove(src_file) != 0) {
11096                 if (errno != ENOENT) {
11097                         EM_DEBUG_EXCEPTION(" remove failed - %d", errno);
11098
11099                         error = EMAIL_ERROR_SYSTEM_FAILURE;
11100                         goto FINISH_OFF;
11101                 }
11102                 else {
11103                         EM_DEBUG_EXCEPTION(" no file found...");
11104
11105                         error = EMAIL_ERROR_FILE_NOT_FOUND;
11106                 }
11107         }
11108
11109         ret = true;
11110
11111 FINISH_OFF:
11112         if (err_code != NULL)
11113                 *err_code = error;
11114
11115         EM_DEBUG_FUNC_END("ret [%d]", ret);
11116         return ret;
11117 }
11118
11119
11120 INTERNAL_FUNC int emstorage_delete_dir(char *src_dir, int *err_code)
11121 {
11122         EM_DEBUG_FUNC_BEGIN("src_dir[%p], err_code[%p]", src_dir, err_code);
11123
11124         if (src_dir == NULL) {
11125                 EM_DEBUG_EXCEPTION("src_dir[%p]", src_dir);
11126
11127                 if (err_code != NULL)
11128                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11129                 return false;
11130         }
11131
11132         int error = EMAIL_ERROR_NONE;
11133
11134         DIR *dirp;
11135         struct dirent *dp;
11136         struct stat sbuf;
11137         char buf[512];
11138
11139         dirp = opendir(src_dir);
11140
11141         if (dirp == NULL)  {
11142                 if (errno == ENOENT)  {
11143                         EM_DEBUG_EXCEPTION("directory[%s] does not exist...", src_dir);
11144                         if (err_code != NULL)
11145                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
11146                         return true;
11147                 }
11148                 else  {
11149                         EM_DEBUG_EXCEPTION("opendir failed - %d", errno);
11150                         if (err_code != NULL)
11151                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
11152                         return false;
11153                 }
11154         }
11155
11156         while ((dp=readdir(dirp)))  {
11157                 if (strncmp(dp->d_name, ".", 1) == 0 || strncmp(dp->d_name, "..", 2) == 0) /* prevent 34360 */
11158                         continue;
11159
11160                 SNPRINTF(buf, sizeof(buf), "%s/%s", src_dir, dp->d_name);
11161
11162                 if (lstat(buf, &sbuf) == 0 || stat(buf, &sbuf) == 0) {
11163                         /*  check directory */
11164                         if ((sbuf.st_mode & S_IFMT) == S_IFDIR)  {      /*  directory */
11165                                 /*  recursive call */
11166                                 if (!emstorage_delete_dir(buf, &error)) {
11167                                         closedir(dirp);
11168                                         if (err_code != NULL)
11169                                                 *err_code = error;
11170                                         return false;
11171                                 }
11172                         }
11173                         else  { /*  file */
11174                                 if (remove(buf) < 0)  {
11175                                         EM_DEBUG_EXCEPTION("remove failed - %s", buf);
11176                                         closedir(dirp);
11177                                         if (err_code != NULL)
11178                                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
11179                                         return false;
11180                                 }
11181                         }
11182                 }
11183                 else
11184                         EM_DEBUG_EXCEPTION("content does not exist...");
11185         }
11186
11187         closedir(dirp);
11188
11189         EM_DEBUG_LOG("remove direcotory [%s]", src_dir);
11190
11191         /* EM_DEBUG_FUNC_BEGIN(); */
11192
11193         if (remove(src_dir) < 0)  {
11194                 EM_DEBUG_EXCEPTION("remove failed [%s]", src_dir);
11195                 if (err_code != NULL)
11196                         *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
11197                 return false;
11198         }
11199
11200         if (err_code != NULL)
11201                 *err_code = error;
11202
11203         return true;
11204 }
11205
11206 /* faizan.h@samsung.com */
11207 INTERNAL_FUNC int emstorage_update_server_uid(char *old_server_uid, char *new_server_uid, int *err_code)
11208 {
11209         EM_DEBUG_FUNC_BEGIN("new_server_uid[%s], old_server_uid[%s]", new_server_uid, old_server_uid);
11210         int rc, ret = false;
11211         int error = EMAIL_ERROR_NONE;
11212         char sql_query_string[QUERY_SIZE] = {0, };
11213
11214         int transaction = true;
11215
11216         if (!old_server_uid || !new_server_uid) {
11217                 EM_DEBUG_EXCEPTION("Invalid parameters");
11218                 error = EMAIL_ERROR_INVALID_PARAM;
11219                 return false;
11220         }
11221         sqlite3 *local_db_handle = emstorage_get_db_connection();
11222
11223         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
11224
11225         SNPRINTF(sql_query_string, sizeof(sql_query_string),
11226                  "UPDATE mail_tbl SET server_mail_id=\'%s\' WHERE server_mail_id=%s ", new_server_uid, old_server_uid);
11227         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
11228
11229         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
11230                 ("sqlite3_exec fail:%d", rc));
11231         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11232                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11233
11234         ret = true;
11235
11236 FINISH_OFF:
11237         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
11238         _DISCONNECT_DB;
11239
11240         if (err_code != NULL)
11241                 *err_code = error;
11242
11243         EM_DEBUG_FUNC_END("ret [%d]", ret);
11244         return ret;
11245
11246 }
11247
11248 INTERNAL_FUNC int emstorage_update_read_mail_uid(int mail_id, char *new_server_uid, char *mbox_name, int *err_code)
11249 {
11250         EM_DEBUG_FUNC_BEGIN("mail_id[%d], new_server_uid[%s], mbox_name[%s]", mail_id, new_server_uid, mbox_name);
11251
11252         int rc, ret = false;
11253         int error = EMAIL_ERROR_NONE;
11254         char sql_query_string[QUERY_SIZE] = {0, };
11255
11256         int transaction = true;
11257
11258         if (!mail_id || !new_server_uid || !mbox_name)  {
11259                 EM_DEBUG_EXCEPTION("Invalid parameters");
11260                 error = EMAIL_ERROR_INVALID_PARAM;
11261                 return false;
11262         }
11263         sqlite3 *local_db_handle = emstorage_get_db_connection();
11264         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
11265
11266
11267         SNPRINTF(sql_query_string, sizeof(sql_query_string),
11268                  "UPDATE mail_read_mail_uid_tbl SET s_uid=\'%s\', mailbox_id=\'%s\', mailbox_name=\'%s\' WHERE local_uid=%d ", new_server_uid, mbox_name, mbox_name, mail_id);
11269         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
11270         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11271                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11272         ret     = true;
11273
11274 FINISH_OFF:
11275
11276         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
11277         _DISCONNECT_DB;
11278
11279         if (err_code != NULL)
11280                 *err_code = error;
11281
11282         EM_DEBUG_FUNC_END("ret [%d]", ret);
11283         return ret;
11284
11285 }
11286
11287
11288 int emstorage_get_latest_unread_mailid(int account_id, int *mail_id, int *err_code)
11289 {
11290         EM_DEBUG_FUNC_BEGIN();
11291
11292         if ((!mail_id) ||(account_id <= 0 &&  account_id != -1)) {
11293                 EM_DEBUG_EXCEPTION(" mail_id[%p], account_id[%d] ", mail_id, account_id);
11294                 if (err_code != NULL)
11295                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11296                 return false;
11297         }
11298
11299         int ret = false;
11300         int rc = -1;
11301         int error = EMAIL_ERROR_NONE;
11302         DB_STMT hStmt = NULL;
11303         int count = 0;
11304         int mailid = 0;
11305         int transaction = false;
11306         char sql_query_string[QUERY_SIZE] = {0, };
11307
11308         sqlite3 *local_db_handle = emstorage_get_db_connection();
11309         EMSTORAGE_START_READ_TRANSACTION(transaction);
11310
11311         memset(sql_query_string, 0x00, sizeof(sql_query_string));
11312
11313         if (account_id == -1)
11314                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl WHERE flags_seen_field = 0 ORDER BY mail_id DESC");
11315         else
11316                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl WHERE account_id = %d AND flags_seen_field = 0 ORDER BY mail_id DESC", account_id);
11317
11318         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11319         EM_DEBUG_LOG("  sqlite3_prepare hStmt = %p", hStmt);
11320         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11321                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11322
11323         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11324         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11325                 ("sqlite3_step fail:%d", rc));
11326
11327         char **result;
11328
11329         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
11330         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11331                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11332
11333         sqlite3_free_table(result);
11334         if (count == 0)  {
11335                 EM_DEBUG_EXCEPTION("no Mails found...");
11336                 ret = false;
11337                 error= EMAIL_ERROR_MAIL_NOT_FOUND;
11338                 goto FINISH_OFF;
11339         }
11340
11341         _get_stmt_field_data_int(hStmt, &mailid, 0);
11342         EM_DEBUG_LOG("mailid [%d]", mailid);
11343
11344         ret = true;
11345
11346 FINISH_OFF:
11347         if (hStmt != NULL)  {
11348                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
11349
11350                 rc = sqlite3_finalize(hStmt);
11351                 if (rc != SQLITE_OK)  {
11352                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
11353
11354                         error = EMAIL_ERROR_DB_FAILURE;
11355                 }
11356         }
11357         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
11358         _DISCONNECT_DB;
11359
11360         if (mail_id != NULL)
11361                 *mail_id = mailid;
11362
11363         if (err_code != NULL)
11364                 *err_code = error;
11365
11366         EM_DEBUG_FUNC_END("ret [%d]", ret);
11367         return ret;
11368 }
11369
11370 int setting_system_command(const char *command)
11371
11372  {
11373         int pid = 0, status = 0;
11374         char *const environ[] = { NULL };
11375
11376         if (command == 0)
11377                 return 1;
11378
11379         pid = fork();
11380
11381         if (pid == -1)
11382                 return -1;
11383
11384         if (pid == 0) {
11385                 char *argv[4];
11386
11387                 argv[0] = "sh";
11388                 argv[1] = "-c";
11389                 argv[2] = (char *)command;
11390                 argv[3] = 0;
11391
11392                 execve("/bin/sh", argv, environ);
11393                 abort();
11394         }
11395         do{
11396                 if (waitpid(pid, &status, 0) == -1) {
11397                         if (errno != EINTR)
11398                                 return -1;
11399                 }
11400                 else {
11401                         return status;
11402                 }
11403         } while (1);
11404
11405         return 0;
11406 }
11407
11408
11409 INTERNAL_FUNC int emstorage_mail_get_total_diskspace_usage(unsigned long *total_usage,  int transaction, int *err_code)
11410 {
11411         EM_DEBUG_FUNC_BEGIN("total_usage[%p],  transaction[%d], err_code[%p]", total_usage, transaction, err_code);
11412
11413         if (!total_usage) {
11414                 EM_DEBUG_EXCEPTION("total_usage[%p]", total_usage);
11415
11416                 if (err_code != NULL)
11417                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11418                 return false;
11419         }
11420
11421         int   ret = false;
11422         int   error = EMAIL_ERROR_NONE;
11423         char  syscmd[256] = {0, };
11424         char  line[256] = {0, };
11425         char *line_from_file = NULL;
11426         FILE *fp = NULL;
11427         unsigned long total_diskusage = 0;
11428
11429         SNPRINTF(syscmd, sizeof(syscmd), "touch %s", SETTING_MEMORY_TEMP_FILE_PATH);
11430         if (setting_system_command(syscmd) == -1) {
11431                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
11432
11433                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11434                 goto FINISH_OFF;
11435         }
11436
11437         SNPRINTF(syscmd, sizeof(syscmd), "du -hsk %s > %s", EMAILPATH, SETTING_MEMORY_TEMP_FILE_PATH);
11438         EM_DEBUG_LOG(" cmd : %s", syscmd);
11439         if (setting_system_command(syscmd) == -1) {
11440                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : Setting > Memory] System Command [%s] is failed", syscmd);
11441
11442                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11443                 goto FINISH_OFF;
11444         }
11445
11446         fp = fopen(SETTING_MEMORY_TEMP_FILE_PATH, "r");
11447         if (fp == NULL) {
11448                 perror(SETTING_MEMORY_TEMP_FILE_PATH);
11449
11450                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11451                 goto FINISH_OFF;
11452         }
11453
11454         line_from_file = fgets(line, sizeof(line), fp);
11455
11456         if(line_from_file == NULL) {
11457                 EM_DEBUG_EXCEPTION("fgets failed");
11458                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11459                 goto FINISH_OFF;
11460         }
11461         total_diskusage = strtoul(line, NULL, 10);
11462
11463         memset(syscmd, 0, sizeof(syscmd));
11464         SNPRINTF(syscmd, sizeof(syscmd), "rm -f %s", SETTING_MEMORY_TEMP_FILE_PATH);
11465         if (setting_system_command(syscmd) == -1) {
11466                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage :  [Setting > Memory] System Command [%s] is failed", syscmd);
11467                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11468                 goto FINISH_OFF;
11469         }
11470
11471         EM_DEBUG_LOG("[Setting > Memory] @@@@@ Size of Directory [%s] is %ld KB", EMAILPATH, total_diskusage);
11472
11473         ret = true;
11474
11475 FINISH_OFF:
11476         if (err_code != NULL)
11477                 *err_code = error;
11478
11479         if (ret)
11480                 *total_usage = total_diskusage;
11481         else
11482                 *total_usage = 0;
11483
11484         if(fp) fclose(fp); /* prevent 32730 */
11485
11486         EM_DEBUG_FUNC_END("ret [%d]", ret);
11487         return ret;
11488 }
11489
11490
11491 INTERNAL_FUNC int emstorage_test(int mail_id, int account_id, char *full_address_to, char *full_address_cc, char *full_address_bcc, int *err_code)
11492 {
11493         DB_STMT hStmt = NULL;
11494         int ret = false;
11495         int error = EMAIL_ERROR_NONE;
11496         int rc = 0;
11497         char sql_query_string[QUERY_SIZE] = {0, };
11498
11499         SNPRINTF(sql_query_string, sizeof(sql_query_string),
11500                 "INSERT INTO mail_tbl VALUES "
11501                 "( ?" /*  mail_id */
11502                 ", ?" /*  account_id */
11503                 ", ?" /*  mail_size */
11504                 ", ?" /*  server_mail_status */
11505                 ", ?" /*  server_mailbox_name */
11506                 ", ?" /*  server_mail_id */
11507                 ", ?" /*  reference_mail_id */
11508                 ", ?" /*  full_address_from */
11509                 ", ?" /*  full_address_reply */
11510                 ", ?" /*  full_address_to */
11511                 ", ?" /*  full_address_cc */
11512                 ", ?" /*  full_address_bcc */
11513                 ", ?" /*  full_address_return */
11514                 ", ?" /*  subject */
11515                 ", ?" /*  body_download_status */
11516                 ", ?" /*  file_path_plain */
11517                 ", ?" /*  file_path_html */
11518                 ", ?" /*  date_time */
11519                 ", ?" /*  flags_seen_field */
11520                 ", ?" /*  flags_deleted_field */
11521                 ", ?" /*  flags_flagged_field */
11522                 ", ?" /*  flags_answered_field */
11523                 ", ?" /*  flags_recent_field */
11524                 ", ?" /*  flags_draft_field */
11525                 ", ?" /*  flags_forwarded_field */
11526                 ", ?" /*  DRM_status */
11527                 ", ?" /*  priority */
11528                 ", ?" /*  save_status */
11529                 ", ?" /*  lock_status */
11530                 ", ?" /*  message_id */
11531                 ", ?" /*  report_status */
11532                 ", ?" /*  email_address_sender */
11533                 ", ?" /*  email_address_recipient */
11534                 ", ?" /*  attachment_count */
11535                 ", ?" /*  inline_content_count */
11536                 ", ?" /*  preview_text */
11537                 ", ?" /*  thread_id */
11538                 ", ?" /*  mailbox_type */
11539                 ", ?" /*  alias_sender */
11540                 ", ?" /*  alias_recipient */
11541                 ", ?" /*  thread_item_count */
11542                 ", ?" /*  meeting_request_status */
11543                 ", ?" /*  message_class */
11544                 ", ?" /*  digest_type */
11545                 ", ?" /*  smime_type */
11546                 ", ?" /*  scheduled_sending_time */
11547                 ", ?" /*  eas_data_length */
11548                 ", ?" /*  eas_data */
11549                 ")");
11550
11551         int transaction = true;
11552         sqlite3 *local_db_handle = emstorage_get_db_connection();
11553
11554         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
11555
11556
11557         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11558         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11559                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11560
11561         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_id);
11562         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, account_id);
11563         _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, EMAIL_MAILBOX_TYPE_OUTBOX);
11564         _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, "save test - long", 1, SUBJECT_LEN_IN_MAIL_TBL);
11565         _bind_stmt_field_data_string(hStmt, DATETIME_IDX_IN_MAIL_TBL, "20100316052908", 0, DATETIME_LEN_IN_MAIL_TBL);
11566         _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, 0);
11567         _bind_stmt_field_data_string(hStmt, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL, "", 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
11568         _bind_stmt_field_data_string(hStmt, SERVER_MAIL_ID_IDX_IN_MAIL_TBL, "", 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
11569         _bind_stmt_field_data_string(hStmt, MESSAGE_ID_IDX_IN_MAIL_TBL, "", 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
11570         _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, 0);
11571         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL, "<test08@streaming.s3glab.net>", 1, FROM_LEN_IN_MAIL_TBL);
11572         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL, "", 1, REPLY_TO_LEN_IN_MAIL_TBL);
11573         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL, full_address_to, 1, TO_LEN_IN_MAIL_TBL);
11574         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL, full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
11575         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL, full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
11576         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL, "", 1, RETURN_PATH_LEN_IN_MAIL_TBL);
11577         _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);
11578         _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);
11579         _bind_stmt_field_data_string(hStmt, ALIAS_SENDER_IDX_IN_MAIL_TBL, "send_alias", 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
11580         _bind_stmt_field_data_string(hStmt, ALIAS_RECIPIENT_IDX_IN_MAIL_TBL, "recipient_alias", 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
11581         _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, 1);
11582         _bind_stmt_field_data_string(hStmt, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL, MAILHOME"/7/348/UTF-8", 0, TEXT_1_LEN_IN_MAIL_TBL);
11583         _bind_stmt_field_data_string(hStmt, FILE_PATH_HTML_IDX_IN_MAIL_TBL, "", 0, TEXT_2_LEN_IN_MAIL_TBL);
11584         _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, 4);
11585         _bind_stmt_field_data_char(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, 0);
11586         _bind_stmt_field_data_char(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, 0);
11587         _bind_stmt_field_data_char(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, 0);
11588         _bind_stmt_field_data_char(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, 0);
11589         _bind_stmt_field_data_char(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, 0);
11590         _bind_stmt_field_data_char(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, 0);
11591         _bind_stmt_field_data_char(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, 0);
11592         _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, 0);
11593         _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, 0);
11594         _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, 0);
11595         _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, 0);
11596         _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, 0);
11597         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
11598         _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, 0);
11599         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
11600         _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, 0);
11601         _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, 0);
11602         _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, "preview body", 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
11603         _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, 0);
11604         _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, 0);
11605         _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, 0);
11606         _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, 0);
11607         _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, 0);
11608         _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, NULL, 0);
11609
11610         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11611         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
11612                 ("sqlite3_step fail:%d", rc));
11613         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11614                 ("sqlite3_step fail:%d", rc));
11615         ret = true;
11616
11617 FINISH_OFF:
11618         if (hStmt != NULL)  {
11619                 rc = sqlite3_finalize(hStmt);
11620                 if (rc != SQLITE_OK)  {
11621                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
11622                         error = EMAIL_ERROR_DB_FAILURE;
11623                 }
11624         }
11625
11626         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
11627         _DISCONNECT_DB;
11628
11629         if (err_code != NULL)
11630                 *err_code = error;
11631
11632         EM_DEBUG_FUNC_END("ret [%d]", ret);
11633         return ret;
11634 }
11635
11636
11637 INTERNAL_FUNC int emstorage_get_max_mail_count()
11638 {
11639         return EMAIL_MAIL_MAX_COUNT;
11640 }
11641
11642 #define STRIPPED_SUBJECT_BUFFER_SIZE 4086
11643
11644 INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(emstorage_mail_tbl_t *mail_tbl, int *thread_id, int *result_latest_mail_id_in_thread, int *thread_item_count)
11645 {
11646         EM_DEBUG_FUNC_BEGIN("mail_tbl [%p], thread_id [%p], result_latest_mail_id_in_thread [%p], thread_item_count [%p]", mail_tbl, thread_id, result_latest_mail_id_in_thread, thread_item_count);
11647         EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
11648         int      rc = 0, query_size = 0, query_size_account = 0;
11649         int      account_id = 0;
11650         int      err_code = EMAIL_ERROR_NONE;
11651         int      count = 0, result_thread_id = -1, latest_mail_id_in_thread = -1;
11652         time_t   latest_date_time = 0;
11653         time_t   date_time = 0;
11654         char    *subject = NULL;
11655         char    *sql_query_string = NULL, *sql_account = NULL;
11656         char    *sql_format = "SELECT thread_id, date_time, mail_id FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
11657         char    *sql_format_account = " AND account_id = %d ";
11658         char    *sql_format_order_by = " ORDER BY date_time DESC ";
11659         char   **result = NULL;
11660         char     stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
11661         sqlite3 *local_db_handle = emstorage_get_db_connection();
11662
11663         EM_IF_NULL_RETURN_VALUE(mail_tbl, EMAIL_ERROR_INVALID_PARAM);
11664         EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
11665         EM_IF_NULL_RETURN_VALUE(result_latest_mail_id_in_thread, EMAIL_ERROR_INVALID_PARAM);
11666         EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
11667
11668         account_id   = mail_tbl->account_id;
11669         subject      = mail_tbl->subject;
11670         date_time    = mail_tbl->date_time;
11671
11672         EM_DEBUG_LOG("subject : %s", subject);
11673
11674         if (em_find_pos_stripped_subject_for_thread_view(subject, stripped_subject, STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE)  {
11675                 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view  is failed");
11676                 err_code =  EMAIL_ERROR_UNKNOWN;
11677                 result_thread_id = -1;
11678                 goto FINISH_OFF;
11679         }
11680
11681         if (EM_SAFE_STRLEN(stripped_subject) < 2) {
11682                 result_thread_id = -1;
11683                 goto FINISH_OFF;
11684         }
11685
11686         EM_DEBUG_LOG("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s", EM_SAFE_STRLEN(stripped_subject), stripped_subject);
11687
11688         if (account_id > 0)     {
11689                 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
11690                 sql_account = malloc(query_size_account);
11691                 if (sql_account == NULL) {
11692                         EM_DEBUG_EXCEPTION("malloc for sql_account  is failed %d", query_size_account);
11693                         err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
11694                         goto FINISH_OFF;
11695                 }
11696                 snprintf(sql_account, query_size_account, sql_format_account, account_id);
11697         }
11698
11699         /* prevent 34362 */
11700         query_size = strlen(sql_format) + strlen(stripped_subject)*2 + 50 + query_size_account + strlen(sql_format_order_by); /*  + query_size_mailbox; */
11701         sql_query_string = malloc(query_size);
11702
11703         if (sql_query_string == NULL) {
11704                 EM_DEBUG_EXCEPTION("malloc for sql  is failed %d", query_size);
11705                 err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
11706                 goto FINISH_OFF;
11707         }
11708
11709         sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mail_tbl->mailbox_id);
11710
11711         if (account_id > 0)
11712                 strcat(sql_query_string, sql_account);
11713
11714         strcat(sql_query_string, sql_format_order_by);
11715         strcat(sql_query_string, ";");
11716
11717         EM_DEBUG_LOG("Query : %s", sql_query_string);
11718
11719         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
11720
11721         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11722                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11723
11724         EM_DEBUG_LOG("Result rows count : %d", count);
11725
11726         if (count == 0)
11727                 result_thread_id = -1;
11728         else {
11729                 _get_table_field_data_int   (result, &result_thread_id, 3);
11730                 _get_table_field_data_time_t(result, &latest_date_time, 4);
11731                 _get_table_field_data_int   (result, &latest_mail_id_in_thread, 5);
11732
11733                 if (latest_date_time < mail_tbl->date_time)
11734                         *result_latest_mail_id_in_thread = latest_mail_id_in_thread;
11735                 else
11736                         *result_latest_mail_id_in_thread = mail_tbl->mail_id;
11737                 EM_DEBUG_LOG("latest_mail_id_in_thread [%d], mail_id [%d]", latest_mail_id_in_thread, mail_tbl->mail_id);
11738         }
11739
11740 FINISH_OFF:
11741         *thread_id = result_thread_id;
11742         *thread_item_count = count;
11743
11744         EM_DEBUG_LOG("Result thread id : %d", *thread_id);
11745         EM_DEBUG_LOG("Result count : %d", *thread_item_count);
11746         EM_DEBUG_LOG("err_code : %d", err_code);
11747
11748         EM_SAFE_FREE(sql_account);
11749         EM_SAFE_FREE(sql_query_string);
11750
11751         sqlite3_free_table(result);
11752
11753         EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
11754
11755         return err_code;
11756 }
11757
11758
11759 INTERNAL_FUNC int emstorage_get_thread_information(int thread_id, emstorage_mail_tbl_t** mail_tbl, int transaction, int *err_code)
11760 {
11761         EM_DEBUG_FUNC_BEGIN();
11762
11763         int count = 0, ret = false;
11764         int error = EMAIL_ERROR_NONE;
11765         emstorage_mail_tbl_t *p_data_tbl = NULL;
11766         char conditional_clause[QUERY_SIZE] = {0, };
11767
11768         EM_IF_NULL_RETURN_VALUE(mail_tbl, false);
11769
11770         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE thread_id = %d AND thread_item_count > 0", thread_id);
11771         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
11772
11773         if(!emstorage_query_mail_tbl(conditional_clause, transaction, &p_data_tbl, &count, &error)) {
11774                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
11775                 goto FINISH_OFF;
11776         }
11777
11778         if(p_data_tbl)
11779                 EM_DEBUG_LOG("thread_id : %d, thread_item_count : %d", p_data_tbl[0].thread_id, p_data_tbl[0].thread_item_count);
11780
11781         ret = true;
11782
11783 FINISH_OFF:
11784         if (ret == true)
11785                 *mail_tbl = p_data_tbl;
11786         else if (p_data_tbl != NULL)
11787                 emstorage_free_mail(&p_data_tbl, 1, NULL);
11788
11789         if (err_code != NULL)
11790                 *err_code = error;
11791
11792         EM_DEBUG_FUNC_END("ret [%d]", ret);
11793         return ret;
11794 }
11795
11796
11797 INTERNAL_FUNC int emstorage_get_sender_list(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)
11798 {
11799         EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id [%d], search_type [%d], search_value [%p], sorting [%d], sender_list[%p], sender_count[%p] err_code[%p]"
11800                 , account_id , mailbox_id , search_type , search_value , sorting , sender_list, sender_count, err_code);
11801
11802         if ((!sender_list) ||(!sender_count)) {
11803                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
11804                 if (err_code != NULL)
11805                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11806                 return false;
11807         }
11808
11809         int rc = -1, ret = false;
11810         int error = EMAIL_ERROR_NONE;
11811         int count = 0;
11812         int i, col_index = 0;
11813         int read_count = 0;
11814         email_sender_list_t *p_sender_list = NULL;
11815         char sql_query_string[QUERY_SIZE] = {0, };
11816         char **result = NULL;
11817         sqlite3 *local_db_handle = emstorage_get_db_connection();
11818
11819         SNPRINTF(sql_query_string, sizeof(sql_query_string),
11820                 "SELECT email_address_sender, alias_sender, COUNT(email_address_sender), SUM(flags_seen_field = 1) "
11821                 "FROM mail_tbl ");
11822
11823         /*  mailbox_id */
11824         if (mailbox_id)
11825                 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);
11826         else    /*  NULL  means all mailbox_name. but except for trash(3), spambox(5), all emails(for GMail, 7) */
11827                 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) ");
11828
11829         /*  account id */
11830         /*  '0' (ALL_ACCOUNT) means all account */
11831         if (account_id > ALL_ACCOUNT)
11832                 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);
11833
11834         if (search_value) {
11835                 switch (search_type) {
11836                         case EMAIL_SEARCH_FILTER_SUBJECT:
11837                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
11838                                         " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\')) ", search_value);
11839                                 break;
11840                         case EMAIL_SEARCH_FILTER_SENDER:
11841                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
11842                                         " AND  ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
11843                                         ") ", search_value);
11844                                 break;
11845                         case EMAIL_SEARCH_FILTER_RECIPIENT:
11846                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
11847                                         " AND ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
11848                                         "       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
11849                                         "       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
11850                                         ") ", search_value, search_value, search_value);
11851                                 break;
11852                         case EMAIL_SEARCH_FILTER_ALL:
11853                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
11854                                         " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') "
11855                                         "       OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
11856                                         "                       OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
11857                                         "                       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
11858                                         "                       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
11859                                         "               ) "
11860                                         "       )"
11861                                         ")", search_value, search_value, search_value, search_value, search_value);
11862                                 break;
11863                 }
11864         }
11865
11866
11867         /*  sorting option is not available now. The order of sender list is ascending order by display name */
11868         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
11869                 "GROUP BY email_address_sender "
11870                 "ORDER BY UPPER(alias_sender) ");
11871
11872         EM_DEBUG_LOG("query[%s]", sql_query_string);
11873
11874         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
11875         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11876                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11877
11878         EM_DEBUG_LOG("Count of Sender [%d]", count);
11879
11880         if (!(p_sender_list = (email_sender_list_t*)em_malloc(sizeof(email_sender_list_t) * count))) {
11881                 EM_DEBUG_EXCEPTION("em_malloc failed...");
11882                 error = EMAIL_ERROR_OUT_OF_MEMORY;
11883                 goto FINISH_OFF;
11884         }
11885
11886         col_index = 4;
11887
11888         EM_DEBUG_LOG(">>>> DATA ASSIGN START >>");
11889         for (i = 0; i < count; i++)  {
11890                 _get_table_field_data_string(result, &(p_sender_list[i].address), 1, col_index++);
11891                 _get_table_field_data_string(result, &(p_sender_list[i].display_name), 1, col_index++);
11892                 _get_table_field_data_int(result, &(p_sender_list[i].total_count), col_index++);
11893                 _get_table_field_data_int(result, &(read_count), col_index++);
11894                 p_sender_list[i].unread_count = p_sender_list[i].total_count - read_count;              /*  unread count = total - read          */
11895         }
11896         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >>", count);
11897
11898         sqlite3_free_table(result);
11899         result = NULL;
11900
11901         ret = true;
11902
11903 FINISH_OFF:
11904         if (ret == true)  {
11905                 *sender_list = p_sender_list;
11906                 *sender_count = count;
11907                 EM_DEBUG_LOG(">>>> COUNT : %d >>", count);
11908         }
11909
11910         _DISCONNECT_DB;
11911
11912         if (err_code != NULL)
11913                 *err_code = error;
11914
11915         EM_DEBUG_FUNC_END("ret [%d]", ret);
11916         return ret;
11917 }
11918
11919 INTERNAL_FUNC int emstorage_free_sender_list(email_sender_list_t **sender_list, int count)
11920 {
11921         EM_DEBUG_FUNC_BEGIN("sender_list[%p], count[%d]", sender_list, count);
11922
11923         int err = EMAIL_ERROR_NONE;
11924
11925         if (count > 0)  {
11926                 if (!sender_list || !*sender_list)  {
11927                         EM_DEBUG_EXCEPTION("sender_list[%p], count[%d]", sender_list, count);
11928                         err = EMAIL_ERROR_INVALID_PARAM;
11929                         return err;
11930                 }
11931
11932                 email_sender_list_t* p = *sender_list;
11933                 int i = 0;
11934
11935                 for (; i < count; i++)  {
11936                         EM_SAFE_FREE(p[i].address);
11937                         EM_SAFE_FREE(p[i].display_name);
11938                 }
11939
11940                 EM_SAFE_FREE(p);
11941                 *sender_list = NULL;
11942         }
11943
11944         return err;
11945 }
11946
11947
11948 INTERNAL_FUNC int emstorage_free_address_info_list(email_address_info_list_t **address_info_list)
11949 {
11950         EM_DEBUG_FUNC_BEGIN("address_info_list[%p]", address_info_list);
11951
11952         int err = EMAIL_ERROR_NONE;
11953         email_address_info_t *p_address_info = NULL;
11954         GList *list = NULL;
11955         GList *node = NULL;
11956         int i = 0;
11957
11958         if (!address_info_list || !*address_info_list)  {
11959                 EM_DEBUG_EXCEPTION("address_info_list[%p]", address_info_list);
11960                 err = EMAIL_ERROR_INVALID_PARAM;
11961                 return err;
11962         }
11963
11964         /*  delete GLists */
11965         for (i = EMAIL_ADDRESS_TYPE_FROM; i <= EMAIL_ADDRESS_TYPE_BCC; i++) {
11966                 switch (i) {
11967                         case EMAIL_ADDRESS_TYPE_FROM:
11968                                 list = (*address_info_list)->from;
11969                                 break;
11970                         case EMAIL_ADDRESS_TYPE_TO:
11971                                 list = (*address_info_list)->to;
11972                                 break;
11973                         case EMAIL_ADDRESS_TYPE_CC:
11974                                 list = (*address_info_list)->cc;
11975                                 break;
11976                         case EMAIL_ADDRESS_TYPE_BCC:
11977                                 list = (*address_info_list)->bcc;
11978                                 break;
11979                 }
11980
11981                 /*  delete dynamic-allocated memory for each item */
11982                 node = g_list_first(list);
11983                 while (node != NULL) {
11984                         p_address_info = (email_address_info_t*)node->data;
11985                         EM_SAFE_FREE(p_address_info->address);
11986                         EM_SAFE_FREE(p_address_info->display_name);
11987                         EM_SAFE_FREE(node->data);
11988
11989                         node = g_list_next(node);
11990                 }
11991         }
11992
11993         EM_SAFE_FREE(*address_info_list);
11994         *address_info_list = NULL;
11995
11996         EM_DEBUG_FUNC_END("err [%d]", err);
11997         return err;
11998 }
11999 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
12000
12001 INTERNAL_FUNC int emstorage_add_pbd_activity(email_event_partial_body_thd* local_activity, int *activity_id, int transaction, int *err_code)
12002 {
12003         EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
12004
12005         if (!local_activity || !activity_id) {
12006                 EM_DEBUG_EXCEPTION("local_activity[%p], transaction[%d], activity_id[%p], err_code[%p]", local_activity, activity_id, transaction, err_code);
12007                 if (err_code != NULL)
12008                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12009                 return false;
12010         }
12011
12012         int rc = -1;
12013         int ret = false;
12014         int error = EMAIL_ERROR_NONE;
12015         int i = 0;
12016
12017         char sql_query_string[QUERY_SIZE] = {0, };
12018         DB_STMT hStmt = NULL;
12019         sqlite3 *local_db_handle = emstorage_get_db_connection();
12020         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12021         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12022         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12023                 "INSERT INTO mail_partial_body_activity_tbl VALUES "
12024                 "( "
12025                 "? "  /* Account ID */
12026                 ",?"  /* Local Mail ID */
12027                 ",?"  /* Server mail ID */
12028                 ",?"  /* Activity ID */
12029                 ",?"  /* Activity type*/
12030                 ",?"  /* Mailbox ID*/
12031                 ",?"  /* Mailbox name*/
12032                 ") ");
12033
12034         char *sql = "SELECT max(rowid) FROM mail_partial_body_activity_tbl;";
12035         char **result = NULL;
12036
12037
12038         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
12039
12040         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12041                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
12042
12043         if (NULL==result[1]) rc = 1;
12044         else rc = atoi(result[1])+1;
12045         sqlite3_free_table(result);
12046         result = NULL;
12047
12048         *activity_id = local_activity->activity_id = rc;
12049
12050         EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ], MAIL ID [ %d ], ACTIVITY TYPE [ %d ], SERVER MAIL ID [ %lu ]", \
12051                 local_activity->activity_id, local_activity->mail_id, local_activity->activity_type, local_activity->server_mail_id);
12052
12053         if (local_activity->mailbox_id)
12054                 EM_DEBUG_LOG(" MAILBOX ID [ %d ]", local_activity->mailbox_id);
12055
12056
12057         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12058         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12059                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12060
12061         EM_DEBUG_LOG(">>>> SQL STMT [ %s ]", sql_query_string);
12062
12063         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
12064         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
12065         _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
12066         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
12067         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
12068         _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
12069         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->mailbox_name, 0, 3999);
12070
12071
12072         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12073
12074         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12075                 ("sqlite3_step fail:%d", rc));
12076         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12077                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
12078
12079         ret = true;
12080
12081 FINISH_OFF:
12082
12083         if (hStmt != NULL) {
12084                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12085
12086                 rc = sqlite3_finalize(hStmt);
12087                 hStmt = NULL;
12088                 if (rc != SQLITE_OK) {
12089                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
12090                         error = EMAIL_ERROR_DB_FAILURE;
12091                 }
12092         }
12093
12094          EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12095         _DISCONNECT_DB;
12096          if (err_code != NULL)
12097                  *err_code = error;
12098
12099          EM_DEBUG_FUNC_END("ret [%d]", ret);
12100         return ret;
12101 }
12102
12103
12104 INTERNAL_FUNC int emstorage_get_pbd_mailbox_list(int account_id, int **mailbox_list, int *count, int transaction, int *err_code)
12105 {
12106         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
12107
12108         if (account_id < FIRST_ACCOUNT_ID || NULL == &mailbox_list || NULL == count) {
12109                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
12110                 if (err_code != NULL)
12111                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12112                 return false;
12113         }
12114
12115         int ret = false;
12116         int error = EMAIL_ERROR_NONE;
12117         char **result;
12118         int i = 0, rc = -1;
12119         int *mbox_list = NULL;
12120         DB_STMT hStmt = NULL;
12121         char sql_query_string[QUERY_SIZE] = {0, };
12122
12123         sqlite3 *local_db_handle = emstorage_get_db_connection();
12124
12125         EMSTORAGE_START_READ_TRANSACTION(transaction);
12126
12127         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12128         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);
12129
12130
12131         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
12132         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12133                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12134
12135         *count = atoi(result[1]);
12136         sqlite3_free_table(result);
12137
12138         if (!*count) {
12139                 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
12140                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
12141                 ret = true;
12142                 goto FINISH_OFF;
12143         }
12144         EM_DEBUG_LOG("Mailbox count = %d", *count);
12145
12146         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12147
12148         /* 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); */
12149         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);
12150
12151         EM_DEBUG_LOG(" Query [%s]", sql_query_string);
12152
12153
12154         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12155
12156         EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
12157         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12158                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12159
12160
12161         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12162         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12163                 ("sqlite3_step fail:%d", rc));
12164
12165         if (NULL == (mbox_list = (int *)em_malloc(sizeof(int *) * (*count)))) {
12166                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
12167                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12168                 goto FINISH_OFF;
12169         }
12170
12171         memset(mbox_list, 0x00, sizeof(int) * (*count));
12172
12173         for (i = 0; i < (*count); i++) {
12174                 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
12175                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12176                 /* EM_DEBUG_LOG("In emstorage_get_pdb_mailbox_list() loop, After sqlite3_step(), , i = %d, rc = %d.", i,  rc); */
12177                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12178                         ("sqlite3_step fail:%d", rc));
12179                 EM_DEBUG_LOG("mbox_list %d", mbox_list + i);
12180         }
12181
12182         ret = true;
12183
12184 FINISH_OFF:
12185         if (ret == true)
12186                 *mailbox_list = mbox_list;
12187         else
12188                 EM_SAFE_FREE(mbox_list);
12189
12190         if (hStmt != NULL) {
12191                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12192
12193                 rc = sqlite3_finalize(hStmt);
12194                 hStmt = NULL;
12195                 if (rc != SQLITE_OK) {
12196                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
12197                         error = EMAIL_ERROR_DB_FAILURE;
12198                 }
12199         }
12200
12201         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12202         _DISCONNECT_DB;
12203         if (err_code != NULL)
12204                 *err_code = error;
12205         EM_DEBUG_FUNC_END("ret [%d]", ret);
12206         return ret;
12207 }
12208
12209
12210 INTERNAL_FUNC int emstorage_get_pbd_account_list(int **account_list, int *count, int transaction, int *err_code)
12211 {
12212         EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
12213
12214         if (NULL == &account_list || NULL == count) {
12215                 EM_DEBUG_EXCEPTION("mailbox_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
12216                 if (err_code != NULL)
12217                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12218                 return false;
12219         }
12220
12221         int ret = false;
12222         int error = EMAIL_ERROR_NONE;
12223         char *sql;
12224         char **result;
12225         int i = 0, rc = -1;
12226         int *result_account_list = NULL;
12227         DB_STMT hStmt = NULL;
12228         char sql_query_string[QUERY_SIZE] = {0, };
12229
12230         sqlite3 *local_db_handle = emstorage_get_db_connection();
12231
12232         EMSTORAGE_START_READ_TRANSACTION(transaction);
12233
12234
12235         sql = "SELECT count(distinct account_id) FROM mail_partial_body_activity_tbl";
12236
12237
12238         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
12239         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12240                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
12241
12242         *count = atoi(result[1]);
12243         sqlite3_free_table(result);
12244
12245         if (!*count) {
12246                 EM_DEBUG_EXCEPTION("no account found...");
12247                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
12248                 ret = true;
12249                 goto FINISH_OFF;
12250         }
12251
12252         EM_DEBUG_LOG("Account count [%d]", *count);
12253
12254         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12255
12256         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_partial_body_activity_tbl");
12257
12258         EM_DEBUG_LOG("Query [%s]", sql_query_string);
12259
12260
12261         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12262
12263         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
12264         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12265                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12266
12267
12268         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12269         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12270                 ("sqlite3_step fail:%d", rc));
12271
12272         if (NULL == (result_account_list = (int *)em_malloc(sizeof(int) * (*count)))) {
12273                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
12274                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12275                 goto FINISH_OFF;
12276         }
12277
12278         memset(result_account_list, 0x00, sizeof(int) * (*count));
12279
12280         for (i = 0; i < (*count); i++) {
12281                 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
12282
12283                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12284                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12285                         ("sqlite3_step fail:%d", rc));
12286                 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
12287         }
12288
12289         ret = true;
12290
12291 FINISH_OFF:
12292         if (ret == true)
12293                 *account_list = result_account_list;
12294         else
12295                 EM_SAFE_FREE(result_account_list);
12296
12297         if (hStmt != NULL) {
12298                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12299
12300                 rc = sqlite3_finalize(hStmt);
12301                 hStmt = NULL;
12302                 if (rc != SQLITE_OK) {
12303                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
12304                         error = EMAIL_ERROR_DB_FAILURE;
12305                 }
12306         }
12307
12308         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12309         _DISCONNECT_DB;
12310         if (err_code != NULL)
12311                 *err_code = error;
12312         EM_DEBUG_FUNC_END("ret [%d]", ret);
12313         return ret;
12314 }
12315
12316
12317
12318 INTERNAL_FUNC int emstorage_get_pbd_activity_data(int account_id, int input_mailbox_id, email_event_partial_body_thd** event_start, int *count, int transaction, int *err_code)
12319 {
12320         EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
12321
12322         if (account_id < FIRST_ACCOUNT_ID || NULL == event_start || 0 == input_mailbox_id || NULL == count) {
12323                 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);
12324
12325                 if (err_code != NULL)
12326                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12327                 return false;
12328         }
12329
12330         int rc = -1;
12331         int ret = false;
12332         char **result;
12333         int error = EMAIL_ERROR_NONE;
12334         int i = 0;
12335         DB_STMT hStmt = NULL;
12336         email_event_partial_body_thd* event_list = NULL;
12337         char sql_query_string[QUERY_SIZE] = {0, };
12338
12339         sqlite3 *local_db_handle = emstorage_get_db_connection();
12340
12341         EMSTORAGE_START_READ_TRANSACTION(transaction);
12342
12343         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12344         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);
12345
12346
12347         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
12348         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12349                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12350
12351         *count = atoi(result[1]);
12352         sqlite3_free_table(result);
12353
12354         EM_DEBUG_LOG("Query = [%s]", sql_query_string);
12355
12356         if (!*count) {
12357                 EM_DEBUG_EXCEPTION("No matched activity found in mail_partial_body_activity_tbl");
12358
12359                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
12360                 ret = true;
12361                 goto FINISH_OFF;
12362         }
12363         EM_DEBUG_LOG("Activity Count = %d", *count);
12364
12365         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12366         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);
12367
12368         EM_DEBUG_LOG("Query [%s]", sql_query_string);
12369
12370
12371         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12372
12373         EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
12374         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12375                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12376
12377
12378         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12379         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12380                 ("sqlite3_step fail:%d", rc));
12381
12382         if (!(event_list = (email_event_partial_body_thd*)em_malloc(sizeof(email_event_partial_body_thd) * (*count)))) {
12383                 EM_DEBUG_EXCEPTION("Malloc failed");
12384
12385                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12386                 goto FINISH_OFF;
12387         }
12388         memset(event_list, 0x00, sizeof(email_event_partial_body_thd) * (*count));
12389
12390         for (i=0; i < (*count); i++) {
12391                 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_IDX_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12392                 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12393                 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12394                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12395                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_type), ACTIVITY_TYPE_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12396                 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12397                 _get_stmt_field_data_string(hStmt, &(event_list[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12398
12399                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12400                 /* EM_DEBUG_LOG("In emstorage_get_pbd_activity_data() loop, After sqlite3_step(), , i = %d, rc = %d.", i,  rc); */
12401                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12402                         ("sqlite3_step fail:%d", rc));
12403
12404                 event_list[i].event_type = 0;
12405         }
12406
12407         ret = true;
12408
12409 FINISH_OFF:
12410         if (true == ret)
12411           *event_start = event_list;
12412         else {
12413                 EM_SAFE_FREE(event_list);
12414         }
12415
12416         if (hStmt != NULL) {
12417                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12418
12419                 rc = sqlite3_finalize(hStmt);
12420                 hStmt = NULL;
12421                 if (rc != SQLITE_OK) {
12422                         EM_DEBUG_LOG("sqlite3_finalize failed - %d", rc);
12423                         error = EMAIL_ERROR_DB_FAILURE;
12424                 }
12425         }
12426
12427         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12428         _DISCONNECT_DB;
12429         if (err_code != NULL)
12430                 *err_code = error;
12431
12432         EM_DEBUG_FUNC_END("ret [%d]", ret);
12433         return ret;
12434 }
12435
12436
12437
12438 INTERNAL_FUNC int emstorage_delete_pbd_activity(int account_id, int mail_id, int activity_id, int transaction, int *err_code)
12439 {
12440         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);
12441
12442
12443         if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
12444                 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);
12445
12446                 if (err_code != NULL)
12447                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12448                 return false;
12449         }
12450
12451         int rc = -1;
12452         int ret = false;
12453         int error = EMAIL_ERROR_NONE;
12454         char sql_query_string[QUERY_SIZE] = {0, };
12455
12456         sqlite3 *local_db_handle = emstorage_get_db_connection();
12457
12458         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12459         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12460
12461         if (activity_id == 0)
12462                 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);
12463         else
12464                 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);
12465
12466         EM_DEBUG_LOG("Query [%s]", sql_query_string);
12467
12468         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12469         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12470                 ("sqlite3_exec fail:%d", rc));
12471         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12472                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12473
12474         /*  validate activity existence */
12475         rc = sqlite3_changes(local_db_handle);
12476         if (rc == 0)  {
12477                 EM_DEBUG_EXCEPTION("No matching activity found");
12478                 error = EMAIL_ERROR_DATA_NOT_FOUND;
12479                 ret = true;
12480                 goto FINISH_OFF;
12481         }
12482
12483         ret = true;
12484
12485 FINISH_OFF:
12486
12487         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12488
12489         _DISCONNECT_DB;
12490         if (err_code != NULL)
12491                 *err_code = error;
12492
12493         EM_DEBUG_FUNC_END("ret [%d]", ret);
12494         return ret;
12495 }
12496
12497 INTERNAL_FUNC int emstorage_get_mailbox_pbd_activity_count(int account_id, int input_mailbox_id, int *activity_count, int transaction, int *err_code)
12498 {
12499         EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
12500
12501         if (account_id < FIRST_ACCOUNT_ID || NULL == activity_count || NULL == err_code) {
12502                 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
12503                 if (err_code != NULL)
12504                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12505                 return false;
12506         }
12507         int rc = -1;
12508         int ret = false;
12509         int error = EMAIL_ERROR_NONE;
12510         char sql_query_string[QUERY_SIZE] = {0, };
12511
12512         DB_STMT hStmt = NULL;
12513
12514         sqlite3 *local_db_handle = emstorage_get_db_connection();
12515
12516         EMSTORAGE_START_READ_TRANSACTION(transaction);
12517         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12518
12519         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);
12520
12521         EM_DEBUG_LOG(" Query [%s]", sql_query_string);
12522
12523
12524         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12525         EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
12526         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12527                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12528
12529
12530         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12531         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12532                 ("sqlite3_step fail:%d", rc));
12533
12534         _get_stmt_field_data_int(hStmt, activity_count, 0);
12535
12536         EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
12537
12538         ret = true;
12539
12540 FINISH_OFF:
12541
12542         if (hStmt != NULL) {
12543                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12544                 rc = sqlite3_finalize(hStmt);
12545                 hStmt=NULL;
12546                 if (rc != SQLITE_OK) {
12547                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
12548                         error = EMAIL_ERROR_DB_FAILURE;
12549                 }
12550                 EM_DEBUG_LOG("sqlite3_finalize- %d", rc);
12551         }
12552
12553         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12554         _DISCONNECT_DB;
12555
12556         if (err_code != NULL)
12557                 *err_code = error;
12558
12559         EM_DEBUG_FUNC_END("ret [%d]", ret);
12560         return ret;
12561 }
12562
12563
12564 INTERNAL_FUNC int emstorage_get_pbd_activity_count(int *activity_count, int transaction, int *err_code)
12565 {
12566         EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
12567
12568         if (NULL == activity_count || NULL == err_code) {
12569                 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
12570                 if (err_code != NULL)
12571                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12572                 return false;
12573         }
12574         int rc = -1;
12575         int ret = false;
12576         int error = EMAIL_ERROR_NONE;
12577         DB_STMT hStmt = NULL;
12578         char sql_query_string[QUERY_SIZE] = {0, };
12579
12580         sqlite3 *local_db_handle = emstorage_get_db_connection();
12581
12582         EMSTORAGE_START_READ_TRANSACTION(transaction);
12583         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12584
12585         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl;");
12586
12587         EM_DEBUG_LOG(" Query [%s]", sql_query_string);
12588
12589
12590         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12591         EM_DEBUG_LOG("  before sqlite3_prepare hStmt = %p", hStmt);
12592         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12593                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12594
12595
12596         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12597         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12598                 ("sqlite3_step fail:%d", rc));
12599
12600         _get_stmt_field_data_int(hStmt, activity_count, 0);
12601
12602         EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
12603
12604         ret = true;
12605
12606 FINISH_OFF:
12607
12608
12609         if (hStmt != NULL) {
12610                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12611
12612                 rc = sqlite3_finalize(hStmt);
12613                 hStmt=NULL;
12614                 if (rc != SQLITE_OK) {
12615                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
12616                         error = EMAIL_ERROR_DB_FAILURE;
12617                 }
12618                 EM_DEBUG_LOG("sqlite3_finalize- %d", rc);
12619         }
12620
12621         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12622         _DISCONNECT_DB;
12623         if (err_code != NULL)
12624                 *err_code = error;
12625
12626         EM_DEBUG_FUNC_END("ret [%d]", ret);
12627         return ret;
12628 }
12629
12630 INTERNAL_FUNC int emstorage_delete_full_pbd_activity_data(int account_id, int transaction, int *err_code)
12631 {
12632         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
12633         if (account_id < FIRST_ACCOUNT_ID) {
12634                 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
12635                 if (err_code != NULL)
12636                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12637                 return false;
12638         }
12639
12640         int rc = -1;
12641         int ret = false;
12642         int error = EMAIL_ERROR_NONE;
12643         char sql_query_string[QUERY_SIZE] = {0, };
12644
12645         sqlite3 *local_db_handle = emstorage_get_db_connection();
12646         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12647         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12648         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d", account_id);
12649
12650         EM_DEBUG_LOG("Query [%s]", sql_query_string);
12651
12652         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12653         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12654                 ("sqlite3_exec fail:%d", rc));
12655         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12656                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12657
12658
12659         rc = sqlite3_changes(local_db_handle);
12660         if (rc == 0) {
12661                 EM_DEBUG_EXCEPTION("No matching activities found in mail_partial_body_activity_tbl");
12662                 error = EMAIL_ERROR_DATA_NOT_FOUND;
12663                 ret = true;
12664                 goto FINISH_OFF;
12665         }
12666
12667         ret = true;
12668
12669 FINISH_OFF:
12670
12671         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12672         _DISCONNECT_DB;
12673         if (err_code != NULL)
12674                 *err_code = error;
12675
12676         EM_DEBUG_FUNC_END("ret [%d]", ret);
12677         return ret;
12678 }
12679
12680 /*Himanshu[h.gahlaut]-> Added below API to update mail_partial_body_activity_tbl
12681 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*/
12682
12683 INTERNAL_FUNC int emstorage_update_pbd_activity(char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code)
12684 {
12685         EM_DEBUG_FUNC_BEGIN("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
12686
12687         int rc = -1, ret = false;
12688         int error = EMAIL_ERROR_NONE;
12689         char sql_query_string[QUERY_SIZE] = {0, };
12690
12691         int transaction = true;
12692
12693         if (!old_server_uid || !new_server_uid || !mbox_name)  {
12694                 EM_DEBUG_EXCEPTION("Invalid parameters");
12695                 error = EMAIL_ERROR_INVALID_PARAM;
12696                 return false;
12697         }
12698
12699         sqlite3 *local_db_handle = emstorage_get_db_connection();
12700         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12701         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12702         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12703                  "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);
12704
12705         EM_DEBUG_LOG("Query [%s]", sql_query_string);
12706
12707         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12708         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12709                 ("sqlite3_exec fail:%d", rc));
12710         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12711                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12712
12713         rc = sqlite3_changes(local_db_handle);
12714         if (rc == 0) {
12715                 EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
12716                 error = EMAIL_ERROR_DATA_NOT_FOUND;
12717                 goto FINISH_OFF;
12718         }
12719
12720         ret = true;
12721
12722 FINISH_OFF:
12723
12724         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12725         _DISCONNECT_DB;
12726         if (err_code != NULL)
12727                 *err_code = error;
12728
12729         EM_DEBUG_FUNC_END("ret [%d]", ret);
12730         return ret;
12731 }
12732
12733
12734 INTERNAL_FUNC int emstorage_create_file(char *data_string, size_t file_size, char *dst_file_name, int *err_code)
12735 {
12736         EM_DEBUG_FUNC_BEGIN("file_size[%d] , dst_file_name[%s], err_code[%p]", file_size, dst_file_name, err_code);
12737
12738         int ret = false;
12739         int error = EMAIL_ERROR_NONE;
12740
12741         FILE* fp_dst = NULL;
12742
12743         if (!data_string || !dst_file_name)  {
12744                 EM_DEBUG_EXCEPTION("data_string[%p], dst_file_name[%p]", data_string, dst_file_name);
12745                 error = EMAIL_ERROR_INVALID_PARAM;
12746                 goto FINISH_OFF;
12747         }
12748
12749         fp_dst = fopen(dst_file_name, "w");
12750
12751         if (!fp_dst)  {
12752                 EM_DEBUG_EXCEPTION("fopen failed - %s", dst_file_name);
12753                 if (errno == 28)
12754                         error = EMAIL_ERROR_MAIL_MEMORY_FULL;
12755                 else
12756                         error = EMAIL_ERROR_SYSTEM_FAILURE;
12757                 goto FINISH_OFF;
12758         }
12759
12760         if (fwrite(data_string, 1, file_size, fp_dst) == 0) {
12761                 EM_DEBUG_EXCEPTION("fwrite failed...");
12762                 error = EMAIL_ERROR_UNKNOWN;
12763                 goto FINISH_OFF;
12764         }
12765
12766         ret = true;
12767
12768 FINISH_OFF:
12769
12770         if (fp_dst != NULL)
12771                 fclose(fp_dst);
12772
12773         if (err_code != NULL)
12774                 *err_code = error;
12775
12776         EM_DEBUG_FUNC_END("ret [%d]", ret);
12777         return ret;
12778 }
12779
12780 #endif /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
12781
12782
12783
12784 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
12785
12786 INTERNAL_FUNC int emstorage_update_read_mail_uid_by_server_uid(char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code)
12787 {
12788         EM_DEBUG_FUNC_BEGIN();
12789         int rc = -1;
12790         int ret = false;
12791         int error = EMAIL_ERROR_NONE;
12792         char sql_query_string[QUERY_SIZE] = {0, };
12793
12794         int transaction = true;
12795
12796         if (!old_server_uid || !new_server_uid || !mbox_name)  {
12797                 EM_DEBUG_EXCEPTION("Invalid parameters");
12798                 error = EMAIL_ERROR_INVALID_PARAM;
12799                 return false;
12800         }
12801
12802         EM_DEBUG_LOG("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
12803
12804         sqlite3 *local_db_handle = emstorage_get_db_connection();
12805         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12806
12807
12808         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12809                  "UPDATE mail_read_mail_uid_tbl SET s_uid=\'%s\' , mailbox_id=\'%s\', mailbox_name=\'%s\' WHERE s_uid=%s ", new_server_uid, mbox_name, mbox_name, old_server_uid);
12810
12811          EM_DEBUG_LOG(" Query [%s]", sql_query_string);
12812
12813         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12814          EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12815                  ("sqlite3_exec fail:%d", rc));
12816          EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12817                  ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12818
12819
12820          rc = sqlite3_changes(local_db_handle);
12821          if (rc == 0)
12822          {
12823                  EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
12824                  error = EMAIL_ERROR_DATA_NOT_FOUND;
12825                  goto FINISH_OFF;
12826          }
12827
12828
12829          ret = true;
12830
12831 FINISH_OFF:
12832
12833         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12834         _DISCONNECT_DB;
12835
12836         if (err_code != NULL)
12837                 *err_code = error;
12838
12839         EM_DEBUG_FUNC_END("ret [%d]", ret);
12840         return ret;
12841
12842 }
12843
12844
12845 /**
12846  * @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);
12847  * Prepare an array of mail_id and corresponding server mail id.
12848  *
12849  *@author                                       h.gahlaut@samsung.com
12850  * @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)
12851  *                                                      where 88 is the length of fixed keywords including ending null character in the QUERY to be formed
12852  * @param[out] idset                    Returns the array of mail_id and corresponding server_mail_id sorted by server_mail_ids
12853  * @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
12854  * @param[out] err_code         Returns the error code.
12855  * @remarks                                     An Example of Query to be exexuted in this API:
12856  *                                                      SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (12, 13, 56, 78);
12857  * @return This function returns true on success or false on failure.
12858  */
12859
12860 INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *mail_ids, email_id_set_t** idset, int *id_set_count, int *err_code)
12861 {
12862         EM_DEBUG_FUNC_BEGIN();
12863         EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds);
12864
12865         int error = EMAIL_ERROR_NONE;
12866         int ret = false;
12867         email_id_set_t* p_id_set = NULL;
12868         int count = 0;
12869         const int buf_size = QUERY_SIZE;
12870         char sql_query_string[QUERY_SIZE] = {0, };
12871         int space_left_in_query_buffer = buf_size;
12872         int i = 0;
12873         int rc = -1;
12874         char *server_mail_id = NULL;
12875         char **result = NULL;
12876         int col_index = 0;
12877
12878
12879         if (NULL == mail_ids || NULL == idset || NULL == id_set_count) {
12880                 EM_DEBUG_EXCEPTION("Invalid Parameters mail_ids[%p] idset[%p]  id_set_count [%p]", mail_ids, idset, id_set_count);
12881                 error = EMAIL_ERROR_INVALID_PARAM;
12882                 goto FINISH_OFF;
12883         }
12884
12885         sqlite3 *local_db_handle = emstorage_get_db_connection();
12886
12887         SNPRINTF(sql_query_string, space_left_in_query_buffer, "SELECT local_uid, s_uid FROM mail_read_mail_uid_tbl WHERE local_uid in (%s) ORDER BY s_uid", mail_ids);
12888
12889         EM_DEBUG_LOG("SQL Query formed [%s] ", sql_query_string);
12890
12891         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
12892         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
12893         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12894                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12895
12896         EM_DEBUG_LOG(" Count of mails [%d ]", count);
12897
12898         if (count <= 0) {
12899                 EM_DEBUG_EXCEPTION("Can't find proper mail");
12900                 error = EMAIL_ERROR_DATA_NOT_FOUND;
12901                 goto FINISH_OFF;
12902         }
12903
12904
12905         if (NULL == (p_id_set = (email_id_set_t*)em_malloc(sizeof(email_id_set_t) * count)))  {
12906                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
12907                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12908                 goto FINISH_OFF;
12909         }
12910
12911         col_index = 2;
12912
12913         EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds_Loop);
12914         EM_DEBUG_LOG(">>>> DATA ASSIGN START");
12915         for (i = 0; i < count; i++)  {
12916                 _get_table_field_data_int(result, &(p_id_set[i].mail_id), col_index++);
12917                 _get_table_field_data_string(result, &server_mail_id, 1, col_index++);
12918                 p_id_set[i].server_mail_id = strtoul(server_mail_id, NULL, 10);
12919                 EM_SAFE_FREE(server_mail_id);
12920         }
12921         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d]", count);
12922         EM_PROFILE_END(EmStorageGetIdSetFromMailIds_Loop);
12923
12924         sqlite3_free_table(result);
12925         result = NULL;
12926
12927         ret = true;
12928
12929         FINISH_OFF:
12930
12931         if (ret == true)  {
12932                 *idset = p_id_set;
12933                 *id_set_count = count;
12934                 EM_DEBUG_LOG(" idset[%p] id_set_count [%d]", *idset, *id_set_count);
12935         }
12936         else
12937                 EM_SAFE_FREE(p_id_set);
12938
12939         _DISCONNECT_DB;
12940
12941         if (err_code != NULL)
12942                 *err_code = error;
12943
12944         EM_PROFILE_END(EmStorageGetIdSetFromMailIds);
12945
12946         EM_DEBUG_FUNC_END("ret [%d]", ret);
12947         return ret;
12948 }
12949
12950
12951
12952 #endif
12953
12954 INTERNAL_FUNC int emstorage_delete_triggers_from_lucene()
12955 {
12956         EM_DEBUG_FUNC_BEGIN();
12957         int rc, ret = true, transaction = true;
12958         int error = EMAIL_ERROR_NONE;
12959         char sql_query_string[QUERY_SIZE] = {0, };
12960
12961         sqlite3 *local_db_handle = emstorage_get_db_connection();
12962         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12963
12964         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerDelete;");
12965
12966         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12967         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12968                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12969
12970         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerInsert;");
12971
12972         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12973         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12974                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12975
12976
12977         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerUpdate;");
12978
12979         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12980         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12981                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12982
12983         ret = true;
12984
12985 FINISH_OFF:
12986
12987         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12988
12989         _DISCONNECT_DB;
12990
12991         EM_DEBUG_FUNC_END("ret [%d]", ret);
12992         return ret;
12993 }
12994
12995 INTERNAL_FUNC int emstorage_filter_mails_by_rule(int account_id, int dest_mailbox_id, int dest_mailbox_type, emstorage_rule_tbl_t *rule, int ** filtered_mail_id_list, int *count_of_mails, int *err_code)
12996 {
12997         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);
12998
12999         if ((account_id <= 0) || (dest_mailbox_id <= 0) || (!rule) || (!rule->value)|| (!filtered_mail_id_list) || (!count_of_mails)) {
13000                 EM_DEBUG_EXCEPTION("Invalid Parameter");
13001
13002                 if (err_code != NULL)
13003                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13004                 return false;
13005         }
13006
13007         int rc = -1, ret = false, error = EMAIL_ERROR_NONE;
13008         int count = 0, col_index = 0, i = 0, where_pararaph_length = 0, *mail_list = NULL;
13009         char **result = NULL, *where_pararaph = NULL;
13010         char sql_query_string[QUERY_SIZE] = {0, };
13011
13012         sqlite3 *local_db_handle = emstorage_get_db_connection();
13013
13014         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl ");
13015
13016         EM_DEBUG_LOG("rule->value [%s]", rule->value);
13017
13018         where_pararaph_length = EM_SAFE_STRLEN(rule->value) + 100;
13019         where_pararaph = malloc(sizeof(char) * where_pararaph_length);
13020
13021         if (where_pararaph == NULL) {
13022                 EM_DEBUG_EXCEPTION("malloc failed for where_pararaph.");
13023                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13024                 goto FINISH_OFF;
13025         }
13026
13027         memset(where_pararaph, 0, sizeof(char) * where_pararaph_length);
13028
13029         EM_DEBUG_LOG("rule->type [%d], rule->flag2[%d]", rule->type, rule->flag2);
13030
13031         if (rule->type == EMAIL_FILTER_FROM) {
13032                 if (rule->flag2 == RULE_TYPE_INCLUDES)
13033                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (3,5) AND full_address_from like \'%%%s%%\'", account_id, rule->value);
13034                 else /*  RULE_TYPE_EXACTLY */
13035                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (3,5) AND full_address_from = \'%s\'", account_id, rule->value);
13036         }
13037         else if (rule->type == EMAIL_FILTER_SUBJECT) {
13038                 if (rule->flag2 == RULE_TYPE_INCLUDES)
13039                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (3,5) AND subject like \'%%%s%%\'", account_id, rule->value);
13040                 else /*  RULE_TYPE_EXACTLY */
13041                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (3,5) AND subject = \'%s\'", account_id, rule->value);
13042         }
13043         else {
13044                 error = EMAIL_ERROR_INVALID_PARAM;
13045                 EM_DEBUG_EXCEPTION("rule->type is invald");
13046                 goto FINISH_OFF;
13047         }
13048
13049         /* prevent 34361 */
13050         if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
13051                 strcat(sql_query_string, where_pararaph);
13052
13053         EM_DEBUG_LOG("query[%s]", sql_query_string);
13054
13055         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
13056         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
13057         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13058                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13059
13060         EM_DEBUG_LOG("Count of mails [%d]", count);
13061
13062         if (count) {
13063                 mail_list = malloc(sizeof(int) * count);
13064                 if (mail_list == NULL) {
13065                         EM_DEBUG_EXCEPTION("malloc failed for mail_list.");
13066                         error = EMAIL_ERROR_OUT_OF_MEMORY;
13067                         goto FINISH_OFF;
13068                 }
13069
13070                 col_index = 1;
13071
13072                 for (i = 0; i < count; i++)
13073                         _get_table_field_data_int(result, &(mail_list[i]), col_index++);
13074
13075                 memset(sql_query_string, 0x00, QUERY_SIZE);
13076                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET mailbox_id = %d, mailbox_type = %d ", dest_mailbox_id, dest_mailbox_type);
13077
13078                 /* prevent 34361 */
13079                 if(strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
13080                 strcat(sql_query_string, where_pararaph);
13081
13082                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13083                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13084                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13085
13086 #ifdef __FEATURE_BODY_SEARCH__
13087                 /* Updating mail_text_tbl */
13088                 memset(sql_query_string, 0x00, QUERY_SIZE);
13089                 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_text_tbl SET mailbox_id = %d ", dest_mailbox_id);
13090                 if(strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
13091                         strcat(sql_query_string, where_pararaph);
13092                 EM_DEBUG_LOG("Query [%s]", sql_query_string);
13093
13094                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13095                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13096                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13097 #endif
13098         }
13099
13100         ret = true;
13101
13102 FINISH_OFF:
13103
13104         if (ret)
13105                 *filtered_mail_id_list = mail_list;
13106         else
13107                 EM_SAFE_FREE(mail_list);
13108
13109         sqlite3_free_table(result);
13110         result = NULL;
13111
13112         _DISCONNECT_DB;
13113
13114         EM_SAFE_FREE(where_pararaph);
13115
13116         if (ret && count_of_mails)
13117                 *count_of_mails = count;
13118
13119         if (err_code != NULL)
13120                 *err_code = error;
13121
13122         EM_DEBUG_FUNC_END("ret [%d]", ret);
13123         return ret;
13124 }
13125
13126 #define EMAIL_SLOT_UNIT 25
13127
13128 INTERNAL_FUNC int emstorage_set_mail_slot_size(int account_id, int mailbox_id, int new_slot_size, int transaction, int *err_code)
13129 {
13130         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);
13131         int rc = -1, ret = false, err = EMAIL_ERROR_NONE;
13132         int where_pararaph_length = 0;
13133         char *where_pararaph = NULL;
13134         char sql_query_string[QUERY_SIZE] = {0, };
13135         int and = 0;
13136         sqlite3 *local_db_handle = emstorage_get_db_connection();
13137
13138         EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
13139
13140         if (new_slot_size > 0)
13141                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = %d ", new_slot_size);
13142         else if (new_slot_size == 0)
13143                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", EMAIL_SLOT_UNIT);
13144         else
13145                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", new_slot_size * -1);
13146
13147
13148         if (mailbox_id)
13149                 where_pararaph_length = 80;
13150         else
13151                 where_pararaph_length = 50;
13152
13153         if (new_slot_size == 0)
13154                 where_pararaph_length += 70;
13155
13156         where_pararaph = malloc(sizeof(char) * where_pararaph_length);
13157         if (where_pararaph == NULL) {
13158                 EM_DEBUG_EXCEPTION("Memory allocation failed for where_pararaph");
13159                 err = EMAIL_ERROR_OUT_OF_MEMORY;
13160                 goto FINISH_OFF;
13161         }
13162         memset(where_pararaph, 0x00, where_pararaph_length);
13163
13164         if (account_id > ALL_ACCOUNT) {
13165                 and = 1;
13166                 if (mailbox_id)
13167                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_id = %d ", mailbox_id);
13168                 else
13169                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d ", account_id);
13170         }
13171
13172         if (new_slot_size == 0)
13173                 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"));
13174
13175         if (strlen(sql_query_string) + EM_SAFE_STRLEN(where_pararaph) < QUERY_SIZE) /* prevent 34363 */
13176                 strcat(sql_query_string, where_pararaph);
13177         else {
13178                 EM_DEBUG_EXCEPTION("Query buffer overflowed !!!");
13179                 err = EMAIL_ERROR_OUT_OF_MEMORY;
13180                 goto FINISH_OFF;
13181         }
13182
13183         EM_DEBUG_LOG("query[%s]", sql_query_string);
13184
13185         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13186         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13187         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13188
13189         ret = true;
13190
13191 FINISH_OFF:
13192
13193         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
13194
13195         _DISCONNECT_DB;
13196
13197         EM_SAFE_FREE(where_pararaph);
13198
13199         if (err_code != NULL)
13200                 *err_code = err;
13201
13202         EM_DEBUG_FUNC_END("ret [%d]", ret);
13203         return ret;
13204 }
13205
13206 INTERNAL_FUNC int emstorage_add_meeting_request(int account_id, int input_mailbox_id, email_meeting_request_t* meeting_req, int transaction, int *err_code)
13207 {
13208         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);
13209
13210         if (!meeting_req || meeting_req->mail_id <= 0) {
13211                 if (meeting_req)
13212                 EM_DEBUG_EXCEPTION("mail_id[%]d", meeting_req->mail_id);
13213
13214                 if (err_code != NULL)
13215                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13216
13217                 return false;
13218         }
13219
13220         int rc = -1;
13221         int ret = false;
13222         int error = EMAIL_ERROR_NONE;
13223         DB_STMT hStmt = NULL;
13224         char sql_query_string[QUERY_SIZE] = {0, };
13225         int col_index = 0;
13226         time_t temp_unix_time = 0;
13227
13228         sqlite3 *local_db_handle = emstorage_get_db_connection();
13229         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13230
13231         SNPRINTF(sql_query_string, sizeof(sql_query_string),
13232                 "INSERT INTO mail_meeting_tbl VALUES "
13233                 "( ?"           /*  mail_id */
13234                 ", ?"           /*  account_id */
13235                 ", ?"           /*  mailbox_id */
13236                 ", ?"           /*  meeting_response */
13237                 ", ?"           /*  start_time */
13238                 ", ?"           /*  end_time */
13239                 ", ?"           /*  location */
13240                 ", ?"           /*  global_object_id */
13241                 ", ?"           /*  offset */
13242                 ", ?"           /*  standard_name */
13243                 ", ?"           /*  standard_time_start_date */
13244                 ", ?"           /*  standard_biad */
13245                 ", ?"           /*  daylight_name */
13246                 ", ?"           /*  daylight_time_start_date */
13247                 ", ?"           /*  daylight_bias */
13248                 " )");
13249
13250         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13251         if (rc != SQLITE_OK)  {
13252                 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
13253                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
13254
13255                 error = EMAIL_ERROR_DB_FAILURE;
13256                 goto FINISH_OFF;
13257         }
13258
13259         col_index = 0;
13260         /*
13261         EM_DEBUG_LOG(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
13262         EM_DEBUG_LOG(">>>>> account_id[%d]", account_id);
13263         EM_DEBUG_LOG(">>>>> mailbox_name[%s]", mailbox_name);
13264         EM_DEBUG_LOG(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
13265         EM_DEBUG_LOG(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
13266         EM_DEBUG_LOG(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
13267         EM_DEBUG_LOG(">>>>> meeting_req->location[%s]", meeting_req->location);
13268         EM_DEBUG_LOG(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
13269         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
13270         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
13271         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
13272         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
13273         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
13274         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
13275         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
13276         */
13277         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->mail_id);
13278         _bind_stmt_field_data_int(hStmt, col_index++, account_id);
13279         _bind_stmt_field_data_int(hStmt, col_index++, input_mailbox_id);
13280         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
13281
13282         temp_unix_time = timegm(&(meeting_req->start_time));
13283         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13284         temp_unix_time = timegm(&(meeting_req->end_time));
13285         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13286
13287         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->location, 0, LOCATION_LEN_IN_MAIL_MEETING_TBL);
13288         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->global_object_id, 0, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
13289
13290         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
13291         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.standard_name, 0, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
13292         temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
13293         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13294         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
13295
13296         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.daylight_name, 0, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
13297         temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
13298         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13299         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
13300
13301
13302         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13303         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
13304                 ("sqlite3_step fail:%d", rc));
13305         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13306                 ("sqlite3_step fail:%d", rc));
13307
13308         ret = true;
13309
13310 FINISH_OFF:
13311         if (hStmt != NULL)  {
13312                 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
13313
13314                 rc = sqlite3_finalize(hStmt);
13315                 if (rc != SQLITE_OK)  {
13316                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
13317                         error = EMAIL_ERROR_DB_FAILURE;
13318                 }
13319         }
13320
13321         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13322         _DISCONNECT_DB;
13323
13324         if (err_code != NULL)
13325                 *err_code = error;
13326
13327         EM_DEBUG_FUNC_END("ret [%d]", ret);
13328         return ret;
13329 }
13330
13331 INTERNAL_FUNC int emstorage_get_meeting_request(int mail_id, email_meeting_request_t ** meeting_req, int transaction, int *err_code)
13332 {
13333         EM_DEBUG_FUNC_BEGIN();
13334
13335         int count = 0;
13336         int rc = -1;
13337         int ret = false;
13338         int error = EMAIL_ERROR_NONE;
13339         email_meeting_request_t *p_temp_meeting_req = NULL;
13340         int col_index = 0;
13341         time_t temp_unix_time;
13342
13343         EM_IF_NULL_RETURN_VALUE(meeting_req, false);
13344
13345         DB_STMT hStmt = NULL;
13346         char sql_query_string[QUERY_SIZE] = {0, };
13347
13348         sqlite3 *local_db_handle = emstorage_get_db_connection();
13349         EMSTORAGE_START_READ_TRANSACTION(transaction);
13350
13351         SNPRINTF(sql_query_string, sizeof(sql_query_string),
13352                 "SELECT  mail_id, meeting_response, start_time, end_time, location, global_object_id, offset, standard_name, standard_time_start_date, standard_bias, daylight_name, daylight_time_start_date, daylight_bias "
13353                 " FROM mail_meeting_tbl "
13354                 " WHERE mail_id = %d", mail_id);
13355         EM_DEBUG_LOG("sql : %s ", sql_query_string);
13356
13357
13358         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13359
13360         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13361                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13362
13363
13364         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13365         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13366                 ("sqlite3_step fail:%d", rc));
13367
13368         if (rc == SQLITE_DONE)  {
13369                 EM_DEBUG_EXCEPTION(" no Meeting request found...");
13370                 count = 0;
13371                 ret = false;
13372                 error= EMAIL_ERROR_DATA_NOT_FOUND;
13373                 goto FINISH_OFF;
13374         }
13375
13376         if (!(p_temp_meeting_req = (email_meeting_request_t*)malloc(sizeof(email_meeting_request_t)))) {
13377                 EM_DEBUG_EXCEPTION(" malloc failed...");
13378                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13379                 goto FINISH_OFF;
13380         }
13381
13382         memset(p_temp_meeting_req, 0x00, sizeof(email_meeting_request_t));
13383
13384         col_index = 0;
13385
13386         _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->mail_id), col_index++);
13387         _get_stmt_field_data_int(hStmt, (int *)&(p_temp_meeting_req->meeting_response), col_index++);
13388         _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
13389         gmtime_r(&temp_unix_time, &(p_temp_meeting_req->start_time));
13390         _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
13391         gmtime_r(&temp_unix_time, &(p_temp_meeting_req->end_time));
13392         _get_stmt_field_data_string(hStmt, &p_temp_meeting_req->location, 1, col_index++);
13393         _get_stmt_field_data_string(hStmt, &p_temp_meeting_req->global_object_id, 1, col_index++);
13394         _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->time_zone.offset_from_GMT), col_index++);
13395
13396         _get_stmt_field_data_string_without_allocation(hStmt, p_temp_meeting_req->time_zone.standard_name, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL, 1, col_index++);
13397         _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
13398         gmtime_r(&temp_unix_time, &(p_temp_meeting_req->time_zone.standard_time_start_date));
13399         _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->time_zone.standard_bias), col_index++);
13400
13401         _get_stmt_field_data_string_without_allocation(hStmt, p_temp_meeting_req->time_zone.daylight_name, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL, 1, col_index++);
13402         _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
13403         gmtime_r(&temp_unix_time, &(p_temp_meeting_req->time_zone.daylight_time_start_date));
13404         _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->time_zone.daylight_bias), col_index++);
13405
13406         /*
13407         EM_DEBUG_LOG(">>>>> meeting_req->mail_id[%d]", p_temp_meeting_req->mail_id);
13408         EM_DEBUG_LOG(">>>>> meeting_req->meeting_response[%d]", p_temp_meeting_req->meeting_response);
13409         EM_DEBUG_LOG(">>>>> meeting_req->start_time[%s]", asctime(&(p_temp_meeting_req->start_time)));
13410         EM_DEBUG_LOG(">>>>> meeting_req->end_time[%s]", asctime(&(p_temp_meeting_req->end_time)));
13411         EM_DEBUG_LOG(">>>>> meeting_req->location[%s]", p_temp_meeting_req->location);
13412         EM_DEBUG_LOG(">>>>> meeting_req->global_object_id[%s]", p_temp_meeting_req->global_object_id);
13413         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", p_temp_meeting_req->time_zone.offset_from_GMT);
13414         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_name[%s]", p_temp_meeting_req->time_zone.standard_name);
13415         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(p_temp_meeting_req->time_zone.standard_time_start_date)));
13416         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_bias[%d]", p_temp_meeting_req->time_zone.standard_bias);
13417         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_name[%s]", p_temp_meeting_req->time_zone.daylight_name);
13418         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(p_temp_meeting_req->time_zone.daylight_time_start_date)));
13419         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_bias[%d]", p_temp_meeting_req->time_zone.daylight_bias);
13420         */
13421         ret = true;
13422
13423 FINISH_OFF:
13424         if (ret == true)
13425                 *meeting_req = p_temp_meeting_req;
13426         else  {
13427                 EM_SAFE_FREE(p_temp_meeting_req);
13428         }
13429
13430         if (hStmt != NULL)  {
13431                 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
13432
13433                 rc = sqlite3_finalize(hStmt);
13434                 hStmt = NULL;
13435                 if (rc != SQLITE_OK)  {
13436                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
13437                         error = EMAIL_ERROR_DB_FAILURE;
13438                 }
13439         }
13440
13441         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13442         _DISCONNECT_DB;
13443
13444         if (err_code != NULL)
13445                 *err_code = error;
13446
13447         EM_DEBUG_FUNC_END("ret [%d]", ret);
13448         return ret;
13449 }
13450
13451
13452 INTERNAL_FUNC int emstorage_update_meeting_request(email_meeting_request_t* meeting_req, int transaction, int *err_code)
13453 {
13454         EM_DEBUG_FUNC_BEGIN("meeting_req[%p], transaction[%d], err_code[%p]", meeting_req, transaction, err_code);
13455
13456         int ret = false;
13457         int error = EMAIL_ERROR_NONE;
13458         int rc;
13459         DB_STMT hStmt = NULL;
13460         char sql_query_string[QUERY_SIZE] = {0, };
13461         time_t temp_unix_time = 0;
13462
13463         if (!meeting_req) {
13464                 EM_DEBUG_EXCEPTION("Invalid Parameter!");
13465                 error = EMAIL_ERROR_INVALID_PARAM;
13466                 goto FINISH_OFF;
13467         }
13468
13469         sqlite3 *local_db_handle = emstorage_get_db_connection();
13470         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13471
13472         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13473         SNPRINTF(sql_query_string, sizeof(sql_query_string),
13474                 "UPDATE mail_meeting_tbl "
13475                 "SET "
13476                 /* "  account_id = ?, "         //  not update here, this can be changed when move or copy */
13477                 /* "  mailbox_name = ?, "               //  not update here, this can be changed when move or copy */
13478                 "  meeting_response = ?, "
13479                 "  start_time = ?, "
13480                 "  end_time = ?, "
13481                 "  location = ?, "
13482                 "  global_object_id = ?, "
13483                 "  offset = ?, "
13484                 "  standard_name = ?, "
13485                 "  standard_time_start_date = ?, "
13486                 "  standard_bias = ?, "
13487                 "  daylight_name = ?, "
13488                 "  daylight_time_start_date = ?, "
13489                 "  daylight_bias = ? "
13490                 "WHERE mail_id = %d",
13491                 meeting_req->mail_id);
13492
13493         EM_DEBUG_LOG("SQL(%s)", sql_query_string);
13494
13495         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13496         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13497                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13498 /*
13499         EM_DEBUG_LOG(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
13500         EM_DEBUG_LOG(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
13501         EM_DEBUG_LOG(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
13502         EM_DEBUG_LOG(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
13503         EM_DEBUG_LOG(">>>>> meeting_req->location[%s]", meeting_req->location);
13504         EM_DEBUG_LOG(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
13505         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
13506         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
13507         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
13508         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
13509         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
13510         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
13511         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
13512 */
13513         int col_index = 0;
13514
13515         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
13516         temp_unix_time = timegm(&(meeting_req->start_time));
13517         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13518         temp_unix_time = timegm(&(meeting_req->end_time));
13519         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13520         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->location, 1, LOCATION_LEN_IN_MAIL_MEETING_TBL);
13521         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->global_object_id, 1, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
13522         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
13523         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.standard_name, 1, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
13524         temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
13525         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13526         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
13527         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.daylight_name, 1, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
13528         temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
13529         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13530         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
13531
13532
13533         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13534         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13535                 ("sqlite3_step fail:%d", rc));
13536         ret = true;
13537
13538
13539 FINISH_OFF:
13540         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13541
13542         if (hStmt != NULL)  {
13543                 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
13544
13545                 rc = sqlite3_finalize(hStmt);
13546                 if (rc != SQLITE_OK)  {
13547                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
13548
13549                         error = EMAIL_ERROR_DB_FAILURE;
13550                 }
13551         }
13552
13553         _DISCONNECT_DB;
13554
13555         if (err_code != NULL)
13556                 *err_code = error;
13557
13558         EM_DEBUG_FUNC_END("ret [%d]", ret);
13559         return ret;
13560 }
13561
13562 INTERNAL_FUNC int emstorage_delete_meeting_request(int account_id, int mail_id, int input_mailbox_id, int transaction, int *err_code)
13563 {
13564         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);
13565
13566         if (account_id < ALL_ACCOUNT || mail_id < 0) {
13567                 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d]", account_id, mail_id);
13568
13569                 if (err_code != NULL)
13570                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13571                 return false;
13572         }
13573
13574         int rc;
13575         int ret = false;
13576         int error = EMAIL_ERROR_NONE;
13577         int and = false;
13578         char sql_query_string[QUERY_SIZE] = {0, };
13579
13580         sqlite3 *local_db_handle = emstorage_get_db_connection();
13581         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13582
13583         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_meeting_tbl ");
13584
13585         if (account_id != ALL_ACCOUNT) {                /*  NOT '0' means a specific account. '0' means all account */
13586                 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);
13587                 and = true;
13588         }
13589         if (mail_id > 0) {
13590                 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);
13591                 and = true;
13592         }
13593         if (input_mailbox_id > 0) {             /*  0 means all mailbox_id */
13594                 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);
13595         }
13596
13597         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13598         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13599                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13600
13601         ret = true;
13602
13603 FINISH_OFF:
13604         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13605         _DISCONNECT_DB;
13606
13607         if (err_code)
13608                 *err_code = error;
13609
13610         EM_DEBUG_FUNC_END("ret [%d]", ret);
13611         return ret;
13612 }
13613
13614
13615 INTERNAL_FUNC void emstorage_free_meeting_request(email_meeting_request_t *meeting_req)
13616 {
13617         EM_DEBUG_FUNC_BEGIN();
13618
13619         if (!meeting_req ) {
13620                 EM_DEBUG_EXCEPTION("NULL PARAM");
13621                 return;
13622                 }
13623
13624         EM_SAFE_FREE(meeting_req->location);
13625         EM_SAFE_FREE(meeting_req->global_object_id);
13626
13627         EM_DEBUG_FUNC_END();
13628 }
13629
13630
13631 INTERNAL_FUNC int emstorage_get_overflowed_mail_id_list(int account_id, int input_mailbox_id, int mail_slot_size, int **mail_id_list, int *mail_id_count, int transaction, int *err_code)
13632 {
13633         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);
13634         EM_PROFILE_BEGIN(profile_emstorage_get_overflowed_mail_id_list);
13635         char sql_query_string[QUERY_SIZE] = {0, };
13636         char **result = NULL;
13637         int rc = -1, ret = false;
13638         int error = EMAIL_ERROR_NONE;
13639         int counter = 0, col_index = 0;
13640         int result_mail_id_count = 0;
13641         int *result_mail_id_list = NULL;
13642
13643         if (input_mailbox_id <= 0 || !mail_id_list || !mail_id_count || account_id < 1) {
13644                 EM_DEBUG_EXCEPTION("Invalid Parameter");
13645                 error = EMAIL_ERROR_INVALID_PARAM;
13646                 goto FINISH_OFF;
13647         }
13648
13649         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);
13650
13651         EM_DEBUG_LOG("query[%s].", sql_query_string);
13652
13653         sqlite3 *local_db_handle = emstorage_get_db_connection();
13654         EMSTORAGE_START_READ_TRANSACTION(transaction);
13655
13656         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_mail_id_count, 0, NULL), rc);
13657         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13658                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13659
13660         if (!result_mail_id_count) {
13661                 EM_DEBUG_LOG("No mail found...");
13662                 ret = false;
13663                 error= EMAIL_ERROR_MAIL_NOT_FOUND;
13664                 goto FINISH_OFF;
13665         }
13666
13667         EM_DEBUG_LOG("There are [%d] overflowed mails in mailbox_id [%d]", result_mail_id_count, input_mailbox_id);
13668
13669         if (!(result_mail_id_list = (int *)malloc(sizeof(int) * result_mail_id_count))) {
13670                 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
13671                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13672                 sqlite3_free_table(result);
13673                 goto FINISH_OFF;
13674         }
13675
13676         memset(result_mail_id_list, 0x00, sizeof(int) * result_mail_id_count);
13677
13678         col_index = 1;
13679
13680         for (counter = 0; counter < result_mail_id_count; counter++)
13681                 _get_table_field_data_int(result, result_mail_id_list + counter, col_index++);
13682
13683         ret = true;
13684
13685 FINISH_OFF:
13686         EM_DEBUG_LOG("finish off [%d]", ret);
13687
13688         if (result)
13689                 sqlite3_free_table(result);
13690
13691         if (ret == true)  {
13692                 *mail_id_list = result_mail_id_list;
13693                 *mail_id_count = result_mail_id_count;
13694         }
13695         else
13696                 EM_SAFE_FREE(result_mail_id_list);
13697
13698         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13699         _DISCONNECT_DB;
13700
13701         if (err_code != NULL)
13702                 *err_code = error;
13703
13704         EM_PROFILE_END(profile_emstorage_get_overflowed_mail_id_list);
13705         EM_DEBUG_FUNC_END("ret [%d]", ret);
13706         return ret;
13707 }
13708
13709
13710 INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(int mail_id, int *thread_id, int *err_code)
13711 {
13712         EM_DEBUG_FUNC_BEGIN("mail_id[%d], thread_id[%p], err_code[%p]", mail_id, thread_id, err_code);
13713
13714         int rc = -1, ret = false;
13715         int err = EMAIL_ERROR_NONE;
13716         char sql_query_string[QUERY_SIZE] = {0, };
13717         char **result;
13718         int result_count = 0;
13719
13720         if (mail_id == 0 || thread_id == NULL) {
13721                 EM_DEBUG_EXCEPTION("Invalid Parameter");
13722                 if (err_code != NULL)
13723                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13724                 goto FINISH_OFF;
13725         }
13726
13727         sqlite3 *local_db_handle = emstorage_get_db_connection();
13728
13729         memset(sql_query_string, 0, QUERY_SIZE);
13730         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT thread_id FROM mail_tbl WHERE mail_id = %d", mail_id);
13731
13732         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
13733         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
13734         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13735                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13736
13737         if (!result_count) {
13738                 EM_DEBUG_EXCEPTION("No mail found...");
13739                 ret = false;
13740                 err= EMAIL_ERROR_MAIL_NOT_FOUND;
13741                 /* sqlite3_free_table(result); */
13742                 goto FINISH_OFF;
13743         }
13744
13745         _get_table_field_data_int(result, thread_id, 1);
13746
13747         sqlite3_free_table(result);
13748
13749         ret = true;
13750
13751 FINISH_OFF:
13752
13753         _DISCONNECT_DB;
13754
13755         if (err_code != NULL)
13756                 *err_code = err;
13757
13758         EM_DEBUG_FUNC_END("ret [%d]", ret);
13759         return ret;
13760 }
13761
13762
13763 INTERNAL_FUNC int emstorage_update_latest_thread_mail(int account_id, int thread_id, int latest_mail_id, int thread_item_count, int transaction, int *err_code)
13764 {
13765         EM_DEBUG_FUNC_BEGIN("account_id [%d], thread_id[%d], latest_mail_id [%d], thread_item_count[%d], err_code[%p]", account_id, thread_id, latest_mail_id, thread_item_count, err_code);
13766
13767         int rc = -1, ret = false;
13768         int err = EMAIL_ERROR_NONE;
13769         char sql_query_string[QUERY_SIZE] = {0, };
13770         char **result = NULL;
13771         int result_count = 0;
13772
13773         if (thread_id == 0) {
13774                 EM_DEBUG_EXCEPTION("Invalid Parameter");
13775                 if (err_code != NULL)
13776                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13777                 goto FINISH_OFF;
13778         }
13779
13780         sqlite3 *local_db_handle = emstorage_get_db_connection();
13781
13782         if (thread_item_count == 0 || latest_mail_id == 0) {
13783                 memset(sql_query_string, 0, QUERY_SIZE);
13784                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id, count(*) FROM (SELECT account_id, mail_id, thread_id, mailbox_type FROM mail_tbl ORDER BY date_time) WHERE account_id = %d AND thread_id = %d AND mailbox_type NOT in (3,5,7)", account_id, thread_id);
13785
13786                 /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
13787                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
13788                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13789                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13790                 EM_DEBUG_LOG("result_count[%d]", result_count);
13791                 if (result_count == 0) {
13792                         EM_DEBUG_EXCEPTION("No mail found...");
13793                         ret = false;
13794                         err= EMAIL_ERROR_MAIL_NOT_FOUND;
13795                         sqlite3_free_table(result);
13796                         goto FINISH_OFF;
13797                 }
13798
13799                 _get_table_field_data_int(result, &latest_mail_id, 2);
13800                 _get_table_field_data_int(result, &thread_item_count, 3);
13801
13802                 EM_DEBUG_LOG("latest_mail_id[%d]", latest_mail_id);
13803                 EM_DEBUG_LOG("thread_item_count[%d]", thread_item_count);
13804
13805                 sqlite3_free_table(result);
13806         }
13807
13808         EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
13809
13810         /* if (thread_item_count > 1) */
13811         /* { */
13812                 memset(sql_query_string, 0, QUERY_SIZE);
13813                 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);
13814                 EM_DEBUG_LOG("query[%s]", sql_query_string);
13815
13816                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13817                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13818                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13819         /* } */
13820
13821         memset(sql_query_string, 0, QUERY_SIZE);
13822         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);
13823         EM_DEBUG_LOG("query[%s]", sql_query_string);
13824
13825         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13826                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13827                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13828
13829         ret = true;
13830
13831 FINISH_OFF:
13832
13833         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
13834
13835         _DISCONNECT_DB;
13836
13837         if (err_code != NULL)
13838                 *err_code = err;
13839
13840         EM_DEBUG_FUNC_END("ret [%d]", ret);
13841         return ret;
13842 }
13843
13844
13845 INTERNAL_FUNC void emstorage_flush_db_cache()
13846 {
13847         sqlite3_release_memory(-1);
13848 }
13849
13850 #ifdef __FEATURE_LOCAL_ACTIVITY__
13851 /**
13852   *     emstorage_add_activity - Add Email Local activity during OFFLINE mode
13853   *
13854   */
13855 INTERNAL_FUNC int emstorage_add_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
13856 {
13857         EM_DEBUG_FUNC_BEGIN();
13858
13859         EM_DEBUG_LOG(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
13860
13861         int rc = -1, ret = false;
13862         int error = EMAIL_ERROR_NONE;
13863         DB_STMT hStmt = NULL;
13864         char sql_query_string[8192] = { 0x00, };
13865         int i = 0;
13866
13867         if (!local_activity) {
13868                 EM_DEBUG_EXCEPTION(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
13869                 if (err_code != NULL)
13870                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13871                 return false;
13872         }
13873
13874         sqlite3 *local_db_handle = emstorage_get_db_connection();
13875         memset(sql_query_string, 0x00 , sizeof(sql_query_string));
13876         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13877
13878         SNPRINTF(sql_query_string, sizeof(sql_query_string), "INSERT INTO mail_local_activity_tbl VALUES (?, ?, ?, ?, ?, ?, ?)");
13879
13880         EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ] ", local_activity->activity_id);
13881         EM_DEBUG_LOG(">>>>> MAIL ID [ %d ] ", local_activity->mail_id);
13882         EM_DEBUG_LOG(">>>>> ACCOUNT ID [ %d ] ", local_activity->account_id);
13883         EM_DEBUG_LOG(">>>>> ACTIVITY TYPE [ %d ] ", local_activity->activity_type);
13884         EM_DEBUG_LOG(">>>>> SERVER MAIL ID [ %s ] ", local_activity->server_mailid);
13885         EM_DEBUG_LOG(">>>>> SOURCE MAILBOX [ %s ] ", local_activity->src_mbox);
13886         EM_DEBUG_LOG(">>>>> DEST MAILBOX   [ %s ] ", local_activity->dest_mbox);
13887
13888         EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
13889
13890
13891         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13892         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13893                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13894
13895         EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
13896
13897
13898
13899
13900         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
13901         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
13902         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
13903         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
13904         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->server_mailid, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
13905         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->src_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
13906         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->dest_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
13907
13908
13909         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13910
13911         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
13912                 ("sqlite3_step fail:%d", rc));
13913         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13914                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
13915
13916         ret = true;
13917
13918 FINISH_OFF:
13919
13920         if (hStmt != NULL)  {
13921                 rc = sqlite3_finalize(hStmt);
13922                 if (rc != SQLITE_OK)  {
13923                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
13924
13925                         error = EMAIL_ERROR_DB_FAILURE;
13926                 }
13927         }
13928         else {
13929                 EM_DEBUG_LOG(" >>>>>>>>>> hStmt is NULL!!!");
13930         }
13931
13932         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13933         _DISCONNECT_DB;
13934
13935
13936         if (err_code != NULL)
13937                 *err_code = error;
13938
13939         EM_DEBUG_FUNC_END("ret [%d]", ret);
13940         return ret;
13941 }
13942
13943 /**
13944   *     emstorage_get_activity - Get the Local activity Information
13945   *
13946   *
13947   */
13948 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)
13949 {
13950         EM_DEBUG_FUNC_BEGIN();
13951
13952         int i = 0, count = 0, rc = -1, ret = false;
13953         int error = EMAIL_ERROR_NONE;
13954         emstorage_activity_tbl_t *p_activity_tbl = NULL;
13955         char sql_query_string[1024] = {0x00, };
13956         char **result = NULL;
13957         int col_index ;
13958
13959         EM_IF_NULL_RETURN_VALUE(activity_list, false);
13960         EM_IF_NULL_RETURN_VALUE(select_num, false);
13961
13962
13963         if (!select_num || !activity_list || account_id <= 0 || activityid < 0) {
13964                 EM_DEBUG_LOG(" select_num[%p], activity_list[%p] account_id [%d] activityid [%d] ", select_num, activity_list, account_id, activityid);
13965                 if (err_code != NULL)
13966                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13967                 return false;
13968         }
13969
13970         EMSTORAGE_START_READ_TRANSACTION(transaction);
13971
13972         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13973
13974         if (activityid == ALL_ACTIVITIES) {
13975                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d order by activity_id", account_id);
13976         }
13977         else {
13978                 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);
13979         }
13980
13981         EM_DEBUG_LOG("Query [%s]", sql_query_string);
13982
13983
13984
13985         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
13986         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13987                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13988
13989
13990
13991         col_index = 7;
13992
13993         if (!(p_activity_tbl = (emstorage_activity_tbl_t*)em_malloc(sizeof(emstorage_activity_tbl_t) * count))) {
13994                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
13995                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13996                 goto FINISH_OFF;
13997         }
13998
13999
14000         for (i = 0; i < count; i++)  {
14001                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
14002                 if (result[col_index])
14003                         p_activity_tbl[i].activity_id = atoi(result[col_index++]);
14004
14005                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
14006                 if (result[col_index])
14007                         p_activity_tbl[i].account_id = atoi(result[col_index++]);
14008
14009                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
14010                 if (result[col_index])
14011                         p_activity_tbl[i].mail_id = atoi(result[col_index++]);
14012
14013                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
14014                 if (result[col_index])
14015                         p_activity_tbl[i].activity_type = atoi(result[col_index++]);
14016
14017
14018                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
14019                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index])>0)
14020                         p_activity_tbl[i].server_mailid = EM_SAFE_STRDUP(result[col_index++]);
14021                 else
14022                         col_index++;
14023
14024                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
14025                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index])>0)
14026                         p_activity_tbl[i].src_mbox = EM_SAFE_STRDUP(result[col_index++]);
14027                 else
14028                         col_index++;
14029
14030                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
14031                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index])>0)
14032                         p_activity_tbl[i].dest_mbox = EM_SAFE_STRDUP(result[col_index++]);
14033                 else
14034                         col_index++;
14035
14036         }
14037
14038         if (result)
14039                 sqlite3_free_table(result);
14040
14041         ret = true;
14042
14043 FINISH_OFF:
14044
14045
14046         if (ret == true)  {
14047                 *activity_list = p_activity_tbl;
14048                 *select_num = count;
14049                 EM_DEBUG_LOG(">>>> COUNT : %d >> ", count);
14050         }
14051         else if (p_activity_tbl != NULL) {
14052                 emstorage_free_local_activity(&p_activity_tbl, count, NULL);
14053         }
14054
14055         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14056         if (err_code != NULL)
14057                 *err_code = error;
14058
14059         EM_DEBUG_FUNC_END("ret [%d]", ret);
14060         return ret;
14061 }
14062
14063
14064 INTERNAL_FUNC int emstorage_get_next_activity_id(int *activity_id, int *err_code)
14065 {
14066
14067         EM_DEBUG_FUNC_BEGIN();
14068
14069         int ret = false;
14070         int err = EMAIL_ERROR_NONE;
14071         int rc = -1;
14072         char *sql = NULL;
14073         char **result = NULL;
14074
14075         if (NULL == activity_id)
14076         {
14077                 EM_DEBUG_EXCEPTION(" activity_id[%p]", activity_id);
14078
14079                 err = EMAIL_ERROR_INVALID_PARAM;
14080                 goto FINISH_OFF;
14081         }
14082
14083         /*  increase unique id */
14084
14085         sql = "SELECT max(rowid) FROM mail_local_activity_tbl;";
14086
14087         /*  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); */
14088         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14089                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
14090
14091         if (NULL==result[1])
14092                 rc = 1;
14093         else
14094                 rc = atoi(result[1])+1;
14095
14096         *activity_id = rc;
14097
14098         if (result)
14099                 sqlite3_free_table(result);
14100
14101         ret = true;
14102
14103         FINISH_OFF:
14104
14105         if (NULL != err_code) {
14106                 *err_code = err;
14107         }
14108
14109         EM_DEBUG_FUNC_END("ret [%d]", ret);
14110         return ret;
14111
14112 }
14113
14114 INTERNAL_FUNC int emstorage_get_activity_id_list(int account_id, int ** activity_id_list, int *activity_id_count, int lowest_activity_type, int highest_activity_type, int transaction, int *err_code)
14115 {
14116
14117         EM_DEBUG_FUNC_BEGIN();
14118
14119         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);
14120
14121         if (account_id <= 0|| NULL == activity_id_list || NULL == activity_id_count ||lowest_activity_type <=0 || highest_activity_type <= 0)  {
14122                 if (err_code != NULL)
14123                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14124                 return false;
14125         }
14126
14127         int ret = false;
14128         int error = EMAIL_ERROR_NONE;
14129         int i = 0, rc = -1, count = 0;
14130         char sql_query_string[1024] = {0x00, };
14131         int *activity_ids = NULL;
14132         int col_index = 0;
14133         char **result = NULL;
14134
14135         EMSTORAGE_START_READ_TRANSACTION(transaction);
14136
14137         memset(sql_query_string, 0x00, sizeof(sql_query_string));
14138
14139         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);
14140
14141         EM_DEBUG_LOG(" Query [%s]", sql_query_string);
14142
14143
14144         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
14145         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14146                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14147
14148         col_index = 1;
14149
14150         EM_DEBUG_LOG(" Activity COUNT : %d ... ", count);
14151
14152         if (NULL == (activity_ids = (int *)em_malloc(sizeof(int) * count))) {
14153                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
14154                 error = EMAIL_ERROR_OUT_OF_MEMORY;
14155                 goto FINISH_OFF;
14156         }
14157
14158         for (i = 0; i < count; i++)  {
14159                 activity_ids[i] = atoi(result[col_index]);
14160                 col_index++;
14161                 EM_DEBUG_LOG("activity_id %d", activity_ids[i]);
14162         }
14163
14164         ret = true;
14165
14166 FINISH_OFF:
14167
14168
14169         if (ret == true)  {
14170                 *activity_id_count = count;
14171                 *activity_id_list = activity_ids;
14172
14173         }
14174         else if (activity_ids != NULL) /* Prevent defect - 216566 */
14175                 EM_SAFE_FREE(activity_ids);
14176
14177
14178         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14179         if (err_code != NULL) {
14180                 *err_code = error;
14181         }
14182
14183         EM_DEBUG_FUNC_END("ret [%d]", ret);
14184         return ret;
14185 }
14186
14187 INTERNAL_FUNC int emstorage_free_activity_id_list(int *activity_id_list, int *error_code)
14188 {
14189         EM_DEBUG_FUNC_BEGIN();
14190
14191         int error = EMAIL_ERROR_NONE;
14192         int ret = false;
14193
14194         EM_DEBUG_LOG(" activity_id_list [%p]", activity_id_list);
14195
14196         if (NULL == activity_id_list) {
14197                 error = EMAIL_ERROR_INVALID_PARAM;
14198                 goto FINISH_OFF;
14199         }
14200         else {
14201                 free(activity_id_list);
14202                 activity_id_list = NULL;
14203         }
14204
14205
14206         ret= true;
14207
14208         FINISH_OFF:
14209
14210         if (NULL != error_code) {
14211                 *error_code = error;
14212         }
14213
14214         EM_DEBUG_FUNC_END("ret [%d]", ret);
14215         return ret;
14216 }
14217
14218 /**
14219  * emstorage_delete_local_activity - Deletes the Local acitivity Generated based on activity_type
14220  * or based on server mail id
14221  *
14222  */
14223 INTERNAL_FUNC int emstorage_delete_local_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
14224 {
14225         EM_DEBUG_FUNC_BEGIN();
14226
14227
14228         EM_DEBUG_LOG(" local_activity[%p] ", local_activity);
14229
14230         if (!local_activity)  {
14231                 EM_DEBUG_EXCEPTION(" local_activity[%p] ", local_activity);
14232                 if (err_code != NULL)
14233                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14234                 return false;
14235         }
14236
14237         int rc = -1, ret = false;                       /* Prevent_FIX  */
14238         int err = EMAIL_ERROR_NONE;
14239         int query_and = 0;
14240         int query_where = 0;
14241         char sql_query_string[8192] = { 0x00, };
14242         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
14243
14244         memset(sql_query_string, 0x00, sizeof(sql_query_string));
14245
14246         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_local_activity_tbl ");
14247
14248         EM_DEBUG_LOG(">>> Query [ %s ] ", sql_query_string);
14249
14250         if (local_activity->account_id) {
14251                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
14252                 " WHERE account_id = %d ", local_activity->account_id);
14253                 query_and = 1;
14254                 query_where = 1;
14255         }
14256
14257         if (local_activity->server_mailid) {
14258                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
14259                 " %s %s server_mailid = '%s' ", query_where? "": "WHERE", query_and? "AND":"", local_activity->server_mailid);
14260                 query_and = 1;
14261                 query_where = 1;
14262         }
14263
14264
14265         if (local_activity->mail_id) {
14266                 EM_DEBUG_LOG(">>>> MAIL ID [ %d ] , ACTIVITY TYPE [%d ]", local_activity->mail_id, local_activity->activity_type);
14267
14268                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
14269                 " %s %s mail_id = %d  ", query_where? "": "WHERE", query_and? "AND":"", local_activity->mail_id);
14270
14271                 query_and = 1;
14272                 query_where = 1;
14273
14274         }
14275
14276         if (local_activity->activity_type > 0) {
14277                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
14278                 " %s %s activity_type = %d ", query_where? "": "WHERE", query_and? "AND" : "" , local_activity->activity_type);
14279         }
14280
14281         if (local_activity->activity_id > 0) {
14282                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
14283                 " %s %s activity_id = %d ", query_where? "": "WHERE", query_and? "AND" : "" , local_activity->activity_id);
14284
14285         }
14286
14287         EM_DEBUG_LOG(">>>>> Query [ %s ] ", sql_query_string);
14288
14289         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
14290         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14291                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14292
14293         rc = sqlite3_changes(local_db_handle);
14294         if (rc == 0)  {
14295                 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
14296                 err = EMAIL_ERROR_MAILBOX_NOT_FOUND;
14297         }
14298
14299         ret = true;
14300
14301 FINISH_OFF:
14302         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
14303         if (err_code != NULL)
14304                 *err_code = err;
14305         EM_DEBUG_FUNC_END("ret [%d]", ret);
14306         return ret;
14307 }
14308
14309 /**
14310 *       emstorage_free_local_activity - Free the Local Activity data
14311 */
14312 INTERNAL_FUNC int emstorage_free_local_activity(emstorage_activity_tbl_t **local_activity_list, int count, int *err_code)
14313 {
14314         EM_DEBUG_FUNC_BEGIN();
14315
14316         EM_DEBUG_LOG(" local_activity_list[%p], count[%d], err_code[%p]", local_activity_list, count, err_code);
14317
14318         int ret = false;
14319         int error = EMAIL_ERROR_INVALID_PARAM;
14320
14321         if (count > 0) {
14322                 if (!local_activity_list || !*local_activity_list) {
14323                         EM_DEBUG_EXCEPTION(" local_activity_list[%p], count[%d]", local_activity_list, count);
14324
14325                         error = EMAIL_ERROR_INVALID_PARAM;
14326                         goto FINISH_OFF;
14327                 }
14328
14329                 emstorage_activity_tbl_t* p = *local_activity_list;
14330                 int i = 0;
14331                 if (p) {
14332                         for (; i < count; i++)  {
14333                                 EM_SAFE_FREE(p[i].dest_mbox);
14334                                 EM_SAFE_FREE(p[i].src_mbox);
14335                                 EM_SAFE_FREE(p[i].server_mailid);
14336                         }
14337
14338                         free(p);        *local_activity_list = NULL;
14339                 }
14340         }
14341
14342         ret = true;
14343
14344 FINISH_OFF:
14345         if (err_code != NULL)
14346                 *err_code = error;
14347
14348         EM_DEBUG_FUNC_END("ret [%d]", ret);
14349         return ret;
14350
14351 }
14352 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
14353
14354
14355 static int _get_key_value_string_for_list_filter_rule(email_list_filter_rule_t *input_list_filter_rule,char **output_key_value_string)
14356 {
14357         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_key_value_string [%p]", input_list_filter_rule, output_key_value_string);
14358
14359         int  ret = EMAIL_ERROR_NONE;
14360         char key_value_string[QUERY_SIZE] = { 0, };
14361         char *temp_key_value_1 = NULL;
14362         char *temp_key_value_2 = NULL;
14363
14364         if(input_list_filter_rule == NULL || output_key_value_string == NULL) {
14365                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14366                 return EMAIL_ERROR_INVALID_PARAM;
14367         }
14368
14369         switch(input_list_filter_rule->target_attribute) {
14370         case EMAIL_MAIL_ATTRIBUTE_MAIL_ID                 :
14371         case EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID              :
14372         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID              :
14373         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE            :
14374         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_STATUS      :
14375         case EMAIL_MAIL_ATTRIBUTE_REFERENCE_MAIL_ID       :
14376         case EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS    :
14377         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN         :
14378         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML          :
14379         case EMAIL_MAIL_ATTRIBUTE_FILE_SIZE               :
14380         case EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD        :
14381         case EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD     :
14382         case EMAIL_MAIL_ATTRIBUTE_FLAGS_FLAGGED_FIELD     :
14383         case EMAIL_MAIL_ATTRIBUTE_FLAGS_ANSWERED_FIELD    :
14384         case EMAIL_MAIL_ATTRIBUTE_FLAGS_RECENT_FIELD      :
14385         case EMAIL_MAIL_ATTRIBUTE_FLAGS_DRAFT_FIELD       :
14386         case EMAIL_MAIL_ATTRIBUTE_FLAGS_FORWARDED_FIELD   :
14387         case EMAIL_MAIL_ATTRIBUTE_DRM_STATUS              :
14388         case EMAIL_MAIL_ATTRIBUTE_PRIORITY                :
14389         case EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS             :
14390         case EMAIL_MAIL_ATTRIBUTE_LOCK_STATUS             :
14391         case EMAIL_MAIL_ATTRIBUTE_REPORT_STATUS           :
14392         case EMAIL_MAIL_ATTRIBUTE_ATTACHMENT_COUNT        :
14393         case EMAIL_MAIL_ATTRIBUTE_INLINE_CONTENT_COUNT    :
14394         case EMAIL_MAIL_ATTRIBUTE_THREAD_ID               :
14395         case EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT       :
14396         case EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS  :
14397         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_CLASS           :
14398         case EMAIL_MAIL_ATTRIBUTE_DIGEST_TYPE             :
14399         case EMAIL_MAIL_ATTRIBUTE_SMIME_TYPE              :
14400                 SNPRINTF(key_value_string, QUERY_SIZE, "%d", input_list_filter_rule->key_value.integer_type_value);
14401                 break;
14402
14403         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME            :
14404         case EMAIL_MAIL_ATTRIBUTE_SUBJECT                 :
14405         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME     :
14406         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID          :
14407         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID              :
14408         case EMAIL_MAIL_ATTRIBUTE_FROM                    :
14409         case EMAIL_MAIL_ATTRIBUTE_TO                      :
14410         case EMAIL_MAIL_ATTRIBUTE_CC                      :
14411         case EMAIL_MAIL_ATTRIBUTE_BCC                     :
14412         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT            :
14413                 if(input_list_filter_rule->key_value.string_type_value == NULL) {
14414                         EM_DEBUG_EXCEPTION("Invalid string_type_value [%p]", input_list_filter_rule->key_value.string_type_value);
14415                         ret = EMAIL_ERROR_INVALID_PARAM;
14416                         goto FINISH_OFF;
14417                 }
14418
14419                 temp_key_value_1 = input_list_filter_rule->key_value.string_type_value;
14420
14421                 temp_key_value_2 = em_replace_all_string(temp_key_value_1, "_", "\\_");
14422                 temp_key_value_1 = em_replace_all_string(temp_key_value_2, "%", "\\%");
14423
14424                 if(input_list_filter_rule->rule_type == EMAIL_LIST_FILTER_RULE_INCLUDE)
14425                         SNPRINTF(key_value_string, QUERY_SIZE, "\'%%%s%%\'", temp_key_value_1);
14426                 else
14427                         SNPRINTF(key_value_string, QUERY_SIZE, "\'%s\'", temp_key_value_1);
14428                 break;
14429
14430         case EMAIL_MAIL_ATTRIBUTE_DATE_TIME               :
14431                 SNPRINTF(key_value_string, QUERY_SIZE, "%d", (int)input_list_filter_rule->key_value.datetime_type_value);
14432                 break;
14433
14434         default :
14435                 ret = EMAIL_ERROR_INVALID_PARAM;
14436                 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
14437                 break;
14438         }
14439
14440         if(ret == EMAIL_ERROR_NONE && EM_SAFE_STRLEN(key_value_string) > 0) {
14441                 *output_key_value_string = strdup(key_value_string);
14442         }
14443
14444 FINISH_OFF:
14445
14446         EM_SAFE_FREE(temp_key_value_1);
14447         EM_SAFE_FREE(temp_key_value_2);
14448
14449         EM_DEBUG_FUNC_END("ret [%d]", ret);
14450         return ret;
14451 }
14452
14453 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14454 static int _get_cert_password_file_name(int index, char *cert_password_file_name)
14455 {
14456         EM_DEBUG_FUNC_BEGIN("index : [%d]", index);
14457
14458         if (index <= 0 || !cert_password_file_name) {
14459                 EM_DEBUG_EXCEPTION("Invalid parameter");
14460                 return EMAIL_ERROR_INVALID_PARAM;
14461         }
14462
14463         sprintf(cert_password_file_name, ".email_cert_%d", index);
14464
14465         EM_DEBUG_FUNC_END();
14466         return EMAIL_ERROR_NONE;
14467 }
14468 #endif
14469
14470 static int _make_filter_rule_string(email_list_filter_rule_t *input_list_filter_rule, char **output_string)
14471 {
14472         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
14473
14474         int   ret = EMAIL_ERROR_NONE;
14475         int   is_alpha = 0;
14476         int   length_field_name = 0;
14477         int   length_value = 0;
14478         char  result_rule_string[QUERY_SIZE] = { 0 , };
14479         char *mod_field_name_string = NULL;
14480         char *mod_value_string = NULL;
14481         char *temp_field_name_string = NULL;
14482         char *temp_key_value_string = NULL;
14483
14484         if(input_list_filter_rule == NULL || output_string == NULL) {
14485                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14486                 return  EMAIL_ERROR_INVALID_PARAM;
14487         }
14488
14489         temp_field_name_string = emcore_get_mail_field_name_by_attribute_type(input_list_filter_rule->target_attribute);
14490
14491         if(temp_field_name_string == NULL) {
14492                 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
14493                 return EMAIL_ERROR_INVALID_PARAM;
14494         }
14495
14496         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) {
14497                 EM_DEBUG_EXCEPTION("_get_key_value_string_for_list_filter_rule failed");
14498                 return EMAIL_ERROR_INVALID_PARAM;
14499         }
14500
14501         length_field_name = EM_SAFE_STRLEN(temp_field_name_string);
14502         length_value      = EM_SAFE_STRLEN(temp_key_value_string);
14503
14504         switch(input_list_filter_rule->target_attribute) {
14505         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME            :
14506         case EMAIL_MAIL_ATTRIBUTE_SUBJECT                 :
14507         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME     :
14508         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID          :
14509         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID              :
14510         case EMAIL_MAIL_ATTRIBUTE_FROM                    :
14511         case EMAIL_MAIL_ATTRIBUTE_TO                      :
14512         case EMAIL_MAIL_ATTRIBUTE_CC                      :
14513         case EMAIL_MAIL_ATTRIBUTE_BCC                     :
14514         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT            :
14515                 is_alpha = 1;
14516                 break;
14517         default :
14518                 is_alpha = 0;
14519                 break;
14520         }
14521
14522         if(is_alpha == 1 && input_list_filter_rule->case_sensitivity == false) {
14523                 length_field_name += strlen("UPPER() ");
14524                 length_value      += strlen("UPPER() ");
14525                 mod_field_name_string = em_malloc(sizeof(char) * length_field_name);
14526                 mod_value_string      = em_malloc(sizeof(char) * length_value);
14527                 SNPRINTF(mod_field_name_string, length_field_name, "UPPER(%s)", temp_field_name_string);
14528                 SNPRINTF(mod_value_string,      length_value, "UPPER(%s)", temp_key_value_string);
14529                 EM_SAFE_FREE(temp_key_value_string);
14530         }
14531         else {
14532                 mod_field_name_string = strdup(temp_field_name_string);
14533                 mod_value_string      = temp_key_value_string;
14534         }
14535
14536         switch (input_list_filter_rule->rule_type) {
14537         case EMAIL_LIST_FILTER_RULE_EQUAL     :
14538                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = %s ", mod_field_name_string, mod_value_string);
14539                 break;
14540         case EMAIL_LIST_FILTER_RULE_NOT_EQUAL :
14541                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s != %s ", mod_field_name_string, mod_value_string);
14542                 break;
14543         case EMAIL_LIST_FILTER_RULE_LESS_THAN :
14544                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s < %s ", mod_field_name_string, mod_value_string);
14545                 break;
14546         case EMAIL_LIST_FILTER_RULE_GREATER_THAN :
14547                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s > %s ", mod_field_name_string, mod_value_string);
14548                 break;
14549         case EMAIL_LIST_FILTER_RULE_LESS_THAN_OR_EQUAL :
14550                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s <= %s ", mod_field_name_string, mod_value_string);
14551                 break;
14552         case EMAIL_LIST_FILTER_RULE_GREATER_THAN_OR_EQUAL :
14553                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s >= %s ", mod_field_name_string, mod_value_string);
14554                 break;
14555         case EMAIL_LIST_FILTER_RULE_INCLUDE   :
14556                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s LIKE %s ", mod_field_name_string, mod_value_string);
14557                 break;
14558         case EMAIL_LIST_FILTER_RULE_IN        :
14559                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s IN (%s) ", mod_field_name_string, mod_value_string);
14560                 break;
14561         case EMAIL_LIST_FILTER_RULE_NOT_IN    :
14562                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s NOT IN (%s) ", mod_field_name_string, mod_value_string);
14563                 break;
14564         default :
14565                 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
14566                 ret = EMAIL_ERROR_INVALID_PARAM;
14567                 goto FINISH_OFF;
14568         }
14569
14570         *output_string = strdup(result_rule_string);
14571
14572 FINISH_OFF:
14573         EM_SAFE_FREE(mod_field_name_string);
14574         EM_SAFE_FREE(mod_value_string);
14575
14576         EM_DEBUG_FUNC_END("ret [%d]", ret);
14577         return ret;
14578 }
14579
14580 static int _make_order_rule_string(email_list_sorting_rule_t *input_sorting_rule, char **output_string) {
14581         EM_DEBUG_FUNC_BEGIN("input_sorting_rule [%p], output_string [%p]", input_sorting_rule, output_string);
14582
14583         char  result_rule_string[QUERY_SIZE] = { 0 , };
14584         int   ret = EMAIL_ERROR_NONE;
14585
14586         if(input_sorting_rule->force_boolean_check) {
14587                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = 0 ", emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
14588         }
14589         else
14590                 EM_SAFE_STRCPY(result_rule_string, emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
14591
14592         switch (input_sorting_rule->sort_order) {
14593                 case EMAIL_SORT_ORDER_ASCEND     :
14594                         EM_SAFE_STRCAT(result_rule_string, " ASC ");
14595                         break;
14596
14597                 case EMAIL_SORT_ORDER_DESCEND    :
14598                         EM_SAFE_STRCAT(result_rule_string, " DESC ");
14599                         break;
14600
14601                 default :
14602                         EM_DEBUG_EXCEPTION("Invalid sort_order [%d]", input_sorting_rule->sort_order);
14603                         ret = EMAIL_ERROR_INVALID_PARAM;
14604                         goto FINISH_OFF;
14605         }
14606
14607         *output_string = strdup(result_rule_string);
14608
14609 FINISH_OFF:
14610         EM_DEBUG_FUNC_END("ret [%d]", ret);
14611         return ret;
14612 }
14613
14614 INTERNAL_FUNC int emstorage_write_conditional_clause_for_getting_mail_list(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)
14615 {
14616         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);
14617         int ret = EMAIL_ERROR_NONE;
14618         int i = 0;
14619         int string_offset = 0;
14620         char conditional_clause_string[QUERY_SIZE] = {0, };
14621         char *result_string_for_a_item = NULL;
14622
14623         if ( (input_filter_count > 0 && !input_filter_list) || (input_sorting_rule_count > 0 && !input_sorting_rule_list) || output_conditional_clause == NULL) {
14624                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14625                 return EMAIL_ERROR_INVALID_PARAM;
14626         }
14627
14628         if(input_filter_count > 0) {
14629                 strcpy(conditional_clause_string,  " WHERE ");
14630
14631                 for ( i = 0; i < input_filter_count; i++) {
14632                         switch (input_filter_list[i].list_filter_item_type) {
14633                         case EMAIL_LIST_FILTER_ITEM_RULE :
14634                                 EM_DEBUG_LOG("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE", i);
14635                                 _make_filter_rule_string(&(input_filter_list[i].list_filter_item.rule), &result_string_for_a_item);
14636                                 break;
14637
14638                         case EMAIL_LIST_FILTER_ITEM_OPERATOR :
14639                                 EM_DEBUG_LOG("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_OPERATOR", i);
14640                                 switch(input_filter_list[i].list_filter_item.operator_type) {
14641                                 case EMAIL_LIST_FILTER_OPERATOR_AND :
14642                                         result_string_for_a_item = strdup("AND ");
14643                                         break;
14644                                 case EMAIL_LIST_FILTER_OPERATOR_OR :
14645                                         result_string_for_a_item = strdup("OR ");
14646                                         break;
14647                                 case EMAIL_LIST_FILTER_OPERATOR_LEFT_PARENTHESIS :
14648                                         result_string_for_a_item = strdup(" (");
14649                                         break;
14650                                 case EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS :
14651                                         result_string_for_a_item = strdup(") ");
14652                                         break;
14653                                 }
14654                                 break;
14655
14656                         default :
14657                                 EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", input_filter_list[i].list_filter_item_type);
14658                                 ret = EMAIL_ERROR_INVALID_PARAM;
14659                                 goto FINISH_OFF;
14660                         }
14661
14662                         if(result_string_for_a_item == NULL) {
14663                                 EM_DEBUG_EXCEPTION("result_string_for_a_item is null");
14664                                 ret = EMAIL_ERROR_INVALID_PARAM;
14665                                 goto FINISH_OFF;
14666                         }
14667
14668                         if(strlen(conditional_clause_string) + EM_SAFE_STRLEN(result_string_for_a_item) >= QUERY_SIZE) { /* prevent 34364 */
14669                                 EM_DEBUG_EXCEPTION("Query is too long");
14670                                 ret = EMAIL_ERROR_DATA_TOO_LONG;
14671                                 goto FINISH_OFF;
14672                         }
14673                         strcat(conditional_clause_string, result_string_for_a_item);
14674                         EM_SAFE_FREE(result_string_for_a_item);
14675                 }
14676         }
14677
14678         if(input_sorting_rule_count > 0) {
14679                 strcat(conditional_clause_string, "ORDER BY ");
14680
14681                 for ( i = 0; i < input_sorting_rule_count; i++) {
14682                         if( (ret = _make_order_rule_string(&input_sorting_rule_list[i], &result_string_for_a_item)) != EMAIL_ERROR_NONE) {
14683                                 EM_DEBUG_EXCEPTION("_make_order_rule_string failed. [%d]", ret);
14684                                 goto FINISH_OFF;
14685                         }
14686                         if(i > 0)
14687                                 strcat(conditional_clause_string, ", ");
14688                         strcat(conditional_clause_string, result_string_for_a_item);
14689                         EM_SAFE_FREE(result_string_for_a_item);
14690                 }
14691         }
14692
14693         if (input_start_index != -1 && input_limit_count != -1) {
14694                 string_offset = strlen(conditional_clause_string);
14695                 SNPRINTF_OFFSET(conditional_clause_string, string_offset, QUERY_SIZE, " LIMIT %d, %d", input_start_index, input_limit_count);
14696         }
14697
14698         *output_conditional_clause = strdup(conditional_clause_string);
14699
14700 FINISH_OFF:
14701         EM_SAFE_FREE(result_string_for_a_item);
14702
14703         EM_DEBUG_FUNC_END("ret [%d]", ret);
14704         return ret;
14705 }
14706
14707 INTERNAL_FUNC int emstorage_free_list_filter(email_list_filter_t **input_filter_list, int input_filter_count)
14708 {
14709         EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d]", input_filter_list, input_filter_count);
14710         int err = EMAIL_ERROR_NONE;
14711         int i = 0;
14712         email_list_filter_t *temp_filter_list = NULL;
14713
14714         EM_IF_NULL_RETURN_VALUE(input_filter_list, EMAIL_ERROR_INVALID_PARAM);
14715
14716         for ( i = 0; i < input_filter_count; i++) {
14717                 temp_filter_list = (*input_filter_list) + i;
14718                 if(temp_filter_list && temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE) {
14719                         switch(temp_filter_list->list_filter_item.rule.target_attribute) {
14720                         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME :
14721                         case EMAIL_MAIL_ATTRIBUTE_SUBJECT :
14722                         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME :
14723                         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID :
14724                         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID :
14725                         case EMAIL_MAIL_ATTRIBUTE_FROM :
14726                         case EMAIL_MAIL_ATTRIBUTE_TO :
14727                         case EMAIL_MAIL_ATTRIBUTE_CC :
14728                         case EMAIL_MAIL_ATTRIBUTE_BCC :
14729                         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN :
14730                         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML :
14731                         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT :
14732                                 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule.key_value.string_type_value);
14733                                 break;
14734                         default :
14735                                 break;
14736                         }
14737                 }
14738         }
14739
14740         free(*input_filter_list);
14741         *input_filter_list = NULL;
14742
14743         EM_DEBUG_FUNC_END("err [%d]", err);
14744         return err;
14745 }
14746
14747 INTERNAL_FUNC int emstorage_add_certificate(emstorage_certificate_tbl_t *certificate, int transaction, int *err_code)
14748 {
14749         EM_DEBUG_FUNC_BEGIN("certificate:[%p], transaction:[%d], err_code:[%p]", certificate, transaction, err_code);
14750
14751         if (!certificate) {
14752                 EM_DEBUG_EXCEPTION("certificate:[%p], transaction:[%d], err_code:[%p]", certificate, transaction, err_code);
14753                 if (err_code != NULL)
14754                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14755
14756                 return false;
14757         }
14758
14759         int rc = -1, ret = false;
14760         int error = EMAIL_ERROR_NONE;
14761         DB_STMT hStmt = NULL;
14762         char sql_query_string[QUERY_SIZE] = {0, };
14763 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14764         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
14765 #endif
14766         sqlite3 *local_db_handle = emstorage_get_db_connection();
14767
14768         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
14769
14770         char *sql = "SELECT max(rowid) FROM mail_certificate_tbl;";
14771         char **result = NULL;
14772
14773         /*  rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
14774         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
14775         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14776                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
14777
14778         if (NULL==result[1]) rc = 1;
14779         else rc = atoi(result[1])+1;
14780         sqlite3_free_table(result);
14781
14782         certificate->certificate_id = rc;
14783 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14784         if ((error = _get_cert_password_file_name(certificate->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
14785                 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
14786                 goto FINISH_OFF;
14787         }
14788 #endif
14789         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14790                 "INSERT INTO mail_certificate_tbl VALUES "
14791                 "(        "
14792                 "   ?  "                /* Index of certificate */
14793                 "  , ? "                /* Select the account */
14794                 "  , ? "                /* Year of issue */
14795                 "  , ? "                /* Month of issue */
14796                 "  , ? "                /* Day of issue */
14797                 "  , ? "                /* Year of expiration */
14798                 "  , ? "                /* Month of expiration */
14799                 "  , ? "                /* Day of expiration */
14800                 "  , ? "                /* Organization of issue */
14801                 "  , ? "                /* Email address */
14802                 "  , ? "                /* Subject of certificate */
14803                 "  , ? "                /* Name of saved certificate */
14804                 ") ");
14805
14806
14807         /*  rc = sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL); */
14808         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14809         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14810                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14811
14812         EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
14813         _bind_stmt_field_data_int(hStmt, CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->certificate_id);
14814         _bind_stmt_field_data_int(hStmt, ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_year);
14815         _bind_stmt_field_data_int(hStmt, ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_month);
14816         _bind_stmt_field_data_int(hStmt, ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_day);
14817         _bind_stmt_field_data_int(hStmt, EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_year);
14818         _bind_stmt_field_data_int(hStmt, EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_month);
14819         _bind_stmt_field_data_int(hStmt, EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_day);
14820         _bind_stmt_field_data_string(hStmt, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_organization_name, 0, ISSUE_ORGANIZATION_LEN_IN_MAIL_CERTIFICATE_TBL);
14821         _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->email_address, 0, EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL);
14822         _bind_stmt_field_data_string(hStmt, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->subject_str, 0, SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL);
14823         _bind_stmt_field_data_string(hStmt, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->filepath, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
14824         /*  rc = sqlite3_step(hStmt); */
14825         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14826
14827         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
14828                 ("sqlite3_step fail:%d", rc));
14829         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14830                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
14831 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14832         if (ssm_write_buffer(certificate->password, EM_SAFE_STRLEN(certificate->password), cert_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
14833                 EM_DEBUG_EXCEPTION("ssm_write_buffer failed - Private certificate password : [%s]", cert_password_file_name);
14834                 error = EMAIL_ERROR_SYSTEM_FAILURE;
14835                 goto FINISH_OFF;
14836         }
14837 #endif
14838         ret = true;
14839
14840         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
14841
14842         if (!emcore_notify_storage_event(NOTI_CERTIFICATE_ADD, certificate->certificate_id, 0, NULL, 0))
14843                 EM_DEBUG_EXCEPTION("emcore_notify_storage_event(NOTI_CERTIFICATE_ADD] : Notification failed");
14844
14845 FINISH_OFF:
14846
14847         if (hStmt != NULL)  {
14848                 rc = sqlite3_finalize(hStmt);
14849                 if (rc != SQLITE_OK)  {
14850                         EM_DEBUG_LOG("sqlite3_finalize failed - %d", rc);
14851                         error = EMAIL_ERROR_DB_FAILURE;
14852                 }
14853         }
14854         else
14855                 EM_DEBUG_LOG("hStmt is NULL!!!");
14856
14857         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
14858         _DISCONNECT_DB;
14859
14860         if (err_code != NULL)
14861                 *err_code = error;
14862
14863         EM_DEBUG_FUNC_END("ret [%d]", ret);
14864         return ret;
14865 }
14866
14867 INTERNAL_FUNC int emstorage_free_certificate(emstorage_certificate_tbl_t **certificate_list, int count, int *err_code)
14868 {
14869         EM_DEBUG_FUNC_BEGIN("certificate_list[%p], count[%d], err_code[%p]", certificate_list, count, err_code);
14870
14871         int ret = false;
14872         int error = EMAIL_ERROR_NONE;
14873
14874         if (count > 0)  {
14875                 if (!certificate_list || !*certificate_list)  {
14876                         EM_DEBUG_EXCEPTION("certificate_list[%p], count[%d]", certificate_list, count);
14877                         error = EMAIL_ERROR_INVALID_PARAM;
14878                         goto FINISH_OFF;
14879                 }
14880
14881                 emstorage_certificate_tbl_t *p = *certificate_list;
14882                 int i = 0;
14883
14884                 for (; i < count; i++)  {
14885                         EM_SAFE_FREE(p[i].issue_organization_name);
14886                         EM_SAFE_FREE(p[i].email_address);
14887                         EM_SAFE_FREE(p[i].subject_str);
14888                         EM_SAFE_FREE(p[i].filepath);
14889                         EM_SAFE_FREE(p[i].password);
14890                 }
14891
14892                 EM_SAFE_FREE(p);
14893                 *certificate_list = NULL;
14894         }
14895
14896         ret = true;
14897
14898 FINISH_OFF:
14899         if (err_code != NULL)
14900                 *err_code = error;
14901
14902         EM_DEBUG_FUNC_END("ret [%d]", ret);
14903         return ret;
14904 }
14905
14906 INTERNAL_FUNC int emstorage_get_certificate_list(int *select_num, emstorage_certificate_tbl_t **certificate_list, int transaction, int with_password, int *err_code)
14907 {
14908         EM_DEBUG_FUNC_BEGIN();
14909
14910         int i = 0, count = 0, rc = -1, ret = false;
14911         int error = EMAIL_ERROR_NONE;
14912         emstorage_certificate_tbl_t *p_data_tbl = NULL;
14913
14914         DB_STMT hStmt = NULL;
14915
14916         if (!select_num || !certificate_list)  {
14917                 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, certificate_list);
14918                 if (err_code != NULL)
14919                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14920                 return false;
14921         }
14922
14923         sqlite3 *local_db_handle = emstorage_get_db_connection();
14924         EMSTORAGE_START_READ_TRANSACTION(transaction);
14925
14926         char sql_query_string[QUERY_SIZE] = {0, };
14927         char *sql = "SELECT count(*) FROM mail_certificate_tbl;";
14928         char **result;
14929
14930         /*  rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
14931         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
14932         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14933                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
14934
14935         count = atoi(result[1]);
14936         sqlite3_free_table(result);
14937
14938         if (!count) {
14939                 EM_DEBUG_EXCEPTION("no account found...");
14940                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
14941                 ret = true;
14942                 goto FINISH_OFF;
14943         }
14944         EM_DEBUG_LOG("count = %d", rc);
14945         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_certificate_tbl ORDER BY account_id");
14946
14947         /*  rc = sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL);   */
14948         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14949
14950         EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
14951         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14952                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14953
14954         /*  rc = sqlite3_step(hStmt); */
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         if (rc == SQLITE_DONE)  {
14959                 EM_DEBUG_EXCEPTION("no account found...");
14960
14961                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
14962                 count = 0;
14963                 ret = true;
14964                 goto FINISH_OFF;
14965         }
14966
14967         if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t) * count)))  {
14968                 EM_DEBUG_EXCEPTION("malloc failed...");
14969                 error = EMAIL_ERROR_OUT_OF_MEMORY;
14970                 goto FINISH_OFF;
14971         }
14972         memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t) * count);
14973         for (i = 0; i < count; i++)  {
14974                 /*  get recordset */
14975                 _get_stmt_field_data_int(hStmt,  &(p_data_tbl[i].certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
14976                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
14977                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
14978                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
14979                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
14980                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
14981                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
14982                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
14983                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
14984                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
14985                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
14986                 if (with_password == true) {
14987 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14988                         /*  get password from the secure storage */
14989                         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
14990
14991                         EM_SAFE_FREE(p_data_tbl[i].password);
14992
14993                         /*  get password file name */
14994                         if ((error = _get_cert_password_file_name(p_data_tbl[i].certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
14995                                 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
14996                                 goto FINISH_OFF;
14997                         }
14998
14999                         /*  read password from secure storage */
15000                         if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl[i].password))) < 0) {
15001                                 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage() failed...");
15002                                 goto FINISH_OFF;
15003                         }
15004                         EM_DEBUG_LOG("recv_password_file_name[%s], password[%s]", cert_password_file_name,  p_data_tbl[i].password);
15005 #endif
15006                 }
15007
15008                 /*  rc = sqlite3_step(hStmt); */
15009                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15010                 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i,  rc);
15011                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15012                         ("sqlite3_step fail:%d", rc));
15013         }
15014
15015         ret = true;
15016
15017 FINISH_OFF:
15018         if (ret == true)  {
15019                 *certificate_list = p_data_tbl;
15020                 *select_num = count;
15021                 EM_DEBUG_LOG("COUNT : %d", count);
15022         }
15023         else if (p_data_tbl != NULL)
15024                 emstorage_free_certificate(&p_data_tbl, count, NULL);
15025         if (hStmt != NULL)  {
15026                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
15027
15028                 rc = sqlite3_finalize(hStmt);
15029                 hStmt = NULL;
15030                 if (rc != SQLITE_OK)  {
15031                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
15032                         error = EMAIL_ERROR_DB_FAILURE;
15033                 }
15034         }
15035
15036         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15037         _DISCONNECT_DB;
15038
15039         if (err_code != NULL)
15040                 *err_code = error;
15041
15042         EM_DEBUG_FUNC_END("ret [%d]", ret);
15043         return ret;
15044 }
15045
15046 INTERNAL_FUNC int emstorage_get_certificate_by_email_address(char *email_address, emstorage_certificate_tbl_t **certificate, int transaction, int with_password, int *err_code)
15047 {
15048         EM_DEBUG_FUNC_BEGIN("email_address[%s], certificate[%p], transaction[%d], err_code[%p]", email_address, certificate, transaction, err_code);
15049
15050         if (!certificate)  {
15051                 EM_DEBUG_EXCEPTION("email_address[%s], certificate[%p]", email_address, certificate);
15052                 if (err_code != NULL)
15053                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15054                 return false;
15055         }
15056
15057         int ret = false;
15058         int error = EMAIL_ERROR_NONE;
15059         emstorage_certificate_tbl_t *p_data_tbl = NULL;
15060         DB_STMT hStmt = NULL;
15061         char sql_query_string[QUERY_SIZE] = {0, };
15062         int rc = -1;
15063         int sql_len = 0;
15064 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15065         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
15066 #endif
15067         sqlite3 *local_db_handle = emstorage_get_db_connection();
15068         EMSTORAGE_START_READ_TRANSACTION(transaction);
15069
15070         /*  Make query string */
15071         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
15072         sql_len = EM_SAFE_STRLEN(sql_query_string);
15073
15074         /*  dummy value, FROM WHERE clause */
15075         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "* FROM mail_certificate_tbl WHERE email_address = '%s'", email_address);
15076
15077         /*  FROM clause */
15078         EM_DEBUG_LOG("query = [%s]", sql_query_string);
15079
15080         /*  execute a sql and count rows */
15081         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15082         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15083                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15084
15085         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15086         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15087                 ("sqlite3_step fail:%d", rc));
15088
15089         if (rc == SQLITE_DONE)  {
15090                 EM_DEBUG_EXCEPTION("no matched certificate found...");
15091                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
15092                 goto FINISH_OFF;
15093         }
15094
15095         /*  Assign query result to structure */
15096         if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t))))  {
15097                 EM_DEBUG_EXCEPTION("malloc failed...");
15098                 error = EMAIL_ERROR_OUT_OF_MEMORY;
15099                 goto FINISH_OFF;
15100         }
15101
15102         memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t));
15103         _get_stmt_field_data_int(hStmt,  &(p_data_tbl->certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
15104         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
15105         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
15106         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
15107         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
15108         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
15109         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
15110         _get_stmt_field_data_string(hStmt, &(p_data_tbl->issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
15111         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
15112         _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
15113         _get_stmt_field_data_string(hStmt, &(p_data_tbl->filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
15114
15115         if (with_password) {
15116 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15117                 /*  get password file name */
15118                 if ((error = _get_cert_password_file_name(p_data_tbl->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
15119                         EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
15120                         goto FINISH_OFF;
15121                 }
15122
15123                 /*  read password from secure storage */
15124                 if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl->password))) != EMAIL_ERROR_NONE) {
15125                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed...");
15126                         goto FINISH_OFF;
15127                 }
15128                 EM_DEBUG_LOG("cert_password_file_name[%s], password[%s]", cert_password_file_name,  p_data_tbl->password);
15129 #endif
15130         }
15131         ret = true;
15132
15133 FINISH_OFF:
15134         if (ret == true)
15135                 *certificate = p_data_tbl;
15136
15137         if (hStmt != NULL)  {
15138                 rc = sqlite3_finalize(hStmt);
15139                 if (rc != SQLITE_OK)  {
15140                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
15141                         error = EMAIL_ERROR_DB_FAILURE;
15142                 }
15143         }
15144
15145         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15146         _DISCONNECT_DB;
15147
15148         if (err_code != NULL)
15149                 *err_code = error;
15150
15151         EM_DEBUG_FUNC_END("ret [%d]", ret);
15152         return ret;
15153 }
15154
15155 INTERNAL_FUNC int emstorage_get_certificate_by_index(int index, emstorage_certificate_tbl_t **certificate, int transaction, int with_password, int *err_code)
15156 {
15157         EM_DEBUG_FUNC_BEGIN("index[%d], certificate[%p], transaction[%d], err_code[%p]", index, certificate, transaction, err_code);
15158
15159         if (!certificate)  {
15160                 EM_DEBUG_EXCEPTION("index[%d], account[%p]", index, certificate);
15161                 if (err_code != NULL)
15162                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15163                 return false;
15164         }
15165
15166         int ret = false;
15167         int error = EMAIL_ERROR_NONE;
15168         emstorage_certificate_tbl_t *p_data_tbl = NULL;
15169         DB_STMT hStmt = NULL;
15170         char sql_query_string[QUERY_SIZE] = {0, };
15171         int rc = -1;
15172         int sql_len = 0;
15173 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15174         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
15175 #endif
15176         sqlite3 *local_db_handle = emstorage_get_db_connection();
15177         EMSTORAGE_START_READ_TRANSACTION(transaction);
15178
15179         /*  Make query string */
15180         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
15181         sql_len = EM_SAFE_STRLEN(sql_query_string);
15182
15183         /*  dummy value, FROM WHERE clause */
15184         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "* FROM mail_certificate_tbl WHERE certificate_id = %d", index);
15185
15186         /*  FROM clause */
15187         EM_DEBUG_LOG("query = [%s]", sql_query_string);
15188
15189         /*  execute a sql and count rows */
15190         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15191         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15192                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15193
15194         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15195         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15196                 ("sqlite3_step fail:%d", rc));
15197
15198         if (rc == SQLITE_DONE)  {
15199                 EM_DEBUG_EXCEPTION("no matched certificate found...");
15200                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
15201                 goto FINISH_OFF;
15202         }
15203
15204         /*  Assign query result to structure */
15205         if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t))))  {
15206                 EM_DEBUG_EXCEPTION("malloc failed...");
15207                 error = EMAIL_ERROR_OUT_OF_MEMORY;
15208                 goto FINISH_OFF;
15209         }
15210
15211         memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t));
15212
15213         _get_stmt_field_data_int(hStmt,  &(p_data_tbl->certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
15214         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
15215         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
15216         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
15217         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
15218         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
15219         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
15220         _get_stmt_field_data_string(hStmt, &(p_data_tbl->issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
15221         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
15222         _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
15223         _get_stmt_field_data_string(hStmt, &(p_data_tbl->filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
15224
15225         if (with_password) {
15226 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15227                 /*  get password file name */
15228                 if ((error = _get_cert_password_file_name(p_data_tbl->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
15229                         EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
15230                         goto FINISH_OFF;
15231                 }
15232
15233                 /*  read password from secure storage */
15234                 if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl->password))) != EMAIL_ERROR_NONE) {
15235                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed...");
15236                         goto FINISH_OFF;
15237                 }
15238                 EM_DEBUG_LOG("cert_password_file_name[%s], password[%s]", cert_password_file_name,  p_data_tbl->password);
15239 #endif
15240         }
15241         ret = true;
15242
15243 FINISH_OFF:
15244         if (ret == true)
15245                 *certificate = p_data_tbl;
15246
15247         if (hStmt != NULL)  {
15248                 rc = sqlite3_finalize(hStmt);
15249                 if (rc != SQLITE_OK)  {
15250                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
15251                         error = EMAIL_ERROR_DB_FAILURE;
15252                 }
15253         }
15254
15255         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15256         _DISCONNECT_DB;
15257
15258         if (err_code != NULL)
15259                 *err_code = error;
15260
15261         EM_DEBUG_FUNC_END("ret [%d]", ret);
15262         return ret;
15263 }
15264
15265 INTERNAL_FUNC int emstorage_delete_certificate(int certificate_id, int transaction, int *err_code)
15266 {
15267         EM_DEBUG_FUNC_BEGIN("certificate_id[%d], transaction[%d], err_code[%p]", certificate_id, transaction, err_code);
15268
15269         if (certificate_id < 1)  {
15270                 EM_DEBUG_EXCEPTION(" certificate_id[%d]", certificate_id);
15271
15272                 if (err_code != NULL)
15273                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15274                 return false;
15275         }
15276
15277         int rc = -1, ret = false;
15278         int error = EMAIL_ERROR_NONE;
15279         sqlite3 *local_db_handle = emstorage_get_db_connection();
15280         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
15281
15282         /*  TODO : delete password files - file names can be obtained from db or a rule that makes a name */
15283         DB_STMT hStmt = NULL;
15284         char sql_query_string[QUERY_SIZE] = {0, };
15285 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15286         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
15287         /*  get password file name */
15288         if ((error = _get_cert_password_file_name(certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
15289                 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
15290                 goto FINISH_OFF;
15291         }
15292 #endif
15293         /*  delete from db */
15294         memset(sql_query_string, 0x00, sizeof(sql_query_string));
15295         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_certificate_tbl WHERE certificate_id = %d", certificate_id);
15296
15297         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
15298         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
15299                 ("sqlite3_exec fail:%d", rc));
15300         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15301                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15302
15303         /*  validate account existence */
15304         rc = sqlite3_changes(local_db_handle);
15305         if (rc == 0)  {
15306                 EM_DEBUG_EXCEPTION(" no matched certificate found...");
15307                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
15308                 goto FINISH_OFF;
15309         }
15310 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15311         /*  delete from secure storage */
15312         if (ssm_delete_file(cert_password_file_name,  SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
15313                 EM_DEBUG_EXCEPTION(" ssm_delete_file failed -cert password : file[%s]", cert_password_file_name);
15314                 error = EMAIL_ERROR_SYSTEM_FAILURE;
15315                 goto FINISH_OFF;
15316         }
15317 #endif
15318         ret = true;
15319
15320 FINISH_OFF:
15321
15322         if (hStmt != NULL)  {
15323                 rc = sqlite3_finalize(hStmt);
15324                 if (rc != SQLITE_OK)  {
15325                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
15326                         error = EMAIL_ERROR_DB_FAILURE;
15327                 }
15328         }
15329
15330         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
15331         _DISCONNECT_DB;
15332
15333         if (err_code != NULL)
15334                 *err_code = error;
15335
15336         EM_DEBUG_FUNC_END("ret [%d]", ret);
15337         return ret;
15338 }
15339
15340 INTERNAL_FUNC int emstorage_update_certificate(int certificate_id, emstorage_certificate_tbl_t *certificate, int transaction, int *err_code)
15341 {
15342         EM_DEBUG_FUNC_BEGIN("certificate_id[%d], certificate[%p], transaction[%d], err_code[%p]", certificate_id, certificate, transaction, err_code);
15343
15344         if (certificate_id < 1)  {
15345                 EM_DEBUG_EXCEPTION(" certificate_id[%d]", certificate_id);
15346
15347                 if (err_code != NULL)
15348                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15349                 return false;
15350         }
15351
15352         int error = EMAIL_ERROR_NONE;
15353         int rc, ret = false;
15354
15355         DB_STMT hStmt = NULL;
15356         char sql_query_string[QUERY_SIZE] = {0, };
15357 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15358         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
15359 #endif
15360         sqlite3 *local_db_handle = emstorage_get_db_connection();
15361         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
15362
15363         SNPRINTF(sql_query_string, sizeof(sql_query_string),
15364                 "UPDATE mail_certificate_tbl SET"
15365                 " issue_year = ?"
15366                 ", issue_month = ?"             /* Index of certificate */
15367                 ", issue_day = ?"               /* Select the account */
15368                 ", expiration_year = ?"         /* Year of issue */
15369                 ", expiration_month = ?"                /* Month of issue */
15370                 ", expiration_day = ?"          /* Day of issue */
15371                 ", issue_organization_name = ?"         /* Year of expiration */
15372                 ", email_address = ?"           /* Month of expiration */
15373                 ", subject_str = ?"             /* Day of expiration */
15374                 ", filepath = ?"                /* Organization of issue */
15375                 ", password = ?"
15376                 " WHERE certificate_id = ?");
15377
15378         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15379         EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
15380         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15381                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15382
15383         _bind_stmt_field_data_int(hStmt, ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_year);
15384         _bind_stmt_field_data_int(hStmt, ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_month);
15385         _bind_stmt_field_data_int(hStmt, ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_day);
15386         _bind_stmt_field_data_int(hStmt, EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_year);
15387         _bind_stmt_field_data_int(hStmt, EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_month);
15388         _bind_stmt_field_data_int(hStmt, EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_day);
15389         _bind_stmt_field_data_string(hStmt, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_organization_name, 0, ISSUE_ORGANIZATION_LEN_IN_MAIL_CERTIFICATE_TBL);
15390         _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->email_address, 0, EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL);
15391         _bind_stmt_field_data_string(hStmt, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->subject_str, 0, SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL);
15392         _bind_stmt_field_data_string(hStmt, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->filepath, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
15393
15394         /*  rc = sqlite3_step(hStmt); */
15395         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15396         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
15397                 ("sqlite3_step fail:%d", rc));
15398         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15399                 ("sqlite3_step fail:%d", rc));
15400
15401         /*  validate account existence */
15402         rc = sqlite3_changes(local_db_handle);
15403         if (rc == 0) {
15404                 EM_DEBUG_EXCEPTION(" no matched account found...");
15405
15406                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
15407                 goto FINISH_OFF;
15408         }
15409
15410 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15411         /*  get password file name */
15412         if ((error = _get_cert_password_file_name(certificate->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
15413                 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
15414                 goto FINISH_OFF;
15415         }
15416
15417         /*  save passwords to the secure storage */
15418         if (ssm_write_buffer(certificate->password, EM_SAFE_STRLEN(certificate->password), cert_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
15419                 EM_DEBUG_EXCEPTION("ssm_write_buffer failed - Private certificate password : [%s]", cert_password_file_name);
15420                 error = EMAIL_ERROR_SYSTEM_FAILURE;
15421                 goto FINISH_OFF;
15422         }
15423 #endif
15424         if (!emcore_notify_storage_event(NOTI_CERTIFICATE_UPDATE, certificate->certificate_id, 0, NULL, 0))
15425                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_CERTIFICATE_UPDATE] : Notification Failed >>> ");
15426
15427         ret = true;
15428
15429 FINISH_OFF:
15430
15431         if (hStmt != NULL)  {
15432                 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
15433
15434                 rc = sqlite3_finalize(hStmt);
15435                 if (rc != SQLITE_OK)  {
15436                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
15437                         error = EMAIL_ERROR_DB_FAILURE;
15438                 }
15439         }
15440
15441         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
15442
15443         _DISCONNECT_DB;
15444
15445         if (err_code != NULL)
15446                 *err_code = error;
15447
15448         EM_DEBUG_FUNC_END("ret [%d]", ret);
15449         return ret;
15450 }
15451
15452 /* Tasks --------------------------------------------------------------------------*/
15453 INTERNAL_FUNC int emstorage_add_task(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)
15454 {
15455         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);
15456         int ret = 0;
15457         int i = 0;
15458         int task_id = 0;
15459         int err = EMAIL_ERROR_NONE;
15460         int rc = -1;
15461         DB_STMT hStmt = NULL;
15462         char sql_query_string[QUERY_SIZE] = {0, };
15463         sqlite3 *local_db_handle = NULL;
15464         char *sql = "SELECT max(rowid) FROM mail_task_tbl;";
15465         char **result = NULL;
15466
15467         if (input_task_parameter == NULL || output_task_id == NULL)  {
15468                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15469                 err = EMAIL_ERROR_INVALID_PARAM;
15470                 goto FINISH_OFF;
15471         }
15472
15473         local_db_handle = emstorage_get_db_connection();
15474
15475         EMSTORAGE_START_WRITE_TRANSACTION(input_transaction, err);
15476
15477         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
15478         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
15479                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
15480
15481         if (NULL == result[1])
15482                 task_id = 1;
15483         else
15484                 task_id = atoi(result[1])+1;
15485
15486         *output_task_id = task_id;
15487
15488         sqlite3_free_table(result);
15489         result = NULL;
15490
15491         SNPRINTF(sql_query_string, sizeof(sql_query_string),
15492                 "INSERT INTO mail_task_tbl VALUES "
15493                 "(        "
15494                 "    ? "  /*   task_id */
15495                 "  , ? "  /*   task_type */
15496                 "  , ? "  /*   task_status */
15497                 "  , ? "  /*   task_priority */
15498                 "  , ? "  /*   task_parameter_length */
15499                 "  , ? "  /*   task_parameter */
15500                 "  , ? "  /*   date_time */
15501                 ") ");
15502
15503         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15504         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15505                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15506
15507         EM_DEBUG_LOG(">>>> SQL STMT [%s] ", sql_query_string);
15508
15509
15510         _bind_stmt_field_data_int(hStmt, i++, task_id);
15511         _bind_stmt_field_data_int(hStmt, i++, input_task_type);
15512         _bind_stmt_field_data_int(hStmt, i++, EMAIL_TASK_STATUS_WAIT);
15513         _bind_stmt_field_data_int(hStmt, i++, input_task_priority);
15514         _bind_stmt_field_data_int(hStmt, i++, input_task_parameter_length);
15515         _bind_stmt_field_data_blob(hStmt, i++, input_task_parameter, input_task_parameter_length);
15516         _bind_stmt_field_data_int(hStmt, i++, time(NULL));
15517
15518         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15519
15520         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {err = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
15521                 ("sqlite3_step fail:%d", rc));
15522         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15523                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
15524
15525         ret = (err == EMAIL_ERROR_NONE);
15526         EMSTORAGE_FINISH_WRITE_TRANSACTION(input_transaction, ret, err);
15527
15528 FINISH_OFF:
15529
15530         if (hStmt != NULL)  {
15531                 rc = sqlite3_finalize(hStmt);
15532                 if (rc != SQLITE_OK)  {
15533                         EM_DEBUG_LOG("sqlite3_finalize failed [%d]", rc);
15534                         err = EMAIL_ERROR_DB_FAILURE;
15535                 }
15536         }
15537         else
15538                 EM_DEBUG_LOG("hStmt is NULL!!!");
15539
15540         _DISCONNECT_DB;
15541
15542         EM_DEBUG_FUNC_END("err [%d]", err);
15543         return err;
15544 }
15545
15546 INTERNAL_FUNC int emstorage_delete_task(int task_id, int transaction)
15547 {
15548         EM_DEBUG_FUNC_BEGIN("task_id[%d], transaction[%d]", task_id, transaction);
15549         int rc, ret = false;
15550         int err = EMAIL_ERROR_NONE;
15551         char sql_query_string[QUERY_SIZE] = {0, };
15552         sqlite3 *local_db_handle = NULL;
15553
15554         if (task_id < 0)  {
15555                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15556                 err = EMAIL_ERROR_INVALID_PARAM;
15557                 goto FINISH_OFF;
15558         }
15559
15560         local_db_handle = emstorage_get_db_connection();
15561
15562         EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
15563
15564         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_task_tbl WHERE task_id = %d", task_id);
15565
15566         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
15567         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15568                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15569
15570         ret = true;
15571
15572 FINISH_OFF:
15573         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
15574         _DISCONNECT_DB;
15575
15576         EM_DEBUG_FUNC_END("err [%d]", err);
15577         return err;
15578 }
15579
15580 INTERNAL_FUNC int emstorage_update_task_status(int task_id, email_task_status_type_t task_status, int transaction)
15581 {
15582         EM_DEBUG_FUNC_BEGIN("task_id[%d] task_status[%d] transaction[%d]", task_id, task_status, transaction);
15583         int rc, ret = false;
15584         int err = EMAIL_ERROR_NONE;
15585         char sql_query_string[QUERY_SIZE] = {0, };
15586
15587         sqlite3 *local_db_handle = emstorage_get_db_connection();
15588
15589         EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
15590
15591         SNPRINTF(sql_query_string, sizeof(sql_query_string),
15592                 "UPDATE mail_task_tbl SET"
15593                 " task_status = %d"
15594                 " WHERE task_id = %d"
15595                 , task_status
15596                 , task_id);
15597         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
15598         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15599                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15600
15601         ret = true;
15602
15603 FINISH_OFF:
15604         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
15605         _DISCONNECT_DB;
15606
15607
15608         EM_DEBUG_FUNC_END("err [%d]", err);
15609         return err;
15610 }
15611
15612 INTERNAL_FUNC int emstorage_query_task(const char *input_conditional_clause, const char *input_ordering_clause, email_task_t **output_task_list, int *output_task_count)
15613 {
15614         EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_ordering_clause [%p], output_task_list[%p], output_task_count[%d]", input_conditional_clause, input_ordering_clause, output_task_list, output_task_count);
15615         int i = 0, count = 0, rc = -1;
15616         int cur_query = 0;
15617         int field_index = 0;
15618         int err = EMAIL_ERROR_NONE;
15619         email_task_t *task_item_from_tbl = NULL;
15620         char sql_query_string[QUERY_SIZE] = {0, };
15621         char *field_list = "task_id, task_type, task_status, task_priority, task_parameter_length, task_parameter ";
15622         char **result;
15623         sqlite3 *local_db_handle = NULL;
15624         DB_STMT hStmt = NULL;
15625
15626         EM_IF_NULL_RETURN_VALUE(input_conditional_clause, false);
15627         EM_IF_NULL_RETURN_VALUE(output_task_count, false);
15628
15629         local_db_handle = emstorage_get_db_connection();
15630
15631         SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, "SELECT COUNT(*) FROM mail_task_tbl %s", input_conditional_clause);
15632         EM_DEBUG_LOG("emstorage_query_mail_list : query[%s].", sql_query_string);
15633
15634         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
15635         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
15636                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15637
15638         count = atoi(result[1]);
15639         sqlite3_free_table(result);
15640
15641         EM_DEBUG_LOG("count = %d", rc);
15642
15643         if (count == 0) {
15644                 EM_DEBUG_EXCEPTION("no task found...");
15645                 err = EMAIL_ERROR_TASK_NOT_FOUND;
15646                 goto FINISH_OFF;
15647         }
15648
15649         SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, "SELECT %s FROM mail_task_tbl %s %s", field_list, input_conditional_clause, input_ordering_clause);
15650         EM_DEBUG_LOG("emstorage_query_mail_list : query[%s].", sql_query_string);
15651
15652         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15653
15654         EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
15655         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15656                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15657
15658         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15659         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15660                 ("sqlite3_step fail:%d", rc));
15661
15662         if (rc == SQLITE_DONE)  {
15663                 EM_DEBUG_EXCEPTION("no task found...");
15664                 err = EMAIL_ERROR_TASK_NOT_FOUND;
15665                 count = 0;
15666                 goto FINISH_OFF;
15667         }
15668
15669         if (!(task_item_from_tbl = (email_task_t*)em_malloc(sizeof(email_task_t) * count))) {
15670                 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
15671                 err = EMAIL_ERROR_OUT_OF_MEMORY;
15672                 goto FINISH_OFF;
15673         }
15674
15675         for (i = 0; i < count; i++)  {
15676                 /*  get recordset */
15677                 field_index = 0;
15678
15679                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_id), field_index++);
15680                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_type), field_index++);
15681                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_status), field_index++);
15682                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_priority), field_index++);
15683                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_parameter_length), field_index++);
15684                 _get_stmt_field_data_blob(hStmt, (void**)&(task_item_from_tbl[i].task_parameter), field_index++);
15685
15686                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15687                 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i,  rc);
15688                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15689                         ("sqlite3_step fail:%d", rc));
15690         }
15691
15692 FINISH_OFF:
15693         if (err == EMAIL_ERROR_NONE)  {
15694                 if (output_task_list)
15695                         *output_task_list = task_item_from_tbl;
15696                 *output_task_count = count;
15697         }
15698         else
15699                 EM_SAFE_FREE(task_item_from_tbl);
15700
15701         if (hStmt != NULL)  {
15702                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
15703                 rc = sqlite3_finalize(hStmt);
15704                 hStmt = NULL;
15705                 if (rc != SQLITE_OK)  {
15706                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
15707                         err = EMAIL_ERROR_DB_FAILURE;
15708                 }
15709         }
15710
15711         _DISCONNECT_DB;
15712
15713         EM_DEBUG_FUNC_END("err [%d]", err);
15714         return err;
15715 }
15716 /* Tasks --------------------------------------------------------------------------*/
15717
15718 /*EOF*/