apply removal of mailbox_name field
[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 %s", fields, input_conditional_clause, input_ordering_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 mailbox_id, char *server_mail_id, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
3056 {
3057         EM_DEBUG_FUNC_BEGIN("mailbox_id [%d], server_mail_id[%s], mail[%p], transaction[%d], err_code[%p]", mailbox_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 (mailbox_id == 0)
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 mailbox_id = %d", server_mail_id, mailbox_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
4593         int ret = false;
4594         int error = EMAIL_ERROR_NONE;
4595         char conditional_clause_string[QUERY_SIZE] = {0, };
4596
4597         if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list || !parent_mailbox_name)  {
4598                 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);
4599                 if (err_code != NULL)
4600                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4601                 return false;
4602         }
4603
4604         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d  AND UPPER(mailbox_name) LIKE UPPER('%s%%')", account_id, parent_mailbox_name);
4605         EM_DEBUG_LOG("conditional_clause_string", conditional_clause_string);
4606
4607         if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, " ORDER BY mailbox_name DESC ", 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
4608                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4609                 goto FINISH_OFF;
4610         }
4611
4612         ret = true;
4613
4614 FINISH_OFF:
4615
4616         if (err_code != NULL)
4617                 *err_code = error;
4618
4619         EM_DEBUG_FUNC_END("ret [%d]", ret);
4620         return ret;
4621 }
4622
4623 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)
4624 {
4625         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);
4626         if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list)  {
4627                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4628
4629                 if (err_code != NULL)
4630                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4631
4632                 return false;
4633         }
4634
4635         int ret = false;
4636         int error = EMAIL_ERROR_NONE;
4637         char conditional_clause_string[QUERY_SIZE] = {0, };
4638
4639         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND modifiable_yn = 0", account_id);
4640         EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
4641
4642         if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, " ORDER BY mailbox_name", 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
4643                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4644                 goto FINISH_OFF;
4645         }
4646
4647         ret = true;
4648
4649 FINISH_OFF:
4650
4651         if (err_code != NULL)
4652                 *err_code = error;
4653
4654         EM_DEBUG_FUNC_END("ret [%d]", ret);
4655         return ret;
4656 }
4657
4658 INTERNAL_FUNC int emstorage_stamp_last_sync_time_of_mailbox(int input_mailbox_id, int input_transaction)
4659 {
4660         EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_transaction [%d]", input_mailbox_id, input_transaction);
4661
4662         int      result_code = false;
4663         int      error = EMAIL_ERROR_NONE;
4664         int      rc;
4665         time_t   current_time = 0;
4666         char     sql_query_string[QUERY_SIZE] = {0, };
4667         sqlite3 *local_db_handle = NULL;
4668
4669         if (!input_mailbox_id )  {
4670                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4671                 return EMAIL_ERROR_INVALID_PARAM;
4672         }
4673
4674         time(&current_time);
4675
4676         local_db_handle = emstorage_get_db_connection();
4677
4678         EMSTORAGE_START_WRITE_TRANSACTION(input_transaction, error);
4679
4680         SNPRINTF(sql_query_string, sizeof(sql_query_string),
4681                 "UPDATE mail_box_tbl SET"
4682                 " last_sync_time = %d"
4683                 " WHERE mailbox_id = %d"
4684                 , (int)current_time
4685                 , input_mailbox_id);
4686
4687         EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
4688
4689         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
4690         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4691                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4692
4693 FINISH_OFF:
4694
4695         if(error == EMAIL_ERROR_NONE)
4696                 result_code = true;
4697
4698         EMSTORAGE_FINISH_WRITE_TRANSACTION(input_transaction, result_code, error);
4699         _DISCONNECT_DB;
4700
4701         EM_DEBUG_FUNC_END("error [%d]", error);
4702         return error;
4703 }
4704
4705 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)
4706 {
4707         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);
4708         EM_PROFILE_BEGIN(profile_emstorage_get_mailbox_by_name);
4709
4710         if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !result_mailbox)  {
4711                 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], mailbox_name[%s], result_mailbox[%p]", account_id, local_yn, mailbox_name, result_mailbox);
4712
4713                 if (err_code != NULL)
4714                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4715                 return false;
4716         }
4717
4718         int ret = false;
4719         int error = EMAIL_ERROR_NONE;
4720         int result_count = 0;
4721         char conditional_clause_string[QUERY_SIZE] = {0, };
4722         char *replaced_mailbox_name = NULL;
4723
4724         if (strstr(mailbox_name, "'")) {
4725                 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
4726                 EM_DEBUG_LOG("replaced_mailbox_name : [%s]", replaced_mailbox_name);
4727         } else {
4728                 replaced_mailbox_name = strdup(mailbox_name);
4729         }
4730
4731         if(strcmp(mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
4732                 if (!(*result_mailbox = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t))))  {
4733                         EM_DEBUG_EXCEPTION("malloc failed...");
4734                         error = EMAIL_ERROR_OUT_OF_MEMORY;
4735                         goto FINISH_OFF;
4736                 }
4737
4738                 (*result_mailbox)->mailbox_id                 = 0;
4739                 (*result_mailbox)->account_id                 = account_id;
4740                 (*result_mailbox)->local_yn                   = 1;
4741                 (*result_mailbox)->mailbox_name               = EM_SAFE_STRDUP(mailbox_name);
4742                 (*result_mailbox)->mailbox_type               = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
4743                 (*result_mailbox)->alias                      = EM_SAFE_STRDUP(mailbox_name);
4744                 (*result_mailbox)->deleted_flag               = 0;
4745                 (*result_mailbox)->modifiable_yn              = 1;
4746                 (*result_mailbox)->total_mail_count_on_server = 1;
4747                 (*result_mailbox)->has_archived_mails         = 0;
4748                 (*result_mailbox)->mail_slot_size             = 0x0FFFFFFF;
4749                 (*result_mailbox)->no_select                  = 0;
4750         }
4751         else {
4752
4753                 if (local_yn == -1)
4754                         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND mailbox_name = '%s'", account_id, replaced_mailbox_name);
4755                 else
4756                         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);
4757
4758                 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
4759
4760                 if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, "", 0, transaction, result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
4761                         EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4762                         goto FINISH_OFF;
4763                 }
4764         }
4765
4766         ret = true;
4767
4768 FINISH_OFF:
4769
4770         EM_SAFE_FREE(replaced_mailbox_name);
4771
4772         if (err_code != NULL)
4773                 *err_code = error;
4774
4775         EM_PROFILE_END(profile_emstorage_get_mailbox_by_name);
4776         EM_DEBUG_FUNC_END("ret [%d]", ret);
4777         return ret;
4778 }
4779
4780 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)
4781 {
4782         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);
4783
4784         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_USER_DEFINED) || !mailbox_name) {
4785
4786                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
4787
4788                 if (err_code != NULL)
4789                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4790                 return false;
4791         }
4792
4793         int rc, ret = false;
4794         int error = EMAIL_ERROR_NONE;
4795         emstorage_mailbox_tbl_t *p_data_tbl = NULL;
4796         emstorage_account_tbl_t *account = NULL;
4797         DB_STMT hStmt = NULL;
4798         char sql_query_string[QUERY_SIZE] = {0,};
4799         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 ";
4800
4801         sqlite3 *local_db_handle = emstorage_get_db_connection();
4802         EMSTORAGE_START_READ_TRANSACTION(transaction);
4803
4804         /* validate account */
4805         /*  Check whether the account exists. */
4806         if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
4807                 EM_DEBUG_EXCEPTION(" emstorage_get_account_by_id failed - %d", error);
4808                 goto FINISH_OFF;
4809         }
4810
4811         if (account)
4812                 emstorage_free_account(&account, 1, NULL);
4813
4814         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);
4815
4816         EM_DEBUG_LOG("query = [%s]", sql_query_string);
4817
4818
4819         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4820         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)));
4821
4822
4823         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4824         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
4825
4826         if (rc == SQLITE_DONE) {
4827                 EM_DEBUG_EXCEPTION(" no matched mailbox_name found...");
4828                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
4829                 goto FINISH_OFF;
4830         }
4831
4832
4833         if (!(p_data_tbl = (emstorage_mailbox_tbl_t*)malloc(sizeof(emstorage_mailbox_tbl_t))))  {
4834                 EM_DEBUG_EXCEPTION(" malloc failed...");
4835                 error = EMAIL_ERROR_OUT_OF_MEMORY;
4836                 goto FINISH_OFF;
4837         }
4838
4839         memset(p_data_tbl, 0x00, sizeof(emstorage_mailbox_tbl_t));
4840
4841         int col_index = 0;
4842
4843         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
4844         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
4845         _get_stmt_field_data_int(hStmt, &(p_data_tbl->local_yn), col_index++);
4846         _get_stmt_field_data_string(hStmt, &(p_data_tbl->mailbox_name), 0, col_index++);
4847         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->mailbox_type), col_index++);
4848         _get_stmt_field_data_string(hStmt, &(p_data_tbl->alias), 0, col_index++);
4849         _get_stmt_field_data_int(hStmt, &(p_data_tbl->deleted_flag), col_index++);
4850         _get_stmt_field_data_int(hStmt, &(p_data_tbl->modifiable_yn), col_index++);
4851         _get_stmt_field_data_int(hStmt, &(p_data_tbl->total_mail_count_on_server), col_index++);
4852         _get_stmt_field_data_int(hStmt, &(p_data_tbl->has_archived_mails), col_index++);
4853         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_slot_size), col_index++);
4854         _get_stmt_field_data_int(hStmt, &(p_data_tbl->no_select), col_index++);
4855         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->last_sync_time), col_index++);
4856
4857         ret = true;
4858
4859 FINISH_OFF:
4860         if (ret == true)
4861                 *mailbox_name = p_data_tbl;
4862
4863         if (hStmt != NULL)  {
4864                 rc = sqlite3_finalize(hStmt);
4865                 if (rc != SQLITE_OK)  {
4866                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
4867                         error = EMAIL_ERROR_DB_FAILURE;
4868                 }
4869         }
4870
4871         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4872         _DISCONNECT_DB;
4873
4874         if (err_code != NULL)
4875                 *err_code = error;
4876
4877         EM_DEBUG_FUNC_END("ret [%d]", ret);
4878         return ret;
4879 }
4880
4881 INTERNAL_FUNC int emstorage_get_mailbox_by_id(int input_mailbox_id, emstorage_mailbox_tbl_t** output_mailbox)
4882 {
4883         EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_mailbox[%p]", input_mailbox_id, output_mailbox);
4884
4885         if (input_mailbox_id <= 0 || !output_mailbox)  {
4886                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4887                 return  EMAIL_ERROR_INVALID_PARAM;
4888         }
4889
4890         int  ret = EMAIL_ERROR_NONE;
4891         int  result_count = 0;
4892         char conditional_clause_string[QUERY_SIZE] = {0, };
4893
4894         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE mailbox_id = %d", input_mailbox_id);
4895
4896         EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
4897
4898         if( (ret = emstorage_query_mailbox_tbl(conditional_clause_string, "", false, false, output_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
4899                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", ret);
4900                 goto FINISH_OFF;
4901         }
4902
4903 FINISH_OFF:
4904
4905         EM_DEBUG_FUNC_END("ret [%d]", ret);
4906         return ret;
4907 }
4908
4909 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)
4910 {
4911         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);
4912         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_id)  {
4913                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_id[%p]", account_id, mailbox_type, mailbox_id);
4914                 if (err_code != NULL)
4915                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4916                 return false;
4917         }
4918
4919         int rc, ret = false;
4920         int error = EMAIL_ERROR_NONE;
4921         emstorage_account_tbl_t* account = NULL;
4922         DB_STMT hStmt = NULL;
4923         char sql_query_string[QUERY_SIZE] = {0, };
4924
4925         sqlite3 *local_db_handle = emstorage_get_db_connection();
4926         EMSTORAGE_START_READ_TRANSACTION(transaction);
4927
4928         /*  Check whether the account exists. */
4929         if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
4930                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
4931                 goto FINISH_OFF;
4932         }
4933
4934         if (account )
4935                 emstorage_free_account(&account, 1, NULL);
4936
4937         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);
4938
4939         EM_DEBUG_LOG("query = [%s]", sql_query_string);
4940
4941
4942         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4943
4944         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4945                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4946
4947
4948         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4949         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4950                 ("sqlite3_step fail:%d", rc));
4951
4952         if (rc == SQLITE_DONE) {
4953                 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
4954                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
4955                 goto FINISH_OFF;
4956         }
4957
4958         _get_stmt_field_data_int(hStmt, mailbox_id, 0);
4959
4960         ret = true;
4961
4962 FINISH_OFF:
4963         if (hStmt != NULL)  {
4964                 rc = sqlite3_finalize(hStmt);
4965                 if (rc != SQLITE_OK)  {
4966                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
4967                         error = EMAIL_ERROR_DB_FAILURE;
4968                 }
4969         }
4970
4971         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4972         _DISCONNECT_DB;
4973
4974         if (err_code != NULL)
4975                 *err_code = error;
4976
4977         EM_DEBUG_FUNC_END("ret [%d]", ret);
4978         return ret;
4979 }
4980
4981 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)
4982 {
4983         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);
4984         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_name)  {
4985                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
4986                 if (err_code != NULL)
4987                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4988                 return false;
4989         }
4990
4991         int rc, ret = false;
4992         int error = EMAIL_ERROR_NONE;
4993         emstorage_account_tbl_t* account = NULL;
4994         DB_STMT hStmt = NULL;
4995         char sql_query_string[QUERY_SIZE] = {0, };
4996
4997         sqlite3 *local_db_handle = emstorage_get_db_connection();
4998         EMSTORAGE_START_READ_TRANSACTION(transaction);
4999
5000         /*  Check whether the account exists. */
5001         if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
5002                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5003                 goto FINISH_OFF;
5004         }
5005
5006         if (account )
5007                 emstorage_free_account(&account, 1, NULL);
5008
5009         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);
5010
5011         EM_DEBUG_LOG("query = [%s]", sql_query_string);
5012
5013
5014         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5015
5016         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5017                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5018
5019
5020         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5021         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5022                 ("sqlite3_step fail:%d", rc));
5023
5024         if (rc == SQLITE_DONE) {
5025                 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5026                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5027                 goto FINISH_OFF;
5028         }
5029
5030         _get_stmt_field_data_string(hStmt, mailbox_name, 0, 0);
5031
5032         ret = true;
5033
5034 FINISH_OFF:
5035         if (hStmt != NULL)  {
5036                 rc = sqlite3_finalize(hStmt);
5037                 if (rc != SQLITE_OK)  {
5038                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5039                         error = EMAIL_ERROR_DB_FAILURE;
5040                 }
5041         }
5042
5043         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5044         _DISCONNECT_DB;
5045
5046         if (err_code != NULL)
5047                 *err_code = error;
5048
5049         EM_DEBUG_FUNC_END("ret [%d]", ret);
5050         return ret;
5051 }
5052
5053 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)
5054 {
5055         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);
5056         int rc, ret = false;
5057         int error = EMAIL_ERROR_NONE;
5058         char sql_query_string[QUERY_SIZE] = {0, };
5059         char *replaced_mailbox_name = NULL;
5060
5061         if (mailbox_name) {     
5062                 if (strstr(mailbox_name, "'")) {
5063                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5064                 } else {
5065                         replaced_mailbox_name = strdup(mailbox_name);
5066                 }
5067         }
5068
5069         sqlite3 *local_db_handle = emstorage_get_db_connection();
5070
5071         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5072
5073         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5074                 "UPDATE mail_box_tbl SET"
5075                 " modifiable_yn = %d"
5076                 " WHERE account_id = %d"
5077                 " AND local_yn = %d"
5078                 " AND mailbox_name = '%s'"
5079                 , modifiable_yn
5080                 , account_id
5081                 , local_yn
5082                 , replaced_mailbox_name);
5083         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5084         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5085                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5086
5087         ret = true;
5088
5089 FINISH_OFF:
5090         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5091         _DISCONNECT_DB;
5092
5093         EM_SAFE_FREE(replaced_mailbox_name);
5094
5095         if (err_code != NULL)
5096                 *err_code = error;
5097
5098         EM_DEBUG_FUNC_END("ret [%d]", ret);
5099         return ret;
5100
5101 }
5102
5103
5104 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)
5105 {
5106         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);
5107         int rc, ret = false;
5108         int error = EMAIL_ERROR_NONE;
5109         char sql_query_string[QUERY_SIZE] = {0, };
5110
5111         if (account_id <= 0 || input_mailbox_id <= 0)  {
5112                 EM_DEBUG_EXCEPTION("account_id[%d], input_mailbox_id[%d]", account_id, input_mailbox_id);
5113
5114                 if (err_code != NULL)
5115                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5116                 return false;
5117         }
5118         sqlite3 *local_db_handle = emstorage_get_db_connection();
5119
5120         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5121
5122         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5123                 "UPDATE mail_box_tbl SET"
5124                 " total_mail_count_on_server = %d"
5125                 " WHERE account_id = %d"
5126                 " AND mailbox_id = %d"
5127                 , total_count_on_server
5128                 , account_id
5129                 , input_mailbox_id);
5130         EM_DEBUG_LOG("query[%s]", sql_query_string);
5131         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5132         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5133                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5134
5135         ret = true;
5136
5137 FINISH_OFF:
5138         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5139         _DISCONNECT_DB;
5140
5141         if (err_code != NULL)
5142                 *err_code = error;
5143
5144         EM_DEBUG_FUNC_END("ret [%d]", ret);
5145         return ret;
5146
5147 }
5148
5149
5150 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)
5151 {
5152         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);
5153
5154         if (account_id < FIRST_ACCOUNT_ID || input_mailbox_id <= 0 || !result_mailbox)  {
5155                 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);
5156
5157                 if (err_code != NULL)
5158                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5159                 return false;
5160         }
5161
5162         int rc, ret = false;
5163         int error = EMAIL_ERROR_NONE;
5164         char sql_query_string[QUERY_SIZE] = {0, };
5165         DB_STMT hStmt = NULL;
5166         int i = 0;
5167
5168         sqlite3 *local_db_handle = emstorage_get_db_connection();
5169         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5170
5171         if (local_yn != -1) {
5172                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5173                         "UPDATE mail_box_tbl SET"
5174                         "  mailbox_id = ?"
5175                         ", mailbox_name = ?"
5176                         ", mailbox_type = ?"
5177                         ", alias = ?"
5178                         ", deleted_flag = ?"
5179                         ", modifiable_yn= ?"
5180                         ", mail_slot_size= ?"
5181                         ", total_mail_count_on_server = ?"
5182                         " WHERE account_id = %d"
5183                         " AND local_yn = %d"
5184                         " AND mailbox_id = '%d'"
5185                         , account_id
5186                         , local_yn
5187                         , input_mailbox_id);
5188         }
5189         else {
5190                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5191                         "UPDATE mail_box_tbl SET"
5192                         "  mailbox_id = ?"
5193                         ", mailbox_name = ?"
5194                         ", mailbox_type = ?"
5195                         ", alias = ?"
5196                         ", deleted_flag = ?"
5197                         ", modifiable_yn= ?"
5198                         ", mail_slot_size= ?"
5199                         ", total_mail_count_on_server = ?"
5200                         " WHERE account_id = %d"
5201                         " AND mailbox_id = '%d'"
5202                         , account_id
5203                         , input_mailbox_id);
5204         }
5205
5206
5207
5208         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5209         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5210                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5211
5212         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_id);
5213         _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);
5214         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_type);
5215         _bind_stmt_field_data_string(hStmt, i++, (char *)result_mailbox->alias ? result_mailbox->alias : "", 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
5216         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->deleted_flag);
5217         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->modifiable_yn);
5218         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mail_slot_size);
5219         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->total_mail_count_on_server);
5220
5221
5222         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5223         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
5224                 ("sqlite3_step fail:%d", rc));
5225         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5226                 ("sqlite3_step fail:%d", rc));
5227
5228         ret = true;
5229
5230 FINISH_OFF:
5231         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5232         _DISCONNECT_DB;
5233
5234         if (hStmt != NULL)  {
5235                 rc = sqlite3_finalize(hStmt);
5236                 if (rc != SQLITE_OK)  {
5237                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5238                         error = EMAIL_ERROR_DB_FAILURE;
5239                 }
5240         }
5241
5242         if (err_code != NULL)
5243                 *err_code = error;
5244
5245         EM_DEBUG_FUNC_END("ret [%d]", ret);
5246         return ret;
5247 }
5248
5249
5250 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)
5251 {
5252         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);
5253
5254         if (account_id < FIRST_ACCOUNT_ID || !mailbox_name)  {
5255                 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], mailbox_name[%s]", account_id, local_yn, mailbox_name);
5256
5257                 if (err_code != NULL)
5258                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5259                 return false;
5260         }
5261
5262         int rc, ret = false;
5263         int error = EMAIL_ERROR_NONE;
5264         char sql_query_string[QUERY_SIZE] = {0, };
5265         char *replaced_mailbox_name = NULL;
5266
5267         if (strstr(mailbox_name, "'")) {
5268                 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5269         } else {
5270                 replaced_mailbox_name = strdup(mailbox_name);
5271         }
5272
5273         EM_DEBUG_LOG("replaced_mailbox_name : [%s]", replaced_mailbox_name);
5274
5275         sqlite3 *local_db_handle = emstorage_get_db_connection();
5276         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5277
5278         EM_DEBUG_LOG("emstorage_update_mailbox_type");
5279
5280         DB_STMT hStmt_box_tbl = NULL;
5281         DB_STMT hStmt_mail_tbl = NULL;
5282         int i = 0;
5283
5284         /*  Update mail_box_tbl */
5285         if (local_yn != -1) {
5286                 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
5287                         "UPDATE mail_box_tbl SET"
5288                         " mailbox_type = ?"
5289                         " WHERE account_id = %d"
5290                         " AND local_yn = %d"
5291                         " AND mailbox_name = '%s'"
5292                         , account_id
5293                         , local_yn
5294                         , replaced_mailbox_name);
5295         }
5296         else {
5297                 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
5298                         "UPDATE mail_box_tbl SET"
5299                         " mailbox_type = ?"
5300                         " WHERE account_id = %d"
5301                         " AND mailbox_name = '%s'"
5302                         , account_id
5303                         , replaced_mailbox_name);
5304         }
5305
5306         EM_DEBUG_LOG("SQL(%s)", sql_query_string);
5307
5308         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_box_tbl, NULL), rc);
5309
5310         if(SQLITE_OK != rc) {
5311                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
5312                 error = EMAIL_ERROR_DB_FAILURE;
5313                 goto FINISH_OFF; 
5314         }
5315
5316         _bind_stmt_field_data_int(hStmt_box_tbl, i++, new_mailbox_type);
5317
5318
5319         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_box_tbl), rc);
5320
5321         if(rc == SQLITE_FULL) {
5322                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
5323                 error   = EMAIL_ERROR_MAIL_MEMORY_FULL;
5324                 goto FINISH_OFF; 
5325         }
5326         
5327         if(rc != SQLITE_ROW && rc != SQLITE_DONE) {
5328                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
5329                 error = EMAIL_ERROR_DB_FAILURE;
5330                 goto FINISH_OFF; 
5331         }
5332         
5333
5334         /*  Update mail_tbl */
5335         i = 0;
5336         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5337                         "UPDATE mail_tbl SET"
5338                         " mailbox_type = ?"
5339                         " WHERE account_id = %d"
5340                         " AND mailbox_name = '%s'"
5341                         , account_id
5342                         , replaced_mailbox_name);
5343
5344         EM_DEBUG_LOG("SQL[%s]", sql_query_string);
5345
5346         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_mail_tbl, NULL), rc);
5347         if(SQLITE_OK != rc) {
5348                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
5349                 error = EMAIL_ERROR_DB_FAILURE;
5350                 goto FINISH_OFF; 
5351         }
5352         
5353         _bind_stmt_field_data_int(hStmt_mail_tbl, i++, new_mailbox_type);
5354
5355         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_mail_tbl), rc);
5356         if(rc == SQLITE_FULL) {
5357                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
5358                 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
5359                 goto FINISH_OFF; 
5360         }
5361         
5362         if(rc != SQLITE_ROW && rc != SQLITE_DONE){
5363                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
5364                 error = EMAIL_ERROR_DB_FAILURE;
5365                 goto FINISH_OFF; 
5366         }
5367
5368         ret = true;
5369
5370 FINISH_OFF:
5371         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5372         _DISCONNECT_DB;
5373
5374         EM_SAFE_FREE(replaced_mailbox_name);
5375
5376         if (hStmt_box_tbl != NULL)  {
5377                 rc = sqlite3_finalize(hStmt_box_tbl);
5378                 if (rc != SQLITE_OK)  {
5379                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5380                         error = EMAIL_ERROR_DB_FAILURE;
5381                 }
5382         }
5383
5384         if (hStmt_mail_tbl != NULL)  {
5385                 rc = sqlite3_finalize(hStmt_mail_tbl);
5386                 if (rc != SQLITE_OK)  {
5387                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5388                         error = EMAIL_ERROR_DB_FAILURE;
5389                 }
5390         }
5391
5392         if (err_code != NULL)
5393                 *err_code = error;
5394
5395         EM_DEBUG_FUNC_END("ret [%d]", ret);
5396         return ret;
5397 }
5398
5399 INTERNAL_FUNC int emstorage_set_local_mailbox(int input_mailbox_id, int input_is_local_mailbox, int transaction)
5400 {
5401         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);
5402
5403         int rc, ret = false;
5404         int error = EMAIL_ERROR_NONE;
5405         char sql_query_string[QUERY_SIZE] = {0, };
5406
5407         if (input_mailbox_id < 0)  {
5408                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5409                 return EMAIL_ERROR_INVALID_PARAM;
5410         }
5411
5412         sqlite3 *local_db_handle = emstorage_get_db_connection();
5413
5414         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5415
5416         EM_DEBUG_LOG("emstorage_update_mailbox_type");
5417
5418         DB_STMT hStmt = NULL;
5419         int i = 0;
5420
5421         /*  Update mail_box_tbl */
5422         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5423                 "UPDATE mail_box_tbl SET"
5424                 " local_yn = ?"
5425                 " WHERE mailbox_id = %d"
5426                 , input_mailbox_id);
5427
5428         EM_DEBUG_LOG("SQL(%s)", sql_query_string);
5429
5430         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5431         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5432                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5433
5434         _bind_stmt_field_data_int(hStmt, i++, input_is_local_mailbox);
5435
5436         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5437         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
5438                 ("sqlite3_step fail:%d", rc));
5439         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5440                 ("sqlite3_step fail:%d", rc));
5441
5442         if (hStmt != NULL)  {
5443                 rc = sqlite3_finalize(hStmt);
5444                 if (rc != SQLITE_OK)  {
5445                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5446                         error = EMAIL_ERROR_DB_FAILURE;
5447                 }
5448                 hStmt = NULL;
5449         }
5450
5451         ret = true;
5452
5453 FINISH_OFF:
5454         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5455         _DISCONNECT_DB;
5456
5457         if (hStmt != NULL)  {
5458                 rc = sqlite3_finalize(hStmt);
5459                 if (rc != SQLITE_OK)  {
5460                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5461                         error = EMAIL_ERROR_DB_FAILURE;
5462                 }
5463         }
5464
5465         EM_DEBUG_FUNC_END("error [%d]", error);
5466         return error;
5467 }
5468
5469 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)
5470 {
5471         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);
5472         int i = 0;
5473         int err = EMAIL_ERROR_NONE;
5474         int rc = 0;
5475         int result = false;
5476         int cur_mailbox_id_string = 0;
5477         int mailbox_id_string_buffer_length = 0;
5478         char  sql_query_string[QUERY_SIZE] = {0, };
5479         char *mailbox_id_string_buffer = NULL;
5480         char *parameter_string = NULL;
5481         sqlite3 *local_db_handle = NULL;
5482
5483         if (input_mailbox_id_array == NULL || input_mailbox_id_count == 0 || input_field_name == NULL) {
5484                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5485                 return EMAIL_ERROR_INVALID_PARAM;
5486         }
5487
5488         local_db_handle = emstorage_get_db_connection();
5489
5490         /* Generating mail id list string */
5491         mailbox_id_string_buffer_length = MAILBOX_ID_STRING_LENGTH * input_mailbox_id_count;
5492
5493         mailbox_id_string_buffer = em_malloc(mailbox_id_string_buffer_length);
5494
5495         if(!mailbox_id_string_buffer) {
5496                 EM_DEBUG_EXCEPTION("em_malloc failed");
5497                 err = EMAIL_ERROR_OUT_OF_MEMORY;
5498                 goto FINISH_OFF;
5499         }
5500
5501         for(i = 0; i < input_mailbox_id_count; i++)
5502                 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]);
5503
5504         if(EM_SAFE_STRLEN(mailbox_id_string_buffer) > 1)
5505                 mailbox_id_string_buffer[EM_SAFE_STRLEN(mailbox_id_string_buffer) - 1] = NULL_CHAR;
5506
5507         /* Generating notification parameter string */
5508         parameter_string = em_malloc(mailbox_id_string_buffer_length + EM_SAFE_STRLEN(input_field_name) + 2);
5509
5510         if(!parameter_string) {
5511                 EM_DEBUG_EXCEPTION("em_malloc failed");
5512                 err = EMAIL_ERROR_OUT_OF_MEMORY;
5513                 goto FINISH_OFF;
5514         }
5515
5516         SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", input_field_name, 0x01, mailbox_id_string_buffer);
5517
5518         /* Write query string */
5519         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);
5520
5521         EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
5522
5523         /* Execute query */
5524         EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
5525         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5526         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5527                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5528         if (sqlite3_changes(local_db_handle) == 0)
5529                 EM_DEBUG_LOG("no mail matched...");
5530
5531         result = true;
5532
5533 FINISH_OFF:
5534         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, result, err);
5535         _DISCONNECT_DB;
5536
5537         if (result && parameter_string && !emcore_notify_storage_event(NOTI_MAILBOX_FIELD_UPDATE, input_account_id, 0, parameter_string, input_value))
5538                 EM_DEBUG_EXCEPTION("emcore_notify_storage_event failed : NOTI_MAILBOX_FIELD_UPDATE [%s,%d]", input_field_name, input_value);
5539
5540         EM_SAFE_FREE(mailbox_id_string_buffer);
5541         EM_SAFE_FREE(parameter_string);
5542
5543         EM_DEBUG_FUNC_END("err [%d]", err);
5544         return err;
5545 }
5546
5547 INTERNAL_FUNC int emstorage_add_mailbox(emstorage_mailbox_tbl_t* mailbox_tbl, int transaction, int *err_code)
5548 {
5549         EM_DEBUG_FUNC_BEGIN("mailbox_tbl[%p], transaction[%d], err_code[%p]", mailbox_tbl, transaction, err_code);
5550
5551         if (!mailbox_tbl)  {
5552                 if (err_code != NULL)
5553                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5554                 return false;
5555         }
5556
5557         int rc, ret = false;
5558         int error = EMAIL_ERROR_NONE;
5559         DB_STMT hStmt = NULL;
5560         char sql_query_string[QUERY_SIZE] = {0,};
5561         char **result = NULL;
5562         time_t current_time;
5563         sqlite3 *local_db_handle = emstorage_get_db_connection();
5564         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5565
5566         EM_SAFE_STRCPY(sql_query_string, "SELECT max(rowid) FROM mail_box_tbl;");
5567
5568         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5569         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
5570                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5571
5572         time(&current_time);
5573
5574         if (NULL == result[1])
5575                 rc = 1;
5576         else
5577                 rc = atoi(result[1]) + 1;
5578         sqlite3_free_table(result);
5579
5580         memset(sql_query_string, 0, sizeof(char) * QUERY_SIZE);
5581
5582         mailbox_tbl->mailbox_id = rc;
5583
5584         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5585                 "INSERT INTO mail_box_tbl VALUES "
5586                 "( ?"    /* mailbox_id */
5587                 ", ?"    /* account_id */
5588                 ", ?"    /* local_yn */
5589                 ", ?"    /* mailbox_name */
5590                 ", ?"    /* mailbox_type */
5591                 ", ?"    /* alias */
5592                 ", ?"    /* deleted_flag */
5593                 ", ?"    /* modifiable_yn */
5594                 ", ?"    /* total_mail_count_on_server */
5595                 ", ?"    /* has_archived_mails */
5596                 ", ?"    /* mail_slot_size */
5597                 ", ?"    /* no_select */
5598                 ", ? )");/* last_sync_time */
5599
5600
5601         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5602         EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
5603         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5604                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5605
5606         int col_index = 0;
5607
5608         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_id);
5609         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->account_id);
5610         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->local_yn);
5611         _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
5612         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_type);
5613         _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->alias, 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
5614         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->deleted_flag);
5615         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->modifiable_yn);
5616         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->total_mail_count_on_server);
5617         _bind_stmt_field_data_int(hStmt, col_index++, 0);
5618         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mail_slot_size);
5619         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->no_select);
5620         _bind_stmt_field_data_int(hStmt, col_index++, current_time);
5621
5622
5623         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5624         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5625                 ("sqlite3_step fail:%dn", rc));
5626
5627         ret = true;
5628
5629 FINISH_OFF:
5630         if (hStmt != NULL)  {
5631                 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
5632
5633                 rc = sqlite3_finalize(hStmt);
5634                 if (rc != SQLITE_OK)  {
5635                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
5636                         error = EMAIL_ERROR_DB_FAILURE;
5637                 }
5638         }
5639
5640         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5641         _DISCONNECT_DB;
5642
5643         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))
5644                 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
5645
5646         if (err_code != NULL)
5647                 *err_code = error;
5648
5649         EM_DEBUG_FUNC_END("ret [%d]", ret);
5650         return ret;
5651 }
5652
5653 INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(int account_id, int modifiable_yn, int transaction, int *err_code)
5654 {
5655         EM_DEBUG_FUNC_BEGIN("account_id[%d], modifiable_yn[%d], err_code[%p]", account_id, modifiable_yn, err_code);
5656
5657         if (account_id < FIRST_ACCOUNT_ID)  {
5658
5659                 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
5660
5661                 if (err_code != NULL)
5662                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5663                 return false;
5664         }
5665         int rc, ret = false;
5666         int error = EMAIL_ERROR_NONE;
5667         char sql_query_string[QUERY_SIZE] = {0,};
5668
5669         sqlite3 *local_db_handle = emstorage_get_db_connection();
5670         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5671
5672
5673         SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET modifiable_yn = %d WHERE account_id = %d", modifiable_yn, account_id);
5674
5675         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5676         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5677                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5678
5679         rc = sqlite3_changes(local_db_handle);
5680         if (rc == 0)
5681                 EM_DEBUG_EXCEPTION("All mailbox_name modifiable_yn set to 0 already");
5682
5683
5684         ret = true;
5685
5686 FINISH_OFF:
5687         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5688         _DISCONNECT_DB;
5689
5690         if (err_code != NULL)
5691                 *err_code = error;
5692         EM_DEBUG_FUNC_END("ret [%d]", ret);
5693         return ret;
5694
5695
5696 }
5697
5698 INTERNAL_FUNC int emstorage_delete_mailbox(int account_id, int local_yn, int input_mailbox_id, int transaction, int *err_code)
5699 {
5700         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);
5701
5702         if (account_id < FIRST_ACCOUNT_ID)  {
5703                 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
5704
5705                 if (err_code != NULL)
5706                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5707                 return false;
5708         }
5709
5710         int rc, ret = false;
5711         int error = EMAIL_ERROR_NONE;
5712         char sql_query_string[QUERY_SIZE] = {0, };
5713
5714         sqlite3 *local_db_handle = emstorage_get_db_connection();
5715         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5716
5717         if (local_yn == -1)
5718                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_box_tbl WHERE account_id = %d ", account_id);
5719         else
5720                 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);
5721
5722         if (input_mailbox_id > 0)  {            /* 0 means all mailbox */
5723                 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);
5724         }
5725
5726         EM_DEBUG_LOG("mailbox sql_query_string [%s]", sql_query_string);
5727         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5728         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5729                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5730
5731         rc = sqlite3_changes(local_db_handle);
5732         if (rc == 0)  {
5733                 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
5734                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5735                 ret = true;
5736         }
5737         ret = true;
5738
5739 FINISH_OFF:
5740
5741         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5742         _DISCONNECT_DB;
5743
5744         if(error == EMAIL_ERROR_NONE) {
5745                 if (!emcore_notify_storage_event(NOTI_MAILBOX_DELETE, account_id, input_mailbox_id, NULL, 0))
5746                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
5747         }
5748
5749         if (err_code != NULL)
5750                 *err_code = error;
5751
5752         EM_DEBUG_FUNC_END("ret [%d]", ret);
5753         return ret;
5754 }
5755
5756 INTERNAL_FUNC int emstorage_free_mailbox(emstorage_mailbox_tbl_t** mailbox_list, int count, int *err_code)
5757 {
5758         EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d], err_code[%p]", mailbox_list, count, err_code);
5759
5760         int ret = false;
5761         int error = EMAIL_ERROR_NONE;
5762
5763         if (count > 0)  {
5764                 if (!mailbox_list || !*mailbox_list)  {
5765                         EM_DEBUG_EXCEPTION(" mailbox_list[%p], count[%d]", mailbox_list, count);
5766
5767                         error = EMAIL_ERROR_INVALID_PARAM;
5768                         goto FINISH_OFF;
5769                 }
5770
5771                 emstorage_mailbox_tbl_t* p = *mailbox_list;
5772                 int i = 0;
5773
5774                 for (; i < count; i++) {
5775                         EM_SAFE_FREE(p[i].mailbox_name);
5776                         EM_SAFE_FREE(p[i].alias);
5777                 }
5778
5779                 EM_SAFE_FREE(p); *mailbox_list = NULL;
5780         }
5781
5782         ret = true;
5783
5784 FINISH_OFF:
5785         if (err_code != NULL)
5786                 *err_code = error;
5787
5788         EM_DEBUG_FUNC_END("ret [%d]", ret);
5789         return ret;
5790 }
5791
5792 INTERNAL_FUNC int emstorage_get_count_read_mail_uid(int account_id, char *mailbox_name, int *count, int transaction, int *err_code)
5793 {
5794         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);
5795
5796         if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !count)  {
5797                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], count[%p], exist[%p]", account_id, mailbox_name, count);
5798
5799                 if (err_code != NULL)
5800                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5801                 return false;
5802         }
5803
5804         int rc = -1, ret = false;
5805         int error = EMAIL_ERROR_NONE;
5806         char sql_query_string[QUERY_SIZE] = {0, };
5807         char *replaced_mailbox_name = NULL;
5808
5809         if (strstr(mailbox_name, "'")) {
5810                 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5811         } else {
5812                 replaced_mailbox_name = EM_SAFE_STRDUP(mailbox_name);
5813         }
5814
5815         EM_DEBUG_LOG("replaced_mailbox_name : [%s]", replaced_mailbox_name);
5816
5817
5818         sqlite3 *local_db_handle = emstorage_get_db_connection();
5819         EMSTORAGE_START_READ_TRANSACTION(transaction);
5820         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);
5821         EM_DEBUG_LOG(">>> SQL [ %s ] ", sql_query_string);
5822
5823         char **result;
5824
5825         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5826         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
5827                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5828
5829         *count = atoi(result[1]);
5830         sqlite3_free_table(result);
5831
5832         ret = true;
5833
5834 FINISH_OFF:
5835         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5836         _DISCONNECT_DB;
5837
5838         EM_SAFE_FREE(replaced_mailbox_name);
5839
5840         if (err_code != NULL)
5841                 *err_code = error;
5842
5843         EM_DEBUG_FUNC_END("ret [%d]", ret);
5844         return ret;
5845 }
5846
5847
5848
5849 INTERNAL_FUNC int emstorage_check_read_mail_uid(int account_id, char *mailbox_name, char *uid, int *exist, int transaction, int *err_code)
5850 {
5851         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);
5852
5853         if (account_id < FIRST_ACCOUNT_ID || !uid || !exist)  {
5854                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], uid[%p], exist[%p]", account_id, mailbox_name , uid, exist);
5855
5856                 if (err_code != NULL)
5857                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5858                 return false;
5859         }
5860
5861         int rc = -1, ret = false;
5862         int error = EMAIL_ERROR_NONE;
5863         char sql_query_string[QUERY_SIZE] = {0, };
5864         char *replaced_mailbox_name = NULL;
5865
5866         EM_DEBUG_LOG("replaced_mailbox_name : [%s]", replaced_mailbox_name);
5867
5868         sqlite3 *local_db_handle = emstorage_get_db_connection();
5869         EMSTORAGE_START_READ_TRANSACTION(transaction);
5870
5871         if (mailbox_name)  {
5872                 if (strstr(mailbox_name, "'")) {
5873                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5874                 } else {
5875                         replaced_mailbox_name = strdup(mailbox_name);
5876                 }
5877
5878                 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);
5879         }
5880         else  {
5881                 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);
5882         }
5883
5884         char **result;
5885
5886         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5887         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
5888                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5889
5890         *exist = atoi(result[1]);
5891         sqlite3_free_table(result);
5892
5893         if (*exist > 0)
5894                 *exist = 1;
5895         else
5896                 *exist = 0;
5897
5898         ret = true;
5899
5900 FINISH_OFF:
5901         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5902         _DISCONNECT_DB;
5903
5904         EM_SAFE_FREE(replaced_mailbox_name);
5905
5906         if (err_code != NULL)
5907                 *err_code = error;
5908
5909         EM_DEBUG_FUNC_END("ret [%d]", ret);
5910         return ret;
5911 }
5912
5913 INTERNAL_FUNC int emstorage_get_downloaded_mail(int mail_id, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
5914 {
5915         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], err_code[%p]", mail_id, mail, err_code);
5916
5917         if (!mail || mail_id <= 0) {
5918                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
5919                 if (err_code != NULL)
5920                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5921                 return false;
5922         }
5923
5924         int rc, ret = false, temp_rule;
5925         int error = EMAIL_ERROR_NONE;
5926         DB_STMT hStmt = NULL;
5927         char sql_query_string[QUERY_SIZE] = {0, };
5928
5929         sqlite3 *local_db_handle = emstorage_get_db_connection();
5930         EMSTORAGE_START_READ_TRANSACTION(transaction);
5931
5932         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE local_uid = %d", mail_id);
5933
5934         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5935         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
5936
5937         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5938                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5939
5940
5941         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5942         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5943                 ("sqlite3_step fail:%d", rc));
5944
5945         *mail = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
5946         if (*mail == NULL) {
5947                 error = EMAIL_ERROR_OUT_OF_MEMORY;
5948                 EM_DEBUG_EXCEPTION("Memory allocation for mail failed.");
5949                 goto FINISH_OFF;
5950
5951         }
5952         memset(*mail, 0x00, sizeof(emstorage_mail_tbl_t));
5953
5954         _get_stmt_field_data_int(hStmt, &((*mail)->account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5955         _get_stmt_field_data_int(hStmt, &((*mail)->mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5956         _get_stmt_field_data_int(hStmt, &((*mail)->mail_id), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5957         _get_stmt_field_data_string(hStmt, &((*mail)->server_mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5958         _get_stmt_field_data_string(hStmt, &((*mail)->server_mail_id), 0, S_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5959         _get_stmt_field_data_int(hStmt, &((*mail)->mail_size), DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5960         _get_stmt_field_data_int(hStmt, &temp_rule, FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5961
5962         (*mail)->server_mail_status = 1;
5963
5964         ret = true;
5965
5966 FINISH_OFF:
5967         if (hStmt != NULL)  {
5968                 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
5969                 rc = sqlite3_finalize(hStmt);
5970                 if (rc != SQLITE_OK)  {
5971                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5972                         error = EMAIL_ERROR_DB_FAILURE;
5973                 }
5974         }
5975
5976         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5977         _DISCONNECT_DB;
5978
5979         if (err_code != NULL)
5980                 *err_code = error;
5981
5982         EM_DEBUG_FUNC_END("ret [%d]", ret);
5983         return ret;
5984 }
5985
5986 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)
5987 {
5988         EM_PROFILE_BEGIN(emStorageGetDownloadList);
5989         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);
5990         if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid || !count)  {
5991                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_id[%s], read_mail_uid[%p], count[%p]", account_id, mailbox_id, read_mail_uid, count);
5992
5993                 if (err_code != NULL)
5994                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5995                 return false;
5996         }
5997
5998         int rc, ret = false;
5999         int error = EMAIL_ERROR_NONE;
6000
6001         DB_STMT hStmt = NULL;
6002         char sql_query_string[QUERY_SIZE] = {0, };
6003
6004         emstorage_read_mail_uid_tbl_t* p_data_tbl = NULL;
6005         int i = 0;
6006
6007         sqlite3 *local_db_handle = emstorage_get_db_connection();
6008         EMSTORAGE_START_READ_TRANSACTION(transaction);
6009
6010         if (mailbox_id)
6011                 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);
6012         else
6013                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
6014
6015         EM_DEBUG_LOG(" sql_query_string : %s", sql_query_string);
6016
6017
6018
6019         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6020         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6021         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6022                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6023
6024
6025         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6026         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6027                 ("sqlite3_step fail:%d", rc));
6028
6029         char **result;
6030         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL); */
6031         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL), rc);
6032         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6033                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6034
6035         sqlite3_free_table(result);
6036         if (*count == 0)  {
6037                 EM_DEBUG_LOG("No mail found in mail_read_mail_uid_tbl");
6038                 ret = true;
6039                 goto FINISH_OFF;
6040         }
6041
6042
6043         if (!(p_data_tbl = (emstorage_read_mail_uid_tbl_t*)malloc(sizeof(emstorage_read_mail_uid_tbl_t) * *count)))  {
6044                 EM_DEBUG_EXCEPTION(" malloc failed...");
6045                 error = EMAIL_ERROR_OUT_OF_MEMORY;
6046                 goto FINISH_OFF;
6047         }
6048
6049         memset(p_data_tbl, 0x00, sizeof(emstorage_read_mail_uid_tbl_t)* *count);
6050
6051         for (i = 0; i < *count; ++i)  {
6052                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6053                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id),LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6054                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].local_uid), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6055                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6056                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].s_uid), 0, S_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6057                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].data1), DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6058                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].data2), 0, DATA2_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6059                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag), FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6060
6061                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6062                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6063                         ("sqlite3_step fail:%d", rc));
6064         }
6065
6066         ret = true;
6067
6068 FINISH_OFF:
6069         if (ret == true)
6070                 *read_mail_uid = p_data_tbl;
6071         else if (p_data_tbl)
6072                 emstorage_free_read_mail_uid(&p_data_tbl, *count, NULL);
6073
6074         if (hStmt != NULL)  {
6075                 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
6076                 rc = sqlite3_finalize(hStmt);
6077                 if (rc != SQLITE_OK)  {
6078                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6079
6080                         error = EMAIL_ERROR_DB_FAILURE;
6081                 }
6082         }
6083
6084         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6085         _DISCONNECT_DB;
6086
6087         if (err_code != NULL)
6088                 *err_code = error;
6089
6090         EM_PROFILE_END(emStorageGetDownloadList);
6091         EM_DEBUG_FUNC_END("ret [%d]", ret);
6092         return ret;
6093 }
6094
6095 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)
6096 {
6097         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);
6098
6099         if (account_id < FIRST_ACCOUNT_ID || !mail_size)  {
6100                 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);
6101
6102                 if (err_code != NULL)
6103                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6104                 return false;
6105         }
6106
6107         int rc, ret = false;
6108         int error = EMAIL_ERROR_NONE;
6109         DB_STMT hStmt = NULL;
6110         char sql_query_string[QUERY_SIZE] = {0, };
6111         char *replaced_mailbox_name = NULL;
6112
6113         sqlite3 *local_db_handle = emstorage_get_db_connection();
6114         EMSTORAGE_START_READ_TRANSACTION(transaction);
6115
6116         if (mailbox_name) {
6117                 if (strstr(mailbox_name, "'")) {
6118                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6119                 } else {
6120                         replaced_mailbox_name = strdup(mailbox_name);
6121                 }
6122
6123                 EM_DEBUG_LOG("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6124
6125                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6126                         "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6127                         "WHERE account_id = %d "
6128                         "AND mailbox_id = '%s' "
6129                         "AND local_uid = %d "
6130                         "AND mailbox_name = '%s' "
6131                         "AND s_uid = '%s'",
6132                         account_id, mailbox_id, local_uid, replaced_mailbox_name, uid);
6133         }
6134         else {
6135                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6136                         "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6137                         "WHERE account_id = %d "
6138                         "AND mailbox_id = '%s' "
6139                         "AND local_uid = %d "
6140                         "AND s_uid = '%s'",
6141                         account_id, mailbox_id, local_uid, uid);
6142         }
6143
6144
6145         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6146         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6147                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6148
6149
6150         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6151         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6152                 ("sqlite3_step fail:%d", rc));
6153
6154         if (rc == SQLITE_DONE) {
6155                 EM_DEBUG_EXCEPTION(" no matched mail found....");
6156                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
6157                 goto FINISH_OFF;
6158         }
6159
6160         _get_stmt_field_data_int(hStmt, mail_size, 0);
6161
6162         ret = true;
6163
6164 FINISH_OFF:
6165         EM_SAFE_FREE(replaced_mailbox_name);
6166
6167         if (hStmt != NULL)  {
6168                 rc = sqlite3_finalize(hStmt);
6169                 if (rc != SQLITE_OK)  {
6170                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6171
6172                         error = EMAIL_ERROR_DB_FAILURE;
6173                 }
6174         }
6175
6176         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6177         _DISCONNECT_DB;
6178
6179         if (err_code != NULL)
6180                 *err_code = error;
6181
6182         EM_DEBUG_FUNC_END("ret [%d]", ret);
6183         return ret;
6184 }
6185
6186 INTERNAL_FUNC int emstorage_add_downloaded_mail(emstorage_read_mail_uid_tbl_t* read_mail_uid, int transaction, int *err_code)
6187 {
6188         EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], transaction[%d], err_code[%p]", read_mail_uid, transaction, err_code);
6189
6190         if (!read_mail_uid)  {
6191                 EM_DEBUG_EXCEPTION("read_mail_uid[%p]", read_mail_uid);
6192                 if (err_code != NULL)
6193                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6194                 return false;
6195         }
6196
6197         int rc, rc2,  ret = false;
6198         int error = EMAIL_ERROR_NONE;
6199         DB_STMT hStmt = NULL;
6200         char sql_query_string[QUERY_SIZE] = {0, };
6201
6202         sqlite3 *local_db_handle = emstorage_get_db_connection();
6203         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6204
6205         char *sql = "SELECT max(rowid) FROM mail_read_mail_uid_tbl;";
6206         char **result = NULL;
6207
6208
6209         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6210         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6211                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6212
6213         if (NULL==result[1]) rc = 1;
6214         else rc = atoi(result[1])+1;
6215         sqlite3_free_table(result);
6216
6217         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6218                 "INSERT INTO mail_read_mail_uid_tbl VALUES "
6219                 "( ?"
6220                 ", ?"
6221                 ", ?"
6222                 ", ?"
6223                 ", ?"
6224                 ", ?"
6225                 ", ?"
6226                 ", ?"
6227                 ", ? )");
6228
6229
6230         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
6231         if (rc2 != SQLITE_OK)  {
6232                 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6233                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6234
6235                 error = EMAIL_ERROR_DB_FAILURE;
6236                 goto FINISH_OFF;
6237         }
6238
6239         EM_DEBUG_LOG("account_id VALUE [%d] ", read_mail_uid->account_id);
6240         EM_DEBUG_LOG("mailbox_id VALUE [%d] ", read_mail_uid->mailbox_id);
6241         EM_DEBUG_LOG("local_uid VALUE [%d] ", read_mail_uid->local_uid);
6242         EM_DEBUG_LOG("mailbox_name VALUE [%s] ", read_mail_uid->mailbox_name);
6243         EM_DEBUG_LOG("s_uid VALUE [%s] ", read_mail_uid->s_uid);
6244         EM_DEBUG_LOG("data1 VALUE [%d] ", read_mail_uid->data1);
6245         EM_DEBUG_LOG("data2 VALUE [%s] ", read_mail_uid->data2);
6246         EM_DEBUG_LOG("flag VALUE [%d] ", read_mail_uid->flag);
6247         EM_DEBUG_LOG("rc VALUE [%d] ", rc);
6248
6249         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->account_id);
6250         _bind_stmt_field_data_int(hStmt, LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->mailbox_id);
6251         _bind_stmt_field_data_int(hStmt, LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->local_uid);
6252         _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);
6253         _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);
6254         _bind_stmt_field_data_int(hStmt, DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->data1);
6255         _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);
6256         _bind_stmt_field_data_int(hStmt, FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flag);
6257         _bind_stmt_field_data_int(hStmt, IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, rc);
6258
6259
6260         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6261         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
6262                 ("sqlite3_step fail:%d", rc));
6263         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6264                 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
6265
6266
6267         ret = true;
6268
6269 FINISH_OFF:
6270         if (hStmt != NULL)  {
6271                 EM_DEBUG_LOG(" sqlite3_finalize hStmt = %p", hStmt);
6272
6273                 rc = sqlite3_finalize(hStmt);
6274                 if (rc != SQLITE_OK)  {
6275                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6276                         error = EMAIL_ERROR_DB_FAILURE;
6277                 }
6278         }
6279
6280         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6281         _DISCONNECT_DB;
6282
6283         if (err_code != NULL)
6284                 *err_code = error;
6285
6286         EM_DEBUG_FUNC_END("ret [%d]", ret);
6287         return ret;
6288 }
6289
6290 #ifdef __FEATURE_BODY_SEARCH__
6291 INTERNAL_FUNC int emstorage_add_mail_text(emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
6292 {
6293         EM_DEBUG_FUNC_BEGIN("mail_text[%p], transaction[%d], err_code[%p]", mail_text, transaction, err_code);
6294
6295         if (!mail_text) {
6296                 EM_DEBUG_EXCEPTION("mail_text[%p]", mail_text);
6297                 if (err_code != NULL)
6298                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6299                 return false;
6300         }
6301
6302         int rc, rc2,  ret = false;
6303         int error = EMAIL_ERROR_NONE;
6304         DB_STMT hStmt = NULL;
6305         char sql_query_string[QUERY_SIZE] = {0, };
6306
6307         sqlite3 *local_db_handle = emstorage_get_db_connection();
6308         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6309
6310         char *sql = "SELECT max(rowid) FROM mail_text_tbl;";
6311         char **result = NULL;
6312
6313         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6314         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6315                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6316         sqlite3_free_table(result);
6317
6318         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6319                 "INSERT INTO mail_text_tbl VALUES "
6320                 "( ?"
6321                 ", ?"
6322                 ", ?"
6323                 ", ? )");
6324
6325         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
6326         if (rc2 != SQLITE_OK)  {
6327                 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6328                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc2, sqlite3_errmsg(local_db_handle));
6329
6330                 error = EMAIL_ERROR_DB_FAILURE;
6331                 goto FINISH_OFF;
6332         }
6333
6334         EM_DEBUG_LOG("mail_id VALUE [%d] ", mail_text->mail_id);
6335         EM_DEBUG_LOG("account_id VALUE [%d] ", mail_text->account_id);
6336         EM_DEBUG_LOG("mailbox_id VALUE [%d] ", mail_text->mailbox_id);
6337         EM_DEBUG_LOG("body_text VALUE [%s] ", mail_text->body_text);
6338
6339         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mail_id);
6340         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->account_id);
6341         _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mailbox_id);
6342         _bind_stmt_field_data_string(hStmt, BODY_TEXT_IDX_IN_MAIL_TEXT_TBL, (char *)mail_text->body_text, 0, -1);
6343
6344         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6345         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
6346                 ("sqlite3_step fail:%d", rc));
6347         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6348                 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
6349
6350         ret = true;
6351
6352 FINISH_OFF:
6353         if (hStmt != NULL) {
6354                 EM_DEBUG_LOG(" sqlite3_finalize hStmt = %p", hStmt);
6355
6356                 rc = sqlite3_finalize(hStmt);
6357                 if (rc != SQLITE_OK)  {
6358                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6359                         error = EMAIL_ERROR_DB_FAILURE;
6360                 }
6361         }
6362
6363         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6364         _DISCONNECT_DB;
6365
6366         if (err_code != NULL)
6367                 *err_code = error;
6368
6369         EM_DEBUG_FUNC_END("ret [%d]", ret);
6370         return ret;
6371 }
6372 #endif
6373
6374 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)
6375 {
6376         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);
6377
6378         if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid)  {
6379                 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);
6380
6381                 if (err_code != NULL)
6382                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6383                 return false;
6384         }
6385
6386         int rc, ret = false;
6387         int error = EMAIL_ERROR_NONE;
6388         DB_STMT hStmt = NULL;
6389         char sql_query_string[QUERY_SIZE] = {0, };
6390
6391         sqlite3 *local_db_handle = emstorage_get_db_connection();
6392
6393         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6394
6395         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6396                 "UPDATE mail_read_mail_uid_tbl SET"
6397                 "  account_id = ?"
6398                 ", mailbox_id = ?"
6399                 ", local_uid  = ?"
6400                 ", mailbox_name = ?"
6401                 ", s_uid = ?"
6402                 ", data1 = ?"
6403                 ", data2 = ?"
6404                 ", flag  = ?"
6405                 " WHERE account_id = ?"
6406                 " AND mailbox_id  = ?"
6407                 " AND local_uid   = ?"
6408                 " AND mailbox_name= ?"
6409                 " AND s_uid = ?");
6410
6411
6412         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6413         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6414         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6415                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6416
6417
6418         int i = 0;
6419
6420         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->account_id);
6421         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->mailbox_id);
6422         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->local_uid);
6423         _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6424         _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->s_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6425         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->data1);
6426         _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->data2, 0, DATA2_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6427         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flag);
6428         _bind_stmt_field_data_int(hStmt, i++, account_id);
6429         _bind_stmt_field_data_int(hStmt, i++, mailbox_id);
6430         _bind_stmt_field_data_int(hStmt, i++, local_uid);
6431         _bind_stmt_field_data_string(hStmt, i++, (char*)mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6432         _bind_stmt_field_data_string(hStmt, i++, (char*)uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6433
6434
6435         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6436         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6437                 ("sqlite3_step fail:%d", rc));
6438
6439         ret = true;
6440
6441 FINISH_OFF:
6442         if (hStmt != NULL)  {
6443                 EM_DEBUG_LOG("sqlite3_finalize hStmt = %p", hStmt);
6444
6445                 rc = sqlite3_finalize(hStmt);
6446                 if (rc != SQLITE_OK)  {
6447                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6448
6449                         error = EMAIL_ERROR_DB_FAILURE;
6450                 }
6451         }
6452
6453         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6454         _DISCONNECT_DB;
6455
6456         if (err_code != NULL)
6457                 *err_code = error;
6458
6459         EM_DEBUG_FUNC_END("ret [%d]", ret);
6460         return ret;
6461 }
6462
6463 INTERNAL_FUNC int emstorage_remove_downloaded_mail(int account_id, char *mailbox_name, char *uid, int transaction, int *err_code)
6464 {
6465         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);
6466
6467         if (account_id < FIRST_ACCOUNT_ID)  {
6468                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%s], uid[%s]", account_id, mailbox_name, uid);
6469
6470                 if (err_code != NULL)
6471                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6472                 return false;
6473         }
6474
6475         int rc, ret = false;
6476         int error = EMAIL_ERROR_NONE;
6477         char sql_query_string[QUERY_SIZE] = {0, };
6478         char *replaced_mailbox_name = NULL;
6479
6480         sqlite3 *local_db_handle = emstorage_get_db_connection();
6481
6482         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6483
6484         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d ", account_id);
6485
6486         if (mailbox_name) {             /*  NULL means all mailbox_name */
6487                 if (strstr(mailbox_name, "'")) {
6488                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6489                 } else {
6490                         replaced_mailbox_name = strdup(mailbox_name);
6491                 }
6492
6493                 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);
6494         }
6495
6496         if (uid) {              /*  NULL means all mail */
6497                 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);
6498         }
6499
6500         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
6501         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6502                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6503
6504
6505         ret = true;
6506
6507 FINISH_OFF:
6508
6509         EM_SAFE_FREE(replaced_mailbox_name);
6510
6511         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6512         _DISCONNECT_DB;
6513
6514         if (err_code != NULL)
6515                 *err_code = error;
6516
6517         EM_DEBUG_FUNC_END("ret [%d]", ret);
6518         return ret;
6519 }
6520
6521 INTERNAL_FUNC int emstorage_free_read_mail_uid(emstorage_read_mail_uid_tbl_t** read_mail_uid, int count, int *err_code)
6522 {
6523         EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], count[%d], err_code[%p]", read_mail_uid, count, err_code);
6524
6525         int ret = false;
6526         int error = EMAIL_ERROR_NONE;
6527
6528         if (count > 0)  {
6529                 if (!read_mail_uid || !*read_mail_uid)  {
6530                         EM_DEBUG_EXCEPTION(" read_mail_uid[%p], count[%d]", read_mail_uid, count);
6531
6532                         error = EMAIL_ERROR_INVALID_PARAM;
6533                         goto FINISH_OFF;
6534                 }
6535
6536                 emstorage_read_mail_uid_tbl_t* p = *read_mail_uid;
6537                 int i;
6538
6539                 for (i = 0; i < count; i++)  {
6540                         EM_SAFE_FREE(p[i].mailbox_name);
6541                         EM_SAFE_FREE(p[i].s_uid);
6542                         EM_SAFE_FREE(p[i].data2);
6543                 }
6544
6545                 EM_SAFE_FREE(p); *read_mail_uid = NULL;
6546         }
6547
6548         ret = true;
6549
6550 FINISH_OFF:
6551         if (err_code != NULL)
6552                 *err_code = error;
6553
6554         EM_DEBUG_FUNC_END("ret [%d]", ret);
6555         return ret;
6556 }
6557
6558 INTERNAL_FUNC int emstorage_get_rule_count_by_account_id(int account_id, int *count, int transaction, int *err_code)
6559 {
6560         EM_DEBUG_FUNC_BEGIN("account_id [%d], count[%p], transaction[%d], err_code[%p]", count, transaction, err_code);
6561
6562         if (!count) {           
6563                 EM_DEBUG_EXCEPTION("count[%p]", count);
6564
6565                 if (err_code != NULL)
6566                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6567                 return false;
6568         }
6569
6570         int rc = -1, ret = false;
6571         int error =  EMAIL_ERROR_NONE;
6572         char sql_query_string[QUERY_SIZE] = {0, };
6573
6574         sqlite3 *local_db_handle = emstorage_get_db_connection();
6575         EMSTORAGE_START_READ_TRANSACTION(transaction);
6576
6577         if (account_id != ALL_ACCOUNT) 
6578                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl where account_id = %d", account_id);
6579         else
6580                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl");
6581
6582         char **result;
6583
6584         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6585         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6586                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6587
6588         *count = atoi(result[1]);
6589         sqlite3_free_table(result);
6590
6591         ret = true;
6592
6593 FINISH_OFF:
6594         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6595         _DISCONNECT_DB;
6596
6597         if (err_code != NULL)
6598                 *err_code = error;
6599
6600         EM_DEBUG_FUNC_END("ret [%d]", ret);
6601         return ret;
6602 }
6603
6604 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)
6605 {
6606         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);
6607
6608         if (!select_num || !is_completed || !rule_list) {               /*  only global rule supported. */
6609                 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);
6610
6611                 if (err_code != NULL)
6612                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6613                 return false;
6614         }
6615
6616         int ret = false;
6617         int error = EMAIL_ERROR_NONE;
6618
6619         emstorage_rule_tbl_t* p_data_tbl = NULL;
6620         int i = 0, count = 0;
6621         DB_STMT hStmt = NULL;
6622         char sql_query_string[QUERY_SIZE] = {0, };
6623
6624         int rc;
6625
6626         sqlite3 *local_db_handle = emstorage_get_db_connection();
6627         EMSTORAGE_START_READ_TRANSACTION(transaction);
6628
6629         if (account_id != ALL_ACCOUNT) {
6630                 if (type)
6631                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d AND type = %d", account_id, type);
6632                 else
6633                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d ORDER BY type", account_id);
6634         } else {
6635                 if (type)
6636                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE type = %d", type);
6637                 else
6638                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl ORDER BY type");
6639         }
6640
6641         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6642         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6643         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6644                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6645
6646
6647         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6648         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6649                 ("sqlite3_step fail:%d", rc));
6650
6651         char **result;
6652         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL); */
6653         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
6654         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6655                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6656
6657         sqlite3_free_table(result);
6658
6659         if (count == 0)  {
6660                 EM_DEBUG_LOG(" no matched rule found...");
6661                 ret = true;
6662                 goto FINISH_OFF;
6663         }
6664
6665
6666         if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t) * count))) {
6667                 EM_DEBUG_EXCEPTION(" malloc failed...");
6668
6669                 error = EMAIL_ERROR_OUT_OF_MEMORY;
6670                 goto FINISH_OFF;
6671         }
6672
6673         memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t) * count);
6674
6675         for (i = 0; i < count; i++)  {
6676                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
6677                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
6678                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].type), TYPE_IDX_IN_MAIL_RULE_TBL);
6679                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
6680                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
6681                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
6682                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
6683                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
6684
6685                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6686                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6687                         ("sqlite3_step fail:%d", rc));
6688         }
6689
6690         ret = true;
6691
6692 FINISH_OFF:
6693
6694         EM_DEBUG_LOG("[%d] rules found.", count);
6695
6696         if (ret == true)  {
6697                 *rule_list = p_data_tbl;
6698                 *select_num = count;
6699         }
6700         else if (p_data_tbl != NULL)
6701                 emstorage_free_rule(&p_data_tbl, count, NULL); /* CID FIX */
6702
6703         if (hStmt != NULL)  {
6704                 EM_DEBUG_LOG("  sqlite3_finalize hStmt = %p", hStmt);
6705
6706                 rc = sqlite3_finalize(hStmt);
6707                 if (rc != SQLITE_OK)  {
6708                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6709                         error = EMAIL_ERROR_DB_FAILURE;
6710                 }
6711         }
6712
6713
6714         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6715         _DISCONNECT_DB;
6716
6717         if (err_code != NULL)
6718                 *err_code = error;
6719
6720         EM_DEBUG_FUNC_END("ret [%d]", ret);
6721         return ret;
6722 }
6723
6724 INTERNAL_FUNC int emstorage_get_rule_by_id(int rule_id, emstorage_rule_tbl_t** rule, int transaction, int *err_code)
6725 {
6726         EM_DEBUG_FUNC_BEGIN("rule_id[%d], rule[%p], transaction[%d], err_code[%p]", rule_id, rule, transaction, err_code);
6727
6728         if (!rule) {
6729                 EM_DEBUG_EXCEPTION("rule_id[%d], rule[%p]", rule_id, rule);
6730
6731                 if (err_code != NULL)
6732                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6733                 return false;
6734         }
6735
6736         emstorage_rule_tbl_t* p_data_tbl = NULL;
6737         int rc, ret = false;
6738         int error = EMAIL_ERROR_NONE;
6739
6740         DB_STMT hStmt = NULL;
6741         char sql_query_string[QUERY_SIZE] = {0, };
6742         sqlite3 *local_db_handle = emstorage_get_db_connection();
6743         EMSTORAGE_START_READ_TRANSACTION(transaction);
6744
6745         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
6746
6747         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6748         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6749                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6750
6751
6752         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6753         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6754                 ("sqlite3_step fail:%d", rc));
6755
6756         if (rc == SQLITE_DONE)  {
6757                 EM_DEBUG_EXCEPTION(" no matched rule found...");
6758                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
6759                 goto FINISH_OFF;
6760         }
6761
6762         if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t))))  {
6763                 EM_DEBUG_EXCEPTION(" malloc failed...");
6764                 error = EMAIL_ERROR_OUT_OF_MEMORY;
6765                 goto FINISH_OFF;
6766         }
6767
6768         memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t));
6769         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
6770         _get_stmt_field_data_int(hStmt, &(p_data_tbl->rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
6771         _get_stmt_field_data_int(hStmt, &(p_data_tbl->type), TYPE_IDX_IN_MAIL_RULE_TBL);
6772         _get_stmt_field_data_string(hStmt, &(p_data_tbl->value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
6773         _get_stmt_field_data_int(hStmt, &(p_data_tbl->action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
6774         _get_stmt_field_data_int(hStmt, &(p_data_tbl->target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
6775         _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
6776         _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
6777
6778         ret = true;
6779
6780 FINISH_OFF:
6781         if (ret == true)
6782                 *rule = p_data_tbl;
6783
6784         if (hStmt != NULL)  {
6785                 EM_DEBUG_LOG(" before sqlite3_finalize hStmt = %p", hStmt);
6786
6787                 rc = sqlite3_finalize(hStmt);
6788                 if (rc != SQLITE_OK)  {
6789                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6790
6791                         error = EMAIL_ERROR_DB_FAILURE;
6792                 }
6793         }
6794
6795         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6796         _DISCONNECT_DB;
6797
6798         if (err_code != NULL)
6799                 *err_code = error;
6800
6801         EM_DEBUG_FUNC_END("ret [%d]", ret);
6802         return ret;
6803 }
6804
6805 INTERNAL_FUNC int emstorage_change_rule(int rule_id, emstorage_rule_tbl_t* new_rule, int transaction, int *err_code)
6806 {
6807         EM_DEBUG_FUNC_BEGIN("rule_id[%d], new_rule[%p], transaction[%d], err_code[%p]", rule_id, new_rule, transaction, err_code);
6808
6809         if (!new_rule) {                /*  only global rule supported. */
6810                 EM_DEBUG_EXCEPTION("rule_id[%d], new_rule[%p]", rule_id, new_rule);
6811
6812                 if (err_code != NULL)
6813                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6814                 return false;
6815         }
6816
6817         int rc, ret = false;
6818         int error = EMAIL_ERROR_NONE;
6819
6820         DB_STMT hStmt = NULL;
6821         char sql_query_string[QUERY_SIZE] = {0, };
6822         sqlite3 *local_db_handle = emstorage_get_db_connection();
6823         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6824
6825         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6826                 "UPDATE mail_rule_tbl SET"
6827                 "  type = ?"
6828                 ", value = ?"
6829                 ", action_type = ?"
6830                 ", target_mailbox_id = ?"
6831                 ", flag1 = ?"
6832                 ", flag2 = ?"
6833                 " WHERE account_id = ?"
6834                 " AND rule_id = ?");
6835
6836
6837         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6838         EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
6839         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6840                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6841
6842         int i = 0;
6843
6844         _bind_stmt_field_data_int(hStmt, i++, new_rule->type);
6845         _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
6846         _bind_stmt_field_data_int(hStmt, i++, new_rule->action_type);
6847         _bind_stmt_field_data_int(hStmt, i++, new_rule->target_mailbox_id);
6848         _bind_stmt_field_data_int(hStmt, i++, new_rule->flag1);
6849         _bind_stmt_field_data_int(hStmt, i++, new_rule->flag2);
6850         _bind_stmt_field_data_int(hStmt, i++, new_rule->account_id);
6851         _bind_stmt_field_data_int(hStmt, i++, rule_id);
6852
6853
6854         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6855         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6856                 ("sqlite3_step fail:%d", rc));
6857
6858         ret = true;
6859
6860 FINISH_OFF:
6861         if (hStmt != NULL)  {
6862                 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
6863
6864                 rc = sqlite3_finalize(hStmt);
6865                 if (rc != SQLITE_OK)  {
6866                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6867
6868                         error = EMAIL_ERROR_DB_FAILURE;
6869                 }
6870         }
6871
6872         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6873         _DISCONNECT_DB;
6874
6875         if (err_code != NULL)
6876                 *err_code = error;
6877
6878         EM_DEBUG_FUNC_END("ret [%d]", ret);
6879         return ret;
6880 }
6881
6882 INTERNAL_FUNC int emstorage_find_rule(emstorage_rule_tbl_t* rule, int transaction, int *err_code)
6883 {
6884         EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
6885
6886         if (!rule) {            
6887                 if (rule != NULL)
6888                         EM_DEBUG_EXCEPTION(" rule->account_id[%d]", rule->account_id);
6889
6890                 if (err_code != NULL)
6891                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6892                 return false;
6893         }
6894
6895         DB_STMT hStmt = NULL;
6896         char sql_query_string[QUERY_SIZE] = {0, };
6897
6898         int error = EMAIL_ERROR_NONE;
6899         int rc, ret = false;
6900
6901         sqlite3 *local_db_handle = emstorage_get_db_connection();
6902         EMSTORAGE_START_READ_TRANSACTION(transaction);
6903
6904         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6905                 "SELECT rule_id FROM mail_rule_tbl WHERE type = %d AND UPPER(value) = UPPER('%s')",
6906                 rule->type, rule->value);
6907
6908
6909         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6910         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6911                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6912
6913
6914         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6915         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6916                 ("sqlite3_step fail:%d", rc));
6917
6918         if (rc == SQLITE_DONE)  {
6919                 EM_DEBUG_EXCEPTION(" no matched rule found...");
6920                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
6921                 goto FINISH_OFF;
6922         }
6923
6924
6925         ret = true;
6926
6927 FINISH_OFF:
6928         if (hStmt != NULL)  {
6929                 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
6930
6931                 rc = sqlite3_finalize(hStmt);
6932                 if (rc != SQLITE_OK)  {
6933                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
6934                         error = EMAIL_ERROR_DB_FAILURE;
6935                 }
6936         }
6937
6938
6939         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6940         _DISCONNECT_DB;
6941
6942         if (err_code)
6943                 *err_code = error;
6944
6945         EM_DEBUG_FUNC_END("ret [%d]", ret);
6946         return ret;
6947 }
6948
6949 INTERNAL_FUNC int emstorage_add_rule(emstorage_rule_tbl_t* rule, int transaction, int *err_code)
6950 {
6951         EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
6952
6953         if (!rule)  {   /*  only global rule supported. */
6954                 if (rule != NULL)
6955                         EM_DEBUG_EXCEPTION(" rule->account_id[%d]", rule->account_id);
6956
6957                 if (err_code != NULL)
6958                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6959                 return false;
6960         }
6961
6962         int rc, rc_2, ret = false;
6963         int error = EMAIL_ERROR_NONE;
6964         DB_STMT hStmt = NULL;
6965         char sql_query_string[QUERY_SIZE] = {0, };
6966
6967
6968         sqlite3 *local_db_handle = emstorage_get_db_connection();
6969
6970         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6971
6972         char *sql;
6973         char **result;
6974         sql = "SELECT max(rowid) FROM mail_rule_tbl;";
6975
6976         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6977         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6978                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6979
6980         if (NULL==result[1])
6981                 rc = 1;
6982         else
6983                 rc = atoi(result[1])+1;
6984
6985         sqlite3_free_table(result);
6986
6987         rule->rule_id = rc;
6988
6989         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6990                 "INSERT INTO mail_rule_tbl VALUES "
6991                 "( ?"           /*  account id */
6992                 ", ?"           /*  rule_id */
6993                 ", ?"           /*  type */
6994                 ", ?"           /*  value */
6995                 ", ?"           /*  action_type */
6996                 ", ?"           /*  target_mailbox_id */
6997                 ", ?"           /*  flag1 */
6998                 ", ?)");        /*  flag2 */
6999
7000         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc_2);
7001         if (rc_2 != SQLITE_OK)  {
7002                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc_2, sqlite3_errmsg(local_db_handle));
7003                 error = EMAIL_ERROR_DB_FAILURE;
7004                 goto FINISH_OFF;
7005         }
7006
7007         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL, rule->account_id);
7008         _bind_stmt_field_data_int(hStmt, RULE_ID_IDX_IN_MAIL_RULE_TBL, rule->rule_id);
7009         _bind_stmt_field_data_int(hStmt, TYPE_IDX_IN_MAIL_RULE_TBL, rule->type);
7010         _bind_stmt_field_data_string(hStmt, VALUE_IDX_IN_MAIL_RULE_TBL, (char*)rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7011         _bind_stmt_field_data_int(hStmt, ACTION_TYPE_IDX_IN_MAIL_RULE_TBL, rule->action_type);
7012         _bind_stmt_field_data_int(hStmt, TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL, rule->target_mailbox_id);
7013         _bind_stmt_field_data_int(hStmt, FLAG1_IDX_IN_MAIL_RULE_TBL, rule->flag1);
7014         _bind_stmt_field_data_int(hStmt, FLAG2_IDX_IN_MAIL_RULE_TBL, rule->flag2);
7015
7016         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7017         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7018                 ("sqlite3_step fail:%d", rc));
7019
7020         ret = true;
7021
7022 FINISH_OFF:
7023         if (hStmt != NULL)  {
7024                 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
7025
7026                 rc = sqlite3_finalize(hStmt);
7027                 if (rc != SQLITE_OK) {
7028                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
7029
7030                         error = EMAIL_ERROR_DB_FAILURE;
7031                 }
7032         }
7033
7034         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
7035         _DISCONNECT_DB;
7036
7037         if (err_code != NULL)
7038                 *err_code = error;
7039
7040         EM_DEBUG_FUNC_END("ret [%d]", ret);
7041         return ret;
7042 }
7043
7044 INTERNAL_FUNC int emstorage_delete_rule(int rule_id, int transaction, int *err_code)
7045 {
7046         EM_DEBUG_FUNC_BEGIN("rule_id[%d], transaction[%d], err_code[%p]", rule_id, transaction, err_code);
7047
7048         if (rule_id <= 0) {             /*  only global rule supported. */
7049                 EM_DEBUG_EXCEPTION("rule_id[%d]", rule_id);
7050
7051                 if (err_code != NULL)
7052                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7053                 return false;
7054         }
7055
7056         int rc, ret = false;
7057         int error = EMAIL_ERROR_NONE;
7058         char sql_query_string[QUERY_SIZE] = {0, };
7059
7060         sqlite3 *local_db_handle = emstorage_get_db_connection();
7061         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
7062
7063         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7064
7065         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
7066         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7067                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7068         rc = sqlite3_changes(local_db_handle);
7069         if (rc == 0) {
7070                 EM_DEBUG_EXCEPTION(" no matched rule found...");
7071
7072                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7073                 goto FINISH_OFF;
7074         }
7075
7076         ret = true;
7077
7078 FINISH_OFF:
7079         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
7080         _DISCONNECT_DB;
7081
7082         if (err_code != NULL)
7083                 *err_code = error;
7084
7085         EM_DEBUG_FUNC_END("ret [%d]", ret);
7086         return ret;
7087 }
7088
7089 INTERNAL_FUNC int emstorage_free_rule(emstorage_rule_tbl_t** rule_list, int count, int *err_code)
7090 {
7091         EM_DEBUG_FUNC_BEGIN("rule_list[%p], conut[%d], err_code[%p]", rule_list, count, err_code);
7092
7093         int ret = false;
7094         int error = EMAIL_ERROR_NONE;
7095
7096         if (count > 0) {
7097                 if (!rule_list || !*rule_list) {
7098                         EM_DEBUG_EXCEPTION(" rule_list[%p], conut[%d]", rule_list, count);
7099
7100                         error = EMAIL_ERROR_INVALID_PARAM;
7101                         goto FINISH_OFF;
7102                 }
7103
7104                 emstorage_rule_tbl_t* p = *rule_list;
7105                 int i = 0;
7106
7107                 for (; i < count; i++) {
7108                         EM_SAFE_FREE(p[i].value);
7109                 }
7110
7111                 EM_SAFE_FREE(p); *rule_list = NULL;
7112         }
7113
7114 FINISH_OFF:
7115         if (err_code != NULL)
7116                 *err_code = error;
7117
7118         EM_DEBUG_FUNC_END("ret [%d]", ret);
7119         return ret;
7120 }
7121
7122 INTERNAL_FUNC int emstorage_get_mail_count(int account_id, int mailbox_id, int *total, int *unseen, int transaction, int *err_code)
7123 {
7124         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);
7125
7126         if (!total && !unseen) {
7127                 EM_DEBUG_EXCEPTION(" accoun_id[%d], mailbox_id[%d], total[%p], unseen[%p]", account_id, mailbox_id, total, unseen);
7128                 if (err_code != NULL)
7129                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7130                 return false;
7131         }
7132
7133         int rc = -1, ret = false;
7134         int error = EMAIL_ERROR_NONE;
7135         DB_STMT hStmt = NULL;
7136         char sql_query_string[QUERY_SIZE] = {0, };
7137         char err_msg[1024];
7138         char *replaced_mailbox_name = NULL;
7139
7140         memset(&sql_query_string, 0x00, sizeof(sql_query_string));
7141         sqlite3 *local_db_handle = emstorage_get_db_connection();
7142         EMSTORAGE_START_READ_TRANSACTION(transaction);
7143
7144         if (total)  {
7145                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl");
7146
7147                 if (account_id != ALL_ACCOUNT)  {
7148                         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);
7149                         if (mailbox_id)
7150                                 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);
7151                 }
7152                 else if (mailbox_id)
7153                         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);
7154
7155 #ifdef USE_GET_RECORD_COUNT_API
7156                 char **result;
7157
7158                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7159                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF2; },
7160                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7161
7162                 *total = atoi(result[1]);
7163                 sqlite3_free_table(result);
7164 #else
7165
7166                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7167                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF2; },
7168                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7169
7170                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7171                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF2; },
7172                         ("sqlite3_step fail:%d", rc));
7173                 _get_stmt_field_data_int(hStmt, total, 0);
7174 #endif          /*  USE_GET_RECORD_COUNT_API */
7175         }
7176
7177         if (unseen)  {
7178                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl WHERE flags_seen_field = 0");               /*  fSEEN = 0x01 */
7179
7180                 if (account_id != ALL_ACCOUNT)
7181                         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);
7182
7183                 if (mailbox_id) {
7184                         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);
7185                 }
7186                 else
7187                         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)");
7188
7189                 char **result;
7190                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7191                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
7192                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7193
7194                 *unseen = atoi(result[1]);
7195                 sqlite3_free_table(result);
7196
7197         }
7198 FINISH_OFF:
7199         ret = true;
7200
7201 FINISH_OFF2:
7202
7203 #ifndef USE_PREPARED_QUERY_
7204         if (hStmt != NULL)  {
7205                 rc = sqlite3_finalize(hStmt);
7206                 if (rc != SQLITE_OK)  {
7207                         EM_DEBUG_LOG(" EDBStmtClearRow failed - %d: %s", rc, err_msg);
7208                         error = EMAIL_ERROR_DB_FAILURE;
7209                 }
7210         }
7211 #endif
7212
7213         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7214         _DISCONNECT_DB;
7215
7216         EM_SAFE_FREE(replaced_mailbox_name);
7217
7218         if (err_code != NULL)
7219                 *err_code = error;
7220
7221         EM_DEBUG_FUNC_END("ret [%d]", ret);
7222         return ret;
7223 }
7224
7225 INTERNAL_FUNC int emstorage_get_mail_field_by_id(int mail_id, int type, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
7226 {
7227         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
7228
7229         if (mail_id <= 0 || !mail)  {
7230                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
7231                 if (err_code != NULL)
7232                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7233                 return false;
7234         }
7235
7236         int col_index = 0;
7237         emstorage_mail_tbl_t* p_data_tbl = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
7238
7239         if (p_data_tbl == NULL)  {
7240                 EM_DEBUG_EXCEPTION("malloc failed...");
7241                 if (err_code != NULL)
7242                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
7243                 return false;
7244         }
7245
7246         memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
7247         DB_STMT hStmt = NULL;
7248         char sql_query_string[QUERY_SIZE] = {0, };
7249
7250         int rc, ret = false;
7251         int error = EMAIL_ERROR_NONE;
7252
7253         sqlite3 *local_db_handle = emstorage_get_db_connection();
7254         EMSTORAGE_START_READ_TRANSACTION(transaction);
7255
7256         switch (type)  {
7257                 case RETRIEVE_SUMMARY:
7258                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7259                                 "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);
7260                         break;
7261
7262                 case RETRIEVE_FIELDS_FOR_DELETE:
7263                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7264                                 "SELECT account_id, mail_id, server_mail_status, server_mailbox_name, server_mail_id FROM mail_tbl WHERE mail_id = %d", mail_id);
7265                         break;
7266
7267                 case RETRIEVE_ACCOUNT:
7268                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7269                                 "SELECT account_id FROM mail_tbl WHERE mail_id = %d", mail_id);
7270                         break;
7271
7272                 case RETRIEVE_FLAG:
7273                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7274                                 "SELECT account_id, flags_seen_field, thread_id, mailbox_id FROM mail_tbl WHERE mail_id = %d", mail_id);
7275                         break;
7276
7277                 default :
7278                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
7279                         error = EMAIL_ERROR_INVALID_PARAM;
7280                         goto FINISH_OFF;
7281         }
7282
7283         EM_DEBUG_LOG("Query [%s]", sql_query_string);
7284
7285         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7286         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7287                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7288
7289
7290         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7291         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7292                 ("sqlite3_step fail:%d", rc));
7293
7294         if (rc == SQLITE_DONE)  {
7295                 EM_DEBUG_EXCEPTION("no matched mail found...");
7296                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7297                 goto FINISH_OFF;
7298         }
7299         switch (type)  {
7300                 case RETRIEVE_SUMMARY:
7301                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
7302                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
7303                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
7304                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
7305                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
7306                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
7307                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, col_index++);
7308                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, col_index++);
7309                         _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
7310                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), col_index++);
7311                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), col_index++);
7312                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
7313                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_item_count), col_index++);
7314                         break;
7315
7316                 case RETRIEVE_FIELDS_FOR_DELETE:
7317                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
7318                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
7319                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
7320                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
7321                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
7322                         break;
7323
7324                 case RETRIEVE_ACCOUNT:
7325                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
7326                         break;
7327
7328                 case RETRIEVE_FLAG:
7329                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
7330                         _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
7331                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
7332                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
7333                         break;
7334         }
7335
7336         ret = true;
7337
7338 FINISH_OFF:
7339         if (ret == true)
7340                 *mail = p_data_tbl;
7341         else if (p_data_tbl != NULL)
7342                 emstorage_free_mail(&p_data_tbl,  1, NULL);
7343
7344         if (hStmt != NULL)  {
7345                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
7346
7347                 rc = sqlite3_finalize(hStmt);
7348                 if (rc != SQLITE_OK)  {
7349                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
7350                         error = EMAIL_ERROR_DB_FAILURE;
7351                 }
7352         }
7353
7354
7355         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7356         _DISCONNECT_DB;
7357
7358         if (err_code != NULL)
7359                 *err_code = error;
7360
7361         EM_DEBUG_FUNC_END("ret [%d]", ret);
7362         return ret;
7363 }
7364
7365 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)
7366 {
7367         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);
7368
7369         int ret = false;
7370         int error = EMAIL_ERROR_NONE;
7371         int query_string_length = 0;
7372         int i = 0, item_count = 0, rc = -1, field_count, col_index, cur_sql_query_string = 0;
7373         char **result = NULL;
7374         char *sql_query_string = NULL;
7375         emstorage_mail_tbl_t* p_data_tbl = NULL;
7376         sqlite3 *local_db_handle = NULL;
7377
7378         if (number_of_mails <= 0 || !mail_ids)  {
7379                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
7380                 if (err_code != NULL)
7381                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7382                 return false;
7383         }
7384
7385         p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * number_of_mails);
7386
7387         query_string_length = (sizeof(char) * 8 * number_of_mails) + 512;
7388         sql_query_string = (char*)em_malloc(query_string_length);
7389
7390         if (p_data_tbl == NULL || sql_query_string == NULL)  {
7391                 EM_DEBUG_EXCEPTION("malloc failed...");
7392
7393                 EM_SAFE_FREE(p_data_tbl);
7394                 EM_SAFE_FREE(sql_query_string);
7395
7396                 if (err_code != NULL)
7397                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
7398                 return false;
7399         }
7400
7401         local_db_handle = emstorage_get_db_connection();
7402
7403         EMSTORAGE_START_READ_TRANSACTION(transaction);
7404
7405         switch (type) {
7406                 case RETRIEVE_SUMMARY:
7407                         cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
7408                                 "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 (");
7409                         field_count = 12;
7410                         break;
7411
7412                 case RETRIEVE_FIELDS_FOR_DELETE:
7413                         cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
7414                                 "SELECT account_id, mail_id, server_mail_status, server_mailbox_name, server_mail_id FROM mail_tbl WHERE mail_id in (");
7415                         field_count = 5;
7416                         break;
7417
7418                 case RETRIEVE_ACCOUNT:
7419                         cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
7420                                 "SELECT account_id FROM mail_tbl WHERE mail_id in (");
7421                         field_count = 1;
7422                         break;
7423
7424                 case RETRIEVE_FLAG:
7425                         cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
7426                                 "SELECT account_id, mailbox_id, flags_seen_field, thread_id FROM mail_tbl WHERE mail_id in (");
7427                         field_count = 4;
7428                         break;
7429
7430                 default :
7431                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
7432                         error = EMAIL_ERROR_INVALID_PARAM;
7433                         goto FINISH_OFF;
7434         }
7435
7436         for(i = 0; i < number_of_mails; i++)
7437                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, QUERY_SIZE, "%d,", mail_ids[i]);
7438         sql_query_string[EM_SAFE_STRLEN(sql_query_string) - 1] = ')';
7439
7440         EM_DEBUG_LOG("Query [%s], Length [%d]", sql_query_string, EM_SAFE_STRLEN(sql_query_string));
7441
7442         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &item_count, 0, NULL), rc);
7443         if (SQLITE_OK != rc && -1 != rc) {
7444                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
7445                 error = EMAIL_ERROR_DB_FAILURE;
7446                 goto FINISH_OFF;
7447         }
7448
7449         if (rc == SQLITE_DONE)  {
7450                 EM_DEBUG_EXCEPTION("no matched mail found...");
7451                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7452                 goto FINISH_OFF;
7453         }
7454
7455         EM_DEBUG_LOG("item_count [%d]", item_count);
7456
7457         if(number_of_mails != item_count) {
7458                 EM_DEBUG_EXCEPTION("Can't find all emails");
7459                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7460                 goto FINISH_OFF;
7461         }
7462
7463         col_index = field_count;
7464
7465         for(i = 0; i < item_count; i++) {
7466                 switch (type) {
7467                         case RETRIEVE_SUMMARY:
7468                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7469                                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
7470                                 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
7471                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
7472                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
7473                                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
7474                                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
7475                                 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
7476                                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
7477                                 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
7478                                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
7479                                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
7480                                 break;
7481
7482                         case RETRIEVE_FIELDS_FOR_DELETE:
7483                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7484                                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
7485                                 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
7486                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
7487                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
7488                                 break;
7489
7490                         case RETRIEVE_ACCOUNT:
7491                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7492                                 break;
7493
7494                         case RETRIEVE_FLAG:
7495                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7496                                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
7497                                 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
7498                                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
7499                                 break;
7500                 }
7501         }
7502
7503         ret = true;
7504
7505 FINISH_OFF:
7506         if (ret == true)
7507                 *mail = p_data_tbl;
7508         else
7509                 emstorage_free_mail(&p_data_tbl, number_of_mails, NULL);
7510
7511         if (result)
7512                 sqlite3_free_table(result);
7513
7514         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7515         _DISCONNECT_DB;
7516
7517         EM_SAFE_FREE(sql_query_string);
7518
7519         if (err_code != NULL)
7520                 *err_code = error;
7521
7522         EM_DEBUG_FUNC_END("ret [%d]", ret);
7523         return ret;
7524 }
7525
7526 INTERNAL_FUNC int emstorage_get_mail_by_id(int mail_id, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
7527 {
7528         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
7529
7530         if (mail_id <= 0 || !mail) {
7531                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
7532                 if (err_code != NULL)
7533                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7534                 return false;
7535         }
7536
7537         int ret = false, error = EMAIL_ERROR_NONE, count;
7538         char conditional_clause[QUERY_SIZE] = {0, };
7539         emstorage_mail_tbl_t* p_data_tbl = NULL;
7540
7541         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
7542         EM_DEBUG_LOG("query = [%s]", conditional_clause);
7543
7544         if(!emstorage_query_mail_tbl(conditional_clause, transaction, &p_data_tbl, &count, &error)) {
7545                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
7546                 goto FINISH_OFF;
7547         }
7548
7549         ret = true;
7550
7551 FINISH_OFF:
7552         if (ret == true)
7553                 *mail = p_data_tbl;
7554         else if (p_data_tbl != NULL)
7555                 emstorage_free_mail(&p_data_tbl, 1, &error);
7556
7557         if (err_code != NULL)
7558                 *err_code = error;
7559
7560         EM_DEBUG_FUNC_END("ret [%d]", ret);
7561         return ret;
7562 }
7563
7564 #ifdef __FEATURE_BODY_SEARCH__
7565 INTERNAL_FUNC int emstorage_get_mail_text_by_id(int mail_id, emstorage_mail_text_tbl_t **mail_text, int transaction, int *err_code)
7566 {
7567         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
7568
7569         if (mail_id <= 0 || !mail_text) {
7570                 EM_DEBUG_EXCEPTION("mail_id[%d], mail_text[%p]", mail_id, mail_text);
7571                 if (err_code != NULL)
7572                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7573                 return false;
7574         }
7575
7576         int ret = false;
7577         int error = EMAIL_ERROR_NONE;
7578         int count = 0;
7579         char conditional_clause[QUERY_SIZE] = {0, };
7580         emstorage_mail_text_tbl_t *p_data_tbl = NULL;
7581
7582         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
7583         EM_DEBUG_LOG("query = [%s]", conditional_clause);
7584
7585         if(!emstorage_query_mail_text_tbl(conditional_clause, transaction, &p_data_tbl, &count, &error)) {
7586                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
7587                 goto FINISH_OFF;
7588         }
7589
7590         ret = true;
7591
7592 FINISH_OFF:
7593         if (ret == true)
7594                 *mail_text = p_data_tbl;
7595         else if (p_data_tbl != NULL)
7596                 emstorage_free_mail_text(&p_data_tbl, 1, &error);
7597
7598         if (err_code != NULL)
7599                 *err_code = error;
7600
7601         EM_DEBUG_FUNC_END("ret [%d]", ret);
7602         return ret;
7603 }
7604 #endif
7605
7606 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)
7607 {
7608         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);
7609
7610         if (!search_handle || !searched)  {
7611                 if (err_code != NULL)
7612                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7613                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
7614                 EM_DEBUG_FUNC_END("false");
7615                 return false;
7616         }
7617
7618         emstorage_search_filter_t* p = search;
7619         int error = EMAIL_ERROR_NONE;
7620         DB_STMT hStmt = NULL;
7621         char sql_query_string[QUERY_SIZE] = {0, };
7622         int rc, ret = false;
7623         int and = false, mail_count = 0;
7624         char *replaced_mailbox_name = NULL;
7625
7626         sqlite3 *local_db_handle = emstorage_get_db_connection();
7627         EMSTORAGE_START_READ_TRANSACTION(transaction);
7628
7629         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl");
7630
7631         if (account_id != ALL_ACCOUNT)  {
7632                 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);
7633                 and = true;
7634         }
7635
7636         if (mailbox_name)  {
7637                 if (strstr(mailbox_name, "'")) {
7638                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
7639                 } else {
7640                         replaced_mailbox_name = strdup(mailbox_name);
7641                 }
7642
7643                 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);
7644                 and = true;
7645         }
7646
7647         while (p)  {
7648
7649                 if (p->key_type) {
7650                         if (!strncmp(p->key_type, "subject", strlen("subject"))) {
7651                                 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);
7652                                 and = true;
7653                         }
7654                         else if (!strncmp(p->key_type, "full_address_from", strlen("full_address_from"))) {
7655                                 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);
7656                                 and = true;
7657                         }
7658                         else if (!strncmp(p->key_type, "full_address_to", strlen("full_address_to"))) {
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_to LIKE '%%%%%s%%%%'", and ? "AND" : "WHERE", p->key_value);
7660                                 and = true;
7661                         }
7662                         else if (!strncmp(p->key_type, "email_address", strlen("email_address"))) {
7663                                 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);
7664                                 and = true;
7665                         }
7666                         p = p->next;
7667                 }
7668         }
7669
7670         if (sorting)
7671                 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");
7672
7673         EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
7674
7675
7676         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7677         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7678                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7679
7680
7681         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7682         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7683                 ("sqlite3_step fail:%d", rc));
7684
7685         char **result;
7686
7687         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &mail_count, NULL, NULL), rc);
7688         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
7689                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7690
7691         sqlite3_free_table(result);
7692
7693         ret = true;
7694
7695 FINISH_OFF:
7696         if (ret == true)  {
7697                 *search_handle = (int)hStmt;
7698                 *searched = mail_count;
7699                 EM_DEBUG_LOG("mail_count [%d]", mail_count);
7700         }
7701         else  {
7702                 if (hStmt != NULL)  {
7703                         rc = sqlite3_finalize(hStmt);
7704                         if (rc != SQLITE_OK)  {
7705                                 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
7706                                 error = EMAIL_ERROR_DB_FAILURE;
7707                         }
7708                 }
7709
7710                 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7711                 _DISCONNECT_DB;
7712         }
7713
7714         EM_SAFE_FREE(replaced_mailbox_name);
7715
7716         if (err_code != NULL)
7717                 *err_code = error;
7718
7719         EM_DEBUG_FUNC_END("ret [%d]", ret);
7720         return ret;
7721 }
7722
7723 INTERNAL_FUNC int emstorage_mail_search_result(int search_handle, emstorage_mail_field_type_t type, void** data, int transaction, int *err_code)
7724 {
7725         EM_DEBUG_FUNC_BEGIN("search_handle[%d], type[%d], data[%p], transaction[%d], err_code[%p]", search_handle, type, data, transaction, err_code);
7726
7727         if (search_handle == 0 || !data) {
7728                 EM_DEBUG_EXCEPTION(" search_handle[%d], type[%d], data[%p]", search_handle, type, data);
7729
7730                 if (err_code != NULL)
7731                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7732                 return false;
7733         }
7734
7735         emstorage_mail_tbl_t* p_data_tbl = NULL;
7736         DB_STMT hStmt = (DB_STMT)search_handle;
7737         int rc, ret = false;
7738         int error = EMAIL_ERROR_NONE;
7739
7740         switch (type)  {
7741                 case RETRIEVE_ID:
7742                         _get_stmt_field_data_int(hStmt, (int *)data, MAIL_ID_IDX_IN_MAIL_TBL);
7743                         break;
7744
7745                 case RETRIEVE_ENVELOPE:
7746                 case RETRIEVE_ALL:
7747                         if (!(p_data_tbl = em_malloc(sizeof(emstorage_mail_tbl_t)))) {
7748                                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
7749
7750                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7751                                 goto FINISH_OFF;
7752                         }
7753
7754                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
7755                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
7756                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->mail_size), MAIL_SIZE_IDX_IN_MAIL_TBL);
7757                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
7758                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_from), 1, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL);
7759                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_to), 1, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL);
7760                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject), 1, SUBJECT_IDX_IN_MAIL_TBL);
7761                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->body_download_status), BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL);
7762                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL);
7763                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
7764                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
7765                         _get_stmt_field_data_time_t(hStmt, &(p_data_tbl->date_time), DATETIME_IDX_IN_MAIL_TBL);
7766                         _get_stmt_field_data_char  (hStmt, &(p_data_tbl->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL);
7767                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->DRM_status), DRM_STATUS_IDX_IN_MAIL_TBL);
7768                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->priority), PRIORITY_IDX_IN_MAIL_TBL);
7769                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->save_status), SAVE_STATUS_IDX_IN_MAIL_TBL);
7770                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->lock_status), LOCK_STATUS_IDX_IN_MAIL_TBL);
7771                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->report_status), REPORT_STATUS_IDX_IN_MAIL_TBL);
7772                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
7773                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->meeting_request_status), MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL);
7774                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->message_class), MESSAGE_CLASS_IDX_IN_MAIL_TBL);
7775                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->digest_type), DIGEST_TYPE_IDX_IN_MAIL_TBL);
7776                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->smime_type), SMIME_TYPE_IDX_IN_MAIL_TBL);
7777                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->scheduled_sending_time), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
7778                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->eas_data_length), EAS_DATA_LENGTH_IDX_IN_MAIL_TBL);
7779                         _get_stmt_field_data_blob   (hStmt, (void**)&(p_data_tbl->eas_data), EAS_DATA_IDX_IN_MAIL_TBL);
7780
7781                         if (type == RETRIEVE_ALL)  {
7782                                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
7783                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
7784                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_reply), 1, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL);
7785                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_cc), 1, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL);
7786                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_bcc), 1, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL);
7787                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_return), 1, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL);
7788                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->message_id), 0, MESSAGE_ID_IDX_IN_MAIL_TBL);
7789                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
7790                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
7791                                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl->attachment_count), ATTACHMENT_COUNT_IDX_IN_MAIL_TBL);
7792                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
7793
7794                         }
7795
7796                         if (p_data_tbl->body_download_status)  {
7797                                 struct stat buf;
7798
7799                                 if (p_data_tbl->file_path_html)  {
7800                                         if (stat(p_data_tbl->file_path_html, &buf) == -1)
7801                                                 p_data_tbl->body_download_status = 0;
7802                                 }
7803                                 else if (p_data_tbl->file_path_plain)  {
7804                                         if (stat(p_data_tbl->file_path_plain, &buf) == -1)
7805                                                 p_data_tbl->body_download_status = 0;
7806                                 }
7807                                 else
7808                                         p_data_tbl->body_download_status = 0;
7809                         }
7810
7811                         *((emstorage_mail_tbl_t**)data) = p_data_tbl;
7812                         break;
7813
7814                 case RETRIEVE_SUMMARY:
7815                         if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t))))  {
7816                                 EM_DEBUG_EXCEPTION(" malloc failed...");
7817
7818                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7819                                 goto FINISH_OFF;
7820                         }
7821
7822                         memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
7823
7824                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
7825                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
7826                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
7827                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
7828                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
7829
7830                         *((emstorage_mail_tbl_t**)data) = p_data_tbl;
7831                         break;
7832
7833                 case RETRIEVE_ADDRESS:
7834                         if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t))))  {
7835                                 EM_DEBUG_EXCEPTION(" malloc failed...");
7836                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7837                                 goto FINISH_OFF;
7838                         }
7839
7840                         memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
7841                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
7842                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
7843                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
7844                         *((emstorage_mail_tbl_t**)data) = p_data_tbl;
7845                         break;
7846
7847                 default:
7848                         break;
7849         }
7850
7851
7852         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7853         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7854                 ("sqlite3_step fail:%d", rc));
7855
7856         ret = true;
7857
7858 FINISH_OFF:
7859
7860         if (err_code != NULL)
7861                 *err_code = error;
7862
7863         EM_DEBUG_FUNC_END("ret [%d]", ret);
7864         return ret;
7865 }
7866
7867 INTERNAL_FUNC int emstorage_mail_search_end(int search_handle, int transaction, int *err_code)
7868 {
7869         EM_DEBUG_FUNC_BEGIN("search_handle[%d], transaction[%d], err_code[%p]", search_handle, transaction, err_code);
7870
7871         int error = EMAIL_ERROR_NONE;
7872         int rc, ret = false;
7873
7874         if (search_handle == 0)  {
7875                 EM_DEBUG_EXCEPTION(" search_handle[%d]", search_handle);
7876                 error = EMAIL_ERROR_INVALID_PARAM;
7877                 goto FINISH_OFF;
7878         }
7879
7880         DB_STMT hStmt = (DB_STMT)search_handle;
7881
7882         EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
7883
7884         rc = sqlite3_finalize(hStmt);
7885         if (rc != SQLITE_OK)  {
7886                 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
7887                 error = EMAIL_ERROR_DB_FAILURE;
7888         }
7889
7890         ret = true;
7891
7892 FINISH_OFF:
7893         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7894         _DISCONNECT_DB;
7895
7896         if (err_code != NULL)
7897                 *err_code = error;
7898
7899         EM_DEBUG_FUNC_END("ret [%d]", ret);
7900         return ret;
7901 }
7902
7903 INTERNAL_FUNC int emstorage_change_mail(int mail_id, emstorage_mail_tbl_t* mail, int transaction, int *err_code)
7904 {
7905         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
7906
7907         if (mail_id <= 0 || !mail) {
7908                 EM_DEBUG_EXCEPTION(" mail_id[%d], mail[%p]", mail_id, mail);
7909
7910                 if (err_code != NULL)
7911                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7912                 return false;
7913         }
7914
7915         DB_STMT hStmt = NULL;
7916         char sql_query_string[QUERY_SIZE] = {0, };
7917         int rc = -1;
7918         int ret = false;
7919         int error = EMAIL_ERROR_NONE;
7920         int i = 0;
7921         sqlite3 *local_db_handle = emstorage_get_db_connection();
7922         char mailbox_id_param_string[10] = {0,};
7923
7924         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
7925
7926         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7927                 "UPDATE mail_tbl SET"
7928                 "  mail_id = ?"
7929                 ", account_id = ?"
7930                 ", mailbox_id = ?"
7931                 ", mail_size = ?"
7932                 ", server_mail_status = ?"
7933                 ", server_mailbox_name = ?"
7934                 ", server_mail_id = ?"
7935                 ", reference_mail_id = ?"
7936                 ", full_address_from = ?"
7937                 ", full_address_reply = ?"  /* 10 */
7938                 ", full_address_to = ?"
7939                 ", full_address_cc = ?"
7940                 ", full_address_bcc = ?"
7941                 ", full_address_return = ?"
7942                 ", subject = ?"
7943                 ", body_download_status = ?"
7944                 ", file_path_plain = ?"
7945                 ", file_path_html = ?"
7946                 ", date_time = ?"
7947                 ", flags_seen_field      = ?"
7948                 ", flags_deleted_field   = ?"
7949                 ", flags_flagged_field   = ?"
7950                 ", flags_answered_field  = ?"
7951                 ", flags_recent_field    = ?"
7952                 ", flags_draft_field     = ?"
7953                 ", flags_forwarded_field = ?"
7954                 ", DRM_status = ?"
7955                 ", priority = ?"
7956                 ", save_status = ?"
7957                 ", lock_status = ?"
7958                 ", message_id = ?"
7959                 ", report_status = ?"
7960                 ", preview_text = ?"
7961                 ", smime_type = ?"
7962                 ", scheduled_sending_time = ?"
7963                 ", eas_data_length = ?"
7964                 ", eas_data = ?"
7965                 " WHERE mail_id = %d AND account_id != 0 "
7966                 , mail_id);
7967
7968
7969         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7970         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7971                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7972
7973         _bind_stmt_field_data_int   (hStmt, i++, mail->mail_id);
7974         _bind_stmt_field_data_int   (hStmt, i++, mail->account_id);
7975         _bind_stmt_field_data_int   (hStmt, i++, mail->mailbox_id);
7976         _bind_stmt_field_data_int   (hStmt, i++, mail->mail_size);
7977         _bind_stmt_field_data_int   (hStmt, i++, mail->server_mail_status);
7978         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mailbox_name, 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
7979         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mail_id, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
7980         _bind_stmt_field_data_int   (hStmt, i++, mail->reference_mail_id);
7981         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
7982         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
7983         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
7984         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
7985         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
7986         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
7987         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
7988         _bind_stmt_field_data_int   (hStmt, i++, mail->body_download_status);
7989         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
7990         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
7991         _bind_stmt_field_data_int   (hStmt, i++, mail->date_time);
7992         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_seen_field);
7993         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_deleted_field);
7994         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_flagged_field);
7995         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_answered_field);
7996         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_recent_field);
7997         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_draft_field);
7998         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_forwarded_field);
7999         _bind_stmt_field_data_int   (hStmt, i++, mail->DRM_status);
8000         _bind_stmt_field_data_int   (hStmt, i++, mail->priority);
8001         _bind_stmt_field_data_int   (hStmt, i++, mail->save_status);
8002         _bind_stmt_field_data_int   (hStmt, i++, mail->lock_status);
8003         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->message_id, 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
8004         _bind_stmt_field_data_int   (hStmt, i++, mail->report_status);
8005         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8006         _bind_stmt_field_data_int   (hStmt, i++, mail->smime_type);
8007         _bind_stmt_field_data_int   (hStmt, i++, mail->scheduled_sending_time);
8008         _bind_stmt_field_data_int   (hStmt, i++, mail->eas_data_length);
8009         _bind_stmt_field_data_blob  (hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
8010
8011         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8012         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8013                 ("sqlite3_step fail:%d", rc));
8014
8015         rc = sqlite3_changes(local_db_handle);
8016         if (rc == 0)  {
8017                 EM_DEBUG_EXCEPTION(" no matched mail found...");
8018                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8019                 goto FINISH_OFF;
8020         }
8021         SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
8022         if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail->mail_id, mailbox_id_param_string, 0))
8023                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_UPDATE ] >>>> ");
8024
8025         ret = true;
8026
8027 FINISH_OFF:
8028         if (hStmt != NULL)  {
8029                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
8030                 rc = sqlite3_finalize(hStmt);
8031                 if (rc != SQLITE_OK)  {
8032                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
8033                         error = EMAIL_ERROR_DB_FAILURE;
8034                 }
8035         }
8036
8037         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8038         _DISCONNECT_DB;
8039
8040         if (err_code != NULL)
8041                 *err_code = error;
8042
8043         EM_DEBUG_FUNC_END("ret [%d]", ret);
8044         return ret;
8045 }
8046
8047 /**
8048   *  emstorage_clean_save_status(int save_status, int  *err_code) - set the all mail status to the set value
8049   *
8050   *
8051   **/
8052 INTERNAL_FUNC int emstorage_clean_save_status(int save_status, int  *err_code)
8053 {
8054         EM_DEBUG_FUNC_BEGIN("save_status[%d], err_code[%p]", save_status, err_code);
8055
8056         EM_IF_NULL_RETURN_VALUE(err_code, false);
8057
8058         int ret = false;
8059         int error = EMAIL_ERROR_NONE;
8060         int rc = 0;
8061         char sql_query_string[QUERY_SIZE] = {0, };
8062         sqlite3 *local_db_handle = emstorage_get_db_connection();
8063
8064         memset(sql_query_string, 0x00, sizeof(sql_query_string));
8065         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);
8066         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8067
8068         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8069         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8070                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8071
8072         rc = sqlite3_changes(local_db_handle);
8073         if (rc == 0) {
8074                 EM_DEBUG_LOG(" No Matched Mail Exists ");
8075                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8076         }
8077
8078         ret = true;
8079
8080 FINISH_OFF:
8081
8082         if (err_code != NULL)
8083                 *err_code = error;
8084
8085         EM_DEBUG_FUNC_END("ret [%d]", ret);
8086         return ret;
8087 }
8088
8089 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)
8090 {
8091         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);
8092         int i = 0;
8093         int error = EMAIL_ERROR_NONE;
8094         int rc = 0;
8095         int ret = false;
8096         int cur_mail_id_string = 0;
8097         int mail_id_string_buffer_length = 0;
8098         char  sql_query_string[QUERY_SIZE] = {0, };
8099         char *mail_id_string_buffer = NULL;
8100         char *parameter_string = NULL;
8101         sqlite3 *local_db_handle = emstorage_get_db_connection();
8102         email_mail_attribute_type target_mail_attribute_type = 0;
8103
8104         if (!mail_ids  || !field_name || account_id == 0) {
8105                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8106                 if (err_code != NULL)
8107                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8108                 return false;
8109         }
8110
8111         if( (error = emcore_get_attribute_type_by_mail_field_name(field_name, &target_mail_attribute_type)) != EMAIL_ERROR_NONE) {
8112                 EM_DEBUG_EXCEPTION("emstorageemcore_get_attribute_type_by_mail_field_name failed [%d]", error);
8113                 if (err_code != NULL)
8114                         *err_code = error;
8115                 return false;
8116         }
8117
8118         /* Generating mail id list string */
8119         mail_id_string_buffer_length = MAIL_ID_STRING_LENGTH * mail_ids_count;
8120
8121         mail_id_string_buffer = em_malloc(mail_id_string_buffer_length);
8122
8123         if(!mail_id_string_buffer) {
8124                 EM_DEBUG_EXCEPTION("em_malloc failed");
8125                 error = EMAIL_ERROR_OUT_OF_MEMORY;
8126                 goto FINISH_OFF;
8127         }
8128
8129         for(i = 0; i < mail_ids_count; i++)
8130                 cur_mail_id_string += SNPRINTF_OFFSET(mail_id_string_buffer, cur_mail_id_string, mail_id_string_buffer_length, "%d,", mail_ids[i]);
8131
8132         if(EM_SAFE_STRLEN(mail_id_string_buffer) > 1)
8133                 mail_id_string_buffer[EM_SAFE_STRLEN(mail_id_string_buffer) - 1] = NULL_CHAR;
8134
8135         /* Generating notification parameter string */
8136         parameter_string = em_malloc(mail_id_string_buffer_length + EM_SAFE_STRLEN(field_name) + 2);
8137
8138         if(!parameter_string) {
8139                 EM_DEBUG_EXCEPTION("em_malloc failed");
8140                 error = EMAIL_ERROR_OUT_OF_MEMORY;
8141                 goto FINISH_OFF;
8142         }
8143
8144         SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", field_name, 0x01, mail_id_string_buffer);
8145
8146         /* Write query string */
8147         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);
8148
8149         EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
8150
8151         /* Execute query */
8152         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8153         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8154         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8155                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8156         if (sqlite3_changes(local_db_handle) == 0)
8157                 EM_DEBUG_LOG("no mail matched...");
8158
8159         ret = true;
8160
8161 FINISH_OFF:
8162         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8163         _DISCONNECT_DB;
8164
8165         if (ret && parameter_string && !emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, target_mail_attribute_type, parameter_string, value))
8166                 EM_DEBUG_EXCEPTION("emcore_notify_storage_event failed : NOTI_MAIL_FIELD_UPDATE [%s,%d]", field_name, value);
8167
8168         EM_SAFE_FREE(mail_id_string_buffer);
8169         EM_SAFE_FREE(parameter_string);
8170
8171         if (err_code != NULL)
8172                 *err_code = error;
8173
8174         EM_DEBUG_FUNC_END("error [%d]", error);
8175         return ret;
8176 }
8177
8178 #ifdef __FEATURE_BODY_SEARCH__
8179 INTERNAL_FUNC int emstorage_change_mail_text_field(int mail_id, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
8180 {
8181         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
8182
8183         if (mail_id <= 0 || !mail_text) {
8184                 EM_DEBUG_EXCEPTION(" mail_id[%d], mail_text[%p]", mail_id, mail_text);
8185                 if (err_code != NULL)
8186                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8187                 return false;
8188         }
8189
8190         int ret = false;
8191         int error = EMAIL_ERROR_NONE;
8192         DB_STMT hStmt = NULL;
8193         char sql_query_string[QUERY_SIZE] = {0, };
8194
8195         int i = 0;
8196         int rc = 0;
8197
8198         sqlite3 *local_db_handle = emstorage_get_db_connection();
8199         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8200
8201         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8202                 "UPDATE mail_text_tbl SET"
8203                 " body_text = ?"
8204                 " WHERE mail_id = %d AND account_id != 0"
8205                 , mail_id);
8206         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8207
8208         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8209         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8210                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8211
8212         i = 0;
8213         _bind_stmt_field_data_string(hStmt, i++, (char *)mail_text->body_text, 0, -1);
8214
8215         if (hStmt != NULL) {
8216                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8217                 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF;}, ("sqlite3_step fail:%d", rc));
8218                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF;}, ("sqlite3_step fail:%d", rc));
8219
8220                 rc = sqlite3_changes(local_db_handle);
8221                 if (rc == 0)  {
8222                         EM_DEBUG_EXCEPTION(" no matched mail found...");
8223                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
8224                         goto FINISH_OFF;
8225                 }
8226         }
8227
8228         ret = true;
8229
8230 FINISH_OFF:
8231
8232         if (hStmt != NULL) {
8233                 rc = sqlite3_finalize(hStmt);
8234                 if (rc != SQLITE_OK) {
8235                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
8236                         error = EMAIL_ERROR_DB_FAILURE;
8237                 }
8238                 hStmt = NULL;
8239         }
8240
8241         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8242         _DISCONNECT_DB;
8243
8244         if (err_code != NULL)
8245                 *err_code = error;
8246
8247         EM_DEBUG_FUNC_END("ret [%d]", ret);
8248         return ret;
8249 }
8250 #endif
8251
8252 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)
8253 {
8254         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
8255
8256         int ret = false;
8257         int error = EMAIL_ERROR_NONE;
8258         int move_flag = 0;
8259         int rc = 0;
8260         int i = 0;
8261         int mailbox_id = 0;
8262         DB_STMT hStmt = NULL;
8263         char sql_query_string[QUERY_SIZE] = {0, };
8264         char mailbox_id_param_string[10] = {0,};
8265         sqlite3 *local_db_handle = NULL;
8266
8267         if (mail_id <= 0 || !mail)  {
8268                 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], mail[%p]", mail_id, type, mail);
8269                 if (err_code != NULL)
8270                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8271                 return false;
8272         }
8273
8274         local_db_handle = emstorage_get_db_connection();
8275
8276         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8277
8278         switch (type) {
8279                 case APPEND_BODY:
8280                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8281                                 "UPDATE mail_tbl SET"
8282                                 "  body_download_status = ?"
8283                                 ", file_path_plain = ?"
8284                                 ", file_path_html = ?"
8285                                 ", flags_seen_field      = ?"
8286                                 ", flags_deleted_field   = ?"
8287                                 ", flags_flagged_field   = ?"
8288                                 ", flags_answered_field  = ?"
8289                                 ", flags_recent_field    = ?"
8290                                 ", flags_draft_field     = ?"
8291                                 ", flags_forwarded_field = ?"
8292                                 ", DRM_status = ?"
8293                                 ", attachment_count = ?"
8294                                 ", preview_text= ?"
8295                                 ", meeting_request_status = ? "
8296                                 ", message_class = ? "
8297                                 ", digest_type = ? "
8298                                 ", smime_type = ? "
8299                                 " WHERE mail_id = %d AND account_id != 0"
8300                                 , mail_id);
8301
8302
8303                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8304                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8305                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8306                          i = 0;
8307
8308                         _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8309                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8310                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8311                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_seen_field);
8312                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_deleted_field);
8313                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_flagged_field);
8314                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_answered_field);
8315                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_recent_field);
8316                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_draft_field);
8317                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_forwarded_field);
8318                         _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
8319                         _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
8320                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
8321                         _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
8322                         _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
8323                         _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
8324                         _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
8325                         break;
8326
8327                 case UPDATE_MAILBOX: {
8328                                 int err;
8329                                 emstorage_mailbox_tbl_t *mailbox_tbl;
8330
8331                                 if ((err = emstorage_get_mailbox_by_id(mail->mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
8332                                         EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_id failed [%d]", err);
8333                                         goto FINISH_OFF;
8334                                 }
8335
8336                                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8337                                         "UPDATE mail_tbl SET"
8338                                         " mailbox_id = '%d'"
8339                                         ",mailbox_type = '%d'"
8340                                         " WHERE mail_id = %d AND account_id != 0"
8341                                         , mailbox_tbl->mailbox_id
8342                                         , mailbox_tbl->mailbox_type
8343                                         , mail_id);
8344                                         move_flag = 1;
8345
8346
8347                                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8348                                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8349                                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8350
8351                                 i = 0;
8352                                 _bind_stmt_field_data_string(hStmt, i++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
8353
8354                                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL); /*prevent 26251*/
8355
8356                         }
8357                         break;
8358
8359                 case UPDATE_FLAG:
8360                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8361                                 "UPDATE mail_tbl SET"
8362                                 " flags_seen_field      = %d"
8363                                 ",flags_deleted_field   = %d"
8364                                 ",flags_flagged_field   = %d"
8365                                 ",flags_answered_field  = %d"
8366                                 ",flags_recent_field    = %d"
8367                                 ",flags_draft_field     = %d"
8368                                 ",flags_forwarded_field = %d"
8369                                 "  WHERE mail_id = %d AND account_id != 0"
8370                                 , mail->flags_seen_field
8371                                 , mail->flags_deleted_field
8372                                 , mail->flags_flagged_field
8373                                 , mail->flags_answered_field
8374                                 , mail->flags_recent_field
8375                                 , mail->flags_draft_field
8376                                 , mail->flags_forwarded_field
8377                                 , mail_id);
8378                         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8379
8380
8381                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8382                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8383                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8384
8385                         break;
8386
8387                 case UPDATE_EXTRA_FLAG:
8388                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8389                                 "UPDATE mail_tbl SET"
8390                                 "  priority = %d"
8391                                 ", save_status = %d"
8392                                 ", lock_status = %d"
8393                                 ", report_status = %d"
8394                                 ", DRM_status = %d"
8395                                 " WHERE mail_id = %d AND account_id != 0"
8396                                 , mail->priority
8397                                 , mail->save_status
8398                                 , mail->lock_status
8399                                 , mail->report_status
8400                                 , mail->DRM_status
8401                                 , mail_id);
8402                         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8403
8404
8405                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8406                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8407                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8408                         break;
8409
8410                 case UPDATE_STICKY_EXTRA_FLAG:
8411                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8412                                 "UPDATE mail_tbl SET"
8413                                 "  lock_status = %d"
8414                                 "  WHERE mail_id = %d AND account_id != 0"
8415                                 , mail->lock_status
8416                                 , mail_id);
8417                         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8418
8419
8420                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8421                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8422                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8423                         break;
8424
8425                 case UPDATE_MAIL:
8426                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8427                                 "UPDATE mail_tbl SET"
8428                                 "  full_address_from = ?"
8429                                 ", full_address_reply = ?"
8430                                 ", full_address_to = ?"
8431                                 ", full_address_cc = ?"
8432                                 ", full_address_bcc = ?"
8433                                 ", full_address_return = ?"
8434                                 ", subject = ?"
8435                                 ", file_path_plain = ?"
8436                                 ", date_time = ?"
8437                                 ", flags_seen_field = ?"
8438                                 ", flags_deleted_field = ?"
8439                                 ", flags_flagged_field = ?"
8440                                 ", flags_answered_field = ?"
8441                                 ", flags_recent_field = ?"
8442                                 ", flags_draft_field = ?"
8443                                 ", flags_forwarded_field = ?"
8444                                 ", priority = ?"
8445                                 ", save_status = ?"
8446                                 ", lock_status = ?"
8447                                 ", report_status = ?"
8448                                 ", DRM_status = ?"
8449                                 ", file_path_html = ?"
8450                                 ", file_path_mime_entity = ?"
8451                                 ", mail_size = ?"
8452                                 ", preview_text = ?"
8453                                 ", body_download_status = ?"
8454                                 ", attachment_count = ?"
8455                                 ", inline_content_count = ?"
8456                                 ", meeting_request_status = ?"
8457                                 ", message_class = ?"
8458                                 ", digest_type = ?"
8459                                 ", smime_type = ?"
8460                                 ", scheduled_sending_time = ?"
8461                                 ", eas_data_length = ?"
8462                                 ", eas_data = ?"
8463                                 " WHERE mail_id = %d AND account_id != 0"
8464                                 , mail_id);
8465
8466
8467                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8468                         EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
8469                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8470                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8471                         i = 0;
8472                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
8473                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
8474                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
8475                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
8476                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
8477                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
8478                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8479                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8480                         _bind_stmt_field_data_time_t(hStmt, i++, mail->date_time);
8481                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_seen_field);
8482                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_deleted_field);
8483                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_flagged_field);
8484                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_answered_field);
8485                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_recent_field);
8486                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_draft_field);
8487                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_forwarded_field);
8488                         _bind_stmt_field_data_int   (hStmt, i++, mail->priority);
8489                         _bind_stmt_field_data_int   (hStmt, i++, mail->save_status);
8490                         _bind_stmt_field_data_int   (hStmt, i++, mail->lock_status);
8491                         _bind_stmt_field_data_int   (hStmt, i++, mail->report_status);
8492                         _bind_stmt_field_data_int   (hStmt, i++, mail->DRM_status);
8493                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8494                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
8495                         _bind_stmt_field_data_int   (hStmt, i++, mail->mail_size);
8496                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8497                         _bind_stmt_field_data_int   (hStmt, i++, mail->body_download_status);
8498                         _bind_stmt_field_data_int   (hStmt, i++, mail->attachment_count);
8499                         _bind_stmt_field_data_int   (hStmt, i++, mail->inline_content_count);
8500                         _bind_stmt_field_data_int   (hStmt, i++, mail->meeting_request_status);
8501                         _bind_stmt_field_data_int   (hStmt, i++, mail->message_class);
8502                         _bind_stmt_field_data_int   (hStmt, i++, mail->digest_type);
8503                         _bind_stmt_field_data_int   (hStmt, i++, mail->smime_type);
8504                         _bind_stmt_field_data_int   (hStmt, i++, mail->scheduled_sending_time);
8505                         _bind_stmt_field_data_int   (hStmt, i++, mail->eas_data_length);
8506                         _bind_stmt_field_data_blob  (hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
8507                         break;
8508
8509                 case UPDATE_DATETIME:  {
8510                         time_t now = time(NULL);
8511
8512                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8513                                 "UPDATE mail_tbl SET"
8514                                 " date_time = '%d'"
8515                                 " WHERE mail_id = %d AND account_id != 0"
8516                                 , (int)now
8517                                 , mail_id);
8518
8519                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8520                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8521                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8522                         break;
8523                 }
8524
8525                 case UPDATE_FROM_CONTACT_INFO:
8526                         EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_FROM_CONTACT_INFO");
8527                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8528                                 "UPDATE mail_tbl SET"
8529                                 " email_address_sender = ?,"
8530                                 " WHERE mail_id = %d",
8531                                 mail_id);
8532
8533                         hStmt = NULL;
8534
8535                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8536                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8537                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8538                         i = 0;
8539                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8540                         break;
8541
8542                 case UPDATE_TO_CONTACT_INFO:
8543                         EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_TO_CONTACT_INFO");
8544                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8545                                 "UPDATE mail_tbl SET"
8546                                 " email_address_recipient = ?,"
8547                                 " WHERE mail_id = %d",
8548                                 mail_id);
8549
8550                         hStmt = NULL;
8551
8552                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8553                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8554                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8555                         i = 0;
8556                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8557                         break;
8558
8559                         case UPDATE_ALL_CONTACT_INFO:
8560                                 EM_DEBUG_LOG("emstorage_change_mail_field - mail change type is UPDATE_ALL_CONTACT_INFO");
8561                                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8562                                         "UPDATE mail_tbl SET"
8563                                         " email_address_sender = ?,"
8564                                         " email_address_recipient = ?,"
8565                                         " WHERE mail_id = %d",
8566                                         mail_id);
8567
8568                                 hStmt = NULL;
8569
8570                                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8571                                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8572                                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8573                                 i = 0;
8574                                 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8575                                 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8576                                 break;
8577
8578
8579 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
8580                         case UPDATE_PARTIAL_BODY_DOWNLOAD:
8581
8582                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8583                         "UPDATE mail_tbl SET"
8584                         "  body_download_status = ?"
8585                         ", file_path_plain = ?"
8586                         ", file_path_html = ?"
8587                         ", attachment_count = ?"
8588                         ", inline_content_count = ?"
8589                         ", preview_text= ?"
8590                         " WHERE mail_id = %d"
8591                         , mail_id);
8592
8593
8594                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8595                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8596                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8597                          i = 0;
8598
8599                         _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8600                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8601                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html,  0, TEXT_2_LEN_IN_MAIL_TBL);
8602                         _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
8603                         _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
8604                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->preview_text,    0, PREVIEWBODY_LEN_IN_MAIL_TBL);
8605
8606                         break;
8607
8608 #endif
8609                 case UPDATE_FILE_PATH:
8610                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8611                                 "UPDATE mail_tbl SET"
8612                                 ", file_path_plain = ?"
8613                                 ", file_path_html = ?"
8614                                 ", file_path_mime_entity = ?"
8615                                 " WHERE mail_id = %d"
8616                                 , mail_id);
8617
8618
8619                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8620                         EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
8621                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8622                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8623                         i = 0;
8624                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8625                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8626                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
8627                         break;
8628
8629                 default:
8630                         EM_DEBUG_LOG(" type[%d]", type);
8631
8632                         error = EMAIL_ERROR_INVALID_PARAM;
8633                         goto FINISH_OFF;
8634         }
8635
8636         if (hStmt != NULL)  {
8637
8638         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8639                 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
8640                 ("sqlite3_step fail:%d", rc));
8641                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8642                         ("sqlite3_step fail:%d", rc));
8643                 rc = sqlite3_changes(local_db_handle);
8644                 if (rc == 0)  {
8645                         EM_DEBUG_EXCEPTION(" no matched mail found...");
8646
8647                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
8648                         goto FINISH_OFF;
8649                 }
8650         }
8651
8652         if (mail->account_id == 0) {
8653                 emstorage_mail_tbl_t* mail_for_account_tbl = NULL;
8654                 if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_ACCOUNT, &mail_for_account_tbl, true, &error) || !mail_for_account_tbl) {
8655                         EM_DEBUG_EXCEPTION(" emstorage_get_mail_field_by_id failed - %d", error);
8656 /*               */
8657                         goto FINISH_OFF;
8658                 }
8659                 mail->account_id = mail_for_account_tbl->account_id;
8660                 if (mail_for_account_tbl)
8661                         emstorage_free_mail(&mail_for_account_tbl, 1, NULL);
8662         }
8663
8664         ret = true;
8665
8666 FINISH_OFF:
8667
8668         if (hStmt != NULL)  {
8669                 rc = sqlite3_finalize(hStmt);
8670                 if (rc != SQLITE_OK)  {
8671                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
8672                         error = EMAIL_ERROR_DB_FAILURE;
8673                 }
8674                 hStmt = NULL;
8675         }
8676
8677         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8678         _DISCONNECT_DB;
8679
8680         /*h.gahlaut@samsung.com: Moving publication of notification after committing transaction to DB */
8681
8682         if (ret == true &&  move_flag != 1) {
8683                 if (!emstorage_get_mailbox_id_by_mailbox_type(mail->account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &mailbox_id, false, &error))
8684                         EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_id_by_mailbox_type failed - %d", error);
8685
8686                 if (mail->mailbox_id == mailbox_id) {
8687                         SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
8688                         if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, mailbox_id_param_string, type))
8689                                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_UPDATE ] >>>> ");
8690                 }
8691                 else {
8692                         /* h.gahlaut@samsung.com: Jan 10, 2011 Publishing noti to refresh outbox when email sending status changes */
8693                         if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, NULL, type))
8694                                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_UPDATE ] ");
8695                 }
8696         }
8697
8698         if (err_code != NULL)
8699                 *err_code = error;
8700
8701         EM_DEBUG_FUNC_END("ret [%d]", ret);
8702         return ret;
8703 }
8704 INTERNAL_FUNC int emstorage_increase_mail_id(int *mail_id, int transaction, int *err_code)
8705 {
8706         EM_DEBUG_FUNC_BEGIN("mail_id[%p], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
8707
8708         int rc, ret = false;
8709         int error = EMAIL_ERROR_NONE;
8710         int latest_mail_id = 0;
8711         sqlite3 *local_db_handle = NULL;
8712         char *sql = "SELECT MAX(mail_id) FROM mail_tbl;";
8713         char **result = NULL;
8714
8715 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
8716         _timedlock_shm_mutex(&mapped_for_generating_mail_id, 2);
8717 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
8718
8719         ret = vconf_get_int(VCONF_KEY_LATEST_MAIL_ID, &latest_mail_id);
8720         if (ret < 0 || latest_mail_id == 0) {
8721                 EM_DEBUG_LOG("vconf_get_int() failed [%d] or latest_mail_id is zero", ret);
8722
8723                 local_db_handle = emstorage_get_db_connection();
8724
8725                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
8726                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
8727                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
8728                 if (NULL == result[1])
8729                         rc = 1;
8730                 else
8731                         rc = atoi(result[1]) + 1;
8732
8733                 sqlite3_free_table(result);
8734                 latest_mail_id = rc;
8735         }
8736
8737         latest_mail_id++;
8738
8739         ret = vconf_set_int(VCONF_KEY_LATEST_MAIL_ID, latest_mail_id);
8740
8741         if (mail_id)
8742                 *mail_id = latest_mail_id;
8743
8744 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
8745         _unlockshm_mutex(&mapped_for_generating_mail_id);
8746 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
8747
8748         ret = true;
8749
8750 FINISH_OFF:
8751         _DISCONNECT_DB;
8752
8753         if (err_code != NULL)
8754                 *err_code = error;
8755
8756         EM_DEBUG_FUNC_END("ret [%d]", ret);
8757         return ret;
8758 }
8759
8760
8761 INTERNAL_FUNC int emstorage_add_mail(emstorage_mail_tbl_t *mail_tbl_data, int get_id, int transaction, int *err_code)
8762 {
8763         EM_PROFILE_BEGIN(profile_emstorage_add_mail);
8764         EM_DEBUG_FUNC_BEGIN("mail_tbl_data[%p], get_id[%d], transaction[%d], err_code[%p]", mail_tbl_data, get_id, transaction, err_code);
8765
8766         if (!mail_tbl_data)  {
8767                 EM_DEBUG_EXCEPTION("mail_tbl_data[%p], get_id[%d]", mail_tbl_data, get_id);
8768                 if (err_code != NULL)
8769                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8770                 return false;
8771         }
8772
8773         int rc, ret = false;
8774         int error = EMAIL_ERROR_NONE;
8775         char sql_query_string[QUERY_SIZE] = {0, };
8776         DB_STMT hStmt = NULL;
8777         sqlite3 *local_db_handle = emstorage_get_db_connection();
8778
8779         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8780
8781         if (get_id)  {
8782                 /*  increase unique id */
8783                 char *sql = "SELECT max(rowid) FROM mail_tbl;";
8784                 char **result;
8785
8786                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
8787                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
8788                         ("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle)));
8789
8790                 if (NULL == result[1])
8791                         rc = 1;
8792                 else
8793                         rc = atoi(result[1])+1;
8794
8795                 sqlite3_free_table(result);
8796
8797                 mail_tbl_data->mail_id   = rc;
8798                 mail_tbl_data->thread_id = rc;
8799         }
8800
8801         if (mail_tbl_data->date_time == 0)
8802                 mail_tbl_data->date_time = time(NULL);
8803
8804         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8805                 "INSERT INTO mail_tbl VALUES "
8806                 "( ?" /*  mail_id */
8807                 ", ?" /*  account_id */
8808                 ", ?" /*  mailbox_id */
8809                 ", ?" /*  mailbox_type */
8810                 ", ?" /*  subject */
8811                 ", ?" /*  date_time */
8812                 ", ?" /*  server_mail_status */
8813                 ", ?" /*  server_mailbox_name */
8814                 ", ?" /*  server_mail_id */
8815                 ", ?" /*  message_id */
8816                 ", ?" /*  reference_mail_id */
8817                 ", ?" /*  full_address_from */
8818                 ", ?" /*  full_address_reply */
8819                 ", ?" /*  full_address_to */
8820                 ", ?" /*  full_address_cc */
8821                 ", ?" /*  full_address_bcc */
8822                 ", ?" /*  full_address_return */
8823                 ", ?" /*  email_address_sender */
8824                 ", ?" /*  email_address_recipient */
8825                 ", ?" /*  alias_sender */
8826                 ", ?" /*  alias_recipient */
8827                 ", ?" /*  body_download_status */
8828                 ", ?" /*  file_path_plain */
8829                 ", ?" /*  file_path_html */
8830                 ", ?" /*  file_path_mime_entity */
8831                 ", ?" /*  mail_size */
8832                 ", ?" /*  flags_seen_field */
8833                 ", ?" /*  flags_deleted_field */
8834                 ", ?" /*  flags_flagged_field */
8835                 ", ?" /*  flags_answered_field */
8836                 ", ?" /*  flags_recent_field */
8837                 ", ?" /*  flags_draft_field */
8838                 ", ?" /*  flags_forwarded_field */
8839                 ", ?" /*  DRM_status */
8840                 ", ?" /*  priority */
8841                 ", ?" /*  save_status */
8842                 ", ?" /*  lock_status */
8843                 ", ?" /*  report_status */
8844                 ", ?" /*  attachment_count */
8845                 ", ?" /*  inline_content_count */
8846                 ", ?" /*  thread_id */
8847                 ", ?" /*  thread_item_count */
8848                 ", ?" /*  preview_text */
8849                 ", ?" /*  meeting_request_status */
8850                 ", ?" /*  message_class */
8851                 ", ?" /*  digest_type */
8852                 ", ?" /*  smime_type */
8853                 ", ?" /*  scheduled_sending_time */
8854                 ", ?" /*  eas_data_length */
8855                 ", ?" /*  eas_data */
8856                 ")");
8857
8858         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8859         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8860                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8861
8862         _bind_stmt_field_data_int   (hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mail_id);
8863         _bind_stmt_field_data_int   (hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, mail_tbl_data->account_id);
8864         _bind_stmt_field_data_int   (hStmt, MAILBOX_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_id);
8865         _bind_stmt_field_data_int   (hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_type);
8866         _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8867         _bind_stmt_field_data_int   (hStmt, DATETIME_IDX_IN_MAIL_TBL, mail_tbl_data->date_time);
8868         _bind_stmt_field_data_int   (hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->server_mail_status);
8869         _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);
8870         _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);
8871         _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);
8872         _bind_stmt_field_data_int   (hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, mail_tbl_data->reference_mail_id);
8873         _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);
8874         _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);
8875         _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);
8876         _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);
8877         _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);
8878         _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);
8879         _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);
8880         _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);
8881         _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);
8882         _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);
8883         _bind_stmt_field_data_int   (hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->body_download_status);
8884         _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);
8885         _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);
8886         _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);
8887         _bind_stmt_field_data_int   (hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, mail_tbl_data->mail_size);
8888         _bind_stmt_field_data_int   (hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_seen_field);
8889         _bind_stmt_field_data_int   (hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_deleted_field);
8890         _bind_stmt_field_data_int   (hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_flagged_field);
8891         _bind_stmt_field_data_int   (hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_answered_field);
8892         _bind_stmt_field_data_int   (hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_recent_field);
8893         _bind_stmt_field_data_int   (hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_draft_field);
8894         _bind_stmt_field_data_int   (hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_forwarded_field);
8895         _bind_stmt_field_data_int   (hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->DRM_status);
8896         _bind_stmt_field_data_int   (hStmt, PRIORITY_IDX_IN_MAIL_TBL, mail_tbl_data->priority);
8897         _bind_stmt_field_data_int   (hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->save_status);
8898         _bind_stmt_field_data_int   (hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->lock_status);
8899         _bind_stmt_field_data_int   (hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->report_status);
8900         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->attachment_count);
8901         _bind_stmt_field_data_int   (hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->inline_content_count);
8902         _bind_stmt_field_data_int   (hStmt, THREAD_ID_IDX_IN_MAIL_TBL, mail_tbl_data->thread_id);
8903         _bind_stmt_field_data_int   (hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->thread_item_count);
8904         _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8905         _bind_stmt_field_data_int   (hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->meeting_request_status);
8906         _bind_stmt_field_data_int   (hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, mail_tbl_data->message_class);
8907         _bind_stmt_field_data_int   (hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->digest_type);
8908         _bind_stmt_field_data_int   (hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->smime_type);
8909         _bind_stmt_field_data_int   (hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->scheduled_sending_time);
8910         _bind_stmt_field_data_int   (hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, mail_tbl_data->eas_data_length);
8911         _bind_stmt_field_data_blob  (hStmt, EAS_DATA_IDX_IN_MAIL_TBL, (void*)mail_tbl_data->eas_data, mail_tbl_data->eas_data_length);
8912
8913         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8914         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
8915         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
8916         ret = true;
8917
8918 FINISH_OFF:
8919
8920         if (hStmt != NULL)  {
8921                 rc = sqlite3_finalize(hStmt);
8922                 if (rc != SQLITE_OK)  {
8923                         EM_DEBUG_LOG("sqlite3_finalize failed [%d]", rc);
8924                         error = EMAIL_ERROR_DB_FAILURE;
8925                 }
8926         }
8927
8928         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8929         _DISCONNECT_DB;
8930
8931
8932         if (err_code != NULL)
8933                 *err_code = error;
8934
8935         EM_PROFILE_END(profile_emstorage_add_mail);
8936         EM_DEBUG_FUNC_END("ret [%d]", ret);
8937         return ret;
8938 }
8939
8940 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)
8941 {
8942         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);
8943
8944         int rc, ret = false, i, cur_conditional_clause = 0;
8945         int error = EMAIL_ERROR_NONE;
8946         int target_account_id;
8947         char sql_query_string[QUERY_SIZE] = {0, }, conditional_clause[QUERY_SIZE] = {0, };
8948         emstorage_mailbox_tbl_t *result_mailbox = NULL;
8949         email_mailbox_type_e target_mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED;
8950         char* target_mailbox_name = NULL;
8951
8952         if (!mail_ids || input_mailbox_id <= 0) {
8953                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8954                 if (err_code != NULL)
8955                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8956                 return false;
8957         }
8958
8959         sqlite3 *local_db_handle = emstorage_get_db_connection();
8960
8961         if ((error = emstorage_get_mailbox_by_id(input_mailbox_id, &result_mailbox)) != EMAIL_ERROR_NONE || !result_mailbox) {
8962                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", error);
8963                 goto FINISH_OFF;
8964         }
8965
8966         if (result_mailbox->mailbox_name) {
8967                 if (strstr(result_mailbox->mailbox_name, "'")) {
8968                         target_mailbox_name = em_replace_all_string(result_mailbox->mailbox_name, "'", "''");
8969                 } else {
8970                         target_mailbox_name = strdup(result_mailbox->mailbox_name);
8971                 }
8972         }
8973
8974         target_mailbox_type = result_mailbox->mailbox_type;
8975         target_account_id   = result_mailbox->account_id;
8976         emstorage_free_mailbox(&result_mailbox, 1, NULL);
8977
8978         cur_conditional_clause = SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id in (");
8979
8980         for(i = 0; i < number_of_mails; i++)
8981                 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, QUERY_SIZE, "%d,", mail_ids[i]);
8982
8983         /* prevent 34415 */
8984         char *last_comma = rindex(conditional_clause, ',');
8985         if(last_comma) *last_comma = ')'; /* replace , with ) */
8986
8987         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8988
8989         /* Updating a mail_tbl */
8990
8991         memset(sql_query_string, 0x00, QUERY_SIZE);
8992         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);
8993         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8994
8995         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8996         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
8997                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8998
8999         /* Updating a mail_attachment_tbl */
9000         memset(sql_query_string, 0x00, QUERY_SIZE);
9001         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);
9002         EM_DEBUG_LOG("Query [%s]", sql_query_string);
9003
9004         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9005         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9006                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9007
9008         /* Updating a mail_meeting_tbl */
9009         memset(sql_query_string, 0x00, QUERY_SIZE);
9010         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);
9011         EM_DEBUG_LOG("Query [%s]", sql_query_string);
9012
9013         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9014         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9015                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9016
9017 #ifdef __FEATURE_BODY_SEARCH__
9018         /* Updating mail_text_tbl */
9019         memset(sql_query_string, 0x00, QUERY_SIZE);
9020         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);
9021         EM_DEBUG_LOG("Query [%s]", sql_query_string);
9022
9023         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9024         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9025                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9026 #endif
9027
9028         /* Updating a mail_read_mail_uid_tbl */
9029         memset(conditional_clause, 0x00, QUERY_SIZE);
9030         cur_conditional_clause = SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE local_uid in (");
9031
9032         for(i = 0; i < number_of_mails; i++)
9033                 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, QUERY_SIZE, "%d,", mail_ids[i]);
9034
9035         /* prevent 34415 */
9036         last_comma = rindex(conditional_clause, ',');
9037         if(last_comma) *last_comma = ')'; /* replace , with ) */
9038
9039         memset(sql_query_string, 0x00, QUERY_SIZE);
9040         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);
9041         EM_DEBUG_LOG("Query [%s]", sql_query_string);
9042
9043         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9044         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9045                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9046
9047         ret = true;
9048
9049 FINISH_OFF:
9050         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9051         _DISCONNECT_DB;
9052
9053         EM_SAFE_FREE(target_mailbox_name);
9054
9055         if (err_code != NULL)
9056                 *err_code = error;
9057
9058         EM_DEBUG_FUNC_END("ret [%d]", ret);
9059         return ret;
9060 }
9061
9062 INTERNAL_FUNC int emstorage_delete_mail(int mail_id, int from_server, int transaction, int *err_code)
9063 {
9064         EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9065
9066         if (!mail_id)  {
9067                 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
9068                 if (err_code != NULL)
9069                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9070                 return false;
9071         }
9072
9073         int rc, ret = false;
9074         int error = EMAIL_ERROR_NONE;
9075         char sql_query_string[QUERY_SIZE] = {0, };
9076
9077         sqlite3 *local_db_handle = emstorage_get_db_connection();
9078         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9079
9080         memset(sql_query_string, 0x00, sizeof(sql_query_string));
9081
9082         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id = %d ", mail_id);
9083         EM_DEBUG_LOG("Query [%s]", sql_query_string);
9084
9085
9086         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9087         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9088                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9089
9090         ret = true;
9091
9092 FINISH_OFF:
9093         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9094         _DISCONNECT_DB;
9095
9096         if (err_code != NULL)
9097                 *err_code = error;
9098
9099         EM_DEBUG_FUNC_END("ret [%d]", ret);
9100         return ret;
9101 }
9102
9103 INTERNAL_FUNC int emstorage_delete_multiple_mails(int mail_ids[], int number_of_mails, int transaction, int *err_code)
9104 {
9105         EM_DEBUG_FUNC_BEGIN("mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", mail_ids, number_of_mails, transaction, err_code);
9106
9107         int rc, ret = false, i, cur_sql_query_string = 0;
9108         int error = EMAIL_ERROR_NONE;
9109         char sql_query_string[QUERY_SIZE] = {0, };
9110
9111         if (!mail_ids) {
9112                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9113                 if (err_code != NULL)
9114                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9115                 return false;
9116         }
9117
9118         sqlite3 *local_db_handle = emstorage_get_db_connection();
9119         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9120
9121         cur_sql_query_string = SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id in (");
9122
9123         for(i = 0; i < number_of_mails; i++)
9124                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, QUERY_SIZE, "%d,", mail_ids[i]);
9125
9126         /* prevent 34414 */
9127         char *last_comma = rindex(sql_query_string, ',');
9128         *last_comma = ')'; /* replace , with ) */
9129
9130         EM_DEBUG_LOG("Query [%s]", sql_query_string);
9131
9132         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9133         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9134                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9135
9136 #ifdef __FEATURE_BODY_SEARCH__
9137         /* delete mail_text from mail_text_tbl */
9138         cur_sql_query_string = SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE mail_id in (");
9139
9140         for(i = 0; i < number_of_mails; i++)
9141                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, QUERY_SIZE, "%d,", mail_ids[i]);
9142
9143         last_comma = rindex(sql_query_string, ',');
9144         *last_comma = ')'; /* replace , with ) */
9145
9146         EM_DEBUG_LOG("Query [%s]", sql_query_string);
9147
9148         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9149         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
9150                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9151 #endif
9152
9153         ret = true;
9154
9155 FINISH_OFF:
9156         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9157         _DISCONNECT_DB;
9158
9159         if (err_code != NULL)
9160                 *err_code = error;
9161
9162         EM_DEBUG_FUNC_END("ret [%d]", ret);
9163         return ret;
9164 }
9165
9166 INTERNAL_FUNC int emstorage_delete_mail_by_account(int account_id, int transaction, int *err_code)
9167 {
9168         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
9169
9170         if (account_id < FIRST_ACCOUNT_ID)  {
9171                 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
9172
9173                 if (err_code != NULL)
9174                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9175                 return false;
9176         }
9177
9178         int rc, ret = false;
9179         int error = EMAIL_ERROR_NONE;
9180         char sql_query_string[QUERY_SIZE] = {0, };
9181
9182         sqlite3 *local_db_handle = emstorage_get_db_connection();
9183         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9184
9185         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d", account_id);
9186         EM_DEBUG_LOG("Query [%s]", sql_query_string);
9187
9188         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9189         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9190                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9191
9192         rc = sqlite3_changes(local_db_handle);
9193         if (rc == 0)  {
9194                 EM_DEBUG_EXCEPTION(" no mail found...");
9195                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9196         }
9197
9198         /* Delete all mails  mail_read_mail_uid_tbl table based on account id */
9199         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
9200         EM_DEBUG_LOG("Query [%s]", sql_query_string);
9201
9202         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9203         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9204                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9205
9206         rc = sqlite3_changes(local_db_handle);
9207         if (rc == 0)  {
9208                 EM_DEBUG_EXCEPTION("no mail found...");
9209                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9210         }
9211
9212 #ifdef __FEATURE_BODY_SEARCH__
9213         /* Delete all mail_text in mail_text_tbl table based on account id */
9214         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE account_id = %d", account_id);
9215         EM_DEBUG_LOG("Query [%s]", sql_query_string);
9216
9217         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9218         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9219                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9220
9221         rc = sqlite3_changes(local_db_handle);
9222         if (rc == 0) {
9223                 EM_DEBUG_EXCEPTION("no mail found...");
9224                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9225         }
9226 #endif
9227
9228         if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_WITH_ACCOUNT, account_id, 0 , NULL, 0))
9229                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_DELETE_ALL ]");
9230
9231         ret = true;
9232
9233 FINISH_OFF:
9234
9235         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9236         _DISCONNECT_DB;
9237
9238         if (err_code != NULL)
9239                 *err_code = error;
9240
9241         EM_DEBUG_FUNC_END("ret [%d]", ret);
9242         return ret;
9243 }
9244
9245 INTERNAL_FUNC int emstorage_delete_mail_by_mailbox(int account_id, int mailbox_id, int transaction, int *err_code)
9246 {
9247         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
9248
9249         if (account_id < FIRST_ACCOUNT_ID || mailbox_id == 0)  {
9250                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_id[%d]", account_id, mailbox_id);
9251                 if (err_code != NULL)
9252                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9253                 return false;
9254         }
9255
9256         int rc, ret = false;
9257         int error = EMAIL_ERROR_NONE;
9258         char sql_query_string[QUERY_SIZE] = {0, };
9259
9260         sqlite3 *local_db_handle = emstorage_get_db_connection();
9261
9262         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9263
9264         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d AND mailbox_id = '%d'", account_id, mailbox_id);
9265         EM_DEBUG_LOG("Query [%s]", sql_query_string);
9266
9267         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9268         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9269                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9270
9271
9272         /* Delete Mails from mail_read_mail_uid_tbl */
9273         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);
9274         EM_DEBUG_LOG("Query [%s]", sql_query_string);
9275
9276         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9277         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9278                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9279
9280 #ifdef __FEATURE_BODY_SEARCH__
9281         /* Delete Mails from mail_text_tbl */
9282         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);
9283         EM_DEBUG_LOG("Query [%s]", sql_query_string);
9284
9285         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9286         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9287                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9288 #endif
9289
9290         if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_ALL, account_id, mailbox_id , 0, 0))
9291                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_DELETE_ALL ] >>>> ");
9292
9293         ret = true;
9294
9295 FINISH_OFF:
9296         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9297         _DISCONNECT_DB;
9298
9299         if (err_code != NULL)
9300                 *err_code = error;
9301
9302         EM_DEBUG_FUNC_END("ret [%d]", ret);
9303         return ret;
9304 }
9305
9306 INTERNAL_FUNC int emstorage_free_mail(emstorage_mail_tbl_t** mail_list, int count, int *err_code)
9307 {
9308         EM_DEBUG_FUNC_BEGIN("mail_list[%p], count[%d], err_code[%p]", mail_list, count, err_code);
9309
9310         if (count > 0)  {
9311                 if ((mail_list == NULL) || (*mail_list == NULL))  {
9312                         EM_DEBUG_EXCEPTION("mail_ilst[%p], count[%d]", mail_list, count);
9313
9314                         if (err_code)
9315                                 *err_code = EMAIL_ERROR_INVALID_PARAM;
9316                         return false;
9317                 }
9318
9319                 emstorage_mail_tbl_t* p = *mail_list;
9320                 int i = 0;
9321
9322                 for (; i < count; i++, p++) {
9323                         EM_SAFE_FREE(p->server_mailbox_name);
9324                         EM_SAFE_FREE(p->server_mail_id);
9325                         EM_SAFE_FREE(p->full_address_from);
9326                         EM_SAFE_FREE(p->full_address_reply);
9327                         EM_SAFE_FREE(p->full_address_to);
9328                         EM_SAFE_FREE(p->full_address_cc);
9329                         EM_SAFE_FREE(p->full_address_bcc);
9330                         EM_SAFE_FREE(p->full_address_return);
9331                         EM_SAFE_FREE(p->subject);
9332                         EM_SAFE_FREE(p->file_path_plain);
9333                         EM_SAFE_FREE(p->file_path_html);
9334                         EM_SAFE_FREE(p->file_path_mime_entity);
9335                         EM_SAFE_FREE(p->message_id);
9336                         EM_SAFE_FREE(p->email_address_sender);
9337                         EM_SAFE_FREE(p->email_address_recipient);
9338                         EM_SAFE_FREE(p->preview_text);
9339                         EM_SAFE_FREE(p->alias_sender);
9340                         EM_SAFE_FREE(p->alias_recipient);
9341                         EM_SAFE_FREE(p->eas_data);
9342                 }
9343                 EM_SAFE_FREE(*mail_list);
9344         }
9345
9346         if (err_code != NULL)
9347                 *err_code = EMAIL_ERROR_NONE;
9348
9349         EM_DEBUG_FUNC_END();
9350         return true;
9351 }
9352
9353 #ifdef __FEATURE_BODY_SEARCH__
9354 INTERNAL_FUNC int emstorage_free_mail_text(emstorage_mail_text_tbl_t** mail_text_list, int count, int *err_code)
9355 {
9356         EM_DEBUG_FUNC_BEGIN("mail_text_list[%p], count[%d], err_code[%p]", mail_text_list, count, err_code);
9357
9358         if (count > 0) {
9359                 if ((mail_text_list == NULL) || (*mail_text_list == NULL)) {
9360                         EM_DEBUG_EXCEPTION("mail_text_list[%p], count[%d]", mail_text_list, count);
9361
9362                         if (err_code)
9363                                 *err_code = EMAIL_ERROR_INVALID_PARAM;
9364                         return false;
9365                 }
9366
9367                 emstorage_mail_text_tbl_t *p = *mail_text_list;
9368                 int i = 0;
9369
9370                 for (; i < count; i++, p++) {
9371                         EM_SAFE_FREE(p->body_text);
9372                 }
9373                 EM_SAFE_FREE(*mail_text_list);
9374         }
9375
9376         if (err_code != NULL)
9377                 *err_code = EMAIL_ERROR_NONE;
9378
9379         EM_DEBUG_FUNC_END();
9380         return true;
9381 }
9382 #endif
9383
9384
9385 INTERNAL_FUNC int emstorage_get_attachment_count(int mail_id, int *count, int transaction, int *err_code)
9386 {
9387         EM_DEBUG_FUNC_BEGIN("mail_id[%d], count[%p], transaction[%d], err_code[%p]", mail_id, count, transaction, err_code);
9388
9389         if (mail_id <= 0 || !count)  {
9390                 EM_DEBUG_EXCEPTION("mail_id[%d], count[%p]", mail_id, count);
9391                 if (err_code != NULL)
9392                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9393                 return false;
9394         }
9395
9396         int rc = -1, ret = false;
9397         int error = EMAIL_ERROR_NONE;
9398         char sql_query_string[QUERY_SIZE] = {0, };
9399
9400         sqlite3 *local_db_handle = emstorage_get_db_connection();
9401         EMSTORAGE_START_READ_TRANSACTION(transaction);
9402
9403         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
9404
9405         char **result;
9406
9407         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
9408         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9409                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9410
9411         *count = atoi(result[1]);
9412         sqlite3_free_table(result);
9413
9414         ret = true;
9415
9416 FINISH_OFF:
9417
9418         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
9419         _DISCONNECT_DB;
9420
9421         if (err_code != NULL)
9422                 *err_code = error;
9423
9424         EM_DEBUG_FUNC_END("ret [%d]", ret);
9425         return ret;
9426 }
9427
9428 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)
9429 {
9430         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);
9431
9432         if (input_mail_id <= 0 || !output_attachment_list || !output_attachment_count)  {
9433                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9434                 return EMAIL_ERROR_INVALID_PARAM;
9435         }
9436
9437         int                         error = EMAIL_ERROR_NONE;
9438         int                         i = 0;
9439         int                         rc = -1;
9440         char                      **result = NULL;
9441         char                        sql_query_string[QUERY_SIZE] = {0, };
9442         emstorage_attachment_tbl_t* p_data_tbl = NULL;
9443         DB_STMT hStmt = NULL;
9444         sqlite3 *local_db_handle = emstorage_get_db_connection();
9445
9446         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
9447         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", input_mail_id);
9448         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
9449         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9450                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9451
9452         *output_attachment_count = atoi(result[1]);
9453         sqlite3_free_table(result);
9454
9455         if(*output_attachment_count == 0) {
9456                 error = EMAIL_ERROR_NONE;
9457                 goto FINISH_OFF;
9458         }
9459
9460         p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * (*output_attachment_count));
9461
9462         if (!p_data_tbl)  {
9463                 EM_DEBUG_EXCEPTION("em_malloc failed...");
9464                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9465                 goto FINISH_OFF;
9466         }
9467
9468         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE mail_id = %d ORDER BY attachment_id", input_mail_id);
9469         EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
9470
9471         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9472         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)));
9473
9474         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9475         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },   ("sqlite3_step fail:%d", rc));
9476
9477         if (rc == SQLITE_DONE)  {
9478                 EM_DEBUG_EXCEPTION("no matched attachment found...");
9479                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
9480                 goto FINISH_OFF;
9481         }
9482         for (i = 0; i < *output_attachment_count; i++)  {
9483                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9484                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
9485                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
9486                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
9487                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9488                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9489                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9490                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9491                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9492                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
9493                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9494                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9495
9496                 EM_DEBUG_LOG("attachment[%d].attachment_id : %d", i, p_data_tbl[i].attachment_id);
9497                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9498                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9499         }
9500
9501 FINISH_OFF:
9502
9503         if (error == EMAIL_ERROR_NONE)
9504                 *output_attachment_list = p_data_tbl;
9505         else if (p_data_tbl != NULL)
9506                 emstorage_free_attachment(&p_data_tbl, *output_attachment_count, NULL);
9507
9508         if (hStmt) {
9509                 rc = sqlite3_finalize(hStmt);
9510                 if (rc != SQLITE_OK)  {
9511                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
9512                         error = EMAIL_ERROR_DB_FAILURE;
9513                 }
9514         }
9515
9516         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
9517
9518         _DISCONNECT_DB;
9519
9520         EM_DEBUG_FUNC_END("error [%d]", error);
9521         return error;
9522 }
9523
9524 INTERNAL_FUNC int emstorage_get_attachment(int attachment_id, emstorage_attachment_tbl_t** attachment, int transaction, int *err_code)
9525 {
9526         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], transaction[%d], err_code[%p]", attachment_id, attachment, transaction, err_code);
9527
9528         if (attachment_id <= 0 || !attachment)  {
9529                 EM_DEBUG_EXCEPTION("attachment_id[%d], attachment[%p]", attachment_id, attachment);
9530                 if (err_code != NULL)
9531                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9532                 return false;
9533         }
9534
9535         emstorage_attachment_tbl_t* p_data_tbl = NULL;
9536         int rc, ret = false;
9537         int error = EMAIL_ERROR_NONE;
9538         char sql_query_string[QUERY_SIZE] = {0, };
9539
9540         sqlite3 *local_db_handle = emstorage_get_db_connection();
9541         EMSTORAGE_START_READ_TRANSACTION(transaction);
9542
9543         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_id = %d",  attachment_id);
9544
9545         sqlite3_stmt* hStmt = NULL;
9546
9547         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9548         EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
9549
9550         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9551                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9552
9553
9554         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9555         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9556                 ("sqlite3_step fail:%d", rc));
9557
9558         if (rc == SQLITE_DONE)  {
9559                 EM_DEBUG_EXCEPTION("no matched attachment found...");
9560                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
9561                 goto FINISH_OFF;
9562         }
9563
9564         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1)))  {
9565                 EM_DEBUG_EXCEPTION("malloc failed...");
9566                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9567                 goto FINISH_OFF;
9568         }
9569
9570         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9571         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
9572         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
9573         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
9574         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9575         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9576         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9577         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9578         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9579         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
9580         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9581         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9582
9583 #ifdef __ATTACHMENT_OPTI__
9584                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
9585                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
9586 #endif
9587
9588         ret = true;
9589
9590 FINISH_OFF:
9591         if (ret == true)
9592                 *attachment = p_data_tbl;
9593
9594         if (hStmt != NULL)  {
9595                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
9596
9597                 rc = sqlite3_finalize(hStmt);
9598                 if (rc != SQLITE_OK)  {
9599                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
9600                         error = EMAIL_ERROR_DB_FAILURE;
9601                 }
9602         }
9603
9604         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
9605         _DISCONNECT_DB;
9606
9607         if (err_code != NULL)
9608                 *err_code = error;
9609
9610         EM_DEBUG_FUNC_END("ret [%d]", ret);
9611         return ret;
9612 }
9613
9614 INTERNAL_FUNC int emstorage_get_attachment_nth(int mail_id, int nth, emstorage_attachment_tbl_t** attachment_tbl, int transaction, int *err_code)
9615 {
9616         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);
9617
9618         if (mail_id <= 0 || nth <= 0 || !attachment_tbl)  {
9619                 EM_DEBUG_EXCEPTION(" mail_id[%d], nth[%d], attachment[%p]", mail_id, nth, attachment_tbl);
9620
9621                 if (err_code != NULL)
9622                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9623                 return false;
9624         }
9625
9626         emstorage_attachment_tbl_t* p_data_tbl = NULL;
9627         char *p = NULL;
9628         int rc, ret = false;
9629         int error = EMAIL_ERROR_NONE;
9630         char sql_query_string[QUERY_SIZE] = {0, };
9631
9632         sqlite3 *local_db_handle = emstorage_get_db_connection();
9633         EMSTORAGE_START_READ_TRANSACTION(transaction);
9634
9635         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));
9636         EM_DEBUG_LOG("query = [%s]", sql_query_string);
9637
9638         DB_STMT hStmt = NULL;
9639
9640         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9641         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9642                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9643
9644
9645         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9646         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9647                 ("sqlite3_step fail:%d", rc));
9648
9649         if (rc == SQLITE_DONE)  {
9650                 EM_DEBUG_EXCEPTION(" no matched attachment found...");
9651                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
9652                 goto FINISH_OFF;
9653         }
9654
9655         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1)))  {
9656                 EM_DEBUG_EXCEPTION(" malloc failed...");
9657                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9658                 goto FINISH_OFF;
9659         }
9660
9661         p_data_tbl->attachment_id = sqlite3_column_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9662         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
9663                 p_data_tbl->attachment_name = cpy_str(p);
9664         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
9665                 p_data_tbl->attachment_path = cpy_str(p);
9666         p_data_tbl->attachment_size = sqlite3_column_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
9667         p_data_tbl->mail_id = sqlite3_column_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9668         p_data_tbl->account_id = sqlite3_column_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9669         p_data_tbl->mailbox_id = sqlite3_column_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9670         p_data_tbl->attachment_save_status = sqlite3_column_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9671         p_data_tbl->attachment_drm_type = sqlite3_column_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9672         p_data_tbl->attachment_drm_method = sqlite3_column_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
9673         p_data_tbl->attachment_inline_content_status = sqlite3_column_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9674         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
9675                 p_data_tbl->attachment_mime_type = cpy_str(p);
9676 #ifdef __ATTACHMENT_OPTI__
9677                 p_data_tbl->encoding = sqlite3_column_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
9678                 if ((p = (char *)sqlite3_column_text(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
9679                         p_data_tbl->section= cpy_str(p);
9680 #endif
9681         ret = true;
9682
9683 FINISH_OFF:
9684         if (ret == true)
9685                 *attachment_tbl = p_data_tbl;
9686
9687         if (hStmt != NULL)  {
9688                 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
9689
9690                 rc = sqlite3_finalize(hStmt);
9691                 if (rc != SQLITE_OK)  {
9692                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
9693                         error = EMAIL_ERROR_DB_FAILURE;
9694                 }
9695         }
9696
9697         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
9698         _DISCONNECT_DB;
9699
9700         if (err_code != NULL)
9701                 *err_code = error;
9702
9703         EM_DEBUG_FUNC_END("ret [%d]", ret);
9704         return ret;
9705 }
9706
9707 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)
9708 {
9709         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], attachment[%p], transaction[%d], err_code[%p]", mail_id, type, attachment, transaction, err_code);
9710
9711         if (mail_id <= 0 || !attachment)  {
9712                 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], attachment[%p]", mail_id, type, attachment);
9713                 if (err_code != NULL)
9714                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9715                 return false;;
9716         }
9717
9718         int rc, ret = false;
9719         int error = EMAIL_ERROR_NONE;
9720         DB_STMT hStmt = NULL;
9721         char sql_query_string[QUERY_SIZE] = {0, };
9722
9723         int i = 0;
9724
9725         sqlite3 *local_db_handle = emstorage_get_db_connection();
9726
9727         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9728
9729         switch (type)  {
9730                 case UPDATE_MAILBOX:
9731                                 EM_DEBUG_LOG("UPDATE_MAILBOX");
9732                         if (!attachment->mailbox_id)  {
9733                                 EM_DEBUG_EXCEPTION(" attachment->mailbox_id[%d]", attachment->mailbox_id);
9734                                 error = EMAIL_ERROR_INVALID_PARAM;
9735                                 goto FINISH_OFF;
9736                         }
9737                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9738                                 "UPDATE mail_attachment_tbl SET mailbox_id = ? WHERE mail_id = %d", mail_id);
9739
9740                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9741                         EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
9742                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9743                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9744
9745                         _bind_stmt_field_data_int(hStmt, i++, attachment->mailbox_id);
9746                         break;
9747
9748                 case UPDATE_SAVENAME:
9749                         EM_DEBUG_LOG("UPDATE_SAVENAME");
9750                         if (!attachment->attachment_path)  {
9751                                 EM_DEBUG_EXCEPTION(" attachment->attachment_path[%p]", attachment->attachment_path);
9752                                 error = EMAIL_ERROR_INVALID_PARAM;
9753                                 goto FINISH_OFF;
9754                         }
9755
9756                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9757                                 "UPDATE mail_attachment_tbl SET"
9758                                 "  attachment_size = ?"
9759                                 ", attachment_save_status = 1"
9760                                 ", attachment_path = ?"
9761                                 " WHERE mail_id = %d"
9762                                 " AND attachment_id = %d"
9763                                 , attachment->mail_id
9764                                 , attachment->attachment_id);
9765
9766
9767                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9768                         EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
9769                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9770                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9771
9772                         _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_size);
9773                         _bind_stmt_field_data_string(hStmt, i++, (char *)attachment->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
9774                         break;
9775
9776                 default:
9777                         EM_DEBUG_LOG("type[%d]", type);
9778                         error = EMAIL_ERROR_INVALID_PARAM;
9779                         goto FINISH_OFF;
9780         }
9781         EM_DEBUG_LOG("query = [%s]", sql_query_string);
9782
9783         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9784         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9785                 ("sqlite3_step fail:%d", rc));
9786         ret = true;
9787
9788 FINISH_OFF:
9789         if (hStmt != NULL)  {
9790                 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
9791                 rc = sqlite3_finalize(hStmt);
9792                 if (rc != SQLITE_OK)  {
9793                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
9794                         error = EMAIL_ERROR_DB_FAILURE;
9795                 }
9796         }
9797
9798         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9799         _DISCONNECT_DB;
9800
9801         if (err_code != NULL)
9802                 *err_code = error;
9803         EM_DEBUG_FUNC_END("ret [%d]", ret);
9804         return ret;
9805 }
9806
9807
9808 INTERNAL_FUNC int emstorage_rename_mailbox(int input_mailbox_id, char *input_new_mailbox_name, char *input_new_mailbox_alias, int input_transaction)
9809 {
9810         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);
9811
9812         int rc = 0;
9813         int ret = false;
9814         int error = EMAIL_ERROR_NONE;
9815         char sql_query_string[QUERY_SIZE] = {0, };
9816         sqlite3 *local_db_handle = NULL;
9817         int account_id = 0;
9818         emstorage_mailbox_tbl_t *old_mailbox_data = NULL;
9819         char *replaced_mailbox_name = NULL;
9820         char *replaced_alias = NULL;
9821
9822         if (input_mailbox_id <= 0 || !input_new_mailbox_name || !input_new_mailbox_alias)  {
9823                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9824                 return EMAIL_ERROR_INVALID_PARAM;
9825         }
9826
9827         if (strstr(input_new_mailbox_name, "'")) {
9828                 replaced_mailbox_name = em_replace_all_string(input_new_mailbox_name, "'", "''");
9829         } else {
9830                 replaced_mailbox_name = strdup(input_new_mailbox_name);
9831         }
9832
9833         if (strstr(input_new_mailbox_alias, "'")) {
9834                 replaced_alias = em_replace_all_string(input_new_mailbox_alias, "'", "''");
9835         } else {
9836                 replaced_alias = strdup(input_new_mailbox_alias);
9837         }
9838
9839         local_db_handle = emstorage_get_db_connection();
9840
9841         if ((error = emstorage_get_mailbox_by_id(input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
9842                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", error);
9843                 goto FINISH_OFF;
9844         }
9845         account_id = old_mailbox_data->account_id;
9846
9847         EMSTORAGE_START_WRITE_TRANSACTION(input_transaction, error);
9848
9849         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9850                 "UPDATE mail_box_tbl SET"
9851                 " mailbox_name = '%s'"
9852                 ",alias = '%s'"
9853                 " WHERE mailbox_id = %d"
9854                 , replaced_mailbox_name
9855                 , replaced_alias
9856                 , input_mailbox_id);
9857
9858         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9859
9860         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9861                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9862
9863         if (sqlite3_changes(local_db_handle) == 0)
9864                 EM_DEBUG_LOG("no mail_meeting_tbl matched...");
9865
9866         ret = true;
9867
9868 FINISH_OFF:
9869
9870         EMSTORAGE_FINISH_WRITE_TRANSACTION(input_transaction, ret, error);
9871
9872         if (ret) {
9873                 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME, account_id, input_mailbox_id, input_new_mailbox_name, 0))
9874                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAILBOX_RENAME ] >>>> ");
9875         }
9876         else {
9877                 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME_FAIL, account_id, input_mailbox_id, input_new_mailbox_name, error))
9878                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAILBOX_RENAME_FAIL ] >>>> ");
9879         }
9880
9881         EM_SAFE_FREE(replaced_mailbox_name);
9882         EM_SAFE_FREE(replaced_alias);
9883
9884         if (old_mailbox_data)
9885                 emstorage_free_mailbox(&old_mailbox_data, 1, NULL);
9886
9887         _DISCONNECT_DB;
9888
9889         EM_DEBUG_FUNC_END("error [%d]", error);
9890         return error;
9891 }
9892
9893 INTERNAL_FUNC int emstorage_get_new_attachment_no(int *attachment_no, int *err_code)
9894 {
9895         EM_DEBUG_FUNC_BEGIN("attachment_no [%p], err_code[%p]", attachment_no, err_code);
9896         int rc, ret = false;
9897         int error = EMAIL_ERROR_NONE;
9898         char *sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
9899         char **result;
9900
9901         if (!attachment_no)  {
9902                 EM_DEBUG_EXCEPTION("Invalid attachment");
9903                 error = EMAIL_ERROR_INVALID_PARAM;
9904                 goto FINISH_OFF;
9905         }
9906
9907         *attachment_no = -1;
9908
9909         sqlite3 *local_db_handle = emstorage_get_db_connection();
9910
9911
9912         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9913         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9914                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9915
9916         if (NULL == result[1])
9917                 rc = 1;
9918         else
9919                 rc = atoi(result[1])+1;
9920
9921         sqlite3_free_table(result);
9922
9923         *attachment_no = rc;
9924         EM_DEBUG_LOG("attachment_no [%d]", *attachment_no);
9925         ret = true;
9926
9927 FINISH_OFF:
9928         _DISCONNECT_DB;
9929
9930         if (err_code != NULL)
9931                 *err_code = error;
9932
9933         EM_DEBUG_FUNC_END("ret [%d]", ret);
9934         return ret;
9935 }
9936
9937 INTERNAL_FUNC int emstorage_add_attachment(emstorage_attachment_tbl_t* attachment_tbl, int iscopy, int transaction, int *err_code)
9938 {
9939         EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], iscopy[%d], transaction[%d], err_code[%p]", attachment_tbl, iscopy, transaction, err_code);
9940
9941         char *sql = NULL;
9942         char **result;
9943         int rc, ret = false;
9944         int error = EMAIL_ERROR_NONE;
9945         DB_STMT hStmt = NULL;
9946         char sql_query_string[QUERY_SIZE] = {0, };
9947         sqlite3 *local_db_handle = emstorage_get_db_connection();
9948
9949         if (!attachment_tbl)  {
9950                 EM_DEBUG_EXCEPTION("attachment_tbl[%p], iscopy[%d]", attachment_tbl, iscopy);
9951                 if (err_code != NULL)
9952                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9953                 return false;
9954         }
9955
9956         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9957
9958         sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
9959
9960         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9961         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9962                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9963
9964         if (NULL==result[1]) rc = 1;
9965         else rc = atoi(result[1]) + 1;
9966         sqlite3_free_table(result);
9967
9968         attachment_tbl->attachment_id = rc;
9969
9970         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9971                 "INSERT INTO mail_attachment_tbl VALUES "
9972                 "( ?"   /* attachment_id */
9973                 ", ?"   /* attachment_name */
9974                 ", ?"   /* attachment_path */
9975                 ", ?"   /* attachment_size */
9976                 ", ?"   /* mail_id */
9977                 ", ?"   /* account_id */
9978                 ", ?"   /* mailbox_id */
9979                 ", ?"   /* attachment_save_status */
9980                 ", ?"   /* attachment_drm_type */
9981                 ", ?"   /* attachment_drm_method */
9982                 ", ?"   /* attachment_inline_content_status */
9983                 ", ?"   /* attachment_mime_type */
9984 #ifdef __ATTACHMENT_OPTI__
9985                 ", ?"
9986                 ", ?"
9987 #endif
9988                 ")");
9989
9990
9991         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9992         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9993                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9994
9995         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_id);
9996         _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);
9997         _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);
9998         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_size);
9999         _bind_stmt_field_data_int   (hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mail_id);
10000         _bind_stmt_field_data_int   (hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->account_id);
10001         _bind_stmt_field_data_int   (hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mailbox_id);
10002         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_save_status);
10003         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_type);
10004         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_method);
10005         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_inline_content_status);
10006         _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);
10007 #ifdef __ATTACHMENT_OPTI__
10008         _bind_stmt_field_data_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->encoding);
10009         _bind_stmt_field_data_string(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->section, 0, ATTACHMENT_LEN_IN_MAIL_ATTACHMENT_TBL);
10010 #endif
10011
10012
10013         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10014         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
10015                 ("sqlite3_step fail:%d", rc));
10016         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10017                 ("sqlite3_step fail:%d", rc));
10018 /*
10019         SNPRINTF(sql_query_string, sizeof(sql_query_string),
10020                 "UPDATE mail_tbl SET attachment_count = 1 WHERE mail_id = %d", attachment_tbl->mail_id);
10021
10022         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10023
10024         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
10025                 ("sqlite3_exec fail:%d", rc));
10026         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10027                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10028 */
10029         rc = sqlite3_changes(local_db_handle);
10030         if (rc == 0)  {
10031                 EM_DEBUG_EXCEPTION(" no matched mail found...");
10032                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10033                 goto FINISH_OFF;
10034         }
10035
10036         ret = true;
10037
10038 FINISH_OFF:
10039         if (hStmt != NULL)  {
10040                 rc = sqlite3_finalize(hStmt);
10041                 if (rc != SQLITE_OK)  {
10042                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
10043                         error = EMAIL_ERROR_DB_FAILURE;
10044                 }
10045         }
10046
10047         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10048         _DISCONNECT_DB;
10049
10050         if (err_code != NULL)
10051         *err_code = error;
10052
10053         EM_DEBUG_FUNC_END("ret [%d]", ret);
10054         return ret;
10055 }
10056
10057 INTERNAL_FUNC int emstorage_update_attachment(emstorage_attachment_tbl_t* attachment_tbl, int transaction, int *err_code)
10058 {
10059         EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], transaction[%d], err_code[%p]", attachment_tbl, transaction, err_code);
10060
10061         int rc, ret = false, field_idx = 0;
10062         int error = EMAIL_ERROR_NONE;
10063         DB_STMT hStmt = NULL;
10064         char sql_query_string[QUERY_SIZE] = {0, };
10065
10066         if (!attachment_tbl)  {
10067                 EM_DEBUG_EXCEPTION(" attachment_tbl[%p] ", attachment_tbl);
10068                 error = EMAIL_ERROR_INVALID_PARAM;
10069                 goto FINISH_OFF;
10070         }
10071
10072         sqlite3 *local_db_handle = emstorage_get_db_connection();
10073
10074         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10075
10076         SNPRINTF(sql_query_string, sizeof(sql_query_string),
10077                 "UPDATE mail_attachment_tbl SET  "
10078                 "  attachment_name = ?"
10079                 ", attachment_path =  ?"
10080                 ", attachment_size = ?"
10081                 ", mail_id = ?"
10082                 ", account_id = ?"
10083                 ", mailbox_id = ?"
10084                 ", attachment_save_status = ?"
10085                 ", attachment_drm_type = ?"
10086                 ", attachment_drm_method = ?"
10087                 ", attachment_inline_content_status = ? "
10088                 ", attachment_mime_type = ? "
10089                 " WHERE attachment_id = ?;");
10090
10091
10092         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10093
10094         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10095                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10096
10097         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10098         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10099         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_size);
10100         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->mail_id);
10101         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->account_id);
10102         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->mailbox_id);
10103         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_save_status);
10104         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_drm_type);
10105         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_drm_method);
10106         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_inline_content_status);
10107         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_mime_type, 0, ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL);
10108         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_id);
10109
10110
10111         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10112         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
10113                 ("sqlite3_step fail:%d", rc));
10114         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10115                 ("sqlite3_step fail:%d", rc));
10116 /*
10117         SNPRINTF(sql_query_string, sizeof(sql_query_string),
10118                 "UPDATE mail_tbl SET attachment_count = 1 WHERE mail_id = %d", attachment_tbl->mail_id);
10119
10120         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10121
10122         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
10123                 ("sqlite3_exec fail:%d", rc));
10124         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10125                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10126 */
10127         rc = sqlite3_changes(local_db_handle);
10128         if (rc == 0)  {
10129                 EM_DEBUG_EXCEPTION(" no matched mail found...");
10130                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10131                 goto FINISH_OFF;
10132         }
10133
10134         ret = true;
10135
10136 FINISH_OFF:
10137         if (hStmt != NULL)  {
10138                 rc = sqlite3_finalize(hStmt);
10139                 if (rc != SQLITE_OK)  {
10140                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
10141                         error = EMAIL_ERROR_DB_FAILURE;
10142                 }
10143         }
10144
10145         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10146         _DISCONNECT_DB;
10147
10148         if (err_code != NULL)
10149         *err_code = error;
10150
10151         EM_DEBUG_FUNC_END("ret [%d]", ret);
10152         return ret;
10153 }
10154
10155 INTERNAL_FUNC int emstorage_delete_attachment_on_db(int attachment_id, int transaction, int *err_code)
10156 {
10157         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], transaction[%d], err_code[%p]", attachment_id, transaction, err_code);
10158
10159         if (attachment_id < 0)  {
10160                 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
10161                 if (err_code != NULL)
10162                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10163                 return false;
10164         }
10165
10166         int rc, ret = false;
10167         int error = EMAIL_ERROR_NONE;
10168         char sql_query_string[QUERY_SIZE] = {0, };
10169
10170         sqlite3 *local_db_handle = emstorage_get_db_connection();
10171
10172         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10173
10174         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
10175
10176         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10177         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10178                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10179
10180         ret = true;
10181
10182 FINISH_OFF:
10183         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10184         _DISCONNECT_DB;
10185
10186         if (err_code)
10187                 *err_code = error;
10188
10189         EM_DEBUG_FUNC_END("ret [%d]", ret);
10190         return ret;
10191 }
10192
10193 INTERNAL_FUNC int emstorage_delete_all_attachments_of_mail(int mail_id, int transaction, int *err_code)
10194 {
10195         EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
10196         int rc, ret = false;
10197         int error = EMAIL_ERROR_NONE;
10198         char sql_query_string[QUERY_SIZE] = {0, };
10199         sqlite3 *local_db_handle = NULL;
10200
10201         if (mail_id <= 0)  {
10202                 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
10203                 if (err_code != NULL)
10204                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10205                 return false;
10206         }
10207
10208         local_db_handle = emstorage_get_db_connection();
10209
10210         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10211
10212         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
10213
10214         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10215         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10216                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10217
10218         ret = true;
10219
10220 FINISH_OFF:
10221         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10222         _DISCONNECT_DB;
10223
10224         if (err_code)
10225                 *err_code = error;
10226
10227         EM_DEBUG_FUNC_END("ret [%d]", ret);
10228         return ret;
10229 }
10230
10231 INTERNAL_FUNC int emstorage_delete_attachment_all_on_db(int account_id, char *mailbox, int transaction, int *err_code)
10232 {
10233         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox, transaction, err_code);
10234
10235         int error = EMAIL_ERROR_NONE;
10236         int rc, ret = false;
10237         char sql_query_string[QUERY_SIZE] = {0, };
10238         char *replaced_mailbox = NULL;
10239
10240         sqlite3 *local_db_handle = emstorage_get_db_connection();
10241
10242         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10243
10244         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl");
10245
10246         if (account_id != ALL_ACCOUNT) /*  '0' means all account */
10247                 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);
10248
10249         if (mailbox)    /*  NULL means all mailbox_name */ {
10250                 if (strstr(mailbox, "'")) {
10251                         replaced_mailbox = em_replace_all_string(mailbox, "'", "''");
10252                 } else {
10253                         replaced_mailbox = strdup(mailbox);
10254                 }
10255
10256                 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);
10257                 EM_SAFE_FREE(replaced_mailbox); /*prevent 49434*/
10258         }
10259         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10260         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10261                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10262
10263         ret = true;
10264
10265 FINISH_OFF:
10266         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10267         _DISCONNECT_DB;
10268
10269         if (err_code != NULL)
10270                 *err_code = error;
10271
10272         EM_DEBUG_FUNC_END("ret [%d]", ret);
10273         return ret;
10274 }
10275
10276 INTERNAL_FUNC int emstorage_free_attachment(emstorage_attachment_tbl_t** attachment_tbl_list, int count, int *err_code)
10277 {
10278         EM_DEBUG_FUNC_BEGIN("attachment_tbl_list[%p], count[%d], err_code[%p]", attachment_tbl_list, count, err_code);
10279
10280         if (count > 0)  {
10281                 if ((attachment_tbl_list == NULL) || (*attachment_tbl_list == NULL))  {
10282                         EM_DEBUG_EXCEPTION(" attachment_tbl_list[%p], count[%d]", attachment_tbl_list, count);
10283                         if (err_code != NULL)
10284                                 *err_code = EMAIL_ERROR_INVALID_PARAM;
10285                         return false;
10286                 }
10287
10288                 emstorage_attachment_tbl_t* p = *attachment_tbl_list;
10289                 int i;
10290
10291                 for (i = 0; i < count; i++)  {
10292                         EM_SAFE_FREE(p[i].attachment_name);
10293                         EM_SAFE_FREE(p[i].attachment_path);
10294                         EM_SAFE_FREE(p[i].attachment_mime_type);
10295 #ifdef __ATTACHMENT_OPTI__
10296                         EM_SAFE_FREE(p[i].section);
10297 #endif
10298                 }
10299
10300                 EM_SAFE_FREE(p);
10301                 *attachment_tbl_list = NULL;
10302         }
10303
10304         if (err_code != NULL)
10305                 *err_code = EMAIL_ERROR_NONE;
10306         EM_DEBUG_FUNC_END();
10307         return true;
10308 }
10309
10310
10311
10312 INTERNAL_FUNC int emstorage_begin_transaction(void *d1, void *d2, int *err_code)
10313 {
10314         EM_PROFILE_BEGIN(emStorageBeginTransaction);
10315         int ret = true;
10316
10317         ENTER_CRITICAL_SECTION(_transactionBeginLock);
10318
10319         /*  wait for the trnasaction authority to be changed. */
10320         while (g_transaction)  {
10321                 EM_DEBUG_LOG(">>>>>>>> Wait for the transaction authority to be changed");
10322                 usleep(50000);
10323         }
10324
10325         /*  take the transaction authority. */
10326         g_transaction = true;
10327
10328         LEAVE_CRITICAL_SECTION(_transactionBeginLock);
10329
10330         sqlite3 *local_db_handle = emstorage_get_db_connection();
10331         int rc;
10332         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN immediate;", NULL, NULL, NULL), rc);
10333         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
10334                 ("SQL(BEGIN) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
10335
10336         if (ret == false) {
10337                 if (err_code != NULL) *err_code = EMAIL_ERROR_DB_FAILURE;
10338                 ENTER_CRITICAL_SECTION(_transactionEndLock);
10339                 g_transaction = false;
10340                 LEAVE_CRITICAL_SECTION(_transactionEndLock);
10341         }
10342
10343         EM_PROFILE_END(emStorageBeginTransaction);
10344         EM_DEBUG_FUNC_END("ret [%d]", ret);
10345         return ret;
10346 }
10347
10348 INTERNAL_FUNC int emstorage_commit_transaction(void *d1, void *d2, int *err_code)
10349 {
10350         EM_DEBUG_FUNC_BEGIN();
10351         int ret = true;
10352         sqlite3 *local_db_handle = emstorage_get_db_connection();
10353
10354         ENTER_CRITICAL_SECTION(_transactionEndLock);
10355
10356         int rc;
10357         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
10358         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; }, ("SQL(END) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
10359         /*  release the transaction authority. */
10360         g_transaction = false;
10361
10362         LEAVE_CRITICAL_SECTION(_transactionEndLock);
10363         if (ret == false && err_code != NULL)
10364                 *err_code = EMAIL_ERROR_DB_FAILURE;
10365
10366         EM_DEBUG_FUNC_END("ret [%d]", ret);
10367         return ret;
10368 }
10369
10370 INTERNAL_FUNC int emstorage_rollback_transaction(void *d1, void *d2, int *err_code)
10371 {
10372         EM_DEBUG_FUNC_BEGIN();
10373         int ret = true;
10374         sqlite3 *local_db_handle = emstorage_get_db_connection();
10375         int rc;
10376
10377         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "ROLLBACK;", NULL, NULL, NULL), rc);
10378         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
10379                 ("SQL(ROLLBACK) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
10380
10381         ENTER_CRITICAL_SECTION(_transactionEndLock);
10382
10383         /*  release the transaction authority. */
10384         g_transaction = false;
10385
10386         LEAVE_CRITICAL_SECTION(_transactionEndLock);
10387
10388         if (ret == false && err_code != NULL)
10389                 *err_code = EMAIL_ERROR_DB_FAILURE;
10390
10391         EM_DEBUG_FUNC_END("ret [%d]", ret);
10392         return ret;
10393 }
10394
10395 INTERNAL_FUNC int emstorage_is_mailbox_full(int account_id, email_mailbox_t *mailbox, int *result, int *err_code)
10396 {
10397         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], result[%p], err_code[%p]", account_id, mailbox, result, err_code);
10398
10399         if (account_id < FIRST_ACCOUNT_ID || !mailbox || !result)  {
10400                 if (mailbox)
10401                         EM_DEBUG_EXCEPTION("Invalid Parameter. accoun_id[%d], mailbox[%p]", account_id, mailbox);
10402
10403                 if (err_code != NULL)
10404                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10405
10406                 return false;
10407         }
10408
10409         int ret = false;
10410         int error = EMAIL_ERROR_NONE;
10411         int mail_count = 0;
10412
10413         if (!emstorage_get_mail_count(account_id, mailbox->mailbox_id, &mail_count, NULL, true, &error)) {
10414                 EM_DEBUG_EXCEPTION("emstorage_get_mail_count failed [%d]", error);
10415                 goto FINISH_OFF;
10416         }
10417
10418         if (mailbox) {
10419                 EM_DEBUG_LOG("mail_count[%d] mail_slot_size[%d]", mail_count, mailbox->mail_slot_size);
10420                 if (mail_count >= mailbox->mail_slot_size)
10421                         *result = true;
10422                 else
10423                         *result = false;
10424
10425                 ret = true;
10426         }
10427
10428         ret = true;
10429 FINISH_OFF:
10430
10431         if (err_code != NULL)
10432                 *err_code = error;
10433
10434         EM_DEBUG_FUNC_END("ret [%d]", ret);
10435         return ret;
10436 }
10437
10438 INTERNAL_FUNC int emstorage_clear_mail_data(int transaction, int *err_code)
10439 {
10440         EM_DEBUG_FUNC_BEGIN("transaction[%d], err_code[%p]", transaction, err_code);
10441
10442         int rc, ret = false;
10443         int error = EMAIL_ERROR_NONE;
10444         char sql_query_string[QUERY_SIZE] = {0, };
10445
10446         const email_db_object_t* tables = _g_db_tables;
10447         const email_db_object_t* indexes = _g_db_indexes;
10448
10449         sqlite3 *local_db_handle = emstorage_get_db_connection();
10450         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10451
10452         if (!emstorage_delete_dir(MAILHOME, &error)) {
10453                 EM_DEBUG_EXCEPTION(" emstorage_delete_dir failed - %d", error);
10454
10455                 goto FINISH_OFF;
10456         }
10457
10458         mkdir(MAILHOME, DIRECTORY_PERMISSION);
10459         mkdir(MAILTEMP, DIRECTORY_PERMISSION);
10460
10461         /*  first clear index. */
10462         while (indexes->object_name)  {
10463                 if (indexes->data_flag)  {
10464                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP index %s", indexes->object_name);
10465                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10466                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10467                                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10468                 }
10469                 indexes++;
10470         }
10471
10472         while (tables->object_name)  {
10473                 if (tables->data_flag)  {
10474                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP table %s", tables->object_name);
10475                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10476                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10477                                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10478                 }
10479
10480                 tables++;
10481         }
10482         ret = true;
10483
10484 FINISH_OFF:
10485         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10486         _DISCONNECT_DB;
10487
10488         if (err_code != NULL)
10489                 *err_code = error;
10490
10491         EM_DEBUG_FUNC_END("ret [%d]", ret);
10492         return ret;
10493 }
10494 /*======================= DB File Utils =============================================*/
10495 #include <dirent.h>
10496 #include <sys/types.h>
10497 #define  DIR_SEPERATOR "/"
10498
10499 INTERNAL_FUNC char *emstorage_make_directory_path_from_file_path(char *file_name)
10500 {
10501         EM_DEBUG_FUNC_BEGIN("Filename [ %p ]", file_name);
10502         char delims[] = "/";
10503         char *result = NULL;
10504         gchar **token = NULL;
10505
10506         token = g_strsplit_set(file_name, delims, 1);
10507
10508         if (token && token[0]) {
10509                 EM_DEBUG_LOG(">>>> Directory_name [ %s ]", token[0]);
10510                 result = EM_SAFE_STRDUP(token[0]);
10511         } else
10512                 EM_DEBUG_LOG(">>>> No Need to create Directory");
10513
10514         g_strfreev(token);
10515
10516         return result;
10517 }
10518
10519 INTERNAL_FUNC int emstorage_get_save_name(int account_id, int mail_id, int atch_id, char *fname, char *name_buf, int *err_code)
10520 {
10521         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);
10522         EM_PROFILE_BEGIN(profile_emstorage_get_save_name);
10523
10524         int ret = false;
10525         int error = EMAIL_ERROR_NONE;
10526         char *dir_name = NULL;
10527         char create_dir[1024]={0};
10528         char *temp_file = NULL;
10529
10530         if (!name_buf || account_id < FIRST_ACCOUNT_ID || mail_id < 0 || atch_id < 0)  {
10531                 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);
10532                 error = EMAIL_ERROR_INVALID_PARAM;
10533                 goto FINISH_OFF;
10534         }
10535
10536         sprintf(name_buf, "%s", MAILHOME);
10537         sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, account_id);
10538
10539         if (mail_id > 0)
10540                 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, mail_id);
10541
10542         if (atch_id > 0)
10543                 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, atch_id);
10544
10545         if (fname) {
10546                 temp_file = EM_SAFE_STRDUP(fname);
10547                 if (strstr(temp_file, "/")) {
10548                         dir_name = emstorage_make_directory_path_from_file_path(temp_file);
10549                 }
10550         }
10551
10552         if (dir_name) {
10553                 sprintf(create_dir, "%s%s%s", name_buf, DIR_SEPERATOR, dir_name);
10554                 EM_DEBUG_LOG(">>>>> DIR PATH [ %s ]", create_dir);
10555                 mkdir(create_dir, DIRECTORY_PERMISSION);
10556                 EM_SAFE_FREE(dir_name);
10557         }
10558
10559         if (fname) {
10560                 EM_DEBUG_LOG(">>>>> fname [ %s ]", fname);
10561                 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%s", DIR_SEPERATOR, fname);
10562         }
10563
10564         EM_DEBUG_LOG(">>>>> name_buf [ %s ]", name_buf);
10565
10566         ret = true;
10567
10568 FINISH_OFF:
10569         EM_SAFE_FREE(temp_file);
10570
10571         if (err_code != NULL)
10572                 *err_code = error;
10573
10574         EM_PROFILE_END(profile_emstorage_get_save_name);
10575         EM_DEBUG_FUNC_END("ret [%d]", ret);
10576         return ret;
10577 }
10578
10579 INTERNAL_FUNC int emstorage_get_dele_name(int account_id, int mail_id, int atch_id, char *fname, char *name_buf, int *err_code)
10580 {
10581         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);
10582
10583         if (!name_buf || account_id < FIRST_ACCOUNT_ID)  {
10584                 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);
10585                 if (err_code != NULL)
10586                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10587                 return false;
10588         }
10589
10590         sprintf(name_buf+EM_SAFE_STRLEN(name_buf),      "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
10591
10592         if (mail_id > 0)
10593                 sprintf(name_buf+EM_SAFE_STRLEN(name_buf),      "%s%d", DIR_SEPERATOR, mail_id);
10594         else
10595                 goto FINISH_OFF;
10596
10597         if (atch_id > 0)
10598                 sprintf(name_buf+EM_SAFE_STRLEN(name_buf),      "%s%d", DIR_SEPERATOR, atch_id);
10599         else
10600                 goto FINISH_OFF;
10601
10602 FINISH_OFF:
10603         sprintf(name_buf+EM_SAFE_STRLEN(name_buf),      ".DELE");
10604
10605         EM_DEBUG_FUNC_END();
10606         return true;
10607 }
10608
10609 INTERNAL_FUNC int emstorage_create_dir(int account_id, int mail_id, int atch_id, int *err_code)
10610 {
10611         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], err_code[%p]", account_id, mail_id, atch_id, err_code);
10612         EM_PROFILE_BEGIN(profile_emcore_save_create_dir);
10613         int ret = false;
10614         int error = EMAIL_ERROR_NONE;
10615
10616         char buf[512];
10617         struct stat sbuf;
10618         if (account_id >= FIRST_ACCOUNT_ID)  {
10619                 SNPRINTF(buf, sizeof(buf), "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
10620
10621                 if (stat(buf, &sbuf) == 0) {
10622                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR)  {
10623                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
10624                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10625                                 goto FINISH_OFF;
10626                         }
10627                 }
10628                 else  {
10629                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
10630                                 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
10631                                 EM_DEBUG_EXCEPTION("mkdir failed l(Errno=%d)][ErrStr=%s]", errno, strerror(errno));
10632                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10633                                 goto FINISH_OFF;
10634                         }
10635                 }
10636         }
10637
10638         if (mail_id > 0)  {
10639                 if (account_id < FIRST_ACCOUNT_ID)  {
10640                         EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
10641                         error = EMAIL_ERROR_INVALID_PARAM;
10642                         goto FINISH_OFF;
10643                 }
10644
10645                 SNPRINTF(buf+EM_SAFE_STRLEN(buf), sizeof(buf), "%s%d", DIR_SEPERATOR, mail_id);
10646
10647                 if (stat(buf, &sbuf) == 0) {
10648                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR)  {
10649                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
10650
10651                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10652                                 goto FINISH_OFF;
10653                         }
10654                 }
10655                 else  {
10656                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
10657                                 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
10658                                 EM_DEBUG_EXCEPTION("mkdir failed l (Errno=%d)][ErrStr=%s]", errno, strerror(errno));
10659                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10660                                 goto FINISH_OFF;
10661                         }
10662                 }
10663         }
10664
10665         if (atch_id > 0)  {
10666                 if (account_id < FIRST_ACCOUNT_ID || mail_id <= 0)  {
10667                         EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
10668
10669                         error = EMAIL_ERROR_INVALID_PARAM;
10670                         goto FINISH_OFF;
10671                 }
10672
10673                 SNPRINTF(buf+EM_SAFE_STRLEN(buf), sizeof(buf)-(EM_SAFE_STRLEN(buf)+1), "%s%d", DIR_SEPERATOR, atch_id);
10674
10675                 if (stat(buf, &sbuf) == 0) {
10676                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR)  {
10677                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
10678
10679                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10680                                 goto FINISH_OFF;
10681                         }
10682                 }
10683                 else  {
10684                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
10685                                 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
10686                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10687                                 goto FINISH_OFF;
10688                         }
10689                 }
10690         }
10691
10692         ret = true;
10693
10694 FINISH_OFF:
10695         if (err_code != NULL)
10696                 *err_code = error;
10697
10698         EM_PROFILE_END(profile_emcore_save_create_dir);
10699         EM_DEBUG_FUNC_END("ret [%d]", ret);
10700         return ret;
10701 }
10702
10703 INTERNAL_FUNC int emstorage_copy_file(char *src_file, char *dst_file, int sync_status, int *err_code)
10704 {
10705         EM_DEBUG_FUNC_BEGIN("src_file[%s], dst_file[%s], err_code[%p]", src_file, dst_file, err_code);
10706         EM_DEBUG_LOG("Using the fsync function");
10707         int ret = false;
10708         int error = EMAIL_ERROR_NONE;
10709         struct stat st_buf;
10710
10711         int fp_src = 0;
10712         int fp_dst = 0;
10713         int nread = 0;
10714         int nwritten = 0;
10715         char *buf =  NULL;
10716         int buf_size = 0;
10717
10718         if (!src_file || !dst_file)  {
10719                 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
10720
10721                 error = EMAIL_ERROR_INVALID_PARAM;
10722                 goto FINISH_OFF;
10723         }
10724
10725         if (stat(src_file, &st_buf) < 0) {
10726                 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", src_file);
10727
10728                 error = EMAIL_ERROR_SYSTEM_FAILURE;             /* EMAIL_ERROR_INVALID_PATH; */
10729                 goto FINISH_OFF;
10730         }
10731
10732         buf_size =  st_buf.st_size;
10733         EM_DEBUG_LOG(">>>> File Size [ %d ]", buf_size);
10734         buf = (char *)calloc(1, buf_size+1);
10735
10736         if (!buf) {
10737                 EM_DEBUG_EXCEPTION(">>> Memory cannot be allocated");
10738                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10739                 goto FINISH_OFF;
10740         }
10741
10742         if (buf) {
10743                 if ((fp_src = open(src_file, O_RDONLY))<0) { /*prevent 24474*/
10744                         EM_DEBUG_EXCEPTION(">>>> Source Fail open %s Failed [ %d ] - Error [ %s ]", src_file, errno, strerror(errno));
10745                         error = EMAIL_ERROR_SYSTEM_FAILURE;
10746                         goto FINISH_OFF;
10747                 }
10748
10749                 if ((fp_dst = open(dst_file, O_CREAT | O_WRONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH))<0) { /*prevent 24474*/
10750                         EM_DEBUG_EXCEPTION(">>>> Destination Fail open %s Failed [ %d ] - Error [ %s ]", dst_file, errno, strerror(errno));
10751                         error = EMAIL_ERROR_SYSTEM_FAILURE;
10752                         goto FINISH_OFF;
10753                 }
10754
10755                 while ((nread = read(fp_src, buf, buf_size)) > 0) {
10756                         if (nread > 0 && nread <= buf_size)  {
10757                                 EM_DEBUG_LOG("Nread Value [%d]", nread);
10758                                 if ((nwritten = write(fp_dst, buf, nread)) != nread) {
10759                                         EM_DEBUG_EXCEPTION("fwrite failed...[%d] : [%s]", errno, strerror(errno));
10760                                         error = EMAIL_ERROR_UNKNOWN;
10761                                         goto FINISH_OFF;
10762                                 }
10763                                 EM_DEBUG_LOG("NWRITTEN [%d]", nwritten);
10764                         }
10765                 }
10766         }
10767
10768         ret = true;
10769
10770 FINISH_OFF:
10771         if (fp_src>0) /*prevent 24474*/
10772                 close(fp_src);
10773
10774         if (fp_dst>0) { /*prevent 24474*/
10775                 if (sync_status) {
10776                         EM_DEBUG_LOG("Before fsync");
10777                         fsync(fp_dst);
10778                 }
10779                 close(fp_dst);
10780         }
10781         EM_SAFE_FREE(buf);
10782         if (nread < 0 || error == EMAIL_ERROR_UNKNOWN)
10783                 remove(dst_file);
10784
10785         if (err_code != NULL)
10786                 *err_code = error;
10787         EM_DEBUG_FUNC_END("ret [%d]", ret);
10788         return ret;
10789 }
10790 /* create Directory if user has deleted [deepam.p@samsung.com] */
10791 INTERNAL_FUNC void emstorage_create_dir_if_delete()
10792 {
10793         EM_DEBUG_FUNC_BEGIN();
10794
10795         mkdir(DATA_PATH, DIRECTORY_PERMISSION);
10796         mkdir(EMAILPATH, DIRECTORY_PERMISSION);
10797         mkdir(MAILHOME, DIRECTORY_PERMISSION);
10798         mkdir(MAILTEMP, DIRECTORY_PERMISSION);
10799
10800         EM_DEBUG_FUNC_END();
10801 }
10802 static int _get_temp_file_name(char **filename, int *err_code)
10803 {
10804         EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
10805
10806         int ret = false;
10807         int error = EMAIL_ERROR_NONE;
10808
10809         if (filename == NULL) {
10810                 EM_DEBUG_EXCEPTION(" filename[%p]", filename);
10811                 error = EMAIL_ERROR_INVALID_PARAM;
10812                 goto FINISH_OFF;
10813         }
10814
10815         char tempname[512] = {0x00, };
10816         struct timeval tv;
10817
10818         gettimeofday(&tv, NULL);
10819         srand(tv.tv_usec);
10820
10821         SNPRINTF(tempname, sizeof(tempname), "%s%c%d", MAILTEMP, '/', rand());
10822
10823         char *p = EM_SAFE_STRDUP(tempname);
10824         if (p == NULL)  {
10825                 EM_DEBUG_EXCEPTION(" strdup failed...");
10826                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10827                 goto FINISH_OFF;
10828         }
10829
10830         *filename = p;
10831
10832         ret = true;
10833
10834 FINISH_OFF:
10835         if (err_code != NULL)
10836                 *err_code = error;
10837
10838         EM_DEBUG_FUNC_END("ret [%d]", ret);
10839         return ret;
10840 }
10841
10842 INTERNAL_FUNC int emstorage_add_content_type(char *file_path, char *char_set, int *err_code)
10843 {
10844         EM_DEBUG_FUNC_BEGIN("File path [ %p ]  Character Set [ %p ] err_code [ %p]", file_path, char_set, err_code);
10845
10846         EM_IF_NULL_RETURN_VALUE(file_path, false);
10847         EM_IF_NULL_RETURN_VALUE(char_set, false);
10848         EM_IF_NULL_RETURN_VALUE(err_code, false);
10849
10850         char *buf =  NULL;
10851         char *buf1 = NULL;
10852         struct stat st_buf;
10853         int buf_size = 0;
10854         char *low_char_set = NULL;
10855         char *match_str = NULL;
10856         int nwritten = 0;
10857         int ret = false;
10858         int error = EMAIL_ERROR_NONE;
10859         int data_count_to_written = 0;
10860         char *temp_file_name = NULL;
10861         int err = 0;
10862
10863         FILE* fp_src = NULL;
10864         FILE* fp_dest = NULL;
10865         int nread = 0;
10866
10867
10868         if (stat(file_path, &st_buf) < 0) {
10869                 EM_DEBUG_EXCEPTION(" stat(\"%s\") failed...", file_path);
10870
10871                 error = EMAIL_ERROR_SYSTEM_FAILURE;             /* EMAIL_ERROR_INVALID_PATH; */
10872                 goto FINISH_OFF;
10873         }
10874
10875         buf_size =  st_buf.st_size;
10876
10877         EM_DEBUG_LOG(">>>> File Size [ %d ] ", buf_size);
10878
10879         buf = (char *)calloc(1, buf_size+1);
10880
10881         if (!buf) {
10882                 EM_DEBUG_LOG(">>> Memory cannot be allocated ");
10883                 goto FINISH_OFF;
10884         }
10885
10886         if (!(fp_src = fopen(file_path, "rb"))) {
10887                 EM_DEBUG_EXCEPTION(" file_path fopen failed - %s", file_path);
10888
10889                         error = EMAIL_ERROR_SYSTEM_FAILURE;
10890                         goto FINISH_OFF;
10891                 }
10892
10893                 if ((nread = fread(buf, 1, buf_size, fp_src)) > 0) {
10894                         if (nread > 0 && nread <= buf_size)  {
10895                                 EM_DEBUG_LOG(">>>> Nread Value [ %d ] ", nread);
10896
10897                                 /**
10898                                   *   1.Add check for whether content type is there.
10899                                   *   2. If not based on the character set, Append it in File
10900                                   **/
10901
10902                                 low_char_set = calloc(1, EM_SAFE_STRLEN(char_set) + strlen(" \" /></head>") +1); /*prevent 34359*/
10903
10904                                 strncat(low_char_set, char_set, EM_SAFE_STRLEN(char_set));
10905
10906                                 EM_DEBUG_LOG(">>>> CHAR SET [ %s ] ", low_char_set);
10907
10908                                 strncat(low_char_set, " \" /></head>", strlen(" \" /></head>")); /*prevent 34359*/
10909
10910                                 EM_DEBUG_LOG(">>> CHARSET [ %s ] ", low_char_set);
10911
10912                                 EM_DEBUG_LOG(">>>>emstorage_add_content_type 1 ");
10913
10914                                 match_str = strstr(buf, CONTENT_TYPE_DATA);
10915                                 EM_DEBUG_LOG(">>>>emstorage_add_content_type 2 ");
10916
10917                                 if (match_str == NULL) {
10918                                         EM_DEBUG_LOG(">>>>emstorage_add_content_type 3 ");
10919                                         if (fp_src !=NULL) {
10920                                                 fclose(fp_src);fp_src = NULL;
10921                                         }
10922                                 data_count_to_written = EM_SAFE_STRLEN(low_char_set)+strlen(CONTENT_DATA)+1; /*prevent 34359*/
10923                                         EM_DEBUG_LOG(">>>>emstorage_add_content_type 4 ");
10924                                 buf1 = (char *)calloc(1, data_count_to_written);
10925                                         EM_DEBUG_LOG(">>>>emstorage_add_content_type 5 ");
10926
10927                                         if (buf1) {
10928                                                 EM_DEBUG_LOG(">>>>emstorage_add_content_type 6 ");
10929                                                 strncat(buf1, CONTENT_DATA, strlen(CONTENT_DATA)); /*prevent 34359*/
10930
10931                                                 EM_DEBUG_LOG(">>>>> BUF 1 [ %s ] ", buf1);
10932
10933                                                 strncat(buf1, low_char_set, EM_SAFE_STRLEN(low_char_set));
10934
10935                                                 EM_DEBUG_LOG(">>>> HTML TAG DATA  [ %s ] ", buf1);
10936
10937
10938                                         /* 1. Create a temporary file name */
10939                                         if (!_get_temp_file_name(&temp_file_name, &err)) {
10940                                                         EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed - %d", err);
10941                                                         if (err_code != NULL) *err_code = err;
10942                                                         EM_SAFE_FREE(temp_file_name);
10943                                                         goto FINISH_OFF;
10944                                         }
10945                                         EM_DEBUG_LOG(">>>>>>> TEMP APPEND FILE PATH [ %s ] ", temp_file_name);
10946
10947                                         /* Open the Temp file in Append mode */
10948                                         if (!(fp_dest = fopen(temp_file_name, "ab"))) {
10949                                                 EM_DEBUG_EXCEPTION(" fopen failed - %s", temp_file_name);
10950                                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10951                                                 goto FINISH_OFF;
10952                                         }
10953
10954                                         /* 2. write the Latest data */
10955                                         nwritten = fwrite(buf1, data_count_to_written-1, 1, fp_dest);
10956
10957                                         if (nwritten > 0) {
10958                                                 EM_DEBUG_LOG(" Latest Data  : [%d ] bytes written ", nwritten);
10959                                                 nwritten = 0;
10960                                                 /* 3. Append old data */
10961                                                 nwritten = fwrite(buf, nread-1, 1, fp_dest);
10962
10963                                                 if (nwritten <= 0) {
10964                                                         EM_DEBUG_EXCEPTION(" Error Occured while writing Old data : [%d ] bytes written ", nwritten);
10965                                                         error = EMAIL_ERROR_SYSTEM_FAILURE;
10966                                                         goto FINISH_OFF;
10967                                                 }
10968                                                 else {
10969                                                         EM_DEBUG_LOG(">>>> OLD data appended [ %d ] ", nwritten);
10970
10971                                                         if (!emstorage_move_file(temp_file_name, file_path, false, &err)) {
10972                                                                 EM_DEBUG_EXCEPTION(" emstorage_move_file failed - %d", err);
10973                                                                 goto FINISH_OFF;
10974                                                         }
10975                                                 }
10976
10977                                         }
10978                                         else {
10979                                                 EM_DEBUG_EXCEPTION(" Error Occured while writing New data : [%d ] bytes written ", nwritten);
10980                                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10981                                                 goto FINISH_OFF;
10982                                         }
10983
10984                                         }
10985
10986                                 }
10987                                 EM_DEBUG_LOG(">>>>emstorage_add_content_type 15 ");
10988
10989
10990                         }
10991                 }
10992
10993         ret = true;
10994 FINISH_OFF:
10995
10996         EM_SAFE_FREE(buf);
10997         EM_SAFE_FREE(buf1);
10998         EM_SAFE_FREE(low_char_set);
10999
11000         if (fp_src != NULL) {
11001                 fclose(fp_src);
11002                 fp_src = NULL;
11003         }
11004
11005         if (fp_dest != NULL) {
11006                 fclose(fp_dest);
11007                 fp_dest = NULL;
11008         }
11009         EM_DEBUG_FUNC_END("ret [%d]", ret);
11010         return ret;
11011
11012 }
11013
11014 INTERNAL_FUNC int emstorage_move_file(char *src_file, char *dst_file, int sync_status, int *err_code)
11015 {
11016         EM_DEBUG_FUNC_BEGIN("src_file[%p], dst_file[%p], err_code[%p]", src_file, dst_file, err_code);
11017
11018         int ret = false;
11019         int error = EMAIL_ERROR_NONE;
11020
11021         if (src_file == NULL || dst_file == NULL)  {
11022                 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
11023                 error = EMAIL_ERROR_INVALID_PARAM;
11024                 goto FINISH_OFF;
11025         }
11026
11027         EM_DEBUG_LOG("src_file[%s], dst_file[%s]", src_file, dst_file);
11028
11029         if (strcmp(src_file, dst_file) != 0) {
11030                 if (rename(src_file, dst_file) != 0) {
11031                         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() */
11032                                 /*  does not work across different mount points, even if the same file system is mounted on both.)       */
11033                                 EM_DEBUG_LOG("oldpath and newpath are not on the same mounted file system.");
11034                                 if (!emstorage_copy_file(src_file, dst_file, sync_status, &error)) {
11035                                         EM_DEBUG_EXCEPTION("emstorage_copy_file failed - %d", error);
11036                                         goto FINISH_OFF;
11037                                 }
11038                                 remove(src_file);
11039                                 EM_DEBUG_LOG("src[%s] removed", src_file);
11040
11041                         }
11042                         else  {
11043                                 if (errno == ENOENT)  {
11044                                         struct stat temp_file_stat;
11045                                         if (stat(src_file, &temp_file_stat) < 0)
11046                                                 EM_DEBUG_EXCEPTION("no src file found [%s]", src_file);
11047                                         if (stat(dst_file, &temp_file_stat) < 0)
11048                                                 EM_DEBUG_EXCEPTION("no dst file found [%s]", src_file);
11049
11050                                         EM_DEBUG_EXCEPTION("no file found [%d]", errno);
11051                                         error = EMAIL_ERROR_FILE_NOT_FOUND;
11052                                         goto FINISH_OFF;
11053
11054                                 }
11055                                 else  {
11056                                         EM_DEBUG_EXCEPTION("rename failed [%d]", errno);
11057                                         error = EMAIL_ERROR_SYSTEM_FAILURE;
11058                                         goto FINISH_OFF;
11059                                 }
11060                         }
11061                 }
11062         }
11063         else {
11064                 EM_DEBUG_LOG("src[%s] = dst[%d]", src_file, dst_file);
11065         }
11066
11067         ret = true;
11068
11069 FINISH_OFF:
11070         if (err_code != NULL)
11071                 *err_code = error;
11072
11073         EM_DEBUG_FUNC_END("ret [%d]", ret);
11074         return ret;
11075 }
11076
11077 INTERNAL_FUNC int emstorage_delete_file(char *src_file, int *err_code)
11078 {
11079         EM_DEBUG_FUNC_BEGIN("src_file[%p], err_code[%p]", src_file, err_code);
11080
11081         int ret = false;
11082         int error = EMAIL_ERROR_NONE;
11083
11084         if (src_file == NULL) {
11085                 EM_DEBUG_EXCEPTION(" src_file[%p]", src_file);
11086
11087                 error = EMAIL_ERROR_INVALID_PARAM;
11088                 goto FINISH_OFF;
11089         }
11090
11091         if (remove(src_file) != 0) {
11092                 if (errno != ENOENT) {
11093                         EM_DEBUG_EXCEPTION(" remove failed - %d", errno);
11094
11095                         error = EMAIL_ERROR_SYSTEM_FAILURE;
11096                         goto FINISH_OFF;
11097                 }
11098                 else {
11099                         EM_DEBUG_EXCEPTION(" no file found...");
11100
11101                         error = EMAIL_ERROR_FILE_NOT_FOUND;
11102                 }
11103         }
11104
11105         ret = true;
11106
11107 FINISH_OFF:
11108         if (err_code != NULL)
11109                 *err_code = error;
11110
11111         EM_DEBUG_FUNC_END("ret [%d]", ret);
11112         return ret;
11113 }
11114
11115
11116 INTERNAL_FUNC int emstorage_delete_dir(char *src_dir, int *err_code)
11117 {
11118         EM_DEBUG_FUNC_BEGIN("src_dir[%p], err_code[%p]", src_dir, err_code);
11119
11120         if (src_dir == NULL) {
11121                 EM_DEBUG_EXCEPTION("src_dir[%p]", src_dir);
11122
11123                 if (err_code != NULL)
11124                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11125                 return false;
11126         }
11127
11128         int error = EMAIL_ERROR_NONE;
11129
11130         DIR *dirp;
11131         struct dirent *dp;
11132         struct stat sbuf;
11133         char buf[512];
11134
11135         dirp = opendir(src_dir);
11136
11137         if (dirp == NULL)  {
11138                 if (errno == ENOENT)  {
11139                         EM_DEBUG_EXCEPTION("directory[%s] does not exist...", src_dir);
11140                         if (err_code != NULL)
11141                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
11142                         return true;
11143                 }
11144                 else  {
11145                         EM_DEBUG_EXCEPTION("opendir failed - %d", errno);
11146                         if (err_code != NULL)
11147                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
11148                         return false;
11149                 }
11150         }
11151
11152         while ((dp=readdir(dirp)))  {
11153                 if (strncmp(dp->d_name, ".", 1) == 0 || strncmp(dp->d_name, "..", 2) == 0) /* prevent 34360 */
11154                         continue;
11155
11156                 SNPRINTF(buf, sizeof(buf), "%s/%s", src_dir, dp->d_name);
11157
11158                 if (lstat(buf, &sbuf) == 0 || stat(buf, &sbuf) == 0) {
11159                         /*  check directory */
11160                         if ((sbuf.st_mode & S_IFMT) == S_IFDIR)  {      /*  directory */
11161                                 /*  recursive call */
11162                                 if (!emstorage_delete_dir(buf, &error)) {
11163                                         closedir(dirp);
11164                                         if (err_code != NULL)
11165                                                 *err_code = error;
11166                                         return false;
11167                                 }
11168                         }
11169                         else  { /*  file */
11170                                 if (remove(buf) < 0)  {
11171                                         EM_DEBUG_EXCEPTION("remove failed - %s", buf);
11172                                         closedir(dirp);
11173                                         if (err_code != NULL)
11174                                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
11175                                         return false;
11176                                 }
11177                         }
11178                 }
11179                 else
11180                         EM_DEBUG_EXCEPTION("content does not exist...");
11181         }
11182
11183         closedir(dirp);
11184
11185         EM_DEBUG_LOG("remove direcotory [%s]", src_dir);
11186
11187         /* EM_DEBUG_FUNC_BEGIN(); */
11188
11189         if (remove(src_dir) < 0)  {
11190                 EM_DEBUG_EXCEPTION("remove failed [%s]", src_dir);
11191                 if (err_code != NULL)
11192                         *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
11193                 return false;
11194         }
11195
11196         if (err_code != NULL)
11197                 *err_code = error;
11198
11199         return true;
11200 }
11201
11202 /* faizan.h@samsung.com */
11203 INTERNAL_FUNC int emstorage_update_server_uid(char *old_server_uid, char *new_server_uid, int *err_code)
11204 {
11205         EM_DEBUG_FUNC_BEGIN("new_server_uid[%s], old_server_uid[%s]", new_server_uid, old_server_uid);
11206         int rc, ret = false;
11207         int error = EMAIL_ERROR_NONE;
11208         char sql_query_string[QUERY_SIZE] = {0, };
11209
11210         int transaction = true;
11211
11212         if (!old_server_uid || !new_server_uid) {
11213                 EM_DEBUG_EXCEPTION("Invalid parameters");
11214                 error = EMAIL_ERROR_INVALID_PARAM;
11215                 return false;
11216         }
11217         sqlite3 *local_db_handle = emstorage_get_db_connection();
11218
11219         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
11220
11221         SNPRINTF(sql_query_string, sizeof(sql_query_string),
11222                  "UPDATE mail_tbl SET server_mail_id=\'%s\' WHERE server_mail_id=%s ", new_server_uid, old_server_uid);
11223         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
11224
11225         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
11226                 ("sqlite3_exec fail:%d", rc));
11227         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11228                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11229
11230         ret = true;
11231
11232 FINISH_OFF:
11233         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
11234         _DISCONNECT_DB;
11235
11236         if (err_code != NULL)
11237                 *err_code = error;
11238
11239         EM_DEBUG_FUNC_END("ret [%d]", ret);
11240         return ret;
11241
11242 }
11243
11244 INTERNAL_FUNC int emstorage_update_read_mail_uid(int mail_id, char *new_server_uid, char *mbox_name, int *err_code)
11245 {
11246         EM_DEBUG_FUNC_BEGIN("mail_id[%d], new_server_uid[%s], mbox_name[%s]", mail_id, new_server_uid, mbox_name);
11247
11248         int rc, ret = false;
11249         int error = EMAIL_ERROR_NONE;
11250         char sql_query_string[QUERY_SIZE] = {0, };
11251
11252         int transaction = true;
11253
11254         if (!mail_id || !new_server_uid || !mbox_name)  {
11255                 EM_DEBUG_EXCEPTION("Invalid parameters");
11256                 error = EMAIL_ERROR_INVALID_PARAM;
11257                 return false;
11258         }
11259         sqlite3 *local_db_handle = emstorage_get_db_connection();
11260         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
11261
11262
11263         SNPRINTF(sql_query_string, sizeof(sql_query_string),
11264                  "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);
11265         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
11266         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11267                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11268         ret     = true;
11269
11270 FINISH_OFF:
11271
11272         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
11273         _DISCONNECT_DB;
11274
11275         if (err_code != NULL)
11276                 *err_code = error;
11277
11278         EM_DEBUG_FUNC_END("ret [%d]", ret);
11279         return ret;
11280
11281 }
11282
11283
11284 int emstorage_get_latest_unread_mailid(int account_id, int *mail_id, int *err_code)
11285 {
11286         EM_DEBUG_FUNC_BEGIN();
11287
11288         if ((!mail_id) ||(account_id <= 0 &&  account_id != -1)) {
11289                 EM_DEBUG_EXCEPTION(" mail_id[%p], account_id[%d] ", mail_id, account_id);
11290                 if (err_code != NULL)
11291                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11292                 return false;
11293         }
11294
11295         int ret = false;
11296         int rc = -1;
11297         int error = EMAIL_ERROR_NONE;
11298         DB_STMT hStmt = NULL;
11299         int count = 0;
11300         int mailid = 0;
11301         int transaction = false;
11302         char sql_query_string[QUERY_SIZE] = {0, };
11303
11304         sqlite3 *local_db_handle = emstorage_get_db_connection();
11305         EMSTORAGE_START_READ_TRANSACTION(transaction);
11306
11307         memset(sql_query_string, 0x00, sizeof(sql_query_string));
11308
11309         if (account_id == -1)
11310                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl WHERE flags_seen_field = 0 ORDER BY mail_id DESC");
11311         else
11312                 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);
11313
11314         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11315         EM_DEBUG_LOG("  sqlite3_prepare hStmt = %p", hStmt);
11316         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11317                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11318
11319         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11320         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11321                 ("sqlite3_step fail:%d", rc));
11322
11323         char **result;
11324
11325         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
11326         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11327                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11328
11329         sqlite3_free_table(result);
11330         if (count == 0)  {
11331                 EM_DEBUG_EXCEPTION("no Mails found...");
11332                 ret = false;
11333                 error= EMAIL_ERROR_MAIL_NOT_FOUND;
11334                 goto FINISH_OFF;
11335         }
11336
11337         _get_stmt_field_data_int(hStmt, &mailid, 0);
11338         EM_DEBUG_LOG("mailid [%d]", mailid);
11339
11340         ret = true;
11341
11342 FINISH_OFF:
11343         if (hStmt != NULL)  {
11344                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
11345
11346                 rc = sqlite3_finalize(hStmt);
11347                 if (rc != SQLITE_OK)  {
11348                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
11349
11350                         error = EMAIL_ERROR_DB_FAILURE;
11351                 }
11352         }
11353         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
11354         _DISCONNECT_DB;
11355
11356         if (mail_id != NULL)
11357                 *mail_id = mailid;
11358
11359         if (err_code != NULL)
11360                 *err_code = error;
11361
11362         EM_DEBUG_FUNC_END("ret [%d]", ret);
11363         return ret;
11364 }
11365
11366 int setting_system_command(const char *command)
11367
11368  {
11369         int pid = 0, status = 0;
11370         char *const environ[] = { NULL };
11371
11372         if (command == 0)
11373                 return 1;
11374
11375         pid = fork();
11376
11377         if (pid == -1)
11378                 return -1;
11379
11380         if (pid == 0) {
11381                 char *argv[4];
11382
11383                 argv[0] = "sh";
11384                 argv[1] = "-c";
11385                 argv[2] = (char *)command;
11386                 argv[3] = 0;
11387
11388                 execve("/bin/sh", argv, environ);
11389                 abort();
11390         }
11391         do{
11392                 if (waitpid(pid, &status, 0) == -1) {
11393                         if (errno != EINTR)
11394                                 return -1;
11395                 }
11396                 else {
11397                         return status;
11398                 }
11399         } while (1);
11400
11401         return 0;
11402 }
11403
11404
11405 INTERNAL_FUNC int emstorage_mail_get_total_diskspace_usage(unsigned long *total_usage,  int transaction, int *err_code)
11406 {
11407         EM_DEBUG_FUNC_BEGIN("total_usage[%p],  transaction[%d], err_code[%p]", total_usage, transaction, err_code);
11408
11409         if (!total_usage) {
11410                 EM_DEBUG_EXCEPTION("total_usage[%p]", total_usage);
11411
11412                 if (err_code != NULL)
11413                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11414                 return false;
11415         }
11416
11417         int   ret = false;
11418         int   error = EMAIL_ERROR_NONE;
11419         char  syscmd[256] = {0, };
11420         char  line[256] = {0, };
11421         char *line_from_file = NULL;
11422         FILE *fp = NULL;
11423         unsigned long total_diskusage = 0;
11424
11425         SNPRINTF(syscmd, sizeof(syscmd), "touch %s", SETTING_MEMORY_TEMP_FILE_PATH);
11426         if (setting_system_command(syscmd) == -1) {
11427                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
11428
11429                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11430                 goto FINISH_OFF;
11431         }
11432
11433         SNPRINTF(syscmd, sizeof(syscmd), "du -hsk %s > %s", EMAILPATH, SETTING_MEMORY_TEMP_FILE_PATH);
11434         EM_DEBUG_LOG(" cmd : %s", syscmd);
11435         if (setting_system_command(syscmd) == -1) {
11436                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : Setting > Memory] System Command [%s] is failed", syscmd);
11437
11438                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11439                 goto FINISH_OFF;
11440         }
11441
11442         fp = fopen(SETTING_MEMORY_TEMP_FILE_PATH, "r");
11443         if (fp == NULL) {
11444                 perror(SETTING_MEMORY_TEMP_FILE_PATH);
11445
11446                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11447                 goto FINISH_OFF;
11448         }
11449
11450         line_from_file = fgets(line, sizeof(line), fp);
11451
11452         if(line_from_file == NULL) {
11453                 EM_DEBUG_EXCEPTION("fgets failed");
11454                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11455                 goto FINISH_OFF;
11456         }
11457         total_diskusage = strtoul(line, NULL, 10);
11458
11459         memset(syscmd, 0, sizeof(syscmd));
11460         SNPRINTF(syscmd, sizeof(syscmd), "rm -f %s", SETTING_MEMORY_TEMP_FILE_PATH);
11461         if (setting_system_command(syscmd) == -1) {
11462                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage :  [Setting > Memory] System Command [%s] is failed", syscmd);
11463                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11464                 goto FINISH_OFF;
11465         }
11466
11467         EM_DEBUG_LOG("[Setting > Memory] @@@@@ Size of Directory [%s] is %ld KB", EMAILPATH, total_diskusage);
11468
11469         ret = true;
11470
11471 FINISH_OFF:
11472         if (err_code != NULL)
11473                 *err_code = error;
11474
11475         if (ret)
11476                 *total_usage = total_diskusage;
11477         else
11478                 *total_usage = 0;
11479
11480         if(fp) fclose(fp); /* prevent 32730 */
11481
11482         EM_DEBUG_FUNC_END("ret [%d]", ret);
11483         return ret;
11484 }
11485
11486
11487 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)
11488 {
11489         DB_STMT hStmt = NULL;
11490         int ret = false;
11491         int error = EMAIL_ERROR_NONE;
11492         int rc = 0;
11493         char sql_query_string[QUERY_SIZE] = {0, };
11494
11495         SNPRINTF(sql_query_string, sizeof(sql_query_string),
11496                 "INSERT INTO mail_tbl VALUES "
11497                 "( ?" /*  mail_id */
11498                 ", ?" /*  account_id */
11499                 ", ?" /*  mail_size */
11500                 ", ?" /*  server_mail_status */
11501                 ", ?" /*  server_mailbox_name */
11502                 ", ?" /*  server_mail_id */
11503                 ", ?" /*  reference_mail_id */
11504                 ", ?" /*  full_address_from */
11505                 ", ?" /*  full_address_reply */
11506                 ", ?" /*  full_address_to */
11507                 ", ?" /*  full_address_cc */
11508                 ", ?" /*  full_address_bcc */
11509                 ", ?" /*  full_address_return */
11510                 ", ?" /*  subject */
11511                 ", ?" /*  body_download_status */
11512                 ", ?" /*  file_path_plain */
11513                 ", ?" /*  file_path_html */
11514                 ", ?" /*  date_time */
11515                 ", ?" /*  flags_seen_field */
11516                 ", ?" /*  flags_deleted_field */
11517                 ", ?" /*  flags_flagged_field */
11518                 ", ?" /*  flags_answered_field */
11519                 ", ?" /*  flags_recent_field */
11520                 ", ?" /*  flags_draft_field */
11521                 ", ?" /*  flags_forwarded_field */
11522                 ", ?" /*  DRM_status */
11523                 ", ?" /*  priority */
11524                 ", ?" /*  save_status */
11525                 ", ?" /*  lock_status */
11526                 ", ?" /*  message_id */
11527                 ", ?" /*  report_status */
11528                 ", ?" /*  email_address_sender */
11529                 ", ?" /*  email_address_recipient */
11530                 ", ?" /*  attachment_count */
11531                 ", ?" /*  inline_content_count */
11532                 ", ?" /*  preview_text */
11533                 ", ?" /*  thread_id */
11534                 ", ?" /*  mailbox_type */
11535                 ", ?" /*  alias_sender */
11536                 ", ?" /*  alias_recipient */
11537                 ", ?" /*  thread_item_count */
11538                 ", ?" /*  meeting_request_status */
11539                 ", ?" /*  message_class */
11540                 ", ?" /*  digest_type */
11541                 ", ?" /*  smime_type */
11542                 ", ?" /*  scheduled_sending_time */
11543                 ", ?" /*  eas_data_length */
11544                 ", ?" /*  eas_data */
11545                 ")");
11546
11547         int transaction = true;
11548         sqlite3 *local_db_handle = emstorage_get_db_connection();
11549
11550         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
11551
11552
11553         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11554         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11555                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11556
11557         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_id);
11558         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, account_id);
11559         _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, EMAIL_MAILBOX_TYPE_OUTBOX);
11560         _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, "save test - long", 1, SUBJECT_LEN_IN_MAIL_TBL);
11561         _bind_stmt_field_data_string(hStmt, DATETIME_IDX_IN_MAIL_TBL, "20100316052908", 0, DATETIME_LEN_IN_MAIL_TBL);
11562         _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, 0);
11563         _bind_stmt_field_data_string(hStmt, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL, "", 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
11564         _bind_stmt_field_data_string(hStmt, SERVER_MAIL_ID_IDX_IN_MAIL_TBL, "", 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
11565         _bind_stmt_field_data_string(hStmt, MESSAGE_ID_IDX_IN_MAIL_TBL, "", 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
11566         _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, 0);
11567         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL, "<test08@streaming.s3glab.net>", 1, FROM_LEN_IN_MAIL_TBL);
11568         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL, "", 1, REPLY_TO_LEN_IN_MAIL_TBL);
11569         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL, full_address_to, 1, TO_LEN_IN_MAIL_TBL);
11570         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL, full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
11571         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL, full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
11572         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL, "", 1, RETURN_PATH_LEN_IN_MAIL_TBL);
11573         _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);
11574         _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);
11575         _bind_stmt_field_data_string(hStmt, ALIAS_SENDER_IDX_IN_MAIL_TBL, "send_alias", 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
11576         _bind_stmt_field_data_string(hStmt, ALIAS_RECIPIENT_IDX_IN_MAIL_TBL, "recipient_alias", 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
11577         _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, 1);
11578         _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);
11579         _bind_stmt_field_data_string(hStmt, FILE_PATH_HTML_IDX_IN_MAIL_TBL, "", 0, TEXT_2_LEN_IN_MAIL_TBL);
11580         _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, 4);
11581         _bind_stmt_field_data_char(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, 0);
11582         _bind_stmt_field_data_char(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, 0);
11583         _bind_stmt_field_data_char(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, 0);
11584         _bind_stmt_field_data_char(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, 0);
11585         _bind_stmt_field_data_char(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, 0);
11586         _bind_stmt_field_data_char(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, 0);
11587         _bind_stmt_field_data_char(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, 0);
11588         _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, 0);
11589         _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, 0);
11590         _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, 0);
11591         _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, 0);
11592         _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, 0);
11593         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
11594         _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, 0);
11595         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
11596         _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, 0);
11597         _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, 0);
11598         _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, "preview body", 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
11599         _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, 0);
11600         _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, 0);
11601         _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, 0);
11602         _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, 0);
11603         _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, 0);
11604         _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, NULL, 0);
11605
11606         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11607         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
11608                 ("sqlite3_step fail:%d", rc));
11609         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11610                 ("sqlite3_step fail:%d", rc));
11611         ret = true;
11612
11613 FINISH_OFF:
11614         if (hStmt != NULL)  {
11615                 rc = sqlite3_finalize(hStmt);
11616                 if (rc != SQLITE_OK)  {
11617                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
11618                         error = EMAIL_ERROR_DB_FAILURE;
11619                 }
11620         }
11621
11622         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
11623         _DISCONNECT_DB;
11624
11625         if (err_code != NULL)
11626                 *err_code = error;
11627
11628         EM_DEBUG_FUNC_END("ret [%d]", ret);
11629         return ret;
11630 }
11631
11632
11633 INTERNAL_FUNC int emstorage_get_max_mail_count()
11634 {
11635         return EMAIL_MAIL_MAX_COUNT;
11636 }
11637
11638 #define STRIPPED_SUBJECT_BUFFER_SIZE 4086
11639
11640 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)
11641 {
11642         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);
11643         EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
11644         int      rc = 0, query_size = 0, query_size_account = 0;
11645         int      account_id = 0;
11646         int      err_code = EMAIL_ERROR_NONE;
11647         int      count = 0, result_thread_id = -1, latest_mail_id_in_thread = -1;
11648         time_t   latest_date_time = 0;
11649         time_t   date_time = 0;
11650         char    *subject = NULL;
11651         char    *sql_query_string = NULL, *sql_account = NULL;
11652         char    *sql_format = "SELECT thread_id, date_time, mail_id FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
11653         char    *sql_format_account = " AND account_id = %d ";
11654         char    *sql_format_order_by = " ORDER BY date_time DESC ";
11655         char   **result = NULL;
11656         char     stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
11657         sqlite3 *local_db_handle = emstorage_get_db_connection();
11658
11659         EM_IF_NULL_RETURN_VALUE(mail_tbl, EMAIL_ERROR_INVALID_PARAM);
11660         EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
11661         EM_IF_NULL_RETURN_VALUE(result_latest_mail_id_in_thread, EMAIL_ERROR_INVALID_PARAM);
11662         EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
11663
11664         account_id   = mail_tbl->account_id;
11665         subject      = mail_tbl->subject;
11666         date_time    = mail_tbl->date_time;
11667
11668         EM_DEBUG_LOG("subject : %s", subject);
11669
11670         if (em_find_pos_stripped_subject_for_thread_view(subject, stripped_subject, STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE)  {
11671                 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view  is failed");
11672                 err_code =  EMAIL_ERROR_UNKNOWN;
11673                 result_thread_id = -1;
11674                 goto FINISH_OFF;
11675         }
11676
11677         if (EM_SAFE_STRLEN(stripped_subject) < 2) {
11678                 result_thread_id = -1;
11679                 goto FINISH_OFF;
11680         }
11681
11682         EM_DEBUG_LOG("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s", EM_SAFE_STRLEN(stripped_subject), stripped_subject);
11683
11684         if (account_id > 0)     {
11685                 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
11686                 sql_account = malloc(query_size_account);
11687                 if (sql_account == NULL) {
11688                         EM_DEBUG_EXCEPTION("malloc for sql_account  is failed %d", query_size_account);
11689                         err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
11690                         goto FINISH_OFF;
11691                 }
11692                 snprintf(sql_account, query_size_account, sql_format_account, account_id);
11693         }
11694
11695         /* prevent 34362 */
11696         query_size = strlen(sql_format) + strlen(stripped_subject)*2 + 50 + query_size_account + strlen(sql_format_order_by); /*  + query_size_mailbox; */
11697         sql_query_string = malloc(query_size);
11698
11699         if (sql_query_string == NULL) {
11700                 EM_DEBUG_EXCEPTION("malloc for sql  is failed %d", query_size);
11701                 err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
11702                 goto FINISH_OFF;
11703         }
11704
11705         sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mail_tbl->mailbox_id);
11706
11707         if (account_id > 0)
11708                 strcat(sql_query_string, sql_account);
11709
11710         strcat(sql_query_string, sql_format_order_by);
11711         strcat(sql_query_string, ";");
11712
11713         EM_DEBUG_LOG("Query : %s", sql_query_string);
11714
11715         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
11716
11717         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11718                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11719
11720         EM_DEBUG_LOG("Result rows count : %d", count);
11721
11722         if (count == 0)
11723                 result_thread_id = -1;
11724         else {
11725                 _get_table_field_data_int   (result, &result_thread_id, 3);
11726                 _get_table_field_data_time_t(result, &latest_date_time, 4);
11727                 _get_table_field_data_int   (result, &latest_mail_id_in_thread, 5);
11728
11729                 if (latest_date_time < mail_tbl->date_time)
11730                         *result_latest_mail_id_in_thread = latest_mail_id_in_thread;
11731                 else
11732                         *result_latest_mail_id_in_thread = mail_tbl->mail_id;
11733                 EM_DEBUG_LOG("latest_mail_id_in_thread [%d], mail_id [%d]", latest_mail_id_in_thread, mail_tbl->mail_id);
11734         }
11735
11736 FINISH_OFF:
11737         *thread_id = result_thread_id;
11738         *thread_item_count = count;
11739
11740         EM_DEBUG_LOG("Result thread id : %d", *thread_id);
11741         EM_DEBUG_LOG("Result count : %d", *thread_item_count);
11742         EM_DEBUG_LOG("err_code : %d", err_code);
11743
11744         EM_SAFE_FREE(sql_account);
11745         EM_SAFE_FREE(sql_query_string);
11746
11747         sqlite3_free_table(result);
11748
11749         EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
11750
11751         return err_code;
11752 }
11753
11754
11755 INTERNAL_FUNC int emstorage_get_thread_information(int thread_id, emstorage_mail_tbl_t** mail_tbl, int transaction, int *err_code)
11756 {
11757         EM_DEBUG_FUNC_BEGIN();
11758
11759         int count = 0, ret = false;
11760         int error = EMAIL_ERROR_NONE;
11761         emstorage_mail_tbl_t *p_data_tbl = NULL;
11762         char conditional_clause[QUERY_SIZE] = {0, };
11763
11764         EM_IF_NULL_RETURN_VALUE(mail_tbl, false);
11765
11766         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE thread_id = %d AND thread_item_count > 0", thread_id);
11767         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
11768
11769         if(!emstorage_query_mail_tbl(conditional_clause, transaction, &p_data_tbl, &count, &error)) {
11770                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
11771                 goto FINISH_OFF;
11772         }
11773
11774         if(p_data_tbl)
11775                 EM_DEBUG_LOG("thread_id : %d, thread_item_count : %d", p_data_tbl[0].thread_id, p_data_tbl[0].thread_item_count);
11776
11777         ret = true;
11778
11779 FINISH_OFF:
11780         if (ret == true)
11781                 *mail_tbl = p_data_tbl;
11782         else if (p_data_tbl != NULL)
11783                 emstorage_free_mail(&p_data_tbl, 1, NULL);
11784
11785         if (err_code != NULL)
11786                 *err_code = error;
11787
11788         EM_DEBUG_FUNC_END("ret [%d]", ret);
11789         return ret;
11790 }
11791
11792
11793 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)
11794 {
11795         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]"
11796                 , account_id , mailbox_id , search_type , search_value , sorting , sender_list, sender_count, err_code);
11797
11798         if ((!sender_list) ||(!sender_count)) {
11799                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
11800                 if (err_code != NULL)
11801                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11802                 return false;
11803         }
11804
11805         int rc = -1, ret = false;
11806         int error = EMAIL_ERROR_NONE;
11807         int count = 0;
11808         int i, col_index = 0;
11809         int read_count = 0;
11810         email_sender_list_t *p_sender_list = NULL;
11811         char sql_query_string[QUERY_SIZE] = {0, };
11812         char **result = NULL;
11813         sqlite3 *local_db_handle = emstorage_get_db_connection();
11814
11815         SNPRINTF(sql_query_string, sizeof(sql_query_string),
11816                 "SELECT email_address_sender, alias_sender, COUNT(email_address_sender), SUM(flags_seen_field = 1) "
11817                 "FROM mail_tbl ");
11818
11819         /*  mailbox_id */
11820         if (mailbox_id)
11821                 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);
11822         else    /*  NULL  means all mailbox_name. but except for trash(3), spambox(5), all emails(for GMail, 7) */
11823                 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) ");
11824
11825         /*  account id */
11826         /*  '0' (ALL_ACCOUNT) means all account */
11827         if (account_id > ALL_ACCOUNT)
11828                 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);
11829
11830         if (search_value) {
11831                 switch (search_type) {
11832                         case EMAIL_SEARCH_FILTER_SUBJECT:
11833                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
11834                                         " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\')) ", search_value);
11835                                 break;
11836                         case EMAIL_SEARCH_FILTER_SENDER:
11837                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
11838                                         " AND  ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
11839                                         ") ", search_value);
11840                                 break;
11841                         case EMAIL_SEARCH_FILTER_RECIPIENT:
11842                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
11843                                         " AND ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
11844                                         "       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
11845                                         "       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
11846                                         ") ", search_value, search_value, search_value);
11847                                 break;
11848                         case EMAIL_SEARCH_FILTER_ALL:
11849                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
11850                                         " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') "
11851                                         "       OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
11852                                         "                       OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
11853                                         "                       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
11854                                         "                       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
11855                                         "               ) "
11856                                         "       )"
11857                                         ")", search_value, search_value, search_value, search_value, search_value);
11858                                 break;
11859                 }
11860         }
11861
11862
11863         /*  sorting option is not available now. The order of sender list is ascending order by display name */
11864         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
11865                 "GROUP BY email_address_sender "
11866                 "ORDER BY UPPER(alias_sender) ");
11867
11868         EM_DEBUG_LOG("query[%s]", sql_query_string);
11869
11870         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
11871         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11872                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11873
11874         EM_DEBUG_LOG("Count of Sender [%d]", count);
11875
11876         if (!(p_sender_list = (email_sender_list_t*)em_malloc(sizeof(email_sender_list_t) * count))) {
11877                 EM_DEBUG_EXCEPTION("em_malloc failed...");
11878                 error = EMAIL_ERROR_OUT_OF_MEMORY;
11879                 goto FINISH_OFF;
11880         }
11881
11882         col_index = 4;
11883
11884         EM_DEBUG_LOG(">>>> DATA ASSIGN START >>");
11885         for (i = 0; i < count; i++)  {
11886                 _get_table_field_data_string(result, &(p_sender_list[i].address), 1, col_index++);
11887                 _get_table_field_data_string(result, &(p_sender_list[i].display_name), 1, col_index++);
11888                 _get_table_field_data_int(result, &(p_sender_list[i].total_count), col_index++);
11889                 _get_table_field_data_int(result, &(read_count), col_index++);
11890                 p_sender_list[i].unread_count = p_sender_list[i].total_count - read_count;              /*  unread count = total - read          */
11891         }
11892         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >>", count);
11893
11894         sqlite3_free_table(result);
11895         result = NULL;
11896
11897         ret = true;
11898
11899 FINISH_OFF:
11900         if (ret == true)  {
11901                 *sender_list = p_sender_list;
11902                 *sender_count = count;
11903                 EM_DEBUG_LOG(">>>> COUNT : %d >>", count);
11904         }
11905
11906         _DISCONNECT_DB;
11907
11908         if (err_code != NULL)
11909                 *err_code = error;
11910
11911         EM_DEBUG_FUNC_END("ret [%d]", ret);
11912         return ret;
11913 }
11914
11915 INTERNAL_FUNC int emstorage_free_sender_list(email_sender_list_t **sender_list, int count)
11916 {
11917         EM_DEBUG_FUNC_BEGIN("sender_list[%p], count[%d]", sender_list, count);
11918
11919         int err = EMAIL_ERROR_NONE;
11920
11921         if (count > 0)  {
11922                 if (!sender_list || !*sender_list)  {
11923                         EM_DEBUG_EXCEPTION("sender_list[%p], count[%d]", sender_list, count);
11924                         err = EMAIL_ERROR_INVALID_PARAM;
11925                         return err;
11926                 }
11927
11928                 email_sender_list_t* p = *sender_list;
11929                 int i = 0;
11930
11931                 for (; i < count; i++)  {
11932                         EM_SAFE_FREE(p[i].address);
11933                         EM_SAFE_FREE(p[i].display_name);
11934                 }
11935
11936                 EM_SAFE_FREE(p);
11937                 *sender_list = NULL;
11938         }
11939
11940         return err;
11941 }
11942
11943
11944 INTERNAL_FUNC int emstorage_free_address_info_list(email_address_info_list_t **address_info_list)
11945 {
11946         EM_DEBUG_FUNC_BEGIN("address_info_list[%p]", address_info_list);
11947
11948         int err = EMAIL_ERROR_NONE;
11949         email_address_info_t *p_address_info = NULL;
11950         GList *list = NULL;
11951         GList *node = NULL;
11952         int i = 0;
11953
11954         if (!address_info_list || !*address_info_list)  {
11955                 EM_DEBUG_EXCEPTION("address_info_list[%p]", address_info_list);
11956                 err = EMAIL_ERROR_INVALID_PARAM;
11957                 return err;
11958         }
11959
11960         /*  delete GLists */
11961         for (i = EMAIL_ADDRESS_TYPE_FROM; i <= EMAIL_ADDRESS_TYPE_BCC; i++) {
11962                 switch (i) {
11963                         case EMAIL_ADDRESS_TYPE_FROM:
11964                                 list = (*address_info_list)->from;
11965                                 break;
11966                         case EMAIL_ADDRESS_TYPE_TO:
11967                                 list = (*address_info_list)->to;
11968                                 break;
11969                         case EMAIL_ADDRESS_TYPE_CC:
11970                                 list = (*address_info_list)->cc;
11971                                 break;
11972                         case EMAIL_ADDRESS_TYPE_BCC:
11973                                 list = (*address_info_list)->bcc;
11974                                 break;
11975                 }
11976
11977                 /*  delete dynamic-allocated memory for each item */
11978                 node = g_list_first(list);
11979                 while (node != NULL) {
11980                         p_address_info = (email_address_info_t*)node->data;
11981                         EM_SAFE_FREE(p_address_info->address);
11982                         EM_SAFE_FREE(p_address_info->display_name);
11983                         EM_SAFE_FREE(node->data);
11984
11985                         node = g_list_next(node);
11986                 }
11987         }
11988
11989         EM_SAFE_FREE(*address_info_list);
11990         *address_info_list = NULL;
11991
11992         EM_DEBUG_FUNC_END("err [%d]", err);
11993         return err;
11994 }
11995 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
11996
11997 INTERNAL_FUNC int emstorage_add_pbd_activity(email_event_partial_body_thd* local_activity, int *activity_id, int transaction, int *err_code)
11998 {
11999         EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
12000
12001         if (!local_activity || !activity_id) {
12002                 EM_DEBUG_EXCEPTION("local_activity[%p], transaction[%d], activity_id[%p], err_code[%p]", local_activity, activity_id, transaction, err_code);
12003                 if (err_code != NULL)
12004                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12005                 return false;
12006         }
12007
12008         int rc = -1;
12009         int ret = false;
12010         int error = EMAIL_ERROR_NONE;
12011         int i = 0;
12012
12013         char sql_query_string[QUERY_SIZE] = {0, };
12014         DB_STMT hStmt = NULL;
12015         sqlite3 *local_db_handle = emstorage_get_db_connection();
12016         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12017         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12018         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12019                 "INSERT INTO mail_partial_body_activity_tbl VALUES "
12020                 "( "
12021                 "? "  /* Account ID */
12022                 ",?"  /* Local Mail ID */
12023                 ",?"  /* Server mail ID */
12024                 ",?"  /* Activity ID */
12025                 ",?"  /* Activity type*/
12026                 ",?"  /* Mailbox ID*/
12027                 ",?"  /* Mailbox name*/
12028                 ") ");
12029
12030         char *sql = "SELECT max(rowid) FROM mail_partial_body_activity_tbl;";
12031         char **result = NULL;
12032
12033
12034         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
12035
12036         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12037                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
12038
12039         if (NULL==result[1]) rc = 1;
12040         else rc = atoi(result[1])+1;
12041         sqlite3_free_table(result);
12042         result = NULL;
12043
12044         *activity_id = local_activity->activity_id = rc;
12045
12046         EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ], MAIL ID [ %d ], ACTIVITY TYPE [ %d ], SERVER MAIL ID [ %lu ]", \
12047                 local_activity->activity_id, local_activity->mail_id, local_activity->activity_type, local_activity->server_mail_id);
12048
12049         if (local_activity->mailbox_id)
12050                 EM_DEBUG_LOG(" MAILBOX ID [ %d ]", local_activity->mailbox_id);
12051
12052
12053         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12054         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12055                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12056
12057         EM_DEBUG_LOG(">>>> SQL STMT [ %s ]", sql_query_string);
12058
12059         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
12060         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
12061         _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
12062         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
12063         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
12064         _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
12065         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->mailbox_name, 0, 3999);
12066
12067
12068         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12069
12070         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12071                 ("sqlite3_step fail:%d", rc));
12072         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12073                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
12074
12075         ret = true;
12076
12077 FINISH_OFF:
12078
12079         if (hStmt != NULL) {
12080                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12081
12082                 rc = sqlite3_finalize(hStmt);
12083                 hStmt = NULL;
12084                 if (rc != SQLITE_OK) {
12085                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
12086                         error = EMAIL_ERROR_DB_FAILURE;
12087                 }
12088         }
12089
12090          EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12091         _DISCONNECT_DB;
12092          if (err_code != NULL)
12093                  *err_code = error;
12094
12095          EM_DEBUG_FUNC_END("ret [%d]", ret);
12096         return ret;
12097 }
12098
12099
12100 INTERNAL_FUNC int emstorage_get_pbd_mailbox_list(int account_id, int **mailbox_list, int *count, int transaction, int *err_code)
12101 {
12102         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
12103
12104         if (account_id < FIRST_ACCOUNT_ID || NULL == &mailbox_list || NULL == count) {
12105                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
12106                 if (err_code != NULL)
12107                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12108                 return false;
12109         }
12110
12111         int ret = false;
12112         int error = EMAIL_ERROR_NONE;
12113         char **result;
12114         int i = 0, rc = -1;
12115         int *mbox_list = NULL;
12116         DB_STMT hStmt = NULL;
12117         char sql_query_string[QUERY_SIZE] = {0, };
12118
12119         sqlite3 *local_db_handle = emstorage_get_db_connection();
12120
12121         EMSTORAGE_START_READ_TRANSACTION(transaction);
12122
12123         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12124         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);
12125
12126
12127         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
12128         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12129                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12130
12131         *count = atoi(result[1]);
12132         sqlite3_free_table(result);
12133
12134         if (!*count) {
12135                 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
12136                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
12137                 ret = true;
12138                 goto FINISH_OFF;
12139         }
12140         EM_DEBUG_LOG("Mailbox count = %d", *count);
12141
12142         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12143
12144         /* 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); */
12145         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);
12146
12147         EM_DEBUG_LOG(" Query [%s]", sql_query_string);
12148
12149
12150         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12151
12152         EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
12153         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12154                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12155
12156
12157         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12158         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12159                 ("sqlite3_step fail:%d", rc));
12160
12161         if (NULL == (mbox_list = (int *)em_malloc(sizeof(int *) * (*count)))) {
12162                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
12163                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12164                 goto FINISH_OFF;
12165         }
12166
12167         memset(mbox_list, 0x00, sizeof(int) * (*count));
12168
12169         for (i = 0; i < (*count); i++) {
12170                 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
12171                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12172                 /* EM_DEBUG_LOG("In emstorage_get_pdb_mailbox_list() loop, After sqlite3_step(), , i = %d, rc = %d.", i,  rc); */
12173                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12174                         ("sqlite3_step fail:%d", rc));
12175                 EM_DEBUG_LOG("mbox_list %d", mbox_list + i);
12176         }
12177
12178         ret = true;
12179
12180 FINISH_OFF:
12181         if (ret == true)
12182                 *mailbox_list = mbox_list;
12183         else
12184                 EM_SAFE_FREE(mbox_list);
12185
12186         if (hStmt != NULL) {
12187                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12188
12189                 rc = sqlite3_finalize(hStmt);
12190                 hStmt = NULL;
12191                 if (rc != SQLITE_OK) {
12192                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
12193                         error = EMAIL_ERROR_DB_FAILURE;
12194                 }
12195         }
12196
12197         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12198         _DISCONNECT_DB;
12199         if (err_code != NULL)
12200                 *err_code = error;
12201         EM_DEBUG_FUNC_END("ret [%d]", ret);
12202         return ret;
12203 }
12204
12205
12206 INTERNAL_FUNC int emstorage_get_pbd_account_list(int **account_list, int *count, int transaction, int *err_code)
12207 {
12208         EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
12209
12210         if (NULL == &account_list || NULL == count) {
12211                 EM_DEBUG_EXCEPTION("mailbox_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
12212                 if (err_code != NULL)
12213                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12214                 return false;
12215         }
12216
12217         int ret = false;
12218         int error = EMAIL_ERROR_NONE;
12219         char *sql;
12220         char **result;
12221         int i = 0, rc = -1;
12222         int *result_account_list = NULL;
12223         DB_STMT hStmt = NULL;
12224         char sql_query_string[QUERY_SIZE] = {0, };
12225
12226         sqlite3 *local_db_handle = emstorage_get_db_connection();
12227
12228         EMSTORAGE_START_READ_TRANSACTION(transaction);
12229
12230
12231         sql = "SELECT count(distinct account_id) FROM mail_partial_body_activity_tbl";
12232
12233
12234         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
12235         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12236                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
12237
12238         *count = atoi(result[1]);
12239         sqlite3_free_table(result);
12240
12241         if (!*count) {
12242                 EM_DEBUG_EXCEPTION("no account found...");
12243                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
12244                 ret = true;
12245                 goto FINISH_OFF;
12246         }
12247
12248         EM_DEBUG_LOG("Account count [%d]", *count);
12249
12250         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12251
12252         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_partial_body_activity_tbl");
12253
12254         EM_DEBUG_LOG("Query [%s]", sql_query_string);
12255
12256
12257         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12258
12259         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
12260         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12261                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12262
12263
12264         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12265         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12266                 ("sqlite3_step fail:%d", rc));
12267
12268         if (NULL == (result_account_list = (int *)em_malloc(sizeof(int) * (*count)))) {
12269                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
12270                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12271                 goto FINISH_OFF;
12272         }
12273
12274         memset(result_account_list, 0x00, sizeof(int) * (*count));
12275
12276         for (i = 0; i < (*count); i++) {
12277                 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
12278
12279                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12280                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12281                         ("sqlite3_step fail:%d", rc));
12282                 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
12283         }
12284
12285         ret = true;
12286
12287 FINISH_OFF:
12288         if (ret == true)
12289                 *account_list = result_account_list;
12290         else
12291                 EM_SAFE_FREE(result_account_list);
12292
12293         if (hStmt != NULL) {
12294                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12295
12296                 rc = sqlite3_finalize(hStmt);
12297                 hStmt = NULL;
12298                 if (rc != SQLITE_OK) {
12299                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
12300                         error = EMAIL_ERROR_DB_FAILURE;
12301                 }
12302         }
12303
12304         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12305         _DISCONNECT_DB;
12306         if (err_code != NULL)
12307                 *err_code = error;
12308         EM_DEBUG_FUNC_END("ret [%d]", ret);
12309         return ret;
12310 }
12311
12312
12313
12314 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)
12315 {
12316         EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
12317
12318         if (account_id < FIRST_ACCOUNT_ID || NULL == event_start || 0 == input_mailbox_id || NULL == count) {
12319                 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);
12320
12321                 if (err_code != NULL)
12322                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12323                 return false;
12324         }
12325
12326         int rc = -1;
12327         int ret = false;
12328         char **result;
12329         int error = EMAIL_ERROR_NONE;
12330         int i = 0;
12331         DB_STMT hStmt = NULL;
12332         email_event_partial_body_thd* event_list = NULL;
12333         char sql_query_string[QUERY_SIZE] = {0, };
12334
12335         sqlite3 *local_db_handle = emstorage_get_db_connection();
12336
12337         EMSTORAGE_START_READ_TRANSACTION(transaction);
12338
12339         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12340         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);
12341
12342
12343         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
12344         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12345                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12346
12347         *count = atoi(result[1]);
12348         sqlite3_free_table(result);
12349
12350         EM_DEBUG_LOG("Query = [%s]", sql_query_string);
12351
12352         if (!*count) {
12353                 EM_DEBUG_EXCEPTION("No matched activity found in mail_partial_body_activity_tbl");
12354
12355                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
12356                 ret = true;
12357                 goto FINISH_OFF;
12358         }
12359         EM_DEBUG_LOG("Activity Count = %d", *count);
12360
12361         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12362         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);
12363
12364         EM_DEBUG_LOG("Query [%s]", sql_query_string);
12365
12366
12367         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12368
12369         EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
12370         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12371                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12372
12373
12374         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12375         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12376                 ("sqlite3_step fail:%d", rc));
12377
12378         if (!(event_list = (email_event_partial_body_thd*)em_malloc(sizeof(email_event_partial_body_thd) * (*count)))) {
12379                 EM_DEBUG_EXCEPTION("Malloc failed");
12380
12381                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12382                 goto FINISH_OFF;
12383         }
12384         memset(event_list, 0x00, sizeof(email_event_partial_body_thd) * (*count));
12385
12386         for (i=0; i < (*count); i++) {
12387                 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_IDX_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12388                 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12389                 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12390                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12391                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_type), ACTIVITY_TYPE_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12392                 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12393                 _get_stmt_field_data_string(hStmt, &(event_list[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12394
12395                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12396                 /* EM_DEBUG_LOG("In emstorage_get_pbd_activity_data() loop, After sqlite3_step(), , i = %d, rc = %d.", i,  rc); */
12397                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12398                         ("sqlite3_step fail:%d", rc));
12399
12400                 event_list[i].event_type = 0;
12401         }
12402
12403         ret = true;
12404
12405 FINISH_OFF:
12406         if (true == ret)
12407           *event_start = event_list;
12408         else {
12409                 EM_SAFE_FREE(event_list);
12410         }
12411
12412         if (hStmt != NULL) {
12413                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12414
12415                 rc = sqlite3_finalize(hStmt);
12416                 hStmt = NULL;
12417                 if (rc != SQLITE_OK) {
12418                         EM_DEBUG_LOG("sqlite3_finalize failed - %d", rc);
12419                         error = EMAIL_ERROR_DB_FAILURE;
12420                 }
12421         }
12422
12423         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12424         _DISCONNECT_DB;
12425         if (err_code != NULL)
12426                 *err_code = error;
12427
12428         EM_DEBUG_FUNC_END("ret [%d]", ret);
12429         return ret;
12430 }
12431
12432
12433
12434 INTERNAL_FUNC int emstorage_delete_pbd_activity(int account_id, int mail_id, int activity_id, int transaction, int *err_code)
12435 {
12436         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);
12437
12438
12439         if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
12440                 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);
12441
12442                 if (err_code != NULL)
12443                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12444                 return false;
12445         }
12446
12447         int rc = -1;
12448         int ret = false;
12449         int error = EMAIL_ERROR_NONE;
12450         char sql_query_string[QUERY_SIZE] = {0, };
12451
12452         sqlite3 *local_db_handle = emstorage_get_db_connection();
12453
12454         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12455         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12456
12457         if (activity_id == 0)
12458                 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);
12459         else
12460                 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);
12461
12462         EM_DEBUG_LOG("Query [%s]", sql_query_string);
12463
12464         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12465         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12466                 ("sqlite3_exec fail:%d", rc));
12467         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12468                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12469
12470         /*  validate activity existence */
12471         rc = sqlite3_changes(local_db_handle);
12472         if (rc == 0)  {
12473                 EM_DEBUG_EXCEPTION("No matching activity found");
12474                 error = EMAIL_ERROR_DATA_NOT_FOUND;
12475                 ret = true;
12476                 goto FINISH_OFF;
12477         }
12478
12479         ret = true;
12480
12481 FINISH_OFF:
12482
12483         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12484
12485         _DISCONNECT_DB;
12486         if (err_code != NULL)
12487                 *err_code = error;
12488
12489         EM_DEBUG_FUNC_END("ret [%d]", ret);
12490         return ret;
12491 }
12492
12493 INTERNAL_FUNC int emstorage_get_mailbox_pbd_activity_count(int account_id, int input_mailbox_id, int *activity_count, int transaction, int *err_code)
12494 {
12495         EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
12496
12497         if (account_id < FIRST_ACCOUNT_ID || NULL == activity_count || NULL == err_code) {
12498                 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
12499                 if (err_code != NULL)
12500                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12501                 return false;
12502         }
12503         int rc = -1;
12504         int ret = false;
12505         int error = EMAIL_ERROR_NONE;
12506         char sql_query_string[QUERY_SIZE] = {0, };
12507
12508         DB_STMT hStmt = NULL;
12509
12510         sqlite3 *local_db_handle = emstorage_get_db_connection();
12511
12512         EMSTORAGE_START_READ_TRANSACTION(transaction);
12513         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12514
12515         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);
12516
12517         EM_DEBUG_LOG(" Query [%s]", sql_query_string);
12518
12519
12520         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12521         EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
12522         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12523                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12524
12525
12526         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12527         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12528                 ("sqlite3_step fail:%d", rc));
12529
12530         _get_stmt_field_data_int(hStmt, activity_count, 0);
12531
12532         EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
12533
12534         ret = true;
12535
12536 FINISH_OFF:
12537
12538         if (hStmt != NULL) {
12539                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12540                 rc = sqlite3_finalize(hStmt);
12541                 hStmt=NULL;
12542                 if (rc != SQLITE_OK) {
12543                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
12544                         error = EMAIL_ERROR_DB_FAILURE;
12545                 }
12546                 EM_DEBUG_LOG("sqlite3_finalize- %d", rc);
12547         }
12548
12549         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12550         _DISCONNECT_DB;
12551
12552         if (err_code != NULL)
12553                 *err_code = error;
12554
12555         EM_DEBUG_FUNC_END("ret [%d]", ret);
12556         return ret;
12557 }
12558
12559
12560 INTERNAL_FUNC int emstorage_get_pbd_activity_count(int *activity_count, int transaction, int *err_code)
12561 {
12562         EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
12563
12564         if (NULL == activity_count || NULL == err_code) {
12565                 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
12566                 if (err_code != NULL)
12567                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12568                 return false;
12569         }
12570         int rc = -1;
12571         int ret = false;
12572         int error = EMAIL_ERROR_NONE;
12573         DB_STMT hStmt = NULL;
12574         char sql_query_string[QUERY_SIZE] = {0, };
12575
12576         sqlite3 *local_db_handle = emstorage_get_db_connection();
12577
12578         EMSTORAGE_START_READ_TRANSACTION(transaction);
12579         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12580
12581         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl;");
12582
12583         EM_DEBUG_LOG(" Query [%s]", sql_query_string);
12584
12585
12586         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12587         EM_DEBUG_LOG("  before sqlite3_prepare hStmt = %p", hStmt);
12588         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12589                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12590
12591
12592         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12593         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12594                 ("sqlite3_step fail:%d", rc));
12595
12596         _get_stmt_field_data_int(hStmt, activity_count, 0);
12597
12598         EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
12599
12600         ret = true;
12601
12602 FINISH_OFF:
12603
12604
12605         if (hStmt != NULL) {
12606                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12607
12608                 rc = sqlite3_finalize(hStmt);
12609                 hStmt=NULL;
12610                 if (rc != SQLITE_OK) {
12611                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
12612                         error = EMAIL_ERROR_DB_FAILURE;
12613                 }
12614                 EM_DEBUG_LOG("sqlite3_finalize- %d", rc);
12615         }
12616
12617         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12618         _DISCONNECT_DB;
12619         if (err_code != NULL)
12620                 *err_code = error;
12621
12622         EM_DEBUG_FUNC_END("ret [%d]", ret);
12623         return ret;
12624 }
12625
12626 INTERNAL_FUNC int emstorage_delete_full_pbd_activity_data(int account_id, int transaction, int *err_code)
12627 {
12628         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
12629         if (account_id < FIRST_ACCOUNT_ID) {
12630                 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
12631                 if (err_code != NULL)
12632                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12633                 return false;
12634         }
12635
12636         int rc = -1;
12637         int ret = false;
12638         int error = EMAIL_ERROR_NONE;
12639         char sql_query_string[QUERY_SIZE] = {0, };
12640
12641         sqlite3 *local_db_handle = emstorage_get_db_connection();
12642         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12643         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12644         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d", account_id);
12645
12646         EM_DEBUG_LOG("Query [%s]", sql_query_string);
12647
12648         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12649         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12650                 ("sqlite3_exec fail:%d", rc));
12651         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12652                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12653
12654
12655         rc = sqlite3_changes(local_db_handle);
12656         if (rc == 0) {
12657                 EM_DEBUG_EXCEPTION("No matching activities found in mail_partial_body_activity_tbl");
12658                 error = EMAIL_ERROR_DATA_NOT_FOUND;
12659                 ret = true;
12660                 goto FINISH_OFF;
12661         }
12662
12663         ret = true;
12664
12665 FINISH_OFF:
12666
12667         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12668         _DISCONNECT_DB;
12669         if (err_code != NULL)
12670                 *err_code = error;
12671
12672         EM_DEBUG_FUNC_END("ret [%d]", ret);
12673         return ret;
12674 }
12675
12676 /*Himanshu[h.gahlaut]-> Added below API to update mail_partial_body_activity_tbl
12677 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*/
12678
12679 INTERNAL_FUNC int emstorage_update_pbd_activity(char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code)
12680 {
12681         EM_DEBUG_FUNC_BEGIN("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
12682
12683         int rc = -1, ret = false;
12684         int error = EMAIL_ERROR_NONE;
12685         char sql_query_string[QUERY_SIZE] = {0, };
12686
12687         int transaction = true;
12688
12689         if (!old_server_uid || !new_server_uid || !mbox_name)  {
12690                 EM_DEBUG_EXCEPTION("Invalid parameters");
12691                 error = EMAIL_ERROR_INVALID_PARAM;
12692                 return false;
12693         }
12694
12695         sqlite3 *local_db_handle = emstorage_get_db_connection();
12696         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12697         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12698         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12699                  "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);
12700
12701         EM_DEBUG_LOG("Query [%s]", sql_query_string);
12702
12703         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12704         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12705                 ("sqlite3_exec fail:%d", rc));
12706         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12707                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12708
12709         rc = sqlite3_changes(local_db_handle);
12710         if (rc == 0) {
12711                 EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
12712                 error = EMAIL_ERROR_DATA_NOT_FOUND;
12713                 goto FINISH_OFF;
12714         }
12715
12716         ret = true;
12717
12718 FINISH_OFF:
12719
12720         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12721         _DISCONNECT_DB;
12722         if (err_code != NULL)
12723                 *err_code = error;
12724
12725         EM_DEBUG_FUNC_END("ret [%d]", ret);
12726         return ret;
12727 }
12728
12729
12730 INTERNAL_FUNC int emstorage_create_file(char *data_string, size_t file_size, char *dst_file_name, int *err_code)
12731 {
12732         EM_DEBUG_FUNC_BEGIN("file_size[%d] , dst_file_name[%s], err_code[%p]", file_size, dst_file_name, err_code);
12733
12734         int ret = false;
12735         int error = EMAIL_ERROR_NONE;
12736
12737         FILE* fp_dst = NULL;
12738
12739         if (!data_string || !dst_file_name)  {
12740                 EM_DEBUG_EXCEPTION("data_string[%p], dst_file_name[%p]", data_string, dst_file_name);
12741                 error = EMAIL_ERROR_INVALID_PARAM;
12742                 goto FINISH_OFF;
12743         }
12744
12745         fp_dst = fopen(dst_file_name, "w");
12746
12747         if (!fp_dst)  {
12748                 EM_DEBUG_EXCEPTION("fopen failed - %s", dst_file_name);
12749                 if (errno == 28)
12750                         error = EMAIL_ERROR_MAIL_MEMORY_FULL;
12751                 else
12752                         error = EMAIL_ERROR_SYSTEM_FAILURE;
12753                 goto FINISH_OFF;
12754         }
12755
12756         if (fwrite(data_string, 1, file_size, fp_dst) == 0) {
12757                 EM_DEBUG_EXCEPTION("fwrite failed...");
12758                 error = EMAIL_ERROR_UNKNOWN;
12759                 goto FINISH_OFF;
12760         }
12761
12762         ret = true;
12763
12764 FINISH_OFF:
12765
12766         if (fp_dst != NULL)
12767                 fclose(fp_dst);
12768
12769         if (err_code != NULL)
12770                 *err_code = error;
12771
12772         EM_DEBUG_FUNC_END("ret [%d]", ret);
12773         return ret;
12774 }
12775
12776 #endif /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
12777
12778
12779
12780 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
12781
12782 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)
12783 {
12784         EM_DEBUG_FUNC_BEGIN();
12785         int rc = -1;
12786         int ret = false;
12787         int error = EMAIL_ERROR_NONE;
12788         char sql_query_string[QUERY_SIZE] = {0, };
12789
12790         int transaction = true;
12791
12792         if (!old_server_uid || !new_server_uid || !mbox_name)  {
12793                 EM_DEBUG_EXCEPTION("Invalid parameters");
12794                 error = EMAIL_ERROR_INVALID_PARAM;
12795                 return false;
12796         }
12797
12798         EM_DEBUG_LOG("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
12799
12800         sqlite3 *local_db_handle = emstorage_get_db_connection();
12801         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12802
12803
12804         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12805                  "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);
12806
12807          EM_DEBUG_LOG(" Query [%s]", sql_query_string);
12808
12809         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12810          EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12811                  ("sqlite3_exec fail:%d", rc));
12812          EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12813                  ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12814
12815
12816          rc = sqlite3_changes(local_db_handle);
12817          if (rc == 0)
12818          {
12819                  EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
12820                  error = EMAIL_ERROR_DATA_NOT_FOUND;
12821                  goto FINISH_OFF;
12822          }
12823
12824
12825          ret = true;
12826
12827 FINISH_OFF:
12828
12829         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12830         _DISCONNECT_DB;
12831
12832         if (err_code != NULL)
12833                 *err_code = error;
12834
12835         EM_DEBUG_FUNC_END("ret [%d]", ret);
12836         return ret;
12837
12838 }
12839
12840
12841 /**
12842  * @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);
12843  * Prepare an array of mail_id and corresponding server mail id.
12844  *
12845  *@author                                       h.gahlaut@samsung.com
12846  * @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)
12847  *                                                      where 88 is the length of fixed keywords including ending null character in the QUERY to be formed
12848  * @param[out] idset                    Returns the array of mail_id and corresponding server_mail_id sorted by server_mail_ids
12849  * @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
12850  * @param[out] err_code         Returns the error code.
12851  * @remarks                                     An Example of Query to be exexuted in this API:
12852  *                                                      SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (12, 13, 56, 78);
12853  * @return This function returns true on success or false on failure.
12854  */
12855
12856 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)
12857 {
12858         EM_DEBUG_FUNC_BEGIN();
12859         EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds);
12860
12861         int error = EMAIL_ERROR_NONE;
12862         int ret = false;
12863         email_id_set_t* p_id_set = NULL;
12864         int count = 0;
12865         const int buf_size = QUERY_SIZE;
12866         char sql_query_string[QUERY_SIZE] = {0, };
12867         int space_left_in_query_buffer = buf_size;
12868         int i = 0;
12869         int rc = -1;
12870         char *server_mail_id = NULL;
12871         char **result = NULL;
12872         int col_index = 0;
12873
12874
12875         if (NULL == mail_ids || NULL == idset || NULL == id_set_count) {
12876                 EM_DEBUG_EXCEPTION("Invalid Parameters mail_ids[%p] idset[%p]  id_set_count [%p]", mail_ids, idset, id_set_count);
12877                 error = EMAIL_ERROR_INVALID_PARAM;
12878                 goto FINISH_OFF;
12879         }
12880
12881         sqlite3 *local_db_handle = emstorage_get_db_connection();
12882
12883         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);
12884
12885         EM_DEBUG_LOG("SQL Query formed [%s] ", sql_query_string);
12886
12887         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
12888         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
12889         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12890                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12891
12892         EM_DEBUG_LOG(" Count of mails [%d ]", count);
12893
12894         if (count <= 0) {
12895                 EM_DEBUG_EXCEPTION("Can't find proper mail");
12896                 error = EMAIL_ERROR_DATA_NOT_FOUND;
12897                 goto FINISH_OFF;
12898         }
12899
12900
12901         if (NULL == (p_id_set = (email_id_set_t*)em_malloc(sizeof(email_id_set_t) * count)))  {
12902                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
12903                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12904                 goto FINISH_OFF;
12905         }
12906
12907         col_index = 2;
12908
12909         EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds_Loop);
12910         EM_DEBUG_LOG(">>>> DATA ASSIGN START");
12911         for (i = 0; i < count; i++)  {
12912                 _get_table_field_data_int(result, &(p_id_set[i].mail_id), col_index++);
12913                 _get_table_field_data_string(result, &server_mail_id, 1, col_index++);
12914                 p_id_set[i].server_mail_id = strtoul(server_mail_id, NULL, 10);
12915                 EM_SAFE_FREE(server_mail_id);
12916         }
12917         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d]", count);
12918         EM_PROFILE_END(EmStorageGetIdSetFromMailIds_Loop);
12919
12920         sqlite3_free_table(result);
12921         result = NULL;
12922
12923         ret = true;
12924
12925         FINISH_OFF:
12926
12927         if (ret == true)  {
12928                 *idset = p_id_set;
12929                 *id_set_count = count;
12930                 EM_DEBUG_LOG(" idset[%p] id_set_count [%d]", *idset, *id_set_count);
12931         }
12932         else
12933                 EM_SAFE_FREE(p_id_set);
12934
12935         _DISCONNECT_DB;
12936
12937         if (err_code != NULL)
12938                 *err_code = error;
12939
12940         EM_PROFILE_END(EmStorageGetIdSetFromMailIds);
12941
12942         EM_DEBUG_FUNC_END("ret [%d]", ret);
12943         return ret;
12944 }
12945
12946
12947
12948 #endif
12949
12950 INTERNAL_FUNC int emstorage_delete_triggers_from_lucene()
12951 {
12952         EM_DEBUG_FUNC_BEGIN();
12953         int rc, ret = true, transaction = true;
12954         int error = EMAIL_ERROR_NONE;
12955         char sql_query_string[QUERY_SIZE] = {0, };
12956
12957         sqlite3 *local_db_handle = emstorage_get_db_connection();
12958         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12959
12960         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerDelete;");
12961
12962         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12963         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12964                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12965
12966         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerInsert;");
12967
12968         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12969         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12970                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12971
12972
12973         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerUpdate;");
12974
12975         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12976         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12977                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12978
12979         ret = true;
12980
12981 FINISH_OFF:
12982
12983         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12984
12985         _DISCONNECT_DB;
12986
12987         EM_DEBUG_FUNC_END("ret [%d]", ret);
12988         return ret;
12989 }
12990
12991 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)
12992 {
12993         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);
12994
12995         if ((account_id <= 0) || (dest_mailbox_id <= 0) || (!rule) || (!rule->value)|| (!filtered_mail_id_list) || (!count_of_mails)) {
12996                 EM_DEBUG_EXCEPTION("Invalid Parameter");
12997
12998                 if (err_code != NULL)
12999                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13000                 return false;
13001         }
13002
13003         int rc = -1, ret = false, error = EMAIL_ERROR_NONE;
13004         int count = 0, col_index = 0, i = 0, where_pararaph_length = 0, *mail_list = NULL;
13005         char **result = NULL, *where_pararaph = NULL;
13006         char sql_query_string[QUERY_SIZE] = {0, };
13007
13008         sqlite3 *local_db_handle = emstorage_get_db_connection();
13009
13010         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl ");
13011
13012         EM_DEBUG_LOG("rule->value [%s]", rule->value);
13013
13014         where_pararaph_length = EM_SAFE_STRLEN(rule->value) + 100;
13015         where_pararaph = malloc(sizeof(char) * where_pararaph_length);
13016
13017         if (where_pararaph == NULL) {
13018                 EM_DEBUG_EXCEPTION("malloc failed for where_pararaph.");
13019                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13020                 goto FINISH_OFF;
13021         }
13022
13023         memset(where_pararaph, 0, sizeof(char) * where_pararaph_length);
13024
13025         EM_DEBUG_LOG("rule->type [%d], rule->flag2[%d]", rule->type, rule->flag2);
13026
13027         if (rule->type == EMAIL_FILTER_FROM) {
13028                 if (rule->flag2 == RULE_TYPE_INCLUDES)
13029                         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);
13030                 else /*  RULE_TYPE_EXACTLY */
13031                         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);
13032         }
13033         else if (rule->type == EMAIL_FILTER_SUBJECT) {
13034                 if (rule->flag2 == RULE_TYPE_INCLUDES)
13035                         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);
13036                 else /*  RULE_TYPE_EXACTLY */
13037                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (3,5) AND subject = \'%s\'", account_id, rule->value);
13038         }
13039         else {
13040                 error = EMAIL_ERROR_INVALID_PARAM;
13041                 EM_DEBUG_EXCEPTION("rule->type is invald");
13042                 goto FINISH_OFF;
13043         }
13044
13045         /* prevent 34361 */
13046         if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
13047                 strcat(sql_query_string, where_pararaph);
13048
13049         EM_DEBUG_LOG("query[%s]", sql_query_string);
13050
13051         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
13052         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
13053         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13054                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13055
13056         EM_DEBUG_LOG("Count of mails [%d]", count);
13057
13058         if (count) {
13059                 mail_list = malloc(sizeof(int) * count);
13060                 if (mail_list == NULL) {
13061                         EM_DEBUG_EXCEPTION("malloc failed for mail_list.");
13062                         error = EMAIL_ERROR_OUT_OF_MEMORY;
13063                         goto FINISH_OFF;
13064                 }
13065
13066                 col_index = 1;
13067
13068                 for (i = 0; i < count; i++)
13069                         _get_table_field_data_int(result, &(mail_list[i]), col_index++);
13070
13071                 memset(sql_query_string, 0x00, QUERY_SIZE);
13072                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET mailbox_id = %d, mailbox_type = %d ", dest_mailbox_id, dest_mailbox_type);
13073
13074                 /* prevent 34361 */
13075                 if(strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
13076                 strcat(sql_query_string, where_pararaph);
13077
13078                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13079                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13080                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13081
13082 #ifdef __FEATURE_BODY_SEARCH__
13083                 /* Updating mail_text_tbl */
13084                 memset(sql_query_string, 0x00, QUERY_SIZE);
13085                 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_text_tbl SET mailbox_id = %d ", dest_mailbox_id);
13086                 if(strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
13087                         strcat(sql_query_string, where_pararaph);
13088                 EM_DEBUG_LOG("Query [%s]", sql_query_string);
13089
13090                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13091                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13092                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13093 #endif
13094         }
13095
13096         ret = true;
13097
13098 FINISH_OFF:
13099
13100         if (ret)
13101                 *filtered_mail_id_list = mail_list;
13102         else
13103                 EM_SAFE_FREE(mail_list);
13104
13105         sqlite3_free_table(result);
13106         result = NULL;
13107
13108         _DISCONNECT_DB;
13109
13110         EM_SAFE_FREE(where_pararaph);
13111
13112         if (ret && count_of_mails)
13113                 *count_of_mails = count;
13114
13115         if (err_code != NULL)
13116                 *err_code = error;
13117
13118         EM_DEBUG_FUNC_END("ret [%d]", ret);
13119         return ret;
13120 }
13121
13122 #define EMAIL_SLOT_UNIT 25
13123
13124 INTERNAL_FUNC int emstorage_set_mail_slot_size(int account_id, int mailbox_id, int new_slot_size, int transaction, int *err_code)
13125 {
13126         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);
13127         int rc = -1, ret = false, err = EMAIL_ERROR_NONE;
13128         int where_pararaph_length = 0;
13129         char *where_pararaph = NULL;
13130         char sql_query_string[QUERY_SIZE] = {0, };
13131         int and = 0;
13132         sqlite3 *local_db_handle = emstorage_get_db_connection();
13133
13134         EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
13135
13136         if (new_slot_size > 0)
13137                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = %d ", new_slot_size);
13138         else if (new_slot_size == 0)
13139                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", EMAIL_SLOT_UNIT);
13140         else
13141                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", new_slot_size * -1);
13142
13143
13144         if (mailbox_id)
13145                 where_pararaph_length = 80;
13146         else
13147                 where_pararaph_length = 50;
13148
13149         if (new_slot_size == 0)
13150                 where_pararaph_length += 70;
13151
13152         where_pararaph = malloc(sizeof(char) * where_pararaph_length);
13153         if (where_pararaph == NULL) {
13154                 EM_DEBUG_EXCEPTION("Memory allocation failed for where_pararaph");
13155                 err = EMAIL_ERROR_OUT_OF_MEMORY;
13156                 goto FINISH_OFF;
13157         }
13158         memset(where_pararaph, 0x00, where_pararaph_length);
13159
13160         if (account_id > ALL_ACCOUNT) {
13161                 and = 1;
13162                 if (mailbox_id)
13163                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_id = %d ", mailbox_id);
13164                 else
13165                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d ", account_id);
13166         }
13167
13168         if (new_slot_size == 0)
13169                 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"));
13170
13171         if (strlen(sql_query_string) + EM_SAFE_STRLEN(where_pararaph) < QUERY_SIZE) /* prevent 34363 */
13172                 strcat(sql_query_string, where_pararaph);
13173         else {
13174                 EM_DEBUG_EXCEPTION("Query buffer overflowed !!!");
13175                 err = EMAIL_ERROR_OUT_OF_MEMORY;
13176                 goto FINISH_OFF;
13177         }
13178
13179         EM_DEBUG_LOG("query[%s]", sql_query_string);
13180
13181         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13182         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13183         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13184
13185         ret = true;
13186
13187 FINISH_OFF:
13188
13189         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
13190
13191         _DISCONNECT_DB;
13192
13193         EM_SAFE_FREE(where_pararaph);
13194
13195         if (err_code != NULL)
13196                 *err_code = err;
13197
13198         EM_DEBUG_FUNC_END("ret [%d]", ret);
13199         return ret;
13200 }
13201
13202 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)
13203 {
13204         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);
13205
13206         if (!meeting_req || meeting_req->mail_id <= 0) {
13207                 if (meeting_req)
13208                 EM_DEBUG_EXCEPTION("mail_id[%]d", meeting_req->mail_id);
13209
13210                 if (err_code != NULL)
13211                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13212
13213                 return false;
13214         }
13215
13216         int rc = -1;
13217         int ret = false;
13218         int error = EMAIL_ERROR_NONE;
13219         DB_STMT hStmt = NULL;
13220         char sql_query_string[QUERY_SIZE] = {0, };
13221         int col_index = 0;
13222         time_t temp_unix_time = 0;
13223
13224         sqlite3 *local_db_handle = emstorage_get_db_connection();
13225         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13226
13227         SNPRINTF(sql_query_string, sizeof(sql_query_string),
13228                 "INSERT INTO mail_meeting_tbl VALUES "
13229                 "( ?"           /*  mail_id */
13230                 ", ?"           /*  account_id */
13231                 ", ?"           /*  mailbox_id */
13232                 ", ?"           /*  meeting_response */
13233                 ", ?"           /*  start_time */
13234                 ", ?"           /*  end_time */
13235                 ", ?"           /*  location */
13236                 ", ?"           /*  global_object_id */
13237                 ", ?"           /*  offset */
13238                 ", ?"           /*  standard_name */
13239                 ", ?"           /*  standard_time_start_date */
13240                 ", ?"           /*  standard_biad */
13241                 ", ?"           /*  daylight_name */
13242                 ", ?"           /*  daylight_time_start_date */
13243                 ", ?"           /*  daylight_bias */
13244                 " )");
13245
13246         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13247         if (rc != SQLITE_OK)  {
13248                 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
13249                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
13250
13251                 error = EMAIL_ERROR_DB_FAILURE;
13252                 goto FINISH_OFF;
13253         }
13254
13255         col_index = 0;
13256         /*
13257         EM_DEBUG_LOG(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
13258         EM_DEBUG_LOG(">>>>> account_id[%d]", account_id);
13259         EM_DEBUG_LOG(">>>>> mailbox_name[%s]", mailbox_name);
13260         EM_DEBUG_LOG(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
13261         EM_DEBUG_LOG(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
13262         EM_DEBUG_LOG(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
13263         EM_DEBUG_LOG(">>>>> meeting_req->location[%s]", meeting_req->location);
13264         EM_DEBUG_LOG(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
13265         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
13266         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
13267         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
13268         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
13269         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
13270         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
13271         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
13272         */
13273         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->mail_id);
13274         _bind_stmt_field_data_int(hStmt, col_index++, account_id);
13275         _bind_stmt_field_data_int(hStmt, col_index++, input_mailbox_id);
13276         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
13277
13278         temp_unix_time = timegm(&(meeting_req->start_time));
13279         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13280         temp_unix_time = timegm(&(meeting_req->end_time));
13281         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13282
13283         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->location, 0, LOCATION_LEN_IN_MAIL_MEETING_TBL);
13284         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->global_object_id, 0, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
13285
13286         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
13287         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.standard_name, 0, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
13288         temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
13289         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13290         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
13291
13292         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.daylight_name, 0, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
13293         temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
13294         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13295         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
13296
13297
13298         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13299         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
13300                 ("sqlite3_step fail:%d", rc));
13301         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13302                 ("sqlite3_step fail:%d", rc));
13303
13304         ret = true;
13305
13306 FINISH_OFF:
13307         if (hStmt != NULL)  {
13308                 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
13309
13310                 rc = sqlite3_finalize(hStmt);
13311                 if (rc != SQLITE_OK)  {
13312                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
13313                         error = EMAIL_ERROR_DB_FAILURE;
13314                 }
13315         }
13316
13317         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13318         _DISCONNECT_DB;
13319
13320         if (err_code != NULL)
13321                 *err_code = error;
13322
13323         EM_DEBUG_FUNC_END("ret [%d]", ret);
13324         return ret;
13325 }
13326
13327 INTERNAL_FUNC int emstorage_get_meeting_request(int mail_id, email_meeting_request_t ** meeting_req, int transaction, int *err_code)
13328 {
13329         EM_DEBUG_FUNC_BEGIN();
13330
13331         int count = 0;
13332         int rc = -1;
13333         int ret = false;
13334         int error = EMAIL_ERROR_NONE;
13335         email_meeting_request_t *p_temp_meeting_req = NULL;
13336         int col_index = 0;
13337         time_t temp_unix_time;
13338
13339         EM_IF_NULL_RETURN_VALUE(meeting_req, false);
13340
13341         DB_STMT hStmt = NULL;
13342         char sql_query_string[QUERY_SIZE] = {0, };
13343
13344         sqlite3 *local_db_handle = emstorage_get_db_connection();
13345         EMSTORAGE_START_READ_TRANSACTION(transaction);
13346
13347         SNPRINTF(sql_query_string, sizeof(sql_query_string),
13348                 "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 "
13349                 " FROM mail_meeting_tbl "
13350                 " WHERE mail_id = %d", mail_id);
13351         EM_DEBUG_LOG("sql : %s ", sql_query_string);
13352
13353
13354         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13355
13356         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13357                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13358
13359
13360         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13361         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13362                 ("sqlite3_step fail:%d", rc));
13363
13364         if (rc == SQLITE_DONE)  {
13365                 EM_DEBUG_EXCEPTION(" no Meeting request found...");
13366                 count = 0;
13367                 ret = false;
13368                 error= EMAIL_ERROR_DATA_NOT_FOUND;
13369                 goto FINISH_OFF;
13370         }
13371
13372         if (!(p_temp_meeting_req = (email_meeting_request_t*)malloc(sizeof(email_meeting_request_t)))) {
13373                 EM_DEBUG_EXCEPTION(" malloc failed...");
13374                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13375                 goto FINISH_OFF;
13376         }
13377
13378         memset(p_temp_meeting_req, 0x00, sizeof(email_meeting_request_t));
13379
13380         col_index = 0;
13381
13382         _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->mail_id), col_index++);
13383         _get_stmt_field_data_int(hStmt, (int *)&(p_temp_meeting_req->meeting_response), col_index++);
13384         _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
13385         gmtime_r(&temp_unix_time, &(p_temp_meeting_req->start_time));
13386         _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
13387         gmtime_r(&temp_unix_time, &(p_temp_meeting_req->end_time));
13388         _get_stmt_field_data_string(hStmt, &p_temp_meeting_req->location, 1, col_index++);
13389         _get_stmt_field_data_string(hStmt, &p_temp_meeting_req->global_object_id, 1, col_index++);
13390         _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->time_zone.offset_from_GMT), col_index++);
13391
13392         _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++);
13393         _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
13394         gmtime_r(&temp_unix_time, &(p_temp_meeting_req->time_zone.standard_time_start_date));
13395         _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->time_zone.standard_bias), col_index++);
13396
13397         _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++);
13398         _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
13399         gmtime_r(&temp_unix_time, &(p_temp_meeting_req->time_zone.daylight_time_start_date));
13400         _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->time_zone.daylight_bias), col_index++);
13401
13402         /*
13403         EM_DEBUG_LOG(">>>>> meeting_req->mail_id[%d]", p_temp_meeting_req->mail_id);
13404         EM_DEBUG_LOG(">>>>> meeting_req->meeting_response[%d]", p_temp_meeting_req->meeting_response);
13405         EM_DEBUG_LOG(">>>>> meeting_req->start_time[%s]", asctime(&(p_temp_meeting_req->start_time)));
13406         EM_DEBUG_LOG(">>>>> meeting_req->end_time[%s]", asctime(&(p_temp_meeting_req->end_time)));
13407         EM_DEBUG_LOG(">>>>> meeting_req->location[%s]", p_temp_meeting_req->location);
13408         EM_DEBUG_LOG(">>>>> meeting_req->global_object_id[%s]", p_temp_meeting_req->global_object_id);
13409         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", p_temp_meeting_req->time_zone.offset_from_GMT);
13410         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_name[%s]", p_temp_meeting_req->time_zone.standard_name);
13411         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(p_temp_meeting_req->time_zone.standard_time_start_date)));
13412         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_bias[%d]", p_temp_meeting_req->time_zone.standard_bias);
13413         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_name[%s]", p_temp_meeting_req->time_zone.daylight_name);
13414         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(p_temp_meeting_req->time_zone.daylight_time_start_date)));
13415         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_bias[%d]", p_temp_meeting_req->time_zone.daylight_bias);
13416         */
13417         ret = true;
13418
13419 FINISH_OFF:
13420         if (ret == true)
13421                 *meeting_req = p_temp_meeting_req;
13422         else  {
13423                 EM_SAFE_FREE(p_temp_meeting_req);
13424         }
13425
13426         if (hStmt != NULL)  {
13427                 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
13428
13429                 rc = sqlite3_finalize(hStmt);
13430                 hStmt = NULL;
13431                 if (rc != SQLITE_OK)  {
13432                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
13433                         error = EMAIL_ERROR_DB_FAILURE;
13434                 }
13435         }
13436
13437         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13438         _DISCONNECT_DB;
13439
13440         if (err_code != NULL)
13441                 *err_code = error;
13442
13443         EM_DEBUG_FUNC_END("ret [%d]", ret);
13444         return ret;
13445 }
13446
13447
13448 INTERNAL_FUNC int emstorage_update_meeting_request(email_meeting_request_t* meeting_req, int transaction, int *err_code)
13449 {
13450         EM_DEBUG_FUNC_BEGIN("meeting_req[%p], transaction[%d], err_code[%p]", meeting_req, transaction, err_code);
13451
13452         int ret = false;
13453         int error = EMAIL_ERROR_NONE;
13454         int rc;
13455         DB_STMT hStmt = NULL;
13456         char sql_query_string[QUERY_SIZE] = {0, };
13457         time_t temp_unix_time = 0;
13458
13459         if (!meeting_req) {
13460                 EM_DEBUG_EXCEPTION("Invalid Parameter!");
13461                 error = EMAIL_ERROR_INVALID_PARAM;
13462                 goto FINISH_OFF;
13463         }
13464
13465         sqlite3 *local_db_handle = emstorage_get_db_connection();
13466         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13467
13468         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13469         SNPRINTF(sql_query_string, sizeof(sql_query_string),
13470                 "UPDATE mail_meeting_tbl "
13471                 "SET "
13472                 /* "  account_id = ?, "         //  not update here, this can be changed when move or copy */
13473                 /* "  mailbox_name = ?, "               //  not update here, this can be changed when move or copy */
13474                 "  meeting_response = ?, "
13475                 "  start_time = ?, "
13476                 "  end_time = ?, "
13477                 "  location = ?, "
13478                 "  global_object_id = ?, "
13479                 "  offset = ?, "
13480                 "  standard_name = ?, "
13481                 "  standard_time_start_date = ?, "
13482                 "  standard_bias = ?, "
13483                 "  daylight_name = ?, "
13484                 "  daylight_time_start_date = ?, "
13485                 "  daylight_bias = ? "
13486                 "WHERE mail_id = %d",
13487                 meeting_req->mail_id);
13488
13489         EM_DEBUG_LOG("SQL(%s)", sql_query_string);
13490
13491         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13492         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13493                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13494 /*
13495         EM_DEBUG_LOG(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
13496         EM_DEBUG_LOG(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
13497         EM_DEBUG_LOG(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
13498         EM_DEBUG_LOG(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
13499         EM_DEBUG_LOG(">>>>> meeting_req->location[%s]", meeting_req->location);
13500         EM_DEBUG_LOG(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
13501         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
13502         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
13503         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
13504         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
13505         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
13506         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
13507         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
13508 */
13509         int col_index = 0;
13510
13511         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
13512         temp_unix_time = timegm(&(meeting_req->start_time));
13513         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13514         temp_unix_time = timegm(&(meeting_req->end_time));
13515         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13516         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->location, 1, LOCATION_LEN_IN_MAIL_MEETING_TBL);
13517         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->global_object_id, 1, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
13518         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
13519         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.standard_name, 1, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
13520         temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
13521         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13522         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
13523         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.daylight_name, 1, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
13524         temp_unix_time = timegm(&(meeting_req->time_zone.daylight_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.daylight_bias);
13527
13528
13529         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13530         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13531                 ("sqlite3_step fail:%d", rc));
13532         ret = true;
13533
13534
13535 FINISH_OFF:
13536         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13537
13538         if (hStmt != NULL)  {
13539                 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
13540
13541                 rc = sqlite3_finalize(hStmt);
13542                 if (rc != SQLITE_OK)  {
13543                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
13544
13545                         error = EMAIL_ERROR_DB_FAILURE;
13546                 }
13547         }
13548
13549         _DISCONNECT_DB;
13550
13551         if (err_code != NULL)
13552                 *err_code = error;
13553
13554         EM_DEBUG_FUNC_END("ret [%d]", ret);
13555         return ret;
13556 }
13557
13558 INTERNAL_FUNC int emstorage_delete_meeting_request(int account_id, int mail_id, int input_mailbox_id, int transaction, int *err_code)
13559 {
13560         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);
13561
13562         if (account_id < ALL_ACCOUNT || mail_id < 0) {
13563                 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d]", account_id, mail_id);
13564
13565                 if (err_code != NULL)
13566                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13567                 return false;
13568         }
13569
13570         int rc;
13571         int ret = false;
13572         int error = EMAIL_ERROR_NONE;
13573         int and = false;
13574         char sql_query_string[QUERY_SIZE] = {0, };
13575
13576         sqlite3 *local_db_handle = emstorage_get_db_connection();
13577         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13578
13579         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_meeting_tbl ");
13580
13581         if (account_id != ALL_ACCOUNT) {                /*  NOT '0' means a specific account. '0' means all account */
13582                 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);
13583                 and = true;
13584         }
13585         if (mail_id > 0) {
13586                 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);
13587                 and = true;
13588         }
13589         if (input_mailbox_id > 0) {             /*  0 means all mailbox_id */
13590                 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);
13591         }
13592
13593         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13594         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13595                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13596
13597         ret = true;
13598
13599 FINISH_OFF:
13600         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13601         _DISCONNECT_DB;
13602
13603         if (err_code)
13604                 *err_code = error;
13605
13606         EM_DEBUG_FUNC_END("ret [%d]", ret);
13607         return ret;
13608 }
13609
13610
13611 INTERNAL_FUNC void emstorage_free_meeting_request(email_meeting_request_t *meeting_req)
13612 {
13613         EM_DEBUG_FUNC_BEGIN();
13614
13615         if (!meeting_req ) {
13616                 EM_DEBUG_EXCEPTION("NULL PARAM");
13617                 return;
13618                 }
13619
13620         EM_SAFE_FREE(meeting_req->location);
13621         EM_SAFE_FREE(meeting_req->global_object_id);
13622
13623         EM_DEBUG_FUNC_END();
13624 }
13625
13626
13627 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)
13628 {
13629         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);
13630         EM_PROFILE_BEGIN(profile_emstorage_get_overflowed_mail_id_list);
13631         char sql_query_string[QUERY_SIZE] = {0, };
13632         char **result = NULL;
13633         int rc = -1, ret = false;
13634         int error = EMAIL_ERROR_NONE;
13635         int counter = 0, col_index = 0;
13636         int result_mail_id_count = 0;
13637         int *result_mail_id_list = NULL;
13638
13639         if (input_mailbox_id <= 0 || !mail_id_list || !mail_id_count || account_id < 1) {
13640                 EM_DEBUG_EXCEPTION("Invalid Parameter");
13641                 error = EMAIL_ERROR_INVALID_PARAM;
13642                 goto FINISH_OFF;
13643         }
13644
13645         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);
13646
13647         EM_DEBUG_LOG("query[%s].", sql_query_string);
13648
13649         sqlite3 *local_db_handle = emstorage_get_db_connection();
13650         EMSTORAGE_START_READ_TRANSACTION(transaction);
13651
13652         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_mail_id_count, 0, NULL), rc);
13653         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13654                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13655
13656         if (!result_mail_id_count) {
13657                 EM_DEBUG_LOG("No mail found...");
13658                 ret = false;
13659                 error= EMAIL_ERROR_MAIL_NOT_FOUND;
13660                 goto FINISH_OFF;
13661         }
13662
13663         EM_DEBUG_LOG("There are [%d] overflowed mails in mailbox_id [%d]", result_mail_id_count, input_mailbox_id);
13664
13665         if (!(result_mail_id_list = (int *)malloc(sizeof(int) * result_mail_id_count))) {
13666                 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
13667                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13668                 sqlite3_free_table(result);
13669                 goto FINISH_OFF;
13670         }
13671
13672         memset(result_mail_id_list, 0x00, sizeof(int) * result_mail_id_count);
13673
13674         col_index = 1;
13675
13676         for (counter = 0; counter < result_mail_id_count; counter++)
13677                 _get_table_field_data_int(result, result_mail_id_list + counter, col_index++);
13678
13679         ret = true;
13680
13681 FINISH_OFF:
13682         EM_DEBUG_LOG("finish off [%d]", ret);
13683
13684         if (result)
13685                 sqlite3_free_table(result);
13686
13687         if (ret == true)  {
13688                 *mail_id_list = result_mail_id_list;
13689                 *mail_id_count = result_mail_id_count;
13690         }
13691         else
13692                 EM_SAFE_FREE(result_mail_id_list);
13693
13694         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13695         _DISCONNECT_DB;
13696
13697         if (err_code != NULL)
13698                 *err_code = error;
13699
13700         EM_PROFILE_END(profile_emstorage_get_overflowed_mail_id_list);
13701         EM_DEBUG_FUNC_END("ret [%d]", ret);
13702         return ret;
13703 }
13704
13705
13706 INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(int mail_id, int *thread_id, int *err_code)
13707 {
13708         EM_DEBUG_FUNC_BEGIN("mail_id[%d], thread_id[%p], err_code[%p]", mail_id, thread_id, err_code);
13709
13710         int rc = -1, ret = false;
13711         int err = EMAIL_ERROR_NONE;
13712         char sql_query_string[QUERY_SIZE] = {0, };
13713         char **result;
13714         int result_count = 0;
13715
13716         if (mail_id == 0 || thread_id == NULL) {
13717                 EM_DEBUG_EXCEPTION("Invalid Parameter");
13718                 if (err_code != NULL)
13719                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13720                 goto FINISH_OFF;
13721         }
13722
13723         sqlite3 *local_db_handle = emstorage_get_db_connection();
13724
13725         memset(sql_query_string, 0, QUERY_SIZE);
13726         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT thread_id FROM mail_tbl WHERE mail_id = %d", mail_id);
13727
13728         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
13729         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
13730         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13731                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13732
13733         if (!result_count) {
13734                 EM_DEBUG_EXCEPTION("No mail found...");
13735                 ret = false;
13736                 err= EMAIL_ERROR_MAIL_NOT_FOUND;
13737                 /* sqlite3_free_table(result); */
13738                 goto FINISH_OFF;
13739         }
13740
13741         _get_table_field_data_int(result, thread_id, 1);
13742
13743         sqlite3_free_table(result);
13744
13745         ret = true;
13746
13747 FINISH_OFF:
13748
13749         _DISCONNECT_DB;
13750
13751         if (err_code != NULL)
13752                 *err_code = err;
13753
13754         EM_DEBUG_FUNC_END("ret [%d]", ret);
13755         return ret;
13756 }
13757
13758
13759 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)
13760 {
13761         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);
13762
13763         int rc = -1, ret = false;
13764         int err = EMAIL_ERROR_NONE;
13765         char sql_query_string[QUERY_SIZE] = {0, };
13766         char **result = NULL;
13767         int result_count = 0;
13768
13769         if (thread_id == 0) {
13770                 EM_DEBUG_EXCEPTION("Invalid Parameter");
13771                 if (err_code != NULL)
13772                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13773                 goto FINISH_OFF;
13774         }
13775
13776         sqlite3 *local_db_handle = emstorage_get_db_connection();
13777
13778         if (thread_item_count == 0 || latest_mail_id == 0) {
13779                 memset(sql_query_string, 0, QUERY_SIZE);
13780                 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);
13781
13782                 /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
13783                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
13784                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13785                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13786                 EM_DEBUG_LOG("result_count[%d]", result_count);
13787                 if (result_count == 0) {
13788                         EM_DEBUG_EXCEPTION("No mail found...");
13789                         ret = false;
13790                         err= EMAIL_ERROR_MAIL_NOT_FOUND;
13791                         sqlite3_free_table(result);
13792                         goto FINISH_OFF;
13793                 }
13794
13795                 _get_table_field_data_int(result, &latest_mail_id, 2);
13796                 _get_table_field_data_int(result, &thread_item_count, 3);
13797
13798                 EM_DEBUG_LOG("latest_mail_id[%d]", latest_mail_id);
13799                 EM_DEBUG_LOG("thread_item_count[%d]", thread_item_count);
13800
13801                 sqlite3_free_table(result);
13802         }
13803
13804         EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
13805
13806         /* if (thread_item_count > 1) */
13807         /* { */
13808                 memset(sql_query_string, 0, QUERY_SIZE);
13809                 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);
13810                 EM_DEBUG_LOG("query[%s]", sql_query_string);
13811
13812                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13813                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13814                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13815         /* } */
13816
13817         memset(sql_query_string, 0, QUERY_SIZE);
13818         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);
13819         EM_DEBUG_LOG("query[%s]", sql_query_string);
13820
13821         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13822                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13823                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13824
13825         ret = true;
13826
13827 FINISH_OFF:
13828
13829         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
13830
13831         _DISCONNECT_DB;
13832
13833         if (err_code != NULL)
13834                 *err_code = err;
13835
13836         EM_DEBUG_FUNC_END("ret [%d]", ret);
13837         return ret;
13838 }
13839
13840
13841 INTERNAL_FUNC void emstorage_flush_db_cache()
13842 {
13843         sqlite3_release_memory(-1);
13844 }
13845
13846 #ifdef __FEATURE_LOCAL_ACTIVITY__
13847 /**
13848   *     emstorage_add_activity - Add Email Local activity during OFFLINE mode
13849   *
13850   */
13851 INTERNAL_FUNC int emstorage_add_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
13852 {
13853         EM_DEBUG_FUNC_BEGIN();
13854
13855         EM_DEBUG_LOG(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
13856
13857         int rc = -1, ret = false;
13858         int error = EMAIL_ERROR_NONE;
13859         DB_STMT hStmt = NULL;
13860         char sql_query_string[8192] = { 0x00, };
13861         int i = 0;
13862
13863         if (!local_activity) {
13864                 EM_DEBUG_EXCEPTION(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
13865                 if (err_code != NULL)
13866                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13867                 return false;
13868         }
13869
13870         sqlite3 *local_db_handle = emstorage_get_db_connection();
13871         memset(sql_query_string, 0x00 , sizeof(sql_query_string));
13872         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13873
13874         SNPRINTF(sql_query_string, sizeof(sql_query_string), "INSERT INTO mail_local_activity_tbl VALUES (?, ?, ?, ?, ?, ?, ?)");
13875
13876         EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ] ", local_activity->activity_id);
13877         EM_DEBUG_LOG(">>>>> MAIL ID [ %d ] ", local_activity->mail_id);
13878         EM_DEBUG_LOG(">>>>> ACCOUNT ID [ %d ] ", local_activity->account_id);
13879         EM_DEBUG_LOG(">>>>> ACTIVITY TYPE [ %d ] ", local_activity->activity_type);
13880         EM_DEBUG_LOG(">>>>> SERVER MAIL ID [ %s ] ", local_activity->server_mailid);
13881         EM_DEBUG_LOG(">>>>> SOURCE MAILBOX [ %s ] ", local_activity->src_mbox);
13882         EM_DEBUG_LOG(">>>>> DEST MAILBOX   [ %s ] ", local_activity->dest_mbox);
13883
13884         EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
13885
13886
13887         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13888         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13889                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13890
13891         EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
13892
13893
13894
13895
13896         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
13897         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
13898         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
13899         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
13900         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->server_mailid, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
13901         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->src_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
13902         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->dest_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
13903
13904
13905         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13906
13907         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
13908                 ("sqlite3_step fail:%d", rc));
13909         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13910                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
13911
13912         ret = true;
13913
13914 FINISH_OFF:
13915
13916         if (hStmt != NULL)  {
13917                 rc = sqlite3_finalize(hStmt);
13918                 if (rc != SQLITE_OK)  {
13919                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
13920
13921                         error = EMAIL_ERROR_DB_FAILURE;
13922                 }
13923         }
13924         else {
13925                 EM_DEBUG_LOG(" >>>>>>>>>> hStmt is NULL!!!");
13926         }
13927
13928         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13929         _DISCONNECT_DB;
13930
13931
13932         if (err_code != NULL)
13933                 *err_code = error;
13934
13935         EM_DEBUG_FUNC_END("ret [%d]", ret);
13936         return ret;
13937 }
13938
13939 /**
13940   *     emstorage_get_activity - Get the Local activity Information
13941   *
13942   *
13943   */
13944 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)
13945 {
13946         EM_DEBUG_FUNC_BEGIN();
13947
13948         int i = 0, count = 0, rc = -1, ret = false;
13949         int error = EMAIL_ERROR_NONE;
13950         emstorage_activity_tbl_t *p_activity_tbl = NULL;
13951         char sql_query_string[1024] = {0x00, };
13952         char **result = NULL;
13953         int col_index ;
13954
13955         EM_IF_NULL_RETURN_VALUE(activity_list, false);
13956         EM_IF_NULL_RETURN_VALUE(select_num, false);
13957
13958
13959         if (!select_num || !activity_list || account_id <= 0 || activityid < 0) {
13960                 EM_DEBUG_LOG(" select_num[%p], activity_list[%p] account_id [%d] activityid [%d] ", select_num, activity_list, account_id, activityid);
13961                 if (err_code != NULL)
13962                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13963                 return false;
13964         }
13965
13966         EMSTORAGE_START_READ_TRANSACTION(transaction);
13967
13968         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13969
13970         if (activityid == ALL_ACTIVITIES) {
13971                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d order by activity_id", account_id);
13972         }
13973         else {
13974                 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);
13975         }
13976
13977         EM_DEBUG_LOG("Query [%s]", sql_query_string);
13978
13979
13980
13981         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
13982         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13983                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13984
13985
13986
13987         col_index = 7;
13988
13989         if (!(p_activity_tbl = (emstorage_activity_tbl_t*)em_malloc(sizeof(emstorage_activity_tbl_t) * count))) {
13990                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
13991                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13992                 goto FINISH_OFF;
13993         }
13994
13995
13996         for (i = 0; i < count; i++)  {
13997                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13998                 if (result[col_index])
13999                         p_activity_tbl[i].activity_id = atoi(result[col_index++]);
14000
14001                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
14002                 if (result[col_index])
14003                         p_activity_tbl[i].account_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].mail_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].activity_type = atoi(result[col_index++]);
14012
14013
14014                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
14015                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index])>0)
14016                         p_activity_tbl[i].server_mailid = EM_SAFE_STRDUP(result[col_index++]);
14017                 else
14018                         col_index++;
14019
14020                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
14021                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index])>0)
14022                         p_activity_tbl[i].src_mbox = EM_SAFE_STRDUP(result[col_index++]);
14023                 else
14024                         col_index++;
14025
14026                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
14027                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index])>0)
14028                         p_activity_tbl[i].dest_mbox = EM_SAFE_STRDUP(result[col_index++]);
14029                 else
14030                         col_index++;
14031
14032         }
14033
14034         if (result)
14035                 sqlite3_free_table(result);
14036
14037         ret = true;
14038
14039 FINISH_OFF:
14040
14041
14042         if (ret == true)  {
14043                 *activity_list = p_activity_tbl;
14044                 *select_num = count;
14045                 EM_DEBUG_LOG(">>>> COUNT : %d >> ", count);
14046         }
14047         else if (p_activity_tbl != NULL) {
14048                 emstorage_free_local_activity(&p_activity_tbl, count, NULL);
14049         }
14050
14051         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14052         if (err_code != NULL)
14053                 *err_code = error;
14054
14055         EM_DEBUG_FUNC_END("ret [%d]", ret);
14056         return ret;
14057 }
14058
14059
14060 INTERNAL_FUNC int emstorage_get_next_activity_id(int *activity_id, int *err_code)
14061 {
14062
14063         EM_DEBUG_FUNC_BEGIN();
14064
14065         int ret = false;
14066         int err = EMAIL_ERROR_NONE;
14067         int rc = -1;
14068         char *sql = NULL;
14069         char **result = NULL;
14070
14071         if (NULL == activity_id)
14072         {
14073                 EM_DEBUG_EXCEPTION(" activity_id[%p]", activity_id);
14074
14075                 err = EMAIL_ERROR_INVALID_PARAM;
14076                 goto FINISH_OFF;
14077         }
14078
14079         /*  increase unique id */
14080
14081         sql = "SELECT max(rowid) FROM mail_local_activity_tbl;";
14082
14083         /*  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); */
14084         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14085                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
14086
14087         if (NULL==result[1])
14088                 rc = 1;
14089         else
14090                 rc = atoi(result[1])+1;
14091
14092         *activity_id = rc;
14093
14094         if (result)
14095                 sqlite3_free_table(result);
14096
14097         ret = true;
14098
14099         FINISH_OFF:
14100
14101         if (NULL != err_code) {
14102                 *err_code = err;
14103         }
14104
14105         EM_DEBUG_FUNC_END("ret [%d]", ret);
14106         return ret;
14107
14108 }
14109
14110 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)
14111 {
14112
14113         EM_DEBUG_FUNC_BEGIN();
14114
14115         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);
14116
14117         if (account_id <= 0|| NULL == activity_id_list || NULL == activity_id_count ||lowest_activity_type <=0 || highest_activity_type <= 0)  {
14118                 if (err_code != NULL)
14119                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14120                 return false;
14121         }
14122
14123         int ret = false;
14124         int error = EMAIL_ERROR_NONE;
14125         int i = 0, rc = -1, count = 0;
14126         char sql_query_string[1024] = {0x00, };
14127         int *activity_ids = NULL;
14128         int col_index = 0;
14129         char **result = NULL;
14130
14131         EMSTORAGE_START_READ_TRANSACTION(transaction);
14132
14133         memset(sql_query_string, 0x00, sizeof(sql_query_string));
14134
14135         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);
14136
14137         EM_DEBUG_LOG(" Query [%s]", sql_query_string);
14138
14139
14140         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
14141         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14142                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14143
14144         col_index = 1;
14145
14146         EM_DEBUG_LOG(" Activity COUNT : %d ... ", count);
14147
14148         if (NULL == (activity_ids = (int *)em_malloc(sizeof(int) * count))) {
14149                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
14150                 error = EMAIL_ERROR_OUT_OF_MEMORY;
14151                 goto FINISH_OFF;
14152         }
14153
14154         for (i = 0; i < count; i++)  {
14155                 activity_ids[i] = atoi(result[col_index]);
14156                 col_index++;
14157                 EM_DEBUG_LOG("activity_id %d", activity_ids[i]);
14158         }
14159
14160         ret = true;
14161
14162 FINISH_OFF:
14163
14164
14165         if (ret == true)  {
14166                 *activity_id_count = count;
14167                 *activity_id_list = activity_ids;
14168
14169         }
14170         else if (activity_ids != NULL) /* Prevent defect - 216566 */
14171                 EM_SAFE_FREE(activity_ids);
14172
14173
14174         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14175         if (err_code != NULL) {
14176                 *err_code = error;
14177         }
14178
14179         EM_DEBUG_FUNC_END("ret [%d]", ret);
14180         return ret;
14181 }
14182
14183 INTERNAL_FUNC int emstorage_free_activity_id_list(int *activity_id_list, int *error_code)
14184 {
14185         EM_DEBUG_FUNC_BEGIN();
14186
14187         int error = EMAIL_ERROR_NONE;
14188         int ret = false;
14189
14190         EM_DEBUG_LOG(" activity_id_list [%p]", activity_id_list);
14191
14192         if (NULL == activity_id_list) {
14193                 error = EMAIL_ERROR_INVALID_PARAM;
14194                 goto FINISH_OFF;
14195         }
14196         else {
14197                 free(activity_id_list);
14198                 activity_id_list = NULL;
14199         }
14200
14201
14202         ret= true;
14203
14204         FINISH_OFF:
14205
14206         if (NULL != error_code) {
14207                 *error_code = error;
14208         }
14209
14210         EM_DEBUG_FUNC_END("ret [%d]", ret);
14211         return ret;
14212 }
14213
14214 /**
14215  * emstorage_delete_local_activity - Deletes the Local acitivity Generated based on activity_type
14216  * or based on server mail id
14217  *
14218  */
14219 INTERNAL_FUNC int emstorage_delete_local_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
14220 {
14221         EM_DEBUG_FUNC_BEGIN();
14222
14223
14224         EM_DEBUG_LOG(" local_activity[%p] ", local_activity);
14225
14226         if (!local_activity)  {
14227                 EM_DEBUG_EXCEPTION(" local_activity[%p] ", local_activity);
14228                 if (err_code != NULL)
14229                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14230                 return false;
14231         }
14232
14233         int rc = -1, ret = false;                       /* Prevent_FIX  */
14234         int err = EMAIL_ERROR_NONE;
14235         int query_and = 0;
14236         int query_where = 0;
14237         char sql_query_string[8192] = { 0x00, };
14238         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
14239
14240         memset(sql_query_string, 0x00, sizeof(sql_query_string));
14241
14242         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_local_activity_tbl ");
14243
14244         EM_DEBUG_LOG(">>> Query [ %s ] ", sql_query_string);
14245
14246         if (local_activity->account_id) {
14247                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
14248                 " WHERE account_id = %d ", local_activity->account_id);
14249                 query_and = 1;
14250                 query_where = 1;
14251         }
14252
14253         if (local_activity->server_mailid) {
14254                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
14255                 " %s %s server_mailid = '%s' ", query_where? "": "WHERE", query_and? "AND":"", local_activity->server_mailid);
14256                 query_and = 1;
14257                 query_where = 1;
14258         }
14259
14260
14261         if (local_activity->mail_id) {
14262                 EM_DEBUG_LOG(">>>> MAIL ID [ %d ] , ACTIVITY TYPE [%d ]", local_activity->mail_id, local_activity->activity_type);
14263
14264                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
14265                 " %s %s mail_id = %d  ", query_where? "": "WHERE", query_and? "AND":"", local_activity->mail_id);
14266
14267                 query_and = 1;
14268                 query_where = 1;
14269
14270         }
14271
14272         if (local_activity->activity_type > 0) {
14273                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
14274                 " %s %s activity_type = %d ", query_where? "": "WHERE", query_and? "AND" : "" , local_activity->activity_type);
14275         }
14276
14277         if (local_activity->activity_id > 0) {
14278                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
14279                 " %s %s activity_id = %d ", query_where? "": "WHERE", query_and? "AND" : "" , local_activity->activity_id);
14280
14281         }
14282
14283         EM_DEBUG_LOG(">>>>> Query [ %s ] ", sql_query_string);
14284
14285         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
14286         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14287                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14288
14289         rc = sqlite3_changes(local_db_handle);
14290         if (rc == 0)  {
14291                 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
14292                 err = EMAIL_ERROR_MAILBOX_NOT_FOUND;
14293         }
14294
14295         ret = true;
14296
14297 FINISH_OFF:
14298         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
14299         if (err_code != NULL)
14300                 *err_code = err;
14301         EM_DEBUG_FUNC_END("ret [%d]", ret);
14302         return ret;
14303 }
14304
14305 /**
14306 *       emstorage_free_local_activity - Free the Local Activity data
14307 */
14308 INTERNAL_FUNC int emstorage_free_local_activity(emstorage_activity_tbl_t **local_activity_list, int count, int *err_code)
14309 {
14310         EM_DEBUG_FUNC_BEGIN();
14311
14312         EM_DEBUG_LOG(" local_activity_list[%p], count[%d], err_code[%p]", local_activity_list, count, err_code);
14313
14314         int ret = false;
14315         int error = EMAIL_ERROR_INVALID_PARAM;
14316
14317         if (count > 0) {
14318                 if (!local_activity_list || !*local_activity_list) {
14319                         EM_DEBUG_EXCEPTION(" local_activity_list[%p], count[%d]", local_activity_list, count);
14320
14321                         error = EMAIL_ERROR_INVALID_PARAM;
14322                         goto FINISH_OFF;
14323                 }
14324
14325                 emstorage_activity_tbl_t* p = *local_activity_list;
14326                 int i = 0;
14327                 if (p) {
14328                         for (; i < count; i++)  {
14329                                 EM_SAFE_FREE(p[i].dest_mbox);
14330                                 EM_SAFE_FREE(p[i].src_mbox);
14331                                 EM_SAFE_FREE(p[i].server_mailid);
14332                         }
14333
14334                         free(p);        *local_activity_list = NULL;
14335                 }
14336         }
14337
14338         ret = true;
14339
14340 FINISH_OFF:
14341         if (err_code != NULL)
14342                 *err_code = error;
14343
14344         EM_DEBUG_FUNC_END("ret [%d]", ret);
14345         return ret;
14346
14347 }
14348 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
14349
14350
14351 static int _get_key_value_string_for_list_filter_rule(email_list_filter_rule_t *input_list_filter_rule,char **output_key_value_string)
14352 {
14353         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_key_value_string [%p]", input_list_filter_rule, output_key_value_string);
14354
14355         int  ret = EMAIL_ERROR_NONE;
14356         char key_value_string[QUERY_SIZE] = { 0, };
14357         char *temp_key_value_1 = NULL;
14358         char *temp_key_value_2 = NULL;
14359
14360         if(input_list_filter_rule == NULL || output_key_value_string == NULL) {
14361                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14362                 return EMAIL_ERROR_INVALID_PARAM;
14363         }
14364
14365         switch(input_list_filter_rule->target_attribute) {
14366         case EMAIL_MAIL_ATTRIBUTE_MAIL_ID                 :
14367         case EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID              :
14368         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID              :
14369         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE            :
14370         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_STATUS      :
14371         case EMAIL_MAIL_ATTRIBUTE_REFERENCE_MAIL_ID       :
14372         case EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS    :
14373         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN         :
14374         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML          :
14375         case EMAIL_MAIL_ATTRIBUTE_FILE_SIZE               :
14376         case EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD        :
14377         case EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD     :
14378         case EMAIL_MAIL_ATTRIBUTE_FLAGS_FLAGGED_FIELD     :
14379         case EMAIL_MAIL_ATTRIBUTE_FLAGS_ANSWERED_FIELD    :
14380         case EMAIL_MAIL_ATTRIBUTE_FLAGS_RECENT_FIELD      :
14381         case EMAIL_MAIL_ATTRIBUTE_FLAGS_DRAFT_FIELD       :
14382         case EMAIL_MAIL_ATTRIBUTE_FLAGS_FORWARDED_FIELD   :
14383         case EMAIL_MAIL_ATTRIBUTE_DRM_STATUS              :
14384         case EMAIL_MAIL_ATTRIBUTE_PRIORITY                :
14385         case EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS             :
14386         case EMAIL_MAIL_ATTRIBUTE_LOCK_STATUS             :
14387         case EMAIL_MAIL_ATTRIBUTE_REPORT_STATUS           :
14388         case EMAIL_MAIL_ATTRIBUTE_ATTACHMENT_COUNT        :
14389         case EMAIL_MAIL_ATTRIBUTE_INLINE_CONTENT_COUNT    :
14390         case EMAIL_MAIL_ATTRIBUTE_THREAD_ID               :
14391         case EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT       :
14392         case EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS  :
14393         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_CLASS           :
14394         case EMAIL_MAIL_ATTRIBUTE_DIGEST_TYPE             :
14395         case EMAIL_MAIL_ATTRIBUTE_SMIME_TYPE              :
14396                 SNPRINTF(key_value_string, QUERY_SIZE, "%d", input_list_filter_rule->key_value.integer_type_value);
14397                 break;
14398
14399         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME            :
14400         case EMAIL_MAIL_ATTRIBUTE_SUBJECT                 :
14401         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME     :
14402         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID          :
14403         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID              :
14404         case EMAIL_MAIL_ATTRIBUTE_FROM                    :
14405         case EMAIL_MAIL_ATTRIBUTE_TO                      :
14406         case EMAIL_MAIL_ATTRIBUTE_CC                      :
14407         case EMAIL_MAIL_ATTRIBUTE_BCC                     :
14408         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT            :
14409                 if(input_list_filter_rule->key_value.string_type_value == NULL) {
14410                         EM_DEBUG_EXCEPTION("Invalid string_type_value [%p]", input_list_filter_rule->key_value.string_type_value);
14411                         ret = EMAIL_ERROR_INVALID_PARAM;
14412                         goto FINISH_OFF;
14413                 }
14414
14415                 temp_key_value_1 = input_list_filter_rule->key_value.string_type_value;
14416
14417                 temp_key_value_2 = em_replace_all_string(temp_key_value_1, "_", "\\_");
14418                 temp_key_value_1 = em_replace_all_string(temp_key_value_2, "%", "\\%");
14419
14420                 if(input_list_filter_rule->rule_type == EMAIL_LIST_FILTER_RULE_INCLUDE)
14421                         SNPRINTF(key_value_string, QUERY_SIZE, "\'%%%s%%\'", temp_key_value_1);
14422                 else
14423                         SNPRINTF(key_value_string, QUERY_SIZE, "\'%s\'", temp_key_value_1);
14424                 break;
14425
14426         case EMAIL_MAIL_ATTRIBUTE_DATE_TIME               :
14427                 SNPRINTF(key_value_string, QUERY_SIZE, "%d", (int)input_list_filter_rule->key_value.datetime_type_value);
14428                 break;
14429
14430         default :
14431                 ret = EMAIL_ERROR_INVALID_PARAM;
14432                 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
14433                 break;
14434         }
14435
14436         if(ret == EMAIL_ERROR_NONE && EM_SAFE_STRLEN(key_value_string) > 0) {
14437                 *output_key_value_string = strdup(key_value_string);
14438         }
14439
14440 FINISH_OFF:
14441
14442         EM_SAFE_FREE(temp_key_value_1);
14443         EM_SAFE_FREE(temp_key_value_2);
14444
14445         EM_DEBUG_FUNC_END("ret [%d]", ret);
14446         return ret;
14447 }
14448
14449 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14450 static int _get_cert_password_file_name(int index, char *cert_password_file_name)
14451 {
14452         EM_DEBUG_FUNC_BEGIN("index : [%d]", index);
14453
14454         if (index <= 0 || !cert_password_file_name) {
14455                 EM_DEBUG_EXCEPTION("Invalid parameter");
14456                 return EMAIL_ERROR_INVALID_PARAM;
14457         }
14458
14459         sprintf(cert_password_file_name, ".email_cert_%d", index);
14460
14461         EM_DEBUG_FUNC_END();
14462         return EMAIL_ERROR_NONE;
14463 }
14464 #endif
14465
14466 static int _make_filter_rule_string(email_list_filter_rule_t *input_list_filter_rule, char **output_string)
14467 {
14468         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
14469
14470         int   ret = EMAIL_ERROR_NONE;
14471         int   is_alpha = 0;
14472         int   length_field_name = 0;
14473         int   length_value = 0;
14474         char  result_rule_string[QUERY_SIZE] = { 0 , };
14475         char *mod_field_name_string = NULL;
14476         char *mod_value_string = NULL;
14477         char *temp_field_name_string = NULL;
14478         char *temp_key_value_string = NULL;
14479
14480         if(input_list_filter_rule == NULL || output_string == NULL) {
14481                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14482                 return  EMAIL_ERROR_INVALID_PARAM;
14483         }
14484
14485         temp_field_name_string = emcore_get_mail_field_name_by_attribute_type(input_list_filter_rule->target_attribute);
14486
14487         if(temp_field_name_string == NULL) {
14488                 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
14489                 return EMAIL_ERROR_INVALID_PARAM;
14490         }
14491
14492         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) {
14493                 EM_DEBUG_EXCEPTION("_get_key_value_string_for_list_filter_rule failed");
14494                 return EMAIL_ERROR_INVALID_PARAM;
14495         }
14496
14497         length_field_name = EM_SAFE_STRLEN(temp_field_name_string);
14498         length_value      = EM_SAFE_STRLEN(temp_key_value_string);
14499
14500         switch(input_list_filter_rule->target_attribute) {
14501         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME            :
14502         case EMAIL_MAIL_ATTRIBUTE_SUBJECT                 :
14503         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME     :
14504         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID          :
14505         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID              :
14506         case EMAIL_MAIL_ATTRIBUTE_FROM                    :
14507         case EMAIL_MAIL_ATTRIBUTE_TO                      :
14508         case EMAIL_MAIL_ATTRIBUTE_CC                      :
14509         case EMAIL_MAIL_ATTRIBUTE_BCC                     :
14510         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT            :
14511                 is_alpha = 1;
14512                 break;
14513         default :
14514                 is_alpha = 0;
14515                 break;
14516         }
14517
14518         if(is_alpha == 1 && input_list_filter_rule->case_sensitivity == false) {
14519                 length_field_name += strlen("UPPER() ");
14520                 length_value      += strlen("UPPER() ");
14521                 mod_field_name_string = em_malloc(sizeof(char) * length_field_name);
14522                 mod_value_string      = em_malloc(sizeof(char) * length_value);
14523                 SNPRINTF(mod_field_name_string, length_field_name, "UPPER(%s)", temp_field_name_string);
14524                 SNPRINTF(mod_value_string,      length_value, "UPPER(%s)", temp_key_value_string);
14525                 EM_SAFE_FREE(temp_key_value_string);
14526         }
14527         else {
14528                 mod_field_name_string = strdup(temp_field_name_string);
14529                 mod_value_string      = temp_key_value_string;
14530         }
14531
14532         switch (input_list_filter_rule->rule_type) {
14533         case EMAIL_LIST_FILTER_RULE_EQUAL     :
14534                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = %s ", mod_field_name_string, mod_value_string);
14535                 break;
14536         case EMAIL_LIST_FILTER_RULE_NOT_EQUAL :
14537                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s != %s ", mod_field_name_string, mod_value_string);
14538                 break;
14539         case EMAIL_LIST_FILTER_RULE_LESS_THAN :
14540                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s < %s ", mod_field_name_string, mod_value_string);
14541                 break;
14542         case EMAIL_LIST_FILTER_RULE_GREATER_THAN :
14543                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s > %s ", mod_field_name_string, mod_value_string);
14544                 break;
14545         case EMAIL_LIST_FILTER_RULE_LESS_THAN_OR_EQUAL :
14546                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s <= %s ", mod_field_name_string, mod_value_string);
14547                 break;
14548         case EMAIL_LIST_FILTER_RULE_GREATER_THAN_OR_EQUAL :
14549                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s >= %s ", mod_field_name_string, mod_value_string);
14550                 break;
14551         case EMAIL_LIST_FILTER_RULE_INCLUDE   :
14552                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s LIKE %s ", mod_field_name_string, mod_value_string);
14553                 break;
14554         case EMAIL_LIST_FILTER_RULE_IN        :
14555                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s IN (%s) ", mod_field_name_string, mod_value_string);
14556                 break;
14557         case EMAIL_LIST_FILTER_RULE_NOT_IN    :
14558                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s NOT IN (%s) ", mod_field_name_string, mod_value_string);
14559                 break;
14560         default :
14561                 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
14562                 ret = EMAIL_ERROR_INVALID_PARAM;
14563                 goto FINISH_OFF;
14564         }
14565
14566         *output_string = strdup(result_rule_string);
14567
14568 FINISH_OFF:
14569         EM_SAFE_FREE(mod_field_name_string);
14570         EM_SAFE_FREE(mod_value_string);
14571
14572         EM_DEBUG_FUNC_END("ret [%d]", ret);
14573         return ret;
14574 }
14575
14576 static int _make_filter_fts_rule_string(email_list_filter_rule_fts_t *input_list_filter_rule, char **output_string)
14577 {
14578         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
14579
14580         int   ret = EMAIL_ERROR_NONE;
14581         char *field_name_string = NULL;
14582         char key_value_string[QUERY_SIZE] = {0,};
14583         char  result_rule_string[QUERY_SIZE] = {0,};
14584
14585         int rc = -1;
14586         int count = 0;
14587         int col_index = 0;
14588         int error = EMAIL_ERROR_NONE;
14589         char **result = NULL;
14590         char sql_query_string[QUERY_SIZE] = {0,};
14591         char sql_query_string2[QUERY_SIZE] = {0,};
14592         sqlite3 *local_db_handle = NULL;
14593
14594         if(input_list_filter_rule == NULL || output_string == NULL) {
14595                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14596                 return  EMAIL_ERROR_INVALID_PARAM;
14597         }
14598
14599         field_name_string = EM_SAFE_STRDUP("body_text");
14600         SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
14601
14602         switch (input_list_filter_rule->rule_type) {
14603
14604         case EMAIL_LIST_FILTER_RULE_INCLUDE:
14605                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
14606                 break;
14607
14608         case EMAIL_LIST_FILTER_RULE_MATCH:
14609                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
14610                 break;
14611
14612         default :
14613                 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
14614                 ret = EMAIL_ERROR_INVALID_PARAM;
14615                 goto FINISH_OFF;
14616         }
14617
14618         local_db_handle = emstorage_get_db_connection();
14619         EMSTORAGE_START_READ_TRANSACTION(true);
14620         SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_text_tbl %s", result_rule_string);
14621
14622         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14623         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF;},
14624                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14625
14626         col_index = 1;
14627
14628         if (!count) {
14629                 EM_DEBUG_EXCEPTION("No mail found...");
14630                 ret = true;
14631                 error= EMAIL_ERROR_MAIL_NOT_FOUND;
14632                 *output_string = strdup("mail_id IN ( ) ");
14633                 goto FINISH_OFF;
14634         }
14635
14636         EM_DEBUG_LOG(">>>> DATA ASSIGN START >>");
14637         int i = 0;
14638         int *mail_ids = NULL;
14639         if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
14640                 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
14641                 error = EMAIL_ERROR_OUT_OF_MEMORY;
14642                 goto FINISH_OFF;
14643         }
14644
14645         for (i = 0; i < count; i++) {
14646                 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
14647                 EM_DEBUG_LOG(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
14648         }
14649
14650         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >>", count);
14651         EM_PROFILE_END(emstorage_query_mail_list_loop);
14652         sqlite3_free_table(result);
14653         EMSTORAGE_FINISH_READ_TRANSACTION(true);
14654
14655         sqlite3_release_memory(-1);
14656
14657         _DISCONNECT_DB;
14658
14659         int cur_query = 0;
14660         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, QUERY_SIZE, "mail_id IN ( ");
14661         for (i = 0; i < count-1; i++) {
14662                 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, QUERY_SIZE, "%d, ", mail_ids[i]);
14663         }
14664         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, QUERY_SIZE, "%d ) ", mail_ids[count-1]);
14665
14666         *output_string = strdup(sql_query_string2);
14667
14668 FINISH_OFF:
14669         EM_SAFE_FREE(field_name_string);
14670         EM_DEBUG_FUNC_END("ret [%d]", ret);
14671         return ret;
14672 }
14673
14674 static int _make_order_rule_string(email_list_sorting_rule_t *input_sorting_rule, char **output_string) {
14675         EM_DEBUG_FUNC_BEGIN("input_sorting_rule [%p], output_string [%p]", input_sorting_rule, output_string);
14676
14677         char  result_rule_string[QUERY_SIZE] = { 0 , };
14678         int   ret = EMAIL_ERROR_NONE;
14679
14680         if(input_sorting_rule->force_boolean_check) {
14681                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = 0 ", emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
14682         }
14683         else
14684                 EM_SAFE_STRCPY(result_rule_string, emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
14685
14686         switch (input_sorting_rule->sort_order) {
14687                 case EMAIL_SORT_ORDER_ASCEND     :
14688                         EM_SAFE_STRCAT(result_rule_string, " ASC ");
14689                         break;
14690
14691                 case EMAIL_SORT_ORDER_DESCEND    :
14692                         EM_SAFE_STRCAT(result_rule_string, " DESC ");
14693                         break;
14694
14695                 default :
14696                         EM_DEBUG_EXCEPTION("Invalid sort_order [%d]", input_sorting_rule->sort_order);
14697                         ret = EMAIL_ERROR_INVALID_PARAM;
14698                         goto FINISH_OFF;
14699         }
14700
14701         *output_string = strdup(result_rule_string);
14702
14703 FINISH_OFF:
14704         EM_DEBUG_FUNC_END("ret [%d]", ret);
14705         return ret;
14706 }
14707
14708 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)
14709 {
14710         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);
14711         int ret = EMAIL_ERROR_NONE;
14712         int i = 0;
14713         int string_offset = 0;
14714         char conditional_clause_string[QUERY_SIZE] = {0, };
14715         char *result_string_for_a_item = NULL;
14716
14717         if ( (input_filter_count > 0 && !input_filter_list) || (input_sorting_rule_count > 0 && !input_sorting_rule_list) || output_conditional_clause == NULL) {
14718                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14719                 return EMAIL_ERROR_INVALID_PARAM;
14720         }
14721
14722         if(input_filter_count > 0) {
14723                 strcpy(conditional_clause_string,  " WHERE ");
14724
14725                 for ( i = 0; i < input_filter_count; i++) {
14726                         switch (input_filter_list[i].list_filter_item_type) {
14727                         case EMAIL_LIST_FILTER_ITEM_RULE :
14728                                 EM_DEBUG_LOG("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE", i);
14729                                 _make_filter_rule_string(&(input_filter_list[i].list_filter_item.rule), &result_string_for_a_item);
14730                                 break;
14731
14732                         case EMAIL_LIST_FILTER_ITEM_RULE_FTS :
14733                                 EM_DEBUG_LOG("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_FTS", i);
14734                                 _make_filter_fts_rule_string(&(input_filter_list[i].list_filter_item.rule_fts), &result_string_for_a_item);
14735                                 break;
14736
14737                         case EMAIL_LIST_FILTER_ITEM_OPERATOR :
14738                                 EM_DEBUG_LOG("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_OPERATOR", i);
14739                                 switch(input_filter_list[i].list_filter_item.operator_type) {
14740                                 case EMAIL_LIST_FILTER_OPERATOR_AND :
14741                                         result_string_for_a_item = strdup("AND ");
14742                                         break;
14743                                 case EMAIL_LIST_FILTER_OPERATOR_OR :
14744                                         result_string_for_a_item = strdup("OR ");
14745                                         break;
14746                                 case EMAIL_LIST_FILTER_OPERATOR_LEFT_PARENTHESIS :
14747                                         result_string_for_a_item = strdup(" (");
14748                                         break;
14749                                 case EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS :
14750                                         result_string_for_a_item = strdup(") ");
14751                                         break;
14752                                 }
14753                                 break;
14754
14755                         default :
14756                                 EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", input_filter_list[i].list_filter_item_type);
14757                                 ret = EMAIL_ERROR_INVALID_PARAM;
14758                                 goto FINISH_OFF;
14759                         }
14760
14761                         if(result_string_for_a_item == NULL) {
14762                                 EM_DEBUG_EXCEPTION("result_string_for_a_item is null");
14763                                 ret = EMAIL_ERROR_INVALID_PARAM;
14764                                 goto FINISH_OFF;
14765                         }
14766
14767                         if(strlen(conditional_clause_string) + EM_SAFE_STRLEN(result_string_for_a_item) >= QUERY_SIZE) { /* prevent 34364 */
14768                                 EM_DEBUG_EXCEPTION("Query is too long");
14769                                 ret = EMAIL_ERROR_DATA_TOO_LONG;
14770                                 goto FINISH_OFF;
14771                         }
14772                         strcat(conditional_clause_string, result_string_for_a_item);
14773                         EM_SAFE_FREE(result_string_for_a_item);
14774                 }
14775         }
14776
14777         if(input_sorting_rule_count > 0) {
14778                 strcat(conditional_clause_string, "ORDER BY ");
14779
14780                 for ( i = 0; i < input_sorting_rule_count; i++) {
14781                         if( (ret = _make_order_rule_string(&input_sorting_rule_list[i], &result_string_for_a_item)) != EMAIL_ERROR_NONE) {
14782                                 EM_DEBUG_EXCEPTION("_make_order_rule_string failed. [%d]", ret);
14783                                 goto FINISH_OFF;
14784                         }
14785                         if(i > 0)
14786                                 strcat(conditional_clause_string, ", ");
14787                         strcat(conditional_clause_string, result_string_for_a_item);
14788                         EM_SAFE_FREE(result_string_for_a_item);
14789                 }
14790         }
14791
14792         if (input_start_index != -1 && input_limit_count != -1) {
14793                 string_offset = strlen(conditional_clause_string);
14794                 SNPRINTF_OFFSET(conditional_clause_string, string_offset, QUERY_SIZE, " LIMIT %d, %d", input_start_index, input_limit_count);
14795         }
14796
14797         *output_conditional_clause = strdup(conditional_clause_string);
14798
14799 FINISH_OFF:
14800         EM_SAFE_FREE(result_string_for_a_item);
14801
14802         EM_DEBUG_FUNC_END("ret [%d]", ret);
14803         return ret;
14804 }
14805
14806 INTERNAL_FUNC int emstorage_free_list_filter(email_list_filter_t **input_filter_list, int input_filter_count)
14807 {
14808         EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d]", input_filter_list, input_filter_count);
14809         int err = EMAIL_ERROR_NONE;
14810         int i = 0;
14811         email_list_filter_t *temp_filter_list = NULL;
14812
14813         EM_IF_NULL_RETURN_VALUE(input_filter_list, EMAIL_ERROR_INVALID_PARAM);
14814
14815         for ( i = 0; i < input_filter_count; i++) {
14816                 temp_filter_list = (*input_filter_list) + i;
14817                 if(temp_filter_list && temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE) {
14818                         switch(temp_filter_list->list_filter_item.rule.target_attribute) {
14819                         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME :
14820                         case EMAIL_MAIL_ATTRIBUTE_SUBJECT :
14821                         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME :
14822                         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID :
14823                         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID :
14824                         case EMAIL_MAIL_ATTRIBUTE_FROM :
14825                         case EMAIL_MAIL_ATTRIBUTE_TO :
14826                         case EMAIL_MAIL_ATTRIBUTE_CC :
14827                         case EMAIL_MAIL_ATTRIBUTE_BCC :
14828                         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN :
14829                         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML :
14830                         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT :
14831                                 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule.key_value.string_type_value);
14832                                 break;
14833                         default :
14834                                 break;
14835                         }
14836                 }
14837         }
14838
14839         free(*input_filter_list);
14840         *input_filter_list = NULL;
14841
14842         EM_DEBUG_FUNC_END("err [%d]", err);
14843         return err;
14844 }
14845
14846 INTERNAL_FUNC int emstorage_add_certificate(emstorage_certificate_tbl_t *certificate, int transaction, int *err_code)
14847 {
14848         EM_DEBUG_FUNC_BEGIN("certificate:[%p], transaction:[%d], err_code:[%p]", certificate, transaction, err_code);
14849
14850         if (!certificate) {
14851                 EM_DEBUG_EXCEPTION("certificate:[%p], transaction:[%d], err_code:[%p]", certificate, transaction, err_code);
14852                 if (err_code != NULL)
14853                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14854
14855                 return false;
14856         }
14857
14858         int rc = -1, ret = false;
14859         int error = EMAIL_ERROR_NONE;
14860         DB_STMT hStmt = NULL;
14861         char sql_query_string[QUERY_SIZE] = {0, };
14862 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14863         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
14864 #endif
14865         sqlite3 *local_db_handle = emstorage_get_db_connection();
14866
14867         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
14868
14869         char *sql = "SELECT max(rowid) FROM mail_certificate_tbl;";
14870         char **result = NULL;
14871
14872         /*  rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
14873         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
14874         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14875                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
14876
14877         if (NULL==result[1]) rc = 1;
14878         else rc = atoi(result[1])+1;
14879         sqlite3_free_table(result);
14880
14881         certificate->certificate_id = rc;
14882 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14883         if ((error = _get_cert_password_file_name(certificate->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
14884                 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
14885                 goto FINISH_OFF;
14886         }
14887 #endif
14888         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14889                 "INSERT INTO mail_certificate_tbl VALUES "
14890                 "(        "
14891                 "   ?  "                /* Index of certificate */
14892                 "  , ? "                /* Select the account */
14893                 "  , ? "                /* Year of issue */
14894                 "  , ? "                /* Month of issue */
14895                 "  , ? "                /* Day of issue */
14896                 "  , ? "                /* Year of expiration */
14897                 "  , ? "                /* Month of expiration */
14898                 "  , ? "                /* Day of expiration */
14899                 "  , ? "                /* Organization of issue */
14900                 "  , ? "                /* Email address */
14901                 "  , ? "                /* Subject of certificate */
14902                 "  , ? "                /* Name of saved certificate */
14903                 ") ");
14904
14905
14906         /*  rc = sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL); */
14907         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14908         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14909                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14910
14911         EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
14912         _bind_stmt_field_data_int(hStmt, CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->certificate_id);
14913         _bind_stmt_field_data_int(hStmt, ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_year);
14914         _bind_stmt_field_data_int(hStmt, ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_month);
14915         _bind_stmt_field_data_int(hStmt, ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_day);
14916         _bind_stmt_field_data_int(hStmt, EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_year);
14917         _bind_stmt_field_data_int(hStmt, EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_month);
14918         _bind_stmt_field_data_int(hStmt, EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_day);
14919         _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);
14920         _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->email_address, 0, EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL);
14921         _bind_stmt_field_data_string(hStmt, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->subject_str, 0, SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL);
14922         _bind_stmt_field_data_string(hStmt, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->filepath, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
14923         /*  rc = sqlite3_step(hStmt); */
14924         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14925
14926         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
14927                 ("sqlite3_step fail:%d", rc));
14928         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14929                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
14930 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14931         if (ssm_write_buffer(certificate->password, EM_SAFE_STRLEN(certificate->password), cert_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
14932                 EM_DEBUG_EXCEPTION("ssm_write_buffer failed - Private certificate password : [%s]", cert_password_file_name);
14933                 error = EMAIL_ERROR_SYSTEM_FAILURE;
14934                 goto FINISH_OFF;
14935         }
14936 #endif
14937         ret = true;
14938
14939         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
14940
14941         if (!emcore_notify_storage_event(NOTI_CERTIFICATE_ADD, certificate->certificate_id, 0, NULL, 0))
14942                 EM_DEBUG_EXCEPTION("emcore_notify_storage_event(NOTI_CERTIFICATE_ADD] : Notification failed");
14943
14944 FINISH_OFF:
14945
14946         if (hStmt != NULL)  {
14947                 rc = sqlite3_finalize(hStmt);
14948                 if (rc != SQLITE_OK)  {
14949                         EM_DEBUG_LOG("sqlite3_finalize failed - %d", rc);
14950                         error = EMAIL_ERROR_DB_FAILURE;
14951                 }
14952         }
14953         else
14954                 EM_DEBUG_LOG("hStmt is NULL!!!");
14955
14956         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
14957         _DISCONNECT_DB;
14958
14959         if (err_code != NULL)
14960                 *err_code = error;
14961
14962         EM_DEBUG_FUNC_END("ret [%d]", ret);
14963         return ret;
14964 }
14965
14966 INTERNAL_FUNC int emstorage_free_certificate(emstorage_certificate_tbl_t **certificate_list, int count, int *err_code)
14967 {
14968         EM_DEBUG_FUNC_BEGIN("certificate_list[%p], count[%d], err_code[%p]", certificate_list, count, err_code);
14969
14970         int ret = false;
14971         int error = EMAIL_ERROR_NONE;
14972
14973         if (count > 0)  {
14974                 if (!certificate_list || !*certificate_list)  {
14975                         EM_DEBUG_EXCEPTION("certificate_list[%p], count[%d]", certificate_list, count);
14976                         error = EMAIL_ERROR_INVALID_PARAM;
14977                         goto FINISH_OFF;
14978                 }
14979
14980                 emstorage_certificate_tbl_t *p = *certificate_list;
14981                 int i = 0;
14982
14983                 for (; i < count; i++)  {
14984                         EM_SAFE_FREE(p[i].issue_organization_name);
14985                         EM_SAFE_FREE(p[i].email_address);
14986                         EM_SAFE_FREE(p[i].subject_str);
14987                         EM_SAFE_FREE(p[i].filepath);
14988                         EM_SAFE_FREE(p[i].password);
14989                 }
14990
14991                 EM_SAFE_FREE(p);
14992                 *certificate_list = NULL;
14993         }
14994
14995         ret = true;
14996
14997 FINISH_OFF:
14998         if (err_code != NULL)
14999                 *err_code = error;
15000
15001         EM_DEBUG_FUNC_END("ret [%d]", ret);
15002         return ret;
15003 }
15004
15005 INTERNAL_FUNC int emstorage_get_certificate_list(int *select_num, emstorage_certificate_tbl_t **certificate_list, int transaction, int with_password, int *err_code)
15006 {
15007         EM_DEBUG_FUNC_BEGIN();
15008
15009         int i = 0, count = 0, rc = -1, ret = false;
15010         int error = EMAIL_ERROR_NONE;
15011         emstorage_certificate_tbl_t *p_data_tbl = NULL;
15012
15013         DB_STMT hStmt = NULL;
15014
15015         if (!select_num || !certificate_list)  {
15016                 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, certificate_list);
15017                 if (err_code != NULL)
15018                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15019                 return false;
15020         }
15021
15022         sqlite3 *local_db_handle = emstorage_get_db_connection();
15023         EMSTORAGE_START_READ_TRANSACTION(transaction);
15024
15025         char sql_query_string[QUERY_SIZE] = {0, };
15026         char *sql = "SELECT count(*) FROM mail_certificate_tbl;";
15027         char **result;
15028
15029         /*  rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
15030         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
15031         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
15032                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
15033
15034         count = atoi(result[1]);
15035         sqlite3_free_table(result);
15036
15037         if (!count) {
15038                 EM_DEBUG_EXCEPTION("no account found...");
15039                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
15040                 ret = true;
15041                 goto FINISH_OFF;
15042         }
15043         EM_DEBUG_LOG("count = %d", rc);
15044         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_certificate_tbl ORDER BY account_id");
15045
15046         /*  rc = sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL);   */
15047         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15048
15049         EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
15050         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15051                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15052
15053         /*  rc = sqlite3_step(hStmt); */
15054         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15055         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15056                 ("sqlite3_step fail:%d", rc));
15057         if (rc == SQLITE_DONE)  {
15058                 EM_DEBUG_EXCEPTION("no account found...");
15059
15060                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
15061                 count = 0;
15062                 ret = true;
15063                 goto FINISH_OFF;
15064         }
15065
15066         if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t) * count)))  {
15067                 EM_DEBUG_EXCEPTION("malloc failed...");
15068                 error = EMAIL_ERROR_OUT_OF_MEMORY;
15069                 goto FINISH_OFF;
15070         }
15071         memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t) * count);
15072         for (i = 0; i < count; i++)  {
15073                 /*  get recordset */
15074                 _get_stmt_field_data_int(hStmt,  &(p_data_tbl[i].certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
15075                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
15076                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
15077                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
15078                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
15079                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
15080                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
15081                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
15082                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
15083                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
15084                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
15085                 if (with_password == true) {
15086 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15087                         /*  get password from the secure storage */
15088                         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
15089
15090                         EM_SAFE_FREE(p_data_tbl[i].password);
15091
15092                         /*  get password file name */
15093                         if ((error = _get_cert_password_file_name(p_data_tbl[i].certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
15094                                 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
15095                                 goto FINISH_OFF;
15096                         }
15097
15098                         /*  read password from secure storage */
15099                         if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl[i].password))) < 0) {
15100                                 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage() failed...");
15101                                 goto FINISH_OFF;
15102                         }
15103                         EM_DEBUG_LOG("recv_password_file_name[%s], password[%s]", cert_password_file_name,  p_data_tbl[i].password);
15104 #endif
15105                 }
15106
15107                 /*  rc = sqlite3_step(hStmt); */
15108                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15109                 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i,  rc);
15110                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15111                         ("sqlite3_step fail:%d", rc));
15112         }
15113
15114         ret = true;
15115
15116 FINISH_OFF:
15117         if (ret == true)  {
15118                 *certificate_list = p_data_tbl;
15119                 *select_num = count;
15120                 EM_DEBUG_LOG("COUNT : %d", count);
15121         }
15122         else if (p_data_tbl != NULL)
15123                 emstorage_free_certificate(&p_data_tbl, count, NULL);
15124         if (hStmt != NULL)  {
15125                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
15126
15127                 rc = sqlite3_finalize(hStmt);
15128                 hStmt = NULL;
15129                 if (rc != SQLITE_OK)  {
15130                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
15131                         error = EMAIL_ERROR_DB_FAILURE;
15132                 }
15133         }
15134
15135         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15136         _DISCONNECT_DB;
15137
15138         if (err_code != NULL)
15139                 *err_code = error;
15140
15141         EM_DEBUG_FUNC_END("ret [%d]", ret);
15142         return ret;
15143 }
15144
15145 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)
15146 {
15147         EM_DEBUG_FUNC_BEGIN("email_address[%s], certificate[%p], transaction[%d], err_code[%p]", email_address, certificate, transaction, err_code);
15148
15149         if (!certificate)  {
15150                 EM_DEBUG_EXCEPTION("email_address[%s], certificate[%p]", email_address, certificate);
15151                 if (err_code != NULL)
15152                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15153                 return false;
15154         }
15155
15156         int ret = false;
15157         int error = EMAIL_ERROR_NONE;
15158         emstorage_certificate_tbl_t *p_data_tbl = NULL;
15159         DB_STMT hStmt = NULL;
15160         char sql_query_string[QUERY_SIZE] = {0, };
15161         int rc = -1;
15162         int sql_len = 0;
15163 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15164         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
15165 #endif
15166         sqlite3 *local_db_handle = emstorage_get_db_connection();
15167         EMSTORAGE_START_READ_TRANSACTION(transaction);
15168
15169         /*  Make query string */
15170         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
15171         sql_len = EM_SAFE_STRLEN(sql_query_string);
15172
15173         /*  dummy value, FROM WHERE clause */
15174         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "* FROM mail_certificate_tbl WHERE email_address = '%s'", email_address);
15175
15176         /*  FROM clause */
15177         EM_DEBUG_LOG("query = [%s]", sql_query_string);
15178
15179         /*  execute a sql and count rows */
15180         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15181         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15182                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15183
15184         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15185         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15186                 ("sqlite3_step fail:%d", rc));
15187
15188         if (rc == SQLITE_DONE)  {
15189                 EM_DEBUG_EXCEPTION("no matched certificate found...");
15190                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
15191                 goto FINISH_OFF;
15192         }
15193
15194         /*  Assign query result to structure */
15195         if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t))))  {
15196                 EM_DEBUG_EXCEPTION("malloc failed...");
15197                 error = EMAIL_ERROR_OUT_OF_MEMORY;
15198                 goto FINISH_OFF;
15199         }
15200
15201         memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t));
15202         _get_stmt_field_data_int(hStmt,  &(p_data_tbl->certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
15203         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
15204         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
15205         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
15206         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
15207         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
15208         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
15209         _get_stmt_field_data_string(hStmt, &(p_data_tbl->issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
15210         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
15211         _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
15212         _get_stmt_field_data_string(hStmt, &(p_data_tbl->filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
15213
15214         if (with_password) {
15215 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15216                 /*  get password file name */
15217                 if ((error = _get_cert_password_file_name(p_data_tbl->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
15218                         EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
15219                         goto FINISH_OFF;
15220                 }
15221
15222                 /*  read password from secure storage */
15223                 if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl->password))) != EMAIL_ERROR_NONE) {
15224                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed...");
15225                         goto FINISH_OFF;
15226                 }
15227                 EM_DEBUG_LOG("cert_password_file_name[%s], password[%s]", cert_password_file_name,  p_data_tbl->password);
15228 #endif
15229         }
15230         ret = true;
15231
15232 FINISH_OFF:
15233         if (ret == true)
15234                 *certificate = p_data_tbl;
15235
15236         if (hStmt != NULL)  {
15237                 rc = sqlite3_finalize(hStmt);
15238                 if (rc != SQLITE_OK)  {
15239                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
15240                         error = EMAIL_ERROR_DB_FAILURE;
15241                 }
15242         }
15243
15244         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15245         _DISCONNECT_DB;
15246
15247         if (err_code != NULL)
15248                 *err_code = error;
15249
15250         EM_DEBUG_FUNC_END("ret [%d]", ret);
15251         return ret;
15252 }
15253
15254 INTERNAL_FUNC int emstorage_get_certificate_by_index(int index, emstorage_certificate_tbl_t **certificate, int transaction, int with_password, int *err_code)
15255 {
15256         EM_DEBUG_FUNC_BEGIN("index[%d], certificate[%p], transaction[%d], err_code[%p]", index, certificate, transaction, err_code);
15257
15258         if (!certificate)  {
15259                 EM_DEBUG_EXCEPTION("index[%d], account[%p]", index, certificate);
15260                 if (err_code != NULL)
15261                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15262                 return false;
15263         }
15264
15265         int ret = false;
15266         int error = EMAIL_ERROR_NONE;
15267         emstorage_certificate_tbl_t *p_data_tbl = NULL;
15268         DB_STMT hStmt = NULL;
15269         char sql_query_string[QUERY_SIZE] = {0, };
15270         int rc = -1;
15271         int sql_len = 0;
15272 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15273         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
15274 #endif
15275         sqlite3 *local_db_handle = emstorage_get_db_connection();
15276         EMSTORAGE_START_READ_TRANSACTION(transaction);
15277
15278         /*  Make query string */
15279         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
15280         sql_len = EM_SAFE_STRLEN(sql_query_string);
15281
15282         /*  dummy value, FROM WHERE clause */
15283         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "* FROM mail_certificate_tbl WHERE certificate_id = %d", index);
15284
15285         /*  FROM clause */
15286         EM_DEBUG_LOG("query = [%s]", sql_query_string);
15287
15288         /*  execute a sql and count rows */
15289         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15290         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15291                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15292
15293         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15294         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15295                 ("sqlite3_step fail:%d", rc));
15296
15297         if (rc == SQLITE_DONE)  {
15298                 EM_DEBUG_EXCEPTION("no matched certificate found...");
15299                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
15300                 goto FINISH_OFF;
15301         }
15302
15303         /*  Assign query result to structure */
15304         if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t))))  {
15305                 EM_DEBUG_EXCEPTION("malloc failed...");
15306                 error = EMAIL_ERROR_OUT_OF_MEMORY;
15307                 goto FINISH_OFF;
15308         }
15309
15310         memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t));
15311
15312         _get_stmt_field_data_int(hStmt,  &(p_data_tbl->certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
15313         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
15314         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
15315         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
15316         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
15317         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
15318         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
15319         _get_stmt_field_data_string(hStmt, &(p_data_tbl->issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
15320         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
15321         _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
15322         _get_stmt_field_data_string(hStmt, &(p_data_tbl->filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
15323
15324         if (with_password) {
15325 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15326                 /*  get password file name */
15327                 if ((error = _get_cert_password_file_name(p_data_tbl->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
15328                         EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
15329                         goto FINISH_OFF;
15330                 }
15331
15332                 /*  read password from secure storage */
15333                 if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl->password))) != EMAIL_ERROR_NONE) {
15334                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed...");
15335                         goto FINISH_OFF;
15336                 }
15337                 EM_DEBUG_LOG("cert_password_file_name[%s], password[%s]", cert_password_file_name,  p_data_tbl->password);
15338 #endif
15339         }
15340         ret = true;
15341
15342 FINISH_OFF:
15343         if (ret == true)
15344                 *certificate = p_data_tbl;
15345
15346         if (hStmt != NULL)  {
15347                 rc = sqlite3_finalize(hStmt);
15348                 if (rc != SQLITE_OK)  {
15349                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
15350                         error = EMAIL_ERROR_DB_FAILURE;
15351                 }
15352         }
15353
15354         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15355         _DISCONNECT_DB;
15356
15357         if (err_code != NULL)
15358                 *err_code = error;
15359
15360         EM_DEBUG_FUNC_END("ret [%d]", ret);
15361         return ret;
15362 }
15363
15364 INTERNAL_FUNC int emstorage_delete_certificate(int certificate_id, int transaction, int *err_code)
15365 {
15366         EM_DEBUG_FUNC_BEGIN("certificate_id[%d], transaction[%d], err_code[%p]", certificate_id, transaction, err_code);
15367
15368         if (certificate_id < 1)  {
15369                 EM_DEBUG_EXCEPTION(" certificate_id[%d]", certificate_id);
15370
15371                 if (err_code != NULL)
15372                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15373                 return false;
15374         }
15375
15376         int rc = -1, ret = false;
15377         int error = EMAIL_ERROR_NONE;
15378         sqlite3 *local_db_handle = emstorage_get_db_connection();
15379         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
15380
15381         /*  TODO : delete password files - file names can be obtained from db or a rule that makes a name */
15382         DB_STMT hStmt = NULL;
15383         char sql_query_string[QUERY_SIZE] = {0, };
15384 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15385         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
15386         /*  get password file name */
15387         if ((error = _get_cert_password_file_name(certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
15388                 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
15389                 goto FINISH_OFF;
15390         }
15391 #endif
15392         /*  delete from db */
15393         memset(sql_query_string, 0x00, sizeof(sql_query_string));
15394         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_certificate_tbl WHERE certificate_id = %d", certificate_id);
15395
15396         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
15397         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
15398                 ("sqlite3_exec fail:%d", rc));
15399         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15400                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15401
15402         /*  validate account existence */
15403         rc = sqlite3_changes(local_db_handle);
15404         if (rc == 0)  {
15405                 EM_DEBUG_EXCEPTION(" no matched certificate found...");
15406                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
15407                 goto FINISH_OFF;
15408         }
15409 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15410         /*  delete from secure storage */
15411         if (ssm_delete_file(cert_password_file_name,  SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
15412                 EM_DEBUG_EXCEPTION(" ssm_delete_file failed -cert password : file[%s]", cert_password_file_name);
15413                 error = EMAIL_ERROR_SYSTEM_FAILURE;
15414                 goto FINISH_OFF;
15415         }
15416 #endif
15417         ret = true;
15418
15419 FINISH_OFF:
15420
15421         if (hStmt != NULL)  {
15422                 rc = sqlite3_finalize(hStmt);
15423                 if (rc != SQLITE_OK)  {
15424                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
15425                         error = EMAIL_ERROR_DB_FAILURE;
15426                 }
15427         }
15428
15429         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
15430         _DISCONNECT_DB;
15431
15432         if (err_code != NULL)
15433                 *err_code = error;
15434
15435         EM_DEBUG_FUNC_END("ret [%d]", ret);
15436         return ret;
15437 }
15438
15439 INTERNAL_FUNC int emstorage_update_certificate(int certificate_id, emstorage_certificate_tbl_t *certificate, int transaction, int *err_code)
15440 {
15441         EM_DEBUG_FUNC_BEGIN("certificate_id[%d], certificate[%p], transaction[%d], err_code[%p]", certificate_id, certificate, transaction, err_code);
15442
15443         if (certificate_id < 1)  {
15444                 EM_DEBUG_EXCEPTION(" certificate_id[%d]", certificate_id);
15445
15446                 if (err_code != NULL)
15447                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15448                 return false;
15449         }
15450
15451         int error = EMAIL_ERROR_NONE;
15452         int rc, ret = false;
15453
15454         DB_STMT hStmt = NULL;
15455         char sql_query_string[QUERY_SIZE] = {0, };
15456 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15457         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
15458 #endif
15459         sqlite3 *local_db_handle = emstorage_get_db_connection();
15460         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
15461
15462         SNPRINTF(sql_query_string, sizeof(sql_query_string),
15463                 "UPDATE mail_certificate_tbl SET"
15464                 " issue_year = ?"
15465                 ", issue_month = ?"             /* Index of certificate */
15466                 ", issue_day = ?"               /* Select the account */
15467                 ", expiration_year = ?"         /* Year of issue */
15468                 ", expiration_month = ?"                /* Month of issue */
15469                 ", expiration_day = ?"          /* Day of issue */
15470                 ", issue_organization_name = ?"         /* Year of expiration */
15471                 ", email_address = ?"           /* Month of expiration */
15472                 ", subject_str = ?"             /* Day of expiration */
15473                 ", filepath = ?"                /* Organization of issue */
15474                 ", password = ?"
15475                 " WHERE certificate_id = ?");
15476
15477         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15478         EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
15479         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15480                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15481
15482         _bind_stmt_field_data_int(hStmt, ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_year);
15483         _bind_stmt_field_data_int(hStmt, ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_month);
15484         _bind_stmt_field_data_int(hStmt, ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_day);
15485         _bind_stmt_field_data_int(hStmt, EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_year);
15486         _bind_stmt_field_data_int(hStmt, EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_month);
15487         _bind_stmt_field_data_int(hStmt, EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_day);
15488         _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);
15489         _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->email_address, 0, EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL);
15490         _bind_stmt_field_data_string(hStmt, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->subject_str, 0, SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL);
15491         _bind_stmt_field_data_string(hStmt, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->filepath, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
15492
15493         /*  rc = sqlite3_step(hStmt); */
15494         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15495         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
15496                 ("sqlite3_step fail:%d", rc));
15497         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15498                 ("sqlite3_step fail:%d", rc));
15499
15500         /*  validate account existence */
15501         rc = sqlite3_changes(local_db_handle);
15502         if (rc == 0) {
15503                 EM_DEBUG_EXCEPTION(" no matched account found...");
15504
15505                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
15506                 goto FINISH_OFF;
15507         }
15508
15509 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15510         /*  get password file name */
15511         if ((error = _get_cert_password_file_name(certificate->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
15512                 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
15513                 goto FINISH_OFF;
15514         }
15515
15516         /*  save passwords to the secure storage */
15517         if (ssm_write_buffer(certificate->password, EM_SAFE_STRLEN(certificate->password), cert_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
15518                 EM_DEBUG_EXCEPTION("ssm_write_buffer failed - Private certificate password : [%s]", cert_password_file_name);
15519                 error = EMAIL_ERROR_SYSTEM_FAILURE;
15520                 goto FINISH_OFF;
15521         }
15522 #endif
15523         if (!emcore_notify_storage_event(NOTI_CERTIFICATE_UPDATE, certificate->certificate_id, 0, NULL, 0))
15524                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_CERTIFICATE_UPDATE] : Notification Failed >>> ");
15525
15526         ret = true;
15527
15528 FINISH_OFF:
15529
15530         if (hStmt != NULL)  {
15531                 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
15532
15533                 rc = sqlite3_finalize(hStmt);
15534                 if (rc != SQLITE_OK)  {
15535                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
15536                         error = EMAIL_ERROR_DB_FAILURE;
15537                 }
15538         }
15539
15540         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
15541
15542         _DISCONNECT_DB;
15543
15544         if (err_code != NULL)
15545                 *err_code = error;
15546
15547         EM_DEBUG_FUNC_END("ret [%d]", ret);
15548         return ret;
15549 }
15550
15551 /* Tasks --------------------------------------------------------------------------*/
15552 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)
15553 {
15554         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);
15555         int ret = 0;
15556         int i = 0;
15557         int task_id = 0;
15558         int err = EMAIL_ERROR_NONE;
15559         int rc = -1;
15560         DB_STMT hStmt = NULL;
15561         char sql_query_string[QUERY_SIZE] = {0, };
15562         sqlite3 *local_db_handle = NULL;
15563         char *sql = "SELECT max(rowid) FROM mail_task_tbl;";
15564         char **result = NULL;
15565
15566         if (input_task_parameter == NULL || output_task_id == NULL)  {
15567                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15568                 err = EMAIL_ERROR_INVALID_PARAM;
15569                 goto FINISH_OFF;
15570         }
15571
15572         local_db_handle = emstorage_get_db_connection();
15573
15574         EMSTORAGE_START_WRITE_TRANSACTION(input_transaction, err);
15575
15576         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
15577         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
15578                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
15579
15580         if (NULL == result[1])
15581                 task_id = 1;
15582         else
15583                 task_id = atoi(result[1])+1;
15584
15585         *output_task_id = task_id;
15586
15587         sqlite3_free_table(result);
15588         result = NULL;
15589
15590         SNPRINTF(sql_query_string, sizeof(sql_query_string),
15591                 "INSERT INTO mail_task_tbl VALUES "
15592                 "(        "
15593                 "    ? "  /*   task_id */
15594                 "  , ? "  /*   task_type */
15595                 "  , ? "  /*   task_status */
15596                 "  , ? "  /*   task_priority */
15597                 "  , ? "  /*   task_parameter_length */
15598                 "  , ? "  /*   task_parameter */
15599                 "  , ? "  /*   date_time */
15600                 ") ");
15601
15602         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15603         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15604                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15605
15606         EM_DEBUG_LOG(">>>> SQL STMT [%s] ", sql_query_string);
15607
15608
15609         _bind_stmt_field_data_int(hStmt, i++, task_id);
15610         _bind_stmt_field_data_int(hStmt, i++, input_task_type);
15611         _bind_stmt_field_data_int(hStmt, i++, EMAIL_TASK_STATUS_WAIT);
15612         _bind_stmt_field_data_int(hStmt, i++, input_task_priority);
15613         _bind_stmt_field_data_int(hStmt, i++, input_task_parameter_length);
15614         _bind_stmt_field_data_blob(hStmt, i++, input_task_parameter, input_task_parameter_length);
15615         _bind_stmt_field_data_int(hStmt, i++, time(NULL));
15616
15617         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15618
15619         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {err = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
15620                 ("sqlite3_step fail:%d", rc));
15621         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15622                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
15623
15624         ret = (err == EMAIL_ERROR_NONE);
15625         EMSTORAGE_FINISH_WRITE_TRANSACTION(input_transaction, ret, err);
15626
15627 FINISH_OFF:
15628
15629         if (hStmt != NULL)  {
15630                 rc = sqlite3_finalize(hStmt);
15631                 if (rc != SQLITE_OK)  {
15632                         EM_DEBUG_LOG("sqlite3_finalize failed [%d]", rc);
15633                         err = EMAIL_ERROR_DB_FAILURE;
15634                 }
15635         }
15636         else
15637                 EM_DEBUG_LOG("hStmt is NULL!!!");
15638
15639         _DISCONNECT_DB;
15640
15641         EM_DEBUG_FUNC_END("err [%d]", err);
15642         return err;
15643 }
15644
15645 INTERNAL_FUNC int emstorage_delete_task(int task_id, int transaction)
15646 {
15647         EM_DEBUG_FUNC_BEGIN("task_id[%d], transaction[%d]", task_id, transaction);
15648         int rc, ret = false;
15649         int err = EMAIL_ERROR_NONE;
15650         char sql_query_string[QUERY_SIZE] = {0, };
15651         sqlite3 *local_db_handle = NULL;
15652
15653         if (task_id < 0)  {
15654                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15655                 err = EMAIL_ERROR_INVALID_PARAM;
15656                 goto FINISH_OFF;
15657         }
15658
15659         local_db_handle = emstorage_get_db_connection();
15660
15661         EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
15662
15663         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_task_tbl WHERE task_id = %d", task_id);
15664
15665         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
15666         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15667                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15668
15669         ret = true;
15670
15671 FINISH_OFF:
15672         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
15673         _DISCONNECT_DB;
15674
15675         EM_DEBUG_FUNC_END("err [%d]", err);
15676         return err;
15677 }
15678
15679 INTERNAL_FUNC int emstorage_update_task_status(int task_id, email_task_status_type_t task_status, int transaction)
15680 {
15681         EM_DEBUG_FUNC_BEGIN("task_id[%d] task_status[%d] transaction[%d]", task_id, task_status, transaction);
15682         int rc, ret = false;
15683         int err = EMAIL_ERROR_NONE;
15684         char sql_query_string[QUERY_SIZE] = {0, };
15685
15686         sqlite3 *local_db_handle = emstorage_get_db_connection();
15687
15688         EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
15689
15690         SNPRINTF(sql_query_string, sizeof(sql_query_string),
15691                 "UPDATE mail_task_tbl SET"
15692                 " task_status = %d"
15693                 " WHERE task_id = %d"
15694                 , task_status
15695                 , task_id);
15696         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
15697         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15698                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15699
15700         ret = true;
15701
15702 FINISH_OFF:
15703         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
15704         _DISCONNECT_DB;
15705
15706
15707         EM_DEBUG_FUNC_END("err [%d]", err);
15708         return err;
15709 }
15710
15711 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)
15712 {
15713         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);
15714         int i = 0, count = 0, rc = -1;
15715         int cur_query = 0;
15716         int field_index = 0;
15717         int err = EMAIL_ERROR_NONE;
15718         email_task_t *task_item_from_tbl = NULL;
15719         char sql_query_string[QUERY_SIZE] = {0, };
15720         char *field_list = "task_id, task_type, task_status, task_priority, task_parameter_length, task_parameter ";
15721         char **result;
15722         sqlite3 *local_db_handle = NULL;
15723         DB_STMT hStmt = NULL;
15724
15725         EM_IF_NULL_RETURN_VALUE(input_conditional_clause, false);
15726         EM_IF_NULL_RETURN_VALUE(output_task_count, false);
15727
15728         local_db_handle = emstorage_get_db_connection();
15729
15730         SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, "SELECT COUNT(*) FROM mail_task_tbl %s", input_conditional_clause);
15731         EM_DEBUG_LOG("emstorage_query_mail_list : query[%s].", sql_query_string);
15732
15733         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
15734         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
15735                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15736
15737         count = atoi(result[1]);
15738         sqlite3_free_table(result);
15739
15740         EM_DEBUG_LOG("count = %d", rc);
15741
15742         if (count == 0) {
15743                 EM_DEBUG_EXCEPTION("no task found...");
15744                 err = EMAIL_ERROR_TASK_NOT_FOUND;
15745                 goto FINISH_OFF;
15746         }
15747
15748         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);
15749         EM_DEBUG_LOG("emstorage_query_mail_list : query[%s].", sql_query_string);
15750
15751         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15752
15753         EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
15754         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15755                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15756
15757         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15758         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15759                 ("sqlite3_step fail:%d", rc));
15760
15761         if (rc == SQLITE_DONE)  {
15762                 EM_DEBUG_EXCEPTION("no task found...");
15763                 err = EMAIL_ERROR_TASK_NOT_FOUND;
15764                 count = 0;
15765                 goto FINISH_OFF;
15766         }
15767
15768         if (!(task_item_from_tbl = (email_task_t*)em_malloc(sizeof(email_task_t) * count))) {
15769                 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
15770                 err = EMAIL_ERROR_OUT_OF_MEMORY;
15771                 goto FINISH_OFF;
15772         }
15773
15774         for (i = 0; i < count; i++)  {
15775                 /*  get recordset */
15776                 field_index = 0;
15777
15778                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_id), field_index++);
15779                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_type), field_index++);
15780                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_status), field_index++);
15781                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_priority), field_index++);
15782                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_parameter_length), field_index++);
15783                 _get_stmt_field_data_blob(hStmt, (void**)&(task_item_from_tbl[i].task_parameter), field_index++);
15784
15785                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15786                 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i,  rc);
15787                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15788                         ("sqlite3_step fail:%d", rc));
15789         }
15790
15791 FINISH_OFF:
15792         if (err == EMAIL_ERROR_NONE)  {
15793                 if (output_task_list)
15794                         *output_task_list = task_item_from_tbl;
15795                 *output_task_count = count;
15796         }
15797         else
15798                 EM_SAFE_FREE(task_item_from_tbl);
15799
15800         if (hStmt != NULL)  {
15801                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
15802                 rc = sqlite3_finalize(hStmt);
15803                 hStmt = NULL;
15804                 if (rc != SQLITE_OK)  {
15805                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
15806                         err = EMAIL_ERROR_DB_FAILURE;
15807                 }
15808         }
15809
15810         _DISCONNECT_DB;
15811
15812         EM_DEBUG_FUNC_END("err [%d]", err);
15813         return err;
15814 }
15815 /* Tasks --------------------------------------------------------------------------*/
15816
15817 /*EOF*/