Tizen 2.0 Release
[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 static int _get_attribute_type_by_mail_field_name(char *input_mail_field_name, email_mail_attribute_type *output_mail_attribute_type);
195
196 #define _MULTIPLE_DB_HANDLE
197
198 #ifdef _MULTIPLE_DB_HANDLE
199
200 #define _DISCONNECT_DB                  /* db_util_close(_db_handle); */
201
202 typedef struct
203 {
204         pthread_t       thread_id;
205         sqlite3 *db_handle;
206 } db_handle_t;
207
208 #define MAX_DB_CLIENT 100
209
210 /* static int _db_handle_count = 0; */
211 db_handle_t _db_handle_list[MAX_DB_CLIENT] = {{0, 0}, };
212
213 sqlite3 *emstorage_get_db_handle()
214 {
215         EM_DEBUG_FUNC_BEGIN();
216         int i;
217         pthread_t current_thread_id = THREAD_SELF();
218         sqlite3 *result_db_handle = NULL;
219
220         ENTER_CRITICAL_SECTION(_db_handle_lock);
221         for (i = 0; i < MAX_DB_CLIENT; i++) {
222                 if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
223                         EM_DEBUG_LOG("found db handle at [%d]", i);
224                         result_db_handle = _db_handle_list[i].db_handle;
225                         break;
226                 }
227         }
228         LEAVE_CRITICAL_SECTION(_db_handle_lock);
229
230         if (!result_db_handle)
231                 EM_DEBUG_EXCEPTION("Can't find proper handle for [%d]", current_thread_id);
232
233         EM_DEBUG_FUNC_END();
234         return result_db_handle;
235 }
236
237 int emstorage_set_db_handle(sqlite3 *db_handle)
238 {
239         EM_DEBUG_FUNC_BEGIN();
240         int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
241         pthread_t current_thread_id = THREAD_SELF();
242
243         ENTER_CRITICAL_SECTION(_db_handle_lock);
244         for (i = 0; i < MAX_DB_CLIENT; i++)     {
245                 if (_db_handle_list[i].thread_id == 0) {
246                         _db_handle_list[i].thread_id = current_thread_id;
247                         _db_handle_list[i].db_handle = db_handle;
248                         EM_DEBUG_LOG("current_thread_id [%d], index [%d]", current_thread_id, i);
249                         error_code =  EMAIL_ERROR_NONE;
250                         break;
251                 }
252         }
253         LEAVE_CRITICAL_SECTION(_db_handle_lock);
254
255         if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
256                 EM_DEBUG_EXCEPTION("Exceeded the limitation of db client. Can't find empty slot in _db_handle_list.");
257
258         EM_DEBUG_FUNC_END("error_code [%d]", error_code);
259         return error_code;
260 }
261
262 int emstorage_remove_db_handle()
263 {
264         EM_DEBUG_FUNC_BEGIN();
265         int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
266         ENTER_CRITICAL_SECTION(_db_handle_lock);
267         for (i = 0; i < MAX_DB_CLIENT; i++)
268         {
269                 if (_db_handle_list[i].thread_id == THREAD_SELF())
270                 {
271                         _db_handle_list[i].thread_id = 0;
272                         _db_handle_list[i].db_handle = NULL;
273                         EM_DEBUG_LOG("index [%d]", i);
274                         error_code = EMAIL_ERROR_NONE;
275                         break;
276                 }
277         }
278         LEAVE_CRITICAL_SECTION(_db_handle_lock);
279
280         if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
281                 EM_DEBUG_EXCEPTION("Can't find proper thread_id");
282
283         EM_DEBUG_FUNC_END("error_code [%d]", error_code);
284         return error_code;
285 }
286
287
288 int emstorage_reset_db_handle_list()
289 {
290         EM_DEBUG_FUNC_BEGIN();
291         int i;
292
293         ENTER_CRITICAL_SECTION(_db_handle_lock);
294         for (i = 0; i < MAX_DB_CLIENT; i++)
295         {
296                 _db_handle_list[i].thread_id = 0;
297                 _db_handle_list[i].db_handle = NULL;
298         }
299         LEAVE_CRITICAL_SECTION(_db_handle_lock)
300
301         EM_DEBUG_FUNC_END();
302         return EMAIL_ERROR_NONE;
303 }
304
305 sqlite3 *emstorage_get_db_connection()
306 {
307         sqlite3 *tmp_db_handle = emstorage_get_db_handle();
308         if (NULL == tmp_db_handle)
309                 tmp_db_handle = emstorage_db_open(NULL);
310         return tmp_db_handle;
311 }
312
313
314 #else   /*  _MULTIPLE_DB_HANDLE */
315 #define _DISCONNECT_DB                  /* db_util_close(_db_handle); */
316
317 sqlite3 *_db_handle = NULL;
318
319 sqlite3 *emstorage_get_db_connection()
320 {
321         if (NULL == _db_handle)
322                 emstorage_db_open(NULL);
323         return _db_handle;
324 }
325 #endif  /*  _MULTIPLE_DB_HANDLE */
326
327 /* ------------------------------------------------------------------------------ */
328 /*  Mutex using shared memory */
329 typedef struct
330 {
331         pthread_mutex_t mutex;
332         int data;
333 } mmapped_t;
334
335 mmapped_t       *mapped_for_db_lock = NULL;
336 int              shm_fd_for_db_lock = 0;
337
338 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
339 mmapped_t       *mapped_for_generating_mail_id = NULL;
340 int              shm_fd_for_generating_mail_id = 0;
341 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
342
343 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__
344 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
345         {  _timedlock_shm_mutex(&mapped_for_db_lock, 2); return_value = function_call; _unlockshm_mutex(&mapped_for_db_lock); }
346 #else /*  __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
347 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
348         {  return_value = function_call; }
349 #endif /*  __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
350
351 INTERNAL_FUNC int emstorage_shm_file_init(const char *shm_file_name)
352 {
353         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
354
355         if(!shm_file_name) {
356                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
357                 return EMAIL_ERROR_INVALID_PARAM;
358         }
359
360         int fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); /*  note: permission is not working */
361
362         if (fd > 0) {
363                 fchmod(fd, 0666);
364                 EM_DEBUG_LOG("** Create SHM FILE **");
365                 if (ftruncate(fd, sizeof(mmapped_t)) != 0) {
366                         EM_DEBUG_EXCEPTION("ftruncate failed: %s", strerror(errno));
367                         return EMAIL_ERROR_SYSTEM_FAILURE;
368                 }
369
370                 mmapped_t *m = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
371                 if (m == MAP_FAILED) {
372                         EM_DEBUG_EXCEPTION("mmap failed: %s", strerror(errno));
373                         return EMAIL_ERROR_SYSTEM_FAILURE;
374                 }
375
376                 m->data = 0;
377
378                 pthread_mutexattr_t mattr;
379                 pthread_mutexattr_init(&mattr);
380                 pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
381                 pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
382                 pthread_mutex_init(&(m->mutex), &mattr);
383                 pthread_mutexattr_destroy(&mattr);
384         }
385         else {
386                 EM_DEBUG_EXCEPTION("shm_open failed: %s", strerror(errno));
387                 return EMAIL_ERROR_SYSTEM_FAILURE;
388         }
389         close(fd);
390         EM_DEBUG_FUNC_END();
391         return EMAIL_ERROR_NONE;
392 }
393
394 int emstorage_shm_file_destroy(const char *shm_file_name)
395 {
396         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
397         if(!shm_file_name) {
398                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
399                 return EMAIL_ERROR_INVALID_PARAM;
400         }
401
402         if (shm_unlink(shm_file_name) != 0)
403                 EM_DEBUG_EXCEPTION("shm_unlink failed: %s", strerror(errno));
404         EM_DEBUG_FUNC_END();
405         return EMAIL_ERROR_NONE;
406 }
407
408 static int _initialize_shm_mutex(const char *shm_file_name, int *param_shm_fd, mmapped_t **param_mapped)
409 {
410         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p] param_shm_fd [%p], param_mapped [%p]", shm_file_name, param_shm_fd, param_mapped);
411
412         if(!shm_file_name || !param_shm_fd || !param_mapped) {
413                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
414                 return EMAIL_ERROR_INVALID_PARAM;
415         }
416
417         if (!(*param_mapped)) {
418                 EM_DEBUG_LOG("** mapping begin **");
419
420                 if (!(*param_shm_fd)) { /*  open shm_file_name at first. Otherwise, the num of files in /proc/pid/fd will be increasing  */
421                         *param_shm_fd = shm_open(shm_file_name, O_RDWR, 0);
422                         if ((*param_shm_fd) == -1) {
423                                 EM_DEBUG_EXCEPTION("FAIL: shm_open(): %s", strerror(errno));
424                                 return EMAIL_ERROR_SYSTEM_FAILURE;
425                         }
426                 }
427                 mmapped_t *tmp = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ|PROT_WRITE, MAP_SHARED, (*param_shm_fd), 0);
428
429                 if (tmp == MAP_FAILED) {
430                         EM_DEBUG_EXCEPTION("mmap failed: %s", strerror(errno));
431                         return EMAIL_ERROR_SYSTEM_FAILURE;
432                 }
433                 *param_mapped = tmp;
434         }
435
436         EM_DEBUG_FUNC_END();
437         return EMAIL_ERROR_NONE;
438 }
439
440 static int _timedlock_shm_mutex(mmapped_t **param_mapped, int sec)
441 {
442         EM_DEBUG_FUNC_BEGIN("param_mapped [%p], sec [%d]", param_mapped, sec);
443
444         if(!param_mapped) {
445                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
446                 return EMAIL_ERROR_INVALID_PARAM;
447         }
448
449         struct timespec abs_time;
450         clock_gettime(CLOCK_REALTIME, &abs_time);
451         abs_time.tv_sec += sec;
452
453         int err = pthread_mutex_timedlock(&((*param_mapped)->mutex), &abs_time);
454
455         if (err == EOWNERDEAD) {
456                 err = pthread_mutex_consistent(&((*param_mapped)->mutex));
457                 EM_DEBUG_EXCEPTION("Previous owner is dead with lock. Fix mutex : %s", EM_STRERROR(err));
458         }
459         else if (err != 0) {
460                 EM_DEBUG_EXCEPTION("ERROR : %s", EM_STRERROR(err));
461                 return err;
462         }
463
464         EM_DEBUG_FUNC_END();
465         return EMAIL_ERROR_NONE;
466 }
467
468 void _unlockshm_mutex(mmapped_t **param_mapped)
469 {
470         EM_DEBUG_FUNC_BEGIN();
471         pthread_mutex_unlock(&((*param_mapped)->mutex));
472         EM_DEBUG_FUNC_END();
473 }
474 /* ------------------------------------------------------------------------------ */
475
476
477 static int _open_counter = 0;
478 static int g_transaction = false;
479
480 static int _get_password_file_name(int account_id, char *recv_password_file_name, char *send_password_file_name);
481 static int _read_password_from_secure_storage(char *file_name, char **password);
482
483 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
484 static int _get_cert_password_file_name(int index, char *cert_password_file_name);
485 #endif
486
487 typedef struct {
488         const char *object_name;
489         unsigned int data_flag;
490 } email_db_object_t;
491
492 static const email_db_object_t _g_db_tables[] =
493 {
494         { "mail_read_mail_uid_tbl", 1},
495         { "mail_tbl", 1},
496         { "mail_attachment_tbl", 1},
497         { NULL,  0},
498 };
499
500 static const email_db_object_t _g_db_indexes[] =
501 {
502         { "mail_read_mail_uid_idx1", 1},
503         { "mail_idx1", 1},
504         { "mail_attachment_idx1", 1},
505         { NULL,  1},
506 };
507
508 enum
509 {
510         CREATE_TABLE_MAIL_ACCOUNT_TBL,
511         CREATE_TABLE_MAIL_BOX_TBL,
512         CREATE_TABLE_MAIL_READ_MAIL_UID_TBL,
513         CREATE_TABLE_MAIL_RULE_TBL,
514         CREATE_TABLE_MAIL_TBL,
515         CREATE_TABLE_MAIL_ATTACHMENT_TBL,
516 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
517         CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL,
518 #endif
519         CREATE_TABLE_MAIL_MEETING_TBL,
520 #ifdef __FEATURE_LOCAL_ACTIVITY__
521         CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL,
522 #endif
523         CREATE_TABLE_MAIL_CERTIFICATE_TBL,
524         CREATE_TABLE_MAIL_TASK_TBL,
525         CREATE_TABLE_MAX,
526 };
527
528 enum
529 {
530         DATA1_IDX_IN_MAIL_ACTIVITY_TBL = 0,
531         TRANSTYPE_IDX_IN_MAIL_ACTIVITY_TBL,
532         FLAG_IDX_IN_MAIL_ACTIVITY_TBL,
533 };
534
535 enum
536 {
537         CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL = 0,
538         ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
539         ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
540         ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
541         EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
542         EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
543         EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
544         ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL,
545         EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL,
546         SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL,
547         FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL,
548 };
549
550 enum
551 {
552         TO_RECIPIENT = 0,
553         CC_RECIPIENT,
554         BCC_RECIPIENT,
555 };
556 enum
557 {
558         ACCOUNT_ID_IDX_IN_MAIL_BOX_TBL = 0,
559         LOCAL_YN_IDX_IN_MAIL_BOX_TBL,
560         MAILBOX_NAME_IDX_IN_MAIL_BOX_TBL,
561         MAILBOX_TYPE_IDX_IN_MAIL_BOX_TBL,
562         ALIAS_IDX_IN_MAIL_BOX_TBL,
563         SYNC_WITH_SERVER_YN_IDX_IN_MAIL_BOX_TBL,
564         MODIFIABLE_YN_IDX_IN_MAIL_BOX_TBL,
565         TOTAL_MAIL_COUNT_ON_SERVER_IDX_IN_MAIL_BOX_TBL,
566         ARCHIVE_IDX_IN_MAIL_BOX_TBL,
567         MAIL_SLOT_SIZE_IDX_IN_MAIL_BOX_TBL,
568 };
569
570 enum
571 {
572         ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL = 0,
573         LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
574         LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
575         MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL,
576         S_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
577         DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL,
578         DATA2_IDX_IN_MAIL_READ_MAIL_UID_TBL,
579         FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL,
580         IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL,          /* unused */
581 };
582
583 enum
584 {
585         ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL = 0,
586         RULE_ID_IDX_IN_MAIL_RULE_TBL,
587         TYPE_IDX_IN_MAIL_RULE_TBL,
588         VALUE_IDX_IN_MAIL_RULE_TBL,
589         ACTION_TYPE_IDX_IN_MAIL_RULE_TBL,
590         TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL,
591         FLAG1_IDX_IN_MAIL_RULE_TBL,
592         FLAG2_IDX_IN_MAIL_RULE_TBL,
593 };
594
595 enum
596 {
597         MAIL_ID_IDX_IN_MAIL_TBL = 0,
598         ACCOUNT_ID_IDX_IN_MAIL_TBL,
599         MAILBOX_ID_IDX_IN_MAIL_TBL,
600         MAILBOX_NAME_IDX_IN_MAIL_TBL,
601         MAILBOX_TYPE_IDX_IN_MAIL_TBL,
602         SUBJECT_IDX_IN_MAIL_TBL,
603         DATETIME_IDX_IN_MAIL_TBL,
604         SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL,
605         SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL,
606         SERVER_MAIL_ID_IDX_IN_MAIL_TBL,
607         MESSAGE_ID_IDX_IN_MAIL_TBL,
608         REFERENCE_ID_IDX_IN_MAIL_TBL,
609         FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL,
610         FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL,
611         FULL_ADDRESS_TO_IDX_IN_MAIL_TBL,
612         FULL_ADDRESS_CC_IDX_IN_MAIL_TBL,
613         FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL,
614         FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL,
615         EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL,
616         EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL,
617         ALIAS_SENDER_IDX_IN_MAIL_TBL,
618         ALIAS_RECIPIENT_IDX_IN_MAIL_TBL,
619         BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL,
620         FILE_PATH_PLAIN_IDX_IN_MAIL_TBL,
621         FILE_PATH_HTML_IDX_IN_MAIL_TBL,
622         FILE_PATH_MIME_ENTITY_IDX_IN_MAIL_TBL,
623         MAIL_SIZE_IDX_IN_MAIL_TBL,
624         FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL,
625         FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL,
626         FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL,
627         FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL,
628         FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL,
629         FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL,
630         FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL,
631         DRM_STATUS_IDX_IN_MAIL_TBL,
632         PRIORITY_IDX_IN_MAIL_TBL,
633         SAVE_STATUS_IDX_IN_MAIL_TBL,
634         LOCK_STATUS_IDX_IN_MAIL_TBL,
635         REPORT_STATUS_IDX_IN_MAIL_TBL,
636         ATTACHMENT_COUNT_IDX_IN_MAIL_TBL,
637         INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL,
638         THREAD_ID_IDX_IN_MAIL_TBL,
639         THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL,
640         PREVIEW_TEXT_IDX_IN_MAIL_TBL,
641         MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL,
642         MESSAGE_CLASS_IDX_IN_MAIL_TBL,
643         DIGEST_TYPE_IDX_IN_MAIL_TBL,
644         SMIME_TYPE_IDX_IN_MAIL_TBL,
645         FIELD_COUNT_OF_MAIL_TBL,  /* End of mail_tbl */
646 };
647
648 enum
649 {
650         ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL = 0,
651         ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL,
652         ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL,
653         ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL,
654         MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
655         ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
656         MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
657         ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
658         ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
659         ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL,
660         ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
661         ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
662 #ifdef __ATTACHMENT_OPTI__
663         ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL,
664         SECTION_IDX_IN_MAIL_ATTACHMENT_TBL,
665 #endif
666 };
667
668 enum {
669         IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL = 0,
670 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
671         MAIL_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
672         ACCOUNT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
673         ADDRESS_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
674         ADDRESS_IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL,
675 #endif
676         ADDRESS_IDX_IN_MAIL_CONTACT_SYNC_TBL,
677         CONTACT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
678         STORAGE_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
679         CONTACT_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
680 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
681         DISPLAY_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
682         FLAG1_IDX_IN_MAIL_CONTACT_SYNC_TBL,
683 #endif
684 };
685
686 /* sowmya.kr 03032010, changes for get list of mails for given addr list */
687 typedef struct _em_mail_id_list {
688         int mail_id;
689         struct _em_mail_id_list *next;
690 } em_mail_id_list;
691
692 static char *g_test_query[] = {
693                 /*  1. select mail_account_tbl */
694                 "SELECT"
695                 " account_name, "
696                 " incoming_server_type, "
697                 " incoming_server_address, "
698                 " user_email_address, "
699                 " incoming_server_user_name, "
700                 " incoming_server_password, "
701                 " retrieval_mode, "
702                 " incoming_server_port_number, "
703                 " incoming_server_secure_connection, "
704                 " outgoing_server_type, "
705                 " outgoing_server_address, "
706                 " outgoing_server_port_number, "
707                 " outgoing_server_need_authentication, "
708                 " outgoing_server_secure_connection, "
709                 " outgoing_server_user_name, "
710                 " outgoing_server_password, "
711                 " display_name, "
712                 " reply_to_addr, "
713                 " return_addr, "
714                 " account_id, "
715                 " keep_mails_on_pop_server_after_download, "
716                 " flag1, "
717                 " flag2, "
718                 " pop_before_smtp, "
719                 " incoming_server_requires_apop"
720                 ", logo_icon_path, "
721                 " is_preset_account, "
722                 " check_interval, "
723                 " priority, "
724                 " keep_local_copy, "
725                 " req_delivery_receipt, "
726                 " req_read_receipt, "
727                 " download_limit, "
728                 " block_address, "
729                 " block_subject, "
730                 " display_name_from, "
731                 " reply_with_body, "
732                 " forward_with_files, "
733                 " add_myname_card, "
734                 " add_signature, "
735                 " signature"
736                 ", add_my_address_to_bcc"
737                 ", account_svc_id "
738                 ", index_color "
739                 ", sync_status "
740                 ", sync_disabled "
741                 ", smime_type"
742                 ", certificate_path"
743                 ", cipher_type"
744                 ", digest_type"
745                 " FROM mail_account_tbl",
746                 /*  2. select mail_box_tbl */
747                 "SELECT "
748                 "   mailbox_id, "
749                 "   account_id, "
750                 "   local_yn,  "
751                 "   mailbox_name,  "
752                 "   mailbox_type,   "
753                 "   alias,  "
754                 "   deleted_flag,  "
755                 "   modifiable_yn,  "
756                 "   total_mail_count_on_server,  "
757                 "   has_archived_mails, "
758                 "   mail_slot_size, "
759                 "   no_select, "
760                 "   last_sync_time "
761                 " FROM mail_box_tbl ",
762                 /*  3. select mail_read_mail_uid_tbl */
763                 "SELECT  "
764                 "   account_id,  "
765                 "   mailbox_id,  "
766                 "   local_uid,  "
767                 "   mailbox_name,  "
768                 "   s_uid,  "
769                 "   data1 ,  "
770                 "   data2,  "
771                 "   flag,  "
772                 "   idx_num "
773                 " FROM mail_read_mail_uid_tbl ",
774                 /*  4. select mail_rule_tbl */
775                 "SELECT "
776                 "   account_id, "
777                 "   rule_id, "
778                 "       type, "
779                 "       value, "
780                 "       action_type, "
781                 "       target_mailbox_id,      "
782                 "       flag1, "
783                 "       flag2 "
784                 " FROM mail_rule_tbl    ",
785                 /*  5. select mail_tbl */
786                 "SELECT"
787                 "       mail_id, "
788                 "       account_id, "
789                 "       mailbox_id, "
790                 "       mailbox_name, "
791                 "   mailbox_type, "
792                 "   subject, "
793                 "       date_time, "
794                 "       server_mail_status, "
795                 "       server_mailbox_name, "
796                 "       server_mail_id, "
797                 "   message_id, "
798                 "       reference_mail_id, "
799                 "   full_address_from, "
800                 "   full_address_reply, "
801                 "   full_address_to, "
802                 "   full_address_cc, "
803                 "   full_address_bcc, "
804                 "   full_address_return, "
805                 "   email_address_sender, "
806                 "   email_address_recipient, "
807                 "   alias_sender, "
808                 "   alias_recipient, "
809                 "       body_download_status, "
810                 "       file_path_plain, "
811                 "       file_path_html, "
812                 "   file_path_mime_entity, "
813                 "       mail_size, "
814                 "   flags_seen_field     ,"
815                 "   flags_deleted_field  ,"
816                 "   flags_flagged_field  ,"
817                 "   flags_answered_field ,"
818                 "   flags_recent_field   ,"
819                 "   flags_draft_field    ,"
820                 "   flags_forwarded_field,"
821                 "       DRM_status, "
822                 "       priority, "
823                 "       save_status, "
824                 "       lock_status, "
825                 "       report_status, "
826                 "   attachment_count, "
827                 "       inline_content_count, "
828                 "       thread_id, "
829                 "       thread_item_count, "
830                 "   preview_text, "
831                 "       meeting_request_status, "
832                 "   message_class, "
833                 "   digest_type, "
834                 "   smime_type "
835                 " FROM mail_tbl",
836                 /*  6. select mail_attachment_tbl */
837                 "SELECT "
838                 "       attachment_id, "
839                 "       attachment_name, "
840                 "       attachment_path, "
841                 "       attachment_size, "
842                 "       mail_id,  "
843                 "       account_id, "
844                 "       mailbox_id, "
845                 "       attachment_save_status,  "
846                 "       attachment_drm_type,  "
847                 "       attachment_drm_method,  "
848                 "       attachment_inline_content_status,  "
849                 "       attachment_mime_type  "
850                 " FROM mail_attachment_tbl ",
851
852 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
853                 "SELECT  "
854                 "   account_id, "
855                 "   mail_id, "
856                 "   server_mail_id, "
857                 "   activity_id, "
858                 "   activity_type, "
859                 "   mailbox_id, "
860                 "   mailbox_name "
861                 " FROM mail_partial_body_activity_tbl ",
862 #endif
863
864                 "SELECT  "
865                 "   mail_id, "
866                 "   account_id, "
867                 "   mailbox_id, "
868                 "   meeting_response, "
869                 "   start_time, "
870                 "   end_time, "
871                 "   location, "
872                 "   global_object_id, "
873                 "   offset, "
874                 "   standard_name, "
875                 "   standard_time_start_date, "
876                 "   standard_bias, "
877                 "   daylight_name, "
878                 "   daylight_time_start_date, "
879                 "   daylight_bias "
880                 " FROM mail_meeting_tbl ",
881
882 #ifdef __FEATURE_LOCAL_ACTIVITY__
883                 "SELECT "
884                 "       activity_id, "
885                 "   account_id, "
886                 "   mail_id, "
887                 "   activity_type, "
888                 "   server_mailid, "
889                 "   src_mbox , "
890                 "       dest_mbox "
891                 " FROM mail_local_activity_tbl  ",
892 #endif
893                 "SELECT "
894                 "       task_id, "
895                 "   task_type, "
896                 "   task_status, "
897                 "   task_priority, "
898                 "   task_parameter_length, "
899                 "   task_parameter , "
900                 "       date_time "
901                 " FROM mail_task_tbl    ",
902                 NULL,
903 };
904
905
906 static int _get_table_field_data_char(char  **table, char *buf, int index)
907 {
908         if ((table == NULL) || (buf == NULL) || (index < 0))  {
909                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
910                 return false;
911         }
912
913         if (table[index] != NULL) {
914                 *buf = (char)atoi(table[index]);
915                 return true;
916         }
917
918         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
919
920         *buf = 0;
921         return false;
922 }
923
924 static int _get_table_field_data_int(char  **table, int *buf, int index)
925 {
926         if ((table == NULL) || (buf == NULL) || (index < 0))  {
927                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
928                 return false;
929         }
930
931         if (table[index] != NULL) {
932                 *buf = atoi(table[index]);
933                 return true;
934         }
935
936         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
937
938         *buf = 0;
939         return false;
940 }
941
942 static int _get_table_field_data_time_t(char  **table, time_t *buf, int index)
943 {
944         if ((table == NULL) || (buf == NULL) || (index < 0))  {
945                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
946                 return false;
947         }
948
949         if (table[index] != NULL) {
950                 *buf = (time_t)atol(table[index]);
951                 return true;
952         }
953
954         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
955
956         *buf = 0;
957         return false;
958 }
959
960 static int _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
961 {
962         int ret = false;
963
964         if ((table == NULL) || (buf == NULL) || (index < 0))  {
965                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
966                 return false;
967         }
968
969         char *pTemp = table[index];
970         int sLen = 0;
971         if (pTemp == NULL)
972                 *buf = NULL;
973         else {
974                 sLen = EM_SAFE_STRLEN(pTemp);
975                 if(sLen) {
976                         *buf = (char *) em_malloc(sLen + 1);
977                         if (*buf == NULL) {
978                                 EM_DEBUG_EXCEPTION("malloc is failed");
979                                 goto FINISH_OFF;
980                         }
981                         strncpy(*buf, pTemp, sLen);
982                 }
983                 else
984                         *buf = NULL;
985         }
986 #ifdef _PRINT_STORAGE_LOG_
987         if (*buf)
988                 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", *buf, index);
989         else
990                 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
991 #endif
992         ret = true;
993 FINISH_OFF:
994
995         return ret;
996 }
997
998 static int _get_table_field_data_string_without_allocation(char **table, char *buf, int buffer_size, int ucs2, int index)
999 {
1000         if ((table == NULL) || (buf == NULL) || (index < 0))  {
1001                 EM_DEBUG_EXCEPTION(" table[%p], buf[%p], index[%d]", table, buf, index);
1002                         return false;
1003         }
1004
1005         char *pTemp = table[index];
1006
1007         if (pTemp == NULL)
1008                 buf = NULL;
1009         else {
1010                 memset(buf, 0, buffer_size);
1011                 strncpy(buf, pTemp, buffer_size - 1);
1012         }
1013 #ifdef _PRINT_STORAGE_LOG_
1014         if (buf)
1015                 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", buf, index);
1016         else
1017                 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1018 #endif
1019
1020         return true;
1021 }
1022
1023 /*
1024 static int _get_table_field_data_blob(char **table, void **buffer, int buffer_size, int index)
1025 {
1026         if ((table == NULL) || (buffer == NULL) || (index < 0))  {
1027                 EM_DEBUG_EXCEPTION(" table[%p], buffer[%p], buffer_size [%d], index[%d]", table, buffer, buffer_size, index);
1028                 return false;
1029         }
1030
1031         char *temp_buffer = table[index];
1032
1033         if (temp_buffer == NULL)
1034                 buffer = NULL;
1035         else {
1036                 *buffer = malloc(buffer_size);
1037                 if(*buffer == NULL) {
1038                         EM_DEBUG_EXCEPTION("allocation failed.");
1039                         return false;
1040                 }
1041                 memset(*buffer, 0, buffer_size);
1042                 memcpy(*buffer, temp_buffer, buffer_size);
1043         }
1044 #ifdef _PRINT_STORAGE_LOG_
1045         if (buf)
1046                 EM_DEBUG_LOG("_get_table_field_data_string - buffer[%s], index[%d]", buffer, index);
1047         else
1048                 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1049 #endif
1050
1051         return true;
1052 }
1053 */
1054
1055 static int _get_stmt_field_data_char(DB_STMT hStmt, char *buf, int index)
1056 {
1057         if ((hStmt == NULL) || (buf == NULL) || (index < 0))  {
1058                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1059                 return false;
1060         }
1061
1062         if (sqlite3_column_text(hStmt, index) != NULL) {
1063                 *buf = (char)sqlite3_column_int(hStmt, index);
1064 #ifdef _PRINT_STORAGE_LOG_
1065                         EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1066 #endif
1067                 return true;
1068         }
1069
1070         EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1071
1072         return false;
1073 }
1074
1075 static int _get_stmt_field_data_int(DB_STMT hStmt, int *buf, int index)
1076 {
1077         if ((hStmt == NULL) || (buf == NULL) || (index < 0))  {
1078                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1079                 return false;
1080         }
1081
1082         if (sqlite3_column_text(hStmt, index) != NULL) {
1083                 *buf = sqlite3_column_int(hStmt, index);
1084 #ifdef _PRINT_STORAGE_LOG_
1085                         EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1086 #endif
1087                 return true;
1088         }
1089
1090         EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1091
1092         return false;
1093 }
1094
1095 static int _get_stmt_field_data_time_t(DB_STMT hStmt, time_t *buf, int index)
1096 {
1097         if ((hStmt == NULL) || (buf == NULL) || (index < 0))  {
1098                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1099                 return false;
1100         }
1101
1102         if (sqlite3_column_text(hStmt, index) != NULL) {
1103                 *buf = (time_t)sqlite3_column_int(hStmt, index);
1104 #ifdef _PRINT_STORAGE_LOG_
1105                 EM_DEBUG_LOG("_get_stmt_field_data_time_t [%d]", *buf);
1106 #endif
1107                 return true;
1108         }
1109
1110         EM_DEBUG_LOG("_get_stmt_field_data_time_t fail. index [%d]", index);
1111         return false;
1112 }
1113
1114 static int _get_stmt_field_data_string(DB_STMT hStmt, char **buf, int ucs2, int index)
1115 {
1116         if (!hStmt || !buf || (index < 0))  { /*prevent 39619*/
1117                 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1118                 return false;
1119         }
1120
1121         int sLen = 0;
1122         sLen = sqlite3_column_bytes(hStmt, index);
1123
1124 #ifdef _PRINT_STORAGE_LOG_
1125                 EM_DEBUG_LOG("_get_stmt_field_data_string sqlite3_column_bytes sLen[%d]", sLen);
1126 #endif
1127
1128         if (sLen > 0) {
1129                 *buf = (char *) em_malloc(sLen + 1);
1130                 strncpy(*buf, (char *)sqlite3_column_text(hStmt, index), sLen);
1131         }
1132         else
1133                 *buf = NULL;
1134
1135 #ifdef _PRINT_STORAGE_LOG_
1136         if (*buf)
1137                 EM_DEBUG_LOG("buf[%s], index[%d]", *buf, index);
1138         else
1139                 EM_DEBUG_LOG("_get_stmt_field_data_string - No string got");
1140 #endif
1141
1142         return false;
1143 }
1144
1145 static void _get_stmt_field_data_blob(DB_STMT hStmt, void **buf, int index)
1146 {
1147         if( !hStmt || !buf || (index < 0))  { /*prevent 39618*/
1148                 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1149                 return;
1150         }
1151
1152         int sLen = 0;
1153         sLen = sqlite3_column_bytes(hStmt, index);
1154
1155 #ifdef _PRINT_STORAGE_LOG_
1156         EM_DEBUG_LOG("_get_stmt_field_data_blob sqlite3_column_bytes sLen[%d]", sLen);
1157 #endif
1158
1159         if (sLen > 0) {
1160                 *buf = (char *) em_malloc(sLen);
1161                 memcpy(*buf, (void *)sqlite3_column_blob(hStmt, index), sLen);
1162         }
1163         else
1164                 *buf = NULL;
1165
1166 }
1167
1168 static int _get_stmt_field_data_string_without_allocation(DB_STMT hStmt, char *buf, int buffer_size, int ucs2, int index)
1169 {
1170         if (!hStmt || !buf || (index < 0))  { /*prevent 39620*/
1171                 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], buffer_size[%d], index[%d]", hStmt, buf, buffer_size, index);
1172                 return false;
1173         }
1174
1175         int sLen = 0;
1176         sLen = sqlite3_column_bytes(hStmt, index);
1177
1178 #ifdef _PRINT_STORAGE_LOG_
1179                 EM_DEBUG_LOG("_get_stmt_field_data_string_without_allocation sqlite3_column_bytes sLen[%d]", sLen);
1180 #endif
1181
1182         if (sLen > 0) {
1183                 memset(buf, 0, buffer_size);
1184                 strncpy(buf, (char *)sqlite3_column_text(hStmt, index), buffer_size - 1);
1185         }
1186         else
1187                 strcpy(buf, "");
1188
1189 #ifdef _PRINT_STORAGE_LOG_
1190         EM_DEBUG_LOG("buf[%s], index[%d]", buf, index);
1191 #endif
1192
1193         return false;
1194 }
1195
1196 static int _bind_stmt_field_data_char(DB_STMT hStmt, int index, char value)
1197 {
1198         if ((hStmt == NULL) || (index < 0)) {
1199                 EM_DEBUG_EXCEPTION("index[%d]", index);
1200                 return false;
1201         }
1202
1203         int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1204
1205         if (ret != SQLITE_OK)  {
1206                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1207                 return false;
1208         }
1209
1210         return true;
1211 }
1212
1213 static int _bind_stmt_field_data_int(DB_STMT hStmt, int index, int value)
1214 {
1215         if ((hStmt == NULL) || (index < 0)) {
1216                 EM_DEBUG_EXCEPTION("index[%d]", index);
1217                 return false;
1218         }
1219
1220         int ret = sqlite3_bind_int(hStmt, index+1, value);
1221
1222         if (ret != SQLITE_OK)  {
1223                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1224                 return false;
1225         }
1226
1227         return true;
1228 }
1229
1230 static int _bind_stmt_field_data_time_t(DB_STMT hStmt, int index, time_t value)
1231 {
1232         if ((hStmt == NULL) || (index < 0)) {
1233                 EM_DEBUG_EXCEPTION("index[%d]", index);
1234                 return false;
1235         }
1236
1237         int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1238
1239         if (ret != SQLITE_OK)  {
1240                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1241                 return false;
1242         }
1243
1244         return true;
1245 }
1246
1247 static int _bind_stmt_field_data_string(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1248 {
1249         if ((hStmt == NULL) || (index < 0)) {
1250                 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1251                 return false;
1252         }
1253
1254 #ifdef _PRINT_STORAGE_LOG_
1255         EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1256 #endif
1257
1258         int ret = 0;
1259         if (value != NULL)
1260                 ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1261         else
1262                 ret = sqlite3_bind_text(hStmt, index+1, "", -1, NULL);
1263
1264         if (ret != SQLITE_OK)  {
1265                 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1266                 return false;
1267         }
1268         return true;
1269 }
1270
1271
1272 static int _bind_stmt_field_data_blob(DB_STMT hStmt, int index, void *blob, int blob_size)
1273 {
1274         if ((hStmt == NULL) || (index < 0)) {
1275                 EM_DEBUG_EXCEPTION("index[%d], blob_size[%d]", index, blob_size);
1276                 return false;
1277         }
1278
1279 #ifdef _PRINT_STORAGE_LOG_
1280         EM_DEBUG_LOG("hStmt = %p, index = %d, blob_size = %d, blob = [%p]", hStmt, index, blob_size, blob);
1281 #endif
1282
1283         int ret = 0;
1284         if (blob_size>0)
1285                 ret = sqlite3_bind_blob(hStmt, index+1, blob, blob_size, SQLITE_STATIC);
1286         else
1287                 ret = sqlite3_bind_null(hStmt, index+1);
1288
1289         if (ret != SQLITE_OK)  {
1290                 EM_DEBUG_EXCEPTION("sqlite3_bind_blob fail [%d]", ret);
1291                 return false;
1292         }
1293         return true;
1294 }
1295
1296
1297 static int _delete_temp_file(const char *path)
1298 {
1299         EM_DEBUG_FUNC_BEGIN("path[%p]", path);
1300
1301         DIR *dp = NULL;
1302         struct dirent *entry = NULL;
1303
1304         char buf[1024] = {0x00, };
1305
1306         if ((dp = opendir(path)) == NULL)  {
1307                 EM_DEBUG_EXCEPTION("opendir(\"%s\") failed...", path);
1308                 return false;
1309         }
1310
1311         while ((entry = readdir(dp)) != NULL)  {
1312                 SNPRINTF(buf, sizeof(buf), "%s/%s", path, entry->d_name);
1313                 remove(buf);
1314         }
1315
1316         closedir(dp);
1317         EM_DEBUG_FUNC_END();
1318         return true;
1319 }
1320
1321 char *cpy_str(char *src)
1322 {
1323         char *p = NULL;
1324
1325         if (src)  {
1326                 if (!(p = em_malloc((int)EM_SAFE_STRLEN(src) + 1)))  {
1327                         EM_DEBUG_EXCEPTION("mailoc failed...");
1328                         return NULL;
1329                 }
1330                 strncpy(p, src, EM_SAFE_STRLEN(src));
1331         }
1332
1333         return p;
1334 }
1335
1336 static void _emstorage_close_once(void)
1337 {
1338         EM_DEBUG_FUNC_BEGIN();
1339
1340         DELETE_CRITICAL_SECTION(_transactionBeginLock);
1341         DELETE_CRITICAL_SECTION(_transactionEndLock);
1342
1343         EM_DEBUG_FUNC_END();
1344 }
1345
1346 INTERNAL_FUNC int emstorage_close(int *err_code)
1347 {
1348         EM_DEBUG_FUNC_BEGIN();
1349
1350         int ret = false;
1351         int error = EMAIL_ERROR_NONE;
1352
1353         emstorage_db_close(&error);
1354
1355         if (--_open_counter == 0)
1356                 _emstorage_close_once();
1357
1358         ret = true;
1359
1360         if (err_code != NULL)
1361                 *err_code = error;
1362
1363         EM_DEBUG_FUNC_END("ret [%d]", ret);
1364         return ret;
1365 }
1366
1367 static void *_emstorage_open_once(int *err_code)
1368 {
1369         EM_DEBUG_FUNC_BEGIN();
1370
1371         int error = EMAIL_ERROR_NONE;
1372
1373         mkdir(USERDATA_PATH, DIRECTORY_PERMISSION);
1374         mkdir(DATA_PATH, DIRECTORY_PERMISSION);
1375         mkdir(EMAILPATH, DIRECTORY_PERMISSION);
1376         mkdir(MAILHOME, DIRECTORY_PERMISSION);
1377         mkdir(MAILTEMP, DIRECTORY_PERMISSION);
1378
1379         _delete_temp_file(MAILTEMP);
1380
1381         g_transaction = false;
1382
1383         if (!emstorage_create_table(EMAIL_CREATE_DB_NORMAL, &error)) {
1384                 EM_DEBUG_EXCEPTION(" emstorage_create_table failed - %d", error);
1385                 goto FINISH_OFF;
1386         }
1387
1388 FINISH_OFF:
1389         if (err_code != NULL)
1390                 *err_code = error;
1391
1392
1393         return NULL;
1394 }
1395
1396  /*  pData : a parameter which is registered when busy handler is registerd */
1397  /*  count : retry count */
1398 #define EMAIL_STORAGE_MAX_RETRY_COUNT   20
1399 static int _callback_sqlite_busy_handler(void *pData, int count)
1400 {
1401         EM_DEBUG_LOG("Busy Handler Called!!: [%d]", count);
1402         usleep(200000);   /*   sleep time when SQLITE_LOCK */
1403
1404         /*  retry will be stopped if  busy handler return 0 */
1405         return EMAIL_STORAGE_MAX_RETRY_COUNT - count;
1406 }
1407
1408 static int _delete_all_files_and_directories(int *err_code)
1409 {
1410         EM_DEBUG_FUNC_BEGIN();
1411
1412         int error = EMAIL_ERROR_NONE;
1413         int ret = false;
1414
1415         if (!emstorage_delete_file(EMAIL_SERVICE_DB_FILE_PATH, &error)) {
1416                 if (error != EMAIL_ERROR_FILE_NOT_FOUND) {
1417                         EM_DEBUG_EXCEPTION("remove failed - %s", EMAIL_SERVICE_DB_FILE_PATH);
1418                         goto FINISH_OFF;
1419                 }
1420         }
1421
1422         if (!emstorage_delete_dir(MAILHOME, &error)) {
1423                 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed");
1424                 goto FINISH_OFF;
1425         }
1426
1427         ret = true;
1428
1429 FINISH_OFF:
1430         if (err_code)
1431                 *err_code = error;
1432         EM_DEBUG_FUNC_END();
1433         return ret;
1434 }
1435
1436 static int _recovery_from_malformed_db_file(int *err_code)
1437 {
1438         EM_DEBUG_FUNC_BEGIN();
1439
1440         int error = EMAIL_ERROR_NONE;
1441         int ret = false;
1442
1443         /* Delete all files and directories */
1444         if (!_delete_all_files_and_directories(&error)) {
1445                 EM_DEBUG_EXCEPTION("_delete_all_files_and_directories failed [%d]", error);
1446                 goto FINISH_OFF;
1447         }
1448
1449         /* Delete all accounts on MyAccount */
1450
1451         /* Delete all managed connection to DB */
1452         emstorage_reset_db_handle_list();
1453
1454         ret = true;
1455
1456 FINISH_OFF:
1457         if (err_code)
1458                 *err_code = error;
1459         EM_DEBUG_FUNC_END();
1460         return ret;
1461 }
1462
1463
1464 INTERNAL_FUNC int em_db_open(sqlite3 **sqlite_handle, int *err_code)
1465 {
1466         EM_DEBUG_FUNC_BEGIN();
1467         int rc = 0;
1468         int error = EMAIL_ERROR_NONE;
1469         int ret = false;
1470
1471         EM_DEBUG_LOG("*sqlite_handle[%p]", *sqlite_handle);
1472
1473         if (*sqlite_handle)  { /*prevent 33351*/
1474                 EM_DEBUG_LOG(">>>>> DB Already Opened......");
1475                 if (err_code != NULL)
1476                         *err_code = error;
1477                 return true;
1478         }
1479
1480         /*  db open */
1481         EM_DEBUG_LOG("Open DB");
1482         EMSTORAGE_PROTECTED_FUNC_CALL(db_util_open(EMAIL_SERVICE_DB_FILE_PATH, sqlite_handle, DB_UTIL_REGISTER_HOOK_METHOD), rc);
1483         if (SQLITE_OK != rc) {
1484                 EM_DEBUG_EXCEPTION("db_util_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1485                 error = EMAIL_ERROR_DB_FAILURE;
1486                 db_util_close(*sqlite_handle);
1487                 *sqlite_handle = NULL;
1488
1489                 if (SQLITE_CORRUPT == rc) /* SQLITE_CORRUPT : The database disk image is malformed */ {/* Recovery DB file */
1490                         EM_DEBUG_LOG("The database disk image is malformed. Trying to remove and create database disk image and directories");
1491                         if (!_recovery_from_malformed_db_file(&error)) {
1492                                 EM_DEBUG_EXCEPTION("_recovery_from_malformed_db_file failed [%d]", error);
1493                                 goto FINISH_OFF;
1494                         }
1495
1496                         EM_DEBUG_LOG("Open DB again");
1497                         EMSTORAGE_PROTECTED_FUNC_CALL(db_util_open(EMAIL_SERVICE_DB_FILE_PATH, sqlite_handle, DB_UTIL_REGISTER_HOOK_METHOD), rc);
1498                         if (SQLITE_OK != rc) {
1499                                 EM_DEBUG_EXCEPTION("db_util_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1500                                 error = EMAIL_ERROR_DB_FAILURE;
1501                                 db_util_close(*sqlite_handle);
1502                                 *sqlite_handle = NULL;
1503                                 goto FINISH_OFF; /*prevent 33351*/
1504                         }
1505                 }
1506                 else
1507                         goto FINISH_OFF;
1508         }
1509         EM_DEBUG_LOG(">>>>> DB Handle : *sqlite_handle[%p]", *sqlite_handle);
1510
1511         /* register busy handler */
1512         EM_DEBUG_LOG(">>>>> Register busy handler.....");
1513         rc = sqlite3_busy_handler(*sqlite_handle, _callback_sqlite_busy_handler, NULL);  /*  Busy Handler registration, NULL is a parameter which will be passed to handler */
1514         if (SQLITE_OK != rc) {
1515                 EM_DEBUG_EXCEPTION("sqlite3_busy_handler fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1516                 error = EMAIL_ERROR_DB_FAILURE;
1517                 db_util_close(*sqlite_handle);
1518                 *sqlite_handle = NULL;
1519                 goto FINISH_OFF;
1520         }
1521
1522
1523         ret = true;
1524
1525 FINISH_OFF:
1526         if (err_code != NULL)
1527                 *err_code = error;
1528
1529         EM_DEBUG_FUNC_END("ret [%d]", ret);
1530         return ret;
1531 }
1532
1533 INTERNAL_FUNC sqlite3* emstorage_db_open(int *err_code)
1534 {
1535         EM_DEBUG_FUNC_BEGIN();
1536 #ifdef _MULTIPLE_DB_HANDLE
1537         sqlite3 *_db_handle = NULL;
1538 #endif
1539
1540         int error = EMAIL_ERROR_NONE;
1541
1542         _initialize_shm_mutex(SHM_FILE_FOR_DB_LOCK, &shm_fd_for_db_lock, &mapped_for_db_lock);
1543
1544 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
1545         _initialize_shm_mutex(SHM_FILE_FOR_MAIL_ID_LOCK, &shm_fd_for_generating_mail_id, &mapped_for_generating_mail_id);
1546 #endif /*__FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
1547
1548         if (!em_db_open(&_db_handle, &error)) {
1549                 EM_DEBUG_EXCEPTION("em_db_open failed[%d]", error);
1550                 goto FINISH_OFF;
1551         }
1552
1553 #ifdef _MULTIPLE_DB_HANDLE
1554         emstorage_set_db_handle(_db_handle);
1555 #endif
1556
1557 FINISH_OFF:
1558         if (err_code != NULL)
1559                 *err_code = error;
1560
1561         EM_DEBUG_FUNC_END("ret [%p]", _db_handle);
1562         return _db_handle;
1563 }
1564
1565 INTERNAL_FUNC int emstorage_db_close(int *err_code)
1566 {
1567         EM_DEBUG_FUNC_BEGIN();
1568 #ifdef _MULTIPLE_DB_HANDLE
1569         sqlite3 *_db_handle = emstorage_get_db_handle();
1570 #endif
1571
1572         int error = EMAIL_ERROR_NONE;
1573         int ret = false;
1574
1575         DELETE_CRITICAL_SECTION(_transactionBeginLock);
1576         DELETE_CRITICAL_SECTION(_transactionEndLock);
1577
1578         if (_db_handle) {
1579                 ret = db_util_close(_db_handle);
1580
1581                 if (ret != SQLITE_OK) {
1582                         EM_DEBUG_EXCEPTION(" db_util_close fail - %d", ret);
1583                         error = EMAIL_ERROR_DB_FAILURE;
1584                         ret = false;
1585                         goto FINISH_OFF;
1586                 }
1587 #ifdef _MULTIPLE_DB_HANDLE
1588                 emstorage_remove_db_handle();
1589 #endif
1590                 _db_handle = NULL;
1591         }
1592
1593         ret = true;
1594
1595 FINISH_OFF:
1596         if (err_code != NULL)
1597                 *err_code = error;
1598
1599         EM_DEBUG_FUNC_END("ret [%d]", ret);
1600         return ret;
1601 }
1602
1603
1604 INTERNAL_FUNC int emstorage_open(int *err_code)
1605 {
1606         EM_DEBUG_FUNC_BEGIN();
1607
1608         int ret = false;
1609         int error = EMAIL_ERROR_NONE;
1610
1611         int retValue;
1612
1613         retValue = mkdir(DB_PATH, DIRECTORY_PERMISSION);
1614
1615         EM_DEBUG_LOG("mkdir return- %d", retValue);
1616         EM_DEBUG_LOG("emstorage_open - before db_util_open - pid = %d", getpid());
1617
1618         if (emstorage_db_open(&error) == NULL) {
1619                 EM_DEBUG_EXCEPTION("emstorage_db_open failed[%d]", error);
1620                 goto FINISH_OFF;
1621         }
1622
1623
1624         if (_open_counter++ == 0)
1625                 _emstorage_open_once(&error);
1626
1627         ret = true;
1628
1629 FINISH_OFF:
1630         if (err_code != NULL)
1631                 *err_code = error;
1632
1633         EM_DEBUG_FUNC_END("ret [%d]", ret);
1634         return ret;
1635 }
1636
1637
1638 INTERNAL_FUNC int emstorage_create_table(emstorage_create_db_type_t type, int *err_code)
1639 {
1640         EM_DEBUG_FUNC_BEGIN();
1641
1642         int error = EMAIL_ERROR_NONE;
1643         int rc = -1, ret = false;
1644         char sql_query_string[QUERY_SIZE] = {0, };
1645         char *create_table_query[] =
1646 {
1647         /*  1. create mail_account_tbl */
1648         "CREATE TABLE mail_account_tbl \n"
1649         "(\n"
1650         "account_id                               INTEGER PRIMARY KEY,\n"
1651         "account_name                             VARCHAR(51),\n"
1652         "logo_icon_path                           VARCHAR(256),\n"
1653         "user_data                                BLOB,\n"
1654         "user_data_length                         INTEGER,\n"
1655         "account_svc_id                           INTEGER,\n"
1656         "sync_status                              INTEGER,\n"
1657         "sync_disabled                            INTEGER,\n"
1658         "default_mail_slot_size                   INTEGER,\n"
1659         "user_display_name                        VARCHAR(31),\n"
1660         "user_email_address                       VARCHAR(129),\n"
1661         "reply_to_address                         VARCHAR(129),\n"
1662         "return_address                           VARCHAR(129),\n"
1663         "incoming_server_type                     INTEGER,\n"
1664         "incoming_server_address                  VARCHAR(51),\n"
1665         "incoming_server_port_number              INTEGER,\n"
1666         "incoming_server_user_name                VARCHAR(51),\n"
1667         "incoming_server_password                 VARCHAR(51),\n"
1668         "incoming_server_secure_connection        INTEGER,\n"
1669         "retrieval_mode                           INTEGER,\n"
1670         "keep_mails_on_pop_server_after_download  INTEGER,\n"
1671         "check_interval                           INTEGER,\n"
1672         "auto_download_size                       INTEGER,\n"
1673         "outgoing_server_type                     INTEGER,\n"
1674         "outgoing_server_address                  VARCHAR(51),\n"
1675         "outgoing_server_port_number              INTEGER,\n"
1676         "outgoing_server_user_name                VARCHAR(51),\n"
1677         "outgoing_server_password                 VARCHAR(51),\n"
1678         "outgoing_server_secure_connection        INTEGER,\n"
1679         "outgoing_server_need_authentication      INTEGER,\n"
1680         "outgoing_server_use_same_authenticator   INTEGER,\n"
1681         "priority                                 INTEGER,\n"
1682         "keep_local_copy                          INTEGER,\n"
1683         "req_delivery_receipt                     INTEGER,\n"
1684         "req_read_receipt                         INTEGER,\n"
1685         "download_limit                           INTEGER,\n"
1686         "block_address                            INTEGER,\n"
1687         "block_subject                            INTEGER,\n"
1688         "display_name_from                        VARCHAR(256),\n"
1689         "reply_with_body                          INTEGER,\n"
1690         "forward_with_files                       INTEGER,\n"
1691         "add_myname_card                          INTEGER,\n"
1692         "add_signature                            INTEGER,\n"
1693         "signature                                VARCHAR(256),\n"
1694         "add_my_address_to_bcc                    INTEGER,\n"
1695         "pop_before_smtp                          INTEGER,\n"
1696         "incoming_server_requires_apop            INTEGER,\n"
1697         "smime_type                               INTEGER,\n"
1698         "certificate_path                         VARCHAR(256),\n"
1699         "cipher_type                              INTEGER,\n"
1700         "digest_type                              INTEGER\n"
1701         "); \n ",
1702         /*  2. create mail_box_tbl */
1703         "CREATE TABLE mail_box_tbl \n"
1704         "(\n"
1705         "  mailbox_id                   INTEGER \n"
1706         ", account_id                   INTEGER \n"
1707         ", local_yn                     INTEGER \n"
1708         ", mailbox_name                 VARCHAR(256) \n"
1709         ", mailbox_type                 INTEGER \n"
1710         ", alias                        VARCHAR(256) \n"
1711         ", deleted_flag          INTEGER \n"
1712         ", modifiable_yn                INTEGER \n"
1713         ", total_mail_count_on_server   INTEGER \n"
1714         ", has_archived_mails           INTEGER \n"
1715         ", mail_slot_size               INTEGER \n"
1716         ", no_select                    INTEGER \n"
1717         ", last_sync_time               DATETIME \n"
1718         "); \n ",
1719
1720         /*  3. create mail_read_mail_uid_tbl */
1721         "CREATE TABLE mail_read_mail_uid_tbl \n"
1722         "(\n"
1723         "  account_id                   INTEGER \n"
1724         ", mailbox_id                   VARCHAR(129) \n"
1725         ", local_uid                    INTEGER \n"
1726         ", mailbox_name                 VARCHAR(256) \n"
1727         ", s_uid                        VARCHAR(129) \n"
1728         ", data1                        INTEGER \n"
1729         ", data2                        VARCHAR(257) \n"
1730         ", flag                         INTEGER \n"
1731         ", idx_num                      INTEGER PRIMARY KEY \n"
1732         "); \n",
1733         /*  4. create mail_rule_tbl */
1734         "CREATE TABLE mail_rule_tbl \n"
1735         "(\n"
1736         "  account_id                   INTEGER \n"
1737         ", rule_id                      INTEGER PRIMARY KEY\n"
1738         ", type                         INTEGER \n"
1739         ", value                        VARCHAR(257) \n"
1740         ", action_type                  INTEGER \n"
1741         ", target_mailbox_id            INTEGER \n"
1742         ", flag1                        INTEGER \n"
1743         ", flag2                        INTEGER \n"
1744         "); \n ",
1745         /*  5. create mail_tbl */
1746         "CREATE TABLE mail_tbl \n"
1747         "(\n"
1748         "  mail_id                      INTEGER PRIMARY KEY \n"
1749         ", account_id                   INTEGER \n"
1750         ", mailbox_id                   INTEGER \n"
1751         ", mailbox_name                 VARCHAR(129) \n"
1752         ", mailbox_type                 INTEGER \n"
1753         ", subject                      TEXT \n"
1754         ", date_time                    DATETIME \n"
1755         ", server_mail_status           INTEGER \n"
1756         ", server_mailbox_name          VARCHAR(129) \n"
1757         ", server_mail_id               VARCHAR(129) \n"
1758         ", message_id                   VARCHAR(257) \n"
1759         ", reference_mail_id            INTEGER \n"
1760         ", full_address_from            TEXT \n"
1761         ", full_address_reply           TEXT \n"
1762         ", full_address_to              TEXT \n"
1763         ", full_address_cc              TEXT \n"
1764         ", full_address_bcc             TEXT \n"
1765         ", full_address_return          TEXT \n"
1766         ", email_address_sender         TEXT collation user1 \n"
1767         ", email_address_recipient      TEXT collation user1 \n"
1768         ", alias_sender                 TEXT \n"
1769         ", alias_recipient              TEXT \n"
1770         ", body_download_status         INTEGER \n"
1771         ", file_path_plain              VARCHAR(257) \n"
1772         ", file_path_html               VARCHAR(257) \n"
1773         ", file_path_mime_entity        VARCHAR(257) \n"
1774         ", mail_size                    INTEGER \n"
1775         ", flags_seen_field             BOOLEAN \n"
1776         ", flags_deleted_field          BOOLEAN \n"
1777         ", flags_flagged_field          BOOLEAN \n"
1778         ", flags_answered_field         BOOLEAN \n"
1779         ", flags_recent_field           BOOLEAN \n"
1780         ", flags_draft_field            BOOLEAN \n"
1781         ", flags_forwarded_field        BOOLEAN \n"
1782         ", DRM_status                   INTEGER \n"
1783         ", priority                     INTEGER \n"
1784         ", save_status                  INTEGER \n"
1785         ", lock_status                  INTEGER \n"
1786         ", report_status                INTEGER \n"
1787         ", attachment_count             INTEGER \n"
1788         ", inline_content_count         INTEGER \n"
1789         ", thread_id                    INTEGER \n"
1790         ", thread_item_count            INTEGER \n"
1791         ", preview_text                 TEXT \n"
1792         ", meeting_request_status       INTEGER \n"
1793         ", message_class                INTEGER \n"
1794         ", digest_type                  INTEGER \n"
1795         ", smime_type                   INTEGER \n"
1796         ", FOREIGN KEY(account_id)      REFERENCES mail_account_tbl(account_id) \n"
1797         "); \n ",
1798
1799         /*  6. create mail_attachment_tbl */
1800         "CREATE TABLE mail_attachment_tbl \n"
1801         "(\n"
1802         "  attachment_id                    INTEGER PRIMARY KEY"
1803         ", attachment_name                  VARCHAR(257) \n"
1804         ", attachment_path                  VARCHAR(257) \n"
1805         ", attachment_size                  INTEGER \n"
1806         ", mail_id                          INTEGER \n"
1807         ", account_id                       INTEGER \n"
1808         ", mailbox_id                       INTEGER \n"
1809         ", attachment_save_status           INTEGER \n"
1810         ", attachment_drm_type              INTEGER \n"
1811         ", attachment_drm_method            INTEGER \n"
1812         ", attachment_inline_content_status INTEGER \n"
1813         ", attachment_mime_type             VARCHAR(257) \n"
1814 #ifdef __ATTACHMENT_OPTI__
1815         ", encoding                     INTEGER \n"
1816         ", section                      varchar(257) \n"
1817 #endif
1818         "); \n ",
1819
1820 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
1821
1822         "CREATE TABLE mail_partial_body_activity_tbl  \n"
1823         "( \n"
1824         "  account_id                   INTEGER \n"
1825         ", mail_id                      INTEGER \n"
1826         ", server_mail_id               INTEGER \n"
1827         ", activity_id                  INTEGER PRIMARY KEY \n"
1828         ", activity_type                INTEGER \n"
1829         ", mailbox_id                   INTEGER \n"
1830         ", mailbox_name                 VARCHAR(4000) \n"
1831         "); \n ",
1832 #endif
1833
1834         "CREATE TABLE mail_meeting_tbl \n"
1835         "(\n"
1836         "  mail_id                     INTEGER PRIMARY KEY \n"
1837         ", account_id                  INTEGER \n"
1838         ", mailbox_id                  INTEGER \n"
1839         ", meeting_response            INTEGER \n"
1840         ", start_time                  INTEGER \n"
1841         ", end_time                    INTEGER \n"
1842         ", location                    TEXT \n"
1843         ", global_object_id            TEXT \n"
1844         ", offset                      INTEGER \n"
1845         ", standard_name               TEXT \n"
1846         ", standard_time_start_date    INTEGER \n"
1847         ", standard_bias               INTEGER \n"
1848         ", daylight_name               TEXT \n"
1849         ", daylight_time_start_date    INTEGER \n"
1850         ", daylight_bias               INTEGER \n"
1851         "); \n ",
1852
1853 #ifdef __FEATURE_LOCAL_ACTIVITY__
1854         "CREATE TABLE mail_local_activity_tbl \n"
1855         "( \n"
1856         "  activity_id                 INTEGER \n"
1857         ", account_id                  INTEGER \n"
1858         ", mail_id                     INTEGER \n"
1859         ", activity_type               INTEGER \n"
1860         ", server_mailid               VARCHAR(129) \n"
1861         ", src_mbox                    VARCHAR(129) \n"
1862         ", dest_mbox                   VARCHAR(129) \n"
1863         "); \n ",
1864 #endif
1865         "CREATE TABLE mail_certificate_tbl \n"
1866         "( \n"
1867         "  certificate_id              INTEGER \n"
1868         ", issue_year                  INTEGER \n"
1869         ", issue_month                 INTEGER \n"
1870         ", issue_day                   INTEGER \n"
1871         ", expiration_year             INTEGER \n"
1872         ", expiration_month            INTEGER \n"
1873         ", expiration_day              INTEGER \n"
1874         ", issue_organization_name     VARCHAR(256) \n"
1875         ", email_address               VARCHAR(129) \n"
1876         ", subject_str                 VARCHAR(256) \n"
1877         ", filepath                    VARCHAR(256) \n"
1878         ", password                    VARCHAR(51) \n"
1879         "); \n ",
1880
1881         "CREATE TABLE mail_task_tbl \n"
1882         "(\n"
1883         "task_id                          INTEGER PRIMARY KEY, \n"
1884         "task_type                        INTEGER, \n"
1885         "task_status                      INTEGER, \n"
1886         "task_priority                    INTEGER, \n"
1887         "task_parameter_length            INTEGER,  \n"
1888         "task_parameter                   BLOB, \n"
1889         "date_time                        DATETIME \n"
1890         "); \n",
1891         NULL,
1892 };
1893
1894         sqlite3 *local_db_handle = emstorage_get_db_connection();
1895
1896         EM_DEBUG_LOG("local_db_handle = %p.", local_db_handle);
1897
1898         char *sql;
1899         char **result;
1900
1901         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_account_tbl';";
1902         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
1903         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1904                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
1905
1906         EM_DEBUG_LOG("emstorage_create_table - result[1] = %s %c", result[1], result[1]);
1907
1908         if (atoi(result[1]) < 1) {
1909                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
1910                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1911                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
1912
1913                 EM_DEBUG_LOG("CREATE TABLE mail_account_tbl");
1914
1915                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
1916
1917                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1918                 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)));
1919
1920                 /*  create mail_account_tbl unique index */
1921                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE UNIQUE INDEX mail_account_idx1 ON mail_account_tbl (account_id)");
1922
1923                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1924                 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)));
1925
1926                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
1927
1928         } /*  mail_account_tbl */
1929         else if (type == EMAIL_CREATE_DB_CHECK)  {
1930                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], NULL, NULL, NULL), rc);
1931                 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)));
1932         }
1933
1934         sqlite3_free_table(result);
1935
1936
1937         /*  2. create mail_box_tbl */
1938         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_box_tbl';";
1939
1940         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
1941         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1942                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
1943
1944         if (atoi(result[1]) < 1) {
1945                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
1946                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1947                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
1948
1949                 EM_DEBUG_LOG("CREATE TABLE mail_box_tbl");
1950
1951                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_BOX_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
1952
1953                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1954                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1955                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1956
1957                 /*  create mail_local_mailbox_tbl unique index */
1958                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE UNIQUE INDEX mail_box_idx1 ON mail_box_tbl (account_id, local_yn, mailbox_name)");
1959
1960                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1961                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1962                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1963
1964                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
1965
1966         } /*  mail_box_tbl */
1967         else if (type == EMAIL_CREATE_DB_CHECK)  {
1968                 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_BOX_TBL], NULL, NULL, NULL);
1969                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1970                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_BOX_TBL], rc, sqlite3_errmsg(local_db_handle)));
1971         }
1972         sqlite3_free_table(result);
1973
1974         /*  3. create mail_read_mail_uid_tbl */
1975         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_read_mail_uid_tbl';";
1976         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
1977         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1978                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
1979
1980
1981         if (atoi(result[1]) < 1) {
1982                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
1983                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1984                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
1985
1986                 EM_DEBUG_LOG("CREATE TABLE mail_read_mail_uid_tbl");
1987
1988                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
1989
1990                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1991                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1992                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1993
1994                 /*  create mail_read_mail_uid_tbl unique index */
1995                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE UNIQUE INDEX mail_read_mail_uid_idx1 ON mail_read_mail_uid_tbl (account_id, mailbox_id, local_uid, mailbox_name, s_uid)");
1996
1997                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1998                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
1999                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2000                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2001
2002         } /*  mail_read_mail_uid_tbl */
2003         else if (type == EMAIL_CREATE_DB_CHECK)  {
2004                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], NULL, NULL, NULL), rc);
2005                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2006                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], rc, sqlite3_errmsg(local_db_handle)));
2007         }
2008         sqlite3_free_table(result);
2009
2010
2011         /*  4. create mail_rule_tbl */
2012         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_rule_tbl';";
2013
2014         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2015         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2016                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2017
2018         if (atoi(result[1]) < 1) {
2019                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2020                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2021                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2022
2023                 EM_DEBUG_LOG("CREATE TABLE mail_rule_tbl");
2024
2025                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_RULE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2026
2027                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2028                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2029                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2030                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2031
2032         } /*  mail_rule_tbl */
2033         else if (type == EMAIL_CREATE_DB_CHECK)  {
2034                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_RULE_TBL], NULL, NULL, NULL), rc);
2035                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2036                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_RULE_TBL], rc, sqlite3_errmsg(local_db_handle)));
2037         }
2038         sqlite3_free_table(result);
2039
2040
2041         /*  5. create mail_tbl */
2042         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_tbl';";
2043         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2044         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2045                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2046
2047         if (atoi(result[1]) < 1) {
2048                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2049                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2050                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2051                 EM_DEBUG_LOG("CREATE TABLE mail_tbl");
2052
2053                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2054
2055                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2056                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2057                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2058
2059                 /*  create mail_tbl unique index */
2060                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE UNIQUE INDEX mail_idx1 ON mail_tbl (mail_id, account_id)");
2061                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2062                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2063                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2064
2065                 /*  create mail_tbl index for date_time */
2066                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE INDEX mail_idx_date_time ON mail_tbl (date_time)");
2067                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2068                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2069                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2070
2071                 /*  create mail_tbl index for thread_item_count */
2072                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE INDEX mail_idx_thread_mail_count ON mail_tbl (thread_item_count)");
2073                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2074                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2075                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2076
2077                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2078
2079                 /*  just one time call */
2080 /*              EFTSInitFTSIndex(FTS_EMAIL_IDX); */
2081         } /*  mail_tbl */
2082         else if (type == EMAIL_CREATE_DB_CHECK)  {
2083                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TBL], NULL, NULL, NULL), rc);
2084                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2085                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TBL], rc, sqlite3_errmsg(local_db_handle)));
2086         }
2087         sqlite3_free_table(result);
2088
2089
2090         /*  6. create mail_attachment_tbl */
2091         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_attachment_tbl';";
2092         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2093         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2094                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2095
2096         if (atoi(result[1]) < 1) {
2097                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2098                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2099                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2100                 EM_DEBUG_LOG("CREATE TABLE mail_attachment_tbl");
2101
2102                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2103
2104                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2105                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2106                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2107
2108                 /*  create mail_attachment_tbl unique index */
2109                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE UNIQUE INDEX mail_attachment_idx1 ON mail_attachment_tbl (mail_id, attachment_id) ");
2110
2111                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2112                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2113                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2114                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2115
2116         } /*  mail_attachment_tbl */
2117         else if (type == EMAIL_CREATE_DB_CHECK)  {
2118                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], NULL, NULL, NULL), rc);
2119                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2120                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], rc, sqlite3_errmsg(local_db_handle)));
2121         }
2122         sqlite3_free_table(result);
2123
2124 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
2125
2126         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_partial_body_activity_tbl';";
2127         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2128         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2129                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2130
2131         if (atoi(result[1]) < 1) {
2132
2133                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2134                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2135                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2136
2137                 EM_DEBUG_LOG("CREATE TABLE mail_partial_body_activity_tbl");
2138
2139                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2140
2141                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2142                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2143                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2144                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2145
2146         } /*  mail_rule_tbl */
2147         else if (type == EMAIL_CREATE_DB_CHECK)  {
2148                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2149                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2150                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2151         }
2152         sqlite3_free_table(result);
2153
2154 #endif /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
2155
2156         /*  create mail_meeting_tbl */
2157         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_meeting_tbl';";
2158         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2159         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2160                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2161
2162         if (atoi(result[1]) < 1) {
2163                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2164                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2165                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2166
2167                 EM_DEBUG_LOG("CREATE TABLE mail_meeting_tbl");
2168
2169                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_MEETING_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2170
2171                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2172                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2173                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2174                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE UNIQUE INDEX mail_meeting_idx1 ON mail_meeting_tbl (mail_id)");
2175
2176                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2177                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2178                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2179                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2180
2181         } /*  mail_contact_sync_tbl */
2182         else if (type == EMAIL_CREATE_DB_CHECK)  {
2183                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], NULL, NULL, NULL), rc);
2184                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2185                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], rc, sqlite3_errmsg(local_db_handle)));
2186         }
2187         sqlite3_free_table(result);
2188
2189 #ifdef __FEATURE_LOCAL_ACTIVITY__
2190
2191                 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_local_activity_tbl';";
2192                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2193                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2194                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2195
2196                 if (atoi(result[1]) < 1) {
2197
2198                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2199                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2200                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2201
2202                         EM_DEBUG_LOG(" CREATE TABLE mail_local_activity_tbl");
2203
2204                         SNPRINTF(sql_query_string, sizeof(sql_query_string), create_table_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL]);
2205
2206                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2207                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2208                                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2209                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2210
2211                 } /*  mail_rule_tbl */
2212                 else if (type == EMAIL_CREATE_DB_CHECK)  {
2213                         rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL);
2214                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2215                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2216                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2217                 }
2218                 sqlite3_free_table(result);
2219
2220 #endif /*  __FEATURE_LOCAL_ACTIVITY__ */
2221         /*  create mail_certificate_tbl */
2222         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_certificate_tbl';";
2223         /*  rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL);   */
2224         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2225         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)));
2226
2227         if (atoi(result[1]) < 1) {
2228                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2229                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2230
2231                 EM_DEBUG_LOG("CREATE TABLE mail_certificate_tbl");
2232
2233                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2234
2235                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2236                 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)));
2237                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE UNIQUE INDEX mail_certificate_idx1 ON mail_certificate_tbl (certificate_id)");
2238
2239                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2240                 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)));
2241                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2242         } /*  mail_contact_sync_tbl */
2243         else if (type == EMAIL_CREATE_DB_CHECK)  {
2244                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], NULL, NULL, NULL), rc);
2245                 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)));
2246         }
2247
2248         /*  create mail_task_tbl */
2249         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_task_tbl';";
2250         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2251         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2252
2253         if (atoi(result[1]) < 1) {
2254                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2255                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2256
2257                 EM_DEBUG_LOG("CREATE TABLE mail_certificate_tbl");
2258
2259                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TASK_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2260
2261                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2262                 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)));
2263                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE UNIQUE INDEX mail_certificate_idx1 ON mail_certificate_tbl (certificate_id)");
2264
2265                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2266                 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)));
2267                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2268         } /*  mail_task_tbl */
2269         else if (type == EMAIL_CREATE_DB_CHECK)  {
2270                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], NULL, NULL, NULL), rc);
2271                 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)));
2272         }
2273
2274         sqlite3_free_table(result);
2275
2276         ret = true;
2277
2278 FINISH_OFF:
2279         if (ret == true) {
2280                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2281         }
2282         else {
2283                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
2284         }
2285
2286         _DISCONNECT_DB;
2287
2288         if (err_code != NULL)
2289                 *err_code = error;
2290
2291         EM_DEBUG_FUNC_END("ret [%d]", ret);
2292         return ret;
2293 }
2294
2295 /* Query series --------------------------------------------------------------*/
2296
2297 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)
2298 {
2299         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);
2300         int rc = -1;
2301         int error = EMAIL_ERROR_NONE;
2302         DB_STMT hStmt = NULL;
2303         char sql_query_string[QUERY_SIZE] = {0, };
2304         char err_msg[1024];
2305         char **result;
2306         sqlite3 *local_db_handle = NULL;
2307
2308         if (!input_conditional_clause || (!output_total_mail_count && !output_unseen_mail_count)) {
2309                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2310                 return EMAIL_ERROR_INVALID_PARAM;
2311         }
2312
2313         memset(&sql_query_string, 0x00, sizeof(sql_query_string));
2314         local_db_handle = emstorage_get_db_connection();
2315
2316         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2317
2318         SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT COUNT(*) FROM mail_tbl");
2319         EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
2320
2321         if (output_total_mail_count)  {
2322                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
2323                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2324                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2325
2326                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2327                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2328                         ("sqlite3_step fail:%d", rc));
2329                 _get_stmt_field_data_int(hStmt, output_total_mail_count, 0);
2330         }
2331
2332         if (output_unseen_mail_count)  {
2333                 EM_SAFE_STRCAT(sql_query_string, " AND flags_seen_field = 0 ");
2334
2335                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
2336                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2337                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2338
2339                 *output_unseen_mail_count = atoi(result[1]);
2340                 sqlite3_free_table(result);
2341         }
2342
2343 FINISH_OFF:
2344
2345         if (hStmt != NULL)  {
2346                 rc = sqlite3_finalize(hStmt);
2347                 if (rc != SQLITE_OK)  {
2348                         EM_DEBUG_LOG("sqlite3_finalize failed [%d] : %s", rc, err_msg);
2349                         error = EMAIL_ERROR_DB_FAILURE;
2350                 }
2351         }
2352
2353         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2354         _DISCONNECT_DB;
2355
2356         EM_DEBUG_FUNC_END("error [%d]", error);
2357         return error;
2358 }
2359
2360 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)
2361 {
2362         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);
2363
2364         int      i = 0;
2365         int      count = 0;
2366         int      rc = -1;
2367         int      cur_query = 0;
2368         int      col_index;
2369         int      error = EMAIL_ERROR_NONE;
2370         int     *result_mail_id_list = NULL;
2371         char   **result = NULL;
2372         char     sql_query_string[QUERY_SIZE] = {0, };
2373         sqlite3 *local_db_handle = emstorage_get_db_connection();
2374
2375         EM_IF_NULL_RETURN_VALUE(input_conditional_clause, EMAIL_ERROR_INVALID_PARAM);
2376         EM_IF_NULL_RETURN_VALUE(output_mail_id_list, EMAIL_ERROR_INVALID_PARAM);
2377         EM_IF_NULL_RETURN_VALUE(output_mail_id_count, EMAIL_ERROR_INVALID_PARAM);
2378
2379         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2380
2381         /* Composing query */
2382         SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, "SELECT mail_id FROM mail_tbl ");
2383         EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
2384
2385         EM_DEBUG_LOG("query[%s].", sql_query_string);
2386
2387         /* Performing query */
2388         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2389         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2390                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2391
2392         col_index = 1;
2393
2394         /*  to get mail list */
2395         if (count == 0) {
2396                 EM_DEBUG_EXCEPTION("No mail found...");
2397                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2398                 goto FINISH_OFF;
2399         }
2400
2401         EM_DEBUG_LOG("There are [%d] mails.", count);
2402
2403         if (!(result_mail_id_list = (int*)em_malloc(sizeof(int) * count))) {
2404                 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
2405                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2406                 goto FINISH_OFF;
2407         }
2408
2409         EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2410
2411         for (i = 0; i < count; i++)
2412                 _get_table_field_data_int(result, result_mail_id_list + i, col_index++);
2413
2414         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >> ", count);
2415
2416         *output_mail_id_list  = result_mail_id_list;
2417         *output_mail_id_count = count;
2418
2419 FINISH_OFF:
2420
2421         if(result)
2422                 sqlite3_free_table(result);
2423
2424         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2425         _DISCONNECT_DB;
2426
2427         if(error != EMAIL_ERROR_NONE)
2428                 EM_SAFE_FREE(result_mail_id_list);
2429
2430         EM_DEBUG_FUNC_END("error [%d]", error);
2431         return error;
2432 }
2433
2434 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)
2435 {
2436         EM_DEBUG_FUNC_BEGIN();
2437         EM_PROFILE_BEGIN(emstorage_query_mail_list_func);
2438
2439         int i = 0, count = 0, rc = -1, to_get_count = (result_mail_list)?0:1;
2440         int local_inline_content_count = 0, local_attachment_count = 0;
2441         int cur_query = 0, base_count = 0, col_index;
2442         int ret = false, error = EMAIL_ERROR_NONE;
2443         char *date_time_string = NULL;
2444         char **result = NULL, sql_query_string[QUERY_SIZE] = {0, };
2445         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 ";
2446         email_mail_list_item_t *mail_list_item_from_tbl = NULL;
2447         sqlite3 *local_db_handle = emstorage_get_db_connection();
2448
2449         EM_IF_NULL_RETURN_VALUE(conditional_clause, false);
2450         EM_IF_NULL_RETURN_VALUE(result_count, false);
2451
2452         EMSTORAGE_START_READ_TRANSACTION(transaction);
2453
2454         /*  select clause */
2455         if (to_get_count) /*  count only */
2456                 cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, "SELECT mail_id FROM mail_tbl");
2457         else /*  mail list in plain form */
2458                 cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, "SELECT %s FROM mail_tbl ", field_list);
2459
2460         /* cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, conditional_clause); This code caused some crashes.*/
2461         strncat(sql_query_string, conditional_clause, QUERY_SIZE - cur_query);
2462
2463         EM_DEBUG_LOG("emstorage_query_mail_list : query[%s].", sql_query_string);
2464
2465         /*  performing query            */
2466         EM_PROFILE_BEGIN(emstorage_query_mail_list_performing_query);
2467         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2468         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2469                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2470         EM_PROFILE_END(emstorage_query_mail_list_performing_query);
2471
2472         if (!base_count)
2473                 base_count = ({ int i=0; char *tmp = field_list; for (i=0; tmp && *(tmp + 1); tmp = index(tmp + 1, ','), i++); i; });
2474
2475         col_index = base_count;
2476
2477         EM_DEBUG_LOG("base_count [%d]", base_count);
2478
2479         if (to_get_count) {
2480                 /*  to get count */
2481                 if (!count) {
2482                         EM_DEBUG_EXCEPTION("No mail found...");
2483                         ret = false;
2484                         error= EMAIL_ERROR_MAIL_NOT_FOUND;
2485                         goto FINISH_OFF;
2486                 }
2487                 EM_DEBUG_LOG("There are [%d] mails.", count);
2488         }
2489         else {
2490                 /*  to get mail list */
2491                 if (!count) {
2492                         EM_DEBUG_EXCEPTION("No mail found...");
2493                         ret = false;
2494                         error= EMAIL_ERROR_MAIL_NOT_FOUND;
2495                         goto FINISH_OFF;
2496                 }
2497
2498                 EM_DEBUG_LOG("There are [%d] mails.", count);
2499                 if (!(mail_list_item_from_tbl = (email_mail_list_item_t*)em_malloc(sizeof(email_mail_list_item_t) * count))) {
2500                         EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
2501                         error = EMAIL_ERROR_OUT_OF_MEMORY;
2502                         goto FINISH_OFF;
2503                 }
2504
2505                 EM_PROFILE_BEGIN(emstorage_query_mail_list_loop);
2506                 EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2507                 for (i = 0; i < count; i++) {
2508                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_id), col_index++);
2509                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].account_id), col_index++);
2510                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mailbox_id), col_index++);
2511                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].full_address_from, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2512                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_sender, MAX_EMAIL_ADDRESS_LENGTH, 1, col_index++);
2513                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_recipient, STRING_LENGTH_FOR_DISPLAY,  1, col_index++);
2514                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].subject, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2515                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].body_download_status), col_index++);
2516                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_seen_field), col_index++);
2517                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_deleted_field), col_index++);
2518                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_flagged_field), col_index++);
2519                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_answered_field), col_index++);
2520                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_recent_field), col_index++);
2521                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_draft_field), col_index++);
2522                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_forwarded_field), col_index++);
2523                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].DRM_status), col_index++);
2524                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].priority), col_index++);
2525                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].save_status), col_index++);
2526                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].lock_status), col_index++);
2527                         _get_table_field_data_int(result, &local_attachment_count, col_index++);
2528                         _get_table_field_data_int(result, &local_inline_content_count, col_index++);
2529                         _get_table_field_data_time_t(result, &(mail_list_item_from_tbl[i].date_time), col_index++);
2530                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].preview_text, MAX_PREVIEW_TEXT_LENGTH, 1, col_index++);
2531                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_id), col_index++);
2532                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_item_count), col_index++);
2533                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].meeting_request_status), col_index++);
2534                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].message_class), col_index++);
2535                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].smime_type), col_index++);
2536
2537                         mail_list_item_from_tbl[i].attachment_count = ((local_attachment_count - local_inline_content_count)>0)?1:0;
2538                 }
2539                 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >> ", count);
2540                 EM_PROFILE_END(emstorage_query_mail_list_loop);
2541         }
2542
2543         sqlite3_free_table(result);
2544         result = NULL;
2545         ret = true;
2546
2547 FINISH_OFF:
2548         EM_DEBUG_LOG("COUNT [%d]", count);
2549
2550         if (to_get_count)
2551                 *result_count = count;
2552         else {
2553                 if (ret == true)  {
2554                         if (result_mail_list)
2555                                 *result_mail_list = mail_list_item_from_tbl;
2556                         *result_count = count;
2557                 }
2558                 else
2559                         EM_SAFE_FREE(mail_list_item_from_tbl);
2560         }
2561
2562         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2563
2564         sqlite3_release_memory(-1);
2565
2566         _DISCONNECT_DB;
2567
2568         EM_SAFE_FREE(date_time_string);
2569
2570         if (err_code != NULL)
2571                 *err_code = error;
2572
2573         EM_PROFILE_END(emstorage_query_mail_list_func);
2574         EM_DEBUG_FUNC_END("ret [%d]", ret);
2575         return ret;
2576 }
2577
2578
2579 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)
2580 {
2581         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);
2582
2583         if (!conditional_clause) {
2584                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2585
2586                 if (err_code != NULL)
2587                         *err_code = EMAIL_ERROR_INVALID_PARAM;
2588                 return false;
2589         }
2590
2591         int i, col_index = FIELD_COUNT_OF_MAIL_TBL, rc, ret = false, count;
2592         int error = EMAIL_ERROR_NONE;
2593         char **result = NULL, sql_query_string[QUERY_SIZE] = {0, };
2594         emstorage_mail_tbl_t* p_data_tbl = NULL;
2595         sqlite3 *local_db_handle = emstorage_get_db_connection();
2596
2597         EMSTORAGE_START_READ_TRANSACTION(transaction);
2598
2599         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl %s", conditional_clause);
2600
2601         EM_DEBUG_LOG("Query [%s]", sql_query_string);
2602
2603         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2604         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2605                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2606
2607         if (!count) {
2608                 EM_DEBUG_EXCEPTION("No mail found...");
2609                 ret = false;
2610                 error= EMAIL_ERROR_MAIL_NOT_FOUND;
2611                 goto FINISH_OFF;
2612         }
2613
2614         EM_DEBUG_LOG("There are [%d] mails.", count);
2615         if (!(p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * count))) {
2616                 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
2617                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2618                 goto FINISH_OFF;
2619         }
2620
2621         EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2622         for (i = 0; i < count; i++) {
2623                 _get_table_field_data_int   (result, &(p_data_tbl[i].mail_id), col_index++);
2624                 _get_table_field_data_int   (result, &(p_data_tbl[i].account_id), col_index++);
2625                 _get_table_field_data_int   (result, &(p_data_tbl[i].mailbox_id), col_index++);
2626                 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
2627                 _get_table_field_data_int   (result, &(p_data_tbl[i].mailbox_type), col_index++);
2628                 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 1, col_index++);
2629                 _get_table_field_data_time_t (result, &(p_data_tbl[i].date_time), col_index++);
2630                 _get_table_field_data_int   (result, &(p_data_tbl[i].server_mail_status), col_index++);
2631                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
2632                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
2633                 _get_table_field_data_string(result, &(p_data_tbl[i].message_id), 0, col_index++);
2634                 _get_table_field_data_int   (result, &(p_data_tbl[i].reference_mail_id), col_index++);
2635                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_from), 1, col_index++);
2636                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_reply), 1, col_index++);
2637                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_to), 1, col_index++);
2638                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_cc), 1, col_index++);
2639                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_bcc), 1, col_index++);
2640                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_return), 1, col_index++);
2641                 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_sender), 1, col_index++);
2642                 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_recipient), 1, col_index++);
2643                 _get_table_field_data_string(result, &(p_data_tbl[i].alias_sender), 1, col_index++);
2644                 _get_table_field_data_string(result, &(p_data_tbl[i].alias_recipient), 1, col_index++);
2645                 _get_table_field_data_int   (result, &(p_data_tbl[i].body_download_status), col_index++);
2646                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
2647                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
2648                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
2649                 _get_table_field_data_int   (result, &(p_data_tbl[i].mail_size), col_index++);
2650                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_seen_field), col_index++);
2651                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_deleted_field), col_index++);
2652                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_flagged_field), col_index++);
2653                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_answered_field), col_index++);
2654                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_recent_field), col_index++);
2655                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_draft_field), col_index++);
2656                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_forwarded_field), col_index++);
2657                 _get_table_field_data_int   (result, &(p_data_tbl[i].DRM_status), col_index++);
2658                 _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].priority), col_index++);
2659                 _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].save_status), col_index++);
2660                 _get_table_field_data_int   (result, &(p_data_tbl[i].lock_status), col_index++);
2661                 _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].report_status), col_index++);
2662                 _get_table_field_data_int   (result, &(p_data_tbl[i].attachment_count), col_index++);
2663                 _get_table_field_data_int   (result, &(p_data_tbl[i].inline_content_count), col_index++);
2664                 _get_table_field_data_int   (result, &(p_data_tbl[i].thread_id), col_index++);
2665                 _get_table_field_data_int   (result, &(p_data_tbl[i].thread_item_count), col_index++);
2666                 _get_table_field_data_string(result, &(p_data_tbl[i].preview_text), 1, col_index++);
2667                 _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].meeting_request_status), col_index++);
2668                 _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].message_class), col_index++);
2669                 _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].digest_type), col_index++);
2670                 _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].smime_type), col_index++);
2671                 /*  check real body file... */
2672                 if (p_data_tbl[i].body_download_status) {
2673                         struct stat buf;
2674
2675                         if (p_data_tbl[i].file_path_html && EM_SAFE_STRLEN(p_data_tbl[i].file_path_html) > 0) {
2676                                 if (stat(p_data_tbl[i].file_path_html, &buf) == -1) {
2677                                         EM_DEBUG_LINE;
2678                                         p_data_tbl[i].body_download_status = 0;
2679                                 }
2680                         }
2681                         else if (p_data_tbl[i].file_path_plain && EM_SAFE_STRLEN(p_data_tbl[i].file_path_plain) > 0) {
2682                                 if (stat(p_data_tbl[i].file_path_plain, &buf) == -1){
2683                                         EM_DEBUG_LINE;
2684                                         p_data_tbl[i].body_download_status = 0;
2685                                 }
2686                         }
2687                         else
2688                                 p_data_tbl[i].body_download_status = 0;
2689                 }
2690         }
2691
2692         ret = true;
2693
2694 FINISH_OFF:
2695         if(result)
2696                 sqlite3_free_table(result);
2697
2698         if (ret == true)  {
2699                 if (result_mail_tbl)
2700                         *result_mail_tbl = p_data_tbl;
2701                 *result_count = count;
2702         }
2703         else
2704                 EM_SAFE_FREE(p_data_tbl);
2705
2706         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2707
2708         sqlite3_release_memory(-1);
2709
2710         _DISCONNECT_DB;
2711
2712         if (err_code != NULL)
2713                 *err_code = error;
2714
2715         EM_DEBUG_FUNC_END("ret [%d]", ret);
2716         return ret;
2717 }
2718
2719 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)
2720 {
2721         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);
2722
2723         int i = 0;
2724         int rc;
2725         int count = 0;
2726         int col_index = 0;
2727         int error = EMAIL_ERROR_NONE;
2728         int read_count = 0;
2729         int total_count = 0;
2730         char **result;
2731         char sql_query_string[QUERY_SIZE] = {0, };
2732         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 ";
2733         emstorage_mailbox_tbl_t* p_data_tbl = NULL;
2734         sqlite3 *local_db_handle = emstorage_get_db_connection();
2735
2736         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2737
2738         if (input_get_mail_count == 0) {        /* without mail count */
2739                 col_index = 13;
2740                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT %s FROM mail_box_tbl AS MBT %s", fields, input_conditional_clause);
2741         }
2742         else {  /* with read count and total count */
2743                 col_index = 15;
2744                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT %s, total, read  FROM mail_box_tbl AS MBT LEFT OUTER JOIN (SELECT mailbox_name, count(mail_id) AS total, SUM(flags_seen_field) AS read FROM mail_tbl %s GROUP BY mailbox_name) AS MT ON MBT.mailbox_name = MT.mailbox_name %s %s", fields, input_conditional_clause, input_conditional_clause, input_ordering_clause);
2745         }
2746
2747         EM_DEBUG_LOG("query[%s]", sql_query_string);
2748
2749         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
2750         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2751                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)))
2752
2753         EM_DEBUG_LOG("result count [%d]", count);
2754
2755         if(count == 0) {
2756                 EM_DEBUG_EXCEPTION("Can't find mailbox");
2757                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
2758                 goto FINISH_OFF;
2759         }
2760
2761         if ((p_data_tbl = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t) * count)) == NULL) {
2762                 EM_DEBUG_EXCEPTION("em_malloc failed...");
2763                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2764                 goto FINISH_OFF;
2765         }
2766
2767         for (i = 0; i < count; i++)  {
2768                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
2769                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
2770                 _get_table_field_data_int(result, &(p_data_tbl[i].local_yn), col_index++);
2771                 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
2772                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].mailbox_type), col_index++);
2773                 _get_table_field_data_string(result, &(p_data_tbl[i].alias), 0, col_index++);
2774                 _get_table_field_data_int(result, &(p_data_tbl[i].deleted_flag), col_index++);
2775                 _get_table_field_data_int(result, &(p_data_tbl[i].modifiable_yn), col_index++);
2776                 _get_table_field_data_int(result, &(p_data_tbl[i].total_mail_count_on_server), col_index++);
2777                 _get_table_field_data_int(result, &(p_data_tbl[i].has_archived_mails), col_index++);
2778                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_slot_size), col_index++);
2779                 _get_table_field_data_int(result, &(p_data_tbl[i].no_select), col_index++);
2780                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].last_sync_time), col_index++);
2781
2782                 if (input_get_mail_count == 1) {
2783                         _get_table_field_data_int(result, &(total_count), col_index++);
2784                         p_data_tbl[i].total_mail_count_on_local = total_count;
2785                         _get_table_field_data_int(result, &(read_count), col_index++);
2786                         p_data_tbl[i].unread_count = total_count - read_count;                  /*  return unread count, NOT  */
2787                 }
2788         }
2789
2790
2791 FINISH_OFF:
2792         if (result)
2793                 sqlite3_free_table(result);
2794
2795         if (error == EMAIL_ERROR_NONE)  {
2796                 *output_mailbox_list = p_data_tbl;
2797                 *output_mailbox_count = count;
2798                 EM_DEBUG_LOG("Mailbox Count [ %d]", count);
2799         }
2800
2801         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2802
2803         sqlite3_release_memory(-1);
2804
2805         _DISCONNECT_DB;
2806
2807         EM_DEBUG_FUNC_END("error [%d]", error);
2808         return error;
2809 }
2810
2811 /* Query series --------------------------------------------------------------*/
2812
2813 INTERNAL_FUNC int emstorage_check_duplicated_account(email_account_t* account, int transaction, int *err_code)
2814 {
2815         EM_DEBUG_FUNC_BEGIN();
2816
2817         if (!account)  {
2818                 if (err_code != NULL)
2819                         *err_code = EMAIL_ERROR_INVALID_PARAM;
2820                 return false;
2821         }
2822
2823         int rc = -1, ret = false;
2824         int error = EMAIL_ERROR_NONE;
2825         char **result;
2826         int count;
2827         char sql_query_string[QUERY_SIZE] = {0, };
2828
2829         sqlite3 *local_db_handle = emstorage_get_db_connection();
2830         EMSTORAGE_START_READ_TRANSACTION(transaction);
2831
2832         SNPRINTF(sql_query_string, sizeof(sql_query_string),
2833                 "SELECT COUNT(*) FROM mail_account_tbl "
2834                 " WHERE "
2835                 " user_email_address = '%s' AND "
2836                 " incoming_server_user_name = '%s' AND "
2837                 " incoming_server_type = %d AND "
2838                 " incoming_server_address = '%s' AND "
2839                 " outgoing_server_user_name = '%s' AND "
2840                 " outgoing_server_type = %d AND "
2841                 " outgoing_server_address = '%s'; ",
2842                 account->user_email_address,
2843                 account->incoming_server_user_name, account->incoming_server_type, account->incoming_server_address,
2844                 account->outgoing_server_user_name, account->outgoing_server_type, account->outgoing_server_address
2845         );
2846         EM_DEBUG_LOG("Query[%s]", sql_query_string);
2847         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
2848         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2849                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2850
2851         count = atoi(result[1]);
2852         sqlite3_free_table(result);
2853
2854         EM_DEBUG_LOG("Count of Duplicated Account Information: [%d]", count);
2855
2856         if (count == 0) {       /*  not duplicated account */
2857                 ret = true;
2858                 EM_DEBUG_LOG("NOT duplicated account: user_email_address[%s]", account->user_email_address);
2859         }
2860         else {  /*  duplicated account */
2861                 ret = false;
2862                 EM_DEBUG_LOG("The same account already exists. Duplicated account: user_email_address[%s]", account->user_email_address);
2863                 error = EMAIL_ERROR_ALREADY_EXISTS;
2864         }
2865
2866 FINISH_OFF:
2867
2868         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2869         _DISCONNECT_DB;
2870
2871         if (err_code != NULL)
2872                 *err_code = error;
2873
2874         EM_DEBUG_FUNC_END("ret [%d]", ret);
2875         return ret;
2876
2877 }
2878
2879 INTERNAL_FUNC int emstorage_get_account_count(int *count, int transaction, int *err_code)
2880 {
2881         EM_DEBUG_FUNC_BEGIN();
2882
2883         if (!count)  {
2884                 if (err_code != NULL)
2885                         *err_code = EMAIL_ERROR_INVALID_PARAM;
2886                 return false;
2887         }
2888
2889         int rc = -1, ret = false;
2890         int error = EMAIL_ERROR_NONE;
2891
2892         DB_STMT hStmt = NULL;
2893         char sql_query_string[QUERY_SIZE] = {0, };
2894         char err_msg[1024];
2895
2896         sqlite3 *local_db_handle = emstorage_get_db_connection();
2897         EMSTORAGE_START_READ_TRANSACTION(transaction);
2898         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_account_tbl");
2899         EM_DEBUG_LOG("SQL STMT [ %s ]", sql_query_string);
2900         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
2901         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
2902         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2903                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2904
2905         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2906         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2907                 ("sqlite3_step fail:%d", rc));
2908
2909         *count = sqlite3_column_int(hStmt, 0);
2910
2911         ret = true;
2912
2913 FINISH_OFF:
2914
2915         if (hStmt != NULL)  {
2916                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
2917
2918                 rc = sqlite3_finalize(hStmt);
2919                 hStmt=NULL;
2920                 if (rc != SQLITE_OK)  {
2921                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d: %s", rc, err_msg);
2922                         error = EMAIL_ERROR_DB_FAILURE;
2923                 }
2924                 EM_DEBUG_LOG("sqlite3_finalize- %d", rc);
2925         }
2926
2927         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2928         _DISCONNECT_DB;
2929
2930         if (err_code != NULL)
2931                 *err_code = error;
2932
2933         EM_DEBUG_FUNC_END("ret [%d]", ret);
2934         return ret;
2935 }
2936
2937
2938 INTERNAL_FUNC int emstorage_get_account_list(int *select_num, emstorage_account_tbl_t** account_list, int transaction, int with_password, int *err_code)
2939 {
2940         EM_DEBUG_FUNC_BEGIN();
2941
2942         int i = 0, count = 0, rc = -1, ret = false;
2943         int field_index = 0;
2944         int error = EMAIL_ERROR_NONE;
2945         emstorage_account_tbl_t *p_data_tbl = NULL;
2946         char sql_query_string[QUERY_SIZE] = {0, };
2947         char *sql = "SELECT count(*) FROM mail_account_tbl;";
2948         char **result;
2949         sqlite3 *local_db_handle = NULL;
2950         DB_STMT hStmt = NULL;
2951
2952         if (!select_num || !account_list)  {
2953                 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, account_list);
2954                 if (err_code != NULL)
2955                         *err_code = EMAIL_ERROR_INVALID_PARAM;
2956                 return false;
2957         }
2958
2959         local_db_handle = emstorage_get_db_connection();
2960         EMSTORAGE_START_READ_TRANSACTION(transaction);
2961
2962         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2963         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2964                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2965
2966         count = atoi(result[1]);
2967         sqlite3_free_table(result);
2968
2969         EM_DEBUG_LOG("count = %d", rc);
2970
2971         if (count == 0) {
2972                 EM_DEBUG_EXCEPTION("no account found...");
2973                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
2974                 ret = true;
2975                 goto FINISH_OFF;
2976         }
2977         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_account_tbl ORDER BY account_id");
2978
2979         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
2980
2981         EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
2982         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2983                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2984
2985         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2986         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2987                 ("sqlite3_step fail:%d", rc));
2988         if (rc == SQLITE_DONE)  {
2989                 EM_DEBUG_EXCEPTION("no account found...");
2990
2991                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
2992                 count = 0;
2993                 ret = true;
2994                 goto FINISH_OFF;
2995         }
2996
2997         if (!(p_data_tbl = (emstorage_account_tbl_t*)malloc(sizeof(emstorage_account_tbl_t) * count)))  {
2998                 EM_DEBUG_EXCEPTION("malloc failed...");
2999                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3000                 goto FINISH_OFF;
3001         }
3002         memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * count);
3003         for (i = 0; i < count; i++)  {
3004                 /*  get recordset */
3005                 field_index = 0;
3006                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_id), field_index++);
3007                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].account_name), 0, field_index++);
3008                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].logo_icon_path), 0, field_index++);
3009                 _get_stmt_field_data_blob(hStmt, &(p_data_tbl[i].user_data), field_index++);
3010                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].user_data_length), field_index++);
3011                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_svc_id), field_index++);
3012                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_status), field_index++);
3013                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_disabled), field_index++);
3014                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].default_mail_slot_size), field_index++);
3015                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_display_name), 0, field_index++);
3016                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_email_address), 0, field_index++);
3017                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].reply_to_address), 0, field_index++);
3018                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].return_address), 0, field_index++);
3019                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].incoming_server_type), field_index++);
3020                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_address), 0, field_index++);
3021                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_port_number), field_index++);
3022                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_user_name), 0, field_index++);
3023                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_password), 0, field_index++);
3024                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_secure_connection), field_index++);
3025                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].retrieval_mode), field_index++);
3026                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].keep_mails_on_pop_server_after_download), field_index++);
3027                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].check_interval), field_index++);
3028                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_download_size), field_index++);
3029                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].outgoing_server_type), field_index++);
3030                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_address), 0, field_index++);
3031                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_port_number), field_index++);
3032                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_user_name), 0, field_index++);
3033                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_password), 0, field_index++);
3034                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_secure_connection), field_index++);
3035                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_need_authentication), field_index++);
3036                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_use_same_authenticator), field_index++);
3037                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.priority), field_index++);
3038                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.keep_local_copy), field_index++);
3039                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_delivery_receipt), field_index++);
3040                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_read_receipt), field_index++);
3041                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.download_limit), field_index++);
3042                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_address), field_index++);
3043                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_subject), field_index++);
3044                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.display_name_from), 0, field_index++);
3045                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.reply_with_body), field_index++);
3046                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.forward_with_files), field_index++);
3047                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_myname_card), field_index++);
3048                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_signature), field_index++);
3049                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.signature), 0, field_index++);
3050                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.add_my_address_to_bcc), field_index++);
3051                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].pop_before_smtp), field_index++);
3052                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_requires_apop), field_index++);
3053                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].smime_type), field_index++);
3054                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].certificate_path), 0, field_index++);
3055                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].cipher_type), field_index++);
3056                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].digest_type), field_index++);
3057
3058                 if (with_password == true) {
3059                         /*  get password from the secure storage */
3060                         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3061                         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3062
3063                         EM_SAFE_FREE(p_data_tbl[i].incoming_server_password);
3064                         EM_SAFE_FREE(p_data_tbl[i].outgoing_server_password);
3065
3066                         /*  get password file name */
3067                         if ((error = _get_password_file_name(p_data_tbl[i].account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
3068                                 EM_DEBUG_EXCEPTION("_get_password_file_name failed. [%d]", error);
3069                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3070                                 goto FINISH_OFF;
3071                         }
3072
3073                         /*  read password from secure storage */
3074                         if ((error = _read_password_from_secure_storage(recv_password_file_name, &(p_data_tbl[i].incoming_server_password))) < 0 ) {
3075                                 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()  failed. [%d]", error);
3076                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3077                                 goto FINISH_OFF;
3078                         }
3079
3080                         if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl[i].outgoing_server_password))) < 0) {
3081                                 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()  failed. [%d]", error);
3082                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3083                                 goto FINISH_OFF;
3084                         }
3085                 }
3086
3087                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3088                 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i,  rc);
3089                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3090                         ("sqlite3_step fail:%d", rc));
3091         }
3092
3093         ret = true;
3094
3095 FINISH_OFF:
3096         if (ret == true)  {
3097                 *account_list = p_data_tbl;
3098                 *select_num = count;
3099                 EM_DEBUG_LOG("COUNT : %d", count);
3100         }
3101         else if (p_data_tbl != NULL)
3102                 emstorage_free_account(&p_data_tbl, count, NULL);
3103
3104         if (hStmt != NULL)  {
3105                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
3106
3107                 rc = sqlite3_finalize(hStmt);
3108                 hStmt = NULL;
3109                 if (rc != SQLITE_OK)  {
3110                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
3111                         error = EMAIL_ERROR_DB_FAILURE;
3112                 }
3113         }
3114
3115         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3116         _DISCONNECT_DB;
3117
3118         if (err_code != NULL)
3119                 *err_code = error;
3120
3121         EM_DEBUG_FUNC_END("ret [%d]", ret);
3122         return ret;
3123 }
3124
3125
3126 INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(int account_id, char *server_mail_id, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
3127 {
3128         EM_DEBUG_FUNC_BEGIN("account_id [%d], server_mail_id[%s], mail[%p], transaction[%d], err_code[%p]", account_id, server_mail_id, mail, transaction, err_code);
3129
3130         int ret = false, error = EMAIL_ERROR_NONE, result_count;
3131         char conditional_clause[QUERY_SIZE] = {0, };
3132         emstorage_mail_tbl_t* p_data_tbl = NULL;
3133
3134         if (!server_mail_id || !mail) {
3135                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3136                 error = EMAIL_ERROR_INVALID_PARAM;
3137                 goto FINISH_OFF;
3138         }
3139
3140         if (account_id == ALL_ACCOUNT)
3141                 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE UPPER(server_mail_id) =UPPER('%s')", server_mail_id);
3142         else
3143                 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE UPPER(server_mail_id) =UPPER('%s') AND account_id = %d", server_mail_id, account_id);
3144
3145         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
3146
3147         if(!emstorage_query_mail_tbl(conditional_clause, transaction, &p_data_tbl, &result_count, &error)) {
3148                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3149                 goto FINISH_OFF;
3150         }
3151
3152         ret = true;
3153
3154 FINISH_OFF:
3155         if (ret == true)
3156                 *mail = p_data_tbl;
3157
3158         if (err_code != NULL)
3159                 *err_code = error;
3160
3161         EM_DEBUG_FUNC_END("ret [%d]", ret);
3162         return ret;
3163 }
3164
3165
3166 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)
3167 {
3168         int cur_clause = 0, conditional_clause_count = 0, i = 0;
3169
3170         if (account_id < ALL_ACCOUNT) {
3171                 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3172                 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3173         }
3174
3175         /*  where clause */
3176         if (account_id == ALL_ACCOUNT) {
3177                 cur_clause += (conditional_clause_count++ == 0)?
3178                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_type not in (3, 5, 7, 8)"):
3179                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_type not in (3, 5, 7, 8)");
3180         }
3181         else {
3182                 cur_clause += (conditional_clause_count++ == 0)?
3183                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE account_id = %d", account_id):
3184                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND account_id = %d", account_id);
3185         }
3186
3187         if (mailbox_id > 0) {
3188                 cur_clause += (conditional_clause_count++ == 0)?
3189                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_id = %d", mailbox_id):
3190                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_id = %d", mailbox_id);
3191         }
3192         else if(account_id != ALL_ACCOUNT) {
3193                 cur_clause += (conditional_clause_count++ == 0)?
3194                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_type not in (3, 5, 7, 8)"):
3195                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_type not in (3, 5, 7, 8)");
3196         }
3197
3198         if (thread_id > 0) {
3199                 cur_clause += (conditional_clause_count++ == 0)?
3200                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_id = %d ", thread_id):
3201                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_id = %d ", thread_id);
3202         }
3203         else if (thread_id == EMAIL_LIST_TYPE_THREAD)   {
3204                 cur_clause += (conditional_clause_count++ == 0)?
3205                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_item_count > 0"):
3206                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_item_count > 0");
3207         }
3208         else if (thread_id == EMAIL_LIST_TYPE_LOCAL) {
3209                 cur_clause += (conditional_clause_count++ == 0)?
3210                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE server_mail_status = 0"):
3211                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND server_mail_status = 0");
3212         }
3213         else if (thread_id == EMAIL_LIST_TYPE_UNREAD) {
3214                 cur_clause += (conditional_clause_count++ == 0)?
3215                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_seen_field == 0"):
3216                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_seen_field == 0");
3217         }
3218
3219         /*  EM_DEBUG_LOG("where clause added [%s]", conditional_clause_string); */
3220         if (addr_list && addr_list->address_count > 0) {
3221                 if (!addr_list->address_type) {
3222                         cur_clause += (conditional_clause_count++ == 0)?
3223                                 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE email_address_sender IN(\"%s\"", addr_list->address_list[0]):
3224                                 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND email_address_sender IN(\"%s\"", addr_list->address_list[0]);
3225
3226                         for (i = 1; i < addr_list->address_count; i++)
3227                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3228
3229                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3230                 } else {
3231                         cur_clause += (conditional_clause_count++ == 0)?
3232                                 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]):
3233                                 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]);
3234
3235                         for (i = 1; i < addr_list->address_count; i++)
3236                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3237
3238                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3239                 }
3240         }
3241
3242         if (input_except_delete_flagged_mails) {
3243                 cur_clause += (conditional_clause_count++ == 0)?
3244                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_deleted_field = 0"):
3245                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_deleted_field = 0");
3246         }
3247
3248         if (search_value) {
3249                 switch (search_type) {
3250                         case EMAIL_SEARCH_FILTER_SUBJECT:
3251                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3252                                         " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3253                                 break;
3254                         case EMAIL_SEARCH_FILTER_SENDER:
3255                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3256                                         " %s  ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3257                                         ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3258                                 break;
3259                         case EMAIL_SEARCH_FILTER_RECIPIENT:
3260                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3261                                         " %s ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3262                                         "       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3263                                         "       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3264                                         ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value);
3265                                 break;
3266                         case EMAIL_SEARCH_FILTER_ALL:
3267                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3268                                         " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\' "
3269                                         "       OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3270                                         "                       OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3271                                         "                       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3272                                         "                       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3273                                         "               ) "
3274                                         "       )"
3275                                         ")", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value, search_value, search_value);
3276                                 break;
3277                 }
3278         }
3279
3280         /*  EM_DEBUG_LOG("where clause [%s]", conditional_clause_string); */
3281         static char sorting_str[][50] = {
3282                          " ORDER BY date_time DESC",                                             /* case EMAIL_SORT_DATETIME_HIGH: */
3283                          " ORDER BY date_time ASC",                                               /* case EMAIL_SORT_DATETIME_LOW: */
3284                          " ORDER BY full_address_from DESC, date_time DESC", /* case EMAIL_SORT_SENDER_HIGH: */
3285                          " ORDER BY full_address_from ASC, date_time DESC",  /* case EMAIL_SORT_SENDER_LOW: */
3286                          " ORDER BY full_address_to DESC, date_time DESC",   /* case EMAIL_SORT_RCPT_HIGH: */
3287                          " ORDER BY full_address_to ASC, date_time DESC",       /* case EMAIL_SORT_RCPT_LOW: */
3288                          " ORDER BY subject DESC, date_time DESC",                 /* case EMAIL_SORT_SUBJECT_HIGH: */
3289                          " ORDER BY subject ASC, date_time DESC",                       /* case EMAIL_SORT_SUBJECT_LOW: */
3290                          " ORDER BY priority DESC, date_time DESC",               /* case EMAIL_SORT_PRIORITY_HIGH: */
3291                          " ORDER BY priority ASC, date_time DESC",                 /* case EMAIL_SORT_PRIORITY_LOW: */
3292                          " ORDER BY attachment_count DESC, date_time DESC",  /* case EMAIL_SORT_ATTACHMENT_HIGH: */
3293                          " ORDER BY attachment_count ASC, date_time DESC",   /* case EMAIL_SORT_ATTACHMENT_LOW: */
3294                          " ORDER BY lock_status DESC, date_time DESC",     /* case EMAIL_SORT_FAVORITE_HIGH: */
3295                          " ORDER BY lock_status ASC, date_time DESC",           /* case EMAIL_SORT_FAVORITE_LOW: */
3296                          " ORDER BY mailbox_name DESC, date_time DESC",   /* case EMAIL_SORT_MAILBOX_NAME_HIGH: */
3297                          " ORDER BY mailbox_name ASC, date_time DESC"           /* case EMAIL_SORT_MAILBOX_NAME_LOW: */
3298                          };
3299
3300         if (sorting < EMAIL_SORT_END && sorting >= 0)
3301                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " %s", sorting_str[sorting]);
3302         else
3303                 EM_DEBUG_LOG(" Invalid Sorting order ");
3304
3305         /*  limit clause */
3306         if (start_index != -1 && limit_count != -1)
3307                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " LIMIT %d, %d", start_index, limit_count);
3308
3309         return true;
3310 }
3311
3312
3313 /**
3314   *     emstorage_get_mail_list - Get the mail list information.
3315   *
3316   *
3317   */
3318 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)
3319 {
3320         EM_DEBUG_FUNC_BEGIN();
3321         EM_PROFILE_BEGIN(emstorage_get_mail_list_func);
3322
3323         int ret = false, error = EMAIL_ERROR_NONE;
3324         char conditional_clause_string[QUERY_SIZE] = { 0, };
3325
3326         if (account_id < ALL_ACCOUNT) {
3327                 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3328                 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3329         }
3330         EM_IF_NULL_RETURN_VALUE(result_count, false);
3331
3332         _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);
3333
3334         EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
3335
3336         if(!emstorage_query_mail_list(conditional_clause_string, transaction, mail_list, result_count, &error)) {
3337                 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3338                 goto FINISH_OFF;
3339         }
3340
3341         ret = true;
3342
3343 FINISH_OFF:
3344         if (err_code != NULL)
3345                 *err_code = error;
3346
3347         EM_PROFILE_END(emstorage_get_mail_list_func);
3348         EM_DEBUG_FUNC_END("ret [%d]", ret);
3349         return ret;
3350 }
3351
3352
3353 /**
3354   *     emstorage_get_mails - Get the Mail list information based on mailbox_name name
3355   *
3356   *
3357   */
3358 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)
3359 {
3360         EM_DEBUG_FUNC_BEGIN();
3361         EM_PROFILE_BEGIN(emStorageGetMails);
3362
3363         int count = 0, ret = false, error = EMAIL_ERROR_NONE;
3364         emstorage_mail_tbl_t *p_data_tbl = NULL;
3365         char conditional_clause_string[QUERY_SIZE] = {0, };
3366
3367         EM_IF_NULL_RETURN_VALUE(mail_list, false);
3368         EM_IF_NULL_RETURN_VALUE(result_count, false);
3369
3370         if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3371                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3372                 if (err_code != NULL)
3373                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3374                 return false;
3375         }
3376
3377         _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);
3378
3379         EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
3380
3381         if(!emstorage_query_mail_tbl(conditional_clause_string, transaction, &p_data_tbl, &count,  &error)) {
3382                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3383                 goto FINISH_OFF;
3384         }
3385
3386         ret = true;
3387
3388 FINISH_OFF:
3389         if (ret == true) {
3390                 *mail_list = p_data_tbl;
3391                 *result_count = count;
3392                 EM_DEBUG_LOG("COUNT : %d", count);
3393         }
3394         else if (p_data_tbl != NULL)
3395                 emstorage_free_mail(&p_data_tbl, count, NULL);
3396
3397         if (err_code != NULL)
3398                 *err_code = error;
3399
3400         EM_PROFILE_END(emStorageGetMails);
3401         EM_DEBUG_FUNC_END("ret [%d]", ret);
3402         return ret;
3403 }
3404
3405
3406
3407 /**
3408   *     emstorage_get_searched_mail_list - Get the mail list information after filtering
3409   *
3410   *
3411   */
3412 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)
3413 {
3414         EM_DEBUG_FUNC_BEGIN();
3415
3416         int ret = false, error = EMAIL_ERROR_NONE;
3417         char conditional_clause[QUERY_SIZE] = {0, };
3418         char *temp_search_value = NULL;
3419         char *temp_search_value2 = NULL;
3420
3421         EM_IF_NULL_RETURN_VALUE(mail_list, false);
3422         EM_IF_NULL_RETURN_VALUE(result_count, false);
3423
3424         if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3425                 EM_DEBUG_EXCEPTION("select_num[%p], Mail_list[%p]", result_count, mail_list);
3426                 error = EMAIL_ERROR_INVALID_PARAM;
3427                 goto FINISH_OFF;
3428         }
3429
3430         temp_search_value = em_replace_all_string((char*)search_value, "_", "\\_");
3431         temp_search_value2 = em_replace_all_string(temp_search_value, "%", "\\%");
3432
3433         _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);
3434
3435         EM_DEBUG_LOG("conditional_clause[%s]", conditional_clause);
3436
3437         if(!emstorage_query_mail_list(conditional_clause, transaction, mail_list, result_count, &error)) {
3438                 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3439                 goto FINISH_OFF;
3440         }
3441
3442         ret = true;
3443
3444 FINISH_OFF:
3445         EM_DEBUG_LOG("emstorage_get_searched_mail_list finish off");
3446
3447         if (err_code != NULL)
3448                 *err_code = error;
3449
3450         EM_SAFE_FREE(temp_search_value);
3451         EM_SAFE_FREE(temp_search_value2);
3452
3453         EM_DEBUG_FUNC_END("ret [%d]", ret);
3454         return ret;
3455 }
3456
3457
3458 static int _get_password_file_name(int account_id, char *recv_password_file_name, char *send_password_file_name)
3459 {
3460         EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
3461
3462         if (account_id <= 0 || !recv_password_file_name || !send_password_file_name)  {
3463                 EM_DEBUG_EXCEPTION("Invalid parameter");
3464                 return EMAIL_ERROR_INVALID_PARAM;
3465         }
3466
3467         sprintf(recv_password_file_name, ".email_account_%d_recv", account_id);
3468         sprintf(send_password_file_name, ".email_account_%d_send", account_id);
3469         EM_DEBUG_FUNC_END();
3470         return EMAIL_ERROR_NONE;
3471 }
3472
3473 static int _read_password_from_secure_storage(char *file_name, char **password)
3474 {
3475         EM_DEBUG_FUNC_BEGIN("file_name[%s], password[%p]", file_name, password);
3476
3477         if (!file_name || !password) {
3478                 EM_DEBUG_EXCEPTION("Invalid Parameter");
3479                 return EMAIL_ERROR_INVALID_PARAM;
3480         }
3481
3482         size_t buf_len = 0, read_len = 0;
3483         ssm_file_info_t sfi;
3484         char *temp_password = NULL;
3485         int error_code_from_ssm = 0;
3486         int ret = EMAIL_ERROR_NONE;
3487
3488         if ( (error_code_from_ssm = ssm_getinfo(file_name, &sfi, SSM_FLAG_SECRET_OPERATION, NULL)) < 0) {
3489                 EM_DEBUG_EXCEPTION("ssm_getinfo() failed. [%d]", error_code_from_ssm);
3490                 ret = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3491                 goto FINISH_OFF;
3492         }
3493
3494         buf_len = sfi.originSize;
3495         EM_DEBUG_LOG("password buf_len[%d]", buf_len);
3496         if ((temp_password = (char *)malloc(buf_len + 1)) == NULL) {
3497                 EM_DEBUG_EXCEPTION("malloc failed...");
3498                 ret = EMAIL_ERROR_OUT_OF_MEMORY;
3499                 goto FINISH_OFF;
3500         }
3501         memset(temp_password, 0x00, buf_len + 1);
3502
3503         if ( (error_code_from_ssm = ssm_read(file_name, temp_password, buf_len, &read_len, SSM_FLAG_SECRET_OPERATION, NULL)) < 0) {
3504                 EM_DEBUG_EXCEPTION("ssm_read() failed. [%d]", error_code_from_ssm);
3505                 ret = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3506                 goto FINISH_OFF;
3507         }
3508
3509         EM_DEBUG_LOG("password_file_name[%s], password[%s], originSize[%d], read len[%d]", file_name,  temp_password, sfi.originSize, read_len);
3510
3511         *password = temp_password;
3512         temp_password = NULL;
3513
3514 FINISH_OFF:
3515         EM_SAFE_FREE(temp_password);
3516
3517         EM_DEBUG_FUNC_END("ret [%d]", ret);
3518         return ret;
3519 }
3520
3521
3522 INTERNAL_FUNC int emstorage_get_account_by_id(int account_id, int pulloption, emstorage_account_tbl_t **account, int transaction, int *err_code)
3523 {
3524         EM_DEBUG_FUNC_BEGIN("account_id[%d], pulloption[%d], account[%p], transaction[%d], err_code[%p]", account_id, pulloption, account, transaction, err_code);
3525
3526         if (!account)  {
3527                 EM_DEBUG_EXCEPTION("account_id[%d], account[%p]", account_id, account);
3528                 if (err_code != NULL)
3529                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3530                 return false;
3531         }
3532
3533         int ret = false;
3534         int error = EMAIL_ERROR_NONE;
3535         emstorage_account_tbl_t* p_data_tbl = NULL;
3536         DB_STMT hStmt = NULL;
3537         char sql_query_string[QUERY_SIZE] = {0, };
3538         int rc = -1;
3539         int sql_len = 0;
3540         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3541         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3542
3543         sqlite3 *local_db_handle = emstorage_get_db_connection();
3544         EMSTORAGE_START_READ_TRANSACTION(transaction);
3545
3546         /*  Make query string */
3547         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
3548         sql_len = EM_SAFE_STRLEN(sql_query_string);
3549
3550         if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
3551                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
3552                         "incoming_server_type,"
3553                         "incoming_server_address,"
3554                         "user_email_address,"
3555                         "incoming_server_user_name,"
3556                         "retrieval_mode,"
3557                         "incoming_server_port_number,"
3558                         "incoming_server_secure_connection,"
3559                         "outgoing_server_type,"
3560                         "outgoing_server_address,"
3561                         "outgoing_server_port_number,"
3562                         "outgoing_server_need_authentication,"
3563                         "outgoing_server_secure_connection,"
3564                         "outgoing_server_user_name,"
3565                         "user_display_name,"
3566                         "reply_to_address,"
3567                         "return_address,"
3568                         "account_id,"
3569                         "keep_mails_on_pop_server_after_download,"
3570                         "auto_download_size,"
3571                         "outgoing_server_use_same_authenticator,"
3572                         "pop_before_smtp,"
3573                         "incoming_server_requires_apop,"
3574                         "logo_icon_path,"
3575                         "user_data,"
3576                         "user_data_length,"
3577                         "check_interval,"
3578                         "sync_status,");
3579                 sql_len = EM_SAFE_STRLEN(sql_query_string);
3580         }
3581
3582         if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME) {
3583                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " account_name, ");
3584                 sql_len = EM_SAFE_STRLEN(sql_query_string);
3585         }
3586
3587         /*  get from secure storage, not from db */
3588         if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
3589                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
3590                         "priority,"
3591                         "keep_local_copy,"
3592                         "req_delivery_receipt,"
3593                         "req_read_receipt,"
3594                         "download_limit,"
3595                         "block_address,"
3596                         "block_subject,"
3597                         "display_name_from,"
3598                         "reply_with_body,"
3599                         "forward_with_files,"
3600                         "add_myname_card,"
3601                         "add_signature,"
3602                         "signature,"
3603                         "add_my_address_to_bcc,"
3604                         "account_svc_id,"
3605                         "sync_disabled,"
3606                         "default_mail_slot_size,"
3607                         "smime_type,"
3608                         "certificate_path,"
3609                         "cipher_type,"
3610                         "digest_type,");
3611
3612                 sql_len = EM_SAFE_STRLEN(sql_query_string);
3613         }
3614
3615         /*  dummy value, FROM WHERE clause */
3616         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "0 FROM mail_account_tbl WHERE account_id = %d", account_id);
3617
3618         /*  FROM clause */
3619         EM_DEBUG_LOG("query = [%s]", sql_query_string);
3620
3621         /*  execute a sql and count rows */
3622         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3623         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3624                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3625
3626         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3627         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3628                 ("sqlite3_step fail:%d", rc));
3629
3630         if (rc == SQLITE_DONE)  {
3631                 EM_DEBUG_EXCEPTION("no matched account found...");
3632                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3633                 goto FINISH_OFF;
3634         }
3635
3636         /*  Assign query result to structure */
3637         if (!(p_data_tbl = (emstorage_account_tbl_t *)malloc(sizeof(emstorage_account_tbl_t) * 1)))  {
3638                 EM_DEBUG_EXCEPTION("malloc failed...");
3639                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3640                 goto FINISH_OFF;
3641         }
3642
3643         memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * 1);
3644         int col_index = 0;
3645
3646         if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
3647                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_type), col_index++);
3648                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_address),  0, col_index++);
3649                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_email_address), 0, col_index++);
3650                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_user_name), 0, col_index++);
3651                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->retrieval_mode), col_index++);
3652                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_port_number), col_index++);
3653                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_secure_connection), col_index++);
3654                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->outgoing_server_type), col_index++);
3655                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_address), 0, col_index++);
3656                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_port_number), col_index++);
3657                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_need_authentication), col_index++);
3658                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_secure_connection), col_index++);
3659                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_user_name), 0, col_index++);
3660                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_display_name), 0, col_index++);
3661                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->reply_to_address), 0, col_index++);
3662                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->return_address), 0, col_index++);
3663                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
3664                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->keep_mails_on_pop_server_after_download), col_index++);
3665                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_download_size), col_index++);
3666                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_use_same_authenticator), col_index++);
3667                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->pop_before_smtp), col_index++);
3668                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_requires_apop), col_index++);
3669                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->logo_icon_path), 0, col_index++);
3670                 _get_stmt_field_data_blob(hStmt, &p_data_tbl->user_data, col_index++);
3671                 _get_stmt_field_data_int(hStmt, &p_data_tbl->user_data_length, col_index++);
3672                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->check_interval), col_index++);
3673                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->sync_status), col_index++);
3674         }
3675
3676         if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME)
3677                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->account_name), 0, col_index++);
3678
3679         if (pulloption & EMAIL_ACC_GET_OPT_PASSWORD) {
3680                 /*  get password file name */
3681                 if ((error = _get_password_file_name(p_data_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
3682                         EM_DEBUG_EXCEPTION("_get_password_file_name failed. [%d]", error);
3683                         goto FINISH_OFF;
3684                 }
3685
3686                 /*  read password from secure storage */
3687                 if ((error = _read_password_from_secure_storage(recv_password_file_name, &(p_data_tbl->incoming_server_password))) < 0) {
3688                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed. [%d]", error);
3689                         goto FINISH_OFF;
3690                 }
3691
3692                 EM_DEBUG_LOG("recv_password_file_name[%s], password[%s]", recv_password_file_name,  p_data_tbl->incoming_server_password);
3693
3694                 if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl->outgoing_server_password))) < 0) {
3695                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed. [%d]", error);
3696                         goto FINISH_OFF;
3697                 }
3698                 EM_DEBUG_LOG("send_password_file_name[%s], password[%s]", send_password_file_name,  p_data_tbl->outgoing_server_password);
3699         }
3700
3701         if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
3702                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.priority), col_index++);
3703                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.keep_local_copy), col_index++);
3704                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_delivery_receipt), col_index++);
3705                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_read_receipt), col_index++);
3706                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.download_limit), col_index++);
3707                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_address), col_index++);
3708                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_subject), col_index++);
3709                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.display_name_from), 0, col_index++);
3710                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.reply_with_body), col_index++);
3711                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.forward_with_files), col_index++);
3712                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_myname_card), col_index++);
3713                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_signature), col_index++);
3714                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.signature), 0, col_index++);
3715                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.add_my_address_to_bcc), col_index++);
3716                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_svc_id), col_index++);
3717                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->sync_disabled), col_index++);
3718                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->default_mail_slot_size), col_index++);
3719                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->smime_type), col_index++);
3720                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->certificate_path), 0, col_index++);
3721                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->cipher_type), col_index++);
3722                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->digest_type), col_index++);
3723         }
3724
3725         ret = true;
3726
3727 FINISH_OFF:
3728         if (ret == true)
3729                 *account = p_data_tbl;
3730         else {
3731                 if (p_data_tbl)
3732                         emstorage_free_account((emstorage_account_tbl_t **)&p_data_tbl, 1, NULL);
3733         }
3734         if (hStmt != NULL)  {
3735                 rc = sqlite3_finalize(hStmt);
3736                 if (rc != SQLITE_OK)  {
3737                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
3738                         error = EMAIL_ERROR_DB_FAILURE;
3739                 }
3740         }
3741
3742         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3743         _DISCONNECT_DB;
3744
3745         if (err_code != NULL)
3746                 *err_code = error;
3747
3748         EM_DEBUG_FUNC_END("ret [%d]", ret);
3749         return ret;
3750 }
3751
3752 INTERNAL_FUNC int emstorage_get_password_length_of_account(int account_id, int *password_length, int *err_code)
3753 {
3754         EM_DEBUG_FUNC_BEGIN("account_id[%d], password_length[%p], err_code[%p]", account_id, password_length, err_code);
3755
3756         if (account_id <= 0 || password_length == NULL)  {
3757                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3758                 if (err_code != NULL)
3759                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3760                 return false;
3761         }
3762
3763         int ret = false;
3764         int error = EMAIL_ERROR_NONE;
3765         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3766         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3767         char *temp_password = NULL;
3768
3769
3770         /*  get password file name */
3771         if ((error = _get_password_file_name(account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
3772                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
3773                 goto FINISH_OFF;
3774         }
3775
3776         /*  read password from secure storage */
3777         if ((error = _read_password_from_secure_storage(recv_password_file_name, &temp_password)) < 0 || !temp_password) {
3778                 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed...");
3779                 goto FINISH_OFF;
3780         }
3781
3782         *password_length = EM_SAFE_STRLEN(temp_password);
3783
3784         EM_DEBUG_LOG("recv_password_file_name[%s], *password_length[%d]", recv_password_file_name,  *password_length);
3785
3786         ret = true;
3787
3788 FINISH_OFF:
3789         EM_SAFE_FREE(temp_password);
3790
3791         if (err_code != NULL)
3792                 *err_code = error;
3793
3794         EM_DEBUG_FUNC_END("ret [%d]", ret);
3795         return ret;
3796 }
3797
3798 INTERNAL_FUNC int emstorage_update_account(int account_id, emstorage_account_tbl_t* account_tbl, int transaction, int *err_code)
3799 {
3800         EM_DEBUG_FUNC_BEGIN("account_id[%d], account[%p], transaction[%d], err_code[%p]", account_id, account_tbl, transaction, err_code);
3801
3802         if (account_id < FIRST_ACCOUNT_ID || !account_tbl)  {
3803                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3804                 if (err_code != NULL)
3805                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3806                 return false;
3807         }
3808
3809         int error = EMAIL_ERROR_NONE;
3810         int rc, ret = false;
3811
3812         DB_STMT hStmt = NULL;
3813         char sql_query_string[QUERY_SIZE] = {0, };
3814         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3815         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3816
3817         sqlite3 *local_db_handle = emstorage_get_db_connection();
3818         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
3819
3820         SNPRINTF(sql_query_string, sizeof(sql_query_string),
3821                 "UPDATE mail_account_tbl SET"
3822                 "  account_name = ?"
3823                 ", logo_icon_path = ?"
3824                 ", user_data = ?"
3825                 ", user_data_length = ?"
3826                 ", account_svc_id = ?"
3827                 ", sync_status = ?"
3828                 ", sync_disabled = ?"
3829                 ", default_mail_slot_size = ?"
3830                 ", user_display_name = ?"
3831                 ", user_email_address = ?"
3832                 ", reply_to_address = ?"
3833                 ", return_address = ?"
3834                 ", incoming_server_type = ?"
3835                 ", incoming_server_address = ?"
3836                 ", incoming_server_port_number = ?"
3837                 ", incoming_server_user_name = ?"
3838                 ", incoming_server_secure_connection = ?"
3839                 ", retrieval_mode = ?"
3840                 ", keep_mails_on_pop_server_after_download = ?"
3841                 ", check_interval = ?"
3842                 ", auto_download_size = ?"
3843                 ", outgoing_server_type = ?"
3844                 ", outgoing_server_address = ?"
3845                 ", outgoing_server_port_number = ?"
3846                 ", outgoing_server_user_name = ?"
3847                 ", outgoing_server_secure_connection = ?"
3848                 ", outgoing_server_need_authentication = ?"
3849                 ", outgoing_server_use_same_authenticator = ?"
3850                 ", priority = ?"
3851                 ", keep_local_copy = ?"
3852                 ", req_delivery_receipt = ?"
3853                 ", req_read_receipt = ?"
3854                 ", download_limit = ?"
3855                 ", block_address = ?"
3856                 ", block_subject = ?"
3857                 ", display_name_from = ?"
3858                 ", reply_with_body = ?"
3859                 ", forward_with_files = ?"
3860                 ", add_myname_card = ?"
3861                 ", add_signature = ?"
3862                 ", signature = ?"
3863                 ", add_my_address_to_bcc = ?"
3864                 ", pop_before_smtp = ?"
3865                 ", incoming_server_requires_apop = ?"
3866                 ", smime_type = ?"
3867                 ", certificate_path = ?"
3868                 ", cipher_type = ?"
3869                 ", digest_type = ?"
3870                 " WHERE account_id = ?");
3871
3872         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3873         EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
3874         EM_DEBUG_LOG("SQL[%s]", sql_query_string);
3875         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3876                 ("sqlite3_prepare fail:(%d) %s", rc, sqlite3_errmsg(local_db_handle)));
3877
3878         int i = 0;
3879
3880         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
3881         _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
3882         _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
3883         _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
3884         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
3885         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
3886         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
3887         _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
3888         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
3889         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3890         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3891         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3892         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
3893         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3894         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
3895         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
3896         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
3897         _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
3898         _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
3899         _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
3900         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
3901         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
3902         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3903         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
3904         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
3905         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
3906         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
3907         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
3908         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
3909         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
3910         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
3911         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
3912         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
3913         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
3914         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
3915         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
3916         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
3917         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
3918         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
3919         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
3920         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
3921         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
3922         _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
3923         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
3924         _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
3925         _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
3926         _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
3927         _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
3928         _bind_stmt_field_data_int(hStmt, i++, account_id);
3929
3930         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3931         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
3932                 ("sqlite3_step fail:%d", rc));
3933         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3934                 ("sqlite3_step fail:%d", rc));
3935
3936         /*  validate account existence */
3937         rc = sqlite3_changes(local_db_handle);
3938         if (rc == 0) {
3939                 EM_DEBUG_EXCEPTION(" no matched account found...");
3940
3941                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3942                 goto FINISH_OFF;
3943         }
3944
3945         /*  get password file name */
3946         if ((error = _get_password_file_name(account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
3947                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
3948                 goto FINISH_OFF;
3949         }
3950
3951         /*  save passwords to the secure storage */
3952         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);
3953         if (account_tbl->incoming_server_password && (EM_SAFE_STRLEN(account_tbl->incoming_server_password) > 0)) {
3954                 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) {
3955                         EM_DEBUG_EXCEPTION(" ssm_write_buffer failed -recv incoming_server_password : file[%s]", recv_password_file_name);
3956                         error = EMAIL_ERROR_SYSTEM_FAILURE;
3957                         goto FINISH_OFF;
3958                 }
3959         }
3960
3961         if (account_tbl->outgoing_server_password && (EM_SAFE_STRLEN(account_tbl->outgoing_server_password) > 0)) {
3962                 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) {
3963                         EM_DEBUG_EXCEPTION(" ssm_write_buffer failed -send password : file[%s]", send_password_file_name);
3964                         error = EMAIL_ERROR_SYSTEM_FAILURE;
3965                         goto FINISH_OFF;
3966                 }
3967         }
3968
3969         if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_tbl->account_id, 0, NULL, 0))
3970                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_ACCOUNT_UPDATE] : Notification Failed >>> ");
3971         ret = true;
3972
3973 FINISH_OFF:
3974
3975         if (hStmt != NULL)  {
3976                 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
3977
3978                 rc = sqlite3_finalize(hStmt);
3979                 if (rc != SQLITE_OK)  {
3980                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
3981                         error = EMAIL_ERROR_DB_FAILURE;
3982                 }
3983         }
3984
3985         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
3986
3987         _DISCONNECT_DB;
3988
3989         if (err_code != NULL)
3990                 *err_code = error;
3991
3992         EM_DEBUG_FUNC_END("ret [%d]", ret);
3993         return ret;
3994 }
3995
3996 INTERNAL_FUNC int emstorage_set_field_of_accounts_with_integer_value(int account_id, char *field_name, int value, int transaction)
3997 {
3998         EM_DEBUG_FUNC_BEGIN("account_id[%d], field_name[%s], value[%d], transaction[%d]", account_id, field_name, value, transaction);
3999         int error = EMAIL_ERROR_NONE;
4000         int rc = 0;
4001         int result = 0;
4002         char sql_query_string[QUERY_SIZE] = {0, };
4003         sqlite3 *local_db_handle = NULL;
4004
4005         if (!account_id  || !field_name) {
4006                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4007                 return EMAIL_ERROR_INVALID_PARAM;
4008         }
4009
4010         local_db_handle = emstorage_get_db_connection();
4011
4012         /* Write query string */
4013         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_account_tbl SET %s = %d WHERE account_id = %d", field_name, value, account_id);
4014
4015         EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
4016
4017         /* Execute query */
4018         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
4019         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
4020         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4021                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4022         if (sqlite3_changes(local_db_handle) == 0)
4023                 EM_DEBUG_LOG("no mail matched...");
4024
4025
4026 FINISH_OFF:
4027         result = (error == EMAIL_ERROR_NONE) ? true : false;
4028         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, result, error);
4029         _DISCONNECT_DB;
4030
4031         if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_id, 0, field_name, value))
4032                 EM_DEBUG_EXCEPTION("emcore_notify_storage_event failed : NOTI_ACCOUNT_UPDATE [%s,%d]", field_name, value);
4033
4034         EM_DEBUG_FUNC_END("error [%d]", error);
4035         return error;
4036 }
4037
4038 INTERNAL_FUNC int emstorage_get_sync_status_of_account(int account_id, int *result_sync_status,int *err_code)
4039 {
4040         EM_DEBUG_FUNC_BEGIN("account_id[%d], result_sync_status [%p], err_code[%p]", account_id, result_sync_status, err_code);
4041
4042         if(!result_sync_status) {
4043                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4044                 if (err_code != NULL)
4045                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4046                 return false;
4047         }
4048
4049         int error = EMAIL_ERROR_NONE, rc, ret = false, sync_status, count, i, col_index;
4050         char sql_query_string[QUERY_SIZE] = {0, };
4051         char **result = NULL;
4052         sqlite3 *local_db_handle = emstorage_get_db_connection();
4053
4054         if(account_id)
4055                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl WHERE account_id = %d", account_id);
4056         else
4057                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl");
4058
4059         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
4060         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
4061                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4062
4063         if (!count) {
4064                 EM_DEBUG_EXCEPTION("no matched account found...");
4065                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4066                 goto FINISH_OFF;
4067         }
4068
4069         col_index = 1;
4070         *result_sync_status = 0;
4071
4072         for(i = 0; i < count; i++) {
4073                 _get_table_field_data_int(result, &sync_status, col_index++);
4074                 *result_sync_status |= sync_status;
4075         }
4076
4077         EM_DEBUG_LOG("sync_status [%d]", sync_status);
4078
4079         sqlite3_free_table(result);
4080
4081         ret = true;
4082
4083 FINISH_OFF:
4084
4085         _DISCONNECT_DB;
4086
4087         if (err_code != NULL)
4088                 *err_code = error;
4089
4090         EM_DEBUG_FUNC_END("ret [%d]", ret);
4091         return ret;
4092 }
4093
4094 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)
4095 {
4096         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);
4097
4098         int error = EMAIL_ERROR_NONE, rc, ret = false, set_value = sync_status, result_sync_status;
4099         char sql_query_string[QUERY_SIZE] = {0, };
4100         sqlite3 *local_db_handle = emstorage_get_db_connection();
4101
4102         if(set_operator != SET_TYPE_SET && account_id) {
4103                 if(!emstorage_get_sync_status_of_account(account_id, &result_sync_status, &error)) {
4104                         EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", error);
4105                         goto FINISH_OFF;
4106                 }
4107                 switch(set_operator) {
4108                         case SET_TYPE_UNION :
4109                                 set_value = result_sync_status | set_value;
4110                                 break;
4111                         case SET_TYPE_MINUS :
4112                                 set_value = result_sync_status & (~set_value);
4113                                 break;
4114                         default:
4115                                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_NOT_SUPPORTED [%d]", set_operator);
4116                                 error = EMAIL_ERROR_NOT_SUPPORTED;
4117                                 break;
4118                 }
4119                 EM_DEBUG_LOG("set_value [%d]", set_value);
4120         }
4121
4122         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
4123
4124         if(account_id)
4125                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE account_id = %d", set_value, account_id);
4126         else
4127                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE incoming_server_type <> 5", set_value);
4128
4129         EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
4130
4131         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
4132         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4133                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4134
4135         rc = sqlite3_changes(local_db_handle);
4136
4137         if (rc == 0) {
4138                 EM_DEBUG_EXCEPTION("no matched account found...");
4139                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4140                 goto FINISH_OFF;
4141         }
4142
4143         if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE_SYNC_STATUS, account_id, 0, NULL, 0))
4144                 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_UPDATE_SYNC_STATUS] : Notification failed");
4145         ret = true;
4146
4147 FINISH_OFF:
4148
4149         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
4150
4151         _DISCONNECT_DB;
4152
4153         if (err_code != NULL)
4154                 *err_code = error;
4155
4156         EM_DEBUG_FUNC_END("ret [%d]", ret);
4157         return ret;
4158 }
4159
4160 INTERNAL_FUNC int emstorage_add_account(emstorage_account_tbl_t* account_tbl, int transaction, int *err_code)
4161 {
4162         EM_DEBUG_FUNC_BEGIN("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4163
4164         if (!account_tbl)  {
4165                 EM_DEBUG_EXCEPTION("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4166                 if (err_code != NULL)
4167                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4168                 return false;
4169         }
4170
4171         int rc = -1, ret = false;
4172         int error = EMAIL_ERROR_NONE;
4173         int error_from_ssm = 0;
4174         DB_STMT hStmt = NULL;
4175         char sql_query_string[QUERY_SIZE] = {0, };
4176         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4177         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4178
4179         sqlite3 *local_db_handle = emstorage_get_db_connection();
4180
4181         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
4182
4183         char *sql = "SELECT max(rowid) FROM mail_account_tbl;";
4184         char **result = NULL;
4185
4186         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
4187         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
4188                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
4189
4190         if (NULL==result[1]) rc = 1;
4191         else rc = atoi(result[1])+1;
4192         sqlite3_free_table(result);
4193         result = NULL;
4194
4195         account_tbl->account_id = rc;
4196
4197         if ((error = _get_password_file_name(account_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4198                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4199                 goto FINISH_OFF;
4200         }
4201
4202         EM_DEBUG_LOG(" >>>> ACCOUNT_ID [ %d ] ", account_tbl->account_id);
4203         SNPRINTF(sql_query_string, sizeof(sql_query_string),
4204                 "INSERT INTO mail_account_tbl VALUES "
4205                 "(        "
4206                 "    ? "  /*   account_id */
4207                 "  , ? "  /*   account_name */
4208                 "  , ? "  /*   logo_icon_path */
4209                 "  , ? "  /*   user_data */
4210                 "  , ? "  /*   user_data_length */
4211                 "  , ? "  /*   account_svc_id */
4212                 "  , ? "  /*   sync_status */
4213                 "  , ? "  /*   sync_disabled */
4214                 "  , ? "  /*   default_mail_slot_size */
4215                 "  , ? "  /*   user_display_name */
4216                 "  , ? "  /*   user_email_address */
4217                 "  , ? "  /*   reply_to_address */
4218                 "  , ? "  /*   return_address */
4219                 "  , ? "  /*   incoming_server_type */
4220                 "  , ? "  /*   incoming_server_address */
4221                 "  , ? "  /*   incoming_server_port_number */
4222                 "  , ? "  /*   incoming_server_user_name */
4223                 "  , ? "  /*   incoming_server_password */
4224                 "  , ? "  /*   incoming_server_secure_connection */
4225                 "  , ? "  /*   retrieval_mode */
4226                 "  , ? "  /*   keep_mails_on_pop_server_after_download */
4227                 "  , ? "  /*   check_interval */
4228                 "  , ? "  /*   auto_download_size */
4229                 "  , ? "  /*   outgoing_server_type */
4230                 "  , ? "  /*   outgoing_server_address */
4231                 "  , ? "  /*   outgoing_server_port_number */
4232                 "  , ? "  /*   outgoing_server_user_name */
4233                 "  , ? "  /*   outgoing_server_password */
4234                 "  , ? "  /*   outgoing_server_secure_connection */
4235                 "  , ? "  /*   outgoing_server_need_authentication */
4236                 "  , ? "  /*   outgoing_server_use_same_authenticator */
4237                 "  , ? "  /*   priority */
4238                 "  , ? "  /*   keep_local_copy */
4239                 "  , ? "  /*   req_delivery_receipt */
4240                 "  , ? "  /*   req_read_receipt */
4241                 "  , ? "  /*   download_limit */
4242                 "  , ? "  /*   block_address */
4243                 "  , ? "  /*   block_subject */
4244                 "  , ? "  /*   display_name_from */
4245                 "  , ? "  /*   reply_with_body */
4246                 "  , ? "  /*   forward_with_files */
4247                 "  , ? "  /*   add_myname_card */
4248                 "  , ? "  /*   add_signature */
4249                 "  , ? "  /*   signature */
4250                 "  , ? "  /*   add_my_address_to_bcc */
4251                 "  , ? "  /*   pop_before_smtp */
4252                 "  , ? "  /*   incoming_server_requires_apop */
4253                 "  , ? "  /*   smime_type */
4254                 "  , ? "  /*   certificate_path */
4255                 "  , ? "  /*   cipher_type */
4256                 "  , ? "  /*   digest_type */
4257                 ") ");
4258
4259         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4260         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4261                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4262
4263         EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
4264         int i = 0;
4265
4266         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_id);
4267         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4268         _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4269         _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4270         _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4271         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4272         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4273         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4274         _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4275         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4276         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4277         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4278         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4279         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4280         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4281         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4282         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4283         _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
4284         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4285         _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
4286         _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
4287         _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
4288         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
4289         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
4290         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4291         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
4292         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
4293         _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
4294         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
4295         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
4296         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
4297         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
4298         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
4299         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
4300         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
4301         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
4302         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
4303         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
4304         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4305         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
4306         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
4307         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
4308         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
4309         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
4310         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
4311         _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
4312         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
4313         _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
4314         _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
4315         _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
4316         _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
4317
4318         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4319
4320         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
4321                 ("sqlite3_step fail:%d", rc));
4322         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4323                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
4324
4325
4326         /*  save passwords to the secure storage */
4327         EM_DEBUG_LOG("save to the secure storage : recv_file[%s], send_file[%s]", recv_password_file_name, send_password_file_name);
4328         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) {
4329                 EM_DEBUG_EXCEPTION("ssm_write_buffer failed [%d] - recv password : file[%s]", error_from_ssm, recv_password_file_name);
4330                 error = EMAIL_ERROR_SYSTEM_FAILURE;
4331                 goto FINISH_OFF;
4332         }
4333         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) {
4334                 EM_DEBUG_EXCEPTION("ssm_write_buffer failed [%d] - send password : file[%s]", error_from_ssm, send_password_file_name);
4335                 error = EMAIL_ERROR_SYSTEM_FAILURE;
4336                 goto FINISH_OFF;
4337         }
4338
4339         ret = true;
4340
4341         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
4342
4343         if (!emcore_notify_storage_event(NOTI_ACCOUNT_ADD, account_tbl->account_id, 0, NULL, 0))
4344                 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_ADD] : Notification failed");
4345
4346 FINISH_OFF:
4347
4348         if (hStmt != NULL)  {
4349                 rc = sqlite3_finalize(hStmt);
4350                 if (rc != SQLITE_OK)  {
4351                         EM_DEBUG_LOG("sqlite3_finalize failed [%d]", rc);
4352                         error = EMAIL_ERROR_DB_FAILURE;
4353                 }
4354         }
4355         else
4356                 EM_DEBUG_LOG("hStmt is NULL!!!");
4357
4358         _DISCONNECT_DB;
4359
4360         if (err_code != NULL)
4361                 *err_code = error;
4362
4363         EM_DEBUG_FUNC_END("ret [%d]", ret);
4364         return ret;
4365 }
4366
4367
4368 INTERNAL_FUNC int emstorage_delete_account(int account_id, int transaction, int *err_code)
4369 {
4370         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
4371
4372         if (account_id < FIRST_ACCOUNT_ID)  {
4373                 EM_DEBUG_EXCEPTION(" account_id[%d]", account_id);
4374
4375                 if (err_code != NULL)
4376                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4377                 return false;
4378         }
4379
4380         int rc = -1, ret = false;
4381         int error = EMAIL_ERROR_NONE;
4382         sqlite3 *local_db_handle = emstorage_get_db_connection();
4383         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
4384
4385         /*  TODO : delete password files - file names can be obtained from db or a rule that makes a name */
4386         DB_STMT hStmt = NULL;
4387         char sql_query_string[QUERY_SIZE] = {0, };
4388         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4389         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4390
4391         /*  get password file name */
4392         if ((error = _get_password_file_name(account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4393                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4394                 goto FINISH_OFF;
4395         }
4396
4397         /*  delete from db */
4398         memset(sql_query_string, 0x00, sizeof(sql_query_string));
4399         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_account_tbl WHERE account_id = %d", account_id);
4400
4401         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
4402         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
4403                 ("sqlite3_exec fail:%d", rc));
4404         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4405                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4406
4407         /*  validate account existence */
4408         rc = sqlite3_changes(local_db_handle);
4409         if (rc == 0)  {
4410                 EM_DEBUG_EXCEPTION(" no matched account found...");
4411                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4412                 goto FINISH_OFF;
4413         }
4414
4415                 /*  delete from secure storage */
4416         if (ssm_delete_file(recv_password_file_name,  SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
4417                 EM_DEBUG_EXCEPTION(" ssm_delete_file failed -recv password : file[%s]", recv_password_file_name);
4418                 error = EMAIL_ERROR_SYSTEM_FAILURE;
4419                 goto FINISH_OFF;
4420         }
4421         if (ssm_delete_file(send_password_file_name,  SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
4422                 EM_DEBUG_EXCEPTION(" ssm_delete_file failed -send password : file[%s]", send_password_file_name);
4423                 error = EMAIL_ERROR_SYSTEM_FAILURE;
4424                 goto FINISH_OFF;
4425         }
4426         ret = true;
4427
4428 FINISH_OFF:
4429
4430         if (hStmt != NULL)  {
4431                 rc = sqlite3_finalize(hStmt);
4432                 if (rc != SQLITE_OK)  {
4433                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
4434                         error = EMAIL_ERROR_DB_FAILURE;
4435                 }
4436         }
4437
4438         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
4439         _DISCONNECT_DB;
4440
4441         if (err_code != NULL)
4442                 *err_code = error;
4443
4444         EM_DEBUG_FUNC_END("ret [%d]", ret);
4445         return ret;
4446 }
4447
4448
4449 INTERNAL_FUNC int emstorage_free_account(emstorage_account_tbl_t** account_list, int count, int *err_code)
4450 {
4451         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%d], err_code[%p]", account_list, count, err_code);
4452
4453         int ret = false;
4454         int error = EMAIL_ERROR_NONE;
4455
4456         if (count > 0)  {
4457                 if (!account_list || !*account_list)  {
4458                         EM_DEBUG_EXCEPTION("account_list[%p], count[%d]", account_list, count);
4459                         error = EMAIL_ERROR_INVALID_PARAM;
4460                         goto FINISH_OFF;
4461                 }
4462
4463                 emstorage_account_tbl_t* p = *account_list;
4464                 int i = 0;
4465
4466                 for (; i < count; i++)  {
4467                         EM_SAFE_FREE(p[i].account_name);
4468                         EM_SAFE_FREE(p[i].incoming_server_address);
4469                         EM_SAFE_FREE(p[i].user_email_address);
4470                         EM_SAFE_FREE(p[i].user_data);
4471                         EM_SAFE_FREE(p[i].incoming_server_user_name);
4472                         EM_SAFE_FREE(p[i].incoming_server_password);
4473                         EM_SAFE_FREE(p[i].outgoing_server_address);
4474                         EM_SAFE_FREE(p[i].outgoing_server_user_name);
4475                         EM_SAFE_FREE(p[i].outgoing_server_password);
4476                         EM_SAFE_FREE(p[i].user_display_name);
4477                         EM_SAFE_FREE(p[i].reply_to_address);
4478                         EM_SAFE_FREE(p[i].return_address);
4479                         EM_SAFE_FREE(p[i].logo_icon_path);
4480                         EM_SAFE_FREE(p[i].options.display_name_from);
4481                         EM_SAFE_FREE(p[i].options.signature);
4482                         EM_SAFE_FREE(p[i].certificate_path);
4483                 }
4484
4485                 EM_SAFE_FREE(p);
4486                 *account_list = NULL;
4487         }
4488
4489         ret = true;
4490
4491 FINISH_OFF:
4492         if (err_code != NULL)
4493                 *err_code = error;
4494
4495         EM_DEBUG_FUNC_END("ret [%d]", ret);
4496         return ret;
4497 }
4498
4499 INTERNAL_FUNC int emstorage_get_mailbox_count(int account_id, int local_yn, int *count, int transaction, int *err_code)
4500 {
4501         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);
4502
4503         if ((account_id < FIRST_ACCOUNT_ID) || (count == NULL))  {
4504                 EM_DEBUG_EXCEPTION(" account_list[%d], local_yn[%d], count[%p]", account_id, local_yn, count);
4505
4506                 if (err_code != NULL)
4507                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4508                 return false;
4509         }
4510
4511         int rc = -1, ret = false;
4512         int error = EMAIL_ERROR_NONE;
4513         char sql_query_string[QUERY_SIZE] = {0, };
4514
4515         sqlite3 *local_db_handle = emstorage_get_db_connection();
4516         EMSTORAGE_START_READ_TRANSACTION(transaction);
4517
4518         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);
4519
4520         char **result;
4521
4522         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
4523         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
4524                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4525
4526         *count = atoi(result[1]);
4527         sqlite3_free_table(result);
4528
4529
4530         ret = true;
4531
4532 FINISH_OFF:
4533
4534         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4535         _DISCONNECT_DB;
4536
4537         if (err_code != NULL)
4538                 *err_code = error;
4539
4540         EM_DEBUG_FUNC_END("ret [%d]", ret);
4541         return ret;
4542 }
4543
4544 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)
4545 {
4546         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);
4547
4548         if (!select_num || !mailbox_list) {
4549                 EM_DEBUG_EXCEPTION("Invalid parameters");
4550
4551                 if (err_code != NULL)
4552                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4553
4554                 return false;
4555         }
4556
4557         int ret = false;
4558         int error = EMAIL_ERROR_NONE;
4559         char conditional_clause_string[QUERY_SIZE] = {0, };
4560         char ordering_clause_string[QUERY_SIZE] = {0, };
4561
4562         if (account_id == ALL_ACCOUNT) {
4563                 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
4564                         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);
4565         }
4566         else {
4567                 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d  ", account_id);
4568                 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
4569                         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);
4570         }
4571
4572         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
4573
4574         switch (sort_type) {
4575                 case EMAIL_MAILBOX_SORT_BY_NAME_ASC :
4576                         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name ASC");
4577                         break;
4578
4579                 case EMAIL_MAILBOX_SORT_BY_NAME_DSC :
4580                         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name DESC");
4581                         break;
4582
4583                 case EMAIL_MAILBOX_SORT_BY_TYPE_ASC :
4584                         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type ASC");
4585                         break;
4586
4587                 case EMAIL_MAILBOX_SORT_BY_TYPE_DSC :
4588                         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type DEC");
4589                         break;
4590         }
4591
4592         EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
4593
4594         if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, ordering_clause_string, 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
4595                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4596                 goto FINISH_OFF;
4597         }
4598
4599         ret = true;
4600
4601 FINISH_OFF:
4602
4603         if (err_code != NULL)
4604                 *err_code = error;
4605
4606         EM_DEBUG_FUNC_END("ret [%d]", ret);
4607         return ret;
4608 }
4609
4610 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)
4611 {
4612         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);
4613
4614         int ret = false;
4615         int error = EMAIL_ERROR_NONE;
4616         int where_clause_count = 0;
4617         char conditional_clause_string[QUERY_SIZE] = {0, };
4618         char ordering_clause_string[QUERY_SIZE] = {0, };
4619
4620         if (!select_num || !mailbox_list) {
4621                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4622                 error = EMAIL_ERROR_INVALID_PARAM;
4623                 goto FINISH_OFF;
4624         }
4625
4626         if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL) {
4627                 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);
4628                 where_clause_count++;
4629         }
4630
4631         if (account_id > 0) {
4632                 if (where_clause_count == 0) {
4633                         SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " WHERE ");
4634                         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);
4635                 }
4636                 else
4637                         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);
4638         }
4639
4640         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY MBT.mailbox_name ");
4641         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
4642         EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
4643
4644         if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, ordering_clause_string, 1, 1, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
4645                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4646                 goto FINISH_OFF;
4647         }
4648
4649         ret = true;
4650
4651 FINISH_OFF:
4652
4653         if (err_code != NULL)
4654                 *err_code = error;
4655
4656         EM_DEBUG_FUNC_END("ret [%d]", ret);
4657         return ret;
4658 }
4659
4660 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)
4661 {
4662         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);
4663         if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list)  {
4664                 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);
4665
4666                 if (err_code != NULL)
4667                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4668                 return false;
4669         }
4670
4671         int ret = false;
4672         int error = EMAIL_ERROR_NONE;
4673         char conditional_clause_string[QUERY_SIZE] = {0, };
4674
4675         if (parent_mailbox_name)
4676                 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d  AND UPPER(mailbox_name) LIKE UPPER('%s/%%')", account_id, parent_mailbox_name);
4677         else
4678                 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d  AND (mailbox_name NOT LIKE '%%/%%')", account_id);
4679
4680         EM_DEBUG_LOG("conditional_clause_string", conditional_clause_string);
4681
4682         if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, " ORDER BY mailbox_name", 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
4683                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4684                 goto FINISH_OFF;
4685         }
4686
4687         ret = true;
4688
4689 FINISH_OFF:
4690
4691         if (err_code != NULL)
4692                 *err_code = error;
4693
4694         EM_DEBUG_FUNC_END("ret [%d]", ret);
4695         return ret;
4696 }
4697
4698 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)
4699 {
4700         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);
4701         if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list)  {
4702                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4703
4704                 if (err_code != NULL)
4705                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4706
4707                 return false;
4708         }
4709
4710         int ret = false;
4711         int error = EMAIL_ERROR_NONE;
4712         char conditional_clause_string[QUERY_SIZE] = {0, };
4713
4714         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND modifiable_yn = 0", account_id);
4715         EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
4716
4717         if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, " ORDER BY mailbox_name", 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
4718                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4719                 goto FINISH_OFF;
4720         }
4721
4722         ret = true;
4723
4724 FINISH_OFF:
4725
4726         if (err_code != NULL)
4727                 *err_code = error;
4728
4729         EM_DEBUG_FUNC_END("ret [%d]", ret);
4730         return ret;
4731 }
4732
4733 INTERNAL_FUNC int emstorage_stamp_last_sync_time_of_mailbox(int input_mailbox_id, int input_transaction)
4734 {
4735         EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_transaction [%d]", input_mailbox_id, input_transaction);
4736
4737         int      result_code = false;
4738         int      error = EMAIL_ERROR_NONE;
4739         int      rc;
4740         time_t   current_time = 0;
4741         char     sql_query_string[QUERY_SIZE] = {0, };
4742         sqlite3 *local_db_handle = NULL;
4743
4744         if (!input_mailbox_id )  {
4745                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4746                 return EMAIL_ERROR_INVALID_PARAM;
4747         }
4748
4749         time(&current_time);
4750
4751         local_db_handle = emstorage_get_db_connection();
4752
4753         EMSTORAGE_START_WRITE_TRANSACTION(input_transaction, error);
4754
4755         SNPRINTF(sql_query_string, sizeof(sql_query_string),
4756                 "UPDATE mail_box_tbl SET"
4757                 " last_sync_time = %d"
4758                 " WHERE mailbox_id = %d"
4759                 , (int)current_time
4760                 , input_mailbox_id);
4761
4762         EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
4763
4764         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
4765         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4766                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4767
4768 FINISH_OFF:
4769
4770         if(error == EMAIL_ERROR_NONE)
4771                 result_code = true;
4772
4773         EMSTORAGE_FINISH_WRITE_TRANSACTION(input_transaction, result_code, error);
4774         _DISCONNECT_DB;
4775
4776         EM_DEBUG_FUNC_END("error [%d]", error);
4777         return error;
4778 }
4779
4780 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)
4781 {
4782         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);
4783         EM_PROFILE_BEGIN(profile_emstorage_get_mailbox_by_name);
4784
4785         if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !result_mailbox)  {
4786                 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], mailbox_name[%s], result_mailbox[%p]", account_id, local_yn, mailbox_name, result_mailbox);
4787
4788                 if (err_code != NULL)
4789                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4790                 return false;
4791         }
4792
4793         int ret = false;
4794         int error = EMAIL_ERROR_NONE;
4795         int result_count = 0;
4796         char conditional_clause_string[QUERY_SIZE] = {0, };
4797
4798         if(strcmp(mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
4799                 if (!(*result_mailbox = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t))))  {
4800                         EM_DEBUG_EXCEPTION("malloc failed...");
4801                         error = EMAIL_ERROR_OUT_OF_MEMORY;
4802                         goto FINISH_OFF;
4803                 }
4804
4805                 (*result_mailbox)->mailbox_id                 = 0;
4806                 (*result_mailbox)->account_id                 = account_id;
4807                 (*result_mailbox)->local_yn                   = 1;
4808                 (*result_mailbox)->mailbox_name               = EM_SAFE_STRDUP(mailbox_name);
4809                 (*result_mailbox)->mailbox_type               = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
4810                 (*result_mailbox)->alias                      = EM_SAFE_STRDUP(mailbox_name);
4811                 (*result_mailbox)->deleted_flag               = 0;
4812                 (*result_mailbox)->modifiable_yn              = 1;
4813                 (*result_mailbox)->total_mail_count_on_server = 1;
4814                 (*result_mailbox)->has_archived_mails         = 0;
4815                 (*result_mailbox)->mail_slot_size             = 0x0FFFFFFF;
4816                 (*result_mailbox)->no_select                  = 0;
4817         }
4818         else {
4819
4820                 if (local_yn == -1)
4821                         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND mailbox_name = '%s'", account_id, mailbox_name);
4822                 else
4823                         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND local_yn = %d AND mailbox_name = '%s'", account_id, local_yn, mailbox_name);
4824
4825                 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
4826
4827                 if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, "", 0, transaction, result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
4828                         EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4829                         goto FINISH_OFF;
4830                 }
4831         }
4832
4833         ret = true;
4834
4835 FINISH_OFF:
4836
4837         if (err_code != NULL)
4838                 *err_code = error;
4839
4840         EM_PROFILE_END(profile_emstorage_get_mailbox_by_name);
4841         EM_DEBUG_FUNC_END("ret [%d]", ret);
4842         return ret;
4843 }
4844
4845 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)
4846 {
4847         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);
4848
4849         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_USER_DEFINED) || !mailbox_name) {
4850
4851                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
4852
4853                 if (err_code != NULL)
4854                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4855                 return false;
4856         }
4857
4858         int rc, ret = false;
4859         int error = EMAIL_ERROR_NONE;
4860         emstorage_mailbox_tbl_t *p_data_tbl = NULL;
4861         emstorage_account_tbl_t *account = NULL;
4862         DB_STMT hStmt = NULL;
4863         char sql_query_string[QUERY_SIZE] = {0,};
4864         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 ";
4865
4866         sqlite3 *local_db_handle = emstorage_get_db_connection();
4867         EMSTORAGE_START_READ_TRANSACTION(transaction);
4868
4869         /* validate account */
4870         /*  Check whether the account exists. */
4871         if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
4872                 EM_DEBUG_EXCEPTION(" emstorage_get_account_by_id failed - %d", error);
4873                 goto FINISH_OFF;
4874         }
4875
4876         if (account)
4877                 emstorage_free_account(&account, 1, NULL);
4878
4879         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);
4880
4881         EM_DEBUG_LOG("query = [%s]", sql_query_string);
4882
4883
4884         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4885         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)));
4886
4887
4888         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4889         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
4890
4891         if (rc == SQLITE_DONE) {
4892                 EM_DEBUG_EXCEPTION(" no matched mailbox_name found...");
4893                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
4894                 goto FINISH_OFF;
4895         }
4896
4897
4898         if (!(p_data_tbl = (emstorage_mailbox_tbl_t*)malloc(sizeof(emstorage_mailbox_tbl_t))))  {
4899                 EM_DEBUG_EXCEPTION(" malloc failed...");
4900                 error = EMAIL_ERROR_OUT_OF_MEMORY;
4901                 goto FINISH_OFF;
4902         }
4903
4904         memset(p_data_tbl, 0x00, sizeof(emstorage_mailbox_tbl_t));
4905
4906         int col_index = 0;
4907
4908         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
4909         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
4910         _get_stmt_field_data_int(hStmt, &(p_data_tbl->local_yn), col_index++);
4911         _get_stmt_field_data_string(hStmt, &(p_data_tbl->mailbox_name), 0, col_index++);
4912         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->mailbox_type), col_index++);
4913         _get_stmt_field_data_string(hStmt, &(p_data_tbl->alias), 0, col_index++);
4914         _get_stmt_field_data_int(hStmt, &(p_data_tbl->deleted_flag), col_index++);
4915         _get_stmt_field_data_int(hStmt, &(p_data_tbl->modifiable_yn), col_index++);
4916         _get_stmt_field_data_int(hStmt, &(p_data_tbl->total_mail_count_on_server), col_index++);
4917         _get_stmt_field_data_int(hStmt, &(p_data_tbl->has_archived_mails), col_index++);
4918         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_slot_size), col_index++);
4919         _get_stmt_field_data_int(hStmt, &(p_data_tbl->no_select), col_index++);
4920         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->last_sync_time), col_index++);
4921
4922         ret = true;
4923
4924 FINISH_OFF:
4925         if (ret == true)
4926                 *mailbox_name = p_data_tbl;
4927
4928         if (hStmt != NULL)  {
4929                 rc = sqlite3_finalize(hStmt);
4930                 if (rc != SQLITE_OK)  {
4931                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
4932                         error = EMAIL_ERROR_DB_FAILURE;
4933                 }
4934         }
4935
4936         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4937         _DISCONNECT_DB;
4938
4939         if (err_code != NULL)
4940                 *err_code = error;
4941
4942         EM_DEBUG_FUNC_END("ret [%d]", ret);
4943         return ret;
4944 }
4945
4946 INTERNAL_FUNC int emstorage_get_mailbox_by_id(int input_mailbox_id, emstorage_mailbox_tbl_t** output_mailbox)
4947 {
4948         EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_mailbox[%p]", input_mailbox_id, output_mailbox);
4949
4950         if (input_mailbox_id <= 0 || !output_mailbox)  {
4951                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4952                 return  EMAIL_ERROR_INVALID_PARAM;
4953         }
4954
4955         int  ret = EMAIL_ERROR_NONE;
4956         int  result_count = 0;
4957         char conditional_clause_string[QUERY_SIZE] = {0, };
4958
4959         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE mailbox_id = %d", input_mailbox_id);
4960
4961         EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
4962
4963         if( (ret = emstorage_query_mailbox_tbl(conditional_clause_string, "", false, false, output_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
4964                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", ret);
4965                 goto FINISH_OFF;
4966         }
4967
4968 FINISH_OFF:
4969
4970         EM_DEBUG_FUNC_END("ret [%d]", ret);
4971         return ret;
4972 }
4973
4974 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)
4975 {
4976         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);
4977         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_id)  {
4978                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_id[%p]", account_id, mailbox_type, mailbox_id);
4979                 if (err_code != NULL)
4980                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4981                 return false;
4982         }
4983
4984         int rc, ret = false;
4985         int error = EMAIL_ERROR_NONE;
4986         emstorage_account_tbl_t* account = NULL;
4987         DB_STMT hStmt = NULL;
4988         char sql_query_string[QUERY_SIZE] = {0, };
4989
4990         sqlite3 *local_db_handle = emstorage_get_db_connection();
4991         EMSTORAGE_START_READ_TRANSACTION(transaction);
4992
4993         /*  Check whether the account exists. */
4994         if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
4995                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
4996                 goto FINISH_OFF;
4997         }
4998
4999         if (account )
5000                 emstorage_free_account(&account, 1, NULL);
5001
5002         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);
5003
5004         EM_DEBUG_LOG("query = [%s]", sql_query_string);
5005
5006
5007         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5008
5009         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5010                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5011
5012
5013         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5014         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5015                 ("sqlite3_step fail:%d", rc));
5016
5017         if (rc == SQLITE_DONE) {
5018                 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5019                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5020                 goto FINISH_OFF;
5021         }
5022
5023         _get_stmt_field_data_int(hStmt, mailbox_id, 0);
5024
5025         ret = true;
5026
5027 FINISH_OFF:
5028         if (hStmt != NULL)  {
5029                 rc = sqlite3_finalize(hStmt);
5030                 if (rc != SQLITE_OK)  {
5031                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5032                         error = EMAIL_ERROR_DB_FAILURE;
5033                 }
5034         }
5035
5036         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5037         _DISCONNECT_DB;
5038
5039         if (err_code != NULL)
5040                 *err_code = error;
5041
5042         EM_DEBUG_FUNC_END("ret [%d]", ret);
5043         return ret;
5044 }
5045
5046 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)
5047 {
5048         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);
5049         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_name)  {
5050                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
5051                 if (err_code != NULL)
5052                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5053                 return false;
5054         }
5055
5056         int rc, ret = false;
5057         int error = EMAIL_ERROR_NONE;
5058         emstorage_account_tbl_t* account = NULL;
5059         DB_STMT hStmt = NULL;
5060         char sql_query_string[QUERY_SIZE] = {0, };
5061
5062         sqlite3 *local_db_handle = emstorage_get_db_connection();
5063         EMSTORAGE_START_READ_TRANSACTION(transaction);
5064
5065         /*  Check whether the account exists. */
5066         if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
5067                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5068                 goto FINISH_OFF;
5069         }
5070
5071         if (account )
5072                 emstorage_free_account(&account, 1, NULL);
5073
5074         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);
5075
5076         EM_DEBUG_LOG("query = [%s]", sql_query_string);
5077
5078
5079         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5080
5081         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5082                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5083
5084
5085         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5086         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5087                 ("sqlite3_step fail:%d", rc));
5088
5089         if (rc == SQLITE_DONE) {
5090                 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5091                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5092                 goto FINISH_OFF;
5093         }
5094
5095         _get_stmt_field_data_string(hStmt, mailbox_name, 0, 0);
5096
5097         ret = true;
5098
5099 FINISH_OFF:
5100         if (hStmt != NULL)  {
5101                 rc = sqlite3_finalize(hStmt);
5102                 if (rc != SQLITE_OK)  {
5103                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5104                         error = EMAIL_ERROR_DB_FAILURE;
5105                 }
5106         }
5107
5108         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5109         _DISCONNECT_DB;
5110
5111         if (err_code != NULL)
5112                 *err_code = error;
5113
5114         EM_DEBUG_FUNC_END("ret [%d]", ret);
5115         return ret;
5116 }
5117
5118 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)
5119 {
5120         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);
5121         int rc, ret = false;
5122         int error = EMAIL_ERROR_NONE;
5123         char sql_query_string[QUERY_SIZE] = {0, };
5124
5125         sqlite3 *local_db_handle = emstorage_get_db_connection();
5126
5127         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5128
5129         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5130                 "UPDATE mail_box_tbl SET"
5131                 " modifiable_yn = %d"
5132                 " WHERE account_id = %d"
5133                 " AND local_yn = %d"
5134                 " AND mailbox_name = '%s'"
5135                 , modifiable_yn
5136                 , account_id
5137                 , local_yn
5138                 , mailbox_name);
5139         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5140         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5141                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5142
5143         ret = true;
5144
5145 FINISH_OFF:
5146         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5147         _DISCONNECT_DB;
5148
5149         if (err_code != NULL)
5150                 *err_code = error;
5151
5152         EM_DEBUG_FUNC_END("ret [%d]", ret);
5153         return ret;
5154
5155 }
5156
5157
5158 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)
5159 {
5160         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);
5161         int rc, ret = false;
5162         int error = EMAIL_ERROR_NONE;
5163         char sql_query_string[QUERY_SIZE] = {0, };
5164
5165         if (account_id <= 0 || input_mailbox_id <= 0)  {
5166                 EM_DEBUG_EXCEPTION("account_id[%d], input_mailbox_id[%d]", account_id, input_mailbox_id);
5167
5168                 if (err_code != NULL)
5169                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5170                 return false;
5171         }
5172         sqlite3 *local_db_handle = emstorage_get_db_connection();
5173
5174         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5175
5176         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5177                 "UPDATE mail_box_tbl SET"
5178                 " total_mail_count_on_server = %d"
5179                 " WHERE account_id = %d"
5180                 " AND mailbox_id = %d"
5181                 , total_count_on_server
5182                 , account_id
5183                 , input_mailbox_id);
5184         EM_DEBUG_LOG("query[%s]", sql_query_string);
5185         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5186         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5187                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5188
5189         ret = true;
5190
5191 FINISH_OFF:
5192         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5193         _DISCONNECT_DB;
5194
5195         if (err_code != NULL)
5196                 *err_code = error;
5197
5198         EM_DEBUG_FUNC_END("ret [%d]", ret);
5199         return ret;
5200
5201 }
5202
5203
5204 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)
5205 {
5206         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);
5207
5208         if (account_id < FIRST_ACCOUNT_ID || input_mailbox_id <= 0 || !result_mailbox)  {
5209                 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);
5210
5211                 if (err_code != NULL)
5212                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5213                 return false;
5214         }
5215
5216         int rc, ret = false;
5217         int error = EMAIL_ERROR_NONE;
5218         char sql_query_string[QUERY_SIZE] = {0, };
5219         DB_STMT hStmt = NULL;
5220         int i = 0;
5221
5222         sqlite3 *local_db_handle = emstorage_get_db_connection();
5223         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5224
5225         if (local_yn != -1) {
5226                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5227                         "UPDATE mail_box_tbl SET"
5228                         "  mailbox_id = ?"
5229                         ", mailbox_name = ?"
5230                         ", mailbox_type = ?"
5231                         ", alias = ?"
5232                         ", deleted_flag = ?"
5233                         ", modifiable_yn= ?"
5234                         ", mail_slot_size= ?"
5235                         ", total_mail_count_on_server = ?"
5236                         " WHERE account_id = %d"
5237                         " AND local_yn = %d"
5238                         " AND mailbox_id = '%d'"
5239                         , account_id
5240                         , local_yn
5241                         , input_mailbox_id);
5242         }
5243         else {
5244                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5245                         "UPDATE mail_box_tbl SET"
5246                         "  mailbox_id = ?"
5247                         ", mailbox_name = ?"
5248                         ", mailbox_type = ?"
5249                         ", alias = ?"
5250                         ", deleted_flag = ?"
5251                         ", modifiable_yn= ?"
5252                         ", mail_slot_size= ?"
5253                         ", total_mail_count_on_server = ?"
5254                         " WHERE account_id = %d"
5255                         " AND mailbox_id = '%d'"
5256                         , account_id
5257                         , input_mailbox_id);
5258         }
5259
5260
5261
5262         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5263         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5264                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5265
5266         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_id);
5267         _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);
5268         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_type);
5269         _bind_stmt_field_data_string(hStmt, i++, (char *)result_mailbox->alias ? result_mailbox->alias : "", 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
5270         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->deleted_flag);
5271         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->modifiable_yn);
5272         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mail_slot_size);
5273         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->total_mail_count_on_server);
5274
5275
5276         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5277         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
5278                 ("sqlite3_step fail:%d", rc));
5279         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5280                 ("sqlite3_step fail:%d", rc));
5281
5282         ret = true;
5283
5284 FINISH_OFF:
5285         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5286         _DISCONNECT_DB;
5287
5288         if (hStmt != NULL)  {
5289                 rc = sqlite3_finalize(hStmt);
5290                 if (rc != SQLITE_OK)  {
5291                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5292                         error = EMAIL_ERROR_DB_FAILURE;
5293                 }
5294         }
5295
5296         if (err_code != NULL)
5297                 *err_code = error;
5298
5299         EM_DEBUG_FUNC_END("ret [%d]", ret);
5300         return ret;
5301 }
5302
5303
5304 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)
5305 {
5306         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);
5307
5308         if (account_id < FIRST_ACCOUNT_ID || !mailbox_name)  {
5309                 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], mailbox_name[%s]", account_id, local_yn, mailbox_name);
5310
5311                 if (err_code != NULL)
5312                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5313                 return false;
5314         }
5315
5316         int rc, ret = false;
5317         int error = EMAIL_ERROR_NONE;
5318         char sql_query_string[QUERY_SIZE] = {0, };
5319
5320         sqlite3 *local_db_handle = emstorage_get_db_connection();
5321         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5322
5323         EM_DEBUG_LOG("emstorage_update_mailbox_type");
5324
5325         DB_STMT hStmt_box_tbl = NULL;
5326         DB_STMT hStmt_mail_tbl = NULL;
5327         int i = 0;
5328
5329         /*  Update mail_box_tbl */
5330         if (local_yn != -1) {
5331                 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
5332                         "UPDATE mail_box_tbl SET"
5333                         " mailbox_type = ?"
5334                         " WHERE account_id = %d"
5335                         " AND local_yn = %d"
5336                         " AND mailbox_name = '%s'"
5337                         , account_id
5338                         , local_yn
5339                         , mailbox_name);
5340         }
5341         else {
5342                 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
5343                         "UPDATE mail_box_tbl SET"
5344                         " mailbox_type = ?"
5345                         " WHERE account_id = %d"
5346                         " AND mailbox_name = '%s'"
5347                         , account_id
5348                         , mailbox_name);
5349         }
5350
5351         EM_DEBUG_LOG("SQL(%s)", sql_query_string);
5352
5353         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_box_tbl, NULL), rc);
5354
5355         if(SQLITE_OK != rc) {
5356                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
5357                 error = EMAIL_ERROR_DB_FAILURE;
5358                 goto FINISH_OFF; 
5359         }
5360
5361         _bind_stmt_field_data_int(hStmt_box_tbl, i++, new_mailbox_type);
5362
5363
5364         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_box_tbl), rc);
5365
5366         if(rc == SQLITE_FULL) {
5367                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
5368                 error   = EMAIL_ERROR_MAIL_MEMORY_FULL;
5369                 goto FINISH_OFF; 
5370         }
5371         
5372         if(rc != SQLITE_ROW && rc != SQLITE_DONE) {
5373                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
5374                 error = EMAIL_ERROR_DB_FAILURE;
5375                 goto FINISH_OFF; 
5376         }
5377         
5378
5379         /*  Update mail_tbl */
5380         i = 0;
5381         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5382                         "UPDATE mail_tbl SET"
5383                         " mailbox_type = ?"
5384                         " WHERE account_id = %d"
5385                         " AND mailbox_name = '%s'"
5386                         , account_id
5387                         , mailbox_name);
5388
5389         EM_DEBUG_LOG("SQL[%s]", sql_query_string);
5390
5391         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_mail_tbl, NULL), rc);
5392         if(SQLITE_OK != rc) {
5393                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
5394                 error = EMAIL_ERROR_DB_FAILURE;
5395                 goto FINISH_OFF; 
5396         }
5397         
5398         _bind_stmt_field_data_int(hStmt_mail_tbl, i++, new_mailbox_type);
5399
5400         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_mail_tbl), rc);
5401         if(rc == SQLITE_FULL) {
5402                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
5403                 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
5404                 goto FINISH_OFF; 
5405         }
5406         
5407         if(rc != SQLITE_ROW && rc != SQLITE_DONE){
5408                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
5409                 error = EMAIL_ERROR_DB_FAILURE;
5410                 goto FINISH_OFF; 
5411         }
5412
5413         ret = true;
5414
5415 FINISH_OFF:
5416         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5417         _DISCONNECT_DB;
5418
5419         if (hStmt_box_tbl != NULL)  {
5420                 rc = sqlite3_finalize(hStmt_box_tbl);
5421                 if (rc != SQLITE_OK)  {
5422                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5423                         error = EMAIL_ERROR_DB_FAILURE;
5424                 }
5425         }
5426
5427         if (hStmt_mail_tbl != NULL)  {
5428                 rc = sqlite3_finalize(hStmt_mail_tbl);
5429                 if (rc != SQLITE_OK)  {
5430                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5431                         error = EMAIL_ERROR_DB_FAILURE;
5432                 }
5433         }
5434
5435         if (err_code != NULL)
5436                 *err_code = error;
5437
5438         EM_DEBUG_FUNC_END("ret [%d]", ret);
5439         return ret;
5440 }
5441
5442 INTERNAL_FUNC int emstorage_set_local_mailbox(int input_mailbox_id, int input_is_local_mailbox, int transaction)
5443 {
5444         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);
5445
5446         int rc, ret = false;
5447         int error = EMAIL_ERROR_NONE;
5448         char sql_query_string[QUERY_SIZE] = {0, };
5449
5450         if (input_mailbox_id < 0)  {
5451                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5452                 return EMAIL_ERROR_INVALID_PARAM;
5453         }
5454
5455         sqlite3 *local_db_handle = emstorage_get_db_connection();
5456
5457         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5458
5459         EM_DEBUG_LOG("emstorage_update_mailbox_type");
5460
5461         DB_STMT hStmt = NULL;
5462         int i = 0;
5463
5464         /*  Update mail_box_tbl */
5465         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5466                 "UPDATE mail_box_tbl SET"
5467                 " local_yn = ?"
5468                 " WHERE mailbox_id = %d"
5469                 , input_mailbox_id);
5470
5471         EM_DEBUG_LOG("SQL(%s)", sql_query_string);
5472
5473         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5474         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5475                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5476
5477         _bind_stmt_field_data_int(hStmt, i++, input_is_local_mailbox);
5478
5479         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5480         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
5481                 ("sqlite3_step fail:%d", rc));
5482         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5483                 ("sqlite3_step fail:%d", rc));
5484
5485         if (hStmt != NULL)  {
5486                 rc = sqlite3_finalize(hStmt);
5487                 if (rc != SQLITE_OK)  {
5488                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5489                         error = EMAIL_ERROR_DB_FAILURE;
5490                 }
5491                 hStmt = NULL;
5492         }
5493
5494         ret = true;
5495
5496 FINISH_OFF:
5497         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5498         _DISCONNECT_DB;
5499
5500         if (hStmt != NULL)  {
5501                 rc = sqlite3_finalize(hStmt);
5502                 if (rc != SQLITE_OK)  {
5503                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5504                         error = EMAIL_ERROR_DB_FAILURE;
5505                 }
5506         }
5507
5508         EM_DEBUG_FUNC_END("error [%d]", error);
5509         return error;
5510 }
5511
5512 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)
5513 {
5514         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);
5515         int i = 0;
5516         int err = EMAIL_ERROR_NONE;
5517         int rc = 0;
5518         int result = false;
5519         int cur_mailbox_id_string = 0;
5520         int mailbox_id_string_buffer_length = 0;
5521         char  sql_query_string[QUERY_SIZE] = {0, };
5522         char *mailbox_id_string_buffer = NULL;
5523         char *parameter_string = NULL;
5524         sqlite3 *local_db_handle = NULL;
5525
5526         if (input_mailbox_id_array == NULL || input_mailbox_id_count == 0 || input_field_name == NULL) {
5527                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5528                 return EMAIL_ERROR_INVALID_PARAM;
5529         }
5530
5531         local_db_handle = emstorage_get_db_connection();
5532
5533         /* Generating mail id list string */
5534         mailbox_id_string_buffer_length = MAILBOX_ID_STRING_LENGTH * input_mailbox_id_count;
5535
5536         mailbox_id_string_buffer = em_malloc(mailbox_id_string_buffer_length);
5537
5538         if(!mailbox_id_string_buffer) {
5539                 EM_DEBUG_EXCEPTION("em_malloc failed");
5540                 err = EMAIL_ERROR_OUT_OF_MEMORY;
5541                 goto FINISH_OFF;
5542         }
5543
5544         for(i = 0; i < input_mailbox_id_count; i++)
5545                 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]);
5546
5547         if(EM_SAFE_STRLEN(mailbox_id_string_buffer) > 1)
5548                 mailbox_id_string_buffer[EM_SAFE_STRLEN(mailbox_id_string_buffer) - 1] = NULL_CHAR;
5549
5550         /* Generating notification parameter string */
5551         parameter_string = em_malloc(mailbox_id_string_buffer_length + EM_SAFE_STRLEN(input_field_name) + 2);
5552
5553         if(!parameter_string) {
5554                 EM_DEBUG_EXCEPTION("em_malloc failed");
5555                 err = EMAIL_ERROR_OUT_OF_MEMORY;
5556                 goto FINISH_OFF;
5557         }
5558
5559         SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", input_field_name, 0x01, mailbox_id_string_buffer);
5560
5561         /* Write query string */
5562         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);
5563
5564         EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
5565
5566         /* Execute query */
5567         EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
5568         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5569         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5570                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5571         if (sqlite3_changes(local_db_handle) == 0)
5572                 EM_DEBUG_LOG("no mail matched...");
5573
5574         result = true;
5575
5576 FINISH_OFF:
5577         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, result, err);
5578         _DISCONNECT_DB;
5579
5580         if (result && parameter_string && !emcore_notify_storage_event(NOTI_MAILBOX_FIELD_UPDATE, input_account_id, 0, parameter_string, input_value))
5581                 EM_DEBUG_EXCEPTION("emcore_notify_storage_event failed : NOTI_MAIL_FIELD_UPDATE [%s,%d]", input_field_name, input_value);
5582
5583         EM_SAFE_FREE(mailbox_id_string_buffer);
5584         EM_SAFE_FREE(parameter_string);
5585
5586         EM_DEBUG_FUNC_END("err [%d]", err);
5587         return err;
5588 }
5589
5590 INTERNAL_FUNC int emstorage_add_mailbox(emstorage_mailbox_tbl_t* mailbox_tbl, int transaction, int *err_code)
5591 {
5592         EM_DEBUG_FUNC_BEGIN("mailbox_tbl[%p], transaction[%d], err_code[%p]", mailbox_tbl, transaction, err_code);
5593
5594         if (!mailbox_tbl)  {
5595                 if (err_code != NULL)
5596                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5597                 return false;
5598         }
5599
5600         int rc, ret = false;
5601         int error = EMAIL_ERROR_NONE;
5602         DB_STMT hStmt = NULL;
5603         char sql_query_string[QUERY_SIZE] = {0,};
5604         char **result = NULL;
5605         time_t current_time;
5606         sqlite3 *local_db_handle = emstorage_get_db_connection();
5607         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5608
5609         EM_SAFE_STRCPY(sql_query_string, "SELECT max(rowid) FROM mail_box_tbl;");
5610
5611         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5612         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
5613                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5614
5615         time(&current_time);
5616
5617         if (NULL == result[1])
5618                 rc = 1;
5619         else
5620                 rc = atoi(result[1]) + 1;
5621         sqlite3_free_table(result);
5622
5623         memset(sql_query_string, 0, sizeof(char) * QUERY_SIZE);
5624
5625         mailbox_tbl->mailbox_id = rc;
5626
5627         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5628                 "INSERT INTO mail_box_tbl VALUES "
5629                 "( ?"    /* mailbox_id */
5630                 ", ?"    /* account_id */
5631                 ", ?"    /* local_yn */
5632                 ", ?"    /* mailbox_name */
5633                 ", ?"    /* mailbox_type */
5634                 ", ?"    /* alias */
5635                 ", ?"    /* deleted_flag */
5636                 ", ?"    /* modifiable_yn */
5637                 ", ?"    /* total_mail_count_on_server */
5638                 ", ?"    /* has_archived_mails */
5639                 ", ?"    /* mail_slot_size */
5640                 ", ?"    /* no_select */
5641                 ", ? )");/* last_sync_time */
5642
5643
5644         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5645         EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
5646         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5647                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5648
5649         int col_index = 0;
5650
5651         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_id);
5652         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->account_id);
5653         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->local_yn);
5654         _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
5655         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_type);
5656         _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->alias, 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
5657         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->deleted_flag);
5658         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->modifiable_yn);
5659         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->total_mail_count_on_server);
5660         _bind_stmt_field_data_int(hStmt, col_index++, 0);
5661         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mail_slot_size);
5662         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->no_select);
5663         _bind_stmt_field_data_int(hStmt, col_index++, current_time);
5664
5665
5666         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5667         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5668                 ("sqlite3_step fail:%dn", rc));
5669
5670         ret = true;
5671
5672 FINISH_OFF:
5673         if (hStmt != NULL)  {
5674                 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
5675
5676                 rc = sqlite3_finalize(hStmt);
5677                 if (rc != SQLITE_OK)  {
5678                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
5679                         error = EMAIL_ERROR_DB_FAILURE;
5680                 }
5681         }
5682
5683         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5684         _DISCONNECT_DB;
5685
5686         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))
5687                 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
5688
5689         if (err_code != NULL)
5690                 *err_code = error;
5691
5692         EM_DEBUG_FUNC_END("ret [%d]", ret);
5693         return ret;
5694 }
5695
5696 INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(int account_id, int modifiable_yn, int transaction, int *err_code)
5697 {
5698         EM_DEBUG_FUNC_BEGIN("account_id[%d], modifiable_yn[%d], err_code[%p]", account_id, modifiable_yn, err_code);
5699
5700         if (account_id < FIRST_ACCOUNT_ID)  {
5701
5702                 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
5703
5704                 if (err_code != NULL)
5705                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5706                 return false;
5707         }
5708         int rc, ret = false;
5709         int error = EMAIL_ERROR_NONE;
5710         char sql_query_string[QUERY_SIZE] = {0,};
5711
5712         sqlite3 *local_db_handle = emstorage_get_db_connection();
5713         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5714
5715
5716         SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET modifiable_yn = %d WHERE account_id = %d", modifiable_yn, account_id);
5717
5718         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5719         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5720                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5721
5722         rc = sqlite3_changes(local_db_handle);
5723         if (rc == 0)
5724                 EM_DEBUG_EXCEPTION("All mailbox_name modifiable_yn set to 0 already");
5725
5726
5727         ret = true;
5728
5729 FINISH_OFF:
5730         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5731         _DISCONNECT_DB;
5732
5733         if (err_code != NULL)
5734                 *err_code = error;
5735         EM_DEBUG_FUNC_END("ret [%d]", ret);
5736         return ret;
5737
5738
5739 }
5740
5741 INTERNAL_FUNC int emstorage_delete_mailbox(int account_id, int local_yn, int input_mailbox_id, int transaction, int *err_code)
5742 {
5743         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);
5744
5745         if (account_id < FIRST_ACCOUNT_ID)  {
5746                 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
5747
5748                 if (err_code != NULL)
5749                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5750                 return false;
5751         }
5752
5753         int rc, ret = false;
5754         int error = EMAIL_ERROR_NONE;
5755         char sql_query_string[QUERY_SIZE] = {0, };
5756
5757         sqlite3 *local_db_handle = emstorage_get_db_connection();
5758         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5759
5760         if (local_yn == -1)
5761                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_box_tbl WHERE account_id = %d ", account_id);
5762         else
5763                 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);
5764
5765         if (input_mailbox_id > 0)  {            /*  0 means all mailbox_name */
5766                 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);
5767         }
5768
5769         EM_DEBUG_LOG("mailbox sql_query_string [%s]", sql_query_string);
5770         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5771         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5772                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5773
5774         rc = sqlite3_changes(local_db_handle);
5775         if (rc == 0)  {
5776                 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
5777                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5778                 ret = true;
5779         }
5780         ret = true;
5781
5782 FINISH_OFF:
5783
5784         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5785         _DISCONNECT_DB;
5786
5787         if(error == EMAIL_ERROR_NONE) {
5788                 if (!emcore_notify_storage_event(NOTI_MAILBOX_DELETE, account_id, input_mailbox_id, NULL, 0))
5789                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
5790         }
5791
5792         if (err_code != NULL)
5793                 *err_code = error;
5794
5795         EM_DEBUG_FUNC_END("ret [%d]", ret);
5796         return ret;
5797 }
5798
5799 INTERNAL_FUNC int emstorage_free_mailbox(emstorage_mailbox_tbl_t** mailbox_list, int count, int *err_code)
5800 {
5801         EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d], err_code[%p]", mailbox_list, count, err_code);
5802
5803         int ret = false;
5804         int error = EMAIL_ERROR_NONE;
5805
5806         if (count > 0)  {
5807                 if (!mailbox_list || !*mailbox_list)  {
5808                         EM_DEBUG_EXCEPTION(" mailbox_list[%p], count[%d]", mailbox_list, count);
5809
5810                         error = EMAIL_ERROR_INVALID_PARAM;
5811                         goto FINISH_OFF;
5812                 }
5813
5814                 emstorage_mailbox_tbl_t* p = *mailbox_list;
5815                 int i = 0;
5816
5817                 for (; i < count; i++) {
5818                         EM_SAFE_FREE(p[i].mailbox_name);
5819                         EM_SAFE_FREE(p[i].alias);
5820                 }
5821
5822                 EM_SAFE_FREE(p); *mailbox_list = NULL;
5823         }
5824
5825         ret = true;
5826
5827 FINISH_OFF:
5828         if (err_code != NULL)
5829                 *err_code = error;
5830
5831         EM_DEBUG_FUNC_END("ret [%d]", ret);
5832         return ret;
5833 }
5834
5835 INTERNAL_FUNC int emstorage_get_count_read_mail_uid(int account_id, char *mailbox_name, int *count, int transaction, int *err_code)
5836 {
5837         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);
5838
5839         if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !count)  {
5840                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], count[%p], exist[%p]", account_id, mailbox_name, count);
5841
5842                 if (err_code != NULL)
5843                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5844                 return false;
5845         }
5846
5847         int rc = -1, ret = false;
5848         int error = EMAIL_ERROR_NONE;
5849         char sql_query_string[QUERY_SIZE] = {0, };
5850
5851         sqlite3 *local_db_handle = emstorage_get_db_connection();
5852         EMSTORAGE_START_READ_TRANSACTION(transaction);
5853         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, mailbox_name);
5854         EM_DEBUG_LOG(">>> SQL [ %s ] ", sql_query_string);
5855
5856         char **result;
5857
5858         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5859         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
5860                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5861
5862         *count = atoi(result[1]);
5863         sqlite3_free_table(result);
5864
5865         ret = true;
5866
5867 FINISH_OFF:
5868         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5869         _DISCONNECT_DB;
5870
5871         if (err_code != NULL)
5872                 *err_code = error;
5873
5874         EM_DEBUG_FUNC_END("ret [%d]", ret);
5875         return ret;
5876 }
5877
5878
5879
5880 INTERNAL_FUNC int emstorage_check_read_mail_uid(int account_id, char *mailbox_name, char *uid, int *exist, int transaction, int *err_code)
5881 {
5882         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);
5883
5884         if (account_id < FIRST_ACCOUNT_ID || !uid || !exist)  {
5885                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], uid[%p], exist[%p]", account_id, mailbox_name , uid, exist);
5886
5887                 if (err_code != NULL)
5888                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5889                 return false;
5890         }
5891
5892         int rc = -1, ret = false;
5893         int error = EMAIL_ERROR_NONE;
5894         char sql_query_string[QUERY_SIZE] = {0, };
5895         sqlite3 *local_db_handle = emstorage_get_db_connection();
5896         EMSTORAGE_START_READ_TRANSACTION(transaction);
5897
5898         if (mailbox_name)  {
5899                 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, mailbox_name, uid);
5900         }
5901         else  {
5902                 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);
5903         }
5904
5905         char **result;
5906
5907         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5908         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
5909                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5910
5911         *exist = atoi(result[1]);
5912         sqlite3_free_table(result);
5913
5914         if (*exist > 0)
5915                 *exist = 1;
5916         else
5917                 *exist = 0;
5918
5919         ret = true;
5920
5921 FINISH_OFF:
5922         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5923         _DISCONNECT_DB;
5924
5925         if (err_code != NULL)
5926                 *err_code = error;
5927
5928         EM_DEBUG_FUNC_END("ret [%d]", ret);
5929         return ret;
5930 }
5931
5932 INTERNAL_FUNC int emstorage_get_downloaded_mail(int mail_id, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
5933 {
5934         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], err_code[%p]", mail_id, mail, err_code);
5935
5936         if (!mail || mail_id <= 0) {
5937                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
5938                 if (err_code != NULL)
5939                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5940                 return false;
5941         }
5942
5943         int rc, ret = false, temp_rule;
5944         int error = EMAIL_ERROR_NONE;
5945         DB_STMT hStmt = NULL;
5946         char sql_query_string[QUERY_SIZE] = {0, };
5947
5948         sqlite3 *local_db_handle = emstorage_get_db_connection();
5949         EMSTORAGE_START_READ_TRANSACTION(transaction);
5950
5951         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE local_uid = %d", mail_id);
5952
5953         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5954         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
5955
5956         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5957                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5958
5959
5960         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5961         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5962                 ("sqlite3_step fail:%d", rc));
5963
5964         *mail = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
5965         if (*mail == NULL) {
5966                 error = EMAIL_ERROR_OUT_OF_MEMORY;
5967                 EM_DEBUG_EXCEPTION("Memory allocation for mail failed.");
5968                 goto FINISH_OFF;
5969
5970         }
5971         memset(*mail, 0x00, sizeof(emstorage_mail_tbl_t));
5972
5973         _get_stmt_field_data_int(hStmt, &((*mail)->account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5974         _get_stmt_field_data_int(hStmt, &((*mail)->mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5975         _get_stmt_field_data_string(hStmt, &((*mail)->mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5976         _get_stmt_field_data_int(hStmt, &((*mail)->mail_id), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5977         _get_stmt_field_data_string(hStmt, &((*mail)->server_mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5978         _get_stmt_field_data_string(hStmt, &((*mail)->server_mail_id), 0, S_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5979         _get_stmt_field_data_int(hStmt, &((*mail)->mail_size), DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5980         _get_stmt_field_data_int(hStmt, &temp_rule, FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5981
5982         (*mail)->server_mail_status = 1;
5983
5984         ret = true;
5985
5986 FINISH_OFF:
5987         if (hStmt != NULL)  {
5988                 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
5989                 rc = sqlite3_finalize(hStmt);
5990                 if (rc != SQLITE_OK)  {
5991                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5992                         error = EMAIL_ERROR_DB_FAILURE;
5993                 }
5994         }
5995
5996         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5997         _DISCONNECT_DB;
5998
5999         if (err_code != NULL)
6000                 *err_code = error;
6001
6002         EM_DEBUG_FUNC_END("ret [%d]", ret);
6003         return ret;
6004 }
6005
6006 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)
6007 {
6008         EM_PROFILE_BEGIN(emStorageGetDownloadList);
6009         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);
6010         if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid || !count)  {
6011                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_id[%s], read_mail_uid[%p], count[%p]", account_id, mailbox_id, read_mail_uid, count);
6012
6013                 if (err_code != NULL)
6014                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6015                 return false;
6016         }
6017
6018         int rc, ret = false;
6019         int error = EMAIL_ERROR_NONE;
6020
6021         DB_STMT hStmt = NULL;
6022         char sql_query_string[QUERY_SIZE] = {0, };
6023
6024         emstorage_read_mail_uid_tbl_t* p_data_tbl = NULL;
6025         int i = 0;
6026
6027         sqlite3 *local_db_handle = emstorage_get_db_connection();
6028         EMSTORAGE_START_READ_TRANSACTION(transaction);
6029
6030         if (mailbox_id)
6031                 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);
6032         else
6033                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
6034
6035         EM_DEBUG_LOG(" sql_query_string : %s", sql_query_string);
6036
6037
6038
6039         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6040         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6041         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6042                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6043
6044
6045         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6046         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6047                 ("sqlite3_step fail:%d", rc));
6048
6049         char **result;
6050         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL); */
6051         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL), rc);
6052         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6053                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6054
6055         sqlite3_free_table(result);
6056         if (*count == 0)  {
6057                 EM_DEBUG_LOG("No mail found in mail_read_mail_uid_tbl");
6058                 ret = true;
6059                 goto FINISH_OFF;
6060         }
6061
6062
6063         if (!(p_data_tbl = (emstorage_read_mail_uid_tbl_t*)malloc(sizeof(emstorage_read_mail_uid_tbl_t) * *count)))  {
6064                 EM_DEBUG_EXCEPTION(" malloc failed...");
6065                 error = EMAIL_ERROR_OUT_OF_MEMORY;
6066                 goto FINISH_OFF;
6067         }
6068
6069         memset(p_data_tbl, 0x00, sizeof(emstorage_read_mail_uid_tbl_t)* *count);
6070
6071         for (i = 0; i < *count; ++i)  {
6072                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6073                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id),LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6074                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].local_uid), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6075                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6076                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].s_uid), 0, S_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6077                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].data1), DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6078                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].data2), 0, DATA2_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6079                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag), FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6080
6081                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6082                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6083                         ("sqlite3_step fail:%d", rc));
6084         }
6085
6086         ret = true;
6087
6088 FINISH_OFF:
6089         if (ret == true)
6090                 *read_mail_uid = p_data_tbl;
6091         else if (p_data_tbl)
6092                 emstorage_free_read_mail_uid(&p_data_tbl, *count, NULL);
6093
6094         if (hStmt != NULL)  {
6095                 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
6096                 rc = sqlite3_finalize(hStmt);
6097                 if (rc != SQLITE_OK)  {
6098                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6099
6100                         error = EMAIL_ERROR_DB_FAILURE;
6101                 }
6102         }
6103
6104         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6105         _DISCONNECT_DB;
6106
6107         if (err_code != NULL)
6108                 *err_code = error;
6109
6110         EM_PROFILE_END(emStorageGetDownloadList);
6111         EM_DEBUG_FUNC_END("ret [%d]", ret);
6112         return ret;
6113 }
6114
6115 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)
6116 {
6117         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);
6118
6119         if (account_id < FIRST_ACCOUNT_ID || !mail_size)  {
6120                 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);
6121
6122                 if (err_code != NULL)
6123                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6124                 return false;
6125         }
6126
6127         int rc, ret = false;
6128         int error = EMAIL_ERROR_NONE;
6129         DB_STMT hStmt = NULL;
6130         char sql_query_string[QUERY_SIZE] = {0, };
6131
6132
6133         sqlite3 *local_db_handle = emstorage_get_db_connection();
6134         EMSTORAGE_START_READ_TRANSACTION(transaction);
6135
6136         if (mailbox_name) {
6137                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6138                         "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6139                         "WHERE account_id = %d "
6140                         "AND mailbox_id = '%s' "
6141                         "AND local_uid = %d "
6142                         "AND mailbox_name = '%s' "
6143                         "AND s_uid = '%s'",
6144                         account_id, mailbox_id, local_uid, mailbox_name, uid);
6145         }
6146         else {
6147                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6148                         "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6149                         "WHERE account_id = %d "
6150                         "AND mailbox_id = '%s' "
6151                         "AND local_uid = %d "
6152                         "AND s_uid = '%s'",
6153                         account_id, mailbox_id, local_uid, uid);
6154         }
6155
6156
6157         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6158         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6159                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6160
6161
6162         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6163         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6164                 ("sqlite3_step fail:%d", rc));
6165
6166         if (rc == SQLITE_DONE) {
6167                 EM_DEBUG_EXCEPTION(" no matched mail found....");
6168                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
6169                 goto FINISH_OFF;
6170         }
6171
6172         _get_stmt_field_data_int(hStmt, mail_size, 0);
6173
6174         ret = true;
6175
6176 FINISH_OFF:
6177         if (hStmt != NULL)  {
6178                 rc = sqlite3_finalize(hStmt);
6179                 if (rc != SQLITE_OK)  {
6180                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6181
6182                         error = EMAIL_ERROR_DB_FAILURE;
6183                 }
6184         }
6185
6186         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6187         _DISCONNECT_DB;
6188
6189         if (err_code != NULL)
6190                 *err_code = error;
6191
6192         EM_DEBUG_FUNC_END("ret [%d]", ret);
6193         return ret;
6194 }
6195
6196 INTERNAL_FUNC int emstorage_add_downloaded_mail(emstorage_read_mail_uid_tbl_t* read_mail_uid, int transaction, int *err_code)
6197 {
6198         EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], transaction[%d], err_code[%p]", read_mail_uid, transaction, err_code);
6199
6200         if (!read_mail_uid)  {
6201                 EM_DEBUG_EXCEPTION("read_mail_uid[%p]", read_mail_uid);
6202                 if (err_code != NULL)
6203                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6204                 return false;
6205         }
6206
6207         int rc, rc2,  ret = false;
6208         int error = EMAIL_ERROR_NONE;
6209         DB_STMT hStmt = NULL;
6210         char sql_query_string[QUERY_SIZE] = {0, };
6211
6212         sqlite3 *local_db_handle = emstorage_get_db_connection();
6213         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6214
6215         char *sql = "SELECT max(rowid) FROM mail_read_mail_uid_tbl;";
6216         char **result = NULL;
6217
6218
6219         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6220         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6221                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6222
6223         if (NULL==result[1]) rc = 1;
6224         else rc = atoi(result[1])+1;
6225         sqlite3_free_table(result);
6226
6227         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6228                 "INSERT INTO mail_read_mail_uid_tbl VALUES "
6229                 "( ?"
6230                 ", ?"
6231                 ", ?"
6232                 ", ?"
6233                 ", ?"
6234                 ", ?"
6235                 ", ?"
6236                 ", ?"
6237                 ", ? )");
6238
6239
6240         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
6241         if (rc2 != SQLITE_OK)  {
6242                 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6243                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6244
6245                 error = EMAIL_ERROR_DB_FAILURE;
6246                 goto FINISH_OFF;
6247         }
6248
6249         EM_DEBUG_LOG("account_id VALUE [%d] ", read_mail_uid->account_id);
6250         EM_DEBUG_LOG("mailbox_id VALUE [%d] ", read_mail_uid->mailbox_id);
6251         EM_DEBUG_LOG("local_uid VALUE [%d] ", read_mail_uid->local_uid);
6252         EM_DEBUG_LOG("mailbox_name VALUE [%s] ", read_mail_uid->mailbox_name);
6253         EM_DEBUG_LOG("s_uid VALUE [%s] ", read_mail_uid->s_uid);
6254         EM_DEBUG_LOG("data1 VALUE [%d] ", read_mail_uid->data1);
6255         EM_DEBUG_LOG("data2 VALUE [%s] ", read_mail_uid->data2);
6256         EM_DEBUG_LOG("flag VALUE [%d] ", read_mail_uid->flag);
6257         EM_DEBUG_LOG("rc VALUE [%d] ", rc);
6258
6259         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->account_id);
6260         _bind_stmt_field_data_int(hStmt, LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->mailbox_id);
6261         _bind_stmt_field_data_int(hStmt, LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->local_uid);
6262         _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);
6263         _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);
6264         _bind_stmt_field_data_int(hStmt, DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->data1);
6265         _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);
6266         _bind_stmt_field_data_int(hStmt, FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flag);
6267         _bind_stmt_field_data_int(hStmt, IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, rc);
6268
6269
6270         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6271         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
6272                 ("sqlite3_step fail:%d", rc));
6273         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6274                 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
6275
6276
6277         ret = true;
6278
6279 FINISH_OFF:
6280         if (hStmt != NULL)  {
6281                 EM_DEBUG_LOG(" sqlite3_finalize hStmt = %p", hStmt);
6282
6283                 rc = sqlite3_finalize(hStmt);
6284                 if (rc != SQLITE_OK)  {
6285                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6286                         error = EMAIL_ERROR_DB_FAILURE;
6287                 }
6288         }
6289
6290         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6291         _DISCONNECT_DB;
6292
6293         if (err_code != NULL)
6294                 *err_code = error;
6295
6296         EM_DEBUG_FUNC_END("ret [%d]", ret);
6297         return ret;
6298 }
6299
6300 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)
6301 {
6302         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);
6303
6304         if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid)  {
6305                 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);
6306
6307                 if (err_code != NULL)
6308                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6309                 return false;
6310         }
6311
6312         int rc, ret = false;
6313         int error = EMAIL_ERROR_NONE;
6314         DB_STMT hStmt = NULL;
6315         char sql_query_string[QUERY_SIZE] = {0, };
6316
6317         sqlite3 *local_db_handle = emstorage_get_db_connection();
6318
6319         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6320
6321         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6322                 "UPDATE mail_read_mail_uid_tbl SET"
6323                 "  account_id = ?"
6324                 ", mailbox_id = ?"
6325                 ", local_uid  = ?"
6326                 ", mailbox_name = ?"
6327                 ", s_uid = ?"
6328                 ", data1 = ?"
6329                 ", data2 = ?"
6330                 ", flag  = ?"
6331                 " WHERE account_id = ?"
6332                 " AND mailbox_id  = ?"
6333                 " AND local_uid   = ?"
6334                 " AND mailbox_name= ?"
6335                 " AND s_uid = ?");
6336
6337
6338         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6339         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6340         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6341                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6342
6343
6344         int i = 0;
6345
6346         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->account_id);
6347         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->mailbox_id);
6348         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->local_uid);
6349         _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6350         _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->s_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6351         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->data1);
6352         _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->data2, 0, DATA2_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6353         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flag);
6354         _bind_stmt_field_data_int(hStmt, i++, account_id);
6355         _bind_stmt_field_data_int(hStmt, i++, mailbox_id);
6356         _bind_stmt_field_data_int(hStmt, i++, local_uid);
6357         _bind_stmt_field_data_string(hStmt, i++, (char*)mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6358         _bind_stmt_field_data_string(hStmt, i++, (char*)uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6359
6360
6361         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6362         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6363                 ("sqlite3_step fail:%d", rc));
6364
6365         ret = true;
6366
6367 FINISH_OFF:
6368         if (hStmt != NULL)  {
6369                 EM_DEBUG_LOG("sqlite3_finalize hStmt = %p", hStmt);
6370
6371                 rc = sqlite3_finalize(hStmt);
6372                 if (rc != SQLITE_OK)  {
6373                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6374
6375                         error = EMAIL_ERROR_DB_FAILURE;
6376                 }
6377         }
6378
6379         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6380         _DISCONNECT_DB;
6381
6382         if (err_code != NULL)
6383                 *err_code = error;
6384
6385         EM_DEBUG_FUNC_END("ret [%d]", ret);
6386         return ret;
6387 }
6388
6389 INTERNAL_FUNC int emstorage_remove_downloaded_mail(int account_id, char *mailbox_name, char *uid, int transaction, int *err_code)
6390 {
6391         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);
6392
6393         if (account_id < FIRST_ACCOUNT_ID)  {
6394                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%s], uid[%s]", account_id, mailbox_name, uid);
6395
6396                 if (err_code != NULL)
6397                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6398                 return false;
6399         }
6400
6401         int rc, ret = false;
6402         int error = EMAIL_ERROR_NONE;
6403         char sql_query_string[QUERY_SIZE] = {0, };
6404
6405         sqlite3 *local_db_handle = emstorage_get_db_connection();
6406
6407         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6408
6409         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d ", account_id);
6410
6411         if (mailbox_name) {             /*  NULL means all mailbox_name */
6412                 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' ", mailbox_name);
6413         }
6414
6415         if (uid) {              /*  NULL means all mail */
6416                 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);
6417         }
6418
6419         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
6420         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6421                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6422
6423
6424         ret = true;
6425
6426 FINISH_OFF:
6427
6428         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6429         _DISCONNECT_DB;
6430
6431         if (err_code != NULL)
6432                 *err_code = error;
6433
6434         EM_DEBUG_FUNC_END("ret [%d]", ret);
6435         return ret;
6436 }
6437
6438 INTERNAL_FUNC int emstorage_free_read_mail_uid(emstorage_read_mail_uid_tbl_t** read_mail_uid, int count, int *err_code)
6439 {
6440         EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], count[%d], err_code[%p]", read_mail_uid, count, err_code);
6441
6442         int ret = false;
6443         int error = EMAIL_ERROR_NONE;
6444
6445         if (count > 0)  {
6446                 if (!read_mail_uid || !*read_mail_uid)  {
6447                         EM_DEBUG_EXCEPTION(" read_mail_uid[%p], count[%d]", read_mail_uid, count);
6448
6449                         error = EMAIL_ERROR_INVALID_PARAM;
6450                         goto FINISH_OFF;
6451                 }
6452
6453                 emstorage_read_mail_uid_tbl_t* p = *read_mail_uid;
6454                 int i;
6455
6456                 for (i = 0; i < count; i++)  {
6457                         EM_SAFE_FREE(p[i].mailbox_name);
6458                         EM_SAFE_FREE(p[i].s_uid);
6459                         EM_SAFE_FREE(p[i].data2);
6460                 }
6461
6462                 EM_SAFE_FREE(p); *read_mail_uid = NULL;
6463         }
6464
6465         ret = true;
6466
6467 FINISH_OFF:
6468         if (err_code != NULL)
6469                 *err_code = error;
6470
6471         EM_DEBUG_FUNC_END("ret [%d]", ret);
6472         return ret;
6473 }
6474
6475 INTERNAL_FUNC int emstorage_get_rule_count(int account_id, int *count, int transaction, int *err_code)
6476 {
6477         EM_DEBUG_FUNC_BEGIN("account_id[%d], count[%p], transaction[%d], err_code[%p]", account_id, count, transaction, err_code);
6478
6479         if (account_id != ALL_ACCOUNT || !count) {              /*  only global rule supported. */
6480                 EM_DEBUG_EXCEPTION(" account_id[%d], count[%p]", account_id, count);
6481
6482                 if (err_code != NULL)
6483                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6484                 return false;
6485         }
6486
6487         int rc = -1, ret = false;
6488         int error =  EMAIL_ERROR_NONE;
6489         char sql_query_string[QUERY_SIZE] = {0, };
6490
6491         sqlite3 *local_db_handle = emstorage_get_db_connection();
6492         EMSTORAGE_START_READ_TRANSACTION(transaction);
6493
6494         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl WHERE account_id = %d", account_id);
6495
6496         char **result;
6497
6498         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6499         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6500                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6501
6502         *count = atoi(result[1]);
6503         sqlite3_free_table(result);
6504
6505         ret = true;
6506
6507 FINISH_OFF:
6508         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6509         _DISCONNECT_DB;
6510
6511         if (err_code != NULL)
6512                 *err_code = error;
6513
6514         EM_DEBUG_FUNC_END("ret [%d]", ret);
6515         return ret;
6516 }
6517
6518 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)
6519 {
6520         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);
6521
6522         if (account_id != ALL_ACCOUNT || !select_num || !is_completed || !rule_list) {          /*  only global rule supported. */
6523                 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);
6524
6525                 if (err_code != NULL)
6526                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6527                 return false;
6528         }
6529
6530         int ret = false;
6531         int error = EMAIL_ERROR_NONE;
6532
6533         emstorage_rule_tbl_t* p_data_tbl = NULL;
6534         int i = 0, count = 0;
6535         DB_STMT hStmt = NULL;
6536         char sql_query_string[QUERY_SIZE] = {0, };
6537
6538         int rc;
6539
6540         sqlite3 *local_db_handle = emstorage_get_db_connection();
6541         EMSTORAGE_START_READ_TRANSACTION(transaction);
6542
6543         if (type)
6544                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d AND type = %d", account_id, type);
6545         else
6546                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d ORDER BY type", account_id);
6547
6548
6549         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6550         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6551         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6552                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6553
6554
6555         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6556         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6557                 ("sqlite3_step fail:%d", rc));
6558
6559         char **result;
6560         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL); */
6561         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
6562         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6563                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6564
6565         sqlite3_free_table(result);
6566
6567         if (count == 0)  {
6568                 EM_DEBUG_LOG(" no matched rule found...");
6569                 ret = true;
6570                 goto FINISH_OFF;
6571         }
6572
6573
6574         if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t) * count))) {
6575                 EM_DEBUG_EXCEPTION(" malloc failed...");
6576
6577                 error = EMAIL_ERROR_OUT_OF_MEMORY;
6578                 goto FINISH_OFF;
6579         }
6580
6581         memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t) * count);
6582
6583         for (i = 0; i < count; i++)  {
6584                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
6585                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
6586                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].type), TYPE_IDX_IN_MAIL_RULE_TBL);
6587                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
6588                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
6589                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
6590                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
6591                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
6592
6593                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6594                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6595                         ("sqlite3_step fail:%d", rc));
6596         }
6597
6598         ret = true;
6599
6600 FINISH_OFF:
6601
6602         EM_DEBUG_LOG("[%d] rules found.", count);
6603
6604         if (ret == true)  {
6605                 *rule_list = p_data_tbl;
6606                 *select_num = count;
6607         }
6608         else if (p_data_tbl != NULL)
6609                 emstorage_free_rule(&p_data_tbl, count, NULL); /* CID FIX */
6610
6611         if (hStmt != NULL)  {
6612                 EM_DEBUG_LOG("  sqlite3_finalize hStmt = %p", hStmt);
6613
6614                 rc = sqlite3_finalize(hStmt);
6615                 if (rc != SQLITE_OK)  {
6616                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6617                         error = EMAIL_ERROR_DB_FAILURE;
6618                 }
6619         }
6620
6621
6622         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6623         _DISCONNECT_DB;
6624
6625         if (err_code != NULL)
6626                 *err_code = error;
6627
6628         EM_DEBUG_FUNC_END("ret [%d]", ret);
6629         return ret;
6630 }
6631
6632 INTERNAL_FUNC int emstorage_get_rule_by_id(int account_id, int rule_id, emstorage_rule_tbl_t** rule, int transaction, int *err_code)
6633 {
6634         EM_DEBUG_FUNC_BEGIN("account_id[%d], rule_id[%d], rule[%p], transaction[%d], err_code[%p]", account_id, rule_id, rule, transaction, err_code);
6635
6636         if (account_id != ALL_ACCOUNT || !rule)  {
6637                 EM_DEBUG_EXCEPTION(" account_id[%d], rule_id[%d], rule[%p]", account_id, rule_id, rule);
6638
6639                 if (err_code != NULL)
6640                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6641                 return false;
6642         }
6643
6644         emstorage_rule_tbl_t* p_data_tbl = NULL;
6645         int rc, ret = false;
6646         int error = EMAIL_ERROR_NONE;
6647
6648         DB_STMT hStmt = NULL;
6649         char sql_query_string[QUERY_SIZE] = {0, };
6650         sqlite3 *local_db_handle = emstorage_get_db_connection();
6651         EMSTORAGE_START_READ_TRANSACTION(transaction);
6652
6653         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d AND rule_id = %d", account_id, rule_id);
6654
6655
6656         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6657         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6658                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6659
6660
6661         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6662         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6663                 ("sqlite3_step fail:%d", rc));
6664
6665         if (rc == SQLITE_DONE)  {
6666                 EM_DEBUG_EXCEPTION(" no matched rule found...");
6667                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
6668                 goto FINISH_OFF;
6669         }
6670
6671         if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t))))  {
6672                 EM_DEBUG_EXCEPTION(" malloc failed...");
6673                 error = EMAIL_ERROR_OUT_OF_MEMORY;
6674                 goto FINISH_OFF;
6675         }
6676
6677         memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t));
6678         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
6679         _get_stmt_field_data_int(hStmt, &(p_data_tbl->rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
6680         _get_stmt_field_data_int(hStmt, &(p_data_tbl->type), TYPE_IDX_IN_MAIL_RULE_TBL);
6681         _get_stmt_field_data_string(hStmt, &(p_data_tbl->value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
6682         _get_stmt_field_data_int(hStmt, &(p_data_tbl->action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
6683         _get_stmt_field_data_int(hStmt, &(p_data_tbl->target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
6684         _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
6685         _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
6686
6687         ret = true;
6688
6689 FINISH_OFF:
6690         if (ret == true)
6691                 *rule = p_data_tbl;
6692
6693         if (hStmt != NULL)  {
6694                 EM_DEBUG_LOG(" before sqlite3_finalize hStmt = %p", hStmt);
6695
6696                 rc = sqlite3_finalize(hStmt);
6697                 if (rc != SQLITE_OK)  {
6698                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6699
6700                         error = EMAIL_ERROR_DB_FAILURE;
6701                 }
6702         }
6703
6704
6705
6706         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6707         _DISCONNECT_DB;
6708
6709         if (err_code != NULL)
6710                 *err_code = error;
6711
6712         EM_DEBUG_FUNC_END("ret [%d]", ret);
6713         return ret;
6714 }
6715
6716 INTERNAL_FUNC int emstorage_change_rule(int account_id, int rule_id, emstorage_rule_tbl_t* new_rule, int transaction, int *err_code)
6717 {
6718         EM_DEBUG_FUNC_BEGIN("account_id[%d], rule_id[%d], new_rule[%p], transaction[%d], err_code[%p]", account_id, rule_id, new_rule, transaction, err_code);
6719
6720         if (account_id != ALL_ACCOUNT || !new_rule) {           /*  only global rule supported. */
6721                 EM_DEBUG_EXCEPTION(" account_id[%d], rule_id[%d], new_rule[%p]", account_id, rule_id, new_rule);
6722
6723                 if (err_code != NULL)
6724                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6725                 return false;
6726         }
6727
6728         int rc, ret = false;
6729         int error = EMAIL_ERROR_NONE;
6730
6731         DB_STMT hStmt = NULL;
6732         char sql_query_string[QUERY_SIZE] = {0, };
6733         sqlite3 *local_db_handle = emstorage_get_db_connection();
6734         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6735
6736         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6737                 "UPDATE mail_rule_tbl SET"
6738                 "  type = ?"
6739                 ", value = ?"
6740                 ", action_type = ?"
6741                 ", target_mailbox_id = ?"
6742                 ", flag1 = ?"
6743                 ", flag2 = ?"
6744                 " WHERE account_id = ?"
6745                 " AND rule_id = ?");
6746
6747
6748         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6749         EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
6750         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6751                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6752
6753         int i = 0;
6754
6755         _bind_stmt_field_data_int(hStmt, i++, new_rule->type);
6756         _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
6757         _bind_stmt_field_data_int(hStmt, i++, new_rule->action_type);
6758         _bind_stmt_field_data_int(hStmt, i++, new_rule->target_mailbox_id);
6759         _bind_stmt_field_data_int(hStmt, i++, new_rule->flag1);
6760         _bind_stmt_field_data_int(hStmt, i++, new_rule->flag2);
6761         _bind_stmt_field_data_int(hStmt, i++, account_id);
6762         _bind_stmt_field_data_int(hStmt, i++, rule_id);
6763
6764
6765         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6766         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6767                 ("sqlite3_step fail:%d", rc));
6768
6769         ret = true;
6770
6771 FINISH_OFF:
6772         if (hStmt != NULL)  {
6773                 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
6774
6775                 rc = sqlite3_finalize(hStmt);
6776                 if (rc != SQLITE_OK)  {
6777                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6778
6779                         error = EMAIL_ERROR_DB_FAILURE;
6780                 }
6781         }
6782
6783         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6784         _DISCONNECT_DB;
6785
6786         if (err_code != NULL)
6787                 *err_code = error;
6788
6789         EM_DEBUG_FUNC_END("ret [%d]", ret);
6790         return ret;
6791 }
6792
6793 INTERNAL_FUNC int emstorage_find_rule(emstorage_rule_tbl_t* rule, int transaction, int *err_code)
6794 {
6795         EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
6796
6797         if (!rule || rule->account_id != ALL_ACCOUNT) {         /*  only global rule supported. */
6798                 if (rule != NULL)
6799                         EM_DEBUG_EXCEPTION(" rule->account_id[%d]", rule->account_id);
6800
6801                 if (err_code != NULL)
6802                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6803                 return false;
6804         }
6805
6806         DB_STMT hStmt = NULL;
6807         char sql_query_string[QUERY_SIZE] = {0, };
6808
6809         int error = EMAIL_ERROR_NONE;
6810         int rc, ret = false;
6811
6812         sqlite3 *local_db_handle = emstorage_get_db_connection();
6813         EMSTORAGE_START_READ_TRANSACTION(transaction);
6814
6815         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6816                 "SELECT rule_id FROM mail_rule_tbl WHERE type = %d AND UPPER(value) = UPPER('%s')",
6817                 rule->type, rule->value);
6818
6819
6820         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6821         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6822                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6823
6824
6825         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6826         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6827                 ("sqlite3_step fail:%d", rc));
6828
6829         if (rc == SQLITE_DONE)  {
6830                 EM_DEBUG_EXCEPTION(" no matched rule found...");
6831                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
6832                 goto FINISH_OFF;
6833         }
6834
6835
6836         ret = true;
6837
6838 FINISH_OFF:
6839         if (hStmt != NULL)  {
6840                 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
6841
6842                 rc = sqlite3_finalize(hStmt);
6843                 if (rc != SQLITE_OK)  {
6844                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
6845                         error = EMAIL_ERROR_DB_FAILURE;
6846                 }
6847         }
6848
6849
6850         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6851         _DISCONNECT_DB;
6852
6853         if (err_code)
6854                 *err_code = error;
6855
6856         EM_DEBUG_FUNC_END("ret [%d]", ret);
6857         return ret;
6858 }
6859
6860 INTERNAL_FUNC int emstorage_add_rule(emstorage_rule_tbl_t* rule, int transaction, int *err_code)
6861 {
6862         EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
6863
6864         if (!rule || rule->account_id != ALL_ACCOUNT)  {        /*  only global rule supported. */
6865                 if (rule != NULL)
6866                         EM_DEBUG_EXCEPTION(" rule->account_id[%d]", rule->account_id);
6867
6868                 if (err_code != NULL)
6869                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6870                 return false;
6871         }
6872
6873         int rc, rc_2, ret = false;
6874         int error = EMAIL_ERROR_NONE;
6875         DB_STMT hStmt = NULL;
6876         char sql_query_string[QUERY_SIZE] = {0, };
6877
6878
6879         sqlite3 *local_db_handle = emstorage_get_db_connection();
6880
6881         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6882
6883         char *sql;
6884         char **result;
6885         sql = "SELECT max(rowid) FROM mail_rule_tbl;";
6886
6887         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6888         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6889                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6890
6891         if (NULL==result[1])
6892                 rc = 1;
6893         else
6894                 rc = atoi(result[1])+1;
6895         sqlite3_free_table(result);
6896
6897         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6898                 "INSERT INTO mail_rule_tbl VALUES "
6899                 "( ?"           /*  account id */
6900                 ", ?"           /*  rule_id */
6901                 ", ?"           /*  type */
6902                 ", ?"           /*  value */
6903                 ", ?"           /*  action_type */
6904                 ", ?"           /*  target_mailbox_id */
6905                 ", ?"           /*  flag1 */
6906                 ", ?)");        /*  flag2 */
6907
6908         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc_2);
6909         if (rc_2 != SQLITE_OK)  {
6910                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc_2, sqlite3_errmsg(local_db_handle));
6911                 error = EMAIL_ERROR_DB_FAILURE;
6912                 goto FINISH_OFF;
6913         }
6914
6915         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL, rule->account_id);
6916         _bind_stmt_field_data_int(hStmt, RULE_ID_IDX_IN_MAIL_RULE_TBL, rc);
6917         _bind_stmt_field_data_int(hStmt, TYPE_IDX_IN_MAIL_RULE_TBL, rule->type);
6918         _bind_stmt_field_data_string(hStmt, VALUE_IDX_IN_MAIL_RULE_TBL, (char*)rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
6919         _bind_stmt_field_data_int(hStmt, ACTION_TYPE_IDX_IN_MAIL_RULE_TBL, rule->action_type);
6920         _bind_stmt_field_data_int(hStmt, TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL, rule->target_mailbox_id);
6921         _bind_stmt_field_data_int(hStmt, FLAG1_IDX_IN_MAIL_RULE_TBL, rule->flag1);
6922         _bind_stmt_field_data_int(hStmt, FLAG2_IDX_IN_MAIL_RULE_TBL, rule->flag2);
6923
6924         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6925         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6926                 ("sqlite3_step fail:%d", rc));
6927
6928         ret = true;
6929
6930 FINISH_OFF:
6931         if (hStmt != NULL)  {
6932                 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
6933
6934                 rc = sqlite3_finalize(hStmt);
6935                 if (rc != SQLITE_OK) {
6936                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6937
6938                         error = EMAIL_ERROR_DB_FAILURE;
6939                 }
6940         }
6941
6942         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6943         _DISCONNECT_DB;
6944
6945         if (err_code != NULL)
6946                 *err_code = error;
6947
6948         EM_DEBUG_FUNC_END("ret [%d]", ret);
6949         return ret;
6950 }
6951
6952 INTERNAL_FUNC int emstorage_delete_rule(int account_id, int rule_id, int transaction, int *err_code)
6953 {
6954         EM_DEBUG_FUNC_BEGIN("account_id[%d], rule_id[%d], transaction[%d], err_code[%p]", account_id, rule_id, transaction, err_code);
6955
6956         if (account_id != ALL_ACCOUNT) {                /*  only global rule supported. */
6957                 EM_DEBUG_EXCEPTION(" account_id[%d], rule_id[%d]", account_id, rule_id);
6958
6959                 if (err_code != NULL)
6960                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6961                 return false;
6962         }
6963
6964         int rc, ret = false;
6965         int error = EMAIL_ERROR_NONE;
6966         char sql_query_string[QUERY_SIZE] = {0, };
6967
6968         sqlite3 *local_db_handle = emstorage_get_db_connection();
6969         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6970
6971         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_rule_tbl WHERE account_id = %d AND rule_id = %d", account_id, rule_id);
6972         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
6973         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6974                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6975         rc = sqlite3_changes(local_db_handle);
6976         if (rc == 0) {
6977                 EM_DEBUG_EXCEPTION(" no matched rule found...");
6978
6979                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
6980                 goto FINISH_OFF;
6981         }
6982
6983         ret = true;
6984
6985 FINISH_OFF:
6986         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6987         _DISCONNECT_DB;
6988
6989         if (err_code != NULL)
6990                 *err_code = error;
6991
6992         EM_DEBUG_FUNC_END("ret [%d]", ret);
6993         return ret;
6994 }
6995
6996 INTERNAL_FUNC int emstorage_free_rule(emstorage_rule_tbl_t** rule_list, int count, int *err_code)
6997 {
6998         EM_DEBUG_FUNC_BEGIN("rule_list[%p], conut[%d], err_code[%p]", rule_list, count, err_code);
6999
7000         int ret = false;
7001         int error = EMAIL_ERROR_NONE;
7002
7003         if (count > 0) {
7004                 if (!rule_list || !*rule_list) {
7005                         EM_DEBUG_EXCEPTION(" rule_list[%p], conut[%d]", rule_list, count);
7006
7007                         error = EMAIL_ERROR_INVALID_PARAM;
7008                         goto FINISH_OFF;
7009                 }
7010
7011                 emstorage_rule_tbl_t* p = *rule_list;
7012                 int i = 0;
7013
7014                 for (; i < count; i++) {
7015                         EM_SAFE_FREE(p[i].value);
7016                 }
7017
7018                 EM_SAFE_FREE(p); *rule_list = NULL;
7019         }
7020
7021 FINISH_OFF:
7022         if (err_code != NULL)
7023                 *err_code = error;
7024
7025         EM_DEBUG_FUNC_END("ret [%d]", ret);
7026         return ret;
7027 }
7028
7029 INTERNAL_FUNC int emstorage_get_mail_count(int account_id, const char *mailbox_name, int *total, int *unseen, int transaction, int *err_code)
7030 {
7031         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_name[%s], total[%p], unseen[%p], transaction[%d], err_code[%p]", account_id, mailbox_name, total, unseen, transaction, err_code);
7032
7033         if (!total && !unseen) {
7034                 EM_DEBUG_EXCEPTION(" accoun_id[%d], mailbox_name[%s], total[%p], unseen[%p]", account_id, mailbox_name, total, unseen);
7035                 if (err_code != NULL)
7036                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7037                 return false;
7038         }
7039
7040         int rc = -1, ret = false;
7041         int error = EMAIL_ERROR_NONE;
7042         DB_STMT hStmt = NULL;
7043         char sql_query_string[QUERY_SIZE] = {0, };
7044         char err_msg[1024];
7045
7046
7047         memset(&sql_query_string, 0x00, sizeof(sql_query_string));
7048         sqlite3 *local_db_handle = emstorage_get_db_connection();
7049         EMSTORAGE_START_READ_TRANSACTION(transaction);
7050
7051         if (total)  {
7052                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl");
7053
7054                 if (account_id != ALL_ACCOUNT)  {
7055                         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);
7056                         if (mailbox_name)
7057                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND UPPER(mailbox_name) =  UPPER('%s')", mailbox_name);
7058                 }
7059                 else if (mailbox_name)
7060                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE UPPER(mailbox_name) =  UPPER('%s')", mailbox_name);
7061
7062 #ifdef USE_GET_RECORD_COUNT_API
7063                 char **result;
7064
7065                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7066                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF2; },
7067                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7068
7069                 *total = atoi(result[1]);
7070                 sqlite3_free_table(result);
7071 #else
7072
7073                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7074                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF2; },
7075                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7076
7077                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7078                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF2; },
7079                         ("sqlite3_step fail:%d", rc));
7080                 _get_stmt_field_data_int(hStmt, total, 0);
7081 #endif          /*  USE_GET_RECORD_COUNT_API */
7082         }
7083
7084         if (unseen)  {
7085                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl WHERE flags_seen_field = 0");               /*  fSEEN = 0x01 */
7086
7087                 if (account_id != ALL_ACCOUNT)
7088                         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);
7089
7090                 if (mailbox_name)
7091                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_name = '%s'", mailbox_name);
7092                 else
7093                         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)");
7094
7095                 char **result;
7096                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7097                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
7098                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7099
7100                 *unseen = atoi(result[1]);
7101                 sqlite3_free_table(result);
7102
7103         }
7104 FINISH_OFF:
7105         ret = true;
7106
7107 FINISH_OFF2:
7108
7109 #ifndef USE_PREPARED_QUERY_
7110         if (hStmt != NULL)  {
7111                 rc = sqlite3_finalize(hStmt);
7112                 if (rc != SQLITE_OK)  {
7113                         EM_DEBUG_LOG(" EDBStmtClearRow failed - %d: %s", rc, err_msg);
7114                         error = EMAIL_ERROR_DB_FAILURE;
7115                 }
7116         }
7117 #endif
7118
7119         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7120         _DISCONNECT_DB;
7121
7122         if (err_code != NULL)
7123                 *err_code = error;
7124
7125         EM_DEBUG_FUNC_END("ret [%d]", ret);
7126         return ret;
7127 }
7128
7129 INTERNAL_FUNC int emstorage_get_mail_field_by_id(int mail_id, int type, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
7130 {
7131         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
7132
7133         if (mail_id <= 0 || !mail)  {
7134                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
7135                 if (err_code != NULL)
7136                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7137                 return false;
7138         }
7139
7140         emstorage_mail_tbl_t* p_data_tbl = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
7141
7142         if (p_data_tbl == NULL)  {
7143                 EM_DEBUG_EXCEPTION("malloc failed...");
7144                 if (err_code != NULL)
7145                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
7146                 return false;
7147         }
7148
7149         memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
7150         DB_STMT hStmt = NULL;
7151         char sql_query_string[QUERY_SIZE] = {0, };
7152
7153         int rc, ret = false;
7154         int error = EMAIL_ERROR_NONE;
7155
7156         sqlite3 *local_db_handle = emstorage_get_db_connection();
7157         EMSTORAGE_START_READ_TRANSACTION(transaction);
7158
7159         switch (type)  {
7160                 case RETRIEVE_SUMMARY:
7161                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7162                                 "SELECT account_id, mail_id, mailbox_id, mailbox_name, 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);
7163                         break;
7164
7165                 case RETRIEVE_FIELDS_FOR_DELETE:
7166                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7167                                 "SELECT account_id, mail_id, server_mail_status, server_mailbox_name, server_mail_id FROM mail_tbl WHERE mail_id = %d", mail_id);
7168                         break;
7169
7170                 case RETRIEVE_ACCOUNT:
7171                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7172                                 "SELECT account_id FROM mail_tbl WHERE mail_id = %d", mail_id);
7173                         break;
7174
7175                 case RETRIEVE_FLAG:
7176                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7177                                 "SELECT account_id, mailbox_name, flags_seen_field, thread_id, mailbox_id FROM mail_tbl WHERE mail_id = %d", mail_id);
7178                         break;
7179
7180                 default :
7181                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
7182                         error = EMAIL_ERROR_INVALID_PARAM;
7183                         goto FINISH_OFF;
7184         }
7185
7186         EM_DEBUG_LOG("Query [%s]", sql_query_string);
7187
7188         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7189         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7190                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7191
7192
7193         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7194         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7195                 ("sqlite3_step fail:%d", rc));
7196
7197         if (rc == SQLITE_DONE)  {
7198                 EM_DEBUG_EXCEPTION("no matched mail found...");
7199                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7200                 goto FINISH_OFF;
7201         }
7202         switch (type)  {
7203                 case RETRIEVE_SUMMARY:
7204                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), 0);
7205                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), 1);
7206                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), 2);
7207                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->mailbox_name), 0, 3);
7208                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), 4);
7209                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, 5);
7210                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, 6);
7211                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, 7);
7212                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, 8);
7213                         _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), 9);
7214                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), 10);
7215                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), 11);
7216                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), 12);
7217                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_item_count), 13);
7218                         break;
7219
7220                 case RETRIEVE_FIELDS_FOR_DELETE:
7221                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), 0);
7222                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), 1);
7223                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), 2);
7224                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, 3);
7225                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, 4);
7226                         break;
7227
7228                 case RETRIEVE_ACCOUNT:
7229                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), 0);
7230                         break;
7231
7232                 case RETRIEVE_FLAG:
7233                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), 0);
7234                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->mailbox_name), 0, 1);
7235                         _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), 2);
7236                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), 3);
7237                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), 4);
7238                         break;
7239         }
7240
7241         ret = true;
7242
7243 FINISH_OFF:
7244         if (ret == true)
7245                 *mail = p_data_tbl;
7246         else if (p_data_tbl != NULL)
7247                 emstorage_free_mail(&p_data_tbl,  1, NULL);
7248
7249         if (hStmt != NULL)  {
7250                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
7251
7252                 rc = sqlite3_finalize(hStmt);
7253                 if (rc != SQLITE_OK)  {
7254                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
7255                         error = EMAIL_ERROR_DB_FAILURE;
7256                 }
7257         }
7258
7259
7260         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7261         _DISCONNECT_DB;
7262
7263         if (err_code != NULL)
7264                 *err_code = error;
7265
7266         EM_DEBUG_FUNC_END("ret [%d]", ret);
7267         return ret;
7268 }
7269
7270 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)
7271 {
7272         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);
7273
7274         int ret = false;
7275         int error = EMAIL_ERROR_NONE;
7276         int query_string_length = 0;
7277         int i = 0, item_count = 0, rc = -1, field_count, col_index, cur_sql_query_string = 0;
7278         char **result = NULL;
7279         char *sql_query_string = NULL;
7280         emstorage_mail_tbl_t* p_data_tbl = NULL;
7281         sqlite3 *local_db_handle = NULL;
7282
7283         if (number_of_mails <= 0 || !mail_ids)  {
7284                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
7285                 if (err_code != NULL)
7286                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7287                 return false;
7288         }
7289
7290         p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * number_of_mails);
7291
7292         query_string_length = (sizeof(char) * 8 * number_of_mails) + 512;
7293         sql_query_string = (char*)em_malloc(query_string_length);
7294
7295         if (p_data_tbl == NULL || sql_query_string == NULL)  {
7296                 EM_DEBUG_EXCEPTION("malloc failed...");
7297
7298                 EM_SAFE_FREE(p_data_tbl);
7299                 EM_SAFE_FREE(sql_query_string);
7300
7301                 if (err_code != NULL)
7302                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
7303                 return false;
7304         }
7305
7306         local_db_handle = emstorage_get_db_connection();
7307
7308         EMSTORAGE_START_READ_TRANSACTION(transaction);
7309
7310         switch (type) {
7311                 case RETRIEVE_SUMMARY:
7312                         cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
7313                                 "SELECT account_id, mail_id, mailbox_name, 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 (");
7314                         field_count = 13;
7315                         break;
7316
7317                 case RETRIEVE_FIELDS_FOR_DELETE:
7318                         cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
7319                                 "SELECT account_id, mail_id, server_mail_status, server_mailbox_name, server_mail_id FROM mail_tbl WHERE mail_id in (");
7320                         field_count = 5;
7321                         break;
7322
7323                 case RETRIEVE_ACCOUNT:
7324                         cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
7325                                 "SELECT account_id FROM mail_tbl WHERE mail_id in (");
7326                         field_count = 1;
7327                         break;
7328
7329                 case RETRIEVE_FLAG:
7330                         cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
7331                                 "SELECT account_id, mailbox_id, flags_seen_field, thread_id FROM mail_tbl WHERE mail_id in (");
7332                         field_count = 4;
7333                         break;
7334
7335                 default :
7336                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
7337                         error = EMAIL_ERROR_INVALID_PARAM;
7338                         goto FINISH_OFF;
7339         }
7340
7341         for(i = 0; i < number_of_mails; i++)
7342                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, QUERY_SIZE, "%d,", mail_ids[i]);
7343         sql_query_string[EM_SAFE_STRLEN(sql_query_string) - 1] = ')';
7344
7345         EM_DEBUG_LOG("Query [%s], Length [%d]", sql_query_string, EM_SAFE_STRLEN(sql_query_string));
7346
7347         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &item_count, 0, NULL), rc);
7348         if (SQLITE_OK != rc && -1 != rc) {
7349                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
7350                 error = EMAIL_ERROR_DB_FAILURE;
7351                 goto FINISH_OFF;
7352         }
7353
7354         if (rc == SQLITE_DONE)  {
7355                 EM_DEBUG_EXCEPTION("no matched mail found...");
7356                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7357                 goto FINISH_OFF;
7358         }
7359
7360         EM_DEBUG_LOG("item_count [%d]", item_count);
7361
7362         if(number_of_mails != item_count) {
7363                 EM_DEBUG_EXCEPTION("Can't find all emails");
7364                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7365                 goto FINISH_OFF;
7366         }
7367
7368         col_index = field_count;
7369
7370         for(i = 0; i < item_count; i++) {
7371                 switch (type) {
7372                         case RETRIEVE_SUMMARY:
7373                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7374                                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
7375                                 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
7376                                 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
7377                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
7378                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
7379                                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
7380                                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
7381                                 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
7382                                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
7383                                 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
7384                                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
7385                                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
7386                                 break;
7387
7388                         case RETRIEVE_FIELDS_FOR_DELETE:
7389                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7390                                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
7391                                 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
7392                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
7393                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
7394                                 break;
7395
7396                         case RETRIEVE_ACCOUNT:
7397                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7398                                 break;
7399
7400                         case RETRIEVE_FLAG:
7401                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7402                                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
7403                                 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
7404                                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
7405                                 break;
7406                 }
7407         }
7408
7409         ret = true;
7410
7411 FINISH_OFF:
7412         if (ret == true)
7413                 *mail = p_data_tbl;
7414         else
7415                 emstorage_free_mail(&p_data_tbl, number_of_mails, NULL);
7416
7417         if (result)
7418                 sqlite3_free_table(result);
7419
7420         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7421         _DISCONNECT_DB;
7422
7423         EM_SAFE_FREE(sql_query_string);
7424
7425         if (err_code != NULL)
7426                 *err_code = error;
7427
7428         EM_DEBUG_FUNC_END("ret [%d]", ret);
7429         return ret;
7430 }
7431
7432 INTERNAL_FUNC int emstorage_get_mail_by_id(int mail_id, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
7433 {
7434         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
7435
7436         if (mail_id <= 0 || !mail) {
7437                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
7438                 if (err_code != NULL)
7439                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7440                 return false;
7441         }
7442
7443         int ret = false, error = EMAIL_ERROR_NONE, count;
7444         char conditional_clause[QUERY_SIZE] = {0, };
7445         emstorage_mail_tbl_t* p_data_tbl = NULL;
7446
7447         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
7448         EM_DEBUG_LOG("query = [%s]", conditional_clause);
7449
7450         if(!emstorage_query_mail_tbl(conditional_clause, transaction, &p_data_tbl, &count, &error)) {
7451                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
7452                 goto FINISH_OFF;
7453         }
7454
7455         ret = true;
7456
7457 FINISH_OFF:
7458         if (ret == true)
7459                 *mail = p_data_tbl;
7460         else if (p_data_tbl != NULL)
7461                 emstorage_free_mail(&p_data_tbl, 1, &error);
7462
7463         if (err_code != NULL)
7464                 *err_code = error;
7465
7466         EM_DEBUG_FUNC_END("ret [%d]", ret);
7467         return ret;
7468 }
7469
7470 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)
7471 {
7472         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);
7473
7474         if (!search_handle || !searched)  {
7475                 if (err_code != NULL)
7476                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7477                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
7478                 EM_DEBUG_FUNC_END("false");
7479                 return false;
7480         }
7481
7482         emstorage_search_filter_t* p = search;
7483         int error = EMAIL_ERROR_NONE;
7484         DB_STMT hStmt = NULL;
7485         char sql_query_string[QUERY_SIZE] = {0, };
7486         int rc, ret = false;
7487         int and = false, mail_count = 0;
7488
7489         sqlite3 *local_db_handle = emstorage_get_db_connection();
7490         EMSTORAGE_START_READ_TRANSACTION(transaction);
7491
7492         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl");
7493
7494         if (account_id != ALL_ACCOUNT)  {
7495                 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);
7496                 and = true;
7497         }
7498
7499         if (mailbox_name)  {
7500                 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", mailbox_name);
7501                 and = true;
7502         }
7503
7504         while (p)  {
7505
7506                 if (p->key_type) {
7507                         if (!strncmp(p->key_type, "subject", strlen("subject"))) {
7508                                 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);
7509                                 and = true;
7510                         }
7511                         else if (!strncmp(p->key_type, "full_address_from", strlen("full_address_from"))) {
7512                                 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);
7513                                 and = true;
7514                         }
7515                         else if (!strncmp(p->key_type, "full_address_to", strlen("full_address_to"))) {
7516                                 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);
7517                                 and = true;
7518                         }
7519                         else if (!strncmp(p->key_type, "email_address", strlen("email_address"))) {
7520                                 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);
7521                                 and = true;
7522                         }
7523                         p = p->next;
7524                 }
7525         }
7526
7527         if (sorting)
7528                 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");
7529
7530         EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
7531
7532
7533         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7534         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7535                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7536
7537
7538         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7539         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7540                 ("sqlite3_step fail:%d", rc));
7541
7542         char **result;
7543
7544         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &mail_count, NULL, NULL), rc);
7545         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
7546                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7547
7548         sqlite3_free_table(result);
7549
7550         ret = true;
7551
7552 FINISH_OFF:
7553         if (ret == true)  {
7554                 *search_handle = (int)hStmt;
7555                 *searched = mail_count;
7556                 EM_DEBUG_LOG("mail_count [%d]", mail_count);
7557         }
7558         else  {
7559                 if (hStmt != NULL)  {
7560                         rc = sqlite3_finalize(hStmt);
7561                         if (rc != SQLITE_OK)  {
7562                                 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
7563                                 error = EMAIL_ERROR_DB_FAILURE;
7564                         }
7565                 }
7566
7567                 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7568                 _DISCONNECT_DB;
7569         }
7570
7571         if (err_code != NULL)
7572                 *err_code = error;
7573
7574         EM_DEBUG_FUNC_END("ret [%d]", ret);
7575         return ret;
7576 }
7577
7578 INTERNAL_FUNC int emstorage_mail_search_result(int search_handle, emstorage_mail_field_type_t type, void** data, int transaction, int *err_code)
7579 {
7580         EM_DEBUG_FUNC_BEGIN("search_handle[%d], type[%d], data[%p], transaction[%d], err_code[%p]", search_handle, type, data, transaction, err_code);
7581
7582         if (search_handle == 0 || !data) {
7583                 EM_DEBUG_EXCEPTION(" search_handle[%d], type[%d], data[%p]", search_handle, type, data);
7584
7585                 if (err_code != NULL)
7586                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7587                 return false;
7588         }
7589
7590         emstorage_mail_tbl_t* p_data_tbl = NULL;
7591         DB_STMT hStmt = (DB_STMT)search_handle;
7592         int rc, ret = false;
7593         int error = EMAIL_ERROR_NONE;
7594
7595         switch (type)  {
7596                 case RETRIEVE_ID:
7597                         _get_stmt_field_data_int(hStmt, (int *)data, MAIL_ID_IDX_IN_MAIL_TBL);
7598                         break;
7599
7600                 case RETRIEVE_ENVELOPE:
7601                 case RETRIEVE_ALL:
7602                         if (!(p_data_tbl = em_malloc(sizeof(emstorage_mail_tbl_t)))) {
7603                                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
7604
7605                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7606                                 goto FINISH_OFF;
7607                         }
7608
7609                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
7610                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
7611                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_TBL);
7612                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->mail_size), MAIL_SIZE_IDX_IN_MAIL_TBL);
7613                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
7614                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_from), 1, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL);
7615                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_to), 1, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL);
7616                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject), 1, SUBJECT_IDX_IN_MAIL_TBL);
7617                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->body_download_status), BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL);
7618                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL);
7619                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
7620                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
7621                         _get_stmt_field_data_time_t(hStmt, &(p_data_tbl->date_time), DATETIME_IDX_IN_MAIL_TBL);
7622                         _get_stmt_field_data_char  (hStmt, &(p_data_tbl->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL);
7623                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->DRM_status), DRM_STATUS_IDX_IN_MAIL_TBL);
7624                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->priority), PRIORITY_IDX_IN_MAIL_TBL);
7625                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->save_status), SAVE_STATUS_IDX_IN_MAIL_TBL);
7626                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->lock_status), LOCK_STATUS_IDX_IN_MAIL_TBL);
7627                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->report_status), REPORT_STATUS_IDX_IN_MAIL_TBL);
7628                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
7629                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->meeting_request_status), MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL);
7630                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->message_class), MESSAGE_CLASS_IDX_IN_MAIL_TBL);
7631                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->digest_type), DIGEST_TYPE_IDX_IN_MAIL_TBL);
7632                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->smime_type), SMIME_TYPE_IDX_IN_MAIL_TBL);
7633
7634                         if (type == RETRIEVE_ALL)  {
7635                                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
7636                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
7637                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_reply), 1, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL);
7638                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_cc), 1, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL);
7639                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_bcc), 1, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL);
7640                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_return), 1, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL);
7641                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->message_id), 0, MESSAGE_ID_IDX_IN_MAIL_TBL);
7642                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
7643                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
7644                                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl->attachment_count), ATTACHMENT_COUNT_IDX_IN_MAIL_TBL);
7645                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
7646
7647                         }
7648
7649                         if (p_data_tbl->body_download_status)  {
7650                                 struct stat buf;
7651
7652                                 if (p_data_tbl->file_path_html)  {
7653                                         if (stat(p_data_tbl->file_path_html, &buf) == -1)
7654                                                 p_data_tbl->body_download_status = 0;
7655                                 }
7656                                 else if (p_data_tbl->file_path_plain)  {
7657                                         if (stat(p_data_tbl->file_path_plain, &buf) == -1)
7658                                                 p_data_tbl->body_download_status = 0;
7659                                 }
7660                                 else
7661                                         p_data_tbl->body_download_status = 0;
7662                         }
7663
7664                         *((emstorage_mail_tbl_t**)data) = p_data_tbl;
7665                         break;
7666
7667                 case RETRIEVE_SUMMARY:
7668                         if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t))))  {
7669                                 EM_DEBUG_EXCEPTION(" malloc failed...");
7670
7671                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7672                                 goto FINISH_OFF;
7673                         }
7674
7675                         memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
7676
7677                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
7678                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
7679                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_TBL);
7680                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
7681                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
7682                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
7683
7684                         *((emstorage_mail_tbl_t**)data) = p_data_tbl;
7685                         break;
7686
7687                 case RETRIEVE_ADDRESS:
7688                         if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t))))  {
7689                                 EM_DEBUG_EXCEPTION(" malloc failed...");
7690                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7691                                 goto FINISH_OFF;
7692                         }
7693
7694                         memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
7695                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
7696                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
7697                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
7698                         *((emstorage_mail_tbl_t**)data) = p_data_tbl;
7699                         break;
7700
7701                 default:
7702                         break;
7703         }
7704
7705
7706         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7707         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7708                 ("sqlite3_step fail:%d", rc));
7709
7710         ret = true;
7711
7712 FINISH_OFF:
7713
7714         if (err_code != NULL)
7715                 *err_code = error;
7716
7717         EM_DEBUG_FUNC_END("ret [%d]", ret);
7718         return ret;
7719 }
7720
7721 INTERNAL_FUNC int emstorage_mail_search_end(int search_handle, int transaction, int *err_code)
7722 {
7723         EM_DEBUG_FUNC_BEGIN("search_handle[%d], transaction[%d], err_code[%p]", search_handle, transaction, err_code);
7724
7725         int error = EMAIL_ERROR_NONE;
7726         int rc, ret = false;
7727
7728         if (search_handle == 0)  {
7729                 EM_DEBUG_EXCEPTION(" search_handle[%d]", search_handle);
7730                 error = EMAIL_ERROR_INVALID_PARAM;
7731                 goto FINISH_OFF;
7732         }
7733
7734         DB_STMT hStmt = (DB_STMT)search_handle;
7735
7736         EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
7737
7738         rc = sqlite3_finalize(hStmt);
7739         if (rc != SQLITE_OK)  {
7740                 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
7741                 error = EMAIL_ERROR_DB_FAILURE;
7742         }
7743
7744         ret = true;
7745
7746 FINISH_OFF:
7747         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7748         _DISCONNECT_DB;
7749
7750         if (err_code != NULL)
7751                 *err_code = error;
7752
7753         EM_DEBUG_FUNC_END("ret [%d]", ret);
7754         return ret;
7755 }
7756
7757 INTERNAL_FUNC int emstorage_change_mail(int mail_id, emstorage_mail_tbl_t* mail, int transaction, int *err_code)
7758 {
7759         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
7760
7761         if (mail_id <= 0 || !mail) {
7762                 EM_DEBUG_EXCEPTION(" mail_id[%d], mail[%p]", mail_id, mail);
7763
7764                 if (err_code != NULL)
7765                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7766                 return false;
7767         }
7768
7769         DB_STMT hStmt = NULL;
7770         char sql_query_string[QUERY_SIZE] = {0, };
7771         int rc = -1;
7772         int ret = false;
7773         int error = EMAIL_ERROR_NONE;
7774         int i = 0;
7775         sqlite3 *local_db_handle = emstorage_get_db_connection();
7776         char mailbox_id_param_string[10] = {0,};
7777
7778         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
7779
7780         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7781                 "UPDATE mail_tbl SET"
7782                 "  mail_id = ?"
7783                 ", account_id = ?"
7784                 ", mailbox_id = ?"
7785                 ", mailbox_name = ?"
7786                 ", mail_size = ?"
7787                 ", server_mail_status = ?"
7788                 ", server_mailbox_name = ?"
7789                 ", server_mail_id = ?"
7790                 ", reference_mail_id = ?"
7791                 ", full_address_from = ?"
7792                 ", full_address_reply = ?"  /* 10 */
7793                 ", full_address_to = ?"
7794                 ", full_address_cc = ?"
7795                 ", full_address_bcc = ?"
7796                 ", full_address_return = ?"
7797                 ", subject = ?"
7798                 ", body_download_status = ?"
7799                 ", file_path_plain = ?"
7800                 ", file_path_html = ?"
7801                 ", date_time = ?"
7802                 ", flags_seen_field      = ?"
7803                 ", flags_deleted_field   = ?"
7804                 ", flags_flagged_field   = ?"
7805                 ", flags_answered_field  = ?"
7806                 ", flags_recent_field    = ?"
7807                 ", flags_draft_field     = ?"
7808                 ", flags_forwarded_field = ?"
7809                 ", DRM_status = ?"
7810                 ", priority = ?"
7811                 ", save_status = ?"
7812                 ", lock_status = ?"
7813                 ", message_id = ?"
7814                 ", report_status = ?"
7815                 ", preview_text = ?"
7816                 ", smime_type = ?"
7817                 " WHERE mail_id = %d AND account_id != 0 "
7818                 , mail_id);
7819
7820
7821         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7822         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7823                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7824
7825         _bind_stmt_field_data_int   (hStmt, i++, mail->mail_id);
7826         _bind_stmt_field_data_int   (hStmt, i++, mail->account_id);
7827         _bind_stmt_field_data_int   (hStmt, i++, mail->mailbox_id);
7828         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->mailbox_name, 0, MAILBOX_LEN_IN_MAIL_TBL);
7829         _bind_stmt_field_data_int   (hStmt, i++, mail->mail_size);
7830         _bind_stmt_field_data_int   (hStmt, i++, mail->server_mail_status);
7831         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mailbox_name, 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
7832         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mail_id, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
7833         _bind_stmt_field_data_int   (hStmt, i++, mail->reference_mail_id);
7834         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
7835         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
7836         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
7837         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
7838         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
7839         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
7840         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
7841         _bind_stmt_field_data_int   (hStmt, i++, mail->body_download_status);
7842         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
7843         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
7844         _bind_stmt_field_data_int   (hStmt, i++, mail->date_time);
7845         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_seen_field);
7846         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_deleted_field);
7847         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_flagged_field);
7848         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_answered_field);
7849         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_recent_field);
7850         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_draft_field);
7851         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_forwarded_field);
7852         _bind_stmt_field_data_int   (hStmt, i++, mail->DRM_status);
7853         _bind_stmt_field_data_int   (hStmt, i++, mail->priority);
7854         _bind_stmt_field_data_int   (hStmt, i++, mail->save_status);
7855         _bind_stmt_field_data_int   (hStmt, i++, mail->lock_status);
7856         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->message_id, 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
7857         _bind_stmt_field_data_int   (hStmt, i++, mail->report_status);
7858         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
7859         _bind_stmt_field_data_int   (hStmt, i++, mail->smime_type);
7860
7861         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7862         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7863                 ("sqlite3_step fail:%d", rc));
7864
7865         rc = sqlite3_changes(local_db_handle);
7866         if (rc == 0)  {
7867                 EM_DEBUG_EXCEPTION(" no matched mail found...");
7868                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7869                 goto FINISH_OFF;
7870         }
7871         SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
7872         if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail->mail_id, mailbox_id_param_string, 0))
7873                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_UPDATE ] >>>> ");
7874
7875         ret = true;
7876
7877 FINISH_OFF:
7878         if (hStmt != NULL)  {
7879                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
7880                 rc = sqlite3_finalize(hStmt);
7881                 if (rc != SQLITE_OK)  {
7882                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
7883                         error = EMAIL_ERROR_DB_FAILURE;
7884                 }
7885         }
7886
7887         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
7888         _DISCONNECT_DB;
7889
7890         if (err_code != NULL)
7891                 *err_code = error;
7892
7893         EM_DEBUG_FUNC_END("ret [%d]", ret);
7894         return ret;
7895 }
7896
7897 INTERNAL_FUNC int emstorage_modify_mailbox_of_mails(char *old_mailbox_name, char *new_mailbox_name, int transaction, int *err_code)
7898 {
7899         EM_DEBUG_FUNC_BEGIN("old_mailbox_name[%p], new_mailbox_name[%p], transaction[%d], err_code[%p]", old_mailbox_name, new_mailbox_name, transaction, err_code);
7900
7901         if (!old_mailbox_name && !new_mailbox_name)  {
7902                 EM_DEBUG_EXCEPTION(" old_mailbox_name[%p], new_mailbox_name[%p]", old_mailbox_name, new_mailbox_name);
7903
7904                 if (err_code != NULL)
7905                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7906                 return false;
7907         }
7908
7909         int rc = -1;
7910         int ret = false;
7911         int error = EMAIL_ERROR_NONE;
7912         char sql_query_string[QUERY_SIZE] = {0, };
7913
7914         sqlite3 *local_db_handle = emstorage_get_db_connection();
7915
7916         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
7917
7918         EM_DEBUG_LOG("Old Mailbox Name  [ %s ] , New Mailbox name [ %s ] ", old_mailbox_name, new_mailbox_name);
7919
7920         SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET mailbox_name = '%s' WHERE mailbox_name = '%s'", new_mailbox_name, old_mailbox_name);
7921
7922         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
7923         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7924                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7925
7926         rc = sqlite3_changes(local_db_handle);
7927         if (rc == 0) {
7928                 EM_DEBUG_EXCEPTION(" no matched mail found...");
7929
7930                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7931                 goto FINISH_OFF;
7932         }
7933
7934         EM_DEBUG_LOG(" Modification done in mail_read_mail_uid_tbl based on Mailbox name ");
7935         /* Modify the mailbox_name name in mail_read_mail_uid_tbl table */
7936         SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_read_mail_uid_tbl SET mailbox_name = '%s' WHERE mailbox_name = '%s'", new_mailbox_name, old_mailbox_name);
7937
7938         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
7939         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7940                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7941
7942         rc = sqlite3_changes(local_db_handle);
7943         if (rc == 0)  {
7944                 EM_DEBUG_EXCEPTION(" no matched mail found...");
7945                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7946                 goto FINISH_OFF;
7947         }
7948
7949         if (!emcore_notify_storage_event(NOTI_MAILBOX_UPDATE, 1, 0, new_mailbox_name, 0))
7950                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_MAILBOX_UPDATE] : Notification Failed >>> ");
7951
7952         ret = true;
7953
7954 FINISH_OFF:
7955
7956         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
7957         _DISCONNECT_DB;
7958
7959         if (err_code != NULL)
7960                 *err_code = error;
7961
7962         EM_DEBUG_FUNC_END("ret [%d]", ret);
7963         return ret;
7964 }
7965
7966 /**
7967   *  emstorage_clean_save_status(int save_status, int  *err_code) - set the all mail status to the set value
7968   *
7969   *
7970   **/
7971 INTERNAL_FUNC int emstorage_clean_save_status(int save_status, int  *err_code)
7972 {
7973         EM_DEBUG_FUNC_BEGIN("save_status[%d], err_code[%p]", save_status, err_code);
7974
7975         EM_IF_NULL_RETURN_VALUE(err_code, false);
7976
7977         int ret = false;
7978         int error = EMAIL_ERROR_NONE;
7979         int rc = 0;
7980         char sql_query_string[QUERY_SIZE] = {0, };
7981         sqlite3 *local_db_handle = emstorage_get_db_connection();
7982
7983         memset(sql_query_string, 0x00, sizeof(sql_query_string));
7984         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);
7985         EM_DEBUG_LOG("Query [%s]", sql_query_string);
7986
7987         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
7988         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7989                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7990
7991         rc = sqlite3_changes(local_db_handle);
7992         if (rc == 0) {
7993                 EM_DEBUG_LOG(" No Matched Mail Exists ");
7994                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7995         }
7996
7997         ret = true;
7998
7999 FINISH_OFF:
8000
8001         if (err_code != NULL)
8002                 *err_code = error;
8003
8004         EM_DEBUG_FUNC_END("ret [%d]", ret);
8005         return ret;
8006 }
8007
8008 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)
8009 {
8010         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);
8011         int i = 0;
8012         int error = EMAIL_ERROR_NONE;
8013         int rc = 0;
8014         int ret = false;
8015         int cur_mail_id_string = 0;
8016         int mail_id_string_buffer_length = 0;
8017         char  sql_query_string[QUERY_SIZE] = {0, };
8018         char *mail_id_string_buffer = NULL;
8019         char *parameter_string = NULL;
8020         sqlite3 *local_db_handle = emstorage_get_db_connection();
8021         email_mail_attribute_type target_mail_attribute_type = 0;
8022
8023         if (!mail_ids  || !field_name || account_id == 0) {
8024                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8025                 if (err_code != NULL)
8026                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8027                 return false;
8028         }
8029
8030         if( (error = _get_attribute_type_by_mail_field_name(field_name, &target_mail_attribute_type)) != EMAIL_ERROR_NONE) {
8031                 EM_DEBUG_EXCEPTION("emstorage_get_attribute_type_by_mail_field_name failed [%d]", error);
8032                 if (err_code != NULL)
8033                         *err_code = error;
8034                 return false;
8035         }
8036
8037         /* Generating mail id list string */
8038         mail_id_string_buffer_length = MAIL_ID_STRING_LENGTH * mail_ids_count;
8039
8040         mail_id_string_buffer = em_malloc(mail_id_string_buffer_length);
8041
8042         if(!mail_id_string_buffer) {
8043                 EM_DEBUG_EXCEPTION("em_malloc failed");
8044                 error = EMAIL_ERROR_OUT_OF_MEMORY;
8045                 goto FINISH_OFF;
8046         }
8047
8048         for(i = 0; i < mail_ids_count; i++)
8049                 cur_mail_id_string += SNPRINTF_OFFSET(mail_id_string_buffer, cur_mail_id_string, mail_id_string_buffer_length, "%d,", mail_ids[i]);
8050
8051         if(EM_SAFE_STRLEN(mail_id_string_buffer) > 1)
8052                 mail_id_string_buffer[EM_SAFE_STRLEN(mail_id_string_buffer) - 1] = NULL_CHAR;
8053
8054         /* Generating notification parameter string */
8055         parameter_string = em_malloc(mail_id_string_buffer_length + EM_SAFE_STRLEN(field_name) + 2);
8056
8057         if(!parameter_string) {
8058                 EM_DEBUG_EXCEPTION("em_malloc failed");
8059                 error = EMAIL_ERROR_OUT_OF_MEMORY;
8060                 goto FINISH_OFF;
8061         }
8062
8063         SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", field_name, 0x01, mail_id_string_buffer);
8064
8065         /* Write query string */
8066         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);
8067
8068         EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
8069
8070         /* Execute query */
8071         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8072         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8073         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8074                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8075         if (sqlite3_changes(local_db_handle) == 0)
8076                 EM_DEBUG_LOG("no mail matched...");
8077
8078         ret = true;
8079
8080 FINISH_OFF:
8081         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8082         _DISCONNECT_DB;
8083
8084         if (ret && parameter_string && !emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, target_mail_attribute_type, parameter_string, value))
8085                 EM_DEBUG_EXCEPTION("emcore_notify_storage_event failed : NOTI_MAIL_FIELD_UPDATE [%s,%d]", field_name, value);
8086
8087         EM_SAFE_FREE(mail_id_string_buffer);
8088         EM_SAFE_FREE(parameter_string);
8089
8090         if (err_code != NULL)
8091                 *err_code = error;
8092
8093         EM_DEBUG_FUNC_END("error [%d]", error);
8094         return ret;
8095 }
8096
8097 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)
8098 {
8099         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
8100
8101         if (mail_id <= 0 || !mail)  {
8102                 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], mail[%p]", mail_id, type, mail);
8103                 if (err_code != NULL)
8104                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8105                 return false;
8106         }
8107
8108         int ret = false;
8109         int error = EMAIL_ERROR_NONE;
8110         int move_flag = 0;
8111         DB_STMT hStmt = NULL;
8112         char sql_query_string[QUERY_SIZE] = {0, };
8113
8114         int rc = 0;
8115         int i = 0;
8116
8117         char *mailbox_name = NULL;
8118         char mailbox_id_param_string[10] = {0,};
8119
8120         sqlite3 *local_db_handle = emstorage_get_db_connection();
8121         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8122
8123         switch (type) {
8124                 case APPEND_BODY:
8125                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8126                                 "UPDATE mail_tbl SET"
8127                                 "  body_download_status = ?"
8128                                 ", file_path_plain = ?"
8129                                 ", file_path_html = ?"
8130                                 ", flags_seen_field      = ?"
8131                                 ", flags_deleted_field   = ?"
8132                                 ", flags_flagged_field   = ?"
8133                                 ", flags_answered_field  = ?"
8134                                 ", flags_recent_field    = ?"
8135                                 ", flags_draft_field     = ?"
8136                                 ", flags_forwarded_field = ?"
8137                                 ", DRM_status = ?"
8138                                 ", attachment_count = ?"
8139                                 ", preview_text= ?"
8140                                 ", meeting_request_status = ? "
8141                                 ", message_class = ? "
8142                                 ", digest_type = ? "
8143                                 ", smime_type = ? "
8144                                 " WHERE mail_id = %d AND account_id != 0"
8145                                 , mail_id);
8146
8147
8148                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8149                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8150                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8151                          i = 0;
8152
8153                         _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8154                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8155                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8156                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_seen_field);
8157                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_deleted_field);
8158                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_flagged_field);
8159                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_answered_field);
8160                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_recent_field);
8161                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_draft_field);
8162                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_forwarded_field);
8163                         _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
8164                         _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
8165                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
8166                         _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
8167                         _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
8168                         _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
8169                         _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
8170                         break;
8171
8172                 case UPDATE_MAILBOX: {
8173                                 int err;
8174                                 emstorage_mailbox_tbl_t *mailbox_tbl;
8175
8176                                 if (!emstorage_get_mailbox_by_name(mail->account_id, -1, mail->mailbox_name, &mailbox_tbl, false, &err)) {
8177                                         EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_name failed - %d", err);
8178
8179                                         goto FINISH_OFF;
8180                                 }
8181
8182                                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8183                                         "UPDATE mail_tbl SET"
8184                                         " mailbox_id = '%d'"
8185                                         " mailbox_name = '%s'"
8186                                         ",mailbox_type = '%d'"
8187                                         " WHERE mail_id = %d AND account_id != 0"
8188                                         , mailbox_tbl->mailbox_id
8189                                         , mail->mailbox_name ? mail->mailbox_name : ""
8190                                         , mailbox_tbl->mailbox_type
8191                                         , mail_id);
8192                                         move_flag = 1;
8193
8194                                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL); /*prevent 26251*/
8195                                 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8196
8197                                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8198                                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8199                                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8200
8201                         }
8202                         break;
8203
8204                 case UPDATE_FLAG:
8205                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8206                                 "UPDATE mail_tbl SET"
8207                                 " flags_seen_field      = %d"
8208                                 ",flags_deleted_field   = %d"
8209                                 ",flags_flagged_field   = %d"
8210                                 ",flags_answered_field  = %d"
8211                                 ",flags_recent_field    = %d"
8212                                 ",flags_draft_field     = %d"
8213                                 ",flags_forwarded_field = %d"
8214                                 "  WHERE mail_id = %d AND account_id != 0"
8215                                 , mail->flags_seen_field
8216                                 , mail->flags_deleted_field
8217                                 , mail->flags_flagged_field
8218                                 , mail->flags_answered_field
8219                                 , mail->flags_recent_field
8220                                 , mail->flags_draft_field
8221                                 , mail->flags_forwarded_field
8222                                 , mail_id);
8223                         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8224
8225
8226                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8227                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8228                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8229
8230                         break;
8231
8232                 case UPDATE_EXTRA_FLAG:
8233                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8234                                 "UPDATE mail_tbl SET"
8235                                 "  priority = %d"
8236                                 ", save_status = %d"
8237                                 ", lock_status = %d"
8238                                 ", report_status = %d"
8239                                 ", DRM_status = %d"
8240                                 " WHERE mail_id = %d AND account_id != 0"
8241                                 , mail->priority
8242                                 , mail->save_status
8243                                 , mail->lock_status
8244                                 , mail->report_status
8245                                 , mail->DRM_status
8246                                 , mail_id);
8247                         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8248
8249
8250                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8251                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8252                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8253                         break;
8254
8255                 case UPDATE_STICKY_EXTRA_FLAG:
8256                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8257                                 "UPDATE mail_tbl SET"
8258                                 "  lock_status = %d"
8259                                 "  WHERE mail_id = %d AND account_id != 0"
8260                                 , mail->lock_status
8261                                 , mail_id);
8262                         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8263
8264
8265                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8266                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8267                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8268                         break;
8269
8270                 case UPDATE_MAIL:
8271                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8272                                 "UPDATE mail_tbl SET"
8273                                 "  full_address_from = ?"
8274                                 ", full_address_reply = ?"
8275                                 ", full_address_to = ?"
8276                                 ", full_address_cc = ?"
8277                                 ", full_address_bcc = ?"
8278                                 ", full_address_return = ?"
8279                                 ", subject = ?"
8280                                 ", file_path_plain = ?"
8281                                 ", date_time = ?"
8282                                 ", flags_seen_field = ?"
8283                                 ", flags_deleted_field = ?"
8284                                 ", flags_flagged_field = ?"
8285                                 ", flags_answered_field = ?"
8286                                 ", flags_recent_field = ?"
8287                                 ", flags_draft_field = ?"
8288                                 ", flags_forwarded_field = ?"
8289                                 ", priority = ?"
8290                                 ", save_status = ?"
8291                                 ", lock_status = ?"
8292                                 ", report_status = ?"
8293                                 ", DRM_status = ?"
8294                                 ", file_path_html = ?"
8295                                 ", file_path_mime_entity = ?"
8296                                 ", mail_size = ?"
8297                                 ", preview_text = ?"
8298                                 ", body_download_status = ?"
8299                                 ", attachment_count = ?"
8300                                 ", inline_content_count = ?"
8301                                 ", meeting_request_status = ?"
8302                                 ", message_class = ?"
8303                                 ", digest_type = ?"
8304                                 ", smime_type = ?"
8305                                 " WHERE mail_id = %d AND account_id != 0"
8306                                 , mail_id);
8307
8308
8309                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8310                         EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
8311                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8312                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8313                         i = 0;
8314                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
8315                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
8316                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
8317                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
8318                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
8319                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
8320                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8321                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8322                         _bind_stmt_field_data_time_t(hStmt, i++, mail->date_time);
8323                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_seen_field);
8324                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_deleted_field);
8325                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_flagged_field);
8326                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_answered_field);
8327                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_recent_field);
8328                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_draft_field);
8329                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_forwarded_field);
8330                         _bind_stmt_field_data_int   (hStmt, i++, mail->priority);
8331                         _bind_stmt_field_data_int   (hStmt, i++, mail->save_status);
8332                         _bind_stmt_field_data_int   (hStmt, i++, mail->lock_status);
8333                         _bind_stmt_field_data_int   (hStmt, i++, mail->report_status);
8334                         _bind_stmt_field_data_int   (hStmt, i++, mail->DRM_status);
8335                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8336                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
8337                         _bind_stmt_field_data_int   (hStmt, i++, mail->mail_size);
8338                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8339                         _bind_stmt_field_data_int   (hStmt, i++, mail->body_download_status);
8340                         _bind_stmt_field_data_int   (hStmt, i++, mail->attachment_count);
8341                         _bind_stmt_field_data_int   (hStmt, i++, mail->inline_content_count);
8342                         _bind_stmt_field_data_int   (hStmt, i++, mail->meeting_request_status);
8343                         _bind_stmt_field_data_int   (hStmt, i++, mail->message_class);
8344                         _bind_stmt_field_data_int   (hStmt, i++, mail->digest_type);
8345                         _bind_stmt_field_data_int   (hStmt, i++, mail->smime_type);
8346                         break;
8347
8348                 case UPDATE_DATETIME:  {
8349                         time_t now = time(NULL);
8350
8351                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8352                                 "UPDATE mail_tbl SET"
8353                                 " date_time = '%d'"
8354                                 " WHERE mail_id = %d AND account_id != 0"
8355                                 , (int)now
8356                                 , mail_id);
8357
8358                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8359                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8360                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8361                         break;
8362                 }
8363
8364                 case UPDATE_FROM_CONTACT_INFO:
8365                         EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_FROM_CONTACT_INFO");
8366                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8367                                 "UPDATE mail_tbl SET"
8368                                 " email_address_sender = ?,"
8369                                 " WHERE mail_id = %d",
8370                                 mail_id);
8371
8372                         hStmt = NULL;
8373
8374                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8375                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8376                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8377                         i = 0;
8378                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8379                         break;
8380
8381                 case UPDATE_TO_CONTACT_INFO:
8382                         EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_TO_CONTACT_INFO");
8383                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8384                                 "UPDATE mail_tbl SET"
8385                                 " email_address_recipient = ?,"
8386                                 " WHERE mail_id = %d",
8387                                 mail_id);
8388
8389                         hStmt = NULL;
8390
8391                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8392                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8393                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8394                         i = 0;
8395                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8396                         break;
8397
8398                         case UPDATE_ALL_CONTACT_INFO:
8399                                 EM_DEBUG_LOG("emstorage_change_mail_field - mail change type is UPDATE_ALL_CONTACT_INFO");
8400                                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8401                                         "UPDATE mail_tbl SET"
8402                                         " email_address_sender = ?,"
8403                                         " email_address_recipient = ?,"
8404                                         " WHERE mail_id = %d",
8405                                         mail_id);
8406
8407                                 hStmt = NULL;
8408
8409                                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8410                                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8411                                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8412                                 i = 0;
8413                                 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8414                                 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8415                                 break;
8416
8417
8418 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
8419                         case UPDATE_PARTIAL_BODY_DOWNLOAD:
8420
8421                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8422                         "UPDATE mail_tbl SET"
8423                         "  body_download_status = ?"
8424                         ", file_path_plain = ?"
8425                         ", file_path_html = ?"
8426                         ", attachment_count = ?"
8427                         ", inline_content_count = ?"
8428                         ", preview_text= ?"
8429                         " WHERE mail_id = %d"
8430                         , mail_id);
8431
8432
8433                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8434                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8435                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8436                          i = 0;
8437
8438                         _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8439                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8440                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html,  0, TEXT_2_LEN_IN_MAIL_TBL);
8441                         _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
8442                         _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
8443                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->preview_text,    0, PREVIEWBODY_LEN_IN_MAIL_TBL);
8444
8445                         break;
8446
8447 #endif
8448                 case UPDATE_FILE_PATH:
8449                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8450                                 "UPDATE mail_tbl SET"
8451                                 ", file_path_plain = ?"
8452                                 ", file_path_html = ?"
8453                                 ", file_path_mime_entity = ?"
8454                                 " WHERE mail_id = %d"
8455                                 , mail_id);
8456
8457
8458                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8459                         EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
8460                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8461                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8462                         i = 0;
8463                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8464                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8465                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
8466                         break;
8467
8468                 default:
8469                         EM_DEBUG_LOG(" type[%d]", type);
8470
8471                         error = EMAIL_ERROR_INVALID_PARAM;
8472                         goto FINISH_OFF;
8473         }
8474
8475         if (hStmt != NULL)  {
8476
8477         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8478                 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
8479                 ("sqlite3_step fail:%d", rc));
8480                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8481                         ("sqlite3_step fail:%d", rc));
8482                 rc = sqlite3_changes(local_db_handle);
8483                 if (rc == 0)  {
8484                         EM_DEBUG_EXCEPTION(" no matched mail found...");
8485
8486                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
8487                         goto FINISH_OFF;
8488                 }
8489         }
8490
8491         if (mail->account_id == 0) {
8492                 emstorage_mail_tbl_t* mail_for_account_tbl = NULL;
8493                 if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_ACCOUNT, &mail_for_account_tbl, true, &error) || !mail_for_account_tbl) {
8494                         EM_DEBUG_EXCEPTION(" emstorage_get_mail_field_by_id failed - %d", error);
8495 /*               */
8496                         goto FINISH_OFF;
8497                 }
8498                 mail->account_id = mail_for_account_tbl->account_id;
8499                 if (mail_for_account_tbl)
8500                         emstorage_free_mail(&mail_for_account_tbl, 1, NULL);
8501         }
8502
8503         ret = true;
8504
8505 FINISH_OFF:
8506
8507         if (hStmt != NULL)  {
8508                 rc = sqlite3_finalize(hStmt);
8509                 if (rc != SQLITE_OK)  {
8510                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
8511                         error = EMAIL_ERROR_DB_FAILURE;
8512                 }
8513                 hStmt = NULL;
8514         }
8515
8516         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8517         _DISCONNECT_DB;
8518
8519         /*h.gahlaut@samsung.com: Moving publication of notification after commiting transaction to DB */
8520
8521         if (ret == true &&  move_flag != 1) {
8522                 if (!emstorage_get_mailbox_name_by_mailbox_type(mail->account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &mailbox_name, false, &error))
8523                         EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_name_by_mailbox_type failed - %d", error);
8524
8525                 if (mail->mailbox_name && mailbox_name) {
8526                         if (strcmp(mail->mailbox_name, mailbox_name) != 0) {
8527                                 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
8528                                 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, mailbox_id_param_string, type))
8529                                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_UPDATE ] >>>> ");
8530                         }
8531                 }
8532                 else {
8533                         /* h.gahlaut@samsung.com: Jan 10, 2011 Publishing noti to refresh outbox when email sending status changes */
8534                         if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, NULL, type))
8535                                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_UPDATE ] ");
8536                 }
8537         }
8538
8539         EM_SAFE_FREE(mailbox_name);
8540
8541         if (err_code != NULL)
8542                 *err_code = error;
8543
8544         EM_DEBUG_FUNC_END("ret [%d]", ret);
8545         return ret;
8546 }
8547 INTERNAL_FUNC int emstorage_increase_mail_id(int *mail_id, int transaction, int *err_code)
8548 {
8549         EM_DEBUG_FUNC_BEGIN("mail_id[%p], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
8550
8551         int rc, ret = false;
8552         int error = EMAIL_ERROR_NONE;
8553         int latest_mail_id = 0;
8554         sqlite3 *local_db_handle = NULL;
8555         char *sql = "SELECT MAX(mail_id) FROM mail_tbl;";
8556         char **result = NULL;
8557
8558 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
8559         _timedlock_shm_mutex(&mapped_for_generating_mail_id, 2);
8560 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
8561
8562         ret = vconf_get_int(VCONF_KEY_LATEST_MAIL_ID, &latest_mail_id);
8563         if (ret < 0 || latest_mail_id == 0) {
8564                 EM_DEBUG_LOG("vconf_get_int() failed [%d] or latest_mail_id is zero", ret);
8565
8566                 local_db_handle = emstorage_get_db_connection();
8567
8568                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
8569                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
8570                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
8571                 if (NULL == result[1])
8572                         rc = 1;
8573                 else
8574                         rc = atoi(result[1]) + 1;
8575
8576                 sqlite3_free_table(result);
8577                 latest_mail_id = rc;
8578         }
8579
8580         latest_mail_id++;
8581
8582         ret = vconf_set_int(VCONF_KEY_LATEST_MAIL_ID, latest_mail_id);
8583
8584         if (mail_id)
8585                 *mail_id = latest_mail_id;
8586
8587 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
8588         _unlockshm_mutex(&mapped_for_generating_mail_id);
8589 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
8590
8591         ret = true;
8592
8593 FINISH_OFF:
8594         _DISCONNECT_DB;
8595
8596         if (err_code != NULL)
8597                 *err_code = error;
8598
8599         EM_DEBUG_FUNC_END("ret [%d]", ret);
8600         return ret;
8601 }
8602
8603
8604 INTERNAL_FUNC int emstorage_add_mail(emstorage_mail_tbl_t *mail_tbl_data, int get_id, int transaction, int *err_code)
8605 {
8606         EM_PROFILE_BEGIN(profile_emstorage_add_mail);
8607         EM_DEBUG_FUNC_BEGIN("mail_tbl_data[%p], get_id[%d], transaction[%d], err_code[%p]", mail_tbl_data, get_id, transaction, err_code);
8608
8609         if (!mail_tbl_data)  {
8610                 EM_DEBUG_EXCEPTION("mail_tbl_data[%p], get_id[%d]", mail_tbl_data, get_id);
8611                 if (err_code != NULL)
8612                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8613                 return false;
8614         }
8615
8616         int rc, ret = false;
8617         int error = EMAIL_ERROR_NONE;
8618         char sql_query_string[QUERY_SIZE] = {0, };
8619         DB_STMT hStmt = NULL;
8620         sqlite3 *local_db_handle = emstorage_get_db_connection();
8621
8622         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8623
8624         if (get_id)  {
8625                 /*  increase unique id */
8626                 char *sql = "SELECT max(rowid) FROM mail_tbl;";
8627                 char **result;
8628
8629                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
8630                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
8631                         ("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle)));
8632
8633                 if (NULL == result[1])
8634                         rc = 1;
8635                 else
8636                         rc = atoi(result[1])+1;
8637
8638                 sqlite3_free_table(result);
8639
8640                 mail_tbl_data->mail_id   = rc;
8641                 mail_tbl_data->thread_id = rc;
8642         }
8643
8644         if (mail_tbl_data->date_time == 0)
8645                 mail_tbl_data->date_time = time(NULL);
8646
8647         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8648                 "INSERT INTO mail_tbl VALUES "
8649                 "( ?" /*  mail_id */
8650                 ", ?" /*  account_id */
8651                 ", ?" /*  mailbox_id */
8652                 ", ?" /*  mailbox_name */
8653                 ", ?" /*  mailbox_type */
8654                 ", ?" /*  subject */
8655                 ", ?" /*  date_time */
8656                 ", ?" /*  server_mail_status */
8657                 ", ?" /*  server_mailbox_name */
8658                 ", ?" /*  server_mail_id */
8659                 ", ?" /*  message_id */
8660                 ", ?" /*  reference_mail_id */
8661                 ", ?" /*  full_address_from */
8662                 ", ?" /*  full_address_reply */
8663                 ", ?" /*  full_address_to */
8664                 ", ?" /*  full_address_cc */
8665                 ", ?" /*  full_address_bcc */
8666                 ", ?" /*  full_address_return */
8667                 ", ?" /*  email_address_sender */
8668                 ", ?" /*  email_address_recipient */
8669                 ", ?" /*  alias_sender */
8670                 ", ?" /*  alias_recipient */
8671                 ", ?" /*  body_download_status */
8672                 ", ?" /*  file_path_plain */
8673                 ", ?" /*  file_path_html */
8674                 ", ?" /*  file_path_mime_entity */
8675                 ", ?" /*  mail_size */
8676                 ", ?" /*  flags_seen_field */
8677                 ", ?" /*  flags_deleted_field */
8678                 ", ?" /*  flags_flagged_field */
8679                 ", ?" /*  flags_answered_field */
8680                 ", ?" /*  flags_recent_field */
8681                 ", ?" /*  flags_draft_field */
8682                 ", ?" /*  flags_forwarded_field */
8683                 ", ?" /*  DRM_status */
8684                 ", ?" /*  priority */
8685                 ", ?" /*  save_status */
8686                 ", ?" /*  lock_status */
8687                 ", ?" /*  report_status */
8688                 ", ?" /*  attachment_count */
8689                 ", ?" /*  inline_content_count */
8690                 ", ?" /*  thread_id */
8691                 ", ?" /*  thread_item_count */
8692                 ", ?" /*  preview_text */
8693                 ", ?" /*  meeting_request_status */
8694                 ", ?" /*  message_class */
8695                 ", ?" /*  digest_type */
8696                 ", ?" /*  smime_type */
8697                 ")");
8698
8699         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8700         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8701                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8702
8703         _bind_stmt_field_data_int   (hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mail_id);
8704         _bind_stmt_field_data_int   (hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, mail_tbl_data->account_id);
8705         _bind_stmt_field_data_int   (hStmt, MAILBOX_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_id);
8706         _bind_stmt_field_data_string(hStmt, MAILBOX_NAME_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->mailbox_name, 0, MAILBOX_LEN_IN_MAIL_TBL);
8707         _bind_stmt_field_data_int   (hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_type);
8708         _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8709         _bind_stmt_field_data_int   (hStmt, DATETIME_IDX_IN_MAIL_TBL, mail_tbl_data->date_time);
8710         _bind_stmt_field_data_int   (hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->server_mail_status);
8711         _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);
8712         _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);
8713         _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);
8714         _bind_stmt_field_data_int   (hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, mail_tbl_data->reference_mail_id);
8715         _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);
8716         _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);
8717         _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);
8718         _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);
8719         _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);
8720         _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);
8721         _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);
8722         _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);
8723         _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);
8724         _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);
8725         _bind_stmt_field_data_int   (hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->body_download_status);
8726         _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);
8727         _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);
8728         _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);
8729         _bind_stmt_field_data_int   (hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, mail_tbl_data->mail_size);
8730         _bind_stmt_field_data_int   (hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_seen_field);
8731         _bind_stmt_field_data_int   (hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_deleted_field);
8732         _bind_stmt_field_data_int   (hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_flagged_field);
8733         _bind_stmt_field_data_int   (hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_answered_field);
8734         _bind_stmt_field_data_int   (hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_recent_field);
8735         _bind_stmt_field_data_int   (hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_draft_field);
8736         _bind_stmt_field_data_int   (hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_forwarded_field);
8737         _bind_stmt_field_data_int   (hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->DRM_status);
8738         _bind_stmt_field_data_int   (hStmt, PRIORITY_IDX_IN_MAIL_TBL, mail_tbl_data->priority);
8739         _bind_stmt_field_data_int   (hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->save_status);
8740         _bind_stmt_field_data_int   (hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->lock_status);
8741         _bind_stmt_field_data_int   (hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->report_status);
8742         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->attachment_count);
8743         _bind_stmt_field_data_int   (hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->inline_content_count);
8744         _bind_stmt_field_data_int   (hStmt, THREAD_ID_IDX_IN_MAIL_TBL, mail_tbl_data->thread_id);
8745         _bind_stmt_field_data_int   (hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->thread_item_count);
8746         _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8747         _bind_stmt_field_data_int   (hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->meeting_request_status);
8748         _bind_stmt_field_data_int   (hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, mail_tbl_data->message_class);
8749         _bind_stmt_field_data_int   (hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->digest_type);
8750         _bind_stmt_field_data_int   (hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->smime_type);
8751
8752         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8753         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
8754         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
8755         ret = true;
8756
8757 FINISH_OFF:
8758
8759         if (hStmt != NULL)  {
8760                 rc = sqlite3_finalize(hStmt);
8761                 if (rc != SQLITE_OK)  {
8762                         EM_DEBUG_LOG("sqlite3_finalize failed [%d]", rc);
8763                         error = EMAIL_ERROR_DB_FAILURE;
8764                 }
8765         }
8766
8767         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8768         _DISCONNECT_DB;
8769
8770
8771         if (err_code != NULL)
8772                 *err_code = error;
8773
8774         EM_PROFILE_END(profile_emstorage_add_mail);
8775         EM_DEBUG_FUNC_END("ret [%d]", ret);
8776         return ret;
8777 }
8778
8779 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)
8780 {
8781         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);
8782
8783         int rc, ret = false, i, cur_conditional_clause = 0;
8784         int error = EMAIL_ERROR_NONE;
8785         int target_account_id;
8786         char sql_query_string[QUERY_SIZE] = {0, }, conditional_clause[QUERY_SIZE] = {0, };
8787         emstorage_mailbox_tbl_t *result_mailbox = NULL;
8788         email_mailbox_type_e target_mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED;
8789         char* target_mailbox_name = NULL;
8790
8791         if (!mail_ids || input_mailbox_id <= 0) {
8792                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8793                 if (err_code != NULL)
8794                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8795                 return false;
8796         }
8797
8798         sqlite3 *local_db_handle = emstorage_get_db_connection();
8799
8800         if ((error = emstorage_get_mailbox_by_id(input_mailbox_id, &result_mailbox)) != EMAIL_ERROR_NONE || !result_mailbox) {
8801                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", error);
8802                 goto FINISH_OFF;
8803         }
8804
8805         target_mailbox_name = EM_SAFE_STRDUP(result_mailbox->mailbox_name);
8806         target_mailbox_type = result_mailbox->mailbox_type;
8807         target_account_id   = result_mailbox->account_id;
8808         emstorage_free_mailbox(&result_mailbox, 1, NULL);
8809
8810         cur_conditional_clause = SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id in (");
8811
8812         for(i = 0; i < number_of_mails; i++)
8813                 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, QUERY_SIZE, "%d,", mail_ids[i]);
8814
8815         conditional_clause[EM_SAFE_STRLEN(conditional_clause) - 1] = ')';
8816
8817         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8818
8819         /* Updating a mail_tbl */
8820
8821         memset(sql_query_string, 0x00, QUERY_SIZE);
8822         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_tbl SET mailbox_name = '%s', mailbox_type = %d, mailbox_id = %d, account_id = %d %s", target_mailbox_name, target_mailbox_type, input_mailbox_id, target_account_id, conditional_clause);
8823         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8824
8825         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8826         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
8827                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8828
8829         /* Updating a mail_attachment_tbl */
8830
8831         memset(sql_query_string, 0x00, QUERY_SIZE);
8832         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);
8833         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8834
8835         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8836         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
8837                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8838
8839         /* Updating a mail_meeting_tbl */
8840         memset(sql_query_string, 0x00, QUERY_SIZE);
8841         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);
8842         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8843
8844         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8845         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
8846                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8847
8848         /* Updating a mail_read_mail_uid_tbl */
8849         memset(conditional_clause, 0x00, QUERY_SIZE);
8850         cur_conditional_clause = SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE local_uid in (");
8851
8852         for(i = 0; i < number_of_mails; i++)
8853                 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, QUERY_SIZE, "%d,", mail_ids[i]);
8854
8855         /* prevent 34415 */
8856         char *last_comma = rindex(conditional_clause, ',');
8857         *last_comma = ')'; /* replace , with ) */
8858
8859         memset(sql_query_string, 0x00, QUERY_SIZE);
8860         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);
8861         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8862
8863         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8864         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
8865                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8866
8867         ret = true;
8868
8869 FINISH_OFF:
8870         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8871         _DISCONNECT_DB;
8872
8873         EM_SAFE_FREE(target_mailbox_name);
8874
8875         if (err_code != NULL)
8876                 *err_code = error;
8877
8878         EM_DEBUG_FUNC_END("ret [%d]", ret);
8879         return ret;
8880 }
8881
8882 INTERNAL_FUNC int emstorage_delete_mail(int mail_id, int from_server, int transaction, int *err_code)
8883 {
8884         EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
8885
8886         if (!mail_id)  {
8887                 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
8888                 if (err_code != NULL)
8889                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8890                 return false;
8891         }
8892
8893         int rc, ret = false;
8894         int error = EMAIL_ERROR_NONE;
8895         char sql_query_string[QUERY_SIZE] = {0, };
8896
8897         sqlite3 *local_db_handle = emstorage_get_db_connection();
8898         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8899
8900         memset(sql_query_string, 0x00, sizeof(sql_query_string));
8901
8902         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id = %d ", mail_id);
8903         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8904
8905
8906         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8907         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
8908                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8909
8910         ret = true;
8911
8912 FINISH_OFF:
8913         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8914         _DISCONNECT_DB;
8915
8916         if (err_code != NULL)
8917                 *err_code = error;
8918
8919         EM_DEBUG_FUNC_END("ret [%d]", ret);
8920         return ret;
8921 }
8922
8923 INTERNAL_FUNC int emstorage_delete_multiple_mails(int mail_ids[], int number_of_mails, int transaction, int *err_code)
8924 {
8925         EM_DEBUG_FUNC_BEGIN("mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", mail_ids, number_of_mails, transaction, err_code);
8926
8927         int rc, ret = false, i, cur_sql_query_string = 0;
8928         int error = EMAIL_ERROR_NONE;
8929         char sql_query_string[QUERY_SIZE] = {0, };
8930
8931         if (!mail_ids) {
8932                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8933                 if (err_code != NULL)
8934                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8935                 return false;
8936         }
8937
8938         sqlite3 *local_db_handle = emstorage_get_db_connection();
8939         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8940
8941         cur_sql_query_string = SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id in (");
8942
8943         for(i = 0; i < number_of_mails; i++)
8944                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, QUERY_SIZE, "%d,", mail_ids[i]);
8945
8946         /* prevent 34414 */
8947         char *last_comma = rindex(sql_query_string, ',');
8948         *last_comma = ')'; /* replace , with ) */
8949
8950         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8951
8952         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8953         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
8954                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8955
8956         ret = true;
8957
8958 FINISH_OFF:
8959         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8960         _DISCONNECT_DB;
8961
8962         if (err_code != NULL)
8963                 *err_code = error;
8964
8965         EM_DEBUG_FUNC_END("ret [%d]", ret);
8966         return ret;
8967 }
8968
8969 INTERNAL_FUNC int emstorage_delete_mail_by_account(int account_id, int transaction, int *err_code)
8970 {
8971         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
8972
8973         if (account_id < FIRST_ACCOUNT_ID)  {
8974                 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
8975
8976                 if (err_code != NULL)
8977                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8978                 return false;
8979         }
8980
8981         int rc, ret = false;
8982         int error = EMAIL_ERROR_NONE;
8983         char sql_query_string[QUERY_SIZE] = {0, };
8984
8985         sqlite3 *local_db_handle = emstorage_get_db_connection();
8986         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8987
8988         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d", account_id);
8989         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8990
8991         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8992         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8993                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8994
8995         rc = sqlite3_changes(local_db_handle);
8996         if (rc == 0)  {
8997                 EM_DEBUG_EXCEPTION(" no mail found...");
8998                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8999         }
9000
9001         /* Delete all mails  mail_read_mail_uid_tbl table based on account id */
9002         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
9003         EM_DEBUG_LOG("Query [%s]", sql_query_string);
9004
9005         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9006         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9007                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9008
9009         rc = sqlite3_changes(local_db_handle);
9010         if (rc == 0)  {
9011                 EM_DEBUG_EXCEPTION("no mail found...");
9012                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9013         }
9014
9015         if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_WITH_ACCOUNT, account_id, 0 , NULL, 0))
9016                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_DELETE_ALL ]");
9017
9018         ret = true;
9019
9020 FINISH_OFF:
9021
9022         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9023         _DISCONNECT_DB;
9024
9025         if (err_code != NULL)
9026                 *err_code = error;
9027
9028         EM_DEBUG_FUNC_END("ret [%d]", ret);
9029         return ret;
9030 }
9031
9032 INTERNAL_FUNC int emstorage_delete_mail_by_mailbox(int account_id, char *mailbox, int transaction, int *err_code)
9033 {
9034         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox, transaction, err_code);
9035
9036         if (account_id < FIRST_ACCOUNT_ID || !mailbox)  {
9037                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox[%p]", account_id, mailbox);
9038                 if (err_code != NULL)
9039                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9040                 return false;
9041         }
9042
9043         int rc, ret = false;
9044         int error = EMAIL_ERROR_NONE;
9045         char sql_query_string[QUERY_SIZE] = {0, };
9046
9047         sqlite3 *local_db_handle = emstorage_get_db_connection();
9048
9049         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9050
9051         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d AND mailbox_name = '%s'", account_id, mailbox);
9052         EM_DEBUG_LOG("Query [%s]", sql_query_string);
9053
9054         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9055         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9056                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9057
9058
9059         /* Delete Mails from mail_read_mail_uid_tbl */
9060         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d AND mailbox_name = '%s'", account_id, mailbox);
9061         EM_DEBUG_LOG("Query [%s]", sql_query_string);
9062
9063         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9064         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9065                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9066
9067         if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_ALL, account_id, 0 , mailbox, 0))
9068                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_DELETE_ALL ] >>>> ");
9069
9070         ret = true;
9071
9072 FINISH_OFF:
9073         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9074         _DISCONNECT_DB;
9075
9076         if (err_code != NULL)
9077                 *err_code = error;
9078
9079         EM_DEBUG_FUNC_END("ret [%d]", ret);
9080         return ret;
9081 }
9082
9083 INTERNAL_FUNC int emstorage_free_mail(emstorage_mail_tbl_t** mail_list, int count, int *err_code)
9084 {
9085         EM_DEBUG_FUNC_BEGIN("mail_list[%p], count[%d], err_code[%p]", mail_list, count, err_code);
9086
9087         if (count > 0)  {
9088                 if ((mail_list == NULL) || (*mail_list == NULL))  {
9089                         EM_DEBUG_EXCEPTION("mail_ilst[%p], count[%d]", mail_list, count);
9090
9091                         if (err_code)
9092                                 *err_code = EMAIL_ERROR_INVALID_PARAM;
9093                         return false;
9094                 }
9095
9096                 emstorage_mail_tbl_t* p = *mail_list;
9097                 int i = 0;
9098
9099                 for (; i < count; i++, p++) {
9100                         EM_SAFE_FREE(p->mailbox_name);
9101                         EM_SAFE_FREE(p->server_mailbox_name);
9102                         EM_SAFE_FREE(p->server_mail_id);
9103                         EM_SAFE_FREE(p->full_address_from);
9104                         EM_SAFE_FREE(p->full_address_reply);
9105                         EM_SAFE_FREE(p->full_address_to);
9106                         EM_SAFE_FREE(p->full_address_cc);
9107                         EM_SAFE_FREE(p->full_address_bcc);
9108                         EM_SAFE_FREE(p->full_address_return);
9109                         EM_SAFE_FREE(p->subject);
9110                         EM_SAFE_FREE(p->file_path_plain);
9111                         EM_SAFE_FREE(p->file_path_html);
9112                         EM_SAFE_FREE(p->file_path_mime_entity);
9113                         EM_SAFE_FREE(p->message_id);
9114                         EM_SAFE_FREE(p->email_address_sender);
9115                         EM_SAFE_FREE(p->email_address_recipient);
9116                         EM_SAFE_FREE(p->preview_text);
9117                         EM_SAFE_FREE(p->alias_sender);
9118                         EM_SAFE_FREE(p->alias_recipient);
9119                 }
9120                 EM_SAFE_FREE(*mail_list);
9121         }
9122
9123         if (err_code != NULL)
9124                 *err_code = EMAIL_ERROR_NONE;
9125
9126         EM_DEBUG_FUNC_END();
9127         return true;
9128 }
9129
9130 INTERNAL_FUNC int emstorage_get_attachment_count(int mail_id, int *count, int transaction, int *err_code)
9131 {
9132         EM_DEBUG_FUNC_BEGIN("mail_id[%d], count[%p], transaction[%d], err_code[%p]", mail_id, count, transaction, err_code);
9133
9134         if (mail_id <= 0 || !count)  {
9135                 EM_DEBUG_EXCEPTION("mail_id[%d], count[%p]", mail_id, count);
9136                 if (err_code != NULL)
9137                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9138                 return false;
9139         }
9140
9141         int rc = -1, ret = false;
9142         int error = EMAIL_ERROR_NONE;
9143         char sql_query_string[QUERY_SIZE] = {0, };
9144
9145         sqlite3 *local_db_handle = emstorage_get_db_connection();
9146         EMSTORAGE_START_READ_TRANSACTION(transaction);
9147
9148         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
9149
9150         char **result;
9151
9152         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
9153         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9154                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9155
9156         *count = atoi(result[1]);
9157         sqlite3_free_table(result);
9158
9159         ret = true;
9160
9161 FINISH_OFF:
9162
9163         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
9164         _DISCONNECT_DB;
9165
9166         if (err_code != NULL)
9167                 *err_code = error;
9168
9169         EM_DEBUG_FUNC_END("ret [%d]", ret);
9170         return ret;
9171 }
9172
9173 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)
9174 {
9175         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);
9176
9177         if (input_mail_id <= 0 || !output_attachment_list || !output_attachment_count)  {
9178                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9179                 return EMAIL_ERROR_INVALID_PARAM;
9180         }
9181
9182         int                         error = EMAIL_ERROR_NONE;
9183         int                         i = 0;
9184         int                         rc = -1;
9185         char                      **result = NULL;
9186         char                        sql_query_string[QUERY_SIZE] = {0, };
9187         emstorage_attachment_tbl_t* p_data_tbl = NULL;
9188         DB_STMT hStmt = NULL;
9189         sqlite3 *local_db_handle = emstorage_get_db_connection();
9190
9191         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
9192         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", input_mail_id);
9193         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
9194         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9195                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9196
9197         *output_attachment_count = atoi(result[1]);
9198         sqlite3_free_table(result);
9199
9200         if(*output_attachment_count == 0) {
9201                 error = EMAIL_ERROR_NONE;
9202                 goto FINISH_OFF;
9203         }
9204
9205         p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * (*output_attachment_count));
9206
9207         if (!p_data_tbl)  {
9208                 EM_DEBUG_EXCEPTION("em_malloc failed...");
9209                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9210                 goto FINISH_OFF;
9211         }
9212
9213         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE mail_id = %d ORDER BY attachment_id", input_mail_id);
9214         EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
9215
9216         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9217         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)));
9218
9219         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9220         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },   ("sqlite3_step fail:%d", rc));
9221
9222         if (rc == SQLITE_DONE)  {
9223                 EM_DEBUG_EXCEPTION("no matched attachment found...");
9224                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
9225                 goto FINISH_OFF;
9226         }
9227         for (i = 0; i < *output_attachment_count; i++)  {
9228                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9229                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
9230                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
9231                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
9232                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9233                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9234                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9235                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9236                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9237                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
9238                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9239                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9240
9241                 EM_DEBUG_LOG("attachment[%d].attachment_id : %d", i, p_data_tbl[i].attachment_id);
9242                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9243                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9244         }
9245
9246 FINISH_OFF:
9247
9248         if (error == EMAIL_ERROR_NONE)
9249                 *output_attachment_list = p_data_tbl;
9250         else if (p_data_tbl != NULL)
9251                 emstorage_free_attachment(&p_data_tbl, *output_attachment_count, NULL);
9252
9253         if (hStmt) {
9254                 rc = sqlite3_finalize(hStmt);
9255                 if (rc != SQLITE_OK)  {
9256                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
9257                         error = EMAIL_ERROR_DB_FAILURE;
9258                 }
9259         }
9260
9261         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
9262
9263         _DISCONNECT_DB;
9264
9265         EM_DEBUG_FUNC_END("error [%d]", error);
9266         return error;
9267 }
9268
9269 INTERNAL_FUNC int emstorage_get_attachment(int attachment_id, emstorage_attachment_tbl_t** attachment, int transaction, int *err_code)
9270 {
9271         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], transaction[%d], err_code[%p]", attachment_id, attachment, transaction, err_code);
9272
9273         if (attachment_id <= 0 || !attachment)  {
9274                 EM_DEBUG_EXCEPTION("attachment_id[%d], attachment[%p]", attachment_id, attachment);
9275                 if (err_code != NULL)
9276                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9277                 return false;
9278         }
9279
9280         emstorage_attachment_tbl_t* p_data_tbl = NULL;
9281         int rc, ret = false;
9282         int error = EMAIL_ERROR_NONE;
9283         char sql_query_string[QUERY_SIZE] = {0, };
9284
9285         sqlite3 *local_db_handle = emstorage_get_db_connection();
9286         EMSTORAGE_START_READ_TRANSACTION(transaction);
9287
9288         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_id = %d",  attachment_id);
9289
9290         sqlite3_stmt* hStmt = NULL;
9291
9292         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9293         EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
9294
9295         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9296                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9297
9298
9299         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9300         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9301                 ("sqlite3_step fail:%d", rc));
9302
9303         if (rc == SQLITE_DONE)  {
9304                 EM_DEBUG_EXCEPTION("no matched attachment found...");
9305                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
9306                 goto FINISH_OFF;
9307         }
9308
9309         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1)))  {
9310                 EM_DEBUG_EXCEPTION("malloc failed...");
9311                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9312                 goto FINISH_OFF;
9313         }
9314
9315         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9316         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
9317         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
9318         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
9319         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9320         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9321         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9322         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9323         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9324         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
9325         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9326         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9327
9328 #ifdef __ATTACHMENT_OPTI__
9329                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
9330                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
9331 #endif
9332
9333         ret = true;
9334
9335 FINISH_OFF:
9336         if (ret == true)
9337                 *attachment = p_data_tbl;
9338
9339         if (hStmt != NULL)  {
9340                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
9341
9342                 rc = sqlite3_finalize(hStmt);
9343                 if (rc != SQLITE_OK)  {
9344                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
9345                         error = EMAIL_ERROR_DB_FAILURE;
9346                 }
9347         }
9348
9349         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
9350         _DISCONNECT_DB;
9351
9352         if (err_code != NULL)
9353                 *err_code = error;
9354
9355         EM_DEBUG_FUNC_END("ret [%d]", ret);
9356         return ret;
9357 }
9358
9359 INTERNAL_FUNC int emstorage_get_attachment_nth(int mail_id, int nth, emstorage_attachment_tbl_t** attachment_tbl, int transaction, int *err_code)
9360 {
9361         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);
9362
9363         if (mail_id <= 0 || nth <= 0 || !attachment_tbl)  {
9364                 EM_DEBUG_EXCEPTION(" mail_id[%d], nth[%d], attachment[%p]", mail_id, nth, attachment_tbl);
9365
9366                 if (err_code != NULL)
9367                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9368                 return false;
9369         }
9370
9371         emstorage_attachment_tbl_t* p_data_tbl = NULL;
9372         char *p = NULL;
9373         int rc, ret = false;
9374         int error = EMAIL_ERROR_NONE;
9375         char sql_query_string[QUERY_SIZE] = {0, };
9376
9377         sqlite3 *local_db_handle = emstorage_get_db_connection();
9378         EMSTORAGE_START_READ_TRANSACTION(transaction);
9379
9380         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));
9381         EM_DEBUG_LOG("query = [%s]", sql_query_string);
9382
9383         DB_STMT hStmt = NULL;
9384
9385         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9386         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9387                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9388
9389
9390         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9391         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9392                 ("sqlite3_step fail:%d", rc));
9393
9394         if (rc == SQLITE_DONE)  {
9395                 EM_DEBUG_EXCEPTION(" no matched attachment found...");
9396                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
9397                 goto FINISH_OFF;
9398         }
9399
9400         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1)))  {
9401                 EM_DEBUG_EXCEPTION(" malloc failed...");
9402                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9403                 goto FINISH_OFF;
9404         }
9405
9406         p_data_tbl->attachment_id = sqlite3_column_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9407         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
9408                 p_data_tbl->attachment_name = cpy_str(p);
9409         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
9410                 p_data_tbl->attachment_path = cpy_str(p);
9411         p_data_tbl->attachment_size = sqlite3_column_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
9412         p_data_tbl->mail_id = sqlite3_column_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9413         p_data_tbl->account_id = sqlite3_column_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9414         p_data_tbl->mailbox_id = sqlite3_column_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9415         p_data_tbl->attachment_save_status = sqlite3_column_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9416         p_data_tbl->attachment_drm_type = sqlite3_column_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9417         p_data_tbl->attachment_drm_method = sqlite3_column_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
9418         p_data_tbl->attachment_inline_content_status = sqlite3_column_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9419         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
9420                 p_data_tbl->attachment_mime_type = cpy_str(p);
9421 #ifdef __ATTACHMENT_OPTI__
9422                 p_data_tbl->encoding = sqlite3_column_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
9423                 if ((p = (char *)sqlite3_column_text(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
9424                         p_data_tbl->section= cpy_str(p);
9425 #endif
9426         ret = true;
9427
9428 FINISH_OFF:
9429         if (ret == true)
9430                 *attachment_tbl = p_data_tbl;
9431
9432         if (hStmt != NULL)  {
9433                 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
9434
9435                 rc = sqlite3_finalize(hStmt);
9436                 if (rc != SQLITE_OK)  {
9437                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
9438                         error = EMAIL_ERROR_DB_FAILURE;
9439                 }
9440         }
9441
9442         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
9443         _DISCONNECT_DB;
9444
9445         if (err_code != NULL)
9446                 *err_code = error;
9447
9448         EM_DEBUG_FUNC_END("ret [%d]", ret);
9449         return ret;
9450 }
9451
9452 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)
9453 {
9454         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], attachment[%p], transaction[%d], err_code[%p]", mail_id, type, attachment, transaction, err_code);
9455
9456         if (mail_id <= 0 || !attachment)  {
9457                 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], attachment[%p]", mail_id, type, attachment);
9458                 if (err_code != NULL)
9459                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9460                 return false;;
9461         }
9462
9463         int rc, ret = false;
9464         int error = EMAIL_ERROR_NONE;
9465         DB_STMT hStmt = NULL;
9466         char sql_query_string[QUERY_SIZE] = {0, };
9467
9468         int i = 0;
9469
9470         sqlite3 *local_db_handle = emstorage_get_db_connection();
9471
9472         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9473
9474         switch (type)  {
9475                 case UPDATE_MAILBOX:
9476                                 EM_DEBUG_LOG("UPDATE_MAILBOX");
9477                         if (!attachment->mailbox_id)  {
9478                                 EM_DEBUG_EXCEPTION(" attachment->mailbox_id[%d]", attachment->mailbox_id);
9479                                 error = EMAIL_ERROR_INVALID_PARAM;
9480                                 goto FINISH_OFF;
9481                         }
9482                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9483                                 "UPDATE mail_attachment_tbl SET mailbox_id = ? WHERE mail_id = %d", mail_id);
9484
9485                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9486                         EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
9487                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9488                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9489
9490                         _bind_stmt_field_data_int(hStmt, i++, attachment->mailbox_id);
9491                         break;
9492
9493                 case UPDATE_SAVENAME:
9494                         EM_DEBUG_LOG("UPDATE_SAVENAME");
9495                         if (!attachment->attachment_path)  {
9496                                 EM_DEBUG_EXCEPTION(" attachment->attachment_path[%p]", attachment->attachment_path);
9497                                 error = EMAIL_ERROR_INVALID_PARAM;
9498                                 goto FINISH_OFF;
9499                         }
9500
9501                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9502                                 "UPDATE mail_attachment_tbl SET"
9503                                 "  attachment_size = ?"
9504                                 ", attachment_save_status = 1"
9505                                 ", attachment_path = ?"
9506                                 " WHERE mail_id = %d"
9507                                 " AND attachment_id = %d"
9508                                 , attachment->mail_id
9509                                 , attachment->attachment_id);
9510
9511
9512                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9513                         EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
9514                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9515                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9516
9517                         _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_size);
9518                         _bind_stmt_field_data_string(hStmt, i++, (char *)attachment->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
9519                         break;
9520
9521                 default:
9522                         EM_DEBUG_LOG("type[%d]", type);
9523                         error = EMAIL_ERROR_INVALID_PARAM;
9524                         goto FINISH_OFF;
9525         }
9526         EM_DEBUG_LOG("query = [%s]", sql_query_string);
9527
9528         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9529         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9530                 ("sqlite3_step fail:%d", rc));
9531         ret = true;
9532
9533 FINISH_OFF:
9534         if (hStmt != NULL)  {
9535                 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
9536                 rc = sqlite3_finalize(hStmt);
9537                 if (rc != SQLITE_OK)  {
9538                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
9539                         error = EMAIL_ERROR_DB_FAILURE;
9540                 }
9541         }
9542
9543         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9544         _DISCONNECT_DB;
9545
9546         if (err_code != NULL)
9547                 *err_code = error;
9548         EM_DEBUG_FUNC_END("ret [%d]", ret);
9549         return ret;
9550 }
9551
9552
9553 INTERNAL_FUNC int emstorage_rename_mailbox(int input_mailbox_id, char *input_new_mailbox_name, char *input_new_mailbox_alias, int input_transaction)
9554 {
9555         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);
9556
9557         int rc = 0;
9558         int ret = false;
9559         int error = EMAIL_ERROR_NONE;
9560         char sql_query_string[QUERY_SIZE] = {0, };
9561         sqlite3 *local_db_handle = NULL;
9562         int account_id = 0;
9563         emstorage_mailbox_tbl_t *old_mailbox_data = NULL;
9564
9565         if (input_mailbox_id <= 0 || !input_new_mailbox_name || !input_new_mailbox_alias)  {
9566                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9567                 return EMAIL_ERROR_INVALID_PARAM;
9568         }
9569
9570         local_db_handle = emstorage_get_db_connection();
9571
9572         if ((error = emstorage_get_mailbox_by_id(input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
9573                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", error);
9574                 goto FINISH_OFF;
9575         }
9576         account_id = old_mailbox_data->account_id;
9577
9578         EMSTORAGE_START_WRITE_TRANSACTION(input_transaction, error);
9579
9580         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9581                 "UPDATE mail_box_tbl SET"
9582                 " mailbox_name = '%s'"
9583                 ",alias = '%s'"
9584                 " WHERE mailbox_id = %d"
9585                 , input_new_mailbox_name
9586                 , input_new_mailbox_alias
9587                 , input_mailbox_id);
9588
9589         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9590
9591         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9592                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9593
9594         if (sqlite3_changes(local_db_handle) == 0)
9595                 EM_DEBUG_LOG("no mail_meeting_tbl matched...");
9596
9597         /* Update mail_tbl */
9598         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9599                 "UPDATE mail_tbl SET"
9600                 " mailbox_name = '%s'"
9601                 " WHERE mailbox_id = %d"
9602                 , input_new_mailbox_name
9603                 , input_mailbox_id);
9604
9605         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9606
9607         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9608                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9609
9610         if (sqlite3_changes(local_db_handle) == 0)
9611                 EM_DEBUG_LOG("no mail matched...");
9612
9613         ret = true;
9614
9615 FINISH_OFF:
9616
9617         EMSTORAGE_FINISH_WRITE_TRANSACTION(input_transaction, ret, error);
9618
9619         if (ret) {
9620                 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME, account_id, input_mailbox_id, input_new_mailbox_name, 0))
9621                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAILBOX_RENAME ] >>>> ");
9622         }
9623         else {
9624                 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME_FAIL, account_id, input_mailbox_id, input_new_mailbox_name, error))
9625                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAILBOX_RENAME_FAIL ] >>>> ");
9626         }
9627
9628         if (old_mailbox_data)
9629                 emstorage_free_mailbox(&old_mailbox_data, 1, NULL);
9630
9631         _DISCONNECT_DB;
9632
9633         EM_DEBUG_FUNC_END("error [%d]", error);
9634         return error;
9635 }
9636
9637 INTERNAL_FUNC int emstorage_get_new_attachment_no(int *attachment_no, int *err_code)
9638 {
9639         EM_DEBUG_FUNC_BEGIN("attachment_no [%p], err_code[%p]", attachment_no, err_code);
9640         int rc, ret = false;
9641         int error = EMAIL_ERROR_NONE;
9642         char *sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
9643         char **result;
9644
9645         if (!attachment_no)  {
9646                 EM_DEBUG_EXCEPTION("Invalid attachment");
9647                 error = EMAIL_ERROR_INVALID_PARAM;
9648                 goto FINISH_OFF;
9649         }
9650
9651         *attachment_no = -1;
9652
9653         sqlite3 *local_db_handle = emstorage_get_db_connection();
9654
9655
9656         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9657         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9658                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9659
9660         if (NULL == result[1])
9661                 rc = 1;
9662         else
9663                 rc = atoi(result[1])+1;
9664
9665         sqlite3_free_table(result);
9666
9667         *attachment_no = rc;
9668         EM_DEBUG_LOG("attachment_no [%d]", *attachment_no);
9669         ret = true;
9670
9671 FINISH_OFF:
9672         _DISCONNECT_DB;
9673
9674         if (err_code != NULL)
9675                 *err_code = error;
9676
9677         EM_DEBUG_FUNC_END("ret [%d]", ret);
9678         return ret;
9679 }
9680
9681 INTERNAL_FUNC int emstorage_add_attachment(emstorage_attachment_tbl_t* attachment_tbl, int iscopy, int transaction, int *err_code)
9682 {
9683         EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], iscopy[%d], transaction[%d], err_code[%p]", attachment_tbl, iscopy, transaction, err_code);
9684
9685         char *sql = NULL;
9686         char **result;
9687         int rc, ret = false;
9688         int error = EMAIL_ERROR_NONE;
9689         DB_STMT hStmt = NULL;
9690         char sql_query_string[QUERY_SIZE] = {0, };
9691         sqlite3 *local_db_handle = emstorage_get_db_connection();
9692
9693         if (!attachment_tbl)  {
9694                 EM_DEBUG_EXCEPTION("attachment_tbl[%p], iscopy[%d]", attachment_tbl, iscopy);
9695                 if (err_code != NULL)
9696                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9697                 return false;
9698         }
9699
9700         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9701
9702         sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
9703
9704         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9705         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9706                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9707
9708         if (NULL==result[1]) rc = 1;
9709         else rc = atoi(result[1]) + 1;
9710         sqlite3_free_table(result);
9711
9712         attachment_tbl->attachment_id = rc;
9713
9714         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9715                 "INSERT INTO mail_attachment_tbl VALUES "
9716                 "( ?"   /* attachment_id */
9717                 ", ?"   /* attachment_name */
9718                 ", ?"   /* attachment_path */
9719                 ", ?"   /* attachment_size */
9720                 ", ?"   /* mail_id */
9721                 ", ?"   /* account_id */
9722                 ", ?"   /* mailbox_id */
9723                 ", ?"   /* attachment_save_status */
9724                 ", ?"   /* attachment_drm_type */
9725                 ", ?"   /* attachment_drm_method */
9726                 ", ?"   /* attachment_inline_content_status */
9727                 ", ?"   /* attachment_mime_type */
9728 #ifdef __ATTACHMENT_OPTI__
9729                 ", ?"
9730                 ", ?"
9731 #endif
9732                 ")");
9733
9734
9735         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9736         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9737                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9738
9739         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_id);
9740         _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);
9741         _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);
9742         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_size);
9743         _bind_stmt_field_data_int   (hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mail_id);
9744         _bind_stmt_field_data_int   (hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->account_id);
9745         _bind_stmt_field_data_int   (hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mailbox_id);
9746         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_save_status);
9747         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_type);
9748         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_method);
9749         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_inline_content_status);
9750         _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);
9751 #ifdef __ATTACHMENT_OPTI__
9752         _bind_stmt_field_data_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->encoding);
9753         _bind_stmt_field_data_string(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->section, 0, ATTACHMENT_LEN_IN_MAIL_ATTACHMENT_TBL);
9754 #endif
9755
9756
9757         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9758         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
9759                 ("sqlite3_step fail:%d", rc));
9760         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9761                 ("sqlite3_step fail:%d", rc));
9762 /*
9763         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9764                 "UPDATE mail_tbl SET attachment_count = 1 WHERE mail_id = %d", attachment_tbl->mail_id);
9765
9766         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9767
9768         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
9769                 ("sqlite3_exec fail:%d", rc));
9770         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9771                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9772 */
9773         rc = sqlite3_changes(local_db_handle);
9774         if (rc == 0)  {
9775                 EM_DEBUG_EXCEPTION(" no matched mail found...");
9776                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9777                 goto FINISH_OFF;
9778         }
9779
9780         ret = true;
9781
9782 FINISH_OFF:
9783         if (hStmt != NULL)  {
9784                 rc = sqlite3_finalize(hStmt);
9785                 if (rc != SQLITE_OK)  {
9786                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
9787                         error = EMAIL_ERROR_DB_FAILURE;
9788                 }
9789         }
9790
9791         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9792         _DISCONNECT_DB;
9793
9794         if (err_code != NULL)
9795         *err_code = error;
9796
9797         EM_DEBUG_FUNC_END("ret [%d]", ret);
9798         return ret;
9799 }
9800
9801 INTERNAL_FUNC int emstorage_update_attachment(emstorage_attachment_tbl_t* attachment_tbl, int transaction, int *err_code)
9802 {
9803         EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], transaction[%d], err_code[%p]", attachment_tbl, transaction, err_code);
9804
9805         int rc, ret = false, field_idx = 0;
9806         int error = EMAIL_ERROR_NONE;
9807         DB_STMT hStmt = NULL;
9808         char sql_query_string[QUERY_SIZE] = {0, };
9809
9810         if (!attachment_tbl)  {
9811                 EM_DEBUG_EXCEPTION(" attachment_tbl[%p] ", attachment_tbl);
9812                 error = EMAIL_ERROR_INVALID_PARAM;
9813                 goto FINISH_OFF;
9814         }
9815
9816         sqlite3 *local_db_handle = emstorage_get_db_connection();
9817
9818         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9819
9820         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9821                 "UPDATE mail_attachment_tbl SET  "
9822                 "  attachment_name = ?"
9823                 ", attachment_path =  ?"
9824                 ", attachment_size = ?"
9825                 ", mail_id = ?"
9826                 ", account_id = ?"
9827                 ", mailbox_id = ?"
9828                 ", attachment_save_status = ?"
9829                 ", attachment_drm_type = ?"
9830                 ", attachment_drm_method = ?"
9831                 ", attachment_inline_content_status = ? "
9832                 ", attachment_mime_type = ? "
9833                 " WHERE attachment_id = ?;");
9834
9835
9836         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9837
9838         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9839                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9840
9841         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
9842         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
9843         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_size);
9844         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->mail_id);
9845         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->account_id);
9846         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->mailbox_id);
9847         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_save_status);
9848         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_drm_type);
9849         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_drm_method);
9850         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_inline_content_status);
9851         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_mime_type, 0, ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL);
9852         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_id);
9853
9854
9855         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9856         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
9857                 ("sqlite3_step fail:%d", rc));
9858         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9859                 ("sqlite3_step fail:%d", rc));
9860 /*
9861         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9862                 "UPDATE mail_tbl SET attachment_count = 1 WHERE mail_id = %d", attachment_tbl->mail_id);
9863
9864         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9865
9866         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
9867                 ("sqlite3_exec fail:%d", rc));
9868         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9869                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9870 */
9871         rc = sqlite3_changes(local_db_handle);
9872         if (rc == 0)  {
9873                 EM_DEBUG_EXCEPTION(" no matched mail found...");
9874                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9875                 goto FINISH_OFF;
9876         }
9877
9878         ret = true;
9879
9880 FINISH_OFF:
9881         if (hStmt != NULL)  {
9882                 rc = sqlite3_finalize(hStmt);
9883                 if (rc != SQLITE_OK)  {
9884                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
9885                         error = EMAIL_ERROR_DB_FAILURE;
9886                 }
9887         }
9888
9889         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9890         _DISCONNECT_DB;
9891
9892         if (err_code != NULL)
9893         *err_code = error;
9894
9895         EM_DEBUG_FUNC_END("ret [%d]", ret);
9896         return ret;
9897 }
9898
9899 INTERNAL_FUNC int emstorage_delete_attachment_on_db(int attachment_id, int transaction, int *err_code)
9900 {
9901         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], transaction[%d], err_code[%p]", attachment_id, transaction, err_code);
9902
9903         if (attachment_id < 0)  {
9904                 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
9905                 if (err_code != NULL)
9906                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9907                 return false;
9908         }
9909
9910         int rc, ret = false;
9911         int error = EMAIL_ERROR_NONE;
9912         char sql_query_string[QUERY_SIZE] = {0, };
9913
9914         sqlite3 *local_db_handle = emstorage_get_db_connection();
9915
9916         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9917
9918         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
9919
9920         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9921         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9922                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9923
9924         ret = true;
9925
9926 FINISH_OFF:
9927         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9928         _DISCONNECT_DB;
9929
9930         if (err_code)
9931                 *err_code = error;
9932
9933         EM_DEBUG_FUNC_END("ret [%d]", ret);
9934         return ret;
9935 }
9936
9937 INTERNAL_FUNC int emstorage_delete_all_attachments_of_mail(int mail_id, int transaction, int *err_code)
9938 {
9939         EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9940         int rc, ret = false;
9941         int error = EMAIL_ERROR_NONE;
9942         char sql_query_string[QUERY_SIZE] = {0, };
9943         sqlite3 *local_db_handle = NULL;
9944
9945         if (mail_id <= 0)  {
9946                 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
9947                 if (err_code != NULL)
9948                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9949                 return false;
9950         }
9951
9952         local_db_handle = emstorage_get_db_connection();
9953
9954         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9955
9956         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
9957
9958         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9959         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9960                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9961
9962         ret = true;
9963
9964 FINISH_OFF:
9965         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9966         _DISCONNECT_DB;
9967
9968         if (err_code)
9969                 *err_code = error;
9970
9971         EM_DEBUG_FUNC_END("ret [%d]", ret);
9972         return ret;
9973 }
9974
9975 INTERNAL_FUNC int emstorage_delete_attachment_all_on_db(int account_id, char *mailbox, int transaction, int *err_code)
9976 {
9977         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox, transaction, err_code);
9978
9979         int error = EMAIL_ERROR_NONE;
9980         int rc, ret = false;
9981         char sql_query_string[QUERY_SIZE] = {0, };
9982
9983         sqlite3 *local_db_handle = emstorage_get_db_connection();
9984
9985         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9986
9987         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl");
9988
9989         if (account_id != ALL_ACCOUNT) /*  '0' means all account */
9990                 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);
9991
9992         if (mailbox)    /*  NULL means all mailbox_name */
9993                 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", mailbox);
9994
9995         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9996         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9997                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9998
9999         ret = true;
10000
10001 FINISH_OFF:
10002         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10003         _DISCONNECT_DB;
10004
10005         if (err_code != NULL)
10006                 *err_code = error;
10007
10008         EM_DEBUG_FUNC_END("ret [%d]", ret);
10009         return ret;
10010 }
10011
10012 INTERNAL_FUNC int emstorage_free_attachment(emstorage_attachment_tbl_t** attachment_tbl_list, int count, int *err_code)
10013 {
10014         EM_DEBUG_FUNC_BEGIN("attachment_tbl_list[%p], count[%d], err_code[%p]", attachment_tbl_list, count, err_code);
10015
10016         if (count > 0)  {
10017                 if ((attachment_tbl_list == NULL) || (*attachment_tbl_list == NULL))  {
10018                         EM_DEBUG_EXCEPTION(" attachment_tbl_list[%p], count[%d]", attachment_tbl_list, count);
10019                         if (err_code != NULL)
10020                                 *err_code = EMAIL_ERROR_INVALID_PARAM;
10021                         return false;
10022                 }
10023
10024                 emstorage_attachment_tbl_t* p = *attachment_tbl_list;
10025                 int i;
10026
10027                 for (i = 0; i < count; i++)  {
10028                         EM_SAFE_FREE(p[i].attachment_name);
10029                         EM_SAFE_FREE(p[i].attachment_path);
10030                         EM_SAFE_FREE(p[i].attachment_mime_type);
10031 #ifdef __ATTACHMENT_OPTI__
10032                         EM_SAFE_FREE(p[i].section);
10033 #endif
10034                 }
10035
10036                 EM_SAFE_FREE(p);
10037                 *attachment_tbl_list = NULL;
10038         }
10039
10040         if (err_code != NULL)
10041                 *err_code = EMAIL_ERROR_NONE;
10042         EM_DEBUG_FUNC_END();
10043         return true;
10044 }
10045
10046
10047
10048 INTERNAL_FUNC int emstorage_begin_transaction(void *d1, void *d2, int *err_code)
10049 {
10050         EM_PROFILE_BEGIN(emStorageBeginTransaction);
10051         int ret = true;
10052
10053         ENTER_CRITICAL_SECTION(_transactionBeginLock);
10054
10055         /*  wait for the trnasaction authority to be changed. */
10056         while (g_transaction)  {
10057                 EM_DEBUG_LOG(">>>>>>>> Wait for the transaction authority to be changed");
10058                 usleep(50000);
10059         }
10060
10061         /*  take the transaction authority. */
10062         g_transaction = true;
10063
10064         LEAVE_CRITICAL_SECTION(_transactionBeginLock);
10065
10066         sqlite3 *local_db_handle = emstorage_get_db_connection();
10067         int rc;
10068         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN immediate;", NULL, NULL, NULL), rc);
10069         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
10070                 ("SQL(BEGIN) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
10071         if (ret == false && err_code != NULL)
10072                 *err_code = EMAIL_ERROR_DB_FAILURE;
10073
10074         EM_PROFILE_END(emStorageBeginTransaction);
10075         EM_DEBUG_FUNC_END("ret [%d]", ret);
10076         return ret;
10077 }
10078
10079 INTERNAL_FUNC int emstorage_commit_transaction(void *d1, void *d2, int *err_code)
10080 {
10081         EM_DEBUG_FUNC_BEGIN();
10082         int ret = true;
10083         sqlite3 *local_db_handle = emstorage_get_db_connection();
10084
10085         ENTER_CRITICAL_SECTION(_transactionEndLock);
10086
10087         int rc;
10088         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
10089         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; }, ("SQL(END) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
10090         /*  release the transaction authority. */
10091         g_transaction = false;
10092
10093         LEAVE_CRITICAL_SECTION(_transactionEndLock);
10094         if (ret == false && err_code != NULL)
10095                 *err_code = EMAIL_ERROR_DB_FAILURE;
10096
10097         EM_DEBUG_FUNC_END("ret [%d]", ret);
10098         return ret;
10099 }
10100
10101 INTERNAL_FUNC int emstorage_rollback_transaction(void *d1, void *d2, int *err_code)
10102 {
10103         EM_DEBUG_FUNC_BEGIN();
10104         int ret = true;
10105         sqlite3 *local_db_handle = emstorage_get_db_connection();
10106         int rc;
10107
10108         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "ROLLBACK;", NULL, NULL, NULL), rc);
10109
10110         ENTER_CRITICAL_SECTION(_transactionEndLock);
10111         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
10112                 ("SQL(ROLLBACK) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
10113
10114         /*  release the transaction authority. */
10115         g_transaction = false;
10116
10117         LEAVE_CRITICAL_SECTION(_transactionEndLock);
10118
10119         if (ret == false && err_code != NULL)
10120                 *err_code = EMAIL_ERROR_DB_FAILURE;
10121
10122         EM_DEBUG_FUNC_END("ret [%d]", ret);
10123         return ret;
10124 }
10125
10126 INTERNAL_FUNC int emstorage_is_mailbox_full(int account_id, email_mailbox_t *mailbox, int *result, int *err_code)
10127 {
10128         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], result[%p], err_code[%p]", account_id, mailbox, result, err_code);
10129
10130         if (account_id < FIRST_ACCOUNT_ID || !mailbox || !result)  {
10131                 if (mailbox)
10132                         EM_DEBUG_EXCEPTION("Invalid Parameter. accoun_id[%d], mailbox[%p]", account_id, mailbox);
10133
10134                 if (err_code != NULL)
10135                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10136
10137                 return false;
10138         }
10139
10140         int ret = false;
10141         int error = EMAIL_ERROR_NONE;
10142         int mail_count = 0;
10143
10144         if (!emstorage_get_mail_count(account_id, mailbox->mailbox_name, &mail_count, NULL, true, &error)) {
10145                 EM_DEBUG_EXCEPTION("emstorage_get_mail_count failed [%d]", error);
10146                 goto FINISH_OFF;
10147         }
10148
10149         if (mailbox) {
10150                 EM_DEBUG_LOG("mail_count[%d] mail_slot_size[%d]", mail_count, mailbox->mail_slot_size);
10151                 if (mail_count >= mailbox->mail_slot_size)
10152                         *result = true;
10153                 else
10154                         *result = false;
10155
10156                 ret = true;
10157         }
10158
10159         ret = true;
10160 FINISH_OFF:
10161
10162         if (err_code != NULL)
10163                 *err_code = error;
10164
10165         EM_DEBUG_FUNC_END("ret [%d]", ret);
10166         return ret;
10167 }
10168
10169 INTERNAL_FUNC int emstorage_clear_mail_data(int transaction, int *err_code)
10170 {
10171         EM_DEBUG_FUNC_BEGIN("transaction[%d], err_code[%p]", transaction, err_code);
10172
10173         int rc, ret = false;
10174         int error = EMAIL_ERROR_NONE;
10175         char sql_query_string[QUERY_SIZE] = {0, };
10176
10177         const email_db_object_t* tables = _g_db_tables;
10178         const email_db_object_t* indexes = _g_db_indexes;
10179
10180         sqlite3 *local_db_handle = emstorage_get_db_connection();
10181         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10182
10183         if (!emstorage_delete_dir(MAILHOME, &error)) {
10184                 EM_DEBUG_EXCEPTION(" emstorage_delete_dir failed - %d", error);
10185
10186                 goto FINISH_OFF;
10187         }
10188
10189         mkdir(MAILHOME, DIRECTORY_PERMISSION);
10190         mkdir(MAILTEMP, DIRECTORY_PERMISSION);
10191
10192         /*  first clear index. */
10193         while (indexes->object_name)  {
10194                 if (indexes->data_flag)  {
10195                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP index %s", indexes->object_name);
10196                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10197                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10198                                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10199                 }
10200                 indexes++;
10201         }
10202
10203         while (tables->object_name)  {
10204                 if (tables->data_flag)  {
10205                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP table %s", tables->object_name);
10206                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10207                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10208                                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10209                 }
10210
10211                 tables++;
10212         }
10213         ret = true;
10214
10215 FINISH_OFF:
10216         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10217         _DISCONNECT_DB;
10218
10219         if (err_code != NULL)
10220                 *err_code = error;
10221
10222         EM_DEBUG_FUNC_END("ret [%d]", ret);
10223         return ret;
10224 }
10225 /*======================= DB File Utils =============================================*/
10226 #include <dirent.h>
10227 #include <sys/types.h>
10228 #define  DIR_SEPERATOR "/"
10229
10230 INTERNAL_FUNC char *emstorage_make_directory_path_from_file_path(char *file_name)
10231 {
10232         EM_DEBUG_FUNC_BEGIN("Filename [ %p ]", file_name);
10233         char delims[] = "/";
10234         char *result = NULL;
10235
10236         result = strtok(file_name, delims);
10237
10238         if (result)
10239                 EM_DEBUG_LOG(">>>> Directory_name [ %s ]", result);
10240         else
10241                 EM_DEBUG_LOG(">>>> No Need to create Directory");
10242
10243         return result;
10244 }
10245
10246 INTERNAL_FUNC int emstorage_get_save_name(int account_id, int mail_id, int atch_id, char *fname, char *name_buf, int *err_code)
10247 {
10248         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);
10249         EM_PROFILE_BEGIN(profile_emstorage_get_save_name);
10250
10251         int ret = false;
10252         int error = EMAIL_ERROR_NONE;
10253         char *dir_name = NULL;
10254         char create_dir[1024]={0};
10255         char *temp_file = NULL;
10256
10257         if (!name_buf || account_id < FIRST_ACCOUNT_ID || mail_id < 0 || atch_id < 0)  {
10258                 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);
10259                 error = EMAIL_ERROR_INVALID_PARAM;
10260                 goto FINISH_OFF;
10261         }
10262
10263         sprintf(name_buf, "%s", MAILHOME);
10264         sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, account_id);
10265
10266         if (mail_id > 0)
10267                 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, mail_id);
10268
10269         if (atch_id > 0)
10270                 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, atch_id);
10271
10272         if (fname) {
10273                 temp_file = EM_SAFE_STRDUP(fname);
10274                 if (strstr(temp_file, "/")) {
10275                         dir_name = emstorage_make_directory_path_from_file_path(temp_file);
10276                 }
10277         }
10278
10279         if (dir_name) {
10280                 sprintf(create_dir, "%s%s%s", name_buf, DIR_SEPERATOR, dir_name);
10281                 EM_DEBUG_LOG(">>>>> DIR PATH [ %s ]", create_dir);
10282                 mkdir(create_dir, DIRECTORY_PERMISSION);
10283                 EM_SAFE_FREE(temp_file);
10284         }
10285
10286         if (fname) {
10287                 EM_DEBUG_LOG(">>>>> fname [ %s ]", fname);
10288                 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%s", DIR_SEPERATOR, fname);
10289         }
10290
10291         EM_DEBUG_LOG(">>>>> name_buf [ %s ]", name_buf);
10292
10293         ret = true;
10294
10295 FINISH_OFF:
10296         EM_SAFE_FREE(temp_file);
10297
10298         if (err_code != NULL)
10299                 *err_code = error;
10300
10301         EM_PROFILE_END(profile_emstorage_get_save_name);
10302         EM_DEBUG_FUNC_END("ret [%d]", ret);
10303         return ret;
10304 }
10305
10306 INTERNAL_FUNC int emstorage_get_dele_name(int account_id, int mail_id, int atch_id, char *fname, char *name_buf, int *err_code)
10307 {
10308         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);
10309
10310         if (!name_buf || account_id < FIRST_ACCOUNT_ID)  {
10311                 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);
10312                 if (err_code != NULL)
10313                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10314                 return false;
10315         }
10316
10317         sprintf(name_buf+EM_SAFE_STRLEN(name_buf),      "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
10318
10319         if (mail_id > 0)
10320                 sprintf(name_buf+EM_SAFE_STRLEN(name_buf),      "%s%d", DIR_SEPERATOR, mail_id);
10321         else
10322                 goto FINISH_OFF;
10323
10324         if (atch_id > 0)
10325                 sprintf(name_buf+EM_SAFE_STRLEN(name_buf),      "%s%d", DIR_SEPERATOR, atch_id);
10326         else
10327                 goto FINISH_OFF;
10328
10329 FINISH_OFF:
10330         sprintf(name_buf+EM_SAFE_STRLEN(name_buf),      ".DELE");
10331
10332         EM_DEBUG_FUNC_END();
10333         return true;
10334 }
10335
10336 INTERNAL_FUNC int emstorage_create_dir(int account_id, int mail_id, int atch_id, int *err_code)
10337 {
10338         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], err_code[%p]", account_id, mail_id, atch_id, err_code);
10339         EM_PROFILE_BEGIN(profile_emcore_save_create_dir);
10340         int ret = false;
10341         int error = EMAIL_ERROR_NONE;
10342
10343         char buf[512];
10344         struct stat sbuf;
10345         if (account_id >= FIRST_ACCOUNT_ID)  {
10346                 SNPRINTF(buf, sizeof(buf), "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
10347
10348                 if (stat(buf, &sbuf) == 0) {
10349                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR)  {
10350                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
10351                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10352                                 goto FINISH_OFF;
10353                         }
10354                 }
10355                 else  {
10356                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
10357                                 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
10358                                 EM_DEBUG_EXCEPTION("mkdir failed l(Errno=%d)][ErrStr=%s]", errno, strerror(errno));
10359                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10360                                 goto FINISH_OFF;
10361                         }
10362                 }
10363         }
10364
10365         if (mail_id > 0)  {
10366                 if (account_id < FIRST_ACCOUNT_ID)  {
10367                         EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
10368                         error = EMAIL_ERROR_INVALID_PARAM;
10369                         goto FINISH_OFF;
10370                 }
10371
10372                 SNPRINTF(buf+EM_SAFE_STRLEN(buf), sizeof(buf), "%s%d", DIR_SEPERATOR, mail_id);
10373
10374                 if (stat(buf, &sbuf) == 0) {
10375                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR)  {
10376                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
10377
10378                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10379                                 goto FINISH_OFF;
10380                         }
10381                 }
10382                 else  {
10383                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
10384                                 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
10385                                 EM_DEBUG_EXCEPTION("mkdir failed l (Errno=%d)][ErrStr=%s]", errno, strerror(errno));
10386                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10387                                 goto FINISH_OFF;
10388                         }
10389                 }
10390         }
10391
10392         if (atch_id > 0)  {
10393                 if (account_id < FIRST_ACCOUNT_ID || mail_id <= 0)  {
10394                         EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
10395
10396                         error = EMAIL_ERROR_INVALID_PARAM;
10397                         goto FINISH_OFF;
10398                 }
10399
10400                 SNPRINTF(buf+EM_SAFE_STRLEN(buf), sizeof(buf)-(EM_SAFE_STRLEN(buf)+1), "%s%d", DIR_SEPERATOR, atch_id);
10401
10402                 if (stat(buf, &sbuf) == 0) {
10403                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR)  {
10404                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
10405
10406                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10407                                 goto FINISH_OFF;
10408                         }
10409                 }
10410                 else  {
10411                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
10412                                 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
10413                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10414                                 goto FINISH_OFF;
10415                         }
10416                 }
10417         }
10418
10419         ret = true;
10420
10421 FINISH_OFF:
10422         if (err_code != NULL)
10423                 *err_code = error;
10424
10425         EM_PROFILE_END(profile_emcore_save_create_dir);
10426         EM_DEBUG_FUNC_END("ret [%d]", ret);
10427         return ret;
10428 }
10429
10430 INTERNAL_FUNC int emstorage_copy_file(char *src_file, char *dst_file, int sync_status, int *err_code)
10431 {
10432         EM_DEBUG_FUNC_BEGIN("src_file[%s], dst_file[%s], err_code[%p]", src_file, dst_file, err_code);
10433         EM_DEBUG_LOG("Using the fsync function");
10434         int ret = false;
10435         int error = EMAIL_ERROR_NONE;
10436         struct stat st_buf;
10437
10438         int fp_src = 0;
10439         int fp_dst = 0;
10440         int nread = 0;
10441         int nwritten = 0;
10442         char *buf =  NULL;
10443         int buf_size = 0;
10444
10445         if (!src_file || !dst_file)  {
10446                 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
10447
10448                 error = EMAIL_ERROR_INVALID_PARAM;
10449                 goto FINISH_OFF;
10450         }
10451
10452         if (stat(src_file, &st_buf) < 0) {
10453                 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", src_file);
10454
10455                 error = EMAIL_ERROR_SYSTEM_FAILURE;             /* EMAIL_ERROR_INVALID_PATH; */
10456                 goto FINISH_OFF;
10457         }
10458
10459         buf_size =  st_buf.st_size;
10460         EM_DEBUG_LOG(">>>> File Size [ %d ]", buf_size);
10461         buf = (char *)calloc(1, buf_size+1);
10462
10463         if (!buf) {
10464                 EM_DEBUG_EXCEPTION(">>> Memory cannot be allocated");
10465                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10466                 goto FINISH_OFF;
10467         }
10468
10469         if (buf) {
10470                 if ((fp_src = open(src_file, O_RDONLY))<0) { /*prevent 24474*/
10471                         EM_DEBUG_EXCEPTION(">>>> Source Fail open %s Failed [ %d ] - Error [ %s ]", src_file, errno, strerror(errno));
10472                         error = EMAIL_ERROR_SYSTEM_FAILURE;
10473                         goto FINISH_OFF;
10474                 }
10475
10476                 if ((fp_dst = open(dst_file, O_CREAT | O_WRONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH))<0) { /*prevent 24474*/
10477                         EM_DEBUG_EXCEPTION(">>>> Destination Fail open %s Failed [ %d ] - Error [ %s ]", dst_file, errno, strerror(errno));
10478                         error = EMAIL_ERROR_SYSTEM_FAILURE;
10479                         goto FINISH_OFF;
10480                 }
10481
10482                 while ((nread = read(fp_src, buf, buf_size)) > 0) {
10483                         if (nread > 0 && nread <= buf_size)  {
10484                                 EM_DEBUG_LOG("Nread Value [%d]", nread);
10485                                 if ((nwritten = write(fp_dst, buf, nread)) != nread) {
10486                                         EM_DEBUG_EXCEPTION("fwrite failed...[%d] : [%s]", errno, strerror(errno));
10487                                         error = EMAIL_ERROR_UNKNOWN;
10488                                         goto FINISH_OFF;
10489                                 }
10490                                 EM_DEBUG_LOG("NWRITTEN [%d]", nwritten);
10491                         }
10492                 }
10493         }
10494
10495         ret = true;
10496
10497 FINISH_OFF:
10498         if (fp_src>0) /*prevent 24474*/
10499                 close(fp_src);
10500
10501         if (fp_dst>0) { /*prevent 24474*/
10502                 if (sync_status) {
10503                         EM_DEBUG_LOG("Before fsync");
10504                         fsync(fp_dst);
10505                 }
10506                 close(fp_dst);
10507         }
10508         EM_SAFE_FREE(buf);
10509         if (nread < 0 || error == EMAIL_ERROR_UNKNOWN)
10510                 remove(dst_file);
10511
10512         if (err_code != NULL)
10513                 *err_code = error;
10514         EM_DEBUG_FUNC_END("ret [%d]", ret);
10515         return ret;
10516 }
10517 /* create Directory if user has deleted [deepam.p@samsung.com] */
10518 INTERNAL_FUNC void emstorage_create_dir_if_delete()
10519 {
10520         EM_DEBUG_FUNC_BEGIN();
10521
10522         mkdir(EMAILPATH, DIRECTORY_PERMISSION);
10523         mkdir(DATA_PATH, DIRECTORY_PERMISSION);
10524         mkdir(MAILHOME, DIRECTORY_PERMISSION);
10525         mkdir(MAILTEMP, DIRECTORY_PERMISSION);
10526
10527         EM_DEBUG_FUNC_END();
10528 }
10529 static int _get_temp_file_name(char **filename, int *err_code)
10530 {
10531         EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
10532
10533         int ret = false;
10534         int error = EMAIL_ERROR_NONE;
10535
10536         if (filename == NULL) {
10537                 EM_DEBUG_EXCEPTION(" filename[%p]", filename);
10538                 error = EMAIL_ERROR_INVALID_PARAM;
10539                 goto FINISH_OFF;
10540         }
10541
10542         char tempname[512] = {0x00, };
10543         struct timeval tv;
10544
10545         gettimeofday(&tv, NULL);
10546         srand(tv.tv_usec);
10547
10548         SNPRINTF(tempname, sizeof(tempname), "%s%c%d", MAILTEMP, '/', rand());
10549
10550         char *p = EM_SAFE_STRDUP(tempname);
10551         if (p == NULL)  {
10552                 EM_DEBUG_EXCEPTION(" strdup failed...");
10553                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10554                 goto FINISH_OFF;
10555         }
10556
10557         *filename = p;
10558
10559         ret = true;
10560
10561 FINISH_OFF:
10562         if (err_code != NULL)
10563                 *err_code = error;
10564
10565         EM_DEBUG_FUNC_END("ret [%d]", ret);
10566         return ret;
10567 }
10568
10569 INTERNAL_FUNC int emstorage_add_content_type(char *file_path, char *char_set, int *err_code)
10570 {
10571         EM_DEBUG_FUNC_BEGIN("File path [ %p ]  Character Set [ %p ] err_code [ %p]", file_path, char_set, err_code);
10572
10573         EM_IF_NULL_RETURN_VALUE(file_path, false);
10574         EM_IF_NULL_RETURN_VALUE(char_set, false);
10575         EM_IF_NULL_RETURN_VALUE(err_code, false);
10576
10577         char *buf =  NULL;
10578         char *buf1 = NULL;
10579         struct stat st_buf;
10580         int buf_size = 0;
10581         char *low_char_set = NULL;
10582         char *match_str = NULL;
10583         int nwritten = 0;
10584         int ret = false;
10585         int error = EMAIL_ERROR_NONE;
10586         int data_count_to_written = 0;
10587         char *temp_file_name = NULL;
10588         int err = 0;
10589
10590         FILE* fp_src = NULL;
10591         FILE* fp_dest = NULL;
10592         int nread = 0;
10593
10594
10595         if (stat(file_path, &st_buf) < 0) {
10596                 EM_DEBUG_EXCEPTION(" stat(\"%s\") failed...", file_path);
10597
10598                 error = EMAIL_ERROR_SYSTEM_FAILURE;             /* EMAIL_ERROR_INVALID_PATH; */
10599                 goto FINISH_OFF;
10600         }
10601
10602         buf_size =  st_buf.st_size;
10603
10604         EM_DEBUG_LOG(">>>> File Size [ %d ] ", buf_size);
10605
10606         buf = (char *)calloc(1, buf_size+1);
10607
10608         if (!buf) {
10609                 EM_DEBUG_LOG(">>> Memory cannot be allocated ");
10610                 goto FINISH_OFF;
10611         }
10612
10613         if (!(fp_src = fopen(file_path, "rb"))) {
10614                 EM_DEBUG_EXCEPTION(" file_path fopen failed - %s", file_path);
10615
10616                         error = EMAIL_ERROR_SYSTEM_FAILURE;
10617                         goto FINISH_OFF;
10618                 }
10619
10620                 if ((nread = fread(buf, 1, buf_size, fp_src)) > 0) {
10621                         if (nread > 0 && nread <= buf_size)  {
10622                                 EM_DEBUG_LOG(">>>> Nread Value [ %d ] ", nread);
10623
10624                                 /**
10625                                   *   1.Add check for whether content type is there.
10626                                   *   2. If not based on the character set, Append it in File
10627                                   **/
10628
10629                                 low_char_set = calloc(1, EM_SAFE_STRLEN(char_set) + strlen(" \" /></head>") +1); /*prevent 34359*/
10630
10631                                 strncat(low_char_set, char_set, EM_SAFE_STRLEN(char_set));
10632
10633                                 EM_DEBUG_LOG(">>>> CHAR SET [ %s ] ", low_char_set);
10634
10635                                 strncat(low_char_set, " \" /></head>", strlen(" \" /></head>")); /*prevent 34359*/
10636
10637                                 EM_DEBUG_LOG(">>> CHARSET [ %s ] ", low_char_set);
10638
10639                                 EM_DEBUG_LOG(">>>>emstorage_add_content_type 1 ");
10640
10641                                 match_str = strstr(buf, CONTENT_TYPE_DATA);
10642                                 EM_DEBUG_LOG(">>>>emstorage_add_content_type 2 ");
10643
10644                                 if (match_str == NULL) {
10645                                         EM_DEBUG_LOG(">>>>emstorage_add_content_type 3 ");
10646                                         if (fp_src !=NULL) {
10647                                                 fclose(fp_src);fp_src = NULL;
10648                                         }
10649                                 data_count_to_written = EM_SAFE_STRLEN(low_char_set)+strlen(CONTENT_DATA)+1; /*prevent 34359*/
10650                                         EM_DEBUG_LOG(">>>>emstorage_add_content_type 4 ");
10651                                 buf1 = (char *)calloc(1, data_count_to_written);
10652                                         EM_DEBUG_LOG(">>>>emstorage_add_content_type 5 ");
10653
10654                                         if (buf1) {
10655                                                 EM_DEBUG_LOG(">>>>emstorage_add_content_type 6 ");
10656                                                 strncat(buf1, CONTENT_DATA, strlen(CONTENT_DATA)); /*prevent 34359*/
10657
10658                                                 EM_DEBUG_LOG(">>>>> BUF 1 [ %s ] ", buf1);
10659
10660                                                 strncat(buf1, low_char_set, EM_SAFE_STRLEN(low_char_set));
10661
10662                                                 EM_DEBUG_LOG(">>>> HTML TAG DATA  [ %s ] ", buf1);
10663
10664
10665                                         /* 1. Create a temporary file name */
10666                                         if (!_get_temp_file_name(&temp_file_name, &err)) {
10667                                                         EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed - %d", err);
10668                                                         if (err_code != NULL) *err_code = err;
10669                                                         EM_SAFE_FREE(temp_file_name);
10670                                                         goto FINISH_OFF;
10671                                         }
10672                                         EM_DEBUG_LOG(">>>>>>> TEMP APPEND FILE PATH [ %s ] ", temp_file_name);
10673
10674                                         /* Open the Temp file in Append mode */
10675                                         if (!(fp_dest = fopen(temp_file_name, "ab"))) {
10676                                                 EM_DEBUG_EXCEPTION(" fopen failed - %s", temp_file_name);
10677                                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10678                                                 goto FINISH_OFF;
10679                                         }
10680
10681                                         /* 2. write the Latest data */
10682                                         nwritten = fwrite(buf1, data_count_to_written-1, 1, fp_dest);
10683
10684                                         if (nwritten > 0) {
10685                                                 EM_DEBUG_LOG(" Latest Data  : [%d ] bytes written ", nwritten);
10686                                                 nwritten = 0;
10687                                                 /* 3. Append old data */
10688                                                 nwritten = fwrite(buf, nread-1, 1, fp_dest);
10689
10690                                                 if (nwritten <= 0) {
10691                                                         EM_DEBUG_EXCEPTION(" Error Occured while writing Old data : [%d ] bytes written ", nwritten);
10692                                                         error = EMAIL_ERROR_SYSTEM_FAILURE;
10693                                                         goto FINISH_OFF;
10694                                                 }
10695                                                 else {
10696                                                         EM_DEBUG_LOG(">>>> OLD data appended [ %d ] ", nwritten);
10697
10698                                                         if (!emstorage_move_file(temp_file_name, file_path, false, &err)) {
10699                                                                 EM_DEBUG_EXCEPTION(" emstorage_move_file failed - %d", err);
10700                                                                 goto FINISH_OFF;
10701                                                         }
10702                                                 }
10703
10704                                         }
10705                                         else {
10706                                                 EM_DEBUG_EXCEPTION(" Error Occured while writing New data : [%d ] bytes written ", nwritten);
10707                                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10708                                                 goto FINISH_OFF;
10709                                         }
10710
10711                                         }
10712
10713                                 }
10714                                 EM_DEBUG_LOG(">>>>emstorage_add_content_type 15 ");
10715
10716
10717                         }
10718                 }
10719
10720         ret = true;
10721 FINISH_OFF:
10722
10723         EM_SAFE_FREE(buf);
10724         EM_SAFE_FREE(buf1);
10725         EM_SAFE_FREE(low_char_set);
10726
10727         if (fp_src != NULL) {
10728                 fclose(fp_src);
10729                 fp_src = NULL;
10730         }
10731
10732         if (fp_dest != NULL) {
10733                 fclose(fp_dest);
10734                 fp_dest = NULL;
10735         }
10736         EM_DEBUG_FUNC_END("ret [%d]", ret);
10737         return ret;
10738
10739 }
10740
10741 INTERNAL_FUNC int emstorage_move_file(char *src_file, char *dst_file, int sync_status, int *err_code)
10742 {
10743         EM_DEBUG_FUNC_BEGIN("src_file[%p], dst_file[%p], err_code[%p]", src_file, dst_file, err_code);
10744
10745         int ret = false;
10746         int error = EMAIL_ERROR_NONE;
10747
10748         if (src_file == NULL || dst_file == NULL)  {
10749                 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
10750                 error = EMAIL_ERROR_INVALID_PARAM;
10751                 goto FINISH_OFF;
10752         }
10753
10754         EM_DEBUG_LOG("src_file[%s], dst_file[%s]", src_file, dst_file);
10755
10756         if (strcmp(src_file, dst_file) != 0) {
10757                 if (rename(src_file, dst_file) != 0) {
10758                         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() */
10759                                 /*  does not work across different mount points, even if the same file system is mounted on both.)       */
10760                                 EM_DEBUG_LOG("oldpath and newpath are not on the same mounted file system.");
10761                                 if (!emstorage_copy_file(src_file, dst_file, sync_status, &error)) {
10762                                         EM_DEBUG_EXCEPTION("emstorage_copy_file failed - %d", error);
10763                                         goto FINISH_OFF;
10764                                 }
10765                                 remove(src_file);
10766                                 EM_DEBUG_LOG("src[%s] removed", src_file);
10767
10768                         }
10769                         else  {
10770                                 if (errno == ENOENT)  {
10771                                         struct stat temp_file_stat;
10772                                         if (stat(src_file, &temp_file_stat) < 0)
10773                                                 EM_DEBUG_EXCEPTION("no src file found [%s]", src_file);
10774                                         if (stat(dst_file, &temp_file_stat) < 0)
10775                                                 EM_DEBUG_EXCEPTION("no dst file found [%s]", src_file);
10776
10777                                         EM_DEBUG_EXCEPTION("no file found [%d]", errno);
10778                                         error = EMAIL_ERROR_FILE_NOT_FOUND;
10779                                         goto FINISH_OFF;
10780
10781                                 }
10782                                 else  {
10783                                         EM_DEBUG_EXCEPTION("rename failed [%d]", errno);
10784                                         error = EMAIL_ERROR_SYSTEM_FAILURE;
10785                                         goto FINISH_OFF;
10786                                 }
10787                         }
10788                 }
10789         }
10790         else {
10791                 EM_DEBUG_LOG("src[%s] = dst[%d]", src_file, dst_file);
10792         }
10793
10794         ret = true;
10795
10796 FINISH_OFF:
10797         if (err_code != NULL)
10798                 *err_code = error;
10799
10800         EM_DEBUG_FUNC_END("ret [%d]", ret);
10801         return ret;
10802 }
10803
10804 INTERNAL_FUNC int emstorage_delete_file(char *src_file, int *err_code)
10805 {
10806         EM_DEBUG_FUNC_BEGIN("src_file[%p], err_code[%p]", src_file, err_code);
10807
10808         int ret = false;
10809         int error = EMAIL_ERROR_NONE;
10810
10811         if (src_file == NULL) {
10812                 EM_DEBUG_EXCEPTION(" src_file[%p]", src_file);
10813
10814                 error = EMAIL_ERROR_INVALID_PARAM;
10815                 goto FINISH_OFF;
10816         }
10817
10818         if (remove(src_file) != 0) {
10819                 if (errno != ENOENT) {
10820                         EM_DEBUG_EXCEPTION(" remove failed - %d", errno);
10821
10822                         error = EMAIL_ERROR_SYSTEM_FAILURE;
10823                         goto FINISH_OFF;
10824                 }
10825                 else {
10826                         EM_DEBUG_EXCEPTION(" no file found...");
10827
10828                         error = EMAIL_ERROR_FILE_NOT_FOUND;
10829                 }
10830         }
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
10843 INTERNAL_FUNC int emstorage_delete_dir(char *src_dir, int *err_code)
10844 {
10845         EM_DEBUG_FUNC_BEGIN("src_dir[%p], err_code[%p]", src_dir, err_code);
10846
10847         if (src_dir == NULL) {
10848                 EM_DEBUG_EXCEPTION("src_dir[%p]", src_dir);
10849
10850                 if (err_code != NULL)
10851                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10852                 return false;
10853         }
10854
10855         int error = EMAIL_ERROR_NONE;
10856
10857         DIR *dirp;
10858         struct dirent *dp;
10859         struct stat sbuf;
10860         char buf[512];
10861
10862         dirp = opendir(src_dir);
10863
10864         if (dirp == NULL)  {
10865                 if (errno == ENOENT)  {
10866                         EM_DEBUG_EXCEPTION("directory[%s] does not exist...", src_dir);
10867                         if (err_code != NULL)
10868                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
10869                         return true;
10870                 }
10871                 else  {
10872                         EM_DEBUG_EXCEPTION("opendir failed - %d", errno);
10873                         if (err_code != NULL)
10874                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
10875                         return false;
10876                 }
10877         }
10878
10879         while ((dp=readdir(dirp)))  {
10880                 if (strncmp(dp->d_name, ".", 1) == 0 || strncmp(dp->d_name, "..", 2) == 0) /* prevent 34360 */
10881                         continue;
10882
10883                 SNPRINTF(buf, sizeof(buf), "%s/%s", src_dir, dp->d_name);
10884
10885                 if (lstat(buf, &sbuf) == 0 || stat(buf, &sbuf) == 0) {
10886                         /*  check directory */
10887                         if ((sbuf.st_mode & S_IFMT) == S_IFDIR)  {      /*  directory */
10888                                 /*  recursive call */
10889                                 if (!emstorage_delete_dir(buf, &error)) {
10890                                         closedir(dirp);
10891                                         if (err_code != NULL)
10892                                                 *err_code = error;
10893                                         return false;
10894                                 }
10895                         }
10896                         else  { /*  file */
10897                                 if (remove(buf) < 0)  {
10898                                         EM_DEBUG_EXCEPTION("remove failed - %s", buf);
10899                                         closedir(dirp);
10900                                         if (err_code != NULL)
10901                                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
10902                                         return false;
10903                                 }
10904                         }
10905                 }
10906                 else
10907                         EM_DEBUG_EXCEPTION("content does not exist...");
10908         }
10909
10910         closedir(dirp);
10911
10912         EM_DEBUG_LOG("remove direcotory [%s]", src_dir);
10913
10914         /* EM_DEBUG_FUNC_BEGIN(); */
10915
10916         if (remove(src_dir) < 0)  {
10917                 EM_DEBUG_EXCEPTION("remove failed [%s]", src_dir);
10918                 if (err_code != NULL)
10919                         *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
10920                 return false;
10921         }
10922
10923         if (err_code != NULL)
10924                 *err_code = error;
10925
10926         return true;
10927 }
10928
10929 /* faizan.h@samsung.com */
10930 INTERNAL_FUNC int emstorage_update_server_uid(char *old_server_uid, char *new_server_uid, int *err_code)
10931 {
10932         EM_DEBUG_FUNC_BEGIN("new_server_uid[%s], old_server_uid[%s]", new_server_uid, old_server_uid);
10933         int rc, ret = false;
10934         int error = EMAIL_ERROR_NONE;
10935         char sql_query_string[QUERY_SIZE] = {0, };
10936
10937         int transaction = true;
10938
10939         if (!old_server_uid || !new_server_uid) {
10940                 EM_DEBUG_EXCEPTION("Invalid parameters");
10941                 error = EMAIL_ERROR_INVALID_PARAM;
10942                 return false;
10943         }
10944         sqlite3 *local_db_handle = emstorage_get_db_connection();
10945
10946         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10947
10948         SNPRINTF(sql_query_string, sizeof(sql_query_string),
10949                  "UPDATE mail_tbl SET server_mail_id=\'%s\' WHERE server_mail_id=%s ", new_server_uid, old_server_uid);
10950         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10951
10952         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
10953                 ("sqlite3_exec fail:%d", rc));
10954         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10955                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10956
10957         ret = true;
10958
10959 FINISH_OFF:
10960         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10961         _DISCONNECT_DB;
10962
10963         if (err_code != NULL)
10964                 *err_code = error;
10965
10966         EM_DEBUG_FUNC_END("ret [%d]", ret);
10967         return ret;
10968
10969 }
10970
10971 INTERNAL_FUNC int emstorage_update_read_mail_uid(int mail_id, char *new_server_uid, char *mbox_name, int *err_code)
10972 {
10973         EM_DEBUG_FUNC_BEGIN("mail_id[%d], new_server_uid[%s], mbox_name[%s]", mail_id, new_server_uid, mbox_name);
10974
10975         int rc, ret = false;
10976         int error = EMAIL_ERROR_NONE;
10977         char sql_query_string[QUERY_SIZE] = {0, };
10978
10979         int transaction = true;
10980
10981         if (!mail_id || !new_server_uid || !mbox_name)  {
10982                 EM_DEBUG_EXCEPTION("Invalid parameters");
10983                 error = EMAIL_ERROR_INVALID_PARAM;
10984                 return false;
10985         }
10986         sqlite3 *local_db_handle = emstorage_get_db_connection();
10987         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10988
10989
10990         SNPRINTF(sql_query_string, sizeof(sql_query_string),
10991                  "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);
10992         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10993         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10994                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10995         ret     = true;
10996
10997 FINISH_OFF:
10998
10999         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
11000         _DISCONNECT_DB;
11001
11002         if (err_code != NULL)
11003                 *err_code = error;
11004
11005         EM_DEBUG_FUNC_END("ret [%d]", ret);
11006         return ret;
11007
11008 }
11009
11010
11011 int emstorage_get_latest_unread_mailid(int account_id, int *mail_id, int *err_code)
11012 {
11013         EM_DEBUG_FUNC_BEGIN();
11014
11015         if ((!mail_id) ||(account_id <= 0 &&  account_id != -1)) {
11016                 EM_DEBUG_EXCEPTION(" mail_id[%p], account_id[%d] ", mail_id, account_id);
11017                 if (err_code != NULL)
11018                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11019                 return false;
11020         }
11021
11022         int ret = false;
11023         int rc = -1;
11024         int error = EMAIL_ERROR_NONE;
11025         DB_STMT hStmt = NULL;
11026         int count = 0;
11027         int mailid = 0;
11028         int transaction = false;
11029         char sql_query_string[QUERY_SIZE] = {0, };
11030
11031         sqlite3 *local_db_handle = emstorage_get_db_connection();
11032         EMSTORAGE_START_READ_TRANSACTION(transaction);
11033
11034         memset(sql_query_string, 0x00, sizeof(sql_query_string));
11035
11036         if (account_id == -1)
11037                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl WHERE flags_seen_field = 0 ORDER BY mail_id DESC");
11038         else
11039                 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);
11040
11041         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11042         EM_DEBUG_LOG("  sqlite3_prepare hStmt = %p", hStmt);
11043         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11044                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11045
11046         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11047         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11048                 ("sqlite3_step fail:%d", rc));
11049
11050         char **result;
11051
11052         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
11053         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11054                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11055
11056         sqlite3_free_table(result);
11057         if (count == 0)  {
11058                 EM_DEBUG_EXCEPTION("no Mails found...");
11059                 ret = false;
11060                 error= EMAIL_ERROR_MAIL_NOT_FOUND;
11061                 goto FINISH_OFF;
11062         }
11063
11064         _get_stmt_field_data_int(hStmt, &mailid, 0);
11065         EM_DEBUG_LOG("mailid [%d]", mailid);
11066
11067         ret = true;
11068
11069 FINISH_OFF:
11070         if (hStmt != NULL)  {
11071                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
11072
11073                 rc = sqlite3_finalize(hStmt);
11074                 if (rc != SQLITE_OK)  {
11075                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
11076
11077                         error = EMAIL_ERROR_DB_FAILURE;
11078                 }
11079         }
11080         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
11081         _DISCONNECT_DB;
11082
11083         if (mail_id != NULL)
11084                 *mail_id = mailid;
11085
11086         if (err_code != NULL)
11087                 *err_code = error;
11088
11089         EM_DEBUG_FUNC_END("ret [%d]", ret);
11090         return ret;
11091 }
11092
11093 int setting_system_command(const char *command)
11094
11095  {
11096         int pid = 0, status = 0;
11097         char *const environ[] = { NULL };
11098
11099         if (command == 0)
11100                 return 1;
11101
11102         pid = fork();
11103
11104         if (pid == -1)
11105                 return -1;
11106
11107         if (pid == 0) {
11108                 char *argv[4];
11109
11110                 argv[0] = "sh";
11111                 argv[1] = "-c";
11112                 argv[2] = (char *)command;
11113                 argv[3] = 0;
11114
11115                 execve("/bin/sh", argv, environ);
11116                 abort();
11117         }
11118         do{
11119                 if (waitpid(pid, &status, 0) == -1) {
11120                         if (errno != EINTR)
11121                                 return -1;
11122                 }
11123                 else {
11124                         return status;
11125                 }
11126         } while (1);
11127
11128         return 0;
11129 }
11130
11131
11132 INTERNAL_FUNC int emstorage_mail_get_total_diskspace_usage(unsigned long *total_usage,  int transaction, int *err_code)
11133 {
11134         EM_DEBUG_FUNC_BEGIN("total_usage[%p],  transaction[%d], err_code[%p]", total_usage, transaction, err_code);
11135
11136         if (!total_usage) {
11137                 EM_DEBUG_EXCEPTION("total_usage[%p]", total_usage);
11138
11139                 if (err_code != NULL)
11140                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11141                 return false;
11142         }
11143
11144         int   ret = false;
11145         int   error = EMAIL_ERROR_NONE;
11146         char  syscmd[256] = {0, };
11147         char  line[256] = {0, };
11148         char *line_from_file = NULL;
11149         FILE *fp = NULL;
11150         unsigned long total_diskusage = 0;
11151
11152         SNPRINTF(syscmd, sizeof(syscmd), "touch %s", SETTING_MEMORY_TEMP_FILE_PATH);
11153         if (setting_system_command(syscmd) == -1) {
11154                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
11155
11156                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11157                 goto FINISH_OFF;
11158         }
11159
11160         SNPRINTF(syscmd, sizeof(syscmd), "du -hsk %s > %s", EMAILPATH, SETTING_MEMORY_TEMP_FILE_PATH);
11161         EM_DEBUG_LOG(" cmd : %s", syscmd);
11162         if (setting_system_command(syscmd) == -1) {
11163                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : Setting > Memory] System Command [%s] is failed", syscmd);
11164
11165                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11166                 goto FINISH_OFF;
11167         }
11168
11169         fp = fopen(SETTING_MEMORY_TEMP_FILE_PATH, "r");
11170         if (fp == NULL) {
11171                 perror(SETTING_MEMORY_TEMP_FILE_PATH);
11172
11173                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11174                 goto FINISH_OFF;
11175         }
11176
11177         line_from_file = fgets(line, sizeof(line), fp);
11178
11179         if(line_from_file == NULL) {
11180                 EM_DEBUG_EXCEPTION("fgets failed");
11181                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11182                 goto FINISH_OFF;
11183         }
11184         total_diskusage = strtoul(line, NULL, 10);
11185
11186         memset(syscmd, 0, sizeof(syscmd));
11187         SNPRINTF(syscmd, sizeof(syscmd), "rm -f %s", SETTING_MEMORY_TEMP_FILE_PATH);
11188         if (setting_system_command(syscmd) == -1) {
11189                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage :  [Setting > Memory] System Command [%s] is failed", syscmd);
11190                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11191                 goto FINISH_OFF;
11192         }
11193
11194         EM_DEBUG_LOG("[Setting > Memory] @@@@@ Size of Directory [%s] is %ld KB", EMAILPATH, total_diskusage);
11195
11196         ret = true;
11197
11198 FINISH_OFF:
11199         if (err_code != NULL)
11200                 *err_code = error;
11201
11202         if (ret)
11203                 *total_usage = total_diskusage;
11204         else
11205                 *total_usage = 0;
11206
11207         if(fp) fclose(fp); /* prevent 32730 */
11208
11209         EM_DEBUG_FUNC_END("ret [%d]", ret);
11210         return ret;
11211 }
11212
11213
11214 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)
11215 {
11216         DB_STMT hStmt = NULL;
11217         int ret = false;
11218         int error = EMAIL_ERROR_NONE;
11219         int rc = 0;
11220         char sql_query_string[QUERY_SIZE] = {0, };
11221
11222         SNPRINTF(sql_query_string, sizeof(sql_query_string),
11223                 "INSERT INTO mail_tbl VALUES "
11224                 "( ?" /*  mail_id */
11225                 ", ?" /*  account_id */
11226                 ", ?" /*  mailbox_name */
11227                 ", ?" /*  mail_size */
11228                 ", ?" /*  server_mail_status */
11229                 ", ?" /*  server_mailbox_name */
11230                 ", ?" /*  server_mail_id */
11231                 ", ?" /*  reference_mail_id */
11232                 ", ?" /*  full_address_from */
11233                 ", ?" /*  full_address_reply */
11234                 ", ?" /*  full_address_to */
11235                 ", ?" /*  full_address_cc */
11236                 ", ?" /*  full_address_bcc */
11237                 ", ?" /*  full_address_return */
11238                 ", ?" /*  subject */
11239                 ", ?" /*  body_download_status */
11240                 ", ?" /*  file_path_plain */
11241                 ", ?" /*  file_path_html */
11242                 ", ?" /*  date_time */
11243                 ", ?" /*  flags_seen_field */
11244                 ", ?" /*  flags_deleted_field */
11245                 ", ?" /*  flags_flagged_field */
11246                 ", ?" /*  flags_answered_field */
11247                 ", ?" /*  flags_recent_field */
11248                 ", ?" /*  flags_draft_field */
11249                 ", ?" /*  flags_forwarded_field */
11250                 ", ?" /*  DRM_status */
11251                 ", ?" /*  priority */
11252                 ", ?" /*  save_status */
11253                 ", ?" /*  lock_status */
11254                 ", ?" /*  message_id */
11255                 ", ?" /*  report_status */
11256                 ", ?" /*  email_address_sender */
11257                 ", ?" /*  email_address_recipient */
11258                 ", ?" /*  attachment_count */
11259                 ", ?" /*  inline_content_count */
11260                 ", ?" /*  preview_text */
11261                 ", ?" /*  thread_id */
11262                 ", ?" /*  mailbox_type */
11263                 ", ?" /*  alias_sender */
11264                 ", ?" /*  alias_recipient */
11265                 ", ?" /*  thread_item_count */
11266                 ", ?" /*  meeting_request_status */
11267                 ", ?" /*  message_class */
11268                 ", ?" /*  digest_type */
11269                 ", ?" /*  smime_type */
11270                 ")");
11271
11272         int transaction = true;
11273         sqlite3 *local_db_handle = emstorage_get_db_connection();
11274
11275         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
11276
11277
11278         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11279         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11280                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11281
11282         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_id);
11283         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, account_id);
11284         _bind_stmt_field_data_string(hStmt, MAILBOX_NAME_IDX_IN_MAIL_TBL, "OUTBOX", 0, MAILBOX_LEN_IN_MAIL_TBL);
11285         _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, EMAIL_MAILBOX_TYPE_OUTBOX);
11286         _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, "save test - long", 1, SUBJECT_LEN_IN_MAIL_TBL);
11287         _bind_stmt_field_data_string(hStmt, DATETIME_IDX_IN_MAIL_TBL, "20100316052908", 0, DATETIME_LEN_IN_MAIL_TBL);
11288         _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, 0);
11289         _bind_stmt_field_data_string(hStmt, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL, "", 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
11290         _bind_stmt_field_data_string(hStmt, SERVER_MAIL_ID_IDX_IN_MAIL_TBL, "", 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
11291         _bind_stmt_field_data_string(hStmt, MESSAGE_ID_IDX_IN_MAIL_TBL, "", 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
11292         _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, 0);
11293         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL, "<test08@streaming.s3glab.net>", 1, FROM_LEN_IN_MAIL_TBL);
11294         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL, "", 1, REPLY_TO_LEN_IN_MAIL_TBL);
11295         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL, full_address_to, 1, TO_LEN_IN_MAIL_TBL);
11296         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL, full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
11297         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL, full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
11298         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL, "", 1, RETURN_PATH_LEN_IN_MAIL_TBL);
11299         _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);
11300         _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);
11301         _bind_stmt_field_data_string(hStmt, ALIAS_SENDER_IDX_IN_MAIL_TBL, "send_alias", 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
11302         _bind_stmt_field_data_string(hStmt, ALIAS_RECIPIENT_IDX_IN_MAIL_TBL, "recipient_alias", 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
11303         _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, 1);
11304         _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);
11305         _bind_stmt_field_data_string(hStmt, FILE_PATH_HTML_IDX_IN_MAIL_TBL, "", 0, TEXT_2_LEN_IN_MAIL_TBL);
11306         _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, 4);
11307         _bind_stmt_field_data_char(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, 0);
11308         _bind_stmt_field_data_char(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, 0);
11309         _bind_stmt_field_data_char(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, 0);
11310         _bind_stmt_field_data_char(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, 0);
11311         _bind_stmt_field_data_char(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, 0);
11312         _bind_stmt_field_data_char(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, 0);
11313         _bind_stmt_field_data_char(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, 0);
11314         _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, 0);
11315         _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, 0);
11316         _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, 0);
11317         _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, 0);
11318         _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, 0);
11319         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
11320         _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, 0);
11321         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
11322         _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, 0);
11323         _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, 0);
11324         _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, "preview body", 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
11325         _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, 0);
11326         _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, 0);
11327         _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, 0);
11328         _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, 0);
11329
11330         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11331         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
11332                 ("sqlite3_step fail:%d", rc));
11333         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11334                 ("sqlite3_step fail:%d", rc));
11335         ret = true;
11336
11337 FINISH_OFF:
11338         if (hStmt != NULL)  {
11339                 rc = sqlite3_finalize(hStmt);
11340                 if (rc != SQLITE_OK)  {
11341                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
11342                         error = EMAIL_ERROR_DB_FAILURE;
11343                 }
11344         }
11345
11346         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
11347         _DISCONNECT_DB;
11348
11349         if (err_code != NULL)
11350                 *err_code = error;
11351
11352         EM_DEBUG_FUNC_END("ret [%d]", ret);
11353         return ret;
11354 }
11355
11356
11357 INTERNAL_FUNC int emstorage_get_max_mail_count()
11358 {
11359         return EMAIL_MAIL_MAX_COUNT;
11360 }
11361
11362 #define STRIPPED_SUBJECT_BUFFER_SIZE 4086
11363
11364 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)
11365 {
11366         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);
11367         EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
11368         int      rc = 0, query_size = 0, query_size_account = 0;
11369         int      account_id = 0;
11370         int      err_code = EMAIL_ERROR_NONE;
11371         int      count = 0, result_thread_id = -1, latest_mail_id_in_thread = -1;
11372         time_t   latest_date_time = 0;
11373         time_t   date_time = 0;
11374         char    *mailbox_name = NULL, *subject = NULL;
11375         char    *sql_query_string = NULL, *sql_account = NULL;
11376         char    *sql_format = "SELECT thread_id, date_time, mail_id FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
11377         char    *sql_format_account = " AND account_id = %d ";
11378         char    *sql_format_order_by = " ORDER BY date_time DESC ";
11379         char   **result = NULL;
11380         char     stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
11381         sqlite3 *local_db_handle = emstorage_get_db_connection();
11382
11383         EM_IF_NULL_RETURN_VALUE(mail_tbl, EMAIL_ERROR_INVALID_PARAM);
11384         EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
11385         EM_IF_NULL_RETURN_VALUE(result_latest_mail_id_in_thread, EMAIL_ERROR_INVALID_PARAM);
11386         EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
11387
11388         account_id   = mail_tbl->account_id;
11389         mailbox_name = mail_tbl->mailbox_name;
11390         subject      = mail_tbl->subject;
11391         date_time    = mail_tbl->date_time;
11392
11393         EM_DEBUG_LOG("subject : %s", subject);
11394
11395         if (em_find_pos_stripped_subject_for_thread_view(subject, stripped_subject, STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE)  {
11396                 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view  is failed");
11397                 err_code =  EMAIL_ERROR_UNKNOWN;
11398                 result_thread_id = -1;
11399                 goto FINISH_OFF;
11400         }
11401
11402         if (EM_SAFE_STRLEN(stripped_subject) < 2) {
11403                 result_thread_id = -1;
11404                 goto FINISH_OFF;
11405         }
11406
11407         EM_DEBUG_LOG("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s", EM_SAFE_STRLEN(stripped_subject), stripped_subject);
11408
11409         if (account_id > 0)     {
11410                 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
11411                 sql_account = malloc(query_size_account);
11412                 if (sql_account == NULL) {
11413                         EM_DEBUG_EXCEPTION("malloc for sql_account  is failed %d", query_size_account);
11414                         err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
11415                         goto FINISH_OFF;
11416                 }
11417                 snprintf(sql_account, query_size_account, sql_format_account, account_id);
11418         }
11419
11420         /* prevent 34362 */
11421         query_size = strlen(sql_format) + strlen(stripped_subject) + 50 + query_size_account + strlen(sql_format_order_by); /*  + query_size_mailbox; */
11422
11423         sql_query_string = malloc(query_size);
11424
11425         if (sql_query_string == NULL) {
11426                 EM_DEBUG_EXCEPTION("malloc for sql  is failed %d", query_size);
11427                 err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
11428                 goto FINISH_OFF;
11429         }
11430
11431         sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mail_tbl->mailbox_id);
11432
11433         if (account_id > 0)
11434                 strcat(sql_query_string, sql_account);
11435
11436         strcat(sql_query_string, sql_format_order_by);
11437         strcat(sql_query_string, ";");
11438
11439         EM_DEBUG_LOG("Query : %s", sql_query_string);
11440
11441         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
11442
11443         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11444                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11445
11446         EM_DEBUG_LOG("Result rows count : %d", count);
11447
11448         if (count == 0)
11449                 result_thread_id = -1;
11450         else {
11451                 _get_table_field_data_int   (result, &result_thread_id, 3);
11452                 _get_table_field_data_time_t(result, &latest_date_time, 4);
11453                 _get_table_field_data_int   (result, &latest_mail_id_in_thread, 5);
11454
11455                 if (latest_date_time < mail_tbl->date_time)
11456                         *result_latest_mail_id_in_thread = latest_mail_id_in_thread;
11457                 else
11458                         *result_latest_mail_id_in_thread = mail_tbl->mail_id;
11459                 EM_DEBUG_LOG("latest_mail_id_in_thread [%d], mail_id [%d]", latest_mail_id_in_thread, mail_tbl->mail_id);
11460         }
11461
11462 FINISH_OFF:
11463         *thread_id = result_thread_id;
11464         *thread_item_count = count;
11465
11466         EM_DEBUG_LOG("Result thread id : %d", *thread_id);
11467         EM_DEBUG_LOG("Result count : %d", *thread_item_count);
11468         EM_DEBUG_LOG("err_code : %d", err_code);
11469
11470         EM_SAFE_FREE(sql_account);
11471         EM_SAFE_FREE(sql_query_string);
11472
11473         sqlite3_free_table(result);
11474
11475         EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
11476
11477         return err_code;
11478 }
11479
11480
11481 INTERNAL_FUNC int emstorage_get_thread_information(int thread_id, emstorage_mail_tbl_t** mail_tbl, int transaction, int *err_code)
11482 {
11483         EM_DEBUG_FUNC_BEGIN();
11484
11485         int count = 0, ret = false;
11486         int error = EMAIL_ERROR_NONE;
11487         emstorage_mail_tbl_t *p_data_tbl = NULL;
11488         char conditional_clause[QUERY_SIZE] = {0, };
11489
11490         EM_IF_NULL_RETURN_VALUE(mail_tbl, false);
11491
11492         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE thread_id = %d AND thread_item_count > 0", thread_id);
11493         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
11494
11495         if(!emstorage_query_mail_tbl(conditional_clause, transaction, &p_data_tbl, &count, &error)) {
11496                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
11497                 goto FINISH_OFF;
11498         }
11499
11500         if(p_data_tbl)
11501                 EM_DEBUG_LOG("thread_id : %d, thread_item_count : %d", p_data_tbl[0].thread_id, p_data_tbl[0].thread_item_count);
11502
11503         ret = true;
11504
11505 FINISH_OFF:
11506         if (ret == true)
11507                 *mail_tbl = p_data_tbl;
11508         else if (p_data_tbl != NULL)
11509                 emstorage_free_mail(&p_data_tbl, 1, NULL);
11510
11511         if (err_code != NULL)
11512                 *err_code = error;
11513
11514         EM_DEBUG_FUNC_END("ret [%d]", ret);
11515         return ret;
11516 }
11517
11518
11519 INTERNAL_FUNC int emstorage_get_sender_list(int account_id, const char *mailbox_name, int search_type, const char *search_value, email_sort_type_t sorting, email_sender_list_t** sender_list, int *sender_count,  int *err_code)
11520 {
11521         EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_name [%p], search_type [%d], search_value [%p], sorting [%d], sender_list[%p], sender_count[%p] err_code[%p]"
11522                 , account_id , mailbox_name , search_type , search_value , sorting , sender_list, sender_count, err_code);
11523
11524         if ((!sender_list) ||(!sender_count)) {
11525                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
11526                 if (err_code != NULL)
11527                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11528                 return false;
11529         }
11530
11531         int rc = -1, ret = false;
11532         int error = EMAIL_ERROR_NONE;
11533         int count = 0;
11534         int i, col_index = 0;
11535         int read_count = 0;
11536         email_sender_list_t *p_sender_list = NULL;
11537         char sql_query_string[QUERY_SIZE] = {0, };
11538         char **result = NULL;
11539         sqlite3 *local_db_handle = emstorage_get_db_connection();
11540
11541         SNPRINTF(sql_query_string, sizeof(sql_query_string),
11542                 "SELECT email_address_sender, alias_sender, COUNT(email_address_sender), SUM(flags_seen_field = 1) "
11543                 "FROM mail_tbl ");
11544
11545         /*  mailbox_name */
11546         if (mailbox_name)
11547                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE UPPER(mailbox_name) = UPPER('%s') ", mailbox_name);
11548         else    /*  NULL  means all mailbox_name. but except for trash(3), spambox(5), all emails(for GMail, 7) */
11549                 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) ");
11550
11551         /*  account id */
11552         /*  '0' (ALL_ACCOUNT) means all account */
11553         if (account_id > ALL_ACCOUNT)
11554                 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);
11555
11556         if (search_value) {
11557                 switch (search_type) {
11558                         case EMAIL_SEARCH_FILTER_SUBJECT:
11559                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
11560                                         " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\')) ", search_value);
11561                                 break;
11562                         case EMAIL_SEARCH_FILTER_SENDER:
11563                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
11564                                         " AND  ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
11565                                         ") ", search_value);
11566                                 break;
11567                         case EMAIL_SEARCH_FILTER_RECIPIENT:
11568                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
11569                                         " AND ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
11570                                         "       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
11571                                         "       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
11572                                         ") ", search_value, search_value, search_value);
11573                                 break;
11574                         case EMAIL_SEARCH_FILTER_ALL:
11575                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
11576                                         " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') "
11577                                         "       OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
11578                                         "                       OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
11579                                         "                       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
11580                                         "                       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
11581                                         "               ) "
11582                                         "       )"
11583                                         ")", search_value, search_value, search_value, search_value, search_value);
11584                                 break;
11585                 }
11586         }
11587
11588
11589         /*  sorting option is not available now. The order of sender list is ascending order by display name */
11590         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
11591                 "GROUP BY email_address_sender "
11592                 "ORDER BY UPPER(alias_sender) ");
11593
11594         EM_DEBUG_LOG("query[%s]", sql_query_string);
11595
11596         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
11597         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11598                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11599
11600         EM_DEBUG_LOG("Count of Sender [%d]", count);
11601
11602         if (!(p_sender_list = (email_sender_list_t*)em_malloc(sizeof(email_sender_list_t) * count))) {
11603                 EM_DEBUG_EXCEPTION("em_malloc failed...");
11604                 error = EMAIL_ERROR_OUT_OF_MEMORY;
11605                 goto FINISH_OFF;
11606         }
11607
11608         col_index = 4;
11609
11610         EM_DEBUG_LOG(">>>> DATA ASSIGN START >>");
11611         for (i = 0; i < count; i++)  {
11612                 _get_table_field_data_string(result, &(p_sender_list[i].address), 1, col_index++);
11613                 _get_table_field_data_string(result, &(p_sender_list[i].display_name), 1, col_index++);
11614                 _get_table_field_data_int(result, &(p_sender_list[i].total_count), col_index++);
11615                 _get_table_field_data_int(result, &(read_count), col_index++);
11616                 p_sender_list[i].unread_count = p_sender_list[i].total_count - read_count;              /*  unread count = total - read          */
11617         }
11618         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >>", count);
11619
11620         sqlite3_free_table(result);
11621         result = NULL;
11622
11623         ret = true;
11624
11625 FINISH_OFF:
11626         if (ret == true)  {
11627                 *sender_list = p_sender_list;
11628                 *sender_count = count;
11629                 EM_DEBUG_LOG(">>>> COUNT : %d >>", count);
11630         }
11631
11632         _DISCONNECT_DB;
11633
11634         if (err_code != NULL)
11635                 *err_code = error;
11636
11637         EM_DEBUG_FUNC_END("ret [%d]", ret);
11638         return ret;
11639 }
11640
11641 INTERNAL_FUNC int emstorage_free_sender_list(email_sender_list_t **sender_list, int count)
11642 {
11643         EM_DEBUG_FUNC_BEGIN("sender_list[%p], count[%d]", sender_list, count);
11644
11645         int err = EMAIL_ERROR_NONE;
11646
11647         if (count > 0)  {
11648                 if (!sender_list || !*sender_list)  {
11649                         EM_DEBUG_EXCEPTION("sender_list[%p], count[%d]", sender_list, count);
11650                         err = EMAIL_ERROR_INVALID_PARAM;
11651                         return err;
11652                 }
11653
11654                 email_sender_list_t* p = *sender_list;
11655                 int i = 0;
11656
11657                 for (; i < count; i++)  {
11658                         EM_SAFE_FREE(p[i].address);
11659                         EM_SAFE_FREE(p[i].display_name);
11660                 }
11661
11662                 EM_SAFE_FREE(p);
11663                 *sender_list = NULL;
11664         }
11665
11666         return err;
11667 }
11668
11669
11670 INTERNAL_FUNC int emstorage_free_address_info_list(email_address_info_list_t **address_info_list)
11671 {
11672         EM_DEBUG_FUNC_BEGIN("address_info_list[%p]", address_info_list);
11673
11674         int err = EMAIL_ERROR_NONE;
11675         email_address_info_t *p_address_info = NULL;
11676         GList *list = NULL;
11677         GList *node = NULL;
11678         int i = 0;
11679
11680         if (!address_info_list || !*address_info_list)  {
11681                 EM_DEBUG_EXCEPTION("address_info_list[%p]", address_info_list);
11682                 err = EMAIL_ERROR_INVALID_PARAM;
11683                 return err;
11684         }
11685
11686         /*  delete GLists */
11687         for (i = EMAIL_ADDRESS_TYPE_FROM; i <= EMAIL_ADDRESS_TYPE_BCC; i++) {
11688                 switch (i) {
11689                         case EMAIL_ADDRESS_TYPE_FROM:
11690                                 list = (*address_info_list)->from;
11691                                 break;
11692                         case EMAIL_ADDRESS_TYPE_TO:
11693                                 list = (*address_info_list)->to;
11694                                 break;
11695                         case EMAIL_ADDRESS_TYPE_CC:
11696                                 list = (*address_info_list)->cc;
11697                                 break;
11698                         case EMAIL_ADDRESS_TYPE_BCC:
11699                                 list = (*address_info_list)->bcc;
11700                                 break;
11701                 }
11702
11703                 /*  delete dynamic-allocated memory for each item */
11704                 node = g_list_first(list);
11705                 while (node != NULL) {
11706                         p_address_info = (email_address_info_t*)node->data;
11707                         EM_SAFE_FREE(p_address_info->address);
11708                         EM_SAFE_FREE(p_address_info->display_name);
11709                         EM_SAFE_FREE(node->data);
11710
11711                         node = g_list_next(node);
11712                 }
11713         }
11714
11715         EM_SAFE_FREE(*address_info_list);
11716         *address_info_list = NULL;
11717
11718         EM_DEBUG_FUNC_END("err [%d]", err);
11719         return err;
11720 }
11721 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
11722
11723 INTERNAL_FUNC int emstorage_add_pbd_activity(email_event_partial_body_thd* local_activity, int *activity_id, int transaction, int *err_code)
11724 {
11725         EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
11726
11727         if (!local_activity || !activity_id) {
11728                 EM_DEBUG_EXCEPTION("local_activity[%p], transaction[%d], activity_id[%p], err_code[%p]", local_activity, activity_id, transaction, err_code);
11729                 if (err_code != NULL)
11730                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11731                 return false;
11732         }
11733
11734         int rc = -1;
11735         int ret = false;
11736         int error = EMAIL_ERROR_NONE;
11737         int i = 0;
11738
11739         char sql_query_string[QUERY_SIZE] = {0, };
11740         DB_STMT hStmt = NULL;
11741         sqlite3 *local_db_handle = emstorage_get_db_connection();
11742         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
11743         memset(sql_query_string, 0x00, sizeof(sql_query_string));
11744         SNPRINTF(sql_query_string, sizeof(sql_query_string),
11745                 "INSERT INTO mail_partial_body_activity_tbl VALUES "
11746                 "( "
11747                 "? "  /* Account ID */
11748                 ",?"  /* Local Mail ID */
11749                 ",?"  /* Server mail ID */
11750                 ",?"  /* Activity ID */
11751                 ",?"  /* Activity type*/
11752                 ",?"  /* Mailbox ID*/
11753                 ",?"  /* Mailbox name*/
11754                 ") ");
11755
11756         char *sql = "SELECT max(rowid) FROM mail_partial_body_activity_tbl;";
11757         char **result = NULL;
11758
11759
11760         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
11761
11762         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11763                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
11764
11765         if (NULL==result[1]) rc = 1;
11766         else rc = atoi(result[1])+1;
11767         sqlite3_free_table(result);
11768         result = NULL;
11769
11770         *activity_id = local_activity->activity_id = rc;
11771
11772         EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ], MAIL ID [ %d ], ACTIVITY TYPE [ %d ], SERVER MAIL ID [ %lu ]", \
11773                 local_activity->activity_id, local_activity->mail_id, local_activity->activity_type, local_activity->server_mail_id);
11774
11775         if (local_activity->mailbox_id)
11776                 EM_DEBUG_LOG(" MAILBOX ID [ %d ]", local_activity->mailbox_id);
11777
11778
11779         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11780         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11781                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11782
11783         EM_DEBUG_LOG(">>>> SQL STMT [ %s ]", sql_query_string);
11784
11785         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
11786         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
11787         _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
11788         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
11789         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
11790         _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
11791         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->mailbox_name, 0, 3999);
11792
11793
11794         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11795
11796         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
11797                 ("sqlite3_step fail:%d", rc));
11798         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11799                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
11800
11801         ret = true;
11802
11803 FINISH_OFF:
11804
11805         if (hStmt != NULL) {
11806                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
11807
11808                 rc = sqlite3_finalize(hStmt);
11809                 hStmt = NULL;
11810                 if (rc != SQLITE_OK) {
11811                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
11812                         error = EMAIL_ERROR_DB_FAILURE;
11813                 }
11814         }
11815
11816          EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
11817         _DISCONNECT_DB;
11818          if (err_code != NULL)
11819                  *err_code = error;
11820
11821          EM_DEBUG_FUNC_END("ret [%d]", ret);
11822         return ret;
11823 }
11824
11825
11826 INTERNAL_FUNC int emstorage_get_pbd_mailbox_list(int account_id, int **mailbox_list, int *count, int transaction, int *err_code)
11827 {
11828         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
11829
11830         if (account_id < FIRST_ACCOUNT_ID || NULL == &mailbox_list || NULL == count) {
11831                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
11832                 if (err_code != NULL)
11833                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11834                 return false;
11835         }
11836
11837         int ret = false;
11838         int error = EMAIL_ERROR_NONE;
11839         char **result;
11840         int i = 0, rc = -1;
11841         int *mbox_list = NULL;
11842         DB_STMT hStmt = NULL;
11843         char sql_query_string[QUERY_SIZE] = {0, };
11844
11845         sqlite3 *local_db_handle = emstorage_get_db_connection();
11846
11847         EMSTORAGE_START_READ_TRANSACTION(transaction);
11848
11849         memset(sql_query_string, 0x00, sizeof(sql_query_string));
11850         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);
11851
11852
11853         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
11854         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11855                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11856
11857         *count = atoi(result[1]);
11858         sqlite3_free_table(result);
11859
11860         if (!*count) {
11861                 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
11862                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
11863                 ret = true;
11864                 goto FINISH_OFF;
11865         }
11866         EM_DEBUG_LOG("Mailbox count = %d", *count);
11867
11868         memset(sql_query_string, 0x00, sizeof(sql_query_string));
11869
11870         /* 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); */
11871         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);
11872
11873         EM_DEBUG_LOG(" Query [%s]", sql_query_string);
11874
11875
11876         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11877
11878         EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
11879         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11880                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11881
11882
11883         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11884         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11885                 ("sqlite3_step fail:%d", rc));
11886
11887         if (NULL == (mbox_list = (int *)em_malloc(sizeof(int *) * (*count)))) {
11888                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
11889                 error = EMAIL_ERROR_OUT_OF_MEMORY;
11890                 goto FINISH_OFF;
11891         }
11892
11893         memset(mbox_list, 0x00, sizeof(int) * (*count));
11894
11895         for (i = 0; i < (*count); i++) {
11896                 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
11897                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11898                 /* EM_DEBUG_LOG("In emstorage_get_pdb_mailbox_list() loop, After sqlite3_step(), , i = %d, rc = %d.", i,  rc); */
11899                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11900                         ("sqlite3_step fail:%d", rc));
11901                 EM_DEBUG_LOG("mbox_list %d", mbox_list + i);
11902         }
11903
11904         ret = true;
11905
11906 FINISH_OFF:
11907         if (ret == true)
11908                 *mailbox_list = mbox_list;
11909         else
11910                 EM_SAFE_FREE(mbox_list);
11911
11912         if (hStmt != NULL) {
11913                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
11914
11915                 rc = sqlite3_finalize(hStmt);
11916                 hStmt = NULL;
11917                 if (rc != SQLITE_OK) {
11918                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
11919                         error = EMAIL_ERROR_DB_FAILURE;
11920                 }
11921         }
11922
11923         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
11924         _DISCONNECT_DB;
11925         if (err_code != NULL)
11926                 *err_code = error;
11927         EM_DEBUG_FUNC_END("ret [%d]", ret);
11928         return ret;
11929 }
11930
11931
11932 INTERNAL_FUNC int emstorage_get_pbd_account_list(int **account_list, int *count, int transaction, int *err_code)
11933 {
11934         EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
11935
11936         if (NULL == &account_list || NULL == count) {
11937                 EM_DEBUG_EXCEPTION("mailbox_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
11938                 if (err_code != NULL)
11939                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11940                 return false;
11941         }
11942
11943         int ret = false;
11944         int error = EMAIL_ERROR_NONE;
11945         char *sql;
11946         char **result;
11947         int i = 0, rc = -1;
11948         int *result_account_list = NULL;
11949         DB_STMT hStmt = NULL;
11950         char sql_query_string[QUERY_SIZE] = {0, };
11951
11952         sqlite3 *local_db_handle = emstorage_get_db_connection();
11953
11954         EMSTORAGE_START_READ_TRANSACTION(transaction);
11955
11956
11957         sql = "SELECT count(distinct account_id) FROM mail_partial_body_activity_tbl";
11958
11959
11960         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
11961         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11962                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
11963
11964         *count = atoi(result[1]);
11965         sqlite3_free_table(result);
11966
11967         if (!*count) {
11968                 EM_DEBUG_EXCEPTION("no account found...");
11969                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
11970                 ret = true;
11971                 goto FINISH_OFF;
11972         }
11973
11974         EM_DEBUG_LOG("Account count [%d]", *count);
11975
11976         memset(sql_query_string, 0x00, sizeof(sql_query_string));
11977
11978         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_partial_body_activity_tbl");
11979
11980         EM_DEBUG_LOG("Query [%s]", sql_query_string);
11981
11982
11983         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11984
11985         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
11986         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11987                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11988
11989
11990         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11991         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11992                 ("sqlite3_step fail:%d", rc));
11993
11994         if (NULL == (result_account_list = (int *)em_malloc(sizeof(int) * (*count)))) {
11995                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
11996                 error = EMAIL_ERROR_OUT_OF_MEMORY;
11997                 goto FINISH_OFF;
11998         }
11999
12000         memset(result_account_list, 0x00, sizeof(int) * (*count));
12001
12002         for (i = 0; i < (*count); i++) {
12003                 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
12004
12005                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12006                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12007                         ("sqlite3_step fail:%d", rc));
12008                 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
12009         }
12010
12011         ret = true;
12012
12013 FINISH_OFF:
12014         if (ret == true)
12015                 *account_list = result_account_list;
12016         else
12017                 EM_SAFE_FREE(result_account_list);
12018
12019         if (hStmt != NULL) {
12020                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12021
12022                 rc = sqlite3_finalize(hStmt);
12023                 hStmt = NULL;
12024                 if (rc != SQLITE_OK) {
12025                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
12026                         error = EMAIL_ERROR_DB_FAILURE;
12027                 }
12028         }
12029
12030         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12031         _DISCONNECT_DB;
12032         if (err_code != NULL)
12033                 *err_code = error;
12034         EM_DEBUG_FUNC_END("ret [%d]", ret);
12035         return ret;
12036 }
12037
12038
12039
12040 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)
12041 {
12042         EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
12043
12044         if (account_id < FIRST_ACCOUNT_ID || NULL == event_start || 0 == input_mailbox_id || NULL == count) {
12045                 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);
12046
12047                 if (err_code != NULL)
12048                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12049                 return false;
12050         }
12051
12052         int rc = -1;
12053         int ret = false;
12054         char **result;
12055         int error = EMAIL_ERROR_NONE;
12056         int i = 0;
12057         DB_STMT hStmt = NULL;
12058         email_event_partial_body_thd* event_list = NULL;
12059         char sql_query_string[QUERY_SIZE] = {0, };
12060
12061         sqlite3 *local_db_handle = emstorage_get_db_connection();
12062
12063         EMSTORAGE_START_READ_TRANSACTION(transaction);
12064
12065         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12066         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);
12067
12068
12069         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
12070         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12071                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12072
12073         *count = atoi(result[1]);
12074         sqlite3_free_table(result);
12075
12076         EM_DEBUG_LOG("Query = [%s]", sql_query_string);
12077
12078         if (!*count) {
12079                 EM_DEBUG_EXCEPTION("No matched activity found in mail_partial_body_activity_tbl");
12080
12081                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
12082                 ret = true;
12083                 goto FINISH_OFF;
12084         }
12085         EM_DEBUG_LOG("Activity Count = %d", *count);
12086
12087         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12088         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);
12089
12090         EM_DEBUG_LOG("Query [%s]", sql_query_string);
12091
12092
12093         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12094
12095         EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
12096         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12097                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12098
12099
12100         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12101         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12102                 ("sqlite3_step fail:%d", rc));
12103
12104         if (!(event_list = (email_event_partial_body_thd*)em_malloc(sizeof(email_event_partial_body_thd) * (*count)))) {
12105                 EM_DEBUG_EXCEPTION("Malloc failed");
12106
12107                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12108                 goto FINISH_OFF;
12109         }
12110         memset(event_list, 0x00, sizeof(email_event_partial_body_thd) * (*count));
12111
12112         for (i=0; i < (*count); i++) {
12113                 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_IDX_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12114                 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12115                 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12116                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12117                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_type), ACTIVITY_TYPE_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12118                 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12119                 _get_stmt_field_data_string(hStmt, &(event_list[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
12120
12121                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12122                 /* EM_DEBUG_LOG("In emstorage_get_pbd_activity_data() loop, After sqlite3_step(), , i = %d, rc = %d.", i,  rc); */
12123                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12124                         ("sqlite3_step fail:%d", rc));
12125
12126                 event_list[i].event_type = 0;
12127         }
12128
12129         ret = true;
12130
12131 FINISH_OFF:
12132         if (true == ret)
12133           *event_start = event_list;
12134         else {
12135                 EM_SAFE_FREE(event_list);
12136         }
12137
12138         if (hStmt != NULL) {
12139                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12140
12141                 rc = sqlite3_finalize(hStmt);
12142                 hStmt = NULL;
12143                 if (rc != SQLITE_OK) {
12144                         EM_DEBUG_LOG("sqlite3_finalize failed - %d", rc);
12145                         error = EMAIL_ERROR_DB_FAILURE;
12146                 }
12147         }
12148
12149         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12150         _DISCONNECT_DB;
12151         if (err_code != NULL)
12152                 *err_code = error;
12153
12154         EM_DEBUG_FUNC_END("ret [%d]", ret);
12155         return ret;
12156 }
12157
12158
12159
12160 INTERNAL_FUNC int emstorage_delete_pbd_activity(int account_id, int mail_id, int activity_id, int transaction, int *err_code)
12161 {
12162         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);
12163
12164
12165         if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
12166                 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);
12167
12168                 if (err_code != NULL)
12169                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12170                 return false;
12171         }
12172
12173         int rc = -1;
12174         int ret = false;
12175         int error = EMAIL_ERROR_NONE;
12176         char sql_query_string[QUERY_SIZE] = {0, };
12177
12178         sqlite3 *local_db_handle = emstorage_get_db_connection();
12179
12180         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12181         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12182
12183         if (activity_id == 0)
12184                 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);
12185         else
12186                 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);
12187
12188         EM_DEBUG_LOG("Query [%s]", sql_query_string);
12189
12190         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12191         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12192                 ("sqlite3_exec fail:%d", rc));
12193         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12194                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12195
12196         /*  validate activity existence */
12197         rc = sqlite3_changes(local_db_handle);
12198         if (rc == 0)  {
12199                 EM_DEBUG_EXCEPTION("No matching activity found");
12200                 error = EMAIL_ERROR_DATA_NOT_FOUND;
12201                 ret = true;
12202                 goto FINISH_OFF;
12203         }
12204
12205         ret = true;
12206
12207 FINISH_OFF:
12208
12209         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12210
12211         _DISCONNECT_DB;
12212         if (err_code != NULL)
12213                 *err_code = error;
12214
12215         EM_DEBUG_FUNC_END("ret [%d]", ret);
12216         return ret;
12217 }
12218
12219 INTERNAL_FUNC int emstorage_get_mailbox_pbd_activity_count(int account_id, int input_mailbox_id, int *activity_count, int transaction, int *err_code)
12220 {
12221         EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
12222
12223         if (account_id < FIRST_ACCOUNT_ID || NULL == activity_count || NULL == err_code) {
12224                 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
12225                 if (err_code != NULL)
12226                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12227                 return false;
12228         }
12229         int rc = -1;
12230         int ret = false;
12231         int error = EMAIL_ERROR_NONE;
12232         char sql_query_string[QUERY_SIZE] = {0, };
12233
12234         DB_STMT hStmt = NULL;
12235
12236         sqlite3 *local_db_handle = emstorage_get_db_connection();
12237
12238         EMSTORAGE_START_READ_TRANSACTION(transaction);
12239         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12240
12241         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);
12242
12243         EM_DEBUG_LOG(" Query [%s]", sql_query_string);
12244
12245
12246         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12247         EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
12248         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12249                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12250
12251
12252         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12253         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12254                 ("sqlite3_step fail:%d", rc));
12255
12256         _get_stmt_field_data_int(hStmt, activity_count, 0);
12257
12258         EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
12259
12260         ret = true;
12261
12262 FINISH_OFF:
12263
12264         if (hStmt != NULL) {
12265                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12266                 rc = sqlite3_finalize(hStmt);
12267                 hStmt=NULL;
12268                 if (rc != SQLITE_OK) {
12269                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
12270                         error = EMAIL_ERROR_DB_FAILURE;
12271                 }
12272                 EM_DEBUG_LOG("sqlite3_finalize- %d", rc);
12273         }
12274
12275         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12276         _DISCONNECT_DB;
12277
12278         if (err_code != NULL)
12279                 *err_code = error;
12280
12281         EM_DEBUG_FUNC_END("ret [%d]", ret);
12282         return ret;
12283 }
12284
12285
12286 INTERNAL_FUNC int emstorage_get_pbd_activity_count(int *activity_count, int transaction, int *err_code)
12287 {
12288         EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
12289
12290         if (NULL == activity_count || NULL == err_code) {
12291                 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
12292                 if (err_code != NULL)
12293                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12294                 return false;
12295         }
12296         int rc = -1;
12297         int ret = false;
12298         int error = EMAIL_ERROR_NONE;
12299         DB_STMT hStmt = NULL;
12300         char sql_query_string[QUERY_SIZE] = {0, };
12301
12302         sqlite3 *local_db_handle = emstorage_get_db_connection();
12303
12304         EMSTORAGE_START_READ_TRANSACTION(transaction);
12305         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12306
12307         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl;");
12308
12309         EM_DEBUG_LOG(" Query [%s]", sql_query_string);
12310
12311
12312         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12313         EM_DEBUG_LOG("  before sqlite3_prepare hStmt = %p", hStmt);
12314         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12315                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12316
12317
12318         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12319         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12320                 ("sqlite3_step fail:%d", rc));
12321
12322         _get_stmt_field_data_int(hStmt, activity_count, 0);
12323
12324         EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
12325
12326         ret = true;
12327
12328 FINISH_OFF:
12329
12330
12331         if (hStmt != NULL) {
12332                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12333
12334                 rc = sqlite3_finalize(hStmt);
12335                 hStmt=NULL;
12336                 if (rc != SQLITE_OK) {
12337                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
12338                         error = EMAIL_ERROR_DB_FAILURE;
12339                 }
12340                 EM_DEBUG_LOG("sqlite3_finalize- %d", rc);
12341         }
12342
12343         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12344         _DISCONNECT_DB;
12345         if (err_code != NULL)
12346                 *err_code = error;
12347
12348         EM_DEBUG_FUNC_END("ret [%d]", ret);
12349         return ret;
12350 }
12351
12352 INTERNAL_FUNC int emstorage_delete_full_pbd_activity_data(int account_id, int transaction, int *err_code)
12353 {
12354         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
12355         if (account_id < FIRST_ACCOUNT_ID) {
12356                 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
12357                 if (err_code != NULL)
12358                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12359                 return false;
12360         }
12361
12362         int rc = -1;
12363         int ret = false;
12364         int error = EMAIL_ERROR_NONE;
12365         char sql_query_string[QUERY_SIZE] = {0, };
12366
12367         sqlite3 *local_db_handle = emstorage_get_db_connection();
12368         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12369         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12370         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d", account_id);
12371
12372         EM_DEBUG_LOG("Query [%s]", sql_query_string);
12373
12374         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12375         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12376                 ("sqlite3_exec fail:%d", rc));
12377         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12378                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12379
12380
12381         rc = sqlite3_changes(local_db_handle);
12382         if (rc == 0) {
12383                 EM_DEBUG_EXCEPTION("No matching activities found in mail_partial_body_activity_tbl");
12384                 error = EMAIL_ERROR_DATA_NOT_FOUND;
12385                 ret = true;
12386                 goto FINISH_OFF;
12387         }
12388
12389         ret = true;
12390
12391 FINISH_OFF:
12392
12393         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12394         _DISCONNECT_DB;
12395         if (err_code != NULL)
12396                 *err_code = error;
12397
12398         EM_DEBUG_FUNC_END("ret [%d]", ret);
12399         return ret;
12400 }
12401
12402 /*Himanshu[h.gahlaut]-> Added below API to update mail_partial_body_activity_tbl
12403 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*/
12404
12405 INTERNAL_FUNC int emstorage_update_pbd_activity(char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code)
12406 {
12407         EM_DEBUG_FUNC_BEGIN("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
12408
12409         int rc = -1, ret = false;
12410         int error = EMAIL_ERROR_NONE;
12411         char sql_query_string[QUERY_SIZE] = {0, };
12412
12413         int transaction = true;
12414
12415         if (!old_server_uid || !new_server_uid || !mbox_name)  {
12416                 EM_DEBUG_EXCEPTION("Invalid parameters");
12417                 error = EMAIL_ERROR_INVALID_PARAM;
12418                 return false;
12419         }
12420
12421         sqlite3 *local_db_handle = emstorage_get_db_connection();
12422         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12423         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12424         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12425                  "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);
12426
12427         EM_DEBUG_LOG("Query [%s]", sql_query_string);
12428
12429         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12430         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12431                 ("sqlite3_exec fail:%d", rc));
12432         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12433                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12434
12435         rc = sqlite3_changes(local_db_handle);
12436         if (rc == 0) {
12437                 EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
12438                 error = EMAIL_ERROR_DATA_NOT_FOUND;
12439                 goto FINISH_OFF;
12440         }
12441
12442         ret = true;
12443
12444 FINISH_OFF:
12445
12446         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12447         _DISCONNECT_DB;
12448         if (err_code != NULL)
12449                 *err_code = error;
12450
12451         EM_DEBUG_FUNC_END("ret [%d]", ret);
12452         return ret;
12453 }
12454
12455
12456 INTERNAL_FUNC int emstorage_create_file(char *data_string, size_t file_size, char *dst_file_name, int *err_code)
12457 {
12458         EM_DEBUG_FUNC_BEGIN("file_size[%d] , dst_file_name[%s], err_code[%p]", file_size, dst_file_name, err_code);
12459
12460         int ret = false;
12461         int error = EMAIL_ERROR_NONE;
12462
12463         FILE* fp_dst = NULL;
12464
12465         if (!data_string || !dst_file_name)  {
12466                 EM_DEBUG_EXCEPTION("data_string[%p], dst_file_name[%p]", data_string, dst_file_name);
12467                 error = EMAIL_ERROR_INVALID_PARAM;
12468                 goto FINISH_OFF;
12469         }
12470
12471         fp_dst = fopen(dst_file_name, "w");
12472
12473         if (!fp_dst)  {
12474                 EM_DEBUG_EXCEPTION("fopen failed - %s", dst_file_name);
12475                 if (errno == 28)
12476                         error = EMAIL_ERROR_MAIL_MEMORY_FULL;
12477                 else
12478                         error = EMAIL_ERROR_SYSTEM_FAILURE;
12479                 goto FINISH_OFF;
12480         }
12481
12482         if (fwrite(data_string, 1, file_size, fp_dst) == 0) {
12483                 EM_DEBUG_EXCEPTION("fwrite failed...");
12484                 error = EMAIL_ERROR_UNKNOWN;
12485                 goto FINISH_OFF;
12486         }
12487
12488         ret = true;
12489
12490 FINISH_OFF:
12491
12492         if (fp_dst != NULL)
12493                 fclose(fp_dst);
12494
12495         if (err_code != NULL)
12496                 *err_code = error;
12497
12498         EM_DEBUG_FUNC_END("ret [%d]", ret);
12499         return ret;
12500 }
12501
12502 #endif /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
12503
12504
12505
12506 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
12507
12508 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)
12509 {
12510         EM_DEBUG_FUNC_BEGIN();
12511         int rc = -1;
12512         int ret = false;
12513         int error = EMAIL_ERROR_NONE;
12514         char sql_query_string[QUERY_SIZE] = {0, };
12515
12516         int transaction = true;
12517
12518         if (!old_server_uid || !new_server_uid || !mbox_name)  {
12519                 EM_DEBUG_EXCEPTION("Invalid parameters");
12520                 error = EMAIL_ERROR_INVALID_PARAM;
12521                 return false;
12522         }
12523
12524         EM_DEBUG_LOG("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
12525
12526         sqlite3 *local_db_handle = emstorage_get_db_connection();
12527         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12528
12529
12530         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12531                  "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);
12532
12533          EM_DEBUG_LOG(" Query [%s]", sql_query_string);
12534
12535         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12536          EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12537                  ("sqlite3_exec fail:%d", rc));
12538          EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12539                  ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12540
12541
12542          rc = sqlite3_changes(local_db_handle);
12543          if (rc == 0)
12544          {
12545                  EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
12546                  error = EMAIL_ERROR_DATA_NOT_FOUND;
12547                  goto FINISH_OFF;
12548          }
12549
12550
12551          ret = true;
12552
12553 FINISH_OFF:
12554
12555         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12556         _DISCONNECT_DB;
12557
12558         if (err_code != NULL)
12559                 *err_code = error;
12560
12561         EM_DEBUG_FUNC_END("ret [%d]", ret);
12562         return ret;
12563
12564 }
12565
12566
12567 /**
12568  * @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);
12569  * Prepare an array of mail_id and corresponding server mail id.
12570  *
12571  *@author                                       h.gahlaut@samsung.com
12572  * @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)
12573  *                                                      where 88 is the length of fixed keywords including ending null character in the QUERY to be formed
12574  * @param[out] idset                    Returns the array of mail_id and corresponding server_mail_id sorted by server_mail_ids
12575  * @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
12576  * @param[out] err_code         Returns the error code.
12577  * @remarks                                     An Example of Query to be exexuted in this API:
12578  *                                                      SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (12, 13, 56, 78);
12579  * @return This function returns true on success or false on failure.
12580  */
12581
12582 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)
12583 {
12584         EM_DEBUG_FUNC_BEGIN();
12585         EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds);
12586
12587         int error = EMAIL_ERROR_NONE;
12588         int ret = false;
12589         email_id_set_t* p_id_set = NULL;
12590         int count = 0;
12591         const int buf_size = QUERY_SIZE;
12592         char sql_query_string[QUERY_SIZE] = {0, };
12593         int space_left_in_query_buffer = buf_size;
12594         int i = 0;
12595         int rc = -1;
12596         char *server_mail_id = NULL;
12597         char **result = NULL;
12598         int col_index = 0;
12599
12600
12601         if (NULL == mail_ids || NULL == idset || NULL == id_set_count) {
12602                 EM_DEBUG_EXCEPTION("Invalid Parameters mail_ids[%p] idset[%p]  id_set_count [%p]", mail_ids, idset, id_set_count);
12603                 error = EMAIL_ERROR_INVALID_PARAM;
12604                 goto FINISH_OFF;
12605         }
12606
12607         sqlite3 *local_db_handle = emstorage_get_db_connection();
12608
12609         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);
12610
12611         EM_DEBUG_LOG("SQL Query formed [%s] ", sql_query_string);
12612
12613         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
12614         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
12615         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12616                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12617
12618         EM_DEBUG_LOG(" Count of mails [%d ]", count);
12619
12620         if (count <= 0) {
12621                 EM_DEBUG_EXCEPTION("Can't find proper mail");
12622                 error = EMAIL_ERROR_DATA_NOT_FOUND;
12623                 goto FINISH_OFF;
12624         }
12625
12626
12627         if (NULL == (p_id_set = (email_id_set_t*)em_malloc(sizeof(email_id_set_t) * count)))  {
12628                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
12629                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12630                 goto FINISH_OFF;
12631         }
12632
12633         col_index = 2;
12634
12635         EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds_Loop);
12636         EM_DEBUG_LOG(">>>> DATA ASSIGN START");
12637         for (i = 0; i < count; i++)  {
12638                 _get_table_field_data_int(result, &(p_id_set[i].mail_id), col_index++);
12639                 _get_table_field_data_string(result, &server_mail_id, 1, col_index++);
12640                 p_id_set[i].server_mail_id = strtoul(server_mail_id, NULL, 10);
12641                 EM_SAFE_FREE(server_mail_id);
12642         }
12643         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d]", count);
12644         EM_PROFILE_END(EmStorageGetIdSetFromMailIds_Loop);
12645
12646         sqlite3_free_table(result);
12647         result = NULL;
12648
12649         ret = true;
12650
12651         FINISH_OFF:
12652
12653         if (ret == true)  {
12654                 *idset = p_id_set;
12655                 *id_set_count = count;
12656                 EM_DEBUG_LOG(" idset[%p] id_set_count [%d]", *idset, *id_set_count);
12657         }
12658         else
12659                 EM_SAFE_FREE(p_id_set);
12660
12661         _DISCONNECT_DB;
12662
12663         if (err_code != NULL)
12664                 *err_code = error;
12665
12666         EM_PROFILE_END(EmStorageGetIdSetFromMailIds);
12667
12668         EM_DEBUG_FUNC_END("ret [%d]", ret);
12669         return ret;
12670 }
12671
12672
12673
12674 #endif
12675
12676 INTERNAL_FUNC int emstorage_delete_triggers_from_lucene()
12677 {
12678         EM_DEBUG_FUNC_BEGIN();
12679         int rc, ret = true, transaction = true;
12680         int error = EMAIL_ERROR_NONE;
12681         char sql_query_string[QUERY_SIZE] = {0, };
12682
12683         sqlite3 *local_db_handle = emstorage_get_db_connection();
12684         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12685
12686         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerDelete;");
12687
12688         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12689         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12690                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12691
12692         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerInsert;");
12693
12694         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12695         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12696                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12697
12698
12699         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerUpdate;");
12700
12701         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12702         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12703                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12704
12705         ret = true;
12706
12707 FINISH_OFF:
12708
12709         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12710
12711         _DISCONNECT_DB;
12712
12713         EM_DEBUG_FUNC_END("ret [%d]", ret);
12714         return ret;
12715 }
12716
12717 INTERNAL_FUNC int emstorage_filter_mails_by_rule(int account_id, int dest_mailbox_id, emstorage_rule_tbl_t *rule, int ** filtered_mail_id_list, int *count_of_mails, int *err_code)
12718 {
12719         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);
12720
12721         if ((account_id <= 0) || (dest_mailbox_id <= 0) || (!rule) || (!rule->value)|| (!filtered_mail_id_list) || (!count_of_mails)) {
12722                 EM_DEBUG_EXCEPTION("Invalid Parameter");
12723
12724                 if (err_code != NULL)
12725                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12726                 return false;
12727         }
12728
12729         int rc = -1, ret = false, error = EMAIL_ERROR_NONE;
12730         int count = 0, col_index = 0, i = 0, where_pararaph_length = 0, *mail_list = NULL;
12731         char **result = NULL, *where_pararaph = NULL;
12732         char sql_query_string[QUERY_SIZE] = {0, };
12733
12734         sqlite3 *local_db_handle = emstorage_get_db_connection();
12735
12736         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl ");
12737
12738         EM_DEBUG_LOG("rule->value [%s]", rule->value);
12739
12740         where_pararaph_length = EM_SAFE_STRLEN(rule->value) + 100;
12741         where_pararaph = malloc(sizeof(char) * where_pararaph_length);
12742
12743         if (where_pararaph == NULL) {
12744                 EM_DEBUG_EXCEPTION("malloc failed for where_pararaph.");
12745                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12746                 goto FINISH_OFF;
12747         }
12748
12749         memset(where_pararaph, 0, sizeof(char) * where_pararaph_length);
12750
12751         EM_DEBUG_LOG("rule->type [%d], rule->flag2[%d]", rule->type, rule->flag2);
12752
12753         if (rule->type == EMAIL_FILTER_FROM) {
12754                 if (rule->flag2 == RULE_TYPE_INCLUDES)
12755                         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);
12756                 else /*  RULE_TYPE_EXACTLY */
12757                         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);
12758         }
12759         else if (rule->type == EMAIL_FILTER_SUBJECT) {
12760                 if (rule->flag2 == RULE_TYPE_INCLUDES)
12761                         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);
12762                 else /*  RULE_TYPE_EXACTLY */
12763                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (3,5) AND subject = \'%s\'", account_id, rule->value);
12764         }
12765         else {
12766                 error = EMAIL_ERROR_INVALID_PARAM;
12767                 EM_DEBUG_EXCEPTION("rule->type is invald");
12768                 goto FINISH_OFF;
12769         }
12770
12771         /* prevent 34361 */
12772         if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
12773                 strcat(sql_query_string, where_pararaph);
12774
12775         EM_DEBUG_LOG("query[%s]", sql_query_string);
12776
12777         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
12778         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
12779         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12780                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12781
12782         EM_DEBUG_LOG("Count of mails [%d]", count);
12783
12784         if (count) {
12785                 mail_list = malloc(sizeof(int) * count);
12786                 if (mail_list == NULL) {
12787                         EM_DEBUG_EXCEPTION("malloc failed for mail_list.");
12788                         error = EMAIL_ERROR_OUT_OF_MEMORY;
12789                         goto FINISH_OFF;
12790                 }
12791
12792                 col_index = 1;
12793
12794                 for (i = 0; i < count; i++)
12795                         _get_table_field_data_int(result, &(mail_list[i]), col_index++);
12796
12797                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET mailbox_id = %d, mailbox_type = 5 ", dest_mailbox_id);
12798
12799                 /* prevent 34361 */
12800                 if(strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
12801                 strcat(sql_query_string, where_pararaph);
12802
12803                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12804                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12805                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12806         }
12807
12808         ret = true;
12809
12810 FINISH_OFF:
12811
12812         if (ret)
12813                 *filtered_mail_id_list = mail_list;
12814         else
12815                 EM_SAFE_FREE(mail_list);
12816
12817         sqlite3_free_table(result);
12818         result = NULL;
12819
12820         _DISCONNECT_DB;
12821
12822         EM_SAFE_FREE(where_pararaph);
12823
12824         if (ret && count_of_mails)
12825                 *count_of_mails = count;
12826
12827         if (err_code != NULL)
12828                 *err_code = error;
12829
12830         EM_DEBUG_FUNC_END("ret [%d]", ret);
12831         return ret;
12832 }
12833
12834 #define EMAIL_SLOT_UNIT 25
12835
12836 INTERNAL_FUNC int emstorage_set_mail_slot_size(int account_id, int mailbox_id, int new_slot_size, int transaction, int *err_code)
12837 {
12838         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);
12839         int rc = -1, ret = false, err = EMAIL_ERROR_NONE;
12840         int where_pararaph_length = 0;
12841         char *where_pararaph = NULL;
12842         char sql_query_string[QUERY_SIZE] = {0, };
12843         int and = 0;
12844         sqlite3 *local_db_handle = emstorage_get_db_connection();
12845
12846         EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
12847
12848         if (new_slot_size > 0)
12849                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = %d ", new_slot_size);
12850         else if (new_slot_size == 0)
12851                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", EMAIL_SLOT_UNIT);
12852         else
12853                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", new_slot_size * -1);
12854
12855
12856         if (mailbox_id)
12857                 where_pararaph_length = 80;
12858         else
12859                 where_pararaph_length = 50;
12860
12861         if (new_slot_size == 0)
12862                 where_pararaph_length += 70;
12863
12864         where_pararaph = malloc(sizeof(char) * where_pararaph_length);
12865         if (where_pararaph == NULL) {
12866                 EM_DEBUG_EXCEPTION("Memory allocation failed for where_pararaph");
12867                 err = EMAIL_ERROR_OUT_OF_MEMORY;
12868                 goto FINISH_OFF;
12869         }
12870         memset(where_pararaph, 0x00, where_pararaph_length);
12871
12872         if (account_id > ALL_ACCOUNT) {
12873                 and = 1;
12874                 if (mailbox_id)
12875                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_id = %d ", mailbox_id);
12876                 else
12877                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d ", account_id);
12878         }
12879
12880         if (new_slot_size == 0)
12881                 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"));
12882
12883         if (strlen(sql_query_string) + EM_SAFE_STRLEN(where_pararaph) < QUERY_SIZE) /* prevent 34363 */
12884                 strcat(sql_query_string, where_pararaph);
12885         else {
12886                 EM_DEBUG_EXCEPTION("Query buffer overflowed !!!");
12887                 err = EMAIL_ERROR_OUT_OF_MEMORY;
12888                 goto FINISH_OFF;
12889         }
12890
12891         EM_DEBUG_LOG("query[%s]", sql_query_string);
12892
12893         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12894         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12895         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12896
12897         ret = true;
12898
12899 FINISH_OFF:
12900
12901         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
12902
12903         _DISCONNECT_DB;
12904
12905         EM_SAFE_FREE(where_pararaph);
12906
12907         if (err_code != NULL)
12908                 *err_code = err;
12909
12910         EM_DEBUG_FUNC_END("ret [%d]", ret);
12911         return ret;
12912 }
12913
12914 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)
12915 {
12916         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);
12917
12918         if (!meeting_req || meeting_req->mail_id <= 0) {
12919                 if (meeting_req)
12920                 EM_DEBUG_EXCEPTION("mail_id[%]d", meeting_req->mail_id);
12921
12922                 if (err_code != NULL)
12923                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12924
12925                 return false;
12926         }
12927
12928         int rc = -1;
12929         int ret = false;
12930         int error = EMAIL_ERROR_NONE;
12931         DB_STMT hStmt = NULL;
12932         char sql_query_string[QUERY_SIZE] = {0, };
12933         int col_index = 0;
12934         time_t temp_unix_time = 0;
12935
12936         sqlite3 *local_db_handle = emstorage_get_db_connection();
12937         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12938
12939         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12940                 "INSERT INTO mail_meeting_tbl VALUES "
12941                 "( ?"           /*  mail_id */
12942                 ", ?"           /*  account_id */
12943                 ", ?"           /*  mailbox_id */
12944                 ", ?"           /*  meeting_response */
12945                 ", ?"           /*  start_time */
12946                 ", ?"           /*  end_time */
12947                 ", ?"           /*  location */
12948                 ", ?"           /*  global_object_id */
12949                 ", ?"           /*  offset */
12950                 ", ?"           /*  standard_name */
12951                 ", ?"           /*  standard_time_start_date */
12952                 ", ?"           /*  standard_biad */
12953                 ", ?"           /*  daylight_name */
12954                 ", ?"           /*  daylight_time_start_date */
12955                 ", ?"           /*  daylight_bias */
12956                 " )");
12957
12958         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12959         if (rc != SQLITE_OK)  {
12960                 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
12961                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
12962
12963                 error = EMAIL_ERROR_DB_FAILURE;
12964                 goto FINISH_OFF;
12965         }
12966
12967         col_index = 0;
12968         /*
12969         EM_DEBUG_LOG(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
12970         EM_DEBUG_LOG(">>>>> account_id[%d]", account_id);
12971         EM_DEBUG_LOG(">>>>> mailbox_name[%s]", mailbox_name);
12972         EM_DEBUG_LOG(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
12973         EM_DEBUG_LOG(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
12974         EM_DEBUG_LOG(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
12975         EM_DEBUG_LOG(">>>>> meeting_req->location[%s]", meeting_req->location);
12976         EM_DEBUG_LOG(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
12977         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
12978         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
12979         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
12980         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
12981         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
12982         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
12983         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
12984         */
12985         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->mail_id);
12986         _bind_stmt_field_data_int(hStmt, col_index++, account_id);
12987         _bind_stmt_field_data_int(hStmt, col_index++, input_mailbox_id);
12988         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
12989
12990         temp_unix_time = timegm(&(meeting_req->start_time));
12991         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
12992         temp_unix_time = timegm(&(meeting_req->end_time));
12993         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
12994
12995         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->location, 0, LOCATION_LEN_IN_MAIL_MEETING_TBL);
12996         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->global_object_id, 0, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
12997
12998         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
12999         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.standard_name, 0, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
13000         temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
13001         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13002         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
13003
13004         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.daylight_name, 0, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
13005         temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
13006         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13007         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
13008
13009
13010         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13011         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
13012                 ("sqlite3_step fail:%d", rc));
13013         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13014                 ("sqlite3_step fail:%d", rc));
13015
13016         ret = true;
13017
13018 FINISH_OFF:
13019         if (hStmt != NULL)  {
13020                 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
13021
13022                 rc = sqlite3_finalize(hStmt);
13023                 if (rc != SQLITE_OK)  {
13024                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
13025                         error = EMAIL_ERROR_DB_FAILURE;
13026                 }
13027         }
13028
13029         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13030         _DISCONNECT_DB;
13031
13032         if (err_code != NULL)
13033                 *err_code = error;
13034
13035         EM_DEBUG_FUNC_END("ret [%d]", ret);
13036         return ret;
13037 }
13038
13039 INTERNAL_FUNC int emstorage_get_meeting_request(int mail_id, email_meeting_request_t ** meeting_req, int transaction, int *err_code)
13040 {
13041         EM_DEBUG_FUNC_BEGIN();
13042
13043         int count = 0;
13044         int rc = -1;
13045         int ret = false;
13046         int error = EMAIL_ERROR_NONE;
13047         email_meeting_request_t *p_temp_meeting_req = NULL;
13048         int col_index = 0;
13049         time_t temp_unix_time;
13050
13051         EM_IF_NULL_RETURN_VALUE(meeting_req, false);
13052
13053         DB_STMT hStmt = NULL;
13054         char sql_query_string[QUERY_SIZE] = {0, };
13055
13056         sqlite3 *local_db_handle = emstorage_get_db_connection();
13057         EMSTORAGE_START_READ_TRANSACTION(transaction);
13058
13059         SNPRINTF(sql_query_string, sizeof(sql_query_string),
13060                 "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 "
13061                 " FROM mail_meeting_tbl "
13062                 " WHERE mail_id = %d", mail_id);
13063         EM_DEBUG_LOG("sql : %s ", sql_query_string);
13064
13065
13066         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13067
13068         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13069                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13070
13071
13072         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13073         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13074                 ("sqlite3_step fail:%d", rc));
13075
13076         if (rc == SQLITE_DONE)  {
13077                 EM_DEBUG_EXCEPTION(" no Meeting request found...");
13078                 count = 0;
13079                 ret = false;
13080                 error= EMAIL_ERROR_DATA_NOT_FOUND;
13081                 goto FINISH_OFF;
13082         }
13083
13084         if (!(p_temp_meeting_req = (email_meeting_request_t*)malloc(sizeof(email_meeting_request_t)))) {
13085                 EM_DEBUG_EXCEPTION(" malloc failed...");
13086                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13087                 goto FINISH_OFF;
13088         }
13089
13090         memset(p_temp_meeting_req, 0x00, sizeof(email_meeting_request_t));
13091
13092         col_index = 0;
13093
13094         _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->mail_id), col_index++);
13095         _get_stmt_field_data_int(hStmt, (int *)&(p_temp_meeting_req->meeting_response), col_index++);
13096         _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
13097         gmtime_r(&temp_unix_time, &(p_temp_meeting_req->start_time));
13098         _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
13099         gmtime_r(&temp_unix_time, &(p_temp_meeting_req->end_time));
13100         _get_stmt_field_data_string(hStmt, &p_temp_meeting_req->location, 1, col_index++);
13101         _get_stmt_field_data_string(hStmt, &p_temp_meeting_req->global_object_id, 1, col_index++);
13102         _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->time_zone.offset_from_GMT), col_index++);
13103
13104         _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++);
13105         _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
13106         gmtime_r(&temp_unix_time, &(p_temp_meeting_req->time_zone.standard_time_start_date));
13107         _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->time_zone.standard_bias), col_index++);
13108
13109         _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++);
13110         _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
13111         gmtime_r(&temp_unix_time, &(p_temp_meeting_req->time_zone.daylight_time_start_date));
13112         _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->time_zone.daylight_bias), col_index++);
13113
13114         /*
13115         EM_DEBUG_LOG(">>>>> meeting_req->mail_id[%d]", p_temp_meeting_req->mail_id);
13116         EM_DEBUG_LOG(">>>>> meeting_req->meeting_response[%d]", p_temp_meeting_req->meeting_response);
13117         EM_DEBUG_LOG(">>>>> meeting_req->start_time[%s]", asctime(&(p_temp_meeting_req->start_time)));
13118         EM_DEBUG_LOG(">>>>> meeting_req->end_time[%s]", asctime(&(p_temp_meeting_req->end_time)));
13119         EM_DEBUG_LOG(">>>>> meeting_req->location[%s]", p_temp_meeting_req->location);
13120         EM_DEBUG_LOG(">>>>> meeting_req->global_object_id[%s]", p_temp_meeting_req->global_object_id);
13121         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", p_temp_meeting_req->time_zone.offset_from_GMT);
13122         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_name[%s]", p_temp_meeting_req->time_zone.standard_name);
13123         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(p_temp_meeting_req->time_zone.standard_time_start_date)));
13124         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_bias[%d]", p_temp_meeting_req->time_zone.standard_bias);
13125         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_name[%s]", p_temp_meeting_req->time_zone.daylight_name);
13126         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(p_temp_meeting_req->time_zone.daylight_time_start_date)));
13127         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_bias[%d]", p_temp_meeting_req->time_zone.daylight_bias);
13128         */
13129         ret = true;
13130
13131 FINISH_OFF:
13132         if (ret == true)
13133                 *meeting_req = p_temp_meeting_req;
13134         else  {
13135                 EM_SAFE_FREE(p_temp_meeting_req);
13136         }
13137
13138         if (hStmt != NULL)  {
13139                 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
13140
13141                 rc = sqlite3_finalize(hStmt);
13142                 hStmt = NULL;
13143                 if (rc != SQLITE_OK)  {
13144                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
13145                         error = EMAIL_ERROR_DB_FAILURE;
13146                 }
13147         }
13148
13149         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13150         _DISCONNECT_DB;
13151
13152         if (err_code != NULL)
13153                 *err_code = error;
13154
13155         EM_DEBUG_FUNC_END("ret [%d]", ret);
13156         return ret;
13157 }
13158
13159
13160 INTERNAL_FUNC int emstorage_update_meeting_request(email_meeting_request_t* meeting_req, int transaction, int *err_code)
13161 {
13162         EM_DEBUG_FUNC_BEGIN("meeting_req[%p], transaction[%d], err_code[%p]", meeting_req, transaction, err_code);
13163
13164         int ret = false;
13165         int error = EMAIL_ERROR_NONE;
13166         int rc;
13167         DB_STMT hStmt = NULL;
13168         char sql_query_string[QUERY_SIZE] = {0, };
13169         time_t temp_unix_time = 0;
13170
13171         if (!meeting_req) {
13172                 EM_DEBUG_EXCEPTION("Invalid Parameter!");
13173                 error = EMAIL_ERROR_INVALID_PARAM;
13174                 goto FINISH_OFF;
13175         }
13176
13177         sqlite3 *local_db_handle = emstorage_get_db_connection();
13178         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13179
13180         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13181         SNPRINTF(sql_query_string, sizeof(sql_query_string),
13182                 "UPDATE mail_meeting_tbl "
13183                 "SET "
13184                 /* "  account_id = ?, "         //  not update here, this can be changed when move or copy */
13185                 /* "  mailbox_name = ?, "               //  not update here, this can be changed when move or copy */
13186                 "  meeting_response = ?, "
13187                 "  start_time = ?, "
13188                 "  end_time = ?, "
13189                 "  location = ?, "
13190                 "  global_object_id = ?, "
13191                 "  offset = ?, "
13192                 "  standard_name = ?, "
13193                 "  standard_time_start_date = ?, "
13194                 "  standard_bias = ?, "
13195                 "  daylight_name = ?, "
13196                 "  daylight_time_start_date = ?, "
13197                 "  daylight_bias = ? "
13198                 "WHERE mail_id = %d",
13199                 meeting_req->mail_id);
13200
13201         EM_DEBUG_LOG("SQL(%s)", sql_query_string);
13202
13203         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13204         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13205                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13206 /*
13207         EM_DEBUG_LOG(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
13208         EM_DEBUG_LOG(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
13209         EM_DEBUG_LOG(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
13210         EM_DEBUG_LOG(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
13211         EM_DEBUG_LOG(">>>>> meeting_req->location[%s]", meeting_req->location);
13212         EM_DEBUG_LOG(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
13213         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
13214         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
13215         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
13216         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
13217         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
13218         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
13219         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
13220 */
13221         int col_index = 0;
13222
13223         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
13224         temp_unix_time = timegm(&(meeting_req->start_time));
13225         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13226         temp_unix_time = timegm(&(meeting_req->end_time));
13227         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13228         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->location, 1, LOCATION_LEN_IN_MAIL_MEETING_TBL);
13229         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->global_object_id, 1, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
13230         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
13231         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.standard_name, 1, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
13232         temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
13233         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13234         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
13235         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.daylight_name, 1, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
13236         temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
13237         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13238         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
13239
13240
13241         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13242         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13243                 ("sqlite3_step fail:%d", rc));
13244         ret = true;
13245
13246
13247 FINISH_OFF:
13248         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13249
13250         if (hStmt != NULL)  {
13251                 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
13252
13253                 rc = sqlite3_finalize(hStmt);
13254                 if (rc != SQLITE_OK)  {
13255                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
13256
13257                         error = EMAIL_ERROR_DB_FAILURE;
13258                 }
13259         }
13260
13261         _DISCONNECT_DB;
13262
13263         if (err_code != NULL)
13264                 *err_code = error;
13265
13266         EM_DEBUG_FUNC_END("ret [%d]", ret);
13267         return ret;
13268 }
13269
13270 INTERNAL_FUNC int emstorage_delete_meeting_request(int account_id, int mail_id, int input_mailbox_id, int transaction, int *err_code)
13271 {
13272         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);
13273
13274         if (account_id < ALL_ACCOUNT || mail_id < 0) {
13275                 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d]", account_id, mail_id);
13276
13277                 if (err_code != NULL)
13278                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13279                 return false;
13280         }
13281
13282         int rc;
13283         int ret = false;
13284         int error = EMAIL_ERROR_NONE;
13285         int and = false;
13286         char sql_query_string[QUERY_SIZE] = {0, };
13287
13288         sqlite3 *local_db_handle = emstorage_get_db_connection();
13289         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13290
13291         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_meeting_tbl ");
13292
13293         if (account_id != ALL_ACCOUNT) {                /*  NOT '0' means a specific account. '0' means all account */
13294                 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);
13295                 and = true;
13296         }
13297         if (mail_id > 0) {
13298                 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);
13299                 and = true;
13300         }
13301         if (input_mailbox_id > 0) {             /*  0 means all mailbox_id */
13302                 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);
13303         }
13304
13305         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13306         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13307                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13308
13309         ret = true;
13310
13311 FINISH_OFF:
13312         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13313         _DISCONNECT_DB;
13314
13315         if (err_code)
13316                 *err_code = error;
13317
13318         EM_DEBUG_FUNC_END("ret [%d]", ret);
13319         return ret;
13320 }
13321
13322
13323 INTERNAL_FUNC void emstorage_free_meeting_request(email_meeting_request_t *meeting_req)
13324 {
13325         EM_DEBUG_FUNC_BEGIN();
13326
13327         if (!meeting_req ) {
13328                 EM_DEBUG_EXCEPTION("NULL PARAM");
13329                 return;
13330                 }
13331
13332         EM_SAFE_FREE(meeting_req->location);
13333         EM_SAFE_FREE(meeting_req->global_object_id);
13334
13335         EM_DEBUG_FUNC_END();
13336 }
13337
13338
13339 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)
13340 {
13341         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);
13342         EM_PROFILE_BEGIN(profile_emstorage_get_overflowed_mail_id_list);
13343         char sql_query_string[QUERY_SIZE] = {0, };
13344         char **result = NULL;
13345         int rc = -1, ret = false;
13346         int error = EMAIL_ERROR_NONE;
13347         int counter = 0, col_index = 0;
13348         int result_mail_id_count = 0;
13349         int *result_mail_id_list = NULL;
13350
13351         if (input_mailbox_id <= 0 || !mail_id_list || !mail_id_count || account_id < 1) {
13352                 EM_DEBUG_EXCEPTION("Invalid Parameter");
13353                 error = EMAIL_ERROR_INVALID_PARAM;
13354                 goto FINISH_OFF;
13355         }
13356
13357         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);
13358
13359         EM_DEBUG_LOG("query[%s].", sql_query_string);
13360
13361         sqlite3 *local_db_handle = emstorage_get_db_connection();
13362         EMSTORAGE_START_READ_TRANSACTION(transaction);
13363
13364         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_mail_id_count, 0, NULL), rc);
13365         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13366                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13367
13368         if (!result_mail_id_count) {
13369                 EM_DEBUG_LOG("No mail found...");
13370                 ret = false;
13371                 error= EMAIL_ERROR_MAIL_NOT_FOUND;
13372                 goto FINISH_OFF;
13373         }
13374
13375         EM_DEBUG_LOG("There are [%d] overflowed mails in mailbox_id [%d]", result_mail_id_count, input_mailbox_id);
13376
13377         if (!(result_mail_id_list = (int *)malloc(sizeof(int) * result_mail_id_count))) {
13378                 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
13379                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13380                 sqlite3_free_table(result);
13381                 goto FINISH_OFF;
13382         }
13383
13384         memset(result_mail_id_list, 0x00, sizeof(int) * result_mail_id_count);
13385
13386         col_index = 1;
13387
13388         for (counter = 0; counter < result_mail_id_count; counter++)
13389                 _get_table_field_data_int(result, result_mail_id_list + counter, col_index++);
13390
13391         ret = true;
13392
13393 FINISH_OFF:
13394         EM_DEBUG_LOG("finish off [%d]", ret);
13395
13396         if (result)
13397                 sqlite3_free_table(result);
13398
13399         if (ret == true)  {
13400                 *mail_id_list = result_mail_id_list;
13401                 *mail_id_count = result_mail_id_count;
13402         }
13403         else
13404                 EM_SAFE_FREE(result_mail_id_list);
13405
13406         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13407         _DISCONNECT_DB;
13408
13409         if (err_code != NULL)
13410                 *err_code = error;
13411
13412         EM_PROFILE_END(profile_emstorage_get_overflowed_mail_id_list);
13413         EM_DEBUG_FUNC_END("ret [%d]", ret);
13414         return ret;
13415 }
13416
13417
13418 INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(int mail_id, int *thread_id, int *err_code)
13419 {
13420         EM_DEBUG_FUNC_BEGIN("mail_id[%d], thread_id[%p], err_code[%p]", mail_id, thread_id, err_code);
13421
13422         int rc = -1, ret = false;
13423         int err = EMAIL_ERROR_NONE;
13424         char sql_query_string[QUERY_SIZE] = {0, };
13425         char **result;
13426         int result_count = 0;
13427
13428         if (mail_id == 0 || thread_id == NULL) {
13429                 EM_DEBUG_EXCEPTION("Invalid Parameter");
13430                 if (err_code != NULL)
13431                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13432                 goto FINISH_OFF;
13433         }
13434
13435         sqlite3 *local_db_handle = emstorage_get_db_connection();
13436
13437         memset(sql_query_string, 0, QUERY_SIZE);
13438         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT thread_id FROM mail_tbl WHERE mail_id = %d", mail_id);
13439
13440         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
13441         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
13442         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13443                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13444
13445         if (!result_count) {
13446                 EM_DEBUG_EXCEPTION("No mail found...");
13447                 ret = false;
13448                 err= EMAIL_ERROR_MAIL_NOT_FOUND;
13449                 /* sqlite3_free_table(result); */
13450                 goto FINISH_OFF;
13451         }
13452
13453         _get_table_field_data_int(result, thread_id, 1);
13454
13455         sqlite3_free_table(result);
13456
13457         ret = true;
13458
13459 FINISH_OFF:
13460
13461         _DISCONNECT_DB;
13462
13463         if (err_code != NULL)
13464                 *err_code = err;
13465
13466         EM_DEBUG_FUNC_END("ret [%d]", ret);
13467         return ret;
13468 }
13469
13470
13471 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)
13472 {
13473         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);
13474
13475         int rc = -1, ret = false;
13476         int err = EMAIL_ERROR_NONE;
13477         char sql_query_string[QUERY_SIZE] = {0, };
13478         char **result = NULL;
13479         int result_count = 0;
13480
13481         if (thread_id == 0) {
13482                 EM_DEBUG_EXCEPTION("Invalid Parameter");
13483                 if (err_code != NULL)
13484                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13485                 goto FINISH_OFF;
13486         }
13487
13488         sqlite3 *local_db_handle = emstorage_get_db_connection();
13489
13490         if (thread_item_count == 0 || latest_mail_id == 0) {
13491                 memset(sql_query_string, 0, QUERY_SIZE);
13492                 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);
13493
13494                 /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
13495                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
13496                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13497                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13498                 EM_DEBUG_LOG("result_count[%d]", result_count);
13499                 if (result_count == 0) {
13500                         EM_DEBUG_EXCEPTION("No mail found...");
13501                         ret = false;
13502                         err= EMAIL_ERROR_MAIL_NOT_FOUND;
13503                         sqlite3_free_table(result);
13504                         goto FINISH_OFF;
13505                 }
13506
13507                 _get_table_field_data_int(result, &latest_mail_id, 2);
13508                 _get_table_field_data_int(result, &thread_item_count, 3);
13509
13510                 EM_DEBUG_LOG("latest_mail_id[%d]", latest_mail_id);
13511                 EM_DEBUG_LOG("thread_item_count[%d]", thread_item_count);
13512
13513                 sqlite3_free_table(result);
13514         }
13515
13516         EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
13517
13518         /* if (thread_item_count > 1) */
13519         /* { */
13520                 memset(sql_query_string, 0, QUERY_SIZE);
13521                 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);
13522                 EM_DEBUG_LOG("query[%s]", sql_query_string);
13523
13524                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13525                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13526                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13527         /* } */
13528
13529         memset(sql_query_string, 0, QUERY_SIZE);
13530         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);
13531         EM_DEBUG_LOG("query[%s]", sql_query_string);
13532
13533         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13534                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13535                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13536
13537         ret = true;
13538
13539 FINISH_OFF:
13540
13541         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
13542
13543         _DISCONNECT_DB;
13544
13545         if (err_code != NULL)
13546                 *err_code = err;
13547
13548         EM_DEBUG_FUNC_END("ret [%d]", ret);
13549         return ret;
13550 }
13551
13552
13553 INTERNAL_FUNC void emstorage_flush_db_cache()
13554 {
13555         sqlite3_release_memory(-1);
13556 }
13557
13558 #ifdef __FEATURE_LOCAL_ACTIVITY__
13559 /**
13560   *     emstorage_add_activity - Add Email Local activity during OFFLINE mode
13561   *
13562   */
13563 INTERNAL_FUNC int emstorage_add_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
13564 {
13565         EM_DEBUG_FUNC_BEGIN();
13566
13567         EM_DEBUG_LOG(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
13568
13569         int rc = -1, ret = false;
13570         int error = EMAIL_ERROR_NONE;
13571         DB_STMT hStmt = NULL;
13572         char sql_query_string[8192] = { 0x00, };
13573         int i = 0;
13574
13575         if (!local_activity) {
13576                 EM_DEBUG_EXCEPTION(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
13577                 if (err_code != NULL)
13578                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13579                 return false;
13580         }
13581
13582         sqlite3 *local_db_handle = emstorage_get_db_connection();
13583         memset(sql_query_string, 0x00 , sizeof(sql_query_string));
13584         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13585
13586         SNPRINTF(sql_query_string, sizeof(sql_query_string), "INSERT INTO mail_local_activity_tbl VALUES (?, ?, ?, ?, ?, ?, ?)");
13587
13588         EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ] ", local_activity->activity_id);
13589         EM_DEBUG_LOG(">>>>> MAIL ID [ %d ] ", local_activity->mail_id);
13590         EM_DEBUG_LOG(">>>>> ACCOUNT ID [ %d ] ", local_activity->account_id);
13591         EM_DEBUG_LOG(">>>>> ACTIVITY TYPE [ %d ] ", local_activity->activity_type);
13592         EM_DEBUG_LOG(">>>>> SERVER MAIL ID [ %s ] ", local_activity->server_mailid);
13593         EM_DEBUG_LOG(">>>>> SOURCE MAILBOX [ %s ] ", local_activity->src_mbox);
13594         EM_DEBUG_LOG(">>>>> DEST MAILBOX   [ %s ] ", local_activity->dest_mbox);
13595
13596         EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
13597
13598
13599         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13600         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13601                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13602
13603         EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
13604
13605
13606
13607
13608         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
13609         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
13610         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
13611         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
13612         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->server_mailid, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
13613         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->src_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
13614         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->dest_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
13615
13616
13617         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13618
13619         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
13620                 ("sqlite3_step fail:%d", rc));
13621         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13622                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
13623
13624         ret = true;
13625
13626 FINISH_OFF:
13627
13628         if (hStmt != NULL)  {
13629                 rc = sqlite3_finalize(hStmt);
13630                 if (rc != SQLITE_OK)  {
13631                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
13632
13633                         error = EMAIL_ERROR_DB_FAILURE;
13634                 }
13635         }
13636         else {
13637                 EM_DEBUG_LOG(" >>>>>>>>>> hStmt is NULL!!!");
13638         }
13639
13640         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13641         _DISCONNECT_DB;
13642
13643
13644         if (err_code != NULL)
13645                 *err_code = error;
13646
13647         EM_DEBUG_FUNC_END("ret [%d]", ret);
13648         return ret;
13649 }
13650
13651 /**
13652   *     emstorage_get_activity - Get the Local activity Information
13653   *
13654   *
13655   */
13656 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)
13657 {
13658         EM_DEBUG_FUNC_BEGIN();
13659
13660         int i = 0, count = 0, rc = -1, ret = false;
13661         int error = EMAIL_ERROR_NONE;
13662         emstorage_activity_tbl_t *p_activity_tbl = NULL;
13663         char sql_query_string[1024] = {0x00, };
13664         char **result = NULL;
13665         int col_index ;
13666
13667         EM_IF_NULL_RETURN_VALUE(activity_list, false);
13668         EM_IF_NULL_RETURN_VALUE(select_num, false);
13669
13670
13671         if (!select_num || !activity_list || account_id <= 0 || activityid < 0) {
13672                 EM_DEBUG_LOG(" select_num[%p], activity_list[%p] account_id [%d] activityid [%d] ", select_num, activity_list, account_id, activityid);
13673                 if (err_code != NULL)
13674                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13675                 return false;
13676         }
13677
13678         EMSTORAGE_START_READ_TRANSACTION(transaction);
13679
13680         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13681
13682         if (activityid == ALL_ACTIVITIES) {
13683                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d order by activity_id", account_id);
13684         }
13685         else {
13686                 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);
13687         }
13688
13689         EM_DEBUG_LOG("Query [%s]", sql_query_string);
13690
13691
13692
13693         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
13694         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13695                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13696
13697
13698
13699         col_index = 7;
13700
13701         if (!(p_activity_tbl = (emstorage_activity_tbl_t*)em_malloc(sizeof(emstorage_activity_tbl_t) * count))) {
13702                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
13703                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13704                 goto FINISH_OFF;
13705         }
13706
13707
13708         for (i = 0; i < count; i++)  {
13709                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13710                 if (result[col_index])
13711                         p_activity_tbl[i].activity_id = atoi(result[col_index++]);
13712
13713                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13714                 if (result[col_index])
13715                         p_activity_tbl[i].account_id = atoi(result[col_index++]);
13716
13717                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13718                 if (result[col_index])
13719                         p_activity_tbl[i].mail_id = atoi(result[col_index++]);
13720
13721                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13722                 if (result[col_index])
13723                         p_activity_tbl[i].activity_type = atoi(result[col_index++]);
13724
13725
13726                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13727                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index])>0)
13728                         p_activity_tbl[i].server_mailid = EM_SAFE_STRDUP(result[col_index++]);
13729                 else
13730                         col_index++;
13731
13732                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13733                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index])>0)
13734                         p_activity_tbl[i].src_mbox = EM_SAFE_STRDUP(result[col_index++]);
13735                 else
13736                         col_index++;
13737
13738                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13739                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index])>0)
13740                         p_activity_tbl[i].dest_mbox = EM_SAFE_STRDUP(result[col_index++]);
13741                 else
13742                         col_index++;
13743
13744         }
13745
13746         if (result)
13747                 sqlite3_free_table(result);
13748
13749         ret = true;
13750
13751 FINISH_OFF:
13752
13753
13754         if (ret == true)  {
13755                 *activity_list = p_activity_tbl;
13756                 *select_num = count;
13757                 EM_DEBUG_LOG(">>>> COUNT : %d >> ", count);
13758         }
13759         else if (p_activity_tbl != NULL) {
13760                 emstorage_free_local_activity(&p_activity_tbl, count, NULL);
13761         }
13762
13763         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13764         if (err_code != NULL)
13765                 *err_code = error;
13766
13767         EM_DEBUG_FUNC_END("ret [%d]", ret);
13768         return ret;
13769 }
13770
13771
13772 INTERNAL_FUNC int emstorage_get_next_activity_id(int *activity_id, int *err_code)
13773 {
13774
13775         EM_DEBUG_FUNC_BEGIN();
13776
13777         int ret = false;
13778         int err = EMAIL_ERROR_NONE;
13779         int rc = -1;
13780         char *sql = NULL;
13781         char **result = NULL;
13782
13783         if (NULL == activity_id)
13784         {
13785                 EM_DEBUG_EXCEPTION(" activity_id[%p]", activity_id);
13786
13787                 err = EMAIL_ERROR_INVALID_PARAM;
13788                 goto FINISH_OFF;
13789         }
13790
13791         /*  increase unique id */
13792
13793         sql = "SELECT max(rowid) FROM mail_local_activity_tbl;";
13794
13795         /*  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); */
13796         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13797                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13798
13799         if (NULL==result[1])
13800                 rc = 1;
13801         else
13802                 rc = atoi(result[1])+1;
13803
13804         *activity_id = rc;
13805
13806         if (result)
13807                 sqlite3_free_table(result);
13808
13809         ret = true;
13810
13811         FINISH_OFF:
13812
13813         if (NULL != err_code) {
13814                 *err_code = err;
13815         }
13816
13817         EM_DEBUG_FUNC_END("ret [%d]", ret);
13818         return ret;
13819
13820 }
13821
13822 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)
13823 {
13824
13825         EM_DEBUG_FUNC_BEGIN();
13826
13827         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);
13828
13829         if (account_id <= 0|| NULL == activity_id_list || NULL == activity_id_count ||lowest_activity_type <=0 || highest_activity_type <= 0)  {
13830                 if (err_code != NULL)
13831                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13832                 return false;
13833         }
13834
13835         int ret = false;
13836         int error = EMAIL_ERROR_NONE;
13837         int i = 0, rc = -1, count = 0;
13838         char sql_query_string[1024] = {0x00, };
13839         int *activity_ids = NULL;
13840         int col_index = 0;
13841         char **result = NULL;
13842
13843         EMSTORAGE_START_READ_TRANSACTION(transaction);
13844
13845         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13846
13847         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);
13848
13849         EM_DEBUG_LOG(" Query [%s]", sql_query_string);
13850
13851
13852         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
13853         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13854                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13855
13856         col_index = 1;
13857
13858         EM_DEBUG_LOG(" Activity COUNT : %d ... ", count);
13859
13860         if (NULL == (activity_ids = (int *)em_malloc(sizeof(int) * count))) {
13861                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
13862                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13863                 goto FINISH_OFF;
13864         }
13865
13866         for (i = 0; i < count; i++)  {
13867                 activity_ids[i] = atoi(result[col_index]);
13868                 col_index++;
13869                 EM_DEBUG_LOG("activity_id %d", activity_ids[i]);
13870         }
13871
13872         ret = true;
13873
13874 FINISH_OFF:
13875
13876
13877         if (ret == true)  {
13878                 *activity_id_count = count;
13879                 *activity_id_list = activity_ids;
13880
13881         }
13882         else if (activity_ids != NULL) /* Prevent defect - 216566 */
13883                 EM_SAFE_FREE(activity_ids);
13884
13885
13886         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13887         if (err_code != NULL) {
13888                 *err_code = error;
13889         }
13890
13891         EM_DEBUG_FUNC_END("ret [%d]", ret);
13892         return ret;
13893 }
13894
13895 INTERNAL_FUNC int emstorage_free_activity_id_list(int *activity_id_list, int *error_code)
13896 {
13897         EM_DEBUG_FUNC_BEGIN();
13898
13899         int error = EMAIL_ERROR_NONE;
13900         int ret = false;
13901
13902         EM_DEBUG_LOG(" activity_id_list [%p]", activity_id_list);
13903
13904         if (NULL == activity_id_list) {
13905                 error = EMAIL_ERROR_INVALID_PARAM;
13906                 goto FINISH_OFF;
13907         }
13908         else {
13909                 free(activity_id_list);
13910                 activity_id_list = NULL;
13911         }
13912
13913
13914         ret= true;
13915
13916         FINISH_OFF:
13917
13918         if (NULL != error_code) {
13919                 *error_code = error;
13920         }
13921
13922         EM_DEBUG_FUNC_END("ret [%d]", ret);
13923         return ret;
13924 }
13925
13926 /**
13927  * emstorage_delete_local_activity - Deletes the Local acitivity Generated based on activity_type
13928  * or based on server mail id
13929  *
13930  */
13931 INTERNAL_FUNC int emstorage_delete_local_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
13932 {
13933         EM_DEBUG_FUNC_BEGIN();
13934
13935
13936         EM_DEBUG_LOG(" local_activity[%p] ", local_activity);
13937
13938         if (!local_activity)  {
13939                 EM_DEBUG_EXCEPTION(" local_activity[%p] ", local_activity);
13940                 if (err_code != NULL)
13941                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13942                 return false;
13943         }
13944
13945         int rc = -1, ret = false;                       /* Prevent_FIX  */
13946         int err = EMAIL_ERROR_NONE;
13947         int query_and = 0;
13948         int query_where = 0;
13949         char sql_query_string[8192] = { 0x00, };
13950         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13951
13952         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13953
13954         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_local_activity_tbl ");
13955
13956         EM_DEBUG_LOG(">>> Query [ %s ] ", sql_query_string);
13957
13958         if (local_activity->account_id) {
13959                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
13960                 " WHERE account_id = %d ", local_activity->account_id);
13961                 query_and = 1;
13962                 query_where = 1;
13963         }
13964
13965         if (local_activity->server_mailid) {
13966                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
13967                 " %s %s server_mailid = '%s' ", query_where? "": "WHERE", query_and? "AND":"", local_activity->server_mailid);
13968                 query_and = 1;
13969                 query_where = 1;
13970         }
13971
13972
13973         if (local_activity->mail_id) {
13974                 EM_DEBUG_LOG(">>>> MAIL ID [ %d ] , ACTIVITY TYPE [%d ]", local_activity->mail_id, local_activity->activity_type);
13975
13976                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
13977                 " %s %s mail_id = %d  ", query_where? "": "WHERE", query_and? "AND":"", local_activity->mail_id);
13978
13979                 query_and = 1;
13980                 query_where = 1;
13981
13982         }
13983
13984         if (local_activity->activity_type > 0) {
13985                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
13986                 " %s %s activity_type = %d ", query_where? "": "WHERE", query_and? "AND" : "" , local_activity->activity_type);
13987         }
13988
13989         if (local_activity->activity_id > 0) {
13990                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
13991                 " %s %s activity_id = %d ", query_where? "": "WHERE", query_and? "AND" : "" , local_activity->activity_id);
13992
13993         }
13994
13995         EM_DEBUG_LOG(">>>>> Query [ %s ] ", sql_query_string);
13996
13997         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13998         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13999                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14000
14001         rc = sqlite3_changes(local_db_handle);
14002         if (rc == 0)  {
14003                 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
14004                 err = EMAIL_ERROR_MAILBOX_NOT_FOUND;
14005         }
14006
14007         ret = true;
14008
14009 FINISH_OFF:
14010         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
14011         if (err_code != NULL)
14012                 *err_code = err;
14013         EM_DEBUG_FUNC_END("ret [%d]", ret);
14014         return ret;
14015 }
14016
14017 /**
14018 *       emstorage_free_local_activity - Free the Local Activity data
14019 */
14020 INTERNAL_FUNC int emstorage_free_local_activity(emstorage_activity_tbl_t **local_activity_list, int count, int *err_code)
14021 {
14022         EM_DEBUG_FUNC_BEGIN();
14023
14024         EM_DEBUG_LOG(" local_activity_list[%p], count[%d], err_code[%p]", local_activity_list, count, err_code);
14025
14026         int ret = false;
14027         int error = EMAIL_ERROR_INVALID_PARAM;
14028
14029         if (count > 0) {
14030                 if (!local_activity_list || !*local_activity_list) {
14031                         EM_DEBUG_EXCEPTION(" local_activity_list[%p], count[%d]", local_activity_list, count);
14032
14033                         error = EMAIL_ERROR_INVALID_PARAM;
14034                         goto FINISH_OFF;
14035                 }
14036
14037                 emstorage_activity_tbl_t* p = *local_activity_list;
14038                 int i = 0;
14039                 if (p) {
14040                         for (; i < count; i++)  {
14041                                 EM_SAFE_FREE(p[i].dest_mbox);
14042                                 EM_SAFE_FREE(p[i].src_mbox);
14043                                 EM_SAFE_FREE(p[i].server_mailid);
14044                         }
14045
14046                         free(p);        *local_activity_list = NULL;
14047                 }
14048         }
14049
14050         ret = true;
14051
14052 FINISH_OFF:
14053         if (err_code != NULL)
14054                 *err_code = error;
14055
14056         EM_DEBUG_FUNC_END("ret [%d]", ret);
14057         return ret;
14058
14059 }
14060 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
14061
14062 static char *_mail_field_name_list[] = {
14063                 "mail_id",
14064                 "account_id",
14065                 "mailbox_id",
14066                 "mailbox_name",
14067                 "mailbox_type",
14068                 "subject",
14069                 "date_time",
14070                 "server_mail_status",
14071                 "server_mailbox_name",
14072                 "server_mail_id",
14073                 "message_id",
14074                 "reference_mail_id",
14075                 "full_address_from",
14076                 "full_address_to",
14077                 "full_address_cc",
14078                 "full_address_bcc",
14079                 "body_download_status",
14080                 "file_path_plain",
14081                 "file_path_html",
14082                 "file_size",
14083                 "flags_seen_field",
14084                 "flags_deleted_field",
14085                 "flags_flagged_field",
14086                 "flags_answered_field",
14087                 "flags_recent_field",
14088                 "flags_draft_field",
14089                 "flags_forwarded_field",
14090                 "drm_status",
14091                 "priority",
14092                 "save_status",
14093                 "lock_status",
14094                 "report_status",
14095                 "attachment_count",
14096                 "inline_content_count",
14097                 "thread_id",
14098                 "thread_item_count",
14099                 "preview_text",
14100                 "meeting_request_status",
14101                 "message_class",
14102                 "digest_type",
14103                 "smime_type"
14104 };
14105
14106 static char*_get_mail_field_name_by_attribute_type(email_mail_attribute_type input_attribute_type)
14107 {
14108         EM_DEBUG_FUNC_BEGIN("input_attribute_type [%d]", input_attribute_type);
14109
14110         if(input_attribute_type > EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS || input_attribute_type < 0) {
14111                 EM_DEBUG_EXCEPTION("Invalid input_attribute_type [%d]", input_attribute_type);
14112                 return NULL;
14113         }
14114
14115         EM_DEBUG_FUNC_END("return [%s]", _mail_field_name_list[input_attribute_type]);
14116         return _mail_field_name_list[input_attribute_type];
14117 }
14118
14119 static int _get_attribute_type_by_mail_field_name(char *input_mail_field_name, email_mail_attribute_type *output_mail_attribute_type)
14120 {
14121         EM_DEBUG_FUNC_BEGIN("input_mail_field_name [%p], output_mail_attribute_type [%p]", input_mail_field_name, output_mail_attribute_type);
14122         int i = 0;
14123         int err = EMAIL_ERROR_DATA_NOT_FOUND;
14124
14125         if(!input_mail_field_name || !output_mail_attribute_type) {
14126                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14127                 return EMAIL_ERROR_INVALID_PARAM;
14128         }
14129
14130         for (i = 0; i < EMAIL_MAIL_ATTRIBUTE_END; i++) {
14131                 if (EM_SAFE_STRCMP(input_mail_field_name, _mail_field_name_list[i]) == 0) {
14132                         *output_mail_attribute_type = i;
14133                         err = EMAIL_ERROR_NONE;
14134                         break;
14135                 }
14136         }
14137
14138         EM_DEBUG_FUNC_END("found mail attribute type [%d]", (int)*output_mail_attribute_type);
14139         return err;
14140 }
14141
14142 static int _get_key_value_string_for_list_filter_rule(email_list_filter_rule_t *input_list_filter_rule,char **output_key_value_string)
14143 {
14144         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_key_value_string [%p]", input_list_filter_rule, output_key_value_string);
14145
14146         int  ret = EMAIL_ERROR_NONE;
14147         char key_value_string[QUERY_SIZE] = { 0, };
14148         char *temp_key_value_1 = NULL;
14149         char *temp_key_value_2 = NULL;
14150
14151         if(input_list_filter_rule == NULL || output_key_value_string == NULL) {
14152                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14153                 return EMAIL_ERROR_INVALID_PARAM;
14154         }
14155
14156         switch(input_list_filter_rule->target_attribute) {
14157         case EMAIL_MAIL_ATTRIBUTE_MAIL_ID                 :
14158         case EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID              :
14159         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID              :
14160         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE            :
14161         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_STATUS      :
14162         case EMAIL_MAIL_ATTRIBUTE_REFERENCE_MAIL_ID       :
14163         case EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS    :
14164         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN         :
14165         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML          :
14166         case EMAIL_MAIL_ATTRIBUTE_FILE_SIZE               :
14167         case EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD        :
14168         case EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD     :
14169         case EMAIL_MAIL_ATTRIBUTE_FLAGS_FLAGGED_FIELD     :
14170         case EMAIL_MAIL_ATTRIBUTE_FLAGS_ANSWERED_FIELD    :
14171         case EMAIL_MAIL_ATTRIBUTE_FLAGS_RECENT_FIELD      :
14172         case EMAIL_MAIL_ATTRIBUTE_FLAGS_DRAFT_FIELD       :
14173         case EMAIL_MAIL_ATTRIBUTE_FLAGS_FORWARDED_FIELD   :
14174         case EMAIL_MAIL_ATTRIBUTE_DRM_STATUS              :
14175         case EMAIL_MAIL_ATTRIBUTE_PRIORITY                :
14176         case EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS             :
14177         case EMAIL_MAIL_ATTRIBUTE_LOCK_STATUS             :
14178         case EMAIL_MAIL_ATTRIBUTE_REPORT_STATUS           :
14179         case EMAIL_MAIL_ATTRIBUTE_ATTACHMENT_COUNT        :
14180         case EMAIL_MAIL_ATTRIBUTE_INLINE_CONTENT_COUNT    :
14181         case EMAIL_MAIL_ATTRIBUTE_THREAD_ID               :
14182         case EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT       :
14183         case EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS  :
14184         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_CLASS           :
14185         case EMAIL_MAIL_ATTRIBUTE_DIGEST_TYPE             :
14186         case EMAIL_MAIL_ATTRIBUTE_SMIME_TYPE              :
14187                 SNPRINTF(key_value_string, QUERY_SIZE, "%d", input_list_filter_rule->key_value.integer_type_value);
14188                 break;
14189
14190         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME            :
14191         case EMAIL_MAIL_ATTRIBUTE_SUBJECT                 :
14192         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME     :
14193         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID          :
14194         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID              :
14195         case EMAIL_MAIL_ATTRIBUTE_FROM                    :
14196         case EMAIL_MAIL_ATTRIBUTE_TO                      :
14197         case EMAIL_MAIL_ATTRIBUTE_CC                      :
14198         case EMAIL_MAIL_ATTRIBUTE_BCC                     :
14199         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT            :
14200                 if(input_list_filter_rule->key_value.string_type_value == NULL) {
14201                         EM_DEBUG_EXCEPTION("Invalid string_type_value [%p]", input_list_filter_rule->key_value.string_type_value);
14202                         ret = EMAIL_ERROR_INVALID_PARAM;
14203                         goto FINISH_OFF;
14204                 }
14205
14206                 temp_key_value_1 = input_list_filter_rule->key_value.string_type_value;
14207
14208                 temp_key_value_2 = em_replace_all_string(temp_key_value_1, "_", "\\_");
14209                 temp_key_value_1 = em_replace_all_string(temp_key_value_2, "%", "\\%");
14210
14211                 if(input_list_filter_rule->rule_type == EMAIL_LIST_FILTER_RULE_INCLUDE)
14212                         SNPRINTF(key_value_string, QUERY_SIZE, "\'%%%s%%\'", temp_key_value_1);
14213                 else
14214                         SNPRINTF(key_value_string, QUERY_SIZE, "\'%s\'", temp_key_value_1);
14215                 break;
14216
14217         case EMAIL_MAIL_ATTRIBUTE_DATE_TIME               :
14218                 SNPRINTF(key_value_string, QUERY_SIZE, "%d", (int)input_list_filter_rule->key_value.datetime_type_value);
14219                 break;
14220
14221         default :
14222                 ret = EMAIL_ERROR_INVALID_PARAM;
14223                 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
14224                 break;
14225         }
14226
14227         if(ret == EMAIL_ERROR_NONE && EM_SAFE_STRLEN(key_value_string) > 0) {
14228                 *output_key_value_string = strdup(key_value_string);
14229         }
14230
14231 FINISH_OFF:
14232
14233         EM_SAFE_FREE(temp_key_value_1);
14234         EM_SAFE_FREE(temp_key_value_2);
14235
14236         EM_DEBUG_FUNC_END("ret [%d]", ret);
14237         return ret;
14238 }
14239
14240 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14241 static int _get_cert_password_file_name(int index, char *cert_password_file_name)
14242 {
14243         EM_DEBUG_FUNC_BEGIN("index : [%d]", index);
14244
14245         if (index <= 0 || !cert_password_file_name) {
14246                 EM_DEBUG_EXCEPTION("Invalid parameter");
14247                 return EMAIL_ERROR_INVALID_PARAM;
14248         }
14249
14250         sprintf(cert_password_file_name, ".email_cert_%d", index);
14251
14252         EM_DEBUG_FUNC_END();
14253         return EMAIL_ERROR_NONE;
14254 }
14255 #endif
14256
14257 static int _make_filter_rule_string(email_list_filter_rule_t *input_list_filter_rule, char **output_string)
14258 {
14259         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
14260
14261         int   ret = EMAIL_ERROR_NONE;
14262         int   is_alpha = 0;
14263         int   length_field_name = 0;
14264         int   length_value = 0;
14265         char  result_rule_string[QUERY_SIZE] = { 0 , };
14266         char *mod_field_name_string = NULL;
14267         char *mod_value_string = NULL;
14268         char *temp_field_name_string = NULL;
14269         char *temp_key_value_string = NULL;
14270
14271         if(input_list_filter_rule == NULL || output_string == NULL) {
14272                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14273                 return  EMAIL_ERROR_INVALID_PARAM;
14274         }
14275
14276         temp_field_name_string = _get_mail_field_name_by_attribute_type(input_list_filter_rule->target_attribute);
14277
14278         if(temp_field_name_string == NULL) {
14279                 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
14280                 return EMAIL_ERROR_INVALID_PARAM;
14281         }
14282
14283         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) {
14284                 EM_DEBUG_EXCEPTION("_get_key_value_string_for_list_filter_rule failed");
14285                 return EMAIL_ERROR_INVALID_PARAM;
14286         }
14287
14288         length_field_name = EM_SAFE_STRLEN(temp_field_name_string);
14289         length_value      = EM_SAFE_STRLEN(temp_key_value_string);
14290
14291         switch(input_list_filter_rule->target_attribute) {
14292         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME            :
14293         case EMAIL_MAIL_ATTRIBUTE_SUBJECT                 :
14294         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME     :
14295         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID          :
14296         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID              :
14297         case EMAIL_MAIL_ATTRIBUTE_FROM                    :
14298         case EMAIL_MAIL_ATTRIBUTE_TO                      :
14299         case EMAIL_MAIL_ATTRIBUTE_CC                      :
14300         case EMAIL_MAIL_ATTRIBUTE_BCC                     :
14301         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT            :
14302                 is_alpha = 1;
14303                 break;
14304         default :
14305                 is_alpha = 0;
14306                 break;
14307         }
14308
14309         if(is_alpha == 1 && input_list_filter_rule->case_sensitivity == false) {
14310                 length_field_name += strlen("UPPER() ");
14311                 length_value      += strlen("UPPER() ");
14312                 mod_field_name_string = em_malloc(sizeof(char) * length_field_name);
14313                 mod_value_string      = em_malloc(sizeof(char) * length_value);
14314                 SNPRINTF(mod_field_name_string, length_field_name, "UPPER(%s)", temp_field_name_string);
14315                 SNPRINTF(mod_value_string,      length_value, "UPPER(%s)", temp_key_value_string);
14316                 EM_SAFE_FREE(temp_key_value_string);
14317         }
14318         else {
14319                 mod_field_name_string = strdup(temp_field_name_string);
14320                 mod_value_string      = temp_key_value_string;
14321         }
14322
14323         switch (input_list_filter_rule->rule_type) {
14324         case EMAIL_LIST_FILTER_RULE_EQUAL     :
14325                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = %s ", mod_field_name_string, mod_value_string);
14326                 break;
14327         case EMAIL_LIST_FILTER_RULE_NOT_EQUAL :
14328                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s != %s ", mod_field_name_string, mod_value_string);
14329                 break;
14330         case EMAIL_LIST_FILTER_RULE_LESS_THAN :
14331                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s < %s ", mod_field_name_string, mod_value_string);
14332                 break;
14333         case EMAIL_LIST_FILTER_RULE_GREATER_THAN :
14334                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s > %s ", mod_field_name_string, mod_value_string);
14335                 break;
14336         case EMAIL_LIST_FILTER_RULE_LESS_THAN_OR_EQUAL :
14337                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s <= %s ", mod_field_name_string, mod_value_string);
14338                 break;
14339         case EMAIL_LIST_FILTER_RULE_GREATER_THAN_OR_EQUAL :
14340                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s >= %s ", mod_field_name_string, mod_value_string);
14341                 break;
14342         case EMAIL_LIST_FILTER_RULE_INCLUDE   :
14343                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s LIKE %s ", mod_field_name_string, mod_value_string);
14344                 break;
14345         case EMAIL_LIST_FILTER_RULE_IN        :
14346                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s IN (%s) ", mod_field_name_string, mod_value_string);
14347                 break;
14348         case EMAIL_LIST_FILTER_RULE_NOT_IN    :
14349                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s NOT IN (%s) ", mod_field_name_string, mod_value_string);
14350                 break;
14351         default :
14352                 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
14353                 ret = EMAIL_ERROR_INVALID_PARAM;
14354                 goto FINISH_OFF;
14355         }
14356
14357         *output_string = strdup(result_rule_string);
14358
14359 FINISH_OFF:
14360         EM_SAFE_FREE(mod_field_name_string);
14361         EM_SAFE_FREE(mod_value_string);
14362
14363         EM_DEBUG_FUNC_END("ret [%d]", ret);
14364         return ret;
14365 }
14366
14367 static int _make_order_rule_string(email_list_sorting_rule_t *input_sorting_rule, char **output_string) {
14368         EM_DEBUG_FUNC_BEGIN("input_sorting_rule [%p], output_string [%p]", input_sorting_rule, output_string);
14369
14370         char  result_rule_string[QUERY_SIZE] = { 0 , };
14371         int   ret = EMAIL_ERROR_NONE;
14372
14373         if(input_sorting_rule->force_boolean_check) {
14374                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = 0 ", _get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
14375         }
14376         else
14377                 EM_SAFE_STRCPY(result_rule_string, _get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
14378
14379         switch (input_sorting_rule->sort_order) {
14380                 case EMAIL_SORT_ORDER_ASCEND     :
14381                         EM_SAFE_STRCAT(result_rule_string, " ASC ");
14382                         break;
14383
14384                 case EMAIL_SORT_ORDER_DESCEND    :
14385                         EM_SAFE_STRCAT(result_rule_string, " DESC ");
14386                         break;
14387
14388                 default :
14389                         EM_DEBUG_EXCEPTION("Invalid sort_order [%d]", input_sorting_rule->sort_order);
14390                         ret = EMAIL_ERROR_INVALID_PARAM;
14391                         goto FINISH_OFF;
14392         }
14393
14394         *output_string = strdup(result_rule_string);
14395
14396 FINISH_OFF:
14397         EM_DEBUG_FUNC_END("ret [%d]", ret);
14398         return ret;
14399 }
14400
14401 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)
14402 {
14403         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);
14404         int ret = EMAIL_ERROR_NONE;
14405         int i = 0;
14406         int string_offset = 0;
14407         char conditional_clause_string[QUERY_SIZE] = {0, };
14408         char *result_string_for_a_item = NULL;
14409
14410         if ( (input_filter_count > 0 && !input_filter_list) || (input_sorting_rule_count > 0 && !input_sorting_rule_list) || output_conditional_clause == NULL) {
14411                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14412                 return EMAIL_ERROR_INVALID_PARAM;
14413         }
14414
14415         if(input_filter_count > 0) {
14416                 strcpy(conditional_clause_string,  " WHERE ");
14417
14418                 for ( i = 0; i < input_filter_count; i++) {
14419                         switch (input_filter_list[i].list_filter_item_type) {
14420                         case EMAIL_LIST_FILTER_ITEM_RULE :
14421                                 EM_DEBUG_LOG("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE", i);
14422                                 _make_filter_rule_string(&(input_filter_list[i].list_filter_item.rule), &result_string_for_a_item);
14423                                 break;
14424
14425                         case EMAIL_LIST_FILTER_ITEM_OPERATOR :
14426                                 EM_DEBUG_LOG("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_OPERATOR", i);
14427                                 switch(input_filter_list[i].list_filter_item.operator_type) {
14428                                 case EMAIL_LIST_FILTER_OPERATOR_AND :
14429                                         result_string_for_a_item = strdup("AND ");
14430                                         break;
14431                                 case EMAIL_LIST_FILTER_OPERATOR_OR :
14432                                         result_string_for_a_item = strdup("OR ");
14433                                         break;
14434                                 case EMAIL_LIST_FILTER_OPERATOR_LEFT_PARENTHESIS :
14435                                         result_string_for_a_item = strdup(" (");
14436                                         break;
14437                                 case EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS :
14438                                         result_string_for_a_item = strdup(") ");
14439                                         break;
14440                                 }
14441                                 break;
14442
14443                         default :
14444                                 EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", input_filter_list[i].list_filter_item_type);
14445                                 ret = EMAIL_ERROR_INVALID_PARAM;
14446                                 goto FINISH_OFF;
14447                         }
14448
14449                         if(result_string_for_a_item == NULL) {
14450                                 EM_DEBUG_EXCEPTION("result_string_for_a_item is null");
14451                                 ret = EMAIL_ERROR_INVALID_PARAM;
14452                                 goto FINISH_OFF;
14453                         }
14454
14455                         if(strlen(conditional_clause_string) + EM_SAFE_STRLEN(result_string_for_a_item) >= QUERY_SIZE) { /* prevent 34364 */
14456                                 EM_DEBUG_EXCEPTION("Query is too long");
14457                                 ret = EMAIL_ERROR_DATA_TOO_LONG;
14458                                 goto FINISH_OFF;
14459                         }
14460                         strcat(conditional_clause_string, result_string_for_a_item);
14461                         EM_SAFE_FREE(result_string_for_a_item);
14462                 }
14463         }
14464
14465         if(input_sorting_rule_count > 0) {
14466                 strcat(conditional_clause_string, "ORDER BY ");
14467
14468                 for ( i = 0; i < input_sorting_rule_count; i++) {
14469                         if( (ret = _make_order_rule_string(&input_sorting_rule_list[i], &result_string_for_a_item)) != EMAIL_ERROR_NONE) {
14470                                 EM_DEBUG_EXCEPTION("_make_order_rule_string failed. [%d]", ret);
14471                                 goto FINISH_OFF;
14472                         }
14473                         if(i > 0)
14474                                 strcat(conditional_clause_string, ", ");
14475                         strcat(conditional_clause_string, result_string_for_a_item);
14476                         EM_SAFE_FREE(result_string_for_a_item);
14477                 }
14478         }
14479
14480         if (input_start_index != -1 && input_limit_count != -1) {
14481                 string_offset = strlen(conditional_clause_string);
14482                 SNPRINTF_OFFSET(conditional_clause_string, string_offset, QUERY_SIZE, " LIMIT %d, %d", input_start_index, input_limit_count);
14483         }
14484
14485         *output_conditional_clause = strdup(conditional_clause_string);
14486
14487 FINISH_OFF:
14488         EM_SAFE_FREE(result_string_for_a_item);
14489
14490         EM_DEBUG_FUNC_END("ret [%d]", ret);
14491         return ret;
14492 }
14493
14494 INTERNAL_FUNC int emstorage_free_list_filter(email_list_filter_t **input_filter_list, int input_filter_count)
14495 {
14496         EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d]", input_filter_list, input_filter_count);
14497         int err = EMAIL_ERROR_NONE;
14498         int i = 0;
14499         email_list_filter_t *temp_filter_list = NULL;
14500
14501         EM_IF_NULL_RETURN_VALUE(input_filter_list, EMAIL_ERROR_INVALID_PARAM);
14502
14503         for ( i = 0; i < input_filter_count; i++) {
14504                 temp_filter_list = (*input_filter_list) + i;
14505                 if(temp_filter_list && temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE) {
14506                         switch(temp_filter_list->list_filter_item.rule.target_attribute) {
14507                         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME :
14508                         case EMAIL_MAIL_ATTRIBUTE_SUBJECT :
14509                         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME :
14510                         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID :
14511                         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID :
14512                         case EMAIL_MAIL_ATTRIBUTE_FROM :
14513                         case EMAIL_MAIL_ATTRIBUTE_TO :
14514                         case EMAIL_MAIL_ATTRIBUTE_CC :
14515                         case EMAIL_MAIL_ATTRIBUTE_BCC :
14516                         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN :
14517                         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML :
14518                         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT :
14519                                 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule.key_value.string_type_value);
14520                                 break;
14521                         default :
14522                                 break;
14523                         }
14524                 }
14525         }
14526
14527         free(*input_filter_list);
14528         *input_filter_list = NULL;
14529
14530         EM_DEBUG_FUNC_END("err [%d]", err);
14531         return err;
14532 }
14533
14534 INTERNAL_FUNC int emstorage_add_certificate(emstorage_certificate_tbl_t *certificate, int transaction, int *err_code)
14535 {
14536         EM_DEBUG_FUNC_BEGIN("certificate:[%p], transaction:[%d], err_code:[%p]", certificate, transaction, err_code);
14537
14538         if (!certificate) {
14539                 EM_DEBUG_EXCEPTION("certificate:[%p], transaction:[%d], err_code:[%p]", certificate, transaction, err_code);
14540                 if (err_code != NULL)
14541                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14542
14543                 return false;
14544         }
14545
14546         int rc = -1, ret = false;
14547         int error = EMAIL_ERROR_NONE;
14548         DB_STMT hStmt = NULL;
14549         char sql_query_string[QUERY_SIZE] = {0, };
14550 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14551         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
14552 #endif
14553         sqlite3 *local_db_handle = emstorage_get_db_connection();
14554
14555         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
14556
14557         char *sql = "SELECT max(rowid) FROM mail_certificate_tbl;";
14558         char **result = NULL;
14559
14560         /*  rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
14561         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
14562         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14563                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
14564
14565         if (NULL==result[1]) rc = 1;
14566         else rc = atoi(result[1])+1;
14567         sqlite3_free_table(result);
14568
14569         certificate->certificate_id = rc;
14570 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14571         if ((error = _get_cert_password_file_name(certificate->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
14572                 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
14573                 goto FINISH_OFF;
14574         }
14575 #endif
14576         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14577                 "INSERT INTO mail_certificate_tbl VALUES "
14578                 "(        "
14579                 "   ?  "                /* Index of certificate */
14580                 "  , ? "                /* Select the account */
14581                 "  , ? "                /* Year of issue */
14582                 "  , ? "                /* Month of issue */
14583                 "  , ? "                /* Day of issue */
14584                 "  , ? "                /* Year of expiration */
14585                 "  , ? "                /* Month of expiration */
14586                 "  , ? "                /* Day of expiration */
14587                 "  , ? "                /* Organization of issue */
14588                 "  , ? "                /* Email address */
14589                 "  , ? "                /* Subject of certificate */
14590                 "  , ? "                /* Name of saved certificate */
14591                 ") ");
14592
14593
14594         /*  rc = sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL); */
14595         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14596         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14597                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14598
14599         EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
14600         _bind_stmt_field_data_int(hStmt, CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->certificate_id);
14601         _bind_stmt_field_data_int(hStmt, ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_year);
14602         _bind_stmt_field_data_int(hStmt, ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_month);
14603         _bind_stmt_field_data_int(hStmt, ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_day);
14604         _bind_stmt_field_data_int(hStmt, EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_year);
14605         _bind_stmt_field_data_int(hStmt, EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_month);
14606         _bind_stmt_field_data_int(hStmt, EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_day);
14607         _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);
14608         _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->email_address, 0, EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL);
14609         _bind_stmt_field_data_string(hStmt, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->subject_str, 0, SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL);
14610         _bind_stmt_field_data_string(hStmt, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->filepath, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
14611         /*  rc = sqlite3_step(hStmt); */
14612         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14613
14614         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
14615                 ("sqlite3_step fail:%d", rc));
14616         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14617                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
14618 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14619         if (ssm_write_buffer(certificate->password, EM_SAFE_STRLEN(certificate->password), cert_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
14620                 EM_DEBUG_EXCEPTION("ssm_write_buffer failed - Private certificate password : [%s]", cert_password_file_name);
14621                 error = EMAIL_ERROR_SYSTEM_FAILURE;
14622                 goto FINISH_OFF;
14623         }
14624 #endif
14625         ret = true;
14626
14627         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
14628
14629         if (!emcore_notify_storage_event(NOTI_CERTIFICATE_ADD, certificate->certificate_id, 0, NULL, 0))
14630                 EM_DEBUG_EXCEPTION("emcore_notify_storage_event(NOTI_CERTIFICATE_ADD] : Notification failed");
14631
14632 FINISH_OFF:
14633
14634         if (hStmt != NULL)  {
14635                 rc = sqlite3_finalize(hStmt);
14636                 if (rc != SQLITE_OK)  {
14637                         EM_DEBUG_LOG("sqlite3_finalize failed - %d", rc);
14638                         error = EMAIL_ERROR_DB_FAILURE;
14639                 }
14640         }
14641         else
14642                 EM_DEBUG_LOG("hStmt is NULL!!!");
14643
14644         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
14645         _DISCONNECT_DB;
14646
14647         if (err_code != NULL)
14648                 *err_code = error;
14649
14650         EM_DEBUG_FUNC_END("ret [%d]", ret);
14651         return ret;
14652 }
14653
14654 INTERNAL_FUNC int emstorage_free_certificate(emstorage_certificate_tbl_t **certificate_list, int count, int *err_code)
14655 {
14656         EM_DEBUG_FUNC_BEGIN("certificate_list[%p], count[%d], err_code[%p]", certificate_list, count, err_code);
14657
14658         int ret = false;
14659         int error = EMAIL_ERROR_NONE;
14660
14661         if (count > 0)  {
14662                 if (!certificate_list || !*certificate_list)  {
14663                         EM_DEBUG_EXCEPTION("certificate_list[%p], count[%d]", certificate_list, count);
14664                         error = EMAIL_ERROR_INVALID_PARAM;
14665                         goto FINISH_OFF;
14666                 }
14667
14668                 emstorage_certificate_tbl_t *p = *certificate_list;
14669                 int i = 0;
14670
14671                 for (; i < count; i++)  {
14672                         EM_SAFE_FREE(p[i].issue_organization_name);
14673                         EM_SAFE_FREE(p[i].email_address);
14674                         EM_SAFE_FREE(p[i].subject_str);
14675                         EM_SAFE_FREE(p[i].filepath);
14676                         EM_SAFE_FREE(p[i].password);
14677                 }
14678
14679                 EM_SAFE_FREE(p);
14680                 *certificate_list = NULL;
14681         }
14682
14683         ret = true;
14684
14685 FINISH_OFF:
14686         if (err_code != NULL)
14687                 *err_code = error;
14688
14689         EM_DEBUG_FUNC_END("ret [%d]", ret);
14690         return ret;
14691 }
14692
14693 INTERNAL_FUNC int emstorage_get_certificate_list(int *select_num, emstorage_certificate_tbl_t **certificate_list, int transaction, int with_password, int *err_code)
14694 {
14695         EM_DEBUG_FUNC_BEGIN();
14696
14697         int i = 0, count = 0, rc = -1, ret = false;
14698         int error = EMAIL_ERROR_NONE;
14699         emstorage_certificate_tbl_t *p_data_tbl = NULL;
14700
14701         DB_STMT hStmt = NULL;
14702
14703         if (!select_num || !certificate_list)  {
14704                 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, certificate_list);
14705                 if (err_code != NULL)
14706                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14707                 return false;
14708         }
14709
14710         sqlite3 *local_db_handle = emstorage_get_db_connection();
14711         EMSTORAGE_START_READ_TRANSACTION(transaction);
14712
14713         char sql_query_string[QUERY_SIZE] = {0, };
14714         char *sql = "SELECT count(*) FROM mail_certificate_tbl;";
14715         char **result;
14716
14717         /*  rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
14718         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
14719         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14720                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
14721
14722         count = atoi(result[1]);
14723         sqlite3_free_table(result);
14724
14725         if (!count) {
14726                 EM_DEBUG_EXCEPTION("no account found...");
14727                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
14728                 ret = true;
14729                 goto FINISH_OFF;
14730         }
14731         EM_DEBUG_LOG("count = %d", rc);
14732         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_certificate_tbl ORDER BY account_id");
14733
14734         /*  rc = sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL);   */
14735         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14736
14737         EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
14738         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14739                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14740
14741         /*  rc = sqlite3_step(hStmt); */
14742         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14743         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14744                 ("sqlite3_step fail:%d", rc));
14745         if (rc == SQLITE_DONE)  {
14746                 EM_DEBUG_EXCEPTION("no account found...");
14747
14748                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
14749                 count = 0;
14750                 ret = true;
14751                 goto FINISH_OFF;
14752         }
14753
14754         if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t) * count)))  {
14755                 EM_DEBUG_EXCEPTION("malloc failed...");
14756                 error = EMAIL_ERROR_OUT_OF_MEMORY;
14757                 goto FINISH_OFF;
14758         }
14759         memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t) * count);
14760         for (i = 0; i < count; i++)  {
14761                 /*  get recordset */
14762                 _get_stmt_field_data_int(hStmt,  &(p_data_tbl[i].certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
14763                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
14764                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
14765                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
14766                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
14767                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
14768                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
14769                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
14770                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
14771                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
14772                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
14773                 if (with_password == true) {
14774 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14775                         /*  get password from the secure storage */
14776                         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
14777
14778                         EM_SAFE_FREE(p_data_tbl[i].password);
14779
14780                         /*  get password file name */
14781                         if ((error = _get_cert_password_file_name(p_data_tbl[i].certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
14782                                 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
14783                                 goto FINISH_OFF;
14784                         }
14785
14786                         /*  read password from secure storage */
14787                         if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl[i].password))) < 0) {
14788                                 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage() failed...");
14789                                 goto FINISH_OFF;
14790                         }
14791                         EM_DEBUG_LOG("recv_password_file_name[%s], password[%s]", cert_password_file_name,  p_data_tbl[i].password);
14792 #endif
14793                 }
14794
14795                 /*  rc = sqlite3_step(hStmt); */
14796                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14797                 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i,  rc);
14798                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14799                         ("sqlite3_step fail:%d", rc));
14800         }
14801
14802         ret = true;
14803
14804 FINISH_OFF:
14805         if (ret == true)  {
14806                 *certificate_list = p_data_tbl;
14807                 *select_num = count;
14808                 EM_DEBUG_LOG("COUNT : %d", count);
14809         }
14810         else if (p_data_tbl != NULL)
14811                 emstorage_free_certificate(&p_data_tbl, count, NULL);
14812         if (hStmt != NULL)  {
14813                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
14814
14815                 rc = sqlite3_finalize(hStmt);
14816                 hStmt = NULL;
14817                 if (rc != SQLITE_OK)  {
14818                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
14819                         error = EMAIL_ERROR_DB_FAILURE;
14820                 }
14821         }
14822
14823         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14824         _DISCONNECT_DB;
14825
14826         if (err_code != NULL)
14827                 *err_code = error;
14828
14829         EM_DEBUG_FUNC_END("ret [%d]", ret);
14830         return ret;
14831 }
14832
14833 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)
14834 {
14835         EM_DEBUG_FUNC_BEGIN("email_address[%s], certificate[%p], transaction[%d], err_code[%p]", email_address, certificate, transaction, err_code);
14836
14837         if (!certificate)  {
14838                 EM_DEBUG_EXCEPTION("email_address[%s], certificate[%p]", email_address, certificate);
14839                 if (err_code != NULL)
14840                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14841                 return false;
14842         }
14843
14844         int ret = false;
14845         int error = EMAIL_ERROR_NONE;
14846         emstorage_certificate_tbl_t *p_data_tbl = NULL;
14847         DB_STMT hStmt = NULL;
14848         char sql_query_string[QUERY_SIZE] = {0, };
14849         int rc = -1;
14850         int sql_len = 0;
14851 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14852         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
14853 #endif
14854         sqlite3 *local_db_handle = emstorage_get_db_connection();
14855         EMSTORAGE_START_READ_TRANSACTION(transaction);
14856
14857         /*  Make query string */
14858         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
14859         sql_len = EM_SAFE_STRLEN(sql_query_string);
14860
14861         /*  dummy value, FROM WHERE clause */
14862         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "* FROM mail_certificate_tbl WHERE email_address = '%s'", email_address);
14863
14864         /*  FROM clause */
14865         EM_DEBUG_LOG("query = [%s]", sql_query_string);
14866
14867         /*  execute a sql and count rows */
14868         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14869         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14870                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14871
14872         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14873         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14874                 ("sqlite3_step fail:%d", rc));
14875
14876         if (rc == SQLITE_DONE)  {
14877                 EM_DEBUG_EXCEPTION("no matched certificate found...");
14878                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
14879                 goto FINISH_OFF;
14880         }
14881
14882         /*  Assign query result to structure */
14883         if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t))))  {
14884                 EM_DEBUG_EXCEPTION("malloc failed...");
14885                 error = EMAIL_ERROR_OUT_OF_MEMORY;
14886                 goto FINISH_OFF;
14887         }
14888
14889         memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t));
14890         _get_stmt_field_data_int(hStmt,  &(p_data_tbl->certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
14891         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
14892         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
14893         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
14894         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
14895         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
14896         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
14897         _get_stmt_field_data_string(hStmt, &(p_data_tbl->issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
14898         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
14899         _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
14900         _get_stmt_field_data_string(hStmt, &(p_data_tbl->filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
14901
14902         if (with_password) {
14903 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14904                 /*  get password file name */
14905                 if ((error = _get_cert_password_file_name(p_data_tbl->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
14906                         EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
14907                         goto FINISH_OFF;
14908                 }
14909
14910                 /*  read password from secure storage */
14911                 if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl->password))) != EMAIL_ERROR_NONE) {
14912                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed...");
14913                         goto FINISH_OFF;
14914                 }
14915                 EM_DEBUG_LOG("cert_password_file_name[%s], password[%s]", cert_password_file_name,  p_data_tbl->password);
14916 #endif
14917         }
14918         ret = true;
14919
14920 FINISH_OFF:
14921         if (ret == true)
14922                 *certificate = p_data_tbl;
14923
14924         if (hStmt != NULL)  {
14925                 rc = sqlite3_finalize(hStmt);
14926                 if (rc != SQLITE_OK)  {
14927                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
14928                         error = EMAIL_ERROR_DB_FAILURE;
14929                 }
14930         }
14931
14932         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14933         _DISCONNECT_DB;
14934
14935         if (err_code != NULL)
14936                 *err_code = error;
14937
14938         EM_DEBUG_FUNC_END("ret [%d]", ret);
14939         return ret;
14940 }
14941
14942 INTERNAL_FUNC int emstorage_get_certificate_by_index(int index, emstorage_certificate_tbl_t **certificate, int transaction, int with_password, int *err_code)
14943 {
14944         EM_DEBUG_FUNC_BEGIN("index[%d], certificate[%p], transaction[%d], err_code[%p]", index, certificate, transaction, err_code);
14945
14946         if (!certificate)  {
14947                 EM_DEBUG_EXCEPTION("index[%d], account[%p]", index, certificate);
14948                 if (err_code != NULL)
14949                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14950                 return false;
14951         }
14952
14953         int ret = false;
14954         int error = EMAIL_ERROR_NONE;
14955         emstorage_certificate_tbl_t *p_data_tbl = NULL;
14956         DB_STMT hStmt = NULL;
14957         char sql_query_string[QUERY_SIZE] = {0, };
14958         int rc = -1;
14959         int sql_len = 0;
14960 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14961         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
14962 #endif
14963         sqlite3 *local_db_handle = emstorage_get_db_connection();
14964         EMSTORAGE_START_READ_TRANSACTION(transaction);
14965
14966         /*  Make query string */
14967         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
14968         sql_len = EM_SAFE_STRLEN(sql_query_string);
14969
14970         /*  dummy value, FROM WHERE clause */
14971         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "* FROM mail_certificate_tbl WHERE certificate_id = %d", index);
14972
14973         /*  FROM clause */
14974         EM_DEBUG_LOG("query = [%s]", sql_query_string);
14975
14976         /*  execute a sql and count rows */
14977         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14978         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14979                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14980
14981         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14982         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14983                 ("sqlite3_step fail:%d", rc));
14984
14985         if (rc == SQLITE_DONE)  {
14986                 EM_DEBUG_EXCEPTION("no matched certificate found...");
14987                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
14988                 goto FINISH_OFF;
14989         }
14990
14991         /*  Assign query result to structure */
14992         if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t))))  {
14993                 EM_DEBUG_EXCEPTION("malloc failed...");
14994                 error = EMAIL_ERROR_OUT_OF_MEMORY;
14995                 goto FINISH_OFF;
14996         }
14997
14998         memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t));
14999
15000         _get_stmt_field_data_int(hStmt,  &(p_data_tbl->certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
15001         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
15002         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
15003         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
15004         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
15005         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
15006         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
15007         _get_stmt_field_data_string(hStmt, &(p_data_tbl->issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
15008         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
15009         _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
15010         _get_stmt_field_data_string(hStmt, &(p_data_tbl->filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
15011
15012         if (with_password) {
15013 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15014                 /*  get password file name */
15015                 if ((error = _get_cert_password_file_name(p_data_tbl->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
15016                         EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
15017                         goto FINISH_OFF;
15018                 }
15019
15020                 /*  read password from secure storage */
15021                 if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl->password))) != EMAIL_ERROR_NONE) {
15022                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed...");
15023                         goto FINISH_OFF;
15024                 }
15025                 EM_DEBUG_LOG("cert_password_file_name[%s], password[%s]", cert_password_file_name,  p_data_tbl->password);
15026 #endif
15027         }
15028         ret = true;
15029
15030 FINISH_OFF:
15031         if (ret == true)
15032                 *certificate = p_data_tbl;
15033
15034         if (hStmt != NULL)  {
15035                 rc = sqlite3_finalize(hStmt);
15036                 if (rc != SQLITE_OK)  {
15037                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
15038                         error = EMAIL_ERROR_DB_FAILURE;
15039                 }
15040         }
15041
15042         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15043         _DISCONNECT_DB;
15044
15045         if (err_code != NULL)
15046                 *err_code = error;
15047
15048         EM_DEBUG_FUNC_END("ret [%d]", ret);
15049         return ret;
15050 }
15051
15052 INTERNAL_FUNC int emstorage_delete_certificate(int certificate_id, int transaction, int *err_code)
15053 {
15054         EM_DEBUG_FUNC_BEGIN("certificate_id[%d], transaction[%d], err_code[%p]", certificate_id, transaction, err_code);
15055
15056         if (certificate_id < 1)  {
15057                 EM_DEBUG_EXCEPTION(" certificate_id[%d]", certificate_id);
15058
15059                 if (err_code != NULL)
15060                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15061                 return false;
15062         }
15063
15064         int rc = -1, ret = false;
15065         int error = EMAIL_ERROR_NONE;
15066         sqlite3 *local_db_handle = emstorage_get_db_connection();
15067         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
15068
15069         /*  TODO : delete password files - file names can be obtained from db or a rule that makes a name */
15070         DB_STMT hStmt = NULL;
15071         char sql_query_string[QUERY_SIZE] = {0, };
15072 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15073         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
15074         /*  get password file name */
15075         if ((error = _get_cert_password_file_name(certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
15076                 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
15077                 goto FINISH_OFF;
15078         }
15079 #endif
15080         /*  delete from db */
15081         memset(sql_query_string, 0x00, sizeof(sql_query_string));
15082         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_certificate_tbl WHERE certificate_id = %d", certificate_id);
15083
15084         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
15085         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
15086                 ("sqlite3_exec fail:%d", rc));
15087         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15088                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15089
15090         /*  validate account existence */
15091         rc = sqlite3_changes(local_db_handle);
15092         if (rc == 0)  {
15093                 EM_DEBUG_EXCEPTION(" no matched certificate found...");
15094                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
15095                 goto FINISH_OFF;
15096         }
15097 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15098         /*  delete from secure storage */
15099         if (ssm_delete_file(cert_password_file_name,  SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
15100                 EM_DEBUG_EXCEPTION(" ssm_delete_file failed -cert password : file[%s]", cert_password_file_name);
15101                 error = EMAIL_ERROR_SYSTEM_FAILURE;
15102                 goto FINISH_OFF;
15103         }
15104 #endif
15105         ret = true;
15106
15107 FINISH_OFF:
15108
15109         if (hStmt != NULL)  {
15110                 rc = sqlite3_finalize(hStmt);
15111                 if (rc != SQLITE_OK)  {
15112                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
15113                         error = EMAIL_ERROR_DB_FAILURE;
15114                 }
15115         }
15116
15117         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
15118         _DISCONNECT_DB;
15119
15120         if (err_code != NULL)
15121                 *err_code = error;
15122
15123         EM_DEBUG_FUNC_END("ret [%d]", ret);
15124         return ret;
15125 }
15126
15127 INTERNAL_FUNC int emstorage_update_certificate(int certificate_id, emstorage_certificate_tbl_t *certificate, int transaction, int *err_code)
15128 {
15129         EM_DEBUG_FUNC_BEGIN("certificate_id[%d], certificate[%p], transaction[%d], err_code[%p]", certificate_id, certificate, transaction, err_code);
15130
15131         if (certificate_id < 1)  {
15132                 EM_DEBUG_EXCEPTION(" certificate_id[%d]", certificate_id);
15133
15134                 if (err_code != NULL)
15135                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15136                 return false;
15137         }
15138
15139         int error = EMAIL_ERROR_NONE;
15140         int rc, ret = false;
15141
15142         DB_STMT hStmt = NULL;
15143         char sql_query_string[QUERY_SIZE] = {0, };
15144 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15145         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
15146 #endif
15147         sqlite3 *local_db_handle = emstorage_get_db_connection();
15148         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
15149
15150         SNPRINTF(sql_query_string, sizeof(sql_query_string),
15151                 "UPDATE mail_certificate_tbl SET"
15152                 " issue_year = ?"
15153                 ", issue_month = ?"             /* Index of certificate */
15154                 ", issue_day = ?"               /* Select the account */
15155                 ", expiration_year = ?"         /* Year of issue */
15156                 ", expiration_month = ?"                /* Month of issue */
15157                 ", expiration_day = ?"          /* Day of issue */
15158                 ", issue_organization_name = ?"         /* Year of expiration */
15159                 ", email_address = ?"           /* Month of expiration */
15160                 ", subject_str = ?"             /* Day of expiration */
15161                 ", filepath = ?"                /* Organization of issue */
15162                 ", password = ?"
15163                 " WHERE certificate_id = ?");
15164
15165         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15166         EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
15167         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15168                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15169
15170         _bind_stmt_field_data_int(hStmt, ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_year);
15171         _bind_stmt_field_data_int(hStmt, ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_month);
15172         _bind_stmt_field_data_int(hStmt, ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_day);
15173         _bind_stmt_field_data_int(hStmt, EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_year);
15174         _bind_stmt_field_data_int(hStmt, EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_month);
15175         _bind_stmt_field_data_int(hStmt, EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_day);
15176         _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);
15177         _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->email_address, 0, EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL);
15178         _bind_stmt_field_data_string(hStmt, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->subject_str, 0, SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL);
15179         _bind_stmt_field_data_string(hStmt, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->filepath, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
15180
15181         /*  rc = sqlite3_step(hStmt); */
15182         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15183         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
15184                 ("sqlite3_step fail:%d", 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         /*  validate account existence */
15189         rc = sqlite3_changes(local_db_handle);
15190         if (rc == 0) {
15191                 EM_DEBUG_EXCEPTION(" no matched account found...");
15192
15193                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
15194                 goto FINISH_OFF;
15195         }
15196
15197 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15198         /*  get password file name */
15199         if ((error = _get_cert_password_file_name(certificate->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
15200                 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
15201                 goto FINISH_OFF;
15202         }
15203
15204         /*  save passwords to the secure storage */
15205         if (ssm_write_buffer(certificate->password, EM_SAFE_STRLEN(certificate->password), cert_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
15206                 EM_DEBUG_EXCEPTION("ssm_write_buffer failed - Private certificate password : [%s]", cert_password_file_name);
15207                 error = EMAIL_ERROR_SYSTEM_FAILURE;
15208                 goto FINISH_OFF;
15209         }
15210 #endif
15211         if (!emcore_notify_storage_event(NOTI_CERTIFICATE_UPDATE, certificate->certificate_id, 0, NULL, 0))
15212                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_CERTIFICATE_UPDATE] : Notification Failed >>> ");
15213
15214         ret = true;
15215
15216 FINISH_OFF:
15217
15218         if (hStmt != NULL)  {
15219                 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
15220
15221                 rc = sqlite3_finalize(hStmt);
15222                 if (rc != SQLITE_OK)  {
15223                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
15224                         error = EMAIL_ERROR_DB_FAILURE;
15225                 }
15226         }
15227
15228         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
15229
15230         _DISCONNECT_DB;
15231
15232         if (err_code != NULL)
15233                 *err_code = error;
15234
15235         EM_DEBUG_FUNC_END("ret [%d]", ret);
15236         return ret;
15237 }
15238
15239 /* Tasks --------------------------------------------------------------------------*/
15240 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)
15241 {
15242         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);
15243         int ret = 0;
15244         int i = 0;
15245         int task_id = 0;
15246         int err = EMAIL_ERROR_NONE;
15247         int rc = -1;
15248         DB_STMT hStmt = NULL;
15249         char sql_query_string[QUERY_SIZE] = {0, };
15250         sqlite3 *local_db_handle = NULL;
15251         char *sql = "SELECT max(rowid) FROM mail_task_tbl;";
15252         char **result = NULL;
15253
15254         if (input_task_parameter == NULL || output_task_id == NULL)  {
15255                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15256                 err = EMAIL_ERROR_INVALID_PARAM;
15257                 goto FINISH_OFF;
15258         }
15259
15260         local_db_handle = emstorage_get_db_connection();
15261
15262         EMSTORAGE_START_WRITE_TRANSACTION(input_transaction, err);
15263
15264         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
15265         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
15266                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
15267
15268         if (NULL == result[1])
15269                 task_id = 1;
15270         else
15271                 task_id = atoi(result[1])+1;
15272
15273         *output_task_id = task_id;
15274
15275         sqlite3_free_table(result);
15276         result = NULL;
15277
15278         SNPRINTF(sql_query_string, sizeof(sql_query_string),
15279                 "INSERT INTO mail_task_tbl VALUES "
15280                 "(        "
15281                 "    ? "  /*   task_id */
15282                 "  , ? "  /*   task_type */
15283                 "  , ? "  /*   task_status */
15284                 "  , ? "  /*   task_priority */
15285                 "  , ? "  /*   task_parameter_length */
15286                 "  , ? "  /*   task_parameter */
15287                 "  , ? "  /*   date_time */
15288                 ") ");
15289
15290         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15291         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15292                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15293
15294         EM_DEBUG_LOG(">>>> SQL STMT [%s] ", sql_query_string);
15295
15296
15297         _bind_stmt_field_data_int(hStmt, i++, task_id);
15298         _bind_stmt_field_data_int(hStmt, i++, input_task_type);
15299         _bind_stmt_field_data_int(hStmt, i++, EMAIL_TASK_STATUS_WAIT);
15300         _bind_stmt_field_data_int(hStmt, i++, input_task_priority);
15301         _bind_stmt_field_data_int(hStmt, i++, input_task_parameter_length);
15302         _bind_stmt_field_data_blob(hStmt, i++, input_task_parameter, input_task_parameter_length);
15303         _bind_stmt_field_data_int(hStmt, i++, time(NULL));
15304
15305         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15306
15307         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {err = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
15308                 ("sqlite3_step fail:%d", rc));
15309         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15310                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
15311
15312         ret = (err == EMAIL_ERROR_NONE);
15313         EMSTORAGE_FINISH_WRITE_TRANSACTION(input_transaction, ret, err);
15314
15315 FINISH_OFF:
15316
15317         if (hStmt != NULL)  {
15318                 rc = sqlite3_finalize(hStmt);
15319                 if (rc != SQLITE_OK)  {
15320                         EM_DEBUG_LOG("sqlite3_finalize failed [%d]", rc);
15321                         err = EMAIL_ERROR_DB_FAILURE;
15322                 }
15323         }
15324         else
15325                 EM_DEBUG_LOG("hStmt is NULL!!!");
15326
15327         _DISCONNECT_DB;
15328
15329         EM_DEBUG_FUNC_END("err [%d]", err);
15330         return err;
15331 }
15332
15333 INTERNAL_FUNC int emstorage_delete_task(int task_id, int transaction)
15334 {
15335         EM_DEBUG_FUNC_BEGIN("task_id[%d], transaction[%d]", task_id, transaction);
15336         int rc, ret = false;
15337         int err = EMAIL_ERROR_NONE;
15338         char sql_query_string[QUERY_SIZE] = {0, };
15339         sqlite3 *local_db_handle = NULL;
15340
15341         if (task_id < 0)  {
15342                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15343                 err = EMAIL_ERROR_INVALID_PARAM;
15344                 goto FINISH_OFF;
15345         }
15346
15347         local_db_handle = emstorage_get_db_connection();
15348
15349         EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
15350
15351         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_task_tbl WHERE task_id = %d", task_id);
15352
15353         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
15354         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15355                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15356
15357         ret = true;
15358
15359 FINISH_OFF:
15360         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
15361         _DISCONNECT_DB;
15362
15363         EM_DEBUG_FUNC_END("err [%d]", err);
15364         return err;
15365 }
15366
15367 INTERNAL_FUNC int emstorage_update_task_status(int task_id, email_task_status_type_t task_status, int transaction)
15368 {
15369         EM_DEBUG_FUNC_BEGIN("task_id[%d] task_status[%d] transaction[%d]", task_id, task_status, transaction);
15370         int rc, ret = false;
15371         int err = EMAIL_ERROR_NONE;
15372         char sql_query_string[QUERY_SIZE] = {0, };
15373
15374         sqlite3 *local_db_handle = emstorage_get_db_connection();
15375
15376         EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
15377
15378         SNPRINTF(sql_query_string, sizeof(sql_query_string),
15379                 "UPDATE mail_task_tbl SET"
15380                 " task_status = %d"
15381                 " WHERE task_id = %d"
15382                 , task_status
15383                 , task_id);
15384         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
15385         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15386                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15387
15388         ret = true;
15389
15390 FINISH_OFF:
15391         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
15392         _DISCONNECT_DB;
15393
15394
15395         EM_DEBUG_FUNC_END("err [%d]", err);
15396         return err;
15397 }
15398
15399 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)
15400 {
15401         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);
15402         int i = 0, count = 0, rc = -1;
15403         int cur_query = 0;
15404         int field_index = 0;
15405         int err = EMAIL_ERROR_NONE;
15406         email_task_t *task_item_from_tbl = NULL;
15407         char sql_query_string[QUERY_SIZE] = {0, };
15408         char *field_list = "task_id, task_type, task_status, task_priority, task_parameter_length, task_parameter ";
15409         char **result;
15410         sqlite3 *local_db_handle = NULL;
15411         DB_STMT hStmt = NULL;
15412
15413         EM_IF_NULL_RETURN_VALUE(input_conditional_clause, false);
15414         EM_IF_NULL_RETURN_VALUE(output_task_count, false);
15415
15416         local_db_handle = emstorage_get_db_connection();
15417
15418         SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, "SELECT COUNT(*) FROM mail_task_tbl %s", input_conditional_clause);
15419         EM_DEBUG_LOG("emstorage_query_mail_list : query[%s].", sql_query_string);
15420
15421         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
15422         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
15423                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15424
15425         count = atoi(result[1]);
15426         sqlite3_free_table(result);
15427
15428         EM_DEBUG_LOG("count = %d", rc);
15429
15430         if (count == 0) {
15431                 EM_DEBUG_EXCEPTION("no task found...");
15432                 err = EMAIL_ERROR_TASK_NOT_FOUND;
15433                 goto FINISH_OFF;
15434         }
15435
15436         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);
15437         EM_DEBUG_LOG("emstorage_query_mail_list : query[%s].", sql_query_string);
15438
15439         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15440
15441         EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
15442         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15443                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15444
15445         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15446         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15447                 ("sqlite3_step fail:%d", rc));
15448
15449         if (rc == SQLITE_DONE)  {
15450                 EM_DEBUG_EXCEPTION("no task found...");
15451                 err = EMAIL_ERROR_TASK_NOT_FOUND;
15452                 count = 0;
15453                 goto FINISH_OFF;
15454         }
15455
15456         if (!(task_item_from_tbl = (email_task_t*)em_malloc(sizeof(email_task_t) * count))) {
15457                 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
15458                 err = EMAIL_ERROR_OUT_OF_MEMORY;
15459                 goto FINISH_OFF;
15460         }
15461
15462         for (i = 0; i < count; i++)  {
15463                 /*  get recordset */
15464                 field_index = 0;
15465
15466                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_id), field_index++);
15467                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_type), field_index++);
15468                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_status), field_index++);
15469                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_priority), field_index++);
15470                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_parameter_length), field_index++);
15471                 _get_stmt_field_data_blob(hStmt, (void**)&(task_item_from_tbl[i].task_parameter), field_index++);
15472
15473                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15474                 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i,  rc);
15475                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15476                         ("sqlite3_step fail:%d", rc));
15477         }
15478
15479 FINISH_OFF:
15480         if (err == EMAIL_ERROR_NONE)  {
15481                 if (output_task_list)
15482                         *output_task_list = task_item_from_tbl;
15483                 *output_task_count = count;
15484         }
15485         else
15486                 EM_SAFE_FREE(task_item_from_tbl);
15487
15488         if (hStmt != NULL)  {
15489                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
15490                 rc = sqlite3_finalize(hStmt);
15491                 hStmt = NULL;
15492                 if (rc != SQLITE_OK)  {
15493                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
15494                         err = EMAIL_ERROR_DB_FAILURE;
15495                 }
15496         }
15497
15498         _DISCONNECT_DB;
15499
15500         EM_DEBUG_FUNC_END("err [%d]", err);
15501         return err;
15502 }
15503 /* Tasks --------------------------------------------------------------------------*/
15504
15505 /*EOF*/