2.0_alpha release commit
[framework/messaging/email-service.git] / email-core / email-storage / email-storage.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2000 - 2011 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 <dbus/dbus.h> 
51 #include <db-util.h>
52
53 #define __USE_UNIX98
54 #define __USE_GNU
55 #include <pthread.h>
56
57 #include "email-internal-types.h"
58 #include "email-convert.h"
59 #include "email-core-utils.h"
60 #include "email-utilities.h"
61 #include "email-storage.h"
62 #include "email-debug-log.h"
63 #include "email-types.h"
64 #include "email-convert.h"
65
66 #define DB_STMT sqlite3_stmt *
67
68 #define SETTING_MEMORY_TEMP_FILE_PATH "/tmp/email_tmp_file"
69
70 #define EMAIL_STORAGE_CHANGE_NOTI       "User.Email.StorageChange"
71 #define EMAIL_NETOWRK_CHANGE_NOTI       "User.Email.NetworkStatus"
72 #define EMAIL_RESPONSE_TO_API_NOTI      "User.Email.ResponseToAPI"
73
74 #define CONTENT_DATA                  "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset="
75 #define CONTENT_TYPE_DATA             "<meta http-equiv=\"Content-Type\" content=\"text/html; charset="
76
77 #define FLAG_SEEN       0x01
78 #define FLAG_DELETED    0x02
79 #define FLAG_FLAGGED    0x04
80 #define FLAG_ANSWERED   0x08
81 #define FLAG_OLD        0x10
82 #define FLAG_DRAFT      0x20
83
84 #undef close
85
86 #define ISSUE_ORGANIZATION_LEN_IN_MAIL_CERTIFICATE_TBL  256
87 #define EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL       128
88 #define SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL      1027
89 #define FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL           256
90
91 #define ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL            50
92 #define RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL   50
93 #define EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL              128
94 #define USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL               50
95 #define PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL                50
96 #define SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL     50
97 #define SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL            50
98 #define SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL        50
99 #define DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL            30
100 #define REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL           128
101 #define RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL             128
102 #define LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL          256
103 #define DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL       256
104 #define SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL               256
105 #define MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL                128
106 #define ALIAS_LEN_IN_MAIL_BOX_TBL                       128
107 #define LOCAL_MBOX_LEN_IN_MAIL_READ_MAIL_UID_TBL        128
108 #define MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL      128
109 #define S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL             128
110 #define DATA2_LEN_IN_MAIL_READ_MAIL_UID_TBL             256
111 #define VALUE_LEN_IN_MAIL_RULE_TBL                      256
112 #define DEST_MAILBOX_LEN_IN_MAIL_RULE_TBL               128
113 #define MAILBOX_NAME_LEN_IN_MAIL_ATTACHMENT_TBL         128
114 #define ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL      256
115 #define ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL      256
116 #define CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL           256
117 #define ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL 128
118 #define MAILBOX_LEN_IN_MAIL_TBL                         128
119 #define SERVER_MAILBOX_LEN_IN_MAIL_TBL                  128
120 #define SERVER_MAIL_ID_LEN_IN_MAIL_TBL                  128
121 #define FROM_LEN_IN_MAIL_TBL                            256
122 #define SENDER_LEN_IN_MAIL_TBL                          256
123 #define REPLY_TO_LEN_IN_MAIL_TBL                        256
124 #define TO_LEN_IN_MAIL_TBL                              3999
125 #define CC_LEN_IN_MAIL_TBL                              3999
126 #define BCC_LEN_IN_MAIL_TBL                             3999
127 #define RETURN_PATH_LEN_IN_MAIL_TBL                     3999
128 #define SUBJECT_LEN_IN_MAIL_TBL                         1027
129 #define THREAD_TOPIC_LEN_IN_MAIL_TBL                    256
130 #define TEXT_1_LEN_IN_MAIL_TBL                          256
131 #define TEXT_2_LEN_IN_MAIL_TBL                          256
132 #define MIME_ENTITY_LEN_IN_MAIL_TBL                     256
133 #define DATETIME_LEN_IN_MAIL_TBL                        128
134 #define MESSAGE_ID_LEN_IN_MAIL_TBL                      256
135 #define FROM_CONTACT_NAME_LEN_IN_MAIL_TBL               3999
136 #define FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL              3999
137 #define TO_CONTACT_NAME_LEN_IN_MAIL_TBL                 3999
138 #define TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL                3999
139 #define MAILBOX_LEN_IN_MAIL_MEETING_TBL                 128
140 #define LOCATION_LEN_IN_MAIL_MEETING_TBL                1024
141 #define GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL        512
142 #define STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL           32
143 #define DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL           32
144 #define PREVIEWBODY_LEN_IN_MAIL_TBL                     512
145 #define CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL        256 
146
147 /*  this define is used for query to change data (delete, insert, update) */
148 #define EMSTORAGE_START_WRITE_TRANSACTION(transaction_flag, error_code) \
149         if (transaction_flag)\
150         {\
151                 _timedlock_shm_mutex(&mapped_for_db_lock, 2);\
152                 if (emstorage_begin_transaction(NULL, NULL, &error_code) == false) \
153                 {\
154                         EM_DEBUG_EXCEPTION("emstorage_begin_transaction() error[%d]", error_code);\
155                         goto FINISH_OFF;\
156                 }\
157         }
158
159 /*  this define is used for query to change data (delete, insert, update) */
160 #define EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction_flag, result_code, error_code) \
161         if (transaction_flag)\
162         {\
163                 if (result_code == true)\
164                 {\
165                         if (emstorage_commit_transaction(NULL, NULL, NULL) == false)\
166                         {\
167                                 error_code = EMAIL_ERROR_DB_FAILURE;\
168                                 result_code = false;\
169                         }\
170                 }\
171                 else\
172                 {\
173                         if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)\
174                                 error_code = EMAIL_ERROR_DB_FAILURE;\
175                 }\
176                 _unlockshm_mutex(&mapped_for_db_lock);\
177         }
178
179 /*  this define is used for query to read (select) */
180 #define EMSTORAGE_START_READ_TRANSACTION(transaction_flag) \
181         if (transaction_flag)\
182         {\
183                 /*_timedlock_shm_mutex(&mapped_for_db_lock, 2);*/\
184         }
185
186 /*  this define is used for query to read (select) */
187 #define EMSTORAGE_FINISH_READ_TRANSACTION(transaction_flag) \
188         if (transaction_flag)\
189         {\
190                 /*_unlockshm_mutex(&mapped_for_db_lock);*/\
191         }
192
193 /*  for safety DB operation */
194 static char g_db_path[248] = {0x00, };
195
196 static pthread_mutex_t _transactionBeginLock = PTHREAD_MUTEX_INITIALIZER;
197 static pthread_mutex_t _transactionEndLock = PTHREAD_MUTEX_INITIALIZER;
198 static pthread_mutex_t _dbus_noti_lock = PTHREAD_MUTEX_INITIALIZER;
199 static pthread_mutex_t _db_handle_lock = PTHREAD_MUTEX_INITIALIZER;
200
201 static int _get_attribute_type_by_mail_field_name(char *input_mail_field_name, email_mail_attribute_type *output_mail_attribute_type);
202
203 #define _MULTIPLE_DB_HANDLE
204
205 #ifdef _MULTIPLE_DB_HANDLE
206
207 #define _DISCONNECT_DB                  /* db_util_close(_db_handle); */
208
209 typedef struct
210 {
211         pthread_t       thread_id;
212         sqlite3 *db_handle;
213 } db_handle_t;
214
215 #define MAX_DB_CLIENT 100
216
217 /* static int _db_handle_count = 0; */
218 db_handle_t _db_handle_list[MAX_DB_CLIENT] = {{0, 0}, };
219
220 sqlite3 *emstorage_get_db_handle()
221 {       
222         EM_DEBUG_FUNC_BEGIN();
223         int i;
224         pthread_t current_thread_id = THREAD_SELF();
225         sqlite3 *result_db_handle = NULL;
226
227         ENTER_CRITICAL_SECTION(_db_handle_lock);
228         for (i = 0; i < MAX_DB_CLIENT; i++) {
229                 if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
230                         EM_DEBUG_LOG("found db handle at [%d]", i);
231                         result_db_handle = _db_handle_list[i].db_handle;
232                         break;
233                 }
234         }
235         LEAVE_CRITICAL_SECTION(_db_handle_lock);
236         
237         if (!result_db_handle)
238                 EM_DEBUG_EXCEPTION("Can't find proper handle for [%d]", current_thread_id);
239
240         EM_DEBUG_FUNC_END();
241         return result_db_handle;
242 }
243
244 int emstorage_set_db_handle(sqlite3 *db_handle)
245 {       
246         EM_DEBUG_FUNC_BEGIN();
247         int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
248         pthread_t current_thread_id = THREAD_SELF();
249
250         ENTER_CRITICAL_SECTION(_db_handle_lock);
251         for (i = 0; i < MAX_DB_CLIENT; i++)     {
252                 if (_db_handle_list[i].thread_id == 0) {
253                         _db_handle_list[i].thread_id = current_thread_id;
254                         _db_handle_list[i].db_handle = db_handle;
255                         EM_DEBUG_LOG("current_thread_id [%d], index [%d]", current_thread_id, i);
256                         error_code =  EMAIL_ERROR_NONE;
257                         break;
258                 }
259         }
260         LEAVE_CRITICAL_SECTION(_db_handle_lock);
261
262         if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
263                 EM_DEBUG_EXCEPTION("Exceeded the limitation of db client. Can't find empty slot in _db_handle_list.");
264
265         EM_DEBUG_FUNC_END("error_code [%d]", error_code);
266         return error_code;
267 }
268
269 int emstorage_remove_db_handle()
270 {       
271         EM_DEBUG_FUNC_BEGIN();
272         int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
273         ENTER_CRITICAL_SECTION(_db_handle_lock);
274         for (i = 0; i < MAX_DB_CLIENT; i++)
275         {
276                 if (_db_handle_list[i].thread_id == THREAD_SELF())
277                 {
278                         _db_handle_list[i].thread_id = 0;
279                         _db_handle_list[i].db_handle = NULL;
280                         EM_DEBUG_LOG("index [%d]", i);
281                         error_code = EMAIL_ERROR_NONE;
282                         break;
283                 }
284         }
285         LEAVE_CRITICAL_SECTION(_db_handle_lock);
286         
287         if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
288                 EM_DEBUG_EXCEPTION("Can't find proper thread_id");
289
290         EM_DEBUG_FUNC_END("error_code [%d]", error_code);
291         return error_code;
292 }
293
294
295 int emstorage_reset_db_handle_list()
296 {       
297         EM_DEBUG_FUNC_BEGIN();
298         int i;
299
300         ENTER_CRITICAL_SECTION(_db_handle_lock);
301         for (i = 0; i < MAX_DB_CLIENT; i++)
302         {
303                 _db_handle_list[i].thread_id = 0;
304                 _db_handle_list[i].db_handle = NULL;
305         }
306         LEAVE_CRITICAL_SECTION(_db_handle_lock)
307         
308         EM_DEBUG_FUNC_END();
309         return EMAIL_ERROR_NONE;
310 }
311
312 sqlite3 *emstorage_get_db_connection()
313 {
314         sqlite3 *tmp_db_handle = emstorage_get_db_handle(); 
315         if (NULL == tmp_db_handle)
316                 tmp_db_handle = emstorage_db_open(NULL); 
317         return tmp_db_handle;
318 }
319
320
321 #else   /*  _MULTIPLE_DB_HANDLE */
322 #define _DISCONNECT_DB                  /* db_util_close(_db_handle); */
323
324 sqlite3 *_db_handle = NULL;
325
326 sqlite3 *emstorage_get_db_connection()
327 {
328         if (NULL == _db_handle)
329                 emstorage_db_open(NULL); 
330         return _db_handle;
331 }
332 #endif  /*  _MULTIPLE_DB_HANDLE */
333
334 /* ------------------------------------------------------------------------------ */
335 /*  Mutex using shared memory */
336 typedef struct 
337 {
338         pthread_mutex_t mutex;
339         int data;
340 } mmapped_t;
341
342 mmapped_t       *mapped_for_db_lock = NULL;
343 int              shm_fd_for_db_lock = 0;
344
345 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
346 mmapped_t       *mapped_for_generating_mail_id = NULL;
347 int              shm_fd_for_generating_mail_id = 0;
348 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
349
350 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__
351 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
352         {  _timedlock_shm_mutex(&mapped_for_db_lock, 2); return_value = function_call; _unlockshm_mutex(&mapped_for_db_lock); }
353 #else /*  __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
354 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
355         {  return_value = function_call; }
356 #endif /*  __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
357
358 INTERNAL_FUNC int emstorage_shm_file_init(const char *shm_file_name)
359 {
360         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
361
362         if(!shm_file_name) {
363                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
364                 return EMAIL_ERROR_INVALID_PARAM;
365         }
366
367         int fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); /*  note: permission is not working */
368
369         if (fd > 0) {
370                 fchmod(fd, 0666);
371                 EM_DEBUG_LOG("** Create SHM FILE **"); 
372                 if (ftruncate(fd, sizeof(mmapped_t)) != 0) {
373                         EM_DEBUG_EXCEPTION("ftruncate failed: %s", strerror(errno));
374                         return EMAIL_ERROR_SYSTEM_FAILURE;
375                 }
376                 
377                 mmapped_t *m = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
378                 if (m == MAP_FAILED) {
379                         EM_DEBUG_EXCEPTION("mmap failed: %s", strerror(errno));
380                         return EMAIL_ERROR_SYSTEM_FAILURE;
381                 }
382
383                 m->data = 0;
384
385                 pthread_mutexattr_t mattr; 
386                 pthread_mutexattr_init(&mattr);
387                 pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
388                 pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
389                 pthread_mutex_init(&(m->mutex), &mattr);
390                 pthread_mutexattr_destroy(&mattr);
391         }
392         else {
393                 EM_DEBUG_EXCEPTION("shm_open failed: %s", strerror(errno));
394                 return EMAIL_ERROR_SYSTEM_FAILURE;
395         }
396         close(fd);
397         EM_DEBUG_FUNC_END();
398         return EMAIL_ERROR_NONE;
399 }
400
401 int emstorage_shm_file_destroy(const char *shm_file_name)
402 {
403         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
404         if(!shm_file_name) {
405                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
406                 return EMAIL_ERROR_INVALID_PARAM;
407         }
408
409         if (shm_unlink(shm_file_name) != 0)
410                 EM_DEBUG_EXCEPTION("shm_unlink failed: %s", strerror(errno));
411         EM_DEBUG_FUNC_END();
412         return EMAIL_ERROR_NONE;
413 }
414
415 static int _initialize_shm_mutex(const char *shm_file_name, int *param_shm_fd, mmapped_t **param_mapped)
416 {
417         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p] param_shm_fd [%p], param_mapped [%p]", shm_file_name, param_shm_fd, param_mapped);
418         
419         if(!shm_file_name || !param_shm_fd || !param_mapped) {
420                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
421                 return EMAIL_ERROR_INVALID_PARAM;
422         }
423
424         if (!(*param_mapped)) {
425                 EM_DEBUG_LOG("** mapping begin **");
426
427                 if (!(*param_shm_fd)) { /*  open shm_file_name at first. Otherwise, the num of files in /proc/pid/fd will be increasing  */
428                         *param_shm_fd = shm_open(shm_file_name, O_RDWR, 0);
429                         if ((*param_shm_fd) == -1) {
430                                 EM_DEBUG_EXCEPTION("FAIL: shm_open(): %s", strerror(errno));
431                                 return EMAIL_ERROR_SYSTEM_FAILURE;
432                         }
433                 }
434                 mmapped_t *tmp = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ|PROT_WRITE, MAP_SHARED, (*param_shm_fd), 0);
435                 
436                 if (tmp == MAP_FAILED) {
437                         EM_DEBUG_EXCEPTION("mmap failed: %s", strerror(errno));
438                         return EMAIL_ERROR_SYSTEM_FAILURE;
439                 }
440                 *param_mapped = tmp;
441         }
442
443         EM_DEBUG_FUNC_END();
444         return EMAIL_ERROR_NONE;
445 }
446
447 static int _timedlock_shm_mutex(mmapped_t **param_mapped, int sec)
448 {
449         EM_DEBUG_FUNC_BEGIN("param_mapped [%p], sec [%d]", param_mapped, sec);
450         
451         if(!param_mapped) {
452                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
453                 return EMAIL_ERROR_INVALID_PARAM;
454         }
455
456         struct timespec abs_time;       
457         clock_gettime(CLOCK_REALTIME, &abs_time);
458         abs_time.tv_sec += sec;
459
460         int err = pthread_mutex_timedlock(&((*param_mapped)->mutex), &abs_time);
461         
462         if (err == EOWNERDEAD) {
463                 err = pthread_mutex_consistent(&((*param_mapped)->mutex));
464                 EM_DEBUG_EXCEPTION("Previous owner is dead with lock. Fix mutex : %s", EM_STRERROR(err));
465         } 
466         else if (err != 0) {
467                 EM_DEBUG_EXCEPTION("ERROR : %s", EM_STRERROR(err));
468                 return err;
469         }
470         
471         EM_DEBUG_FUNC_END();
472         return EMAIL_ERROR_NONE;
473 }
474
475 void _unlockshm_mutex(mmapped_t **param_mapped)
476 {
477         EM_DEBUG_FUNC_BEGIN(); 
478         pthread_mutex_unlock(&((*param_mapped)->mutex));
479         EM_DEBUG_FUNC_END();
480 }
481 /* ------------------------------------------------------------------------------ */
482
483
484 static int _open_counter = 0;
485 static int g_transaction = false;
486
487 static int _get_password_file_name(int account_id, char *recv_password_file_name, char *send_password_file_name);
488 static int _read_password_from_secure_storage(char *file_name, char **password);
489
490 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
491 static int _get_cert_password_file_name(int index, char *cert_password_file_name);
492 #endif
493
494 typedef struct {
495         const char *object_name;
496         unsigned int data_flag;
497 } email_db_object_t;
498
499 static const email_db_object_t _g_db_tables[] =
500 {
501         { "mail_read_mail_uid_tbl", 1},
502         { "mail_tbl", 1},
503         { "mail_attachment_tbl", 1},
504         { NULL,  0},
505 };
506
507 static const email_db_object_t _g_db_indexes[] =
508 {
509         { "mail_read_mail_uid_idx1", 1},
510         { "mail_idx1", 1},
511         { "mail_attachment_idx1", 1},
512         { NULL,  1},
513 };
514
515 enum
516 {
517         CREATE_TABLE_MAIL_ACCOUNT_TBL,
518         CREATE_TABLE_MAIL_BOX_TBL,
519         CREATE_TABLE_MAIL_READ_MAIL_UID_TBL,
520         CREATE_TABLE_MAIL_RULE_TBL,
521         CREATE_TABLE_MAIL_TBL,
522         CREATE_TABLE_MAIL_ATTACHMENT_TBL,
523 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
524         CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL,
525 #endif
526         CREATE_TABLE_MAIL_MEETING_TBL,
527 #ifdef __FEATURE_LOCAL_ACTIVITY__
528         CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL,
529 #endif
530         CREATE_TABLE_MAIL_CERTIFICATE_TBL,
531         CREATE_TABLE_MAX,
532 };
533
534 enum
535 {
536         DATA1_IDX_IN_MAIL_ACTIVITY_TBL = 0,
537         TRANSTYPE_IDX_IN_MAIL_ACTIVITY_TBL,
538         FLAG_IDX_IN_MAIL_ACTIVITY_TBL,
539 };
540
541 enum
542 {
543         CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL = 0,
544         ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
545         ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
546         ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
547         EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
548         EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
549         EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, 
550         ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL,
551         EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL,
552         SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL,     
553         FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL, 
554 };
555
556 enum
557 {
558         TO_RECIPIENT = 0,
559         CC_RECIPIENT,
560         BCC_RECIPIENT,
561 };
562 enum 
563 {
564         ACCOUNT_ID_IDX_IN_MAIL_BOX_TBL = 0,
565         LOCAL_YN_IDX_IN_MAIL_BOX_TBL,
566         MAILBOX_NAME_IDX_IN_MAIL_BOX_TBL,
567         MAILBOX_TYPE_IDX_IN_MAIL_BOX_TBL,
568         ALIAS_IDX_IN_MAIL_BOX_TBL,
569         SYNC_WITH_SERVER_YN_IDX_IN_MAIL_BOX_TBL,
570         MODIFIABLE_YN_IDX_IN_MAIL_BOX_TBL,
571         TOTAL_MAIL_COUNT_ON_SERVER_IDX_IN_MAIL_BOX_TBL,
572         ARCHIVE_IDX_IN_MAIL_BOX_TBL,
573         MAIL_SLOT_SIZE_IDX_IN_MAIL_BOX_TBL,
574 };
575
576 enum 
577 {
578         ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL = 0,
579         LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
580         LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
581         MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL,
582         S_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
583         DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL,
584         DATA2_IDX_IN_MAIL_READ_MAIL_UID_TBL,
585         FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL,
586         IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL,          /* unused */
587 };
588
589 enum 
590 {
591         ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL = 0,
592         RULE_ID_IDX_IN_MAIL_RULE_TBL,
593         TYPE_IDX_IN_MAIL_RULE_TBL,
594         VALUE_IDX_IN_MAIL_RULE_TBL,
595         ACTION_TYPE_IDX_IN_MAIL_RULE_TBL,
596         TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL,
597         FLAG1_IDX_IN_MAIL_RULE_TBL,
598         FLAG2_IDX_IN_MAIL_RULE_TBL,
599 };
600
601 enum 
602 {
603         MAIL_ID_IDX_IN_MAIL_TBL = 0,
604         ACCOUNT_ID_IDX_IN_MAIL_TBL,
605         MAILBOX_ID_IDX_IN_MAIL_TBL,
606         MAILBOX_NAME_IDX_IN_MAIL_TBL,
607         MAILBOX_TYPE_IDX_IN_MAIL_TBL,
608         SUBJECT_IDX_IN_MAIL_TBL,
609         DATETIME_IDX_IN_MAIL_TBL,
610         SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL,
611         SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL,
612         SERVER_MAIL_ID_IDX_IN_MAIL_TBL,
613         MESSAGE_ID_IDX_IN_MAIL_TBL,
614         FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL,
615         FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL,
616         FULL_ADDRESS_TO_IDX_IN_MAIL_TBL,
617         FULL_ADDRESS_CC_IDX_IN_MAIL_TBL,
618         FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL,
619         FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL,
620         EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL,
621         EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL,
622         ALIAS_SENDER_IDX_IN_MAIL_TBL,
623         ALIAS_RECIPIENT_IDX_IN_MAIL_TBL,
624         BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL,
625         FILE_PATH_PLAIN_IDX_IN_MAIL_TBL,
626         FILE_PATH_HTML_IDX_IN_MAIL_TBL,
627         FILE_PATH_MIME_ENTITY_IDX_IN_MAIL_TBL,
628         MAIL_SIZE_IDX_IN_MAIL_TBL,
629         FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL,
630         FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL,
631         FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL,
632         FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL,
633         FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL,
634         FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL,
635         FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL,
636         DRM_STATUS_IDX_IN_MAIL_TBL,
637         PRIORITY_IDX_IN_MAIL_TBL,
638         SAVE_STATUS_IDX_IN_MAIL_TBL,
639         LOCK_STATUS_IDX_IN_MAIL_TBL,
640         REPORT_STATUS_IDX_IN_MAIL_TBL,
641         ATTACHMENT_COUNT_IDX_IN_MAIL_TBL,
642         INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL,
643         THREAD_ID_IDX_IN_MAIL_TBL,
644         THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL,
645         PREVIEW_TEXT_IDX_IN_MAIL_TBL,
646         MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL,
647         MESSAGE_CLASS_IDX_IN_MAIL_TBL,
648         DIGEST_TYPE_IDX_IN_MAIL_TBL,
649         SMIME_TYPE_IDX_IN_MAIL_TBL,
650         FIELD_COUNT_OF_MAIL_TBL,  /* End of mail_tbl */
651 };
652
653 enum 
654 {
655         ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL = 0,
656         ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL,
657         ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL,
658         ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL,
659         MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
660         ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
661         MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
662         ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
663         ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
664         ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL,
665         ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
666         ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
667 #ifdef __ATTACHMENT_OPTI__      
668         ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL,
669         SECTION_IDX_IN_MAIL_ATTACHMENT_TBL,
670 #endif  
671 };
672
673 enum {
674         IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL = 0,
675 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
676         MAIL_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
677         ACCOUNT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
678         ADDRESS_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
679         ADDRESS_IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL,
680 #endif
681         ADDRESS_IDX_IN_MAIL_CONTACT_SYNC_TBL,
682         CONTACT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
683         STORAGE_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
684         CONTACT_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
685 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
686         DISPLAY_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
687         FLAG1_IDX_IN_MAIL_CONTACT_SYNC_TBL,
688 #endif
689 };
690
691 /* sowmya.kr 03032010, changes for get list of mails for given addr list */
692 typedef struct _em_mail_id_list {
693         int mail_id;    
694         struct _em_mail_id_list *next;
695 } em_mail_id_list;
696
697 static char *g_test_query[] = {
698                 /*  1. select mail_account_tbl */
699                 "SELECT"
700                 " account_name, "
701                 " incoming_server_type, "
702                 " incoming_server_address, "
703                 " user_email_address, "
704                 " incoming_server_user_name, "
705                 " incoming_server_password, "
706                 " retrieval_mode, "
707                 " incoming_server_port_number, "
708                 " incoming_server_secure_connection, "
709                 " outgoing_server_type, "
710                 " outgoing_server_address, "
711                 " outgoing_server_port_number, "
712                 " outgoing_server_need_authentication, "
713                 " outgoing_server_secure_connection, "
714                 " outgoing_server_user_name, "
715                 " outgoing_server_password, "
716                 " display_name, "
717                 " reply_to_addr, "
718                 " return_addr, "
719                 " account_id, "
720                 " keep_mails_on_pop_server_after_download, "
721                 " flag1, "
722                 " flag2, "
723                 " pop_before_smtp, "
724                 " incoming_server_requires_apop"                           
725                 ", logo_icon_path, "
726                 " is_preset_account, "
727                 " check_interval, "
728                 " priority, "
729                 " keep_local_copy, "
730                 " req_delivery_receipt, "
731                 " req_read_receipt, "
732                 " download_limit, "
733                 " block_address, "
734                 " block_subject, "
735                 " display_name_from, "
736                 " reply_with_body, "
737                 " forward_with_files, "
738                 " add_myname_card, "
739                 " add_signature, "
740                 " signature"
741                 ", add_my_address_to_bcc"
742                 ", account_svc_id "
743                 ", index_color "
744                 ", sync_status "
745                 ", sync_disabled "
746                 ", smime_type"
747                 ", certificate_path"
748                 ", cipher_type"
749                 ", digest_type"
750                 " FROM mail_account_tbl",
751                 /*  2. select mail_box_tbl */
752                 "SELECT "
753                 "   mailbox_id, "
754                 "   account_id, "
755                 "   local_yn,  "
756                 "   mailbox_name,  "
757                 "   mailbox_type,   "
758                 "   alias,  "
759                 "   sync_with_server_yn,  "
760                 "   modifiable_yn,  "
761                 "   total_mail_count_on_server,  "
762                 "   has_archived_mails, "
763                 "   mail_slot_size "
764                 "   last_sync_time "
765                 " FROM mail_box_tbl ",
766                 /*  3. select mail_read_mail_uid_tbl */
767                 "SELECT  "
768                 "   account_id,  "
769                 "   mailbox_id,  "
770                 "   local_uid,  "
771                 "   mailbox_name,  "
772                 "   s_uid,  "
773                 "   data1 ,  "
774                 "   data2,  "
775                 "   flag,  "
776                 "   idx_num "
777                 " FROM mail_read_mail_uid_tbl ",
778                 /*  4. select mail_rule_tbl */
779                 "SELECT "
780                 "   account_id, "
781                 "   rule_id, "
782                 "       type, "
783                 "       value, "
784                 "       action_type, "
785                 "       target_mailbox_id,      "
786                 "       flag1, "
787                 "       flag2 "
788                 " FROM mail_rule_tbl    ",
789                 /*  5. select mail_tbl */
790                 "SELECT"
791                 "       mail_id, "
792                 "       account_id, "
793                 "       mailbox_id, "
794                 "       mailbox_name, "
795                 "   mailbox_type, "
796                 "   subject, "
797                 "       date_time, "
798                 "       server_mail_status, "
799                 "       server_mailbox_name, "
800                 "       server_mail_id, "
801                 " message_id, "
802                 "   full_address_from, "
803                 "   full_address_reply, "
804                 "   full_address_to, "
805                 "   full_address_cc, "
806                 "   full_address_bcc, "
807                 "   full_address_return, "
808                 " email_address_sender, "
809                 " email_address_recipient, "
810                 " alias_sender, "
811                 " alias_recipient, "
812                 "       body_download_status, "
813                 "       file_path_plain, "
814                 "       file_path_html, "
815                 "   file_path_mime_entity, "
816                 "       mail_size, "
817                 " flags_seen_field     ,"
818                 " flags_deleted_field  ,"
819                 " flags_flagged_field  ,"
820                 " flags_answered_field ,"
821                 " flags_recent_field   ,"
822                 " flags_draft_field    ,"
823                 " flags_forwarded_field,"
824                 "       DRM_status, "
825                 "       priority, "
826                 "       save_status, "
827                 "       lock_status, "
828                 "       report_status, "
829                 "   attachment_count, "
830                 "       inline_content_count, "
831                 "       thread_id, "
832                 "       thread_item_count, "
833                 "   preview_text, "
834                 "       meeting_request_status, "
835                 "   message_class, "
836                 "   digest_type, "
837                 "   smime_type "
838                 " FROM mail_tbl",
839                 /*  6. select mail_attachment_tbl */
840                 "SELECT "
841                 "       attachment_id, "
842                 "       attachment_name, "
843                 "       attachment_path, "
844                 "       attachment_size, "
845                 "       mail_id,  "
846                 "       account_id, "
847                 "       mailbox_id, "
848                 "       attachment_save_status,  "
849                 "       attachment_drm_type,  "
850                 "       attachment_drm_method,  "
851                 "       attachment_inline_content_status,  "
852                 "       attachment_mime_type  "
853                 " FROM mail_attachment_tbl ",
854
855 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
856                 "SELECT  "
857                 "   account_id, "
858                 "   mail_id, "
859                 "   server_mail_id, "
860                 "   activity_id, "
861                 "   activity_type, "
862                 "   mailbox_id, "
863                 "   mailbox_name "
864                 " FROM mail_partial_body_activity_tbl ",
865 #endif
866
867                 "SELECT  "
868                 "   mail_id, "
869                 "   account_id, "
870                 "   mailbox_id, "
871                 "   meeting_response, "
872                 "   start_time, "
873                 "   end_time, "
874                 "   location, "
875                 "   global_object_id, "
876                 "   offset, "
877                 "   standard_name, "
878                 "   standard_time_start_date, "
879                 "   standard_bias, "
880                 "   daylight_name, "
881                 "   daylight_time_start_date, "
882                 "   daylight_bias "
883                 " FROM mail_meeting_tbl ",
884
885 #ifdef __FEATURE_LOCAL_ACTIVITY__
886                 "SELECT "
887                 "       activity_id, "
888                 "   account_id, "
889                 "   mail_id, "
890                 "   activity_type, "
891                 "   server_mailid, "
892                 "   src_mbox , "
893                 "       dest_mbox "
894                 " FROM mail_local_activity_tbl  ",
895 #endif
896                 NULL,
897 };
898
899
900 /* ----------- Notification Changes ----------- */
901 typedef enum
902 {
903         _NOTI_TYPE_STORAGE         = 0,
904         _NOTI_TYPE_NETWORK         = 1,
905         _NOTI_TYPE_RESPONSE_TO_API = 2,
906 } enotitype_t;
907
908
909 INTERNAL_FUNC int emstorage_send_noti(enotitype_t notiType, int subType, int data1, int data2, char *data3, int data4)
910 {
911         EM_DEBUG_FUNC_BEGIN();
912         EM_PROFILE_BEGIN(profile_emstorage_send_noti);
913
914         int ret = 0;
915         DBusConnection *connection;
916         DBusMessage     *signal = NULL;
917         DBusError          dbus_error;
918         dbus_uint32_t   error; 
919         const char       *nullString = "";
920
921         ENTER_CRITICAL_SECTION(_dbus_noti_lock);
922
923         dbus_error_init (&dbus_error);
924         connection = dbus_bus_get(DBUS_BUS_SYSTEM, &dbus_error);
925
926         if (connection == NULL) {
927                 EM_DEBUG_LOG("dbus_bus_get is failed");
928                 goto FINISH_OFF;
929         }
930
931         if (notiType == _NOTI_TYPE_STORAGE) {
932                 signal = dbus_message_new_signal("/User/Email/StorageChange", EMAIL_STORAGE_CHANGE_NOTI, "email");
933
934                 if (signal == NULL) {
935                         EM_DEBUG_EXCEPTION("dbus_message_new_signal is failed");
936                         goto FINISH_OFF;
937                 }
938                 EM_DEBUG_LOG("/User/Email/StorageChange Signal is created by dbus_message_new_signal");
939
940                 dbus_message_append_args(signal, DBUS_TYPE_INT32, &subType, DBUS_TYPE_INVALID);
941                 dbus_message_append_args(signal, DBUS_TYPE_INT32, &data1, DBUS_TYPE_INVALID);
942                 dbus_message_append_args(signal, DBUS_TYPE_INT32, &data2, DBUS_TYPE_INVALID);
943                 if (data3 == NULL)
944                         dbus_message_append_args(signal, DBUS_TYPE_STRING, &nullString, DBUS_TYPE_INVALID);
945                 else
946                         dbus_message_append_args(signal, DBUS_TYPE_STRING, &data3, DBUS_TYPE_INVALID);
947                 dbus_message_append_args(signal, DBUS_TYPE_INT32, &data4, DBUS_TYPE_INVALID);
948         }
949         else if (notiType == _NOTI_TYPE_NETWORK) {
950                 signal = dbus_message_new_signal("/User/Email/NetworkStatus", EMAIL_NETOWRK_CHANGE_NOTI, "email");
951
952                 if (signal == NULL) {
953                         EM_DEBUG_EXCEPTION("dbus_message_new_signal is failed");
954                         goto FINISH_OFF;
955                 }
956                         
957                 EM_DEBUG_LOG("/User/Email/NetworkStatus Signal is created by dbus_message_new_signal");
958
959                 dbus_message_append_args(signal, DBUS_TYPE_INT32, &subType, DBUS_TYPE_INVALID);
960                 dbus_message_append_args(signal, DBUS_TYPE_INT32, &data1, DBUS_TYPE_INVALID);
961                 if (data3 == NULL)
962                         dbus_message_append_args(signal, DBUS_TYPE_STRING, &nullString, DBUS_TYPE_INVALID);
963                 else
964                         dbus_message_append_args(signal, DBUS_TYPE_STRING, &data3, DBUS_TYPE_INVALID);
965                 dbus_message_append_args(signal, DBUS_TYPE_INT32, &data2, DBUS_TYPE_INVALID);
966                 dbus_message_append_args(signal, DBUS_TYPE_INT32, &data4, DBUS_TYPE_INVALID);
967         }
968         else if (notiType == _NOTI_TYPE_RESPONSE_TO_API) {
969                 signal = dbus_message_new_signal("/User/Email/ResponseToAPI", EMAIL_RESPONSE_TO_API_NOTI, "email");
970
971                 if (signal == NULL) {
972                         EM_DEBUG_EXCEPTION("dbus_message_new_signal is failed");
973                         goto FINISH_OFF;
974                 }
975
976                 EM_DEBUG_LOG("/User/Email/ResponseToAPI Signal is created by dbus_message_new_signal");
977
978                 dbus_message_append_args(signal, DBUS_TYPE_INT32, &subType, DBUS_TYPE_INVALID);
979                 dbus_message_append_args(signal, DBUS_TYPE_INT32, &data1, DBUS_TYPE_INVALID);
980                 dbus_message_append_args(signal, DBUS_TYPE_INT32, &data2, DBUS_TYPE_INVALID);
981         }
982         else {
983                 EM_DEBUG_EXCEPTION("Wrong notification type [%d]", notiType);
984                 error = EMAIL_ERROR_IPC_CRASH;
985                 goto FINISH_OFF;
986         }
987
988         if (!dbus_connection_send(connection, signal, &error)) {
989                 EM_DEBUG_LOG("dbus_connection_send is failed [%d]", error);
990         }
991         else {
992                 EM_DEBUG_LOG("dbus_connection_send is successful");
993                 ret = 1;
994         }
995                 
996 /*      EM_DEBUG_LOG("Before dbus_connection_flush");    */
997 /*      dbus_connection_flush(connection);               */
998 /*      EM_DEBUG_LOG("After dbus_connection_flush");     */
999         
1000         ret = true;
1001 FINISH_OFF:
1002         if (signal)
1003                 dbus_message_unref(signal);
1004
1005         LEAVE_CRITICAL_SECTION(_dbus_noti_lock);
1006         EM_PROFILE_END(profile_emstorage_send_noti);
1007         EM_DEBUG_FUNC_END("ret [%d]", ret);
1008         return ret;
1009 }
1010
1011
1012 INTERNAL_FUNC int emstorage_notify_storage_event(email_noti_on_storage_event transaction_type, int data1, int data2 , char *data3, int data4)
1013 {
1014         EM_DEBUG_FUNC_BEGIN("transaction_type[%d], data1[%d], data2[%d], data3[%p], data4[%d]", transaction_type, data1, data2, data3, data4);
1015         return emstorage_send_noti(_NOTI_TYPE_STORAGE, (int)transaction_type, data1, data2, data3, data4);
1016 }
1017
1018 INTERNAL_FUNC int emstorage_notify_network_event(email_noti_on_network_event status_type, int data1, char *data2, int data3, int data4)
1019 {
1020         EM_DEBUG_FUNC_BEGIN("status_type[%d], data1[%d], data2[%p], data3[%d], data4[%d]", status_type, data1, data2, data3, data4);
1021         return emstorage_send_noti(_NOTI_TYPE_NETWORK, (int)status_type, data1, data3, data2, data4);
1022 }
1023
1024 INTERNAL_FUNC int emstorage_notify_response_to_api(email_event_type_t event_type, int data1, int data2)
1025 {
1026         EM_DEBUG_FUNC_BEGIN("event_type[%d], data1[%d], data2[%p], data3[%d], data4[%d]", event_type, data1, data2);
1027         return emstorage_send_noti(_NOTI_TYPE_RESPONSE_TO_API, (int)event_type, data1, data2, NULL, 0);
1028 }
1029
1030 /* ----------- Notification Changes End----------- */
1031 static int _get_table_field_data_char(char  **table, char *buf, int index)
1032 {
1033         if ((table == NULL) || (buf == NULL) || (index < 0))  {
1034                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1035                 return false;
1036         }
1037
1038         if (table[index] != NULL) {
1039                 *buf = (char)atoi(table[index]);
1040                 return true;
1041         }
1042         
1043         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
1044         
1045         *buf = 0;
1046         return false;
1047 }
1048
1049 static int _get_table_field_data_int(char  **table, int *buf, int index)
1050 {
1051         if ((table == NULL) || (buf == NULL) || (index < 0))  {
1052                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1053                 return false;
1054         }
1055
1056         if (table[index] != NULL) {
1057                 *buf = atoi(table[index]);
1058                 return true;
1059         }
1060         
1061         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
1062         
1063         *buf = 0;
1064         return false;
1065 }
1066
1067 static int _get_table_field_data_time_t(char  **table, time_t *buf, int index)
1068 {
1069         if ((table == NULL) || (buf == NULL) || (index < 0))  {
1070                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1071                 return false;
1072         }
1073
1074         if (table[index] != NULL) {
1075                 *buf = (time_t)atol(table[index]);
1076                 return true;
1077         }
1078
1079         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
1080
1081         *buf = 0;
1082         return false;
1083 }
1084
1085 static int _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
1086 {
1087         int ret = false;
1088
1089         if ((table == NULL) || (buf == NULL) || (index < 0))  {
1090                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1091                 return false;
1092         }
1093  
1094         char *pTemp = table[index];
1095         int sLen = 0;   
1096         if (pTemp == NULL) 
1097                 *buf = NULL;
1098         else {
1099                 sLen = strlen(pTemp);
1100                 if(sLen) {
1101                         *buf = (char *) em_malloc(sLen + 1);
1102                         if (*buf == NULL) {
1103                                 EM_DEBUG_EXCEPTION("malloc is failed");
1104                                 goto FINISH_OFF;
1105                         }
1106                         strncpy(*buf, pTemp, sLen);
1107                 }
1108                 else 
1109                         *buf = NULL;
1110         }
1111 #ifdef _PRINT_STORAGE_LOG_
1112         if (*buf)
1113                 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", *buf, index);
1114         else
1115                 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1116 #endif  
1117         ret = true;
1118 FINISH_OFF:
1119
1120         return ret;
1121 }
1122
1123 static int _get_table_field_data_string_without_allocation(char **table, char *buf, int buffer_size, int ucs2, int index)
1124 {
1125         if ((table == NULL) || (buf == NULL) || (index < 0))  {
1126                 EM_DEBUG_EXCEPTION(" table[%p], buf[%p], index[%d]", table, buf, index);
1127                         return false;
1128         }
1129
1130         char *pTemp = table[index];
1131         if (pTemp == NULL)
1132                 buf = NULL;
1133         else {
1134                 memset(buf, 0, buffer_size);
1135                 strncpy(buf, pTemp, buffer_size - 1);
1136         }
1137 #ifdef _PRINT_STORAGE_LOG_
1138         if (buf)
1139                 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", buf, index);
1140         else
1141                 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1142 #endif  
1143
1144         return true;
1145 }
1146
1147 static int _get_stmt_field_data_char(DB_STMT hStmt, char *buf, int index)
1148 {
1149         if ((hStmt == NULL) || (buf == NULL) || (index < 0))  {
1150                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1151                 return false;
1152         }
1153
1154         if (sqlite3_column_text(hStmt, index) != NULL) {
1155                 *buf = (char)sqlite3_column_int(hStmt, index);
1156 #ifdef _PRINT_STORAGE_LOG_
1157                         EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1158 #endif
1159                 return true;
1160         }
1161         
1162         EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1163
1164         return false;
1165 }
1166
1167 static int _get_stmt_field_data_int(DB_STMT hStmt, int *buf, int index)
1168 {
1169         if ((hStmt == NULL) || (buf == NULL) || (index < 0))  {
1170                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1171                 return false;
1172         }
1173
1174         if (sqlite3_column_text(hStmt, index) != NULL) {
1175                 *buf = sqlite3_column_int(hStmt, index);
1176 #ifdef _PRINT_STORAGE_LOG_
1177                         EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1178 #endif
1179                 return true;
1180         }
1181         
1182         EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1183
1184         return false;
1185 }
1186
1187 static int _get_stmt_field_data_time_t(DB_STMT hStmt, time_t *buf, int index)
1188 {
1189         if ((hStmt == NULL) || (buf == NULL) || (index < 0))  {
1190                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1191                 return false;
1192         }
1193
1194         if (sqlite3_column_text(hStmt, index) != NULL) {
1195                 *buf = (time_t)sqlite3_column_int(hStmt, index);
1196 #ifdef _PRINT_STORAGE_LOG_
1197                 EM_DEBUG_LOG("_get_stmt_field_data_time_t [%d]", *buf);
1198 #endif
1199                 return true;
1200         }
1201
1202         EM_DEBUG_LOG("_get_stmt_field_data_time_t fail. index [%d]", index);
1203         return false;
1204 }
1205
1206 static int _get_stmt_field_data_string(DB_STMT hStmt, char **buf, int ucs2, int index)
1207 {
1208         if ((hStmt < 0) || (buf == NULL) || (index < 0))  {
1209                 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1210                 return false;
1211         }
1212
1213         int sLen = 0;   
1214         sLen = sqlite3_column_bytes(hStmt, index);
1215
1216 #ifdef _PRINT_STORAGE_LOG_
1217                 EM_DEBUG_LOG("_get_stmt_field_data_string sqlite3_column_bytes sLen[%d]", sLen);
1218 #endif
1219
1220         if (sLen > 0) {
1221                 *buf = (char *) em_malloc(sLen + 1);
1222                 strncpy(*buf, (char *)sqlite3_column_text(hStmt, index), sLen);
1223         }
1224         else
1225                 *buf = NULL;
1226
1227 #ifdef _PRINT_STORAGE_LOG_
1228         if (*buf)
1229                 EM_DEBUG_LOG("buf[%s], index[%d]", *buf, index);
1230         else
1231                 EM_DEBUG_LOG("_get_stmt_field_data_string - No string got");
1232 #endif  
1233
1234         return false;
1235 }
1236
1237 static void _get_stmt_field_data_blob(DB_STMT hStmt, void **buf, int index)
1238 {
1239         if ((hStmt < 0) || (buf == NULL) || (index < 0))  {
1240                 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1241                 return;
1242         }
1243
1244         int sLen = 0;
1245         sLen = sqlite3_column_bytes(hStmt, index);
1246
1247 #ifdef _PRINT_STORAGE_LOG_
1248         EM_DEBUG_LOG("_get_stmt_field_data_blob sqlite3_column_bytes sLen[%d]", sLen);
1249 #endif
1250
1251         if (sLen > 0) {
1252                 *buf = (char *) em_malloc(sLen);
1253                 memcpy(*buf, (void *)sqlite3_column_blob(hStmt, index), sLen);
1254         }
1255         else
1256                 *buf = NULL;
1257
1258 }
1259
1260 static int _get_stmt_field_data_string_without_allocation(DB_STMT hStmt, char *buf, int buffer_size, int ucs2, int index)
1261 {
1262         if ((hStmt < 0) || (buf == NULL) || (index < 0))  {
1263                 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], buffer_size[%d], index[%d]", hStmt, buf, buffer_size, index);
1264                 return false;
1265         }
1266
1267         int sLen = 0;   
1268         sLen = sqlite3_column_bytes(hStmt, index);
1269
1270 #ifdef _PRINT_STORAGE_LOG_
1271                 EM_DEBUG_LOG("_get_stmt_field_data_string_without_allocation sqlite3_column_bytes sLen[%d]", sLen);
1272 #endif
1273
1274         if (sLen > 0) {
1275                 memset(buf, 0, buffer_size);
1276                 strncpy(buf, (char *)sqlite3_column_text(hStmt, index), buffer_size - 1);
1277         }
1278         else
1279                 strcpy(buf, "");
1280
1281 #ifdef _PRINT_STORAGE_LOG_
1282         EM_DEBUG_LOG("buf[%s], index[%d]", buf, index);
1283 #endif  
1284         
1285         return false;
1286 }
1287
1288 static int _bind_stmt_field_data_char(DB_STMT hStmt, int index, char value)
1289 {
1290         if ((hStmt == NULL) || (index < 0)) {
1291                 EM_DEBUG_EXCEPTION("index[%d]", index);
1292                 return false;
1293         }
1294
1295         int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1296         
1297         if (ret != SQLITE_OK)  {
1298                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1299                 return false;
1300         }
1301         
1302         return true;
1303 }
1304
1305 static int _bind_stmt_field_data_int(DB_STMT hStmt, int index, int value)
1306 {
1307         if ((hStmt == NULL) || (index < 0)) {
1308                 EM_DEBUG_EXCEPTION("index[%d]", index);
1309                 return false;
1310         }
1311         
1312         int ret = sqlite3_bind_int(hStmt, index+1, value);
1313         
1314         if (ret != SQLITE_OK)  {
1315                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1316                 return false;
1317         }
1318         
1319         return true;
1320 }
1321
1322 static int _bind_stmt_field_data_time_t(DB_STMT hStmt, int index, time_t value)
1323 {
1324         if ((hStmt == NULL) || (index < 0)) {
1325                 EM_DEBUG_EXCEPTION("index[%d]", index);
1326                 return false;
1327         }
1328
1329         int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1330
1331         if (ret != SQLITE_OK)  {
1332                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1333                 return false;
1334         }
1335
1336         return true;
1337 }
1338
1339 static int _bind_stmt_field_data_string(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1340 {
1341         if ((hStmt == NULL) || (index < 0)) {
1342                 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1343                 return false;
1344         }
1345
1346 #ifdef _PRINT_STORAGE_LOG_
1347         EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1348 #endif
1349
1350         int ret = 0;
1351         if (value != NULL)
1352                 ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1353         else 
1354                 ret = sqlite3_bind_text(hStmt, index+1, "", -1, NULL);
1355
1356         if (ret != SQLITE_OK)  {
1357                 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1358                 return false;
1359         }
1360         return true;
1361 }
1362
1363
1364 static int _bind_stmt_field_data_blob(DB_STMT hStmt, int index, void *blob, int blob_size)
1365 {
1366         if ((hStmt == NULL) || (index < 0)) {
1367                 EM_DEBUG_EXCEPTION("index[%d], blob_size[%d]", index, blob_size);
1368                 return false;
1369         }
1370
1371 #ifdef _PRINT_STORAGE_LOG_
1372         EM_DEBUG_LOG("hStmt = %p, index = %d, blob_size = %d, blob = [%p]", hStmt, index, blob_size, blob);
1373 #endif
1374
1375         int ret = 0;
1376         if (blob_size>0)
1377                 ret = sqlite3_bind_blob(hStmt, index+1, blob, blob_size, SQLITE_STATIC);
1378         else
1379                 ret = sqlite3_bind_null(hStmt, index+1);
1380
1381         if (ret != SQLITE_OK)  {
1382                 EM_DEBUG_EXCEPTION("sqlite3_bind_blob fail [%d]", ret);
1383                 return false;
1384         }
1385         return true;
1386 }
1387
1388
1389 static int _delete_temp_file(const char *path)
1390 {
1391         EM_DEBUG_FUNC_BEGIN("path[%p]", path);
1392         
1393         DIR *dp = NULL;
1394         struct dirent *entry = NULL;
1395         
1396         char buf[1024] = {0x00, };
1397         
1398         if ((dp = opendir(path)) == NULL)  {
1399                 EM_DEBUG_EXCEPTION("opendir(\"%s\") failed...", path);
1400                 return false;
1401         }
1402         
1403         while ((entry = readdir(dp)) != NULL)  {
1404                 SNPRINTF(buf, sizeof(buf), "%s/%s", path, entry->d_name);
1405                 remove(buf);
1406         }
1407         
1408         closedir(dp);   
1409         EM_DEBUG_FUNC_END();    
1410         return true;
1411 }
1412
1413 char *cpy_str(char *src)
1414 {
1415         char *p = NULL;
1416         
1417         if (src)  {
1418                 if (!(p = em_malloc((int)strlen(src) + 1)))  {
1419                         EM_DEBUG_EXCEPTION("mailoc failed...");
1420                         return NULL;
1421                 }
1422                 strncpy(p, src, strlen(src));
1423         }
1424         
1425         return p;
1426 }
1427
1428 static void _emstorage_close_once(void)
1429 {
1430         EM_DEBUG_FUNC_BEGIN();
1431         
1432         DELETE_CRITICAL_SECTION(_transactionBeginLock);
1433         DELETE_CRITICAL_SECTION(_transactionEndLock);
1434         
1435         EM_DEBUG_FUNC_END();
1436 }
1437
1438 INTERNAL_FUNC int emstorage_close(int *err_code)
1439 {
1440         EM_DEBUG_FUNC_BEGIN();
1441         
1442         int ret = false;
1443         int error = EMAIL_ERROR_NONE;
1444
1445         emstorage_db_close(&error);             
1446         
1447         if (--_open_counter == 0)
1448                 _emstorage_close_once();
1449         
1450         ret = true;
1451
1452         if (err_code != NULL)
1453                 *err_code = error;
1454         
1455         EM_DEBUG_FUNC_END("ret [%d]", ret);
1456         return ret;
1457 }
1458
1459 static void *_emstorage_open_once(int *err_code)
1460 {
1461         EM_DEBUG_FUNC_BEGIN();
1462         
1463         int error = EMAIL_ERROR_NONE;
1464         
1465         mkdir(EMAILPATH, DIRECTORY_PERMISSION);
1466         mkdir(DATA_PATH, DIRECTORY_PERMISSION);
1467
1468         mkdir(MAILHOME, DIRECTORY_PERMISSION);
1469         
1470         SNPRINTF(g_db_path, sizeof(g_db_path), "%s/%s", MAILHOME, MAILTEMP);
1471         mkdir(g_db_path, DIRECTORY_PERMISSION);
1472         
1473         _delete_temp_file(g_db_path);
1474         
1475         g_transaction = false;
1476         
1477         if (!emstorage_create_table(EMAIL_CREATE_DB_NORMAL, &error)) {
1478                 EM_DEBUG_EXCEPTION(" emstorage_create_table failed - %d", error);
1479                 goto FINISH_OFF;
1480         }
1481         
1482 FINISH_OFF:
1483         if (err_code != NULL)
1484                 *err_code = error;
1485                 
1486         
1487         return NULL;
1488 }
1489
1490  /*  pData : a parameter which is registered when busy handler is registerd */
1491  /*  count : retry count */
1492 #define EMAIL_STORAGE_MAX_RETRY_COUNT   20
1493 static int _callback_sqlite_busy_handler(void *pData, int count)
1494 {
1495         EM_DEBUG_LOG("Busy Handler Called!!: [%d]", count);
1496         usleep(200000);   /*   sleep time when SQLITE_LOCK */
1497
1498         /*  retry will be stopped if  busy handler return 0 */
1499         return EMAIL_STORAGE_MAX_RETRY_COUNT - count;   
1500 }
1501
1502 static int _delete_all_files_and_directories(int *err_code)
1503 {
1504         EM_DEBUG_FUNC_BEGIN();
1505
1506         int error = EMAIL_ERROR_NONE;
1507         int ret = false;
1508
1509         if (!emstorage_delete_file(EMAIL_SERVICE_DB_FILE_PATH, &error)) {
1510                 if (error != EMAIL_ERROR_FILE_NOT_FOUND) {
1511                         EM_DEBUG_EXCEPTION("remove failed - %s", EMAIL_SERVICE_DB_FILE_PATH);
1512                         goto FINISH_OFF;        
1513                 }
1514         }
1515
1516         if (!emstorage_delete_dir(MAILHOME, &error)) {
1517                 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed");
1518                 goto FINISH_OFF;
1519         }
1520
1521         ret = true;
1522
1523 FINISH_OFF:
1524         if (err_code)
1525                 *err_code = error;
1526         EM_DEBUG_FUNC_END();
1527         return ret;
1528 }
1529
1530 static int _recovery_from_malformed_db_file(int *err_code)
1531 {
1532         EM_DEBUG_FUNC_BEGIN();
1533
1534         int error = EMAIL_ERROR_NONE;
1535         int ret = false;
1536         
1537         /* Delete all files and directories */
1538         if (!_delete_all_files_and_directories(&error)) {
1539                 EM_DEBUG_EXCEPTION("_delete_all_files_and_directories failed [%d]", error);
1540                 goto FINISH_OFF;
1541         }
1542
1543         /* Delete all accounts on MyAccount */
1544
1545         /* Delete all managed connection to DB */
1546         emstorage_reset_db_handle_list();
1547
1548         ret = true;
1549
1550 FINISH_OFF:
1551         if (err_code)
1552                 *err_code = error;
1553         EM_DEBUG_FUNC_END();
1554         return ret;
1555 }
1556
1557
1558 INTERNAL_FUNC int em_db_open(sqlite3 **sqlite_handle, int *err_code)
1559 {
1560         EM_DEBUG_FUNC_BEGIN();
1561         int rc = 0;
1562         int error = EMAIL_ERROR_NONE;
1563         int ret = false;
1564
1565         EM_DEBUG_LOG("*sqlite_handle[%p]", *sqlite_handle);
1566
1567         if (NULL == *sqlite_handle)  {
1568                 /*  db open */
1569                 EM_DEBUG_LOG("Open DB");
1570                 EMSTORAGE_PROTECTED_FUNC_CALL(db_util_open(EMAIL_SERVICE_DB_FILE_PATH, sqlite_handle, DB_UTIL_REGISTER_HOOK_METHOD), rc);
1571                 if (SQLITE_OK != rc) {
1572                         EM_DEBUG_EXCEPTION("db_util_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1573                         error = EMAIL_ERROR_DB_FAILURE;
1574                         db_util_close(*sqlite_handle); 
1575                         *sqlite_handle = NULL; 
1576
1577                         if (SQLITE_CORRUPT == rc) /* SQLITE_CORRUPT : The database disk image is malformed */ {/* Recovery DB file */ 
1578                                 EM_DEBUG_LOG("The database disk image is malformed. Trying to remove and create database disk image and directories");
1579                                 if (!_recovery_from_malformed_db_file(&error)) {
1580                                         EM_DEBUG_EXCEPTION("_recovery_from_malformed_db_file failed [%d]", error);
1581                                         goto FINISH_OFF;
1582                                 }
1583                                 
1584                                 EM_DEBUG_LOG("Open DB again");
1585                                 EMSTORAGE_PROTECTED_FUNC_CALL(db_util_open(EMAIL_SERVICE_DB_FILE_PATH, sqlite_handle, DB_UTIL_REGISTER_HOOK_METHOD), rc);
1586                                 if (SQLITE_OK != rc) {
1587                                         EM_DEBUG_EXCEPTION("db_util_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1588                                         error = EMAIL_ERROR_DB_FAILURE;
1589                                         db_util_close(*sqlite_handle); 
1590                                         *sqlite_handle = NULL; 
1591                                 }
1592                         }
1593                         else
1594                                 goto FINISH_OFF;        
1595                 }
1596                 EM_DEBUG_LOG(">>>>> DB Handle : *sqlite_handle[%p]", *sqlite_handle);
1597
1598                 /* register busy handler */
1599                 EM_DEBUG_LOG(">>>>> Register busy handler.....");
1600                 rc = sqlite3_busy_handler(*sqlite_handle, _callback_sqlite_busy_handler, NULL);  /*  Busy Handler registration, NULL is a parameter which will be passed to handler */
1601                 if (SQLITE_OK != rc) {
1602                         EM_DEBUG_EXCEPTION("sqlite3_busy_handler fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1603                         error = EMAIL_ERROR_DB_FAILURE;
1604                         db_util_close(*sqlite_handle); 
1605                         *sqlite_handle = NULL; 
1606                         goto FINISH_OFF;                
1607                 }
1608         }
1609         else {
1610                 EM_DEBUG_LOG(">>>>> DB Already Opened......");
1611         }
1612         
1613         ret = true;
1614         
1615 FINISH_OFF:
1616         if (err_code != NULL)
1617                 *err_code = error;      
1618
1619         EM_DEBUG_FUNC_END("ret [%d]", ret);
1620         return ret;     
1621 }
1622
1623 INTERNAL_FUNC sqlite3* emstorage_db_open(int *err_code)
1624 {
1625         EM_DEBUG_FUNC_BEGIN();
1626 #ifdef _MULTIPLE_DB_HANDLE
1627         sqlite3 *_db_handle = NULL;
1628 #endif
1629         
1630         int error = EMAIL_ERROR_NONE;
1631
1632         _initialize_shm_mutex(SHM_FILE_FOR_DB_LOCK, &shm_fd_for_db_lock, &mapped_for_db_lock);
1633
1634 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
1635         _initialize_shm_mutex(SHM_FILE_FOR_MAIL_ID_LOCK, &shm_fd_for_generating_mail_id, &mapped_for_generating_mail_id);
1636 #endif /*__FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
1637
1638         if (!em_db_open(&_db_handle, &error)) {
1639                 EM_DEBUG_EXCEPTION("em_db_open failed[%d]", error);
1640                 goto FINISH_OFF;
1641         }
1642
1643 #ifdef _MULTIPLE_DB_HANDLE
1644         emstorage_set_db_handle(_db_handle);
1645 #endif
1646         
1647 FINISH_OFF:
1648         if (err_code != NULL)
1649                 *err_code = error;      
1650
1651         EM_DEBUG_FUNC_END("ret [%p]", _db_handle);
1652         return _db_handle;      
1653 }
1654
1655 INTERNAL_FUNC int emstorage_db_close(int *err_code)
1656 {
1657         EM_DEBUG_FUNC_BEGIN();
1658 #ifdef _MULTIPLE_DB_HANDLE
1659         sqlite3 *_db_handle = emstorage_get_db_handle();
1660 #endif
1661         
1662         int error = EMAIL_ERROR_NONE;
1663         int ret = false;
1664
1665         DELETE_CRITICAL_SECTION(_transactionBeginLock);
1666         DELETE_CRITICAL_SECTION(_transactionEndLock);
1667         DELETE_CRITICAL_SECTION(_dbus_noti_lock); 
1668
1669         if (_db_handle) {
1670                 ret = db_util_close(_db_handle);
1671                 
1672                 if (ret != SQLITE_OK) {
1673                         EM_DEBUG_EXCEPTION(" db_util_close fail - %d", ret);
1674                         error = EMAIL_ERROR_DB_FAILURE;
1675                         ret = false;
1676                         goto FINISH_OFF;
1677                 }
1678 #ifdef _MULTIPLE_DB_HANDLE
1679                 emstorage_remove_db_handle();
1680 #endif
1681                 _db_handle = NULL;
1682         }
1683
1684         ret = true;
1685         
1686 FINISH_OFF:
1687         if (err_code != NULL)
1688                 *err_code = error;      
1689
1690         EM_DEBUG_FUNC_END("ret [%d]", ret);
1691         return ret;     
1692 }
1693
1694
1695 INTERNAL_FUNC int emstorage_open(int *err_code)
1696 {
1697         EM_DEBUG_FUNC_BEGIN();
1698         
1699         int ret = false;
1700         int error = EMAIL_ERROR_NONE;
1701
1702         int retValue;
1703
1704         retValue = mkdir(DB_PATH, DIRECTORY_PERMISSION);
1705
1706         EM_DEBUG_LOG("mkdir return- %d", retValue);
1707         EM_DEBUG_LOG("emstorage_open - before db_util_open - pid = %d", getpid());
1708
1709         if (emstorage_db_open(&error) == NULL) {
1710                 EM_DEBUG_EXCEPTION("emstorage_db_open failed[%d]", error);
1711                 goto FINISH_OFF;
1712         }
1713
1714         
1715         if (_open_counter++ == 0)
1716                 _emstorage_open_once(&error);
1717
1718         ret = true;
1719         
1720 FINISH_OFF:
1721         if (err_code != NULL)
1722                 *err_code = error;      
1723
1724         EM_DEBUG_FUNC_END("ret [%d]", ret);
1725         return ret;
1726 }
1727
1728
1729 INTERNAL_FUNC int emstorage_create_table(emstorage_create_db_type_t type, int *err_code)
1730 {
1731         EM_DEBUG_FUNC_BEGIN();
1732         
1733         int error = EMAIL_ERROR_NONE;
1734         int rc = -1, ret = false;
1735         char sql_query_string[QUERY_SIZE] = {0, };
1736         char *create_table_query[] =
1737 {
1738         /*  1. create mail_account_tbl */
1739         "CREATE TABLE mail_account_tbl \n"
1740         "(\n"
1741         "account_id                               INTEGER PRIMARY KEY,\n"
1742         "account_name                             VARCHAR(51),\n"
1743         "logo_icon_path                           VARCHAR(256),\n"
1744         "user_data                                BLOB,\n"
1745         "user_data_length                         INTEGER,\n"
1746         "account_svc_id                           INTEGER,\n"
1747         "sync_status                              INTEGER,\n"
1748         "sync_disabled                            INTEGER,\n"
1749         "default_mail_slot_size                   INTEGER,\n"
1750         "user_display_name                        VARCHAR(31),\n"
1751         "user_email_address                       VARCHAR(129),\n"
1752         "reply_to_address                         VARCHAR(129),\n"
1753         "return_address                           VARCHAR(129),\n"
1754         "incoming_server_type                     INTEGER,\n"
1755         "incoming_server_address                  VARCHAR(51),\n"
1756         "incoming_server_port_number              INTEGER,\n"
1757         "incoming_server_user_name                VARCHAR(51),\n"
1758         "incoming_server_password                 VARCHAR(51),\n"
1759         "incoming_server_secure_connection        INTEGER,\n"
1760         "retrieval_mode                           INTEGER,\n"
1761         "keep_mails_on_pop_server_after_download  INTEGER,\n"
1762         "check_interval                           INTEGER,\n"
1763         "auto_download_size                       INTEGER,\n"
1764         "outgoing_server_type                     INTEGER,\n"
1765         "outgoing_server_address                  VARCHAR(51),\n"
1766         "outgoing_server_port_number              INTEGER,\n"
1767         "outgoing_server_user_name                VARCHAR(51),\n"
1768         "outgoing_server_password                 VARCHAR(51),\n"
1769         "outgoing_server_secure_connection        INTEGER,\n"
1770         "outgoing_server_need_authentication      INTEGER,\n"
1771         "outgoing_server_use_same_authenticator   INTEGER,\n"
1772         "priority                                 INTEGER,\n"
1773         "keep_local_copy                          INTEGER,\n"
1774         "req_delivery_receipt                     INTEGER,\n"
1775         "req_read_receipt                         INTEGER,\n"
1776         "download_limit                           INTEGER,\n"
1777         "block_address                            INTEGER,\n"
1778         "block_subject                            INTEGER,\n"
1779         "display_name_from                        VARCHAR(256),\n"
1780         "reply_with_body                          INTEGER,\n"
1781         "forward_with_files                       INTEGER,\n"
1782         "add_myname_card                          INTEGER,\n"
1783         "add_signature                            INTEGER,\n"
1784         "signature                                VARCHAR(256),\n"
1785         "add_my_address_to_bcc                    INTEGER,\n"
1786         "pop_before_smtp                          INTEGER,\n"
1787         "incoming_server_requires_apop            INTEGER,\n"
1788         "smime_type                               INTEGER,\n"
1789         "certificate_path                         VARCHAR(256),\n"
1790         "cipher_type                              INTEGER,\n"
1791         "digest_type                              INTEGER\n"
1792         "); \n ",
1793         /*  2. create mail_box_tbl */
1794         "CREATE TABLE mail_box_tbl \n"
1795         "(\n"
1796         "  mailbox_id                   INTEGER \n"
1797         ", account_id                   INTEGER \n"
1798         ", local_yn                     INTEGER \n"
1799         ", mailbox_name                 VARCHAR(256) \n"
1800         ", mailbox_type                 INTEGER \n"
1801         ", alias                        VARCHAR(256) \n"
1802         ", sync_with_server_yn          INTEGER \n"
1803         ", modifiable_yn                INTEGER \n"
1804         ", total_mail_count_on_server   INTEGER \n"
1805         ", has_archived_mails           INTEGER \n"
1806         ", mail_slot_size               INTEGER \n"
1807         ", last_sync_time               DATETIME \n"
1808         "); \n ",
1809
1810         /*  3. create mail_read_mail_uid_tbl */
1811         "CREATE TABLE mail_read_mail_uid_tbl \n"
1812         "(\n"
1813         "  account_id                   INTEGER \n"
1814         ", mailbox_id                   VARCHAR(129) \n"
1815         ", local_uid                    INTEGER \n"
1816         ", mailbox_name                 VARCHAR(256) \n"
1817         ", s_uid                        VARCHAR(129) \n"
1818         ", data1                        INTEGER \n"
1819         ", data2                        VARCHAR(257) \n"
1820         ", flag                         INTEGER \n"
1821         ", idx_num                      INTEGER PRIMARY KEY \n"
1822         "); \n",
1823         /*  4. create mail_rule_tbl */
1824         "CREATE TABLE mail_rule_tbl \n"
1825         "(\n"
1826         "  account_id                   INTEGER \n"
1827         ", rule_id                      INTEGER PRIMARY KEY\n"
1828         ", type                         INTEGER \n"
1829         ", value                        VARCHAR(257) \n"
1830         ", action_type                  INTEGER \n"
1831         ", target_mailbox_id            INTEGER \n"
1832         ", flag1                        INTEGER \n"
1833         ", flag2                        INTEGER \n"
1834         "); \n ",
1835         /*  5. create mail_tbl */
1836         "CREATE TABLE mail_tbl \n"
1837         "(\n"
1838         "  mail_id                      INTEGER PRIMARY KEY \n"
1839         ", account_id                   INTEGER \n"
1840         ", mailbox_id                   INTEGER \n"
1841         ", mailbox_name                 VARCHAR(129) \n"
1842         ", mailbox_type                 INTEGER \n"
1843         ", subject                      TEXT \n"
1844         ", date_time                    DATETIME \n"
1845         ", server_mail_status           INTEGER \n"
1846         ", server_mailbox_name          VARCHAR(129) \n"
1847         ", server_mail_id               VARCHAR(129) \n"
1848         ", message_id                   VARCHAR(257) \n"
1849         ", full_address_from            TEXT \n"
1850         ", full_address_reply           TEXT \n"
1851         ", full_address_to              TEXT \n"
1852         ", full_address_cc              TEXT \n"
1853         ", full_address_bcc             TEXT \n"
1854         ", full_address_return          TEXT \n"
1855         ", email_address_sender         TEXT collation user1 \n"
1856         ", email_address_recipient      TEXT collation user1 \n"
1857         ", alias_sender                 TEXT \n"
1858         ", alias_recipient              TEXT \n"
1859         ", body_download_status         INTEGER \n"
1860         ", file_path_plain              VARCHAR(257) \n"
1861         ", file_path_html               VARCHAR(257) \n"
1862         ", file_path_mime_entity        VARCHAR(257) \n"
1863         ", mail_size                    INTEGER \n"
1864         ", flags_seen_field             BOOLEAN \n"
1865         ", flags_deleted_field          BOOLEAN \n"
1866         ", flags_flagged_field          BOOLEAN \n"
1867         ", flags_answered_field         BOOLEAN \n"
1868         ", flags_recent_field           BOOLEAN \n"
1869         ", flags_draft_field            BOOLEAN \n"
1870         ", flags_forwarded_field        BOOLEAN \n"
1871         ", DRM_status                   INTEGER \n"
1872         ", priority                     INTEGER \n"
1873         ", save_status                  INTEGER \n"
1874         ", lock_status                  INTEGER \n"
1875         ", report_status                INTEGER \n"
1876         ", attachment_count             INTEGER \n"
1877         ", inline_content_count         INTEGER \n"
1878         ", thread_id                    INTEGER \n"
1879         ", thread_item_count            INTEGER \n"
1880         ", preview_text                 TEXT \n"
1881         ", meeting_request_status       INTEGER \n"
1882         ", message_class                INTEGER \n"
1883         ", digest_type                  INTEGER \n"
1884         ", smime_type                   INTEGER \n"
1885         ", FOREIGN KEY(account_id)      REFERENCES mail_account_tbl(account_id) \n"
1886         "); \n ",
1887         
1888         /*  6. create mail_attachment_tbl */
1889         "CREATE TABLE mail_attachment_tbl \n"
1890         "(\n"
1891         "  attachment_id                    INTEGER PRIMARY KEY"
1892         ", attachment_name                  VARCHAR(257) \n"
1893         ", attachment_path                  VARCHAR(257) \n"
1894         ", attachment_size                  INTEGER \n"
1895         ", mail_id                          INTEGER \n"
1896         ", account_id                       INTEGER \n"
1897         ", mailbox_id                       INTEGER \n"
1898         ", attachment_save_status           INTEGER \n"
1899         ", attachment_drm_type              INTEGER \n"
1900         ", attachment_drm_method            INTEGER \n"
1901         ", attachment_inline_content_status INTEGER \n"
1902         ", attachment_mime_type             VARCHAR(257) \n"
1903 #ifdef __ATTACHMENT_OPTI__
1904         ", encoding                     INTEGER \n"
1905         ", section                      varchar(257) \n"
1906 #endif
1907         "); \n ",
1908
1909 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
1910
1911         "CREATE TABLE mail_partial_body_activity_tbl  \n"
1912         "( \n"
1913         "  account_id                   INTEGER \n"
1914         ", mail_id                      INTEGER \n"
1915         ", server_mail_id               INTEGER \n"
1916         ", activity_id                  INTEGER PRIMARY KEY \n"
1917         ", activity_type                INTEGER \n"
1918         ", mailbox_id                   INTEGER \n"
1919         ", mailbox_name                 VARCHAR(4000) \n"
1920         "); \n ",
1921 #endif
1922
1923         "CREATE TABLE mail_meeting_tbl \n"
1924         "(\n"
1925         "  mail_id                     INTEGER PRIMARY KEY \n"
1926         ", account_id                  INTEGER \n"
1927         ", mailbox_id                  INTEGER \n"
1928         ", meeting_response            INTEGER \n"
1929         ", start_time                  INTEGER \n"
1930         ", end_time                    INTEGER \n"
1931         ", location                    TEXT \n"
1932         ", global_object_id            TEXT \n"
1933         ", offset                      INTEGER \n"
1934         ", standard_name               TEXT \n"
1935         ", standard_time_start_date    INTEGER \n"
1936         ", standard_bias               INTEGER \n"
1937         ", daylight_name               TEXT \n"
1938         ", daylight_time_start_date    INTEGER \n"
1939         ", daylight_bias               INTEGER \n"
1940         "); \n ",
1941
1942 #ifdef __FEATURE_LOCAL_ACTIVITY__
1943         "CREATE TABLE mail_local_activity_tbl \n"
1944         "( \n"
1945         "  activity_id                 INTEGER \n"
1946         ", account_id                  INTEGER \n"
1947         ", mail_id                     INTEGER \n"
1948         ", activity_type               INTEGER \n"
1949         ", server_mailid               VARCHAR(129) \n"
1950         ", src_mbox                    VARCHAR(129) \n"
1951         ", dest_mbox                   VARCHAR(129) \n"
1952         "); \n ",
1953 #endif
1954         "CREATE TABLE mail_certificate_tbl \n"
1955         "( \n"
1956         "  certificate_id              INTEGER \n"
1957         ", issue_year                  INTEGER \n"
1958         ", issue_month                 INTEGER \n"
1959         ", issue_day                   INTEGER \n"
1960         ", expiration_year             INTEGER \n"
1961         ", expiration_month            INTEGER \n"
1962         ", expiration_day              INTEGER \n"
1963         ", issue_organization_name     VARCHAR(256) \n"
1964         ", email_address               VARCHAR(129) \n"
1965         ", subject_str                 VARCHAR(256) \n"
1966         ", filepath                    VARCHAR(256) \n"
1967         ", password                    VARCHAR(51) \n"
1968         "); \n ",
1969         NULL,
1970 };
1971         
1972         sqlite3 *local_db_handle = emstorage_get_db_connection();
1973  
1974         EM_DEBUG_LOG("local_db_handle = %p.", local_db_handle);
1975
1976         char *sql;
1977         char **result;
1978         
1979         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_account_tbl';";
1980         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
1981         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1982                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
1983
1984         EM_DEBUG_LOG("emstorage_create_table - result[1] = %s %c", result[1], result[1]);
1985
1986         if (atoi(result[1]) < 1) {
1987                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
1988                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1989                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
1990                 
1991                 EM_DEBUG_LOG("CREATE TABLE mail_account_tbl");
1992                 
1993                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], sizeof(sql_query_string));
1994
1995                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1996                 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)));
1997
1998                 /*  create mail_account_tbl unique index */
1999                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE UNIQUE INDEX mail_account_idx1 ON mail_account_tbl (account_id)");
2000                 
2001                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2002                 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)));
2003                 
2004                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2005
2006         } /*  mail_account_tbl */
2007         else if (type == EMAIL_CREATE_DB_CHECK)  {              
2008                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], NULL, NULL, NULL), rc);
2009                 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)));
2010         }
2011
2012         sqlite3_free_table(result); 
2013         
2014                         
2015         /*  2. create mail_box_tbl */
2016         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_box_tbl';";
2017
2018         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2019         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2020                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2021
2022         if (atoi(result[1]) < 1) {
2023                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2024                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2025                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2026
2027                 EM_DEBUG_LOG("CREATE TABLE mail_box_tbl");
2028                 
2029                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_BOX_TBL], sizeof(sql_query_string));
2030                 
2031                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2032                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2033                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2034                 
2035                 /*  create mail_local_mailbox_tbl unique index */
2036                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE UNIQUE INDEX mail_box_idx1 ON mail_box_tbl (account_id, local_yn, mailbox_name)");
2037                 
2038                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2039                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2040                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2041                 
2042                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2043                 
2044         } /*  mail_box_tbl */
2045         else if (type == EMAIL_CREATE_DB_CHECK)  {              
2046                 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_BOX_TBL], NULL, NULL, NULL);
2047                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2048                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_BOX_TBL], rc, sqlite3_errmsg(local_db_handle)));
2049         }
2050         sqlite3_free_table(result); 
2051         
2052         /*  3. create mail_read_mail_uid_tbl */
2053         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_read_mail_uid_tbl';";
2054         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2055         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2056                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2057         
2058
2059         if (atoi(result[1]) < 1) {
2060                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2061                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2062                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2063                 
2064                 EM_DEBUG_LOG("CREATE TABLE mail_read_mail_uid_tbl");
2065                 
2066                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], sizeof(sql_query_string));
2067                 
2068                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2069                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2070                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2071                 
2072                 /*  create mail_read_mail_uid_tbl unique index */
2073                 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)");
2074                 
2075                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2076                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2077                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2078                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2079                 
2080         } /*  mail_read_mail_uid_tbl */
2081         else if (type == EMAIL_CREATE_DB_CHECK)  {              
2082                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], NULL, NULL, NULL), rc);
2083                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2084                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], rc, sqlite3_errmsg(local_db_handle)));
2085         }
2086         sqlite3_free_table(result);
2087         
2088         
2089         /*  4. create mail_rule_tbl */
2090         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_rule_tbl';";
2091
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
2101                 EM_DEBUG_LOG("CREATE TABLE mail_rule_tbl");
2102                 
2103                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_RULE_TBL], sizeof(sql_query_string));
2104                 
2105                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2106                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2107                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2108                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2109                 
2110         } /*  mail_rule_tbl */
2111         else if (type == EMAIL_CREATE_DB_CHECK)  {              
2112                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_RULE_TBL], NULL, NULL, NULL), rc);
2113                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2114                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_RULE_TBL], rc, sqlite3_errmsg(local_db_handle)));
2115         }
2116         sqlite3_free_table(result);
2117         
2118         
2119         /*  5. create mail_tbl */
2120         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_tbl';";
2121         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2122         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2123                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2124         
2125         if (atoi(result[1]) < 1) {
2126                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2127                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2128                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2129                 EM_DEBUG_LOG("CREATE TABLE mail_tbl");
2130                 
2131                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TBL], sizeof(sql_query_string));
2132                 
2133                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2134                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2135                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2136                 
2137                 /*  create mail_tbl unique index */
2138                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE UNIQUE INDEX mail_idx1 ON mail_tbl (mail_id, account_id)");
2139                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2140                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2141                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2142
2143                 /*  create mail_tbl index for date_time */
2144                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE INDEX mail_idx_date_time ON mail_tbl (date_time)");
2145                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2146                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2147                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2148
2149                 /*  create mail_tbl index for thread_item_count */
2150                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE INDEX mail_idx_thread_mail_count ON mail_tbl (thread_item_count)");
2151                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2152                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2153                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2154
2155                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2156                 
2157                 /*  just one time call */
2158 /*              EFTSInitFTSIndex(FTS_EMAIL_IDX); */
2159         } /*  mail_tbl */
2160         else if (type == EMAIL_CREATE_DB_CHECK)  {              
2161                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TBL], NULL, NULL, NULL), rc);
2162                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2163                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TBL], rc, sqlite3_errmsg(local_db_handle)));
2164         }
2165         sqlite3_free_table(result);
2166         
2167         
2168         /*  6. create mail_attachment_tbl */
2169         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_attachment_tbl';";
2170         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2171         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2172                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2173
2174         if (atoi(result[1]) < 1) {
2175                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2176                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2177                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2178                 EM_DEBUG_LOG("CREATE TABLE mail_attachment_tbl");
2179                 
2180                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], sizeof(sql_query_string));
2181                 
2182                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2183                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2184                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2185                 
2186                 /*  create mail_attachment_tbl unique index */
2187                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE UNIQUE INDEX mail_attachment_idx1 ON mail_attachment_tbl (mail_id, attachment_id) ");
2188                 
2189                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2190                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2191                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2192                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2193                 
2194         } /*  mail_attachment_tbl */
2195         else if (type == EMAIL_CREATE_DB_CHECK)  {              
2196                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], NULL, NULL, NULL), rc);
2197                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2198                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], rc, sqlite3_errmsg(local_db_handle)));
2199         }
2200         sqlite3_free_table(result);
2201         
2202 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
2203
2204         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_partial_body_activity_tbl';";
2205         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2206         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2207                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2208         
2209         if (atoi(result[1]) < 1) {
2210                 
2211                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2212                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2213                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2214
2215                 EM_DEBUG_LOG("CREATE TABLE mail_partial_body_activity_tbl");
2216                 
2217                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], sizeof(sql_query_string));
2218                 
2219                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2220                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2221                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2222                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2223                 
2224         } /*  mail_rule_tbl */
2225         else if (type == EMAIL_CREATE_DB_CHECK)  {              
2226                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2227                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2228                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2229         }
2230         sqlite3_free_table(result);
2231         
2232 #endif /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
2233
2234         /*  create mail_meeting_tbl */
2235         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_meeting_tbl';";
2236         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2237         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2238                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2239         
2240         if (atoi(result[1]) < 1) {
2241                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2242                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2243                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2244                 
2245                 EM_DEBUG_LOG("CREATE TABLE mail_meeting_tbl");
2246                 
2247                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_MEETING_TBL], sizeof(sql_query_string));
2248                 
2249                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2250                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2251                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2252                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE UNIQUE INDEX mail_meeting_idx1 ON mail_meeting_tbl (mail_id)");
2253                 
2254                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2255                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2256                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2257                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2258                 
2259         } /*  mail_contact_sync_tbl */
2260         else if (type == EMAIL_CREATE_DB_CHECK)  {              
2261                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], NULL, NULL, NULL), rc);
2262                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2263                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], rc, sqlite3_errmsg(local_db_handle)));
2264         }
2265         sqlite3_free_table(result);
2266
2267 #ifdef __FEATURE_LOCAL_ACTIVITY__
2268         
2269                 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_local_activity_tbl';";
2270                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2271                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2272                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2273                 
2274                 if (atoi(result[1]) < 1) {
2275                         
2276                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2277                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2278                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2279         
2280                         EM_DEBUG_LOG(" CREATE TABLE mail_local_activity_tbl");
2281                         
2282                         SNPRINTF(sql_query_string, sizeof(sql_query_string), create_table_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL]);
2283                         
2284                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2285                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2286                                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2287                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2288                         
2289                 } /*  mail_rule_tbl */
2290                 else if (type == EMAIL_CREATE_DB_CHECK)  {      
2291                         rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL);
2292                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2293                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2294                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2295                 }
2296                 sqlite3_free_table(result);
2297                 
2298 #endif /*  __FEATURE_LOCAL_ACTIVITY__ */
2299         /*  create mail_certificate_tbl */
2300         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_certificate_tbl';";
2301         /*  rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL);   */
2302         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2303         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)));
2304
2305         if (atoi(result[1]) < 1) {
2306                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2307                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2308
2309                 EM_DEBUG_LOG("CREATE TABLE mail_certificate_tbl");
2310
2311                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], sizeof(sql_query_string));
2312
2313                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2314                 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)));
2315                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE UNIQUE INDEX mail_certificate_idx1 ON mail_certificate_tbl (certificate_id)");
2316
2317                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2318                 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)));
2319                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2320         } /*  mail_contact_sync_tbl */
2321         else if (type == EMAIL_CREATE_DB_CHECK)  {                
2322                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], NULL, NULL, NULL), rc);
2323                 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)));
2324         }
2325         sqlite3_free_table(result);
2326
2327         ret = true;
2328         
2329 FINISH_OFF:
2330         if (ret == true) {
2331                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2332         }
2333         else {
2334                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
2335         }
2336  
2337         _DISCONNECT_DB;
2338         
2339         if (err_code != NULL)
2340                 *err_code = error;
2341
2342         EM_DEBUG_FUNC_END("ret [%d]", ret);
2343         return ret;
2344 }
2345
2346 /* Query series --------------------------------------------------------------*/
2347
2348 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)
2349 {
2350         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);
2351         int rc = -1;
2352         int error = EMAIL_ERROR_NONE;
2353         DB_STMT hStmt = NULL;
2354         char sql_query_string[QUERY_SIZE] = {0, };
2355         char err_msg[1024];
2356         char **result;
2357         sqlite3 *local_db_handle = NULL;
2358
2359         if (!input_conditional_clause || (!output_total_mail_count && !output_unseen_mail_count)) {
2360                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2361                 return EMAIL_ERROR_INVALID_PARAM;
2362         }
2363
2364         memset(&sql_query_string, 0x00, sizeof(sql_query_string));
2365         local_db_handle = emstorage_get_db_connection();
2366
2367         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2368
2369         SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT COUNT(*) FROM mail_tbl");
2370         EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
2371
2372         if (output_total_mail_count)  {
2373                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
2374                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2375                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2376
2377                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2378                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2379                         ("sqlite3_step fail:%d", rc));
2380                 _get_stmt_field_data_int(hStmt, output_total_mail_count, 0);
2381         }
2382
2383         if (output_unseen_mail_count)  {
2384                 EM_SAFE_STRCAT(sql_query_string, " AND flags_seen_field = 0 ");
2385
2386                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
2387                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2388                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2389
2390                 *output_unseen_mail_count = atoi(result[1]);
2391                 sqlite3_free_table(result);
2392         }
2393
2394 FINISH_OFF:
2395
2396         if (hStmt != NULL)  {
2397                 rc = sqlite3_finalize(hStmt);
2398                 if (rc != SQLITE_OK)  {
2399                         EM_DEBUG_LOG("sqlite3_finalize failed [%d] : %s", rc, err_msg);
2400                         error = EMAIL_ERROR_DB_FAILURE;
2401                 }
2402         }
2403
2404         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2405         _DISCONNECT_DB;
2406
2407         EM_DEBUG_FUNC_END("error [%d]", error);
2408         return error;
2409 }
2410
2411 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)
2412 {
2413         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);
2414
2415         int      i = 0;
2416         int      count = 0;
2417         int      rc = -1;
2418         int      cur_query = 0;
2419         int      col_index;
2420         int      error = EMAIL_ERROR_NONE;
2421         int     *result_mail_id_list = NULL;
2422         char   **result = NULL;
2423         char     sql_query_string[QUERY_SIZE] = {0, };
2424         sqlite3 *local_db_handle = emstorage_get_db_connection();
2425
2426         EM_IF_NULL_RETURN_VALUE(input_conditional_clause, EMAIL_ERROR_INVALID_PARAM);
2427         EM_IF_NULL_RETURN_VALUE(output_mail_id_list, EMAIL_ERROR_INVALID_PARAM);
2428         EM_IF_NULL_RETURN_VALUE(output_mail_id_count, EMAIL_ERROR_INVALID_PARAM);
2429
2430         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2431
2432         /* Composing query */
2433         SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, "SELECT mail_id FROM mail_tbl ");
2434         EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
2435
2436         EM_DEBUG_LOG("query[%s].", sql_query_string);
2437
2438         /* Performing query */
2439         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2440         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2441                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2442
2443         col_index = 1;
2444
2445         /*  to get mail list */
2446         if (count == 0) {
2447                 EM_DEBUG_EXCEPTION("No mail found...");
2448                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2449                 goto FINISH_OFF;
2450         }
2451
2452         EM_DEBUG_LOG("There are [%d] mails.", count);
2453
2454         if (!(result_mail_id_list = (int*)em_malloc(sizeof(int) * count))) {
2455                 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
2456                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2457                 goto FINISH_OFF;
2458         }
2459
2460         EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2461
2462         for (i = 0; i < count; i++)
2463                 _get_table_field_data_int(result, result_mail_id_list + i, col_index++);
2464
2465         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >> ", count);
2466
2467         *output_mail_id_list  = result_mail_id_list;
2468         *output_mail_id_count = count;
2469
2470 FINISH_OFF:
2471
2472         if(result)
2473                 sqlite3_free_table(result);
2474
2475         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2476         _DISCONNECT_DB;
2477
2478         if(error != EMAIL_ERROR_NONE)
2479                 EM_SAFE_FREE(result_mail_id_list);
2480
2481         EM_DEBUG_FUNC_END("error [%d]", error);
2482         return error;
2483 }
2484
2485 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)
2486 {
2487         EM_DEBUG_FUNC_BEGIN();
2488         EM_PROFILE_BEGIN(emstorage_query_mail_list_func);
2489                    
2490         int i = 0, count = 0, rc = -1, to_get_count = (result_mail_list)?0:1;
2491         int local_inline_content_count = 0, local_attachment_count = 0;
2492         int cur_query = 0, base_count = 0, col_index;
2493         int ret = false, error = EMAIL_ERROR_NONE;
2494         char *date_time_string = NULL;
2495         char **result = NULL, sql_query_string[QUERY_SIZE] = {0, };
2496         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 ";
2497         email_mail_list_item_t *mail_list_item_from_tbl = NULL;
2498         sqlite3 *local_db_handle = emstorage_get_db_connection();
2499
2500         EM_IF_NULL_RETURN_VALUE(conditional_clause, false);
2501         EM_IF_NULL_RETURN_VALUE(result_count, false);
2502
2503         EMSTORAGE_START_READ_TRANSACTION(transaction);
2504         
2505         /*  select clause */
2506         if (to_get_count) /*  count only */
2507                 cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, "SELECT mail_id FROM mail_tbl");
2508         else /*  mail list in plain form */
2509                 cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, "SELECT %s FROM mail_tbl ", field_list);
2510
2511         /* cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, conditional_clause); This code caused some crashes.*/
2512         strncat(sql_query_string, conditional_clause, QUERY_SIZE - cur_query);
2513         
2514         EM_DEBUG_LOG("emstorage_query_mail_list : query[%s].", sql_query_string);
2515
2516         /*  performing query            */      
2517         EM_PROFILE_BEGIN(emstorage_query_mail_list_performing_query);
2518         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2519         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2520                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2521         EM_PROFILE_END(emstorage_query_mail_list_performing_query);
2522         
2523         if (!base_count) 
2524                 base_count = ({ int i=0; char *tmp = field_list; for (i=0; tmp && *(tmp + 1); tmp = index(tmp + 1, ','), i++); i; });
2525
2526         col_index = base_count;
2527
2528         EM_DEBUG_LOG("base_count [%d]", base_count);
2529
2530         if (to_get_count) {     
2531                 /*  to get count */
2532                 if (!count) {
2533                         EM_DEBUG_EXCEPTION("No mail found...");
2534                         ret = false;
2535                         error= EMAIL_ERROR_MAIL_NOT_FOUND;
2536                         goto FINISH_OFF;
2537                 }
2538                 EM_DEBUG_LOG("There are [%d] mails.", count);
2539         }
2540         else {
2541                 /*  to get mail list */
2542                 if (!count) {
2543                         EM_DEBUG_EXCEPTION("No mail found...");                 
2544                         ret = false;
2545                         error= EMAIL_ERROR_MAIL_NOT_FOUND;
2546                         goto FINISH_OFF;
2547                 }
2548                 
2549                 EM_DEBUG_LOG("There are [%d] mails.", count);
2550                 if (!(mail_list_item_from_tbl = (email_mail_list_item_t*)em_malloc(sizeof(email_mail_list_item_t) * count))) {
2551                         EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
2552                         error = EMAIL_ERROR_OUT_OF_MEMORY;
2553                         goto FINISH_OFF;
2554                 }
2555
2556                 EM_PROFILE_BEGIN(emstorage_query_mail_list_loop);
2557                 EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");     
2558                 for (i = 0; i < count; i++) {
2559                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_id), col_index++);
2560                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].account_id), col_index++);
2561                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mailbox_id), col_index++);
2562                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].from, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2563                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].from_email_address, MAX_EMAIL_ADDRESS_LENGTH, 1, col_index++);
2564                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].recipients, STRING_LENGTH_FOR_DISPLAY,  1, col_index++);
2565                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].subject, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2566                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].is_text_downloaded), col_index++);
2567                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_seen_field), col_index++);
2568                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_deleted_field), col_index++);
2569                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_flagged_field), col_index++);
2570                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_answered_field), col_index++);
2571                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_recent_field), col_index++);
2572                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_draft_field), col_index++);
2573                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_forwarded_field), col_index++);
2574                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].has_drm_attachment), col_index++);
2575                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].priority), col_index++);
2576                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].save_status), col_index++);
2577                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].is_locked), col_index++);
2578                         _get_table_field_data_int(result, &local_attachment_count, col_index++);
2579                         _get_table_field_data_int(result, &local_inline_content_count, col_index++);
2580                         _get_table_field_data_time_t(result, &(mail_list_item_from_tbl[i].date_time), col_index++);
2581                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].previewBodyText, MAX_PREVIEW_TEXT_LENGTH, 1, col_index++);
2582                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_id), col_index++);
2583                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_item_count), col_index++);
2584                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].is_meeting_request), col_index++);
2585                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].message_class), col_index++);
2586                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].smime_type), col_index++);
2587
2588                         mail_list_item_from_tbl[i].has_attachment = ((local_attachment_count - local_inline_content_count)>0)?1:0;
2589                 }
2590                 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >> ", count);
2591                 EM_PROFILE_END(emstorage_query_mail_list_loop);
2592         }
2593
2594         sqlite3_free_table(result);
2595         result = NULL;
2596         ret = true;
2597         
2598 FINISH_OFF:
2599         EM_DEBUG_LOG("COUNT [%d]", count);
2600
2601         if (to_get_count)
2602                 *result_count = count;
2603         else {
2604                 if (ret == true)  {
2605                         if (result_mail_list)
2606                                 *result_mail_list = mail_list_item_from_tbl;
2607                         *result_count = count;
2608                 }
2609                 else 
2610                         EM_SAFE_FREE(mail_list_item_from_tbl);
2611         }
2612
2613         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2614         _DISCONNECT_DB;
2615         
2616         EM_SAFE_FREE(date_time_string);
2617
2618         if (err_code != NULL)
2619                 *err_code = error;
2620
2621         EM_PROFILE_END(emstorage_query_mail_list_func);
2622         EM_DEBUG_FUNC_END("ret [%d]", ret);
2623         return ret;
2624 }
2625
2626
2627 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)
2628 {
2629         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);
2630
2631         if (!conditional_clause) {
2632                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2633                 
2634                 if (err_code != NULL)
2635                         *err_code = EMAIL_ERROR_INVALID_PARAM;
2636                 return false;
2637         }
2638         
2639         int i, col_index = FIELD_COUNT_OF_MAIL_TBL, rc, ret = false, count;
2640         int error = EMAIL_ERROR_NONE;
2641         char **result = NULL, sql_query_string[QUERY_SIZE] = {0, };
2642         emstorage_mail_tbl_t* p_data_tbl = NULL;
2643         sqlite3 *local_db_handle = emstorage_get_db_connection();
2644
2645         EMSTORAGE_START_READ_TRANSACTION(transaction);
2646
2647         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl %s", conditional_clause);
2648
2649         EM_DEBUG_LOG("Query [%s]", sql_query_string);
2650         
2651         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2652         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2653                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2654
2655         if (!count) {
2656                 EM_DEBUG_EXCEPTION("No mail found...");                 
2657                 ret = false;
2658                 error= EMAIL_ERROR_MAIL_NOT_FOUND;
2659                 goto FINISH_OFF;
2660         }
2661         
2662         EM_DEBUG_LOG("There are [%d] mails.", count);
2663         if (!(p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * count))) {
2664                 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
2665                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2666                 goto FINISH_OFF;
2667         }
2668
2669         EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");     
2670         for (i = 0; i < count; i++) {
2671                 _get_table_field_data_int   (result, &(p_data_tbl[i].mail_id), col_index++);
2672                 _get_table_field_data_int   (result, &(p_data_tbl[i].account_id), col_index++);
2673                 _get_table_field_data_int   (result, &(p_data_tbl[i].mailbox_id), col_index++);
2674                 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
2675                 _get_table_field_data_int   (result, &(p_data_tbl[i].mailbox_type), col_index++);
2676                 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 1, col_index++);
2677                 _get_table_field_data_time_t (result, &(p_data_tbl[i].date_time), col_index++);
2678                 _get_table_field_data_int   (result, &(p_data_tbl[i].server_mail_status), col_index++);
2679                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
2680                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
2681                 _get_table_field_data_string(result, &(p_data_tbl[i].message_id), 0, col_index++);
2682                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_from), 1, col_index++);
2683                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_reply), 1, col_index++);
2684                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_to), 1, col_index++);
2685                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_cc), 1, col_index++);
2686                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_bcc), 1, col_index++);
2687                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_return), 1, col_index++);
2688                 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_sender), 1, col_index++);
2689                 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_recipient), 1, col_index++);
2690                 _get_table_field_data_string(result, &(p_data_tbl[i].alias_sender), 1, col_index++);
2691                 _get_table_field_data_string(result, &(p_data_tbl[i].alias_recipient), 1, col_index++);
2692                 _get_table_field_data_int   (result, &(p_data_tbl[i].body_download_status), col_index++);
2693                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
2694                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
2695                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
2696                 _get_table_field_data_int   (result, &(p_data_tbl[i].mail_size), col_index++);
2697                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_seen_field), col_index++);
2698                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_deleted_field), col_index++);
2699                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_flagged_field), col_index++);
2700                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_answered_field), col_index++);
2701                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_recent_field), col_index++);
2702                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_draft_field), col_index++);
2703                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_forwarded_field), col_index++);
2704                 _get_table_field_data_int   (result, &(p_data_tbl[i].DRM_status), col_index++);
2705                 _get_table_field_data_int   (result, &(p_data_tbl[i].priority), col_index++);
2706                 _get_table_field_data_int   (result, &(p_data_tbl[i].save_status), col_index++);
2707                 _get_table_field_data_int   (result, &(p_data_tbl[i].lock_status), col_index++);
2708                 _get_table_field_data_int   (result, &(p_data_tbl[i].report_status), col_index++);
2709                 _get_table_field_data_int   (result, &(p_data_tbl[i].attachment_count), col_index++);
2710                 _get_table_field_data_int   (result, &(p_data_tbl[i].inline_content_count), col_index++);
2711                 _get_table_field_data_int   (result, &(p_data_tbl[i].thread_id), col_index++);
2712                 _get_table_field_data_int   (result, &(p_data_tbl[i].thread_item_count), col_index++);
2713                 _get_table_field_data_string(result, &(p_data_tbl[i].preview_text), 1, col_index++);
2714                 _get_table_field_data_int   (result, &(p_data_tbl[i].meeting_request_status), col_index++);
2715                 _get_table_field_data_int   (result, &(p_data_tbl[i].message_class), col_index++);
2716                 _get_table_field_data_int   (result, &(p_data_tbl[i].digest_type), col_index++);
2717                 _get_table_field_data_int   (result, &(p_data_tbl[i].smime_type), col_index++);
2718                 /*  check real body file... */
2719                 if (p_data_tbl[i].body_download_status) {
2720                         struct stat buf;
2721                         
2722                         if (p_data_tbl[i].file_path_html && strlen(p_data_tbl[i].file_path_html) > 0) {
2723                                 if (stat(p_data_tbl[i].file_path_html, &buf) == -1) {
2724                                         EM_DEBUG_LINE;
2725                                         p_data_tbl[i].body_download_status = 0;
2726                                 }
2727                         }
2728                         else if (p_data_tbl[i].file_path_plain && strlen(p_data_tbl[i].file_path_plain) > 0) {
2729                                 if (stat(p_data_tbl[i].file_path_plain, &buf) == -1){
2730                                         EM_DEBUG_LINE;
2731                                         p_data_tbl[i].body_download_status = 0;
2732                                 }
2733                         }
2734                         else 
2735                                 p_data_tbl[i].body_download_status = 0;
2736                 }
2737         }
2738         
2739         ret = true;
2740         
2741 FINISH_OFF:
2742         if(result)
2743                 sqlite3_free_table(result);
2744
2745         if (ret == true)  {
2746                 if (result_mail_tbl)
2747                         *result_mail_tbl = p_data_tbl;
2748                 *result_count = count;
2749         }
2750         else 
2751                 EM_SAFE_FREE(p_data_tbl);
2752         
2753         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2754         _DISCONNECT_DB;
2755         
2756         if (err_code != NULL)
2757                 *err_code = error;
2758
2759         EM_DEBUG_FUNC_END("ret [%d]", ret);
2760         return ret;
2761 }
2762
2763 INTERNAL_FUNC int emstorage_query_mailbox_tbl(const char *input_conditional_clause, int input_get_mail_count,  int input_transaction, emstorage_mailbox_tbl_t **output_mailbox_list, int *output_mailbox_count)
2764 {
2765         EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_get_mail_count[%d], input_transaction[%d], output_mailbox_list[%p], output_mailbox_count[%d]", input_conditional_clause, input_get_mail_count, input_transaction, output_mailbox_list, output_mailbox_count);
2766
2767         int i = 0;
2768         int rc;
2769         int count = 0;
2770         int col_index = 0;
2771         int error = EMAIL_ERROR_NONE;
2772         int read_count = 0;
2773         int total_count = 0;
2774         char **result;
2775         char sql_query_string[QUERY_SIZE] = {0, };
2776         char *fields = "MBT.mailbox_id, MBT.account_id, local_yn, MBT.mailbox_name, MBT.mailbox_type, alias, sync_with_server_yn, modifiable_yn, total_mail_count_on_server, has_archived_mails, mail_slot_size, last_sync_time ";
2777         emstorage_mailbox_tbl_t* p_data_tbl = NULL;
2778         DB_STMT hStmt = NULL;
2779         sqlite3 *local_db_handle = emstorage_get_db_connection();
2780
2781         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2782
2783         if (input_get_mail_count == 0) {        /*  without mail count  */
2784                 col_index = 12;
2785                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT %s FROM mail_box_tbl AS MBT %s", fields, input_conditional_clause);
2786         }
2787         else {  /*  with read count and total count */
2788                 col_index = 14;
2789                 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 GROUP BY mailbox_name) AS MT ON MBT.mailbox_name = MT.mailbox_name %s", fields, input_conditional_clause);
2790         }
2791
2792         EM_DEBUG_LOG("query[%s]", sql_query_string);
2793
2794         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
2795         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2796                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)))
2797
2798         EM_DEBUG_LOG("result count [%d]", count);
2799
2800         if(count == 0) {
2801                 EM_DEBUG_EXCEPTION("Can't find mailbox");
2802                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
2803                 goto FINISH_OFF;
2804         }
2805
2806         if ((p_data_tbl = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t) * count)) == NULL) {
2807                 EM_DEBUG_EXCEPTION("malloc failed...");
2808                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2809                 goto FINISH_OFF;
2810         }
2811
2812         for (i = 0; i < count; i++)  {
2813                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
2814                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
2815                 _get_table_field_data_int(result, &(p_data_tbl[i].local_yn), col_index++);
2816                 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
2817                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].mailbox_type), col_index++);
2818                 _get_table_field_data_string(result, &(p_data_tbl[i].alias), 0, col_index++);
2819                 _get_table_field_data_int(result, &(p_data_tbl[i].sync_with_server_yn), col_index++);
2820                 _get_table_field_data_int(result, &(p_data_tbl[i].modifiable_yn), col_index++);
2821                 _get_table_field_data_int(result, &(p_data_tbl[i].total_mail_count_on_server), col_index++);
2822                 _get_table_field_data_int(result, &(p_data_tbl[i].has_archived_mails), col_index++);
2823                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_slot_size), col_index++);
2824                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].last_sync_time), col_index++);
2825
2826                 if (input_get_mail_count == 1) {
2827                         _get_table_field_data_int(result, &(total_count), col_index++);
2828                         p_data_tbl[i].total_mail_count_on_local = total_count;
2829                         _get_table_field_data_int(result, &(read_count), col_index++);
2830                         p_data_tbl[i].unread_count = total_count - read_count;                  /*  return unread count, NOT  */
2831                 }
2832         }
2833
2834
2835 FINISH_OFF:
2836         if (result)
2837                 sqlite3_free_table(result);
2838
2839         if (error == EMAIL_ERROR_NONE)  {
2840                 *output_mailbox_list = p_data_tbl;
2841                 *output_mailbox_count = count;
2842                 EM_DEBUG_LOG("Mailbox Count [ %d]", count);
2843         }
2844         else if (p_data_tbl != NULL)
2845                 emstorage_free_mailbox(&p_data_tbl, count, NULL);
2846
2847         if (hStmt != NULL)  {
2848                 rc = sqlite3_finalize(hStmt);
2849                 if (rc != SQLITE_OK) {
2850                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
2851
2852                         error = EMAIL_ERROR_DB_FAILURE;
2853                 }
2854         }
2855
2856         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2857         _DISCONNECT_DB;
2858
2859         EM_DEBUG_FUNC_END("error [%d]", error);
2860         return error;
2861 }
2862
2863 /* Query series --------------------------------------------------------------*/
2864
2865 INTERNAL_FUNC int emstorage_check_duplicated_account(email_account_t* account, int transaction, int *err_code)
2866 {
2867         EM_DEBUG_FUNC_BEGIN();
2868         
2869         if (!account)  {
2870                 if (err_code != NULL)
2871                         *err_code = EMAIL_ERROR_INVALID_PARAM;
2872                 return false;
2873         }
2874         
2875         int rc = -1, ret = false;
2876         int error = EMAIL_ERROR_NONE;
2877         char **result;
2878         int count;
2879         char sql_query_string[QUERY_SIZE] = {0, };
2880
2881         sqlite3 *local_db_handle = emstorage_get_db_connection();
2882         EMSTORAGE_START_READ_TRANSACTION(transaction);
2883
2884         SNPRINTF(sql_query_string, sizeof(sql_query_string),
2885                 "SELECT COUNT(*) FROM mail_account_tbl "
2886                 " WHERE "
2887                 " user_email_address = '%s' AND "
2888                 " incoming_server_user_name = '%s' AND "
2889                 " incoming_server_type = %d AND "
2890                 " incoming_server_address = '%s' AND "
2891                 " outgoing_server_user_name = '%s' AND "
2892                 " outgoing_server_type = %d AND "
2893                 " outgoing_server_address = '%s'; ",
2894                 account->user_email_address,
2895                 account->incoming_server_user_name, account->incoming_server_type, account->incoming_server_address,
2896                 account->outgoing_server_user_name, account->outgoing_server_type, account->outgoing_server_address
2897         );
2898         EM_DEBUG_LOG("Query[%s]", sql_query_string);
2899         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
2900         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2901                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2902
2903         count = atoi(result[1]);
2904         sqlite3_free_table(result);
2905
2906         EM_DEBUG_LOG("Count of Duplicated Account Information: [%d]", count);
2907
2908         if (count == 0) {       /*  not duplicated account */
2909                 ret = true;
2910                 EM_DEBUG_LOG("NOT duplicated account: user_email_address[%s]", account->user_email_address);
2911         }
2912         else {  /*  duplicated account */
2913                 ret = false;
2914                 EM_DEBUG_LOG("The same account already exists. Duplicated account: user_email_address[%s]", account->user_email_address);
2915                 error = EMAIL_ERROR_ALREADY_EXISTS;
2916         }
2917         
2918 FINISH_OFF:
2919
2920         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2921         _DISCONNECT_DB;
2922         
2923         if (err_code != NULL)
2924                 *err_code = error;
2925
2926         EM_DEBUG_FUNC_END("ret [%d]", ret);
2927         return ret;
2928
2929 }
2930
2931 INTERNAL_FUNC int emstorage_get_account_count(int *count, int transaction, int *err_code)
2932 {
2933         EM_DEBUG_FUNC_BEGIN();
2934         
2935         if (!count)  {
2936                 if (err_code != NULL)
2937                         *err_code = EMAIL_ERROR_INVALID_PARAM;
2938                 return false;
2939         }
2940         
2941         int rc = -1, ret = false;
2942         int error = EMAIL_ERROR_NONE;
2943  
2944         DB_STMT hStmt = NULL;
2945         char sql_query_string[QUERY_SIZE] = {0, };
2946         char err_msg[1024];
2947         
2948         sqlite3 *local_db_handle = emstorage_get_db_connection();
2949         EMSTORAGE_START_READ_TRANSACTION(transaction);
2950         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_account_tbl");
2951         EM_DEBUG_LOG("SQL STMT [ %s ]", sql_query_string);
2952         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
2953         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
2954         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2955                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2956
2957         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2958         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2959                 ("sqlite3_step fail:%d", rc));
2960         
2961         *count = sqlite3_column_int(hStmt, 0);
2962         
2963         ret = true;
2964         
2965 FINISH_OFF:
2966
2967         if (hStmt != NULL)  {
2968                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
2969
2970                 rc = sqlite3_finalize(hStmt);
2971                 hStmt=NULL;
2972                 if (rc != SQLITE_OK)  {
2973                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d: %s", rc, err_msg);
2974                         error = EMAIL_ERROR_DB_FAILURE;
2975                 }
2976                 EM_DEBUG_LOG("sqlite3_finalize- %d", rc);
2977         }
2978  
2979         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2980         _DISCONNECT_DB;
2981         
2982         if (err_code != NULL)
2983                 *err_code = error;
2984
2985         EM_DEBUG_FUNC_END("ret [%d]", ret);
2986         return ret;
2987 }
2988
2989
2990 INTERNAL_FUNC int emstorage_get_account_list(int *select_num, emstorage_account_tbl_t** account_list, int transaction, int with_password, int *err_code)
2991 {
2992         EM_DEBUG_FUNC_BEGIN();
2993         
2994         int i = 0, count = 0, rc = -1, ret = false;
2995         int field_index = 0;
2996         int error = EMAIL_ERROR_NONE;
2997         emstorage_account_tbl_t *p_data_tbl = NULL;
2998         char sql_query_string[QUERY_SIZE] = {0, };
2999         char *sql = "SELECT count(*) FROM mail_account_tbl;";
3000         char **result;
3001         sqlite3 *local_db_handle = NULL;
3002         DB_STMT hStmt = NULL;
3003
3004         if (!select_num || !account_list)  {
3005                 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, account_list);
3006                 if (err_code != NULL)
3007                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3008                 return false;
3009         }
3010         
3011         local_db_handle = emstorage_get_db_connection();
3012         EMSTORAGE_START_READ_TRANSACTION(transaction);
3013         
3014         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
3015         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
3016                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
3017
3018         count = atoi(result[1]);
3019         sqlite3_free_table(result);
3020
3021         EM_DEBUG_LOG("count = %d", rc);
3022
3023         if (count == 0) {
3024                 EM_DEBUG_EXCEPTION("no account found...");
3025                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3026                 ret = true;
3027                 goto FINISH_OFF;
3028         }
3029         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_account_tbl ORDER BY account_id");
3030
3031         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
3032
3033         EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
3034         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3035                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3036
3037         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3038         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3039                 ("sqlite3_step fail:%d", rc));
3040         if (rc == SQLITE_DONE)  {
3041                 EM_DEBUG_EXCEPTION("no account found...");
3042                 
3043                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3044                 count = 0;
3045                 ret = true;
3046                 goto FINISH_OFF;
3047         }
3048
3049         if (!(p_data_tbl = (emstorage_account_tbl_t*)malloc(sizeof(emstorage_account_tbl_t) * count)))  {
3050                 EM_DEBUG_EXCEPTION("malloc failed...");
3051                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3052                 goto FINISH_OFF;
3053         }
3054         memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * count);
3055         for (i = 0; i < count; i++)  {
3056                 /*  get recordset */
3057                 field_index = 0;
3058                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_id), field_index++);
3059                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].account_name), 0, field_index++);
3060                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].logo_icon_path), 0, field_index++);
3061                 _get_stmt_field_data_blob(hStmt, &(p_data_tbl[i].user_data), field_index++);
3062                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].user_data_length), field_index++);
3063                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_svc_id), field_index++);
3064                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_status), field_index++);
3065                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_disabled), field_index++);
3066                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].default_mail_slot_size), field_index++);
3067                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_display_name), 0, field_index++);
3068                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_email_address), 0, field_index++);
3069                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].reply_to_address), 0, field_index++);
3070                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].return_address), 0, field_index++);
3071                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].incoming_server_type), field_index++);
3072                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_address), 0, field_index++);
3073                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_port_number), field_index++);
3074                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_user_name), 0, field_index++);
3075                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_password), 0, field_index++);
3076                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_secure_connection), field_index++);
3077                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].retrieval_mode), field_index++);
3078                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].keep_mails_on_pop_server_after_download), field_index++);
3079                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].check_interval), field_index++);
3080                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_download_size), field_index++);
3081                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].outgoing_server_type), field_index++);
3082                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_address), 0, field_index++);
3083                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_port_number), field_index++);
3084                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_user_name), 0, field_index++);
3085                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_password), 0, field_index++);
3086                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_secure_connection), field_index++);
3087                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_need_authentication), field_index++);
3088                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_use_same_authenticator), field_index++);
3089                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.priority), field_index++);
3090                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.keep_local_copy), field_index++);
3091                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_delivery_receipt), field_index++);
3092                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_read_receipt), field_index++);
3093                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.download_limit), field_index++);
3094                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_address), field_index++);
3095                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_subject), field_index++);
3096                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.display_name_from), 0, field_index++);
3097                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.reply_with_body), field_index++);
3098                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.forward_with_files), field_index++);
3099                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_myname_card), field_index++);
3100                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_signature), field_index++);
3101                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.signature), 0, field_index++);
3102                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.add_my_address_to_bcc), field_index++);
3103                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].pop_before_smtp), field_index++);
3104                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_requires_apop), field_index++);
3105                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].smime_type), field_index++);
3106                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].certificate_path), 0, field_index++);
3107                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].cipher_type), field_index++);
3108                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].digest_type), field_index++);
3109
3110                 if (with_password == true) {
3111                         /*  get password from the secure storage */
3112                         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3113                         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3114
3115                         EM_SAFE_FREE(p_data_tbl[i].incoming_server_password);
3116                         EM_SAFE_FREE(p_data_tbl[i].outgoing_server_password);
3117
3118                         /*  get password file name */
3119                         if ((error = _get_password_file_name(p_data_tbl[i].account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
3120                                 EM_DEBUG_EXCEPTION("_get_password_file_name failed. [%d]", error);
3121                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3122                                 goto FINISH_OFF;
3123                         }
3124                                 
3125                         /*  read password from secure storage */
3126                         if ((error = _read_password_from_secure_storage(recv_password_file_name, &(p_data_tbl[i].incoming_server_password))) < 0 ) {
3127                                 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()  failed. [%d]", error);
3128                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3129                                 goto FINISH_OFF;
3130                         }
3131                         EM_DEBUG_LOG("recv_password_file_name[%s], incoming_server_password[%s]", recv_password_file_name,  p_data_tbl[i].incoming_server_password);
3132                         
3133                         if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl[i].outgoing_server_password))) < 0) {
3134                                 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()  failed. [%d]", error);
3135                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3136                                 goto FINISH_OFF;
3137                         }
3138                         EM_DEBUG_LOG("send_password_file_name[%s], password[%s]", send_password_file_name,  p_data_tbl[i].outgoing_server_password);
3139                 }
3140
3141                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3142                 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i,  rc);
3143                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3144                         ("sqlite3_step fail:%d", rc));
3145         } 
3146         
3147         ret = true;
3148         
3149 FINISH_OFF:
3150         if (ret == true)  {
3151                 *account_list = p_data_tbl;
3152                 *select_num = count;
3153                 EM_DEBUG_LOG("COUNT : %d", count);
3154         }
3155         else if (p_data_tbl != NULL)
3156                 emstorage_free_account(&p_data_tbl, count, NULL);
3157
3158         if (hStmt != NULL)  {
3159                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
3160         
3161                 rc = sqlite3_finalize(hStmt);
3162                 hStmt = NULL;
3163                 if (rc != SQLITE_OK)  {
3164                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
3165                         error = EMAIL_ERROR_DB_FAILURE;
3166                 }
3167         }
3168         
3169         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3170         _DISCONNECT_DB;
3171
3172         if (err_code != NULL)
3173                 *err_code = error;
3174
3175         EM_DEBUG_FUNC_END("ret [%d]", ret);
3176         return ret;
3177 }
3178
3179
3180 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)
3181 {
3182         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);
3183         
3184         int ret = false, error = EMAIL_ERROR_NONE, result_count;
3185         char conditional_clause[QUERY_SIZE] = {0, };
3186         emstorage_mail_tbl_t* p_data_tbl = NULL;
3187
3188         if (!server_mail_id || !mail) {
3189                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3190                 error = EMAIL_ERROR_INVALID_PARAM;
3191                 goto FINISH_OFF;
3192         }
3193         
3194         if (account_id == ALL_ACCOUNT)
3195                 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE UPPER(server_mail_id) =UPPER('%s')", server_mail_id);
3196         else
3197                 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE UPPER(server_mail_id) =UPPER('%s') AND account_id = %d", server_mail_id, account_id);
3198
3199         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
3200
3201         if(!emstorage_query_mail_tbl(conditional_clause, transaction, &p_data_tbl, &result_count, &error)) {
3202                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3203                 goto FINISH_OFF;
3204         }
3205         
3206         ret = true;
3207         
3208 FINISH_OFF:
3209         if (ret == true)
3210                 *mail = p_data_tbl;
3211         
3212         if (err_code != NULL)
3213                 *err_code = error;
3214
3215         EM_DEBUG_FUNC_END("ret [%d]", ret);
3216         return ret;
3217 }
3218
3219
3220 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)
3221 {
3222         int cur_clause = 0, conditional_clause_count = 0, i = 0;
3223
3224         if (account_id < ALL_ACCOUNT) {
3225                 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3226                 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3227         }
3228
3229         /*  where clause */
3230         if (account_id == ALL_ACCOUNT) {
3231                 cur_clause += (conditional_clause_count++ == 0)?
3232                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_type not in (3, 5, 7, 8)"):
3233                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_type not in (3, 5, 7, 8)");
3234         }
3235         else {
3236                 cur_clause += (conditional_clause_count++ == 0)?
3237                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE account_id = %d", account_id):
3238                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND account_id = %d", account_id);
3239         }
3240         
3241         if (mailbox_id > 0) {
3242                 cur_clause += (conditional_clause_count++ == 0)?
3243                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_id = %d", mailbox_id):
3244                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_id = %d", mailbox_id);
3245         }
3246         else if(account_id != ALL_ACCOUNT) {
3247                 cur_clause += (conditional_clause_count++ == 0)?
3248                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_type not in (3, 5, 7, 8)"):
3249                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_type not in (3, 5, 7, 8)");
3250         }
3251
3252         if (thread_id > 0) {
3253                 cur_clause += (conditional_clause_count++ == 0)?
3254                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_id = %d ", thread_id):
3255                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_id = %d ", thread_id);
3256         }
3257         else if (thread_id == EMAIL_LIST_TYPE_THREAD)   {
3258                 cur_clause += (conditional_clause_count++ == 0)?
3259                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_item_count > 0"):
3260                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_item_count > 0");
3261         }
3262         else if (thread_id == EMAIL_LIST_TYPE_LOCAL) {
3263                 cur_clause += (conditional_clause_count++ == 0)?
3264                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE server_mail_status = 0"):
3265                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND server_mail_status = 0");
3266         }
3267         else if (thread_id == EMAIL_LIST_TYPE_UNREAD) {
3268                 cur_clause += (conditional_clause_count++ == 0)?
3269                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_seen_field == 0"):
3270                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_seen_field == 0");
3271         }
3272
3273         /*  EM_DEBUG_LOG("where clause added [%s]", conditional_clause_string); */
3274         if (addr_list && addr_list->address_count > 0) {
3275                 if (!addr_list->address_type) {
3276                         cur_clause += (conditional_clause_count++ == 0)?
3277                                 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE email_address_sender IN(\"%s\"", addr_list->address_list[0]):
3278                                 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND email_address_sender IN(\"%s\"", addr_list->address_list[0]);
3279
3280                         for (i = 1; i < addr_list->address_count; i++)
3281                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3282
3283                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3284                 } else {
3285                         cur_clause += (conditional_clause_count++ == 0)?
3286                                 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]):
3287                                 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]);
3288
3289                         for (i = 1; i < addr_list->address_count; i++)
3290                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3291
3292                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");            
3293                 }
3294         }
3295
3296         if (input_except_delete_flagged_mails) {
3297                 cur_clause += (conditional_clause_count++ == 0)?
3298                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_deleted_field = 0"):
3299                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_deleted_field = 0");
3300         }
3301
3302         if (search_value) {
3303                 switch (search_type) {
3304                         case EMAIL_SEARCH_FILTER_SUBJECT:
3305                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3306                                         " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3307                                 break;
3308                         case EMAIL_SEARCH_FILTER_SENDER:
3309                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3310                                         " %s  ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3311                                         ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3312                                 break;
3313                         case EMAIL_SEARCH_FILTER_RECIPIENT:
3314                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3315                                         " %s ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') " 
3316                                         "       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3317                                         "       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3318                                         ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value);
3319                                 break;
3320                         case EMAIL_SEARCH_FILTER_ALL:
3321                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3322                                         " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\' "
3323                                         "       OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3324                                         "                       OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3325                                         "                       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3326                                         "                       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3327                                         "               ) "
3328                                         "       )"
3329                                         ")", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value, search_value, search_value);
3330                                 break;
3331                 }
3332         }
3333         
3334         /*  EM_DEBUG_LOG("where clause [%s]", conditional_clause_string); */
3335         static char sorting_str[][50] = { 
3336                          " ORDER BY date_time DESC",                                             /* case EMAIL_SORT_DATETIME_HIGH: */
3337                          " ORDER BY date_time ASC",                                               /* case EMAIL_SORT_DATETIME_LOW: */
3338                          " ORDER BY full_address_from DESC, date_time DESC", /* case EMAIL_SORT_SENDER_HIGH: */
3339                          " ORDER BY full_address_from ASC, date_time DESC",  /* case EMAIL_SORT_SENDER_LOW: */
3340                          " ORDER BY full_address_to DESC, date_time DESC",   /* case EMAIL_SORT_RCPT_HIGH: */
3341                          " ORDER BY full_address_to ASC, date_time DESC",       /* case EMAIL_SORT_RCPT_LOW: */
3342                          " ORDER BY subject DESC, date_time DESC",                 /* case EMAIL_SORT_SUBJECT_HIGH: */
3343                          " ORDER BY subject ASC, date_time DESC",                       /* case EMAIL_SORT_SUBJECT_LOW: */
3344                          " ORDER BY priority DESC, date_time DESC",               /* case EMAIL_SORT_PRIORITY_HIGH: */
3345                          " ORDER BY priority ASC, date_time DESC",                 /* case EMAIL_SORT_PRIORITY_LOW: */
3346                          " ORDER BY attachment_count DESC, date_time DESC",  /* case EMAIL_SORT_ATTACHMENT_HIGH: */
3347                          " ORDER BY attachment_count ASC, date_time DESC",   /* case EMAIL_SORT_ATTACHMENT_LOW: */
3348                          " ORDER BY lock_status DESC, date_time DESC",     /* case EMAIL_SORT_FAVORITE_HIGH: */
3349                          " ORDER BY lock_status ASC, date_time DESC",           /* case EMAIL_SORT_FAVORITE_LOW: */
3350                          " ORDER BY mailbox_name DESC, date_time DESC",   /* case EMAIL_SORT_MAILBOX_NAME_HIGH: */
3351                          " ORDER BY mailbox_name ASC, date_time DESC"           /* case EMAIL_SORT_MAILBOX_NAME_LOW: */
3352                          };
3353
3354         if (sorting < EMAIL_SORT_END && sorting >= 0)
3355                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " %s", sorting_str[sorting]);
3356         else
3357                 EM_DEBUG_LOG(" Invalid Sorting order ");
3358
3359         /*  limit clause */
3360         if (start_index != -1 && limit_count != -1)
3361                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " LIMIT %d, %d", start_index, limit_count);
3362
3363         return true;
3364 }
3365
3366
3367 /**
3368   *     emstorage_get_mail_list - Get the mail list information.
3369   *     
3370   *
3371   */
3372 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)
3373 {
3374         EM_DEBUG_FUNC_BEGIN();
3375         EM_PROFILE_BEGIN(emstorage_get_mail_list_func);
3376
3377         int ret = false, error = EMAIL_ERROR_NONE;
3378         char conditional_clause_string[QUERY_SIZE] = { 0, };
3379
3380         if (account_id < ALL_ACCOUNT) {
3381                 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3382                 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3383         }
3384         EM_IF_NULL_RETURN_VALUE(result_count, false);
3385
3386         _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);
3387         
3388         EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
3389
3390         if(!emstorage_query_mail_list(conditional_clause_string, transaction, mail_list, result_count, &error)) {
3391                 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3392                 goto FINISH_OFF;
3393         }
3394
3395         ret = true;
3396         
3397 FINISH_OFF:
3398         if (err_code != NULL)
3399                 *err_code = error;
3400
3401         EM_PROFILE_END(emstorage_get_mail_list_func);
3402         EM_DEBUG_FUNC_END("ret [%d]", ret);
3403         return ret;
3404 }
3405
3406
3407 /**
3408   *     emstorage_get_mails - Get the Mail list information based on mailbox_name name
3409   *     
3410   *
3411   */
3412 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)
3413 {
3414         EM_DEBUG_FUNC_BEGIN();
3415         EM_PROFILE_BEGIN(emStorageGetMails);
3416         
3417         int count = 0, ret = false, error = EMAIL_ERROR_NONE;
3418         emstorage_mail_tbl_t *p_data_tbl = NULL;
3419         char conditional_clause_string[QUERY_SIZE] = {0, };
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("EMAIL_ERROR_INVALID_PARAM");
3426                 if (err_code != NULL)
3427                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3428                 return false;
3429         }
3430
3431         _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);
3432
3433         EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
3434
3435         if(!emstorage_query_mail_tbl(conditional_clause_string, transaction, &p_data_tbl, &count,  &error)) {
3436                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3437                 goto FINISH_OFF;
3438         }
3439
3440         ret = true;
3441         
3442 FINISH_OFF:
3443         if (ret == true) {
3444                 *mail_list = p_data_tbl;
3445                 *result_count = count;
3446                 EM_DEBUG_LOG("COUNT : %d", count);
3447         }
3448         else if (p_data_tbl != NULL)
3449                 emstorage_free_mail(&p_data_tbl, count, NULL);
3450         
3451         if (err_code != NULL)
3452                 *err_code = error;
3453
3454         EM_PROFILE_END(emStorageGetMails);
3455         EM_DEBUG_FUNC_END("ret [%d]", ret);
3456         return ret;
3457 }
3458
3459
3460
3461 /**
3462   *     emstorage_get_searched_mail_list - Get the mail list information after filtering
3463   *     
3464   *
3465   */
3466 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)
3467 {
3468         EM_DEBUG_FUNC_BEGIN();
3469         
3470         int ret = false, error = EMAIL_ERROR_NONE;
3471         char conditional_clause[QUERY_SIZE] = {0, };
3472         char *temp_search_value = (char *)search_value;
3473
3474         EM_IF_NULL_RETURN_VALUE(mail_list, false);
3475         EM_IF_NULL_RETURN_VALUE(result_count, false);
3476
3477         if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3478                 EM_DEBUG_EXCEPTION("select_num[%p], Mail_list[%p]", result_count, mail_list);
3479                 error = EMAIL_ERROR_INVALID_PARAM;
3480                 goto FINISH_OFF;
3481         }
3482
3483         temp_search_value = em_replace_all_string(temp_search_value, "_", "\\_");
3484         temp_search_value = em_replace_all_string(temp_search_value, "%", "\\%");
3485
3486         _write_conditional_clause_for_getting_mail_list(account_id, mailbox_id, NULL, thread_id, start_index, limit_count, search_type, temp_search_value, sorting, true, conditional_clause, QUERY_SIZE, &error);
3487         
3488         EM_DEBUG_LOG("conditional_clause[%s]", conditional_clause);
3489
3490         if(!emstorage_query_mail_list(conditional_clause, transaction, mail_list, result_count, &error)) {
3491                 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3492                 goto FINISH_OFF;
3493         }
3494
3495         ret = true;
3496         
3497 FINISH_OFF:
3498         EM_DEBUG_LOG("emstorage_get_searched_mail_list finish off");    
3499         
3500         if (err_code != NULL)
3501                 *err_code = error;
3502
3503         EM_DEBUG_FUNC_END("ret [%d]", ret);
3504         return ret;
3505 }
3506
3507
3508 static int _get_password_file_name(int account_id, char *recv_password_file_name, char *send_password_file_name)
3509 {
3510         EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
3511
3512         if (account_id <= 0 || !recv_password_file_name || !send_password_file_name)  {
3513                 EM_DEBUG_EXCEPTION("Invalid parameter");
3514                 return EMAIL_ERROR_INVALID_PARAM;
3515         }
3516
3517         sprintf(recv_password_file_name, ".email_account_%d_recv", account_id);
3518         sprintf(send_password_file_name, ".email_account_%d_send", account_id);
3519         EM_DEBUG_FUNC_END();
3520         return EMAIL_ERROR_NONE;
3521 }
3522
3523 static int _read_password_from_secure_storage(char *file_name, char **password)
3524 {
3525         EM_DEBUG_FUNC_BEGIN("file_name[%s], password[%p]", file_name, password);
3526
3527         if (!file_name || !password) {
3528                 EM_DEBUG_EXCEPTION("Invalid Parameter");
3529                 return EMAIL_ERROR_INVALID_PARAM;
3530         }       
3531
3532         size_t buf_len = 0, read_len = 0;
3533         ssm_file_info_t sfi;
3534         char *temp_password = NULL;
3535         int error_code_from_ssm = 0;
3536         int ret = EMAIL_ERROR_NONE;
3537
3538         if ( (error_code_from_ssm = ssm_getinfo(file_name, &sfi, SSM_FLAG_SECRET_OPERATION, NULL)) < 0) {
3539                 EM_DEBUG_EXCEPTION("ssm_getinfo() failed. [%d]", error_code_from_ssm);
3540                 ret = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3541                 goto FINISH_OFF;
3542         }
3543
3544         buf_len = sfi.originSize;
3545         EM_DEBUG_LOG("password buf_len[%d]", buf_len);
3546         if ((temp_password = (char *)malloc(buf_len + 1)) == NULL) {
3547                 EM_DEBUG_EXCEPTION("malloc failed...");
3548                 ret = EMAIL_ERROR_OUT_OF_MEMORY;
3549                 goto FINISH_OFF;
3550         }
3551         memset(temp_password, 0x00, buf_len + 1);
3552
3553         if ( (error_code_from_ssm = ssm_read(file_name, temp_password, buf_len, &read_len, SSM_FLAG_SECRET_OPERATION, NULL)) < 0) {
3554                 EM_DEBUG_EXCEPTION("ssm_read() failed. [%d]", error_code_from_ssm);
3555                 ret = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3556                 goto FINISH_OFF;
3557         }
3558
3559         EM_DEBUG_LOG("password_file_name[%s], password[%s], originSize[%d], read len[%d]", file_name,  temp_password, sfi.originSize, read_len);
3560
3561         *password = temp_password;
3562         temp_password = NULL;
3563
3564 FINISH_OFF:
3565         EM_SAFE_FREE(temp_password);
3566         
3567         EM_DEBUG_FUNC_END("ret [%d]", ret);
3568         return ret;
3569 }
3570
3571
3572 INTERNAL_FUNC int emstorage_get_account_by_id(int account_id, int pulloption, emstorage_account_tbl_t **account, int transaction, int *err_code)
3573 {
3574         EM_DEBUG_FUNC_BEGIN("account_id[%d], pulloption[%d], account[%p], transaction[%d], err_code[%p]", account_id, pulloption, account, transaction, err_code);
3575
3576         if (!account)  {
3577                 EM_DEBUG_EXCEPTION("account_id[%d], account[%p]", account_id, account);
3578                 if (err_code != NULL)
3579                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3580                 return false;
3581         }
3582
3583         int ret = false;
3584         int error = EMAIL_ERROR_NONE;
3585         emstorage_account_tbl_t* p_data_tbl = NULL;
3586         DB_STMT hStmt = NULL;
3587         char sql_query_string[QUERY_SIZE] = {0, };
3588         int rc = -1;
3589         int sql_len = 0;
3590         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3591         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3592         
3593         sqlite3 *local_db_handle = emstorage_get_db_connection();
3594         EMSTORAGE_START_READ_TRANSACTION(transaction);
3595
3596         /*  Make query string */
3597         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
3598         sql_len = strlen(sql_query_string);
3599                 
3600         if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
3601                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
3602                         "incoming_server_type,"
3603                         "incoming_server_address,"
3604                         "user_email_address,"
3605                         "incoming_server_user_name,"
3606                         "retrieval_mode,"
3607                         "incoming_server_port_number,"
3608                         "incoming_server_secure_connection,"
3609                         "outgoing_server_type,"
3610                         "outgoing_server_address,"
3611                         "outgoing_server_port_number,"
3612                         "outgoing_server_need_authentication,"
3613                         "outgoing_server_secure_connection,"
3614                         "outgoing_server_user_name,"
3615                         "user_display_name,"
3616                         "reply_to_address,"
3617                         "return_address,"
3618                         "account_id,"
3619                         "keep_mails_on_pop_server_after_download,"
3620                         "auto_download_size,"
3621                         "outgoing_server_use_same_authenticator,"
3622                         "pop_before_smtp,"
3623                         "incoming_server_requires_apop,"
3624                         "logo_icon_path,"
3625                         "user_data,"
3626                         "user_data_length,"
3627                         "check_interval,"
3628                         "sync_status,");
3629                 sql_len = strlen(sql_query_string); 
3630         }
3631
3632         if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME) {
3633                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " account_name, ");
3634                 sql_len = strlen(sql_query_string); 
3635         }
3636
3637         /*  get from secure storage, not from db */
3638         if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
3639                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
3640                         "priority,"
3641                         "keep_local_copy,"
3642                         "req_delivery_receipt,"
3643                         "req_read_receipt,"
3644                         "download_limit,"
3645                         "block_address,"
3646                         "block_subject,"
3647                         "display_name_from,"
3648                         "reply_with_body,"
3649                         "forward_with_files,"
3650                         "add_myname_card,"
3651                         "add_signature,"
3652                         "signature,"
3653                         "add_my_address_to_bcc,"
3654                         "account_svc_id,"
3655                         "sync_disabled,"
3656                         "default_mail_slot_size,"
3657                         "smime_type,"
3658                         "certificate_path,"
3659                         "cipher_type,"
3660                         "digest_type,");
3661
3662                 sql_len = strlen(sql_query_string); 
3663         }
3664
3665         /*  dummy value, FROM WHERE clause */
3666         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "0 FROM mail_account_tbl WHERE account_id = %d", account_id);
3667
3668         /*  FROM clause */
3669         EM_DEBUG_LOG("query = [%s]", sql_query_string);
3670
3671         /*  execute a sql and count rows */
3672         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
3673         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3674                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3675         
3676         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3677         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3678                 ("sqlite3_step fail:%d", rc));
3679
3680         if (rc == SQLITE_DONE)  {
3681                 EM_DEBUG_EXCEPTION("no matched account found...");
3682                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3683                 goto FINISH_OFF;
3684         }
3685
3686         /*  Assign query result to structure */
3687         if (!(p_data_tbl = (emstorage_account_tbl_t *)malloc(sizeof(emstorage_account_tbl_t) * 1)))  {
3688                 EM_DEBUG_EXCEPTION("malloc failed...");
3689                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3690                 goto FINISH_OFF;
3691         }
3692
3693         memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * 1);
3694         int col_index = 0;
3695
3696         if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
3697                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_type), col_index++);
3698                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_address),  0, col_index++);
3699                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_email_address), 0, col_index++);
3700                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_user_name), 0, col_index++);
3701                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->retrieval_mode), col_index++);
3702                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_port_number), col_index++);
3703                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_secure_connection), col_index++);
3704                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->outgoing_server_type), col_index++);
3705                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_address), 0, col_index++);
3706                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_port_number), col_index++);
3707                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_need_authentication), col_index++);
3708                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_secure_connection), col_index++);
3709                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_user_name), 0, col_index++);
3710                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_display_name), 0, col_index++);
3711                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->reply_to_address), 0, col_index++);
3712                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->return_address), 0, col_index++);
3713                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
3714                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->keep_mails_on_pop_server_after_download), col_index++);
3715                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_download_size), col_index++);
3716                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_use_same_authenticator), col_index++);
3717                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->pop_before_smtp), col_index++);
3718                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_requires_apop), col_index++);
3719                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->logo_icon_path), 0, col_index++);
3720                 _get_stmt_field_data_blob(hStmt, &p_data_tbl->user_data, col_index++);
3721                 _get_stmt_field_data_int(hStmt, &p_data_tbl->user_data_length, col_index++);
3722                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->check_interval), col_index++);
3723                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->sync_status), col_index++);
3724         }
3725
3726         if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME)
3727                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->account_name), 0, col_index++);
3728
3729         if (pulloption & EMAIL_ACC_GET_OPT_PASSWORD) {
3730                 /*  get password file name */
3731                 if ((error = _get_password_file_name(p_data_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
3732                         EM_DEBUG_EXCEPTION("_get_password_file_name failed. [%d]", error);
3733                         goto FINISH_OFF;
3734                 }               
3735
3736                 /*  read password from secure storage */
3737                 if ((error = _read_password_from_secure_storage(recv_password_file_name, &(p_data_tbl->incoming_server_password))) < 0) {
3738                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed. [%d]", error);
3739                         goto FINISH_OFF;
3740                 }
3741
3742                 EM_DEBUG_LOG("recv_password_file_name[%s], password[%s]", recv_password_file_name,  p_data_tbl->incoming_server_password);
3743                 
3744                 if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl->outgoing_server_password))) < 0) {
3745                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed. [%d]", error);
3746                         goto FINISH_OFF;
3747                 }
3748                 EM_DEBUG_LOG("send_password_file_name[%s], password[%s]", send_password_file_name,  p_data_tbl->outgoing_server_password);
3749         }
3750
3751         if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
3752                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.priority), col_index++);
3753                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.keep_local_copy), col_index++);
3754                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_delivery_receipt), col_index++);
3755                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_read_receipt), col_index++);
3756                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.download_limit), col_index++);
3757                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_address), col_index++);
3758                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_subject), col_index++);
3759                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.display_name_from), 0, col_index++);
3760                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.reply_with_body), col_index++);
3761                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.forward_with_files), col_index++);
3762                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_myname_card), col_index++);
3763                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_signature), col_index++);
3764                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.signature), 0, col_index++);
3765                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.add_my_address_to_bcc), col_index++);
3766                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_svc_id), col_index++);
3767                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->sync_disabled), col_index++);
3768                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->default_mail_slot_size), col_index++);
3769                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->smime_type), col_index++);
3770                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->certificate_path), 0, col_index++);
3771                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->cipher_type), col_index++);
3772                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->digest_type), col_index++);
3773         }
3774
3775         ret = true;
3776
3777 FINISH_OFF:
3778         if (ret == true)
3779                 *account = p_data_tbl;
3780         else {
3781                 if (p_data_tbl)
3782                         emstorage_free_account((emstorage_account_tbl_t **)&p_data_tbl, 1, NULL);
3783         }
3784         if (hStmt != NULL)  {
3785                 rc = sqlite3_finalize(hStmt);
3786                 if (rc != SQLITE_OK)  {
3787                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
3788                         error = EMAIL_ERROR_DB_FAILURE;
3789                 }
3790         }
3791         
3792         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3793         _DISCONNECT_DB;
3794
3795         if (err_code != NULL)
3796                 *err_code = error;
3797
3798         EM_DEBUG_FUNC_END("ret [%d]", ret);
3799         return ret;
3800 }
3801
3802 INTERNAL_FUNC int emstorage_get_password_length_of_account(int account_id, int *password_length, int *err_code)
3803 {
3804         EM_DEBUG_FUNC_BEGIN("account_id[%d], password_length[%p], err_code[%p]", account_id, password_length, err_code);
3805
3806         if (account_id <= 0 || password_length == NULL)  {
3807                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3808                 if (err_code != NULL)
3809                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3810                 return false;
3811         }
3812
3813         int ret = false;
3814         int error = EMAIL_ERROR_NONE;
3815         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3816         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3817         char *temp_password = NULL;
3818         
3819
3820         /*  get password file name */
3821         if ((error = _get_password_file_name(account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
3822                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
3823                 goto FINISH_OFF;
3824         }               
3825
3826         /*  read password from secure storage */
3827         if ((error = _read_password_from_secure_storage(recv_password_file_name, &temp_password)) < 0 || !temp_password) {
3828                 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed...");
3829                 goto FINISH_OFF;
3830         }
3831
3832         *password_length = strlen(temp_password);
3833         
3834         EM_DEBUG_LOG("recv_password_file_name[%s], *password_length[%d]", recv_password_file_name,  *password_length);
3835
3836         ret = true;
3837
3838 FINISH_OFF:
3839         EM_SAFE_FREE(temp_password);
3840
3841         if (err_code != NULL)
3842                 *err_code = error;
3843
3844         EM_DEBUG_FUNC_END("ret [%d]", ret);
3845         return ret;
3846 }
3847
3848 INTERNAL_FUNC int emstorage_update_account(int account_id, emstorage_account_tbl_t* account_tbl, int transaction, int *err_code)
3849 {
3850         EM_DEBUG_FUNC_BEGIN("account_id[%d], account[%p], transaction[%d], err_code[%p]", account_id, account_tbl, transaction, err_code);
3851         
3852         if (account_id < FIRST_ACCOUNT_ID || !account_tbl)  {
3853                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3854                 if (err_code != NULL)
3855                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3856                 return false;
3857         }
3858         
3859         int error = EMAIL_ERROR_NONE;
3860         int rc, ret = false;
3861  
3862         DB_STMT hStmt = NULL;
3863         char sql_query_string[QUERY_SIZE] = {0, };
3864         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3865         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3866
3867         sqlite3 *local_db_handle = emstorage_get_db_connection();
3868         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
3869                 
3870         SNPRINTF(sql_query_string, sizeof(sql_query_string),
3871                 "UPDATE mail_account_tbl SET"
3872                 "  account_name = ?"
3873                 ", logo_icon_path = ?"
3874                 ", user_data = ?"
3875                 ", user_data_length = ?"
3876                 ", account_svc_id = ?"
3877                 ", sync_status = ?"
3878                 ", sync_disabled = ?"
3879                 ", default_mail_slot_size = ?"
3880                 ", user_display_name = ?"
3881                 ", user_email_address = ?"
3882                 ", reply_to_address = ?"
3883                 ", return_address = ?"
3884                 ", incoming_server_type = ?"
3885                 ", incoming_server_address = ?"
3886                 ", incoming_server_port_number = ?"
3887                 ", incoming_server_user_name = ?"
3888                 ", incoming_server_secure_connection = ?"
3889                 ", retrieval_mode = ?"
3890                 ", keep_mails_on_pop_server_after_download = ?"
3891                 ", check_interval = ?"
3892                 ", auto_download_size = ?"
3893                 ", outgoing_server_type = ?"
3894                 ", outgoing_server_address = ?"
3895                 ", outgoing_server_port_number = ?"
3896                 ", outgoing_server_user_name = ?"
3897                 ", outgoing_server_secure_connection = ?"
3898                 ", outgoing_server_need_authentication = ?"
3899                 ", outgoing_server_use_same_authenticator = ?"
3900                 ", priority = ?"
3901                 ", keep_local_copy = ?"
3902                 ", req_delivery_receipt = ?"
3903                 ", req_read_receipt = ?"
3904                 ", download_limit = ?"
3905                 ", block_address = ?"
3906                 ", block_subject = ?"
3907                 ", display_name_from = ?"
3908                 ", reply_with_body = ?"
3909                 ", forward_with_files = ?"
3910                 ", add_myname_card = ?"
3911                 ", add_signature = ?"
3912                 ", signature = ?"
3913                 ", add_my_address_to_bcc = ?"
3914                 ", pop_before_smtp = ?"
3915                 ", incoming_server_requires_apop = ?"
3916                 ", smime_type = ?"
3917                 ", certificate_path = ?"
3918                 ", cipher_type = ?"
3919                 ", digest_type = ?"
3920                 " WHERE account_id = ?");        
3921  
3922         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
3923         EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
3924         EM_DEBUG_LOG("SQL[%s]", sql_query_string);
3925         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3926                 ("sqlite3_prepare fail:(%d) %s", rc, sqlite3_errmsg(local_db_handle)));
3927         
3928         int i = 0;
3929
3930         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
3931         _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
3932         _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
3933         _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
3934         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
3935         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
3936         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
3937         _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
3938         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
3939         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3940         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3941         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3942         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
3943         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3944         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
3945         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
3946         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
3947         _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
3948         _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
3949         _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
3950         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
3951         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
3952         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3953         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
3954         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
3955         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
3956         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
3957         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
3958         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
3959         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
3960         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
3961         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
3962         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
3963         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
3964         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
3965         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
3966         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
3967         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
3968         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
3969         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
3970         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
3971         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
3972         _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
3973         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
3974         _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
3975         _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
3976         _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
3977         _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
3978         _bind_stmt_field_data_int(hStmt, i++, account_id);
3979  
3980         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3981         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
3982                 ("sqlite3_step fail:%d", rc));
3983         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3984                 ("sqlite3_step fail:%d", rc));
3985         
3986         /*  validate account existence */
3987         rc = sqlite3_changes(local_db_handle);
3988         if (rc == 0) {
3989                 EM_DEBUG_EXCEPTION(" no matched account found...");
3990         
3991                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3992                 goto FINISH_OFF;
3993         }
3994         
3995         /*  get password file name */
3996         if ((error = _get_password_file_name(account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
3997                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
3998                 goto FINISH_OFF;
3999         }
4000
4001         /*  save passwords to the secure storage */
4002         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);
4003         if (account_tbl->incoming_server_password && (strlen(account_tbl->incoming_server_password) > 0)) {
4004                 if (ssm_write_buffer(account_tbl->incoming_server_password, strlen(account_tbl->incoming_server_password), recv_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
4005                         EM_DEBUG_EXCEPTION(" ssm_write_buffer failed -recv incoming_server_password : file[%s]", recv_password_file_name);
4006                         error = EMAIL_ERROR_SYSTEM_FAILURE;
4007                         goto FINISH_OFF;                
4008                 }
4009         }
4010
4011         if (account_tbl->outgoing_server_password && (strlen(account_tbl->outgoing_server_password) > 0)) {
4012                 if (ssm_write_buffer(account_tbl->outgoing_server_password, strlen(account_tbl->outgoing_server_password), send_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
4013                         EM_DEBUG_EXCEPTION(" ssm_write_buffer failed -send password : file[%s]", send_password_file_name);
4014                         error = EMAIL_ERROR_SYSTEM_FAILURE;
4015                         goto FINISH_OFF;                
4016                 }
4017         }
4018         
4019         if (!emstorage_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_tbl->account_id, 0, NULL, 0))
4020                 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event[ NOTI_ACCOUNT_UPDATE] : Notification Failed >>> ");
4021         ret = true;
4022         
4023 FINISH_OFF:
4024  
4025         if (hStmt != NULL)  {
4026                 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
4027
4028                 rc = sqlite3_finalize(hStmt);
4029                 if (rc != SQLITE_OK)  {
4030                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
4031                         error = EMAIL_ERROR_DB_FAILURE;
4032                 }
4033         }
4034
4035         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
4036         
4037         _DISCONNECT_DB;
4038         
4039         if (err_code != NULL)
4040                 *err_code = error;
4041
4042         EM_DEBUG_FUNC_END("ret [%d]", ret);
4043         return ret;
4044 }
4045
4046 INTERNAL_FUNC int emstorage_set_field_of_accounts_with_integer_value(int account_id, char *field_name, int value, int transaction)
4047 {
4048         EM_DEBUG_FUNC_BEGIN("account_id[%d], field_name[%s], value[%d], transaction[%d]", account_id, field_name, value, transaction);
4049         int error = EMAIL_ERROR_NONE;
4050         int rc = 0;
4051         int result = 0;
4052         char sql_query_string[QUERY_SIZE] = {0, };
4053         sqlite3 *local_db_handle = NULL;
4054
4055         if (!account_id  || !field_name) {
4056                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4057                 return EMAIL_ERROR_INVALID_PARAM;
4058         }
4059
4060         local_db_handle = emstorage_get_db_connection();
4061
4062         /* Write query string */
4063         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_account_tbl SET %s = %d WHERE account_id = %d", field_name, value, account_id);
4064
4065         EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
4066
4067         /* Execute query */
4068         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
4069         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
4070         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4071                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4072         if (sqlite3_changes(local_db_handle) == 0)
4073                 EM_DEBUG_LOG("no mail matched...");
4074
4075
4076 FINISH_OFF:
4077         result = (error == EMAIL_ERROR_NONE) ? true : false;
4078         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, result, error);
4079         _DISCONNECT_DB;
4080
4081         if (!emstorage_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_id, 0, field_name, value))
4082                 EM_DEBUG_EXCEPTION("emstorage_notify_storage_event failed : NOTI_ACCOUNT_UPDATE [%s,%d]", field_name, value);
4083
4084         EM_DEBUG_FUNC_END("error [%d]", error);
4085         return error;
4086 }
4087
4088 INTERNAL_FUNC int emstorage_get_sync_status_of_account(int account_id, int *result_sync_status,int *err_code) 
4089 {
4090         EM_DEBUG_FUNC_BEGIN("account_id[%d], result_sync_status [%p], err_code[%p]", account_id, result_sync_status, err_code);
4091
4092         if(!result_sync_status) {
4093                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4094                 if (err_code != NULL)
4095                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4096                 return false;
4097         }
4098         
4099         int error = EMAIL_ERROR_NONE, rc, ret = false, sync_status, count, i, col_index;
4100         char sql_query_string[QUERY_SIZE] = {0, };
4101         char **result = NULL;
4102         sqlite3 *local_db_handle = emstorage_get_db_connection();
4103
4104         if(account_id)
4105                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl WHERE account_id = %d", account_id);      
4106         else
4107                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl");       
4108  
4109         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
4110         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
4111                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4112
4113         if (!count) {
4114                 EM_DEBUG_EXCEPTION("no matched account found...");
4115                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4116                 goto FINISH_OFF;
4117         }
4118
4119         col_index = 1;
4120         *result_sync_status = 0;
4121
4122         for(i = 0; i < count; i++) {
4123                 _get_table_field_data_int(result, &sync_status, col_index++);
4124                 *result_sync_status |= sync_status;
4125         }
4126         
4127         EM_DEBUG_LOG("sync_status [%d]", sync_status);
4128
4129         sqlite3_free_table(result);
4130
4131         ret = true;
4132         
4133 FINISH_OFF:
4134         
4135         _DISCONNECT_DB;
4136         
4137         if (err_code != NULL)
4138                 *err_code = error;
4139
4140         EM_DEBUG_FUNC_END("ret [%d]", ret);
4141         return ret;
4142 }
4143
4144 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)
4145 {
4146         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);
4147         
4148         int error = EMAIL_ERROR_NONE, rc, ret = false, set_value = sync_status, result_sync_status;
4149         char sql_query_string[QUERY_SIZE] = {0, };
4150         sqlite3 *local_db_handle = emstorage_get_db_connection();
4151
4152         if(set_operator != SET_TYPE_SET && account_id) {
4153                 if(!emstorage_get_sync_status_of_account(account_id, &result_sync_status, &error)) {
4154                         EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", error);
4155                         goto FINISH_OFF;
4156                 }
4157                 switch(set_operator) {
4158                         case SET_TYPE_UNION :
4159                                 set_value = result_sync_status | set_value;
4160                                 break;
4161                         case SET_TYPE_MINUS :
4162                                 set_value = result_sync_status & (~set_value);
4163                                 break;
4164                         default:
4165                                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_NOT_SUPPORTED [%d]", set_operator);
4166                                 error = EMAIL_ERROR_NOT_SUPPORTED;
4167                                 break;
4168                 }
4169                 EM_DEBUG_LOG("set_value [%d]", set_value);
4170         }
4171
4172         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
4173
4174         if(account_id)
4175                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE account_id = %d", set_value, account_id);       
4176         else
4177                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE incoming_server_type <> 5", set_value);         
4178
4179         EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
4180  
4181         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
4182         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4183                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4184         
4185         rc = sqlite3_changes(local_db_handle);
4186
4187         if (rc == 0) {
4188                 EM_DEBUG_EXCEPTION("no matched account found...");
4189                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4190                 goto FINISH_OFF;
4191         }
4192         
4193         if (!emstorage_notify_storage_event(NOTI_ACCOUNT_UPDATE_SYNC_STATUS, account_id, 0, NULL, 0))
4194                 EM_DEBUG_EXCEPTION("emstorage_notify_storage_event[NOTI_ACCOUNT_UPDATE_SYNC_STATUS] : Notification failed");
4195         ret = true;
4196         
4197 FINISH_OFF:
4198
4199         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
4200         
4201         _DISCONNECT_DB;
4202         
4203         if (err_code != NULL)
4204                 *err_code = error;
4205
4206         EM_DEBUG_FUNC_END("ret [%d]", ret);
4207         return ret;
4208 }
4209
4210 INTERNAL_FUNC int emstorage_add_account(emstorage_account_tbl_t* account_tbl, int transaction, int *err_code)
4211 {
4212         EM_DEBUG_FUNC_BEGIN("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4213         
4214         if (!account_tbl)  {
4215                 EM_DEBUG_EXCEPTION("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4216                 if (err_code != NULL)
4217                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4218                 return false;
4219         }
4220         
4221         int rc = -1, ret = false;
4222         int error = EMAIL_ERROR_NONE;
4223         int error_from_ssm = 0;
4224         DB_STMT hStmt = NULL;
4225         char sql_query_string[QUERY_SIZE] = {0, };
4226         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4227         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4228         
4229         sqlite3 *local_db_handle = emstorage_get_db_connection();
4230
4231         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
4232
4233         char *sql = "SELECT max(rowid) FROM mail_account_tbl;";
4234         char **result = NULL;
4235         
4236         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
4237         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
4238                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
4239
4240         if (NULL==result[1]) rc = 1;
4241         else rc = atoi(result[1])+1;
4242         sqlite3_free_table(result);
4243         result = NULL;
4244
4245         account_tbl->account_id = rc;
4246
4247         if ((error = _get_password_file_name(account_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4248                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4249                 goto FINISH_OFF;
4250         }
4251
4252         EM_DEBUG_LOG(" >>>> ACCOUNT_ID [ %d ] ", account_tbl->account_id);
4253         SNPRINTF(sql_query_string, sizeof(sql_query_string),
4254                 "INSERT INTO mail_account_tbl VALUES "
4255                 "(        "
4256                 "    ? "  /*   account_id */
4257                 "  , ? "  /*   account_name */
4258                 "  , ? "  /*   logo_icon_path */
4259                 "  , ? "  /*   user_data */
4260                 "  , ? "  /*   user_data_length */
4261                 "  , ? "  /*   account_svc_id */
4262                 "  , ? "  /*   sync_status */
4263                 "  , ? "  /*   sync_disabled */
4264                 "  , ? "  /*   default_mail_slot_size */
4265                 "  , ? "  /*   user_display_name */
4266                 "  , ? "  /*   user_email_address */
4267                 "  , ? "  /*   reply_to_address */
4268                 "  , ? "  /*   return_address */
4269                 "  , ? "  /*   incoming_server_type */
4270                 "  , ? "  /*   incoming_server_address */
4271                 "  , ? "  /*   incoming_server_port_number */
4272                 "  , ? "  /*   incoming_server_user_name */
4273                 "  , ? "  /*   incoming_server_password */
4274                 "  , ? "  /*   incoming_server_secure_connection */
4275                 "  , ? "  /*   retrieval_mode */
4276                 "  , ? "  /*   keep_mails_on_pop_server_after_download */
4277                 "  , ? "  /*   check_interval */
4278                 "  , ? "  /*   auto_download_size */
4279                 "  , ? "  /*   outgoing_server_type */
4280                 "  , ? "  /*   outgoing_server_address */
4281                 "  , ? "  /*   outgoing_server_port_number */
4282                 "  , ? "  /*   outgoing_server_user_name */
4283                 "  , ? "  /*   outgoing_server_password */
4284                 "  , ? "  /*   outgoing_server_secure_connection */
4285                 "  , ? "  /*   outgoing_server_need_authentication */
4286                 "  , ? "  /*   outgoing_server_use_same_authenticator */
4287                 "  , ? "  /*   priority */
4288                 "  , ? "  /*   keep_local_copy */
4289                 "  , ? "  /*   req_delivery_receipt */
4290                 "  , ? "  /*   req_read_receipt */
4291                 "  , ? "  /*   download_limit */
4292                 "  , ? "  /*   block_address */
4293                 "  , ? "  /*   block_subject */
4294                 "  , ? "  /*   display_name_from */
4295                 "  , ? "  /*   reply_with_body */
4296                 "  , ? "  /*   forward_with_files */
4297                 "  , ? "  /*   add_myname_card */
4298                 "  , ? "  /*   add_signature */
4299                 "  , ? "  /*   signature */
4300                 "  , ? "  /*   add_my_address_to_bcc */
4301                 "  , ? "  /*   pop_before_smtp */
4302                 "  , ? "  /*   incoming_server_requires_apop */
4303                 "  , ? "  /*   smime_type */
4304                 "  , ? "  /*   certificate_path */
4305                 "  , ? "  /*   cipher_type */
4306                 "  , ? "  /*   digest_type */
4307                 ") ");
4308         
4309         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
4310         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4311                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4312
4313         EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
4314         int i = 0;
4315
4316         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_id);
4317         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4318         _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4319         _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4320         _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4321         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4322         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4323         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4324         _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4325         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4326         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4327         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4328         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4329         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4330         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4331         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4332         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4333         _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
4334         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4335         _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
4336         _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
4337         _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
4338         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
4339         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
4340         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4341         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
4342         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
4343         _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
4344         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
4345         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
4346         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
4347         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
4348         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
4349         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
4350         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
4351         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
4352         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
4353         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
4354         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4355         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
4356         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
4357         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
4358         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
4359         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
4360         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
4361         _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
4362         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
4363         _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
4364         _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
4365         _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
4366         _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
4367         
4368         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4369
4370         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
4371                 ("sqlite3_step fail:%d", rc));
4372         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4373                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
4374         
4375
4376         /*  save passwords to the secure storage */
4377         EM_DEBUG_LOG("save to the secure storage : recv_file[%s], send_file[%s]", recv_password_file_name, send_password_file_name);
4378         if ( (error_from_ssm = ssm_write_buffer(account_tbl->incoming_server_password, strlen(account_tbl->incoming_server_password), recv_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL)) < 0) {
4379                 EM_DEBUG_EXCEPTION("ssm_write_buffer failed [%d] - recv password : file[%s]", error_from_ssm, recv_password_file_name);
4380                 error = EMAIL_ERROR_SYSTEM_FAILURE;
4381                 goto FINISH_OFF;                
4382         }
4383         if ( (error_from_ssm = ssm_write_buffer(account_tbl->outgoing_server_password, strlen(account_tbl->outgoing_server_password), send_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL)) < 0) {
4384                 EM_DEBUG_EXCEPTION("ssm_write_buffer failed [%d] - send password : file[%s]", error_from_ssm, send_password_file_name);
4385                 error = EMAIL_ERROR_SYSTEM_FAILURE;
4386                 goto FINISH_OFF;                
4387         }
4388
4389         ret = true;
4390
4391         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
4392
4393         if (!emstorage_notify_storage_event(NOTI_ACCOUNT_ADD, account_tbl->account_id, 0, NULL, 0))
4394                 EM_DEBUG_EXCEPTION("emstorage_notify_storage_event[NOTI_ACCOUNT_ADD] : Notification failed");
4395         
4396 FINISH_OFF:
4397         
4398         if (hStmt != NULL)  {
4399                 rc = sqlite3_finalize(hStmt);
4400                 if (rc != SQLITE_OK)  {
4401                         EM_DEBUG_LOG("sqlite3_finalize failed [%d]", rc);
4402                         error = EMAIL_ERROR_DB_FAILURE;
4403                 }               
4404         }
4405         else
4406                 EM_DEBUG_LOG("hStmt is NULL!!!");
4407         
4408         _DISCONNECT_DB;
4409         
4410         if (err_code != NULL)
4411                 *err_code = error;
4412
4413         EM_DEBUG_FUNC_END("ret [%d]", ret);
4414         return ret;
4415 }
4416
4417
4418 INTERNAL_FUNC int emstorage_delete_account(int account_id, int transaction, int *err_code)
4419 {
4420         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
4421         
4422         if (account_id < FIRST_ACCOUNT_ID)  {   
4423                 EM_DEBUG_EXCEPTION(" account_id[%d]", account_id);
4424                 
4425                 if (err_code != NULL)
4426                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4427                 return false;
4428         }
4429         
4430         int rc = -1, ret = false;
4431         int error = EMAIL_ERROR_NONE;
4432         sqlite3 *local_db_handle = emstorage_get_db_connection();
4433         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
4434
4435         /*  TODO : delete password files - file names can be obtained from db or a rule that makes a name */
4436         DB_STMT hStmt = NULL;
4437         char sql_query_string[QUERY_SIZE] = {0, };
4438         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4439         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4440
4441         /*  get password file name */
4442         if ((error = _get_password_file_name(account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4443                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4444                 goto FINISH_OFF;
4445         }
4446
4447         /*  delete from db */
4448         memset(sql_query_string, 0x00, sizeof(sql_query_string));
4449         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_account_tbl WHERE account_id = %d", account_id);
4450  
4451         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
4452         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
4453                 ("sqlite3_exec fail:%d", rc));
4454         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4455                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4456
4457         /*  validate account existence */
4458         rc = sqlite3_changes(local_db_handle);
4459         if (rc == 0)  {
4460                 EM_DEBUG_EXCEPTION(" no matched account found...");
4461                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4462                 goto FINISH_OFF;
4463         }
4464
4465                 /*  delete from secure storage */
4466         if (ssm_delete_file(recv_password_file_name,  SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
4467                 EM_DEBUG_EXCEPTION(" ssm_delete_file failed -recv password : file[%s]", recv_password_file_name);
4468                 error = EMAIL_ERROR_SYSTEM_FAILURE;
4469                 goto FINISH_OFF;                
4470         }
4471         if (ssm_delete_file(send_password_file_name,  SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
4472                 EM_DEBUG_EXCEPTION(" ssm_delete_file failed -send password : file[%s]", send_password_file_name);
4473                 error = EMAIL_ERROR_SYSTEM_FAILURE;
4474                 goto FINISH_OFF;                
4475         }
4476         ret = true;
4477         
4478 FINISH_OFF:
4479  
4480         if (hStmt != NULL)  {
4481                 rc = sqlite3_finalize(hStmt);
4482                 if (rc != SQLITE_OK)  {
4483                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
4484                         error = EMAIL_ERROR_DB_FAILURE;
4485                 }
4486         }
4487
4488         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
4489         _DISCONNECT_DB;
4490
4491         if (err_code != NULL)
4492                 *err_code = error;
4493
4494         EM_DEBUG_FUNC_END("ret [%d]", ret);
4495         return ret;
4496 }
4497
4498
4499 INTERNAL_FUNC int emstorage_free_account(emstorage_account_tbl_t** account_list, int count, int *err_code)
4500 {
4501         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%d], err_code[%p]", account_list, count, err_code);
4502         
4503         int ret = false;
4504         int error = EMAIL_ERROR_NONE;
4505         
4506         if (count > 0)  {
4507                 if (!account_list || !*account_list)  {
4508                         EM_DEBUG_EXCEPTION("account_list[%p], count[%d]", account_list, count);
4509                         error = EMAIL_ERROR_INVALID_PARAM;
4510                         goto FINISH_OFF;
4511                 }
4512                 
4513                 emstorage_account_tbl_t* p = *account_list;
4514                 int i = 0;
4515                 
4516                 for (; i < count; i++)  {
4517                         EM_SAFE_FREE(p[i].account_name);
4518                         EM_SAFE_FREE(p[i].incoming_server_address);
4519                         EM_SAFE_FREE(p[i].user_email_address);
4520                         EM_SAFE_FREE(p[i].user_data);
4521                         EM_SAFE_FREE(p[i].incoming_server_user_name);
4522                         EM_SAFE_FREE(p[i].incoming_server_password);
4523                         EM_SAFE_FREE(p[i].outgoing_server_address);
4524                         EM_SAFE_FREE(p[i].outgoing_server_user_name);
4525                         EM_SAFE_FREE(p[i].outgoing_server_password);
4526                         EM_SAFE_FREE(p[i].user_display_name);
4527                         EM_SAFE_FREE(p[i].reply_to_address);
4528                         EM_SAFE_FREE(p[i].return_address);
4529                         EM_SAFE_FREE(p[i].logo_icon_path);
4530                         EM_SAFE_FREE(p[i].options.display_name_from);
4531                         EM_SAFE_FREE(p[i].options.signature);
4532                         EM_SAFE_FREE(p[i].certificate_path);
4533                 }
4534                 
4535                 EM_SAFE_FREE(p); 
4536                 *account_list = NULL;
4537         }
4538         
4539         ret = true;
4540         
4541 FINISH_OFF:
4542         if (err_code != NULL)
4543                 *err_code = error;
4544
4545         EM_DEBUG_FUNC_END("ret [%d]", ret);
4546         return ret;
4547 }
4548
4549 INTERNAL_FUNC int emstorage_get_mailbox_count(int account_id, int local_yn, int *count, int transaction, int *err_code)
4550 {
4551         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);
4552         
4553         if ((account_id < FIRST_ACCOUNT_ID) || (count == NULL))  {
4554                 EM_DEBUG_EXCEPTION(" account_list[%d], local_yn[%d], count[%p]", account_id, local_yn, count);
4555                 
4556                 if (err_code != NULL)
4557                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4558                 return false;
4559         }
4560         
4561         int rc = -1, ret = false;
4562         int error = EMAIL_ERROR_NONE;
4563         char sql_query_string[QUERY_SIZE] = {0, };
4564         
4565         sqlite3 *local_db_handle = emstorage_get_db_connection();
4566         EMSTORAGE_START_READ_TRANSACTION(transaction);
4567         
4568         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);
4569         
4570         char **result;
4571
4572         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
4573         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
4574                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4575
4576         *count = atoi(result[1]);
4577         sqlite3_free_table(result);
4578  
4579
4580         ret = true;
4581         
4582 FINISH_OFF:
4583  
4584         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4585         _DISCONNECT_DB;
4586         
4587         if (err_code != NULL)
4588                 *err_code = error;
4589
4590         EM_DEBUG_FUNC_END("ret [%d]", ret);
4591         return ret;
4592 }
4593
4594 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)
4595 {
4596         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);
4597         
4598         if (!select_num || !mailbox_list) {
4599                 EM_DEBUG_EXCEPTION("Invalid parameters");
4600                 
4601                 if (err_code != NULL)
4602                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4603
4604                 return false;
4605         }
4606
4607         int ret = false;
4608         int error = EMAIL_ERROR_NONE;
4609         char conditional_clause_string[QUERY_SIZE] = {0, };
4610                 
4611         if (account_id == ALL_ACCOUNT) {
4612                 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
4613                         SNPRINTF(conditional_clause_string + strlen(conditional_clause_string), sizeof(conditional_clause_string)-(strlen(conditional_clause_string)+1), " WHERE local_yn = %d ", local_yn);
4614         }
4615         else {
4616                 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d  ", account_id);
4617                 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
4618                         SNPRINTF(conditional_clause_string + strlen(conditional_clause_string), sizeof(conditional_clause_string)-(strlen(conditional_clause_string)+1), " AND local_yn = %d ", local_yn);
4619         }
4620
4621         switch (sort_type) {
4622                 case EMAIL_MAILBOX_SORT_BY_NAME_ASC :
4623                         SNPRINTF(conditional_clause_string + strlen(conditional_clause_string), sizeof(conditional_clause_string)-(strlen(conditional_clause_string)+1), " ORDER BY mailbox_name ASC");
4624                         break;
4625
4626                 case EMAIL_MAILBOX_SORT_BY_NAME_DSC :
4627                         SNPRINTF(conditional_clause_string + strlen(conditional_clause_string), sizeof(conditional_clause_string)-(strlen(conditional_clause_string)+1), " ORDER BY mailbox_name DESC");
4628                         break;
4629
4630                 case EMAIL_MAILBOX_SORT_BY_TYPE_ASC :
4631                         SNPRINTF(conditional_clause_string + strlen(conditional_clause_string), sizeof(conditional_clause_string)-(strlen(conditional_clause_string)+1), " ORDER BY mailbox_type ASC");
4632                         break;
4633
4634                 case EMAIL_MAILBOX_SORT_BY_TYPE_DSC :
4635                         SNPRINTF(conditional_clause_string + strlen(conditional_clause_string), sizeof(conditional_clause_string)-(strlen(conditional_clause_string)+1), " ORDER BY mailbox_type DEC");
4636                         break;
4637         }
4638
4639         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
4640
4641         if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
4642                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4643                 goto FINISH_OFF;
4644         }
4645
4646         ret = true;
4647         
4648 FINISH_OFF:
4649         
4650         if (err_code != NULL)
4651                 *err_code = error;
4652
4653         EM_DEBUG_FUNC_END("ret [%d]", ret);
4654         return ret;
4655 }
4656
4657 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)
4658 {
4659         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);
4660
4661         int ret = false;
4662         int error = EMAIL_ERROR_NONE;
4663         int where_clause_count = 0;
4664         char conditional_clause_string[QUERY_SIZE] = {0, };
4665
4666         if (!select_num || !mailbox_list) {
4667                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4668                 error = EMAIL_ERROR_INVALID_PARAM;
4669                 goto FINISH_OFF;
4670         }
4671
4672         if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL) {
4673                 SNPRINTF(conditional_clause_string + strlen(conditional_clause_string), sizeof(conditional_clause_string)-(strlen(conditional_clause_string)+1), "WHERE local_yn = %d ",  local_yn);
4674                 where_clause_count++;
4675         }
4676
4677         if (account_id > 0) {
4678                 if (where_clause_count == 0) {
4679                         SNPRINTF(conditional_clause_string + strlen(conditional_clause_string), sizeof(conditional_clause_string)-(strlen(conditional_clause_string)+1), " WHERE ");
4680                         SNPRINTF(conditional_clause_string + strlen(conditional_clause_string), sizeof(conditional_clause_string)-(strlen(conditional_clause_string)+1), " account_id = %d ", account_id);
4681                 }
4682                 else
4683                         SNPRINTF(conditional_clause_string + strlen(conditional_clause_string), sizeof(conditional_clause_string)-(strlen(conditional_clause_string)+1), " AND account_id = %d ", account_id);
4684         }
4685
4686         SNPRINTF(conditional_clause_string + strlen(conditional_clause_string), sizeof(conditional_clause_string)-(strlen(conditional_clause_string)+1), " ORDER BY MBT.mailbox_name ");
4687         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
4688
4689         if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, 1, 1, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
4690                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4691                 goto FINISH_OFF;
4692         }
4693
4694         ret = true;
4695         
4696 FINISH_OFF:
4697         
4698         if (err_code != NULL)
4699                 *err_code = error;
4700
4701         EM_DEBUG_FUNC_END("ret [%d]", ret);
4702         return ret;
4703 }
4704
4705 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)
4706 {
4707         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);
4708         if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list)  {
4709                 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);
4710                 
4711                 if (err_code != NULL)
4712                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4713                 return false;
4714         }
4715
4716         int ret = false;
4717         int error = EMAIL_ERROR_NONE;
4718         char conditional_clause_string[QUERY_SIZE] = {0, };
4719
4720         if (parent_mailbox_name)
4721                 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d  AND UPPER(mailbox_name) LIKE UPPER('%s/%%') ORDER BY mailbox_name", account_id, parent_mailbox_name);
4722         else
4723                 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d  AND (mailbox_name NOT LIKE '%%/%%') ORDER BY mailbox_name", account_id);
4724
4725         EM_DEBUG_LOG("conditional_clause_string", conditional_clause_string);
4726
4727         if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
4728                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4729                 goto FINISH_OFF;
4730         }
4731
4732         ret = true;
4733         
4734 FINISH_OFF:
4735         
4736         if (err_code != NULL)
4737                 *err_code = error;
4738
4739         EM_DEBUG_FUNC_END("ret [%d]", ret);
4740         return ret;
4741 }
4742  
4743 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)
4744 {
4745         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);
4746         if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list)  {
4747                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4748                 
4749                 if (err_code != NULL)
4750                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4751
4752                 return false;
4753         }
4754
4755         int ret = false;
4756         int error = EMAIL_ERROR_NONE;
4757         char conditional_clause_string[QUERY_SIZE] = {0, };
4758         
4759         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND modifiable_yn = 0 ORDER BY mailbox_name", account_id);
4760         EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
4761         
4762         if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
4763                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4764                 goto FINISH_OFF;
4765         }
4766         
4767         ret = true;
4768         
4769 FINISH_OFF:
4770         
4771         if (err_code != NULL)
4772                 *err_code = error;
4773
4774         EM_DEBUG_FUNC_END("ret [%d]", ret);
4775         return ret;
4776 }
4777
4778 INTERNAL_FUNC int emstorage_stamp_last_sync_time_of_mailbox(int input_mailbox_id, int input_transaction)
4779 {
4780         EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_transaction [%d]", input_mailbox_id, input_transaction);
4781
4782         int      result_code = false;
4783         int      error = EMAIL_ERROR_NONE;
4784         int      rc;
4785         time_t   current_time = 0;
4786         char     sql_query_string[QUERY_SIZE] = {0, };
4787         sqlite3 *local_db_handle = NULL;
4788
4789         if (!input_mailbox_id )  {
4790                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4791                 return EMAIL_ERROR_INVALID_PARAM;
4792         }
4793
4794         time(&current_time);
4795
4796         local_db_handle = emstorage_get_db_connection();
4797
4798         EMSTORAGE_START_WRITE_TRANSACTION(input_transaction, error);
4799
4800         SNPRINTF(sql_query_string, sizeof(sql_query_string),
4801                 "UPDATE mail_box_tbl SET"
4802                 " last_sync_time = %d"
4803                 " WHERE mailbox_id = %d"
4804                 , (int)current_time
4805                 , input_mailbox_id);
4806
4807         EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
4808
4809         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
4810         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4811                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4812
4813 FINISH_OFF:
4814
4815         if(error == EMAIL_ERROR_NONE)
4816                 result_code = true;
4817
4818         EMSTORAGE_FINISH_WRITE_TRANSACTION(input_transaction, result_code, error);
4819         _DISCONNECT_DB;
4820
4821         EM_DEBUG_FUNC_END("error [%d]", error);
4822         return error;
4823 }
4824
4825 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)
4826 {
4827         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);
4828         EM_PROFILE_BEGIN(profile_emstorage_get_mailbox_by_name);
4829         
4830         if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !result_mailbox)  {
4831                 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], mailbox_name[%s], result_mailbox[%p]", account_id, local_yn, mailbox_name, result_mailbox);
4832                 
4833                 if (err_code != NULL)
4834                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4835                 return false;
4836         }
4837
4838         int ret = false;
4839         int error = EMAIL_ERROR_NONE;
4840         int result_count = 0;
4841         char conditional_clause_string[QUERY_SIZE] = {0, };
4842
4843         if(strcmp(mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
4844                 if (!(*result_mailbox = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t))))  {
4845                         EM_DEBUG_EXCEPTION("malloc failed...");
4846                         error = EMAIL_ERROR_OUT_OF_MEMORY;
4847                         goto FINISH_OFF;
4848                 }
4849
4850                 (*result_mailbox)->mailbox_id                 = 0;
4851                 (*result_mailbox)->account_id                 = account_id;
4852                 (*result_mailbox)->local_yn                   = 1;
4853                 (*result_mailbox)->mailbox_name               = EM_SAFE_STRDUP(mailbox_name);
4854                 (*result_mailbox)->mailbox_type               = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
4855                 (*result_mailbox)->alias                      = EM_SAFE_STRDUP(mailbox_name);
4856                 (*result_mailbox)->sync_with_server_yn        = 0;
4857                 (*result_mailbox)->modifiable_yn              = 1;
4858                 (*result_mailbox)->total_mail_count_on_server = 1;
4859                 (*result_mailbox)->has_archived_mails         = 0;
4860                 (*result_mailbox)->mail_slot_size             = 0x0FFFFFFF;
4861         }
4862         else {
4863
4864                 if (local_yn == -1)
4865                         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND mailbox_name = '%s'", account_id, mailbox_name);
4866                 else
4867                         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);
4868         
4869                 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
4870
4871                 if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, 0, transaction, result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
4872                         EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4873                         goto FINISH_OFF;
4874                 }
4875         }
4876
4877         ret = true;
4878         
4879 FINISH_OFF:
4880
4881         if (err_code != NULL)
4882                 *err_code = error;
4883
4884         EM_PROFILE_END(profile_emstorage_get_mailbox_by_name);
4885         EM_DEBUG_FUNC_END("ret [%d]", ret);
4886         return ret;
4887 }
4888
4889 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)
4890 {
4891         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);
4892
4893         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_name) {
4894
4895                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
4896                 
4897                 if (err_code != NULL)
4898                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4899                 return false;
4900         }
4901
4902         int rc, ret = false;
4903         int error = EMAIL_ERROR_NONE;
4904         emstorage_mailbox_tbl_t *p_data_tbl = NULL;
4905         emstorage_account_tbl_t *account = NULL;
4906         DB_STMT hStmt = NULL;
4907         char sql_query_string[QUERY_SIZE] = {0,};
4908         char *fields = "mailbox_id, account_id, local_yn, mailbox_name, mailbox_type, alias, sync_with_server_yn, modifiable_yn, total_mail_count_on_server, has_archived_mails, mail_slot_size ";
4909         
4910         sqlite3 *local_db_handle = emstorage_get_db_connection();
4911         EMSTORAGE_START_READ_TRANSACTION(transaction);
4912         
4913         /* validate account */
4914         /*  Check whether the account exists. */
4915         if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
4916                 EM_DEBUG_EXCEPTION(" emstorage_get_account_by_id failed - %d", error);
4917                 goto FINISH_OFF;
4918         }
4919
4920         if (account)
4921                 emstorage_free_account(&account, 1, NULL);
4922         
4923         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);
4924
4925         EM_DEBUG_LOG("query = [%s]", sql_query_string);
4926                 
4927         
4928         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
4929         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)));
4930
4931
4932         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4933         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
4934         
4935         if (rc == SQLITE_DONE) {
4936                 EM_DEBUG_EXCEPTION(" no matched mailbox_name found...");
4937                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
4938                 goto FINISH_OFF;
4939         }
4940
4941         
4942         if (!(p_data_tbl = (emstorage_mailbox_tbl_t*)malloc(sizeof(emstorage_mailbox_tbl_t))))  {
4943                 EM_DEBUG_EXCEPTION(" malloc failed...");
4944                 error = EMAIL_ERROR_OUT_OF_MEMORY;
4945                 goto FINISH_OFF;
4946         }
4947         
4948         memset(p_data_tbl, 0x00, sizeof(emstorage_mailbox_tbl_t));
4949         
4950         int col_index = 0;
4951
4952         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
4953         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
4954         _get_stmt_field_data_int(hStmt, &(p_data_tbl->local_yn), col_index++);
4955         _get_stmt_field_data_string(hStmt, &(p_data_tbl->mailbox_name), 0, col_index++);
4956         _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->mailbox_type), col_index++);
4957         _get_stmt_field_data_string(hStmt, &(p_data_tbl->alias), 0, col_index++);
4958         _get_stmt_field_data_int(hStmt, &(p_data_tbl->sync_with_server_yn), col_index++);
4959         _get_stmt_field_data_int(hStmt, &(p_data_tbl->modifiable_yn), col_index++);
4960         _get_stmt_field_data_int(hStmt, &(p_data_tbl->total_mail_count_on_server), col_index++);
4961         _get_stmt_field_data_int(hStmt, &(p_data_tbl->has_archived_mails), col_index++);
4962         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_slot_size), col_index++);
4963
4964         ret = true;
4965         
4966 FINISH_OFF:
4967         if (ret == true)
4968                 *mailbox_name = p_data_tbl;
4969         else if (p_data_tbl != NULL)
4970                 emstorage_free_mailbox(&p_data_tbl, 1, NULL);
4971         
4972         if (hStmt != NULL)  {
4973                 rc = sqlite3_finalize(hStmt);
4974                 if (rc != SQLITE_OK)  {
4975                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
4976                         error = EMAIL_ERROR_DB_FAILURE;
4977                 }
4978         }
4979         
4980         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4981         _DISCONNECT_DB;
4982         
4983         if (err_code != NULL)
4984                 *err_code = error;
4985
4986         EM_DEBUG_FUNC_END("ret [%d]", ret);
4987         return ret;
4988 }
4989
4990 INTERNAL_FUNC int emstorage_get_mailbox_by_id(int input_mailbox_id, emstorage_mailbox_tbl_t** output_mailbox)
4991 {
4992         EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_mailbox[%p]", input_mailbox_id, output_mailbox);
4993
4994         if (input_mailbox_id <= 0 || !output_mailbox)  {
4995                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4996                 return  EMAIL_ERROR_INVALID_PARAM;
4997         }
4998
4999         int  ret = EMAIL_ERROR_NONE;
5000         int  result_count = 0;
5001         char conditional_clause_string[QUERY_SIZE] = {0, };
5002
5003         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE mailbox_id = %d", input_mailbox_id);
5004
5005         EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5006
5007         if( (ret = emstorage_query_mailbox_tbl(conditional_clause_string, false, false, output_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5008                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", ret);
5009                 goto FINISH_OFF;
5010         }
5011
5012 FINISH_OFF:
5013
5014         EM_DEBUG_FUNC_END("ret [%d]", ret);
5015         return ret;
5016 }
5017
5018 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)
5019 {
5020         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);
5021         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_id)  {
5022                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_id[%p]", account_id, mailbox_type, mailbox_id);
5023                 if (err_code != NULL)
5024                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5025                 return false;
5026         }
5027
5028         int rc, ret = false;
5029         int error = EMAIL_ERROR_NONE;
5030         emstorage_account_tbl_t* account = NULL;
5031         DB_STMT hStmt = NULL;
5032         char sql_query_string[QUERY_SIZE] = {0, };
5033         
5034         sqlite3 *local_db_handle = emstorage_get_db_connection();
5035         EMSTORAGE_START_READ_TRANSACTION(transaction);
5036
5037         /*  Check whether the account exists. */
5038         if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
5039                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5040                 goto FINISH_OFF;
5041         }
5042
5043         if (account )
5044                 emstorage_free_account(&account, 1, NULL);
5045         
5046         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);
5047
5048         EM_DEBUG_LOG("query = [%s]", sql_query_string);
5049
5050
5051         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
5052
5053         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5054                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5055
5056
5057         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5058         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5059                 ("sqlite3_step fail:%d", rc));
5060
5061         if (rc == SQLITE_DONE) {
5062                 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5063                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5064                 goto FINISH_OFF;
5065         }
5066
5067         _get_stmt_field_data_int(hStmt, mailbox_id, 0);
5068
5069         ret = true;
5070
5071 FINISH_OFF:
5072         if (hStmt != NULL)  {
5073                 rc = sqlite3_finalize(hStmt);
5074                 if (rc != SQLITE_OK)  {
5075                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5076                         error = EMAIL_ERROR_DB_FAILURE;
5077                 }
5078         }
5079
5080         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5081         _DISCONNECT_DB;
5082
5083         if (err_code != NULL)
5084                 *err_code = error;
5085
5086         EM_DEBUG_FUNC_END("ret [%d]", ret);
5087         return ret;
5088 }
5089
5090 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)
5091 {
5092         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);
5093         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_name)  {
5094                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
5095                 if (err_code != NULL)
5096                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5097                 return false;
5098         }
5099
5100         int rc, ret = false;
5101         int error = EMAIL_ERROR_NONE;
5102         emstorage_account_tbl_t* account = NULL;
5103         DB_STMT hStmt = NULL;
5104         char sql_query_string[QUERY_SIZE] = {0, };
5105
5106         sqlite3 *local_db_handle = emstorage_get_db_connection();
5107         EMSTORAGE_START_READ_TRANSACTION(transaction);
5108
5109         /*  Check whether the account exists. */
5110         if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
5111                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5112                 goto FINISH_OFF;
5113         }
5114
5115         if (account )
5116                 emstorage_free_account(&account, 1, NULL);
5117
5118         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);
5119
5120         EM_DEBUG_LOG("query = [%s]", sql_query_string);
5121
5122         
5123         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
5124
5125         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5126                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5127
5128
5129         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5130         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5131                 ("sqlite3_step fail:%d", rc));
5132         
5133         if (rc == SQLITE_DONE) {
5134                 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5135                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5136                 goto FINISH_OFF;
5137         }
5138  
5139         _get_stmt_field_data_string(hStmt, mailbox_name, 0, 0);
5140
5141         ret = true;
5142         
5143 FINISH_OFF:
5144         if (hStmt != NULL)  {
5145                 rc = sqlite3_finalize(hStmt);
5146                 if (rc != SQLITE_OK)  {
5147                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5148                         error = EMAIL_ERROR_DB_FAILURE;
5149                 }
5150         }
5151         
5152         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5153         _DISCONNECT_DB;
5154         
5155         if (err_code != NULL)
5156                 *err_code = error;
5157
5158         EM_DEBUG_FUNC_END("ret [%d]", ret);
5159         return ret;
5160 }
5161
5162 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)
5163 {
5164         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);
5165         int rc, ret = false;
5166         int error = EMAIL_ERROR_NONE;
5167         char sql_query_string[QUERY_SIZE] = {0, };
5168         
5169         sqlite3 *local_db_handle = emstorage_get_db_connection();
5170
5171         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5172         
5173         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5174                 "UPDATE mail_box_tbl SET"
5175                 " modifiable_yn = %d"
5176                 " WHERE account_id = %d"
5177                 " AND local_yn = %d"
5178                 " AND mailbox_name = '%s'"
5179                 , modifiable_yn
5180                 , account_id
5181                 , local_yn
5182                 , mailbox_name);
5183         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5184         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5185                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5186  
5187         ret = true;
5188
5189 FINISH_OFF:
5190         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5191         _DISCONNECT_DB;
5192         
5193         if (err_code != NULL)
5194                 *err_code = error;
5195
5196         EM_DEBUG_FUNC_END("ret [%d]", ret);
5197         return ret;
5198
5199 }
5200
5201
5202 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)
5203 {
5204         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);
5205         int rc, ret = false;
5206         int error = EMAIL_ERROR_NONE;
5207         char sql_query_string[QUERY_SIZE] = {0, };
5208         
5209         if (account_id <= 0 || input_mailbox_id <= 0)  {
5210                 EM_DEBUG_EXCEPTION("account_id[%d], input_mailbox_id[%d]", account_id, input_mailbox_id);
5211                 
5212                 if (err_code != NULL)
5213                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5214                 return false;
5215         }       
5216         sqlite3 *local_db_handle = emstorage_get_db_connection();
5217
5218         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5219
5220         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5221                 "UPDATE mail_box_tbl SET"
5222                 " total_mail_count_on_server = %d"
5223                 " WHERE account_id = %d"
5224                 " AND mailbox_id = %d"
5225                 , total_count_on_server
5226                 , account_id
5227                 , input_mailbox_id);
5228         EM_DEBUG_LOG("query[%s]", sql_query_string);
5229         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5230         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5231                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5232  
5233         ret = true;
5234
5235 FINISH_OFF:
5236         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5237         _DISCONNECT_DB;
5238         
5239         if (err_code != NULL)
5240                 *err_code = error;
5241
5242         EM_DEBUG_FUNC_END("ret [%d]", ret);
5243         return ret;
5244
5245 }
5246
5247
5248 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)
5249 {
5250         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);
5251         
5252         if (account_id < FIRST_ACCOUNT_ID || input_mailbox_id <= 0 || !result_mailbox)  {
5253                 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);
5254                 
5255                 if (err_code != NULL)
5256                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5257                 return false;
5258         }
5259         
5260         int rc, ret = false;
5261         int error = EMAIL_ERROR_NONE;
5262         char sql_query_string[QUERY_SIZE] = {0, };
5263         DB_STMT hStmt = NULL;
5264         int i = 0;
5265         
5266         sqlite3 *local_db_handle = emstorage_get_db_connection();
5267         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5268         
5269         if (local_yn != -1) {
5270                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5271                         "UPDATE mail_box_tbl SET"
5272                         "  mailbox_id = ?"
5273                         ", mailbox_name = ?"
5274                         ", mailbox_type = ?"
5275                         ", alias = ?"
5276                         ", sync_with_server_yn = ?"
5277                         ", modifiable_yn= ?"
5278                         ", mail_slot_size= ?"
5279                         ", total_mail_count_on_server = ?"
5280                         " WHERE account_id = %d"
5281                         " AND local_yn = %d"
5282                         " AND mailbox_id = '%d'"
5283                         , account_id
5284                         , local_yn
5285                         , input_mailbox_id);
5286         }
5287         else {
5288                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5289                         "UPDATE mail_box_tbl SET"
5290                         "  mailbox_id = ?"
5291                         ", mailbox_name = ?"
5292                         ", mailbox_type = ?"
5293                         ", alias = ?"
5294                         ", sync_with_server_yn = ?"
5295                         ", modifiable_yn= ?"
5296                         ", mail_slot_size= ?"
5297                         ", total_mail_count_on_server = ?"
5298                         " WHERE account_id = %d"
5299                         " AND mailbox_id = '%d'"
5300                         , account_id
5301                         , input_mailbox_id);
5302         }
5303
5304                 
5305         
5306         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
5307         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5308                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5309
5310         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_id);
5311         _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);
5312         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_type);
5313         _bind_stmt_field_data_string(hStmt, i++, (char *)result_mailbox->alias ? result_mailbox->alias : "", 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
5314         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->sync_with_server_yn);
5315         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->modifiable_yn);
5316         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mail_slot_size);
5317         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->total_mail_count_on_server);
5318         
5319
5320         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5321         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
5322                 ("sqlite3_step fail:%d", rc));
5323         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5324                 ("sqlite3_step fail:%d", rc));
5325
5326         ret = true;
5327
5328 FINISH_OFF:
5329         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5330         _DISCONNECT_DB;
5331         
5332         if (hStmt != NULL)  {
5333                 rc = sqlite3_finalize(hStmt);
5334                 if (rc != SQLITE_OK)  {
5335                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5336                         error = EMAIL_ERROR_DB_FAILURE;
5337                 }
5338         }
5339         
5340         if (err_code != NULL)
5341                 *err_code = error;
5342
5343         EM_DEBUG_FUNC_END("ret [%d]", ret);
5344         return ret;
5345 }
5346
5347
5348 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)
5349 {
5350         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);
5351         
5352         if (account_id < FIRST_ACCOUNT_ID || !mailbox_name)  {
5353                 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], mailbox_name[%s]", account_id, local_yn, mailbox_name);
5354                 
5355                 if (err_code != NULL)
5356                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5357                 return false;
5358         }
5359         
5360         int rc, ret = false;
5361         int error = EMAIL_ERROR_NONE;
5362         char sql_query_string[QUERY_SIZE] = {0, };
5363         
5364         sqlite3 *local_db_handle = emstorage_get_db_connection();
5365         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5366         
5367         EM_DEBUG_LOG("emstorage_update_mailbox_type");
5368         
5369         DB_STMT hStmt = NULL;
5370         int i = 0;
5371
5372         /*  Update mail_box_tbl */
5373         if (local_yn != -1) {
5374                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5375                         "UPDATE mail_box_tbl SET"
5376                         " mailbox_type = ?"
5377                         " WHERE account_id = %d"
5378                         " AND local_yn = %d"
5379                         " AND mailbox_name = '%s'"
5380                         , account_id
5381                         , local_yn
5382                         , mailbox_name);
5383         }
5384         else {
5385                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5386                         "UPDATE mail_box_tbl SET"
5387                         " mailbox_type = ?"
5388                         " WHERE account_id = %d"
5389                         " AND mailbox_name = '%s'"
5390                         , account_id
5391                         , mailbox_name);
5392         }
5393
5394         EM_DEBUG_LOG("SQL(%s)", sql_query_string);
5395         
5396         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
5397         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5398                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5399
5400         _bind_stmt_field_data_int(hStmt, i++, new_mailbox_type);
5401         
5402
5403         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5404         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
5405                 ("sqlite3_step fail:%d", rc));
5406         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5407                 ("sqlite3_step fail:%d", rc));
5408
5409         if (hStmt != NULL)  {
5410                 rc = sqlite3_finalize(hStmt);
5411                 if (rc != SQLITE_OK)  {
5412                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5413                         error = EMAIL_ERROR_DB_FAILURE;
5414                 }
5415                 hStmt = NULL;
5416         }
5417         
5418         /*  Update mail_tbl */
5419         i = 0;
5420         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5421                         "UPDATE mail_tbl SET"
5422                         " mailbox_type = ?"
5423                         " WHERE account_id = %d"
5424                         " AND mailbox_name = '%s'"
5425                         , account_id
5426                         , mailbox_name);
5427
5428         EM_DEBUG_LOG("SQL[%s]", sql_query_string);
5429         
5430         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
5431         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5432                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5433
5434         _bind_stmt_field_data_int(hStmt, i++, new_mailbox_type);
5435         
5436
5437         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5438         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
5439                 ("sqlite3_step fail:%d", rc));
5440         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5441                 ("sqlite3_step fail:%d", rc));
5442
5443         ret = true;
5444
5445 FINISH_OFF:
5446         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5447         _DISCONNECT_DB;
5448         
5449         if (hStmt != NULL)  {
5450                 rc = sqlite3_finalize(hStmt);
5451                 if (rc != SQLITE_OK)  {
5452                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5453                         error = EMAIL_ERROR_DB_FAILURE;
5454                 }
5455         }
5456         
5457         if (err_code != NULL)
5458                 *err_code = error;
5459
5460         EM_DEBUG_FUNC_END("ret [%d]", ret);
5461         return ret;
5462 }
5463
5464
5465 INTERNAL_FUNC int emstorage_add_mailbox(emstorage_mailbox_tbl_t* mailbox_tbl, int transaction, int *err_code)
5466 {
5467         EM_DEBUG_FUNC_BEGIN("mailbox_tbl[%p], transaction[%d], err_code[%p]", mailbox_tbl, transaction, err_code);
5468         
5469         if (!mailbox_tbl)  {
5470                 if (err_code != NULL)
5471                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5472                 return false;
5473         }
5474
5475         int rc, ret = false;
5476         int error = EMAIL_ERROR_NONE;
5477         DB_STMT hStmt = NULL;
5478         char sql_query_string[QUERY_SIZE] = {0,};
5479         char **result = NULL;
5480         time_t current_time;
5481         sqlite3 *local_db_handle = emstorage_get_db_connection();
5482         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5483         
5484         EM_SAFE_STRCPY(sql_query_string, "SELECT max(rowid) FROM mail_box_tbl;");
5485
5486         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5487         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
5488                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5489
5490         time(&current_time);
5491
5492         if (NULL == result[1])
5493                 rc = 1;
5494         else
5495                 rc = atoi(result[1]) + 1;
5496         sqlite3_free_table(result);
5497
5498         memset(sql_query_string, 0, sizeof(char) * QUERY_SIZE);
5499
5500         mailbox_tbl->mailbox_id = rc;
5501
5502         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5503                 "INSERT INTO mail_box_tbl VALUES "
5504                 "( ?"    /* mailbox_id */
5505                 ", ?"    /* account_id */
5506                 ", ?"    /* local_yn */
5507                 ", ?"    /* mailbox_name */
5508                 ", ?"    /* mailbox_type */
5509                 ", ?"    /* alias */
5510                 ", ?"    /* sync_with_server_yn */
5511                 ", ?"    /* modifiable_yn */
5512                 ", ?"    /* total_mail_count_on_server */
5513                 ", ?"    /* has_archived_mails */
5514                 ", ?"    /* mail_slot_size */
5515                 ", ? )");/* last_sync_time */
5516         
5517         
5518         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
5519         EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
5520         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5521                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5522
5523         int col_index = 0;
5524
5525         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_id);
5526         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->account_id);
5527         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->local_yn);
5528         _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
5529         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_type);
5530         _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->alias, 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
5531         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->sync_with_server_yn);
5532         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->modifiable_yn);
5533         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->total_mail_count_on_server);
5534         _bind_stmt_field_data_int(hStmt, col_index++, 0);
5535         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mail_slot_size);
5536         _bind_stmt_field_data_int(hStmt, col_index++, current_time);
5537
5538
5539         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5540         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5541                 ("sqlite3_step fail:%dn", rc));
5542
5543         ret = true;
5544         
5545 FINISH_OFF:
5546         if (hStmt != NULL)  {
5547                 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
5548
5549                 rc = sqlite3_finalize(hStmt);
5550                 if (rc != SQLITE_OK)  {
5551                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
5552                         error = EMAIL_ERROR_DB_FAILURE;
5553                 }
5554         }
5555
5556         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5557         _DISCONNECT_DB;
5558         
5559         if (error == EMAIL_ERROR_NONE && !emstorage_notify_storage_event(NOTI_MAILBOX_ADD, mailbox_tbl->account_id, mailbox_tbl->mailbox_id, mailbox_tbl->mailbox_name, 0))
5560                 EM_DEBUG_EXCEPTION("emstorage_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
5561
5562         if (err_code != NULL)
5563                 *err_code = error;
5564
5565         EM_DEBUG_FUNC_END("ret [%d]", ret);
5566         return ret;
5567 }
5568
5569 INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(int account_id, int modifiable_yn, int transaction, int *err_code)
5570 {
5571         EM_DEBUG_FUNC_BEGIN("account_id[%d], modifiable_yn[%d], err_code[%p]", account_id, modifiable_yn, err_code);
5572         
5573         if (account_id < FIRST_ACCOUNT_ID)  {
5574
5575                 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
5576                 
5577                 if (err_code != NULL)
5578                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5579                 return false;
5580         }
5581         int rc, ret = false;
5582         int error = EMAIL_ERROR_NONE;
5583         char sql_query_string[QUERY_SIZE] = {0,};
5584
5585         sqlite3 *local_db_handle = emstorage_get_db_connection();
5586         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5587
5588
5589         SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET modifiable_yn = %d WHERE account_id = %d", modifiable_yn, account_id);
5590  
5591         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5592         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5593                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5594
5595         rc = sqlite3_changes(local_db_handle);
5596         if (rc == 0) 
5597                 EM_DEBUG_EXCEPTION("All mailbox_name modifiable_yn set to 0 already");
5598  
5599                 
5600         ret = true;
5601         
5602 FINISH_OFF:
5603         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5604         _DISCONNECT_DB;
5605
5606         if (err_code != NULL)
5607                 *err_code = error;
5608         EM_DEBUG_FUNC_END("ret [%d]", ret);
5609         return ret;
5610         
5611
5612 }
5613
5614 INTERNAL_FUNC int emstorage_delete_mailbox(int account_id, int local_yn, int input_mailbox_id, int transaction, int *err_code)
5615 {
5616         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);
5617         
5618         if (account_id < FIRST_ACCOUNT_ID)  {   
5619                 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
5620                 
5621                 if (err_code != NULL)
5622                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5623                 return false;
5624         }
5625         
5626         int rc, ret = false;
5627         int error = EMAIL_ERROR_NONE;
5628         char sql_query_string[QUERY_SIZE] = {0, };
5629         
5630         sqlite3 *local_db_handle = emstorage_get_db_connection();
5631         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5632         
5633         if (local_yn == -1)
5634                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_box_tbl WHERE account_id = %d ", account_id);
5635         else
5636                 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);
5637         
5638         if (input_mailbox_id > 0)  {            /*  0 means all mailbox_name */
5639                 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(1+ strlen(sql_query_string)), "AND mailbox_id = %d", input_mailbox_id);
5640         }
5641         
5642         EM_DEBUG_LOG("mailbox sql_query_string [%s]", sql_query_string);
5643         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5644         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5645                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5646         
5647         rc = sqlite3_changes(local_db_handle);
5648         if (rc == 0)  {
5649                 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
5650                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5651                 ret = true;
5652         }
5653         ret = true;
5654         
5655 FINISH_OFF:
5656
5657         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5658         _DISCONNECT_DB;
5659         
5660         if(error == EMAIL_ERROR_NONE) {
5661                 if (!emstorage_notify_storage_event(NOTI_MAILBOX_DELETE, account_id, input_mailbox_id, NULL, 0))
5662                         EM_DEBUG_EXCEPTION("emstorage_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
5663         }
5664
5665         if (err_code != NULL)
5666                 *err_code = error;
5667
5668         EM_DEBUG_FUNC_END("ret [%d]", ret);
5669         return ret;
5670 }
5671
5672 INTERNAL_FUNC int emstorage_free_mailbox(emstorage_mailbox_tbl_t** mailbox_list, int count, int *err_code)
5673 {
5674         EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d], err_code[%p]", mailbox_list, count, err_code);
5675         
5676         int ret = false;
5677         int error = EMAIL_ERROR_NONE;
5678                 
5679         if (count > 0)  {
5680                 if (!mailbox_list || !*mailbox_list)  {
5681                         EM_DEBUG_EXCEPTION(" mailbox_list[%p], count[%d]", mailbox_list, count);
5682                         
5683                         error = EMAIL_ERROR_INVALID_PARAM;
5684                         goto FINISH_OFF;
5685                 }
5686                 
5687                 emstorage_mailbox_tbl_t* p = *mailbox_list;
5688                 int i = 0;
5689                 
5690                 for (; i < count; i++) {
5691                         EM_SAFE_FREE(p[i].mailbox_name);
5692                         EM_SAFE_FREE(p[i].alias);
5693                 }
5694                 
5695                 EM_SAFE_FREE(p); *mailbox_list = NULL;
5696         }
5697         
5698         ret = true;
5699         
5700 FINISH_OFF:
5701         if (err_code != NULL)
5702                 *err_code = error;
5703
5704         EM_DEBUG_FUNC_END("ret [%d]", ret);
5705         return ret;
5706 }
5707
5708 INTERNAL_FUNC int emstorage_get_count_read_mail_uid(int account_id, char *mailbox_name, int *count, int transaction, int *err_code)
5709 {
5710         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);
5711         
5712         if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !count)  {
5713                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], count[%p], exist[%p]", account_id, mailbox_name, count);
5714                 
5715                 if (err_code != NULL)
5716                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5717                 return false;
5718         }
5719         
5720         int rc = -1, ret = false;
5721         int error = EMAIL_ERROR_NONE;
5722         char sql_query_string[QUERY_SIZE] = {0, };
5723
5724         sqlite3 *local_db_handle = emstorage_get_db_connection();
5725         EMSTORAGE_START_READ_TRANSACTION(transaction);
5726         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);
5727         EM_DEBUG_LOG(">>> SQL [ %s ] ", sql_query_string);
5728
5729         char **result;
5730
5731         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5732         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
5733                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5734
5735         *count = atoi(result[1]);
5736         sqlite3_free_table(result);
5737         
5738         ret = true;
5739         
5740 FINISH_OFF:
5741         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5742         _DISCONNECT_DB;
5743         
5744         if (err_code != NULL)
5745                 *err_code = error;
5746
5747         EM_DEBUG_FUNC_END("ret [%d]", ret);
5748         return ret;
5749 }
5750
5751
5752
5753 INTERNAL_FUNC int emstorage_check_read_mail_uid(int account_id, char *mailbox_name, char *uid, int *exist, int transaction, int *err_code)
5754 {
5755         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);
5756         
5757         if (account_id < FIRST_ACCOUNT_ID || !uid || !exist)  { 
5758                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], uid[%p], exist[%p]", account_id, mailbox_name , uid, exist);
5759                 
5760                 if (err_code != NULL)
5761                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5762                 return false;
5763         }
5764         
5765         int rc = -1, ret = false;
5766         int error = EMAIL_ERROR_NONE;
5767         char sql_query_string[QUERY_SIZE] = {0, };
5768         sqlite3 *local_db_handle = emstorage_get_db_connection();
5769         EMSTORAGE_START_READ_TRANSACTION(transaction);
5770         
5771         if (mailbox_name)  {
5772                 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);
5773         }
5774         else  {
5775                 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);
5776         }
5777         
5778         char **result;
5779
5780         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5781         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
5782                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5783
5784         *exist = atoi(result[1]);
5785         sqlite3_free_table(result);
5786  
5787         if (*exist > 0)
5788                 *exist = 1;             
5789         else
5790                 *exist = 0;
5791         
5792         ret = true;
5793         
5794 FINISH_OFF:
5795         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5796         _DISCONNECT_DB;
5797         
5798         if (err_code != NULL)
5799                 *err_code = error;
5800
5801         EM_DEBUG_FUNC_END("ret [%d]", ret);
5802         return ret;
5803 }
5804
5805 INTERNAL_FUNC int emstorage_get_downloaded_mail(int mail_id, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
5806 {
5807         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], err_code[%p]", mail_id, mail, err_code);
5808         
5809         if (!mail)  {
5810                 EM_DEBUG_EXCEPTION(" mail_id[%d], mail[%p]", mail_id, mail);
5811                 if (err_code != NULL)
5812                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5813                 return false;
5814         }
5815         
5816         int rc, ret = false, temp_rule;
5817         int error = EMAIL_ERROR_NONE;
5818         DB_STMT hStmt = NULL;
5819         char sql_query_string[QUERY_SIZE] = {0, };
5820
5821         sqlite3 *local_db_handle = emstorage_get_db_connection();
5822         EMSTORAGE_START_READ_TRANSACTION(transaction);
5823         
5824         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE local_uid = %d", mail_id);
5825         
5826         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
5827         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
5828
5829         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5830                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5831
5832
5833         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5834         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5835                 ("sqlite3_step fail:%d", rc));
5836
5837         *mail = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
5838         if (*mail == NULL) {
5839                 error = EMAIL_ERROR_OUT_OF_MEMORY;
5840                 EM_DEBUG_EXCEPTION("Memory allocation for mail failed.");
5841                 goto FINISH_OFF;
5842
5843         }
5844         memset(*mail, 0x00, sizeof(emstorage_mail_tbl_t));
5845
5846         _get_stmt_field_data_int(hStmt, &((*mail)->account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5847         _get_stmt_field_data_int(hStmt, &((*mail)->mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5848         _get_stmt_field_data_string(hStmt, &((*mail)->mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5849         _get_stmt_field_data_int(hStmt, &((*mail)->mail_id), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5850         _get_stmt_field_data_string(hStmt, &((*mail)->server_mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5851         _get_stmt_field_data_string(hStmt, &((*mail)->server_mail_id), 0, S_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5852         _get_stmt_field_data_int(hStmt, &((*mail)->mail_size), DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5853         _get_stmt_field_data_int(hStmt, &temp_rule, FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5854         
5855         (*mail)->server_mail_status = 1;
5856         
5857         ret = true;
5858         
5859 FINISH_OFF:
5860         if (hStmt != NULL)  {
5861                 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
5862                 rc = sqlite3_finalize(hStmt);
5863                 if (rc != SQLITE_OK)  {
5864                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5865                         error = EMAIL_ERROR_DB_FAILURE;
5866                 }
5867         }
5868         
5869         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5870         _DISCONNECT_DB;
5871         
5872         if (err_code != NULL)
5873                 *err_code = error;
5874
5875         EM_DEBUG_FUNC_END("ret [%d]", ret);
5876         return ret;
5877 }
5878
5879 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)
5880 {
5881         EM_PROFILE_BEGIN(emStorageGetDownloadList);
5882         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);
5883         if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid || !count)  {
5884                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_id[%s], read_mail_uid[%p], count[%p]", account_id, mailbox_id, read_mail_uid, count);
5885                 
5886                 if (err_code != NULL)
5887                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5888                 return false;
5889         }
5890
5891         int rc, ret = false;
5892         int error = EMAIL_ERROR_NONE;
5893  
5894         DB_STMT hStmt = NULL;
5895         char sql_query_string[QUERY_SIZE] = {0, };
5896         
5897         emstorage_read_mail_uid_tbl_t* p_data_tbl = NULL;
5898         int i = 0;
5899         
5900         sqlite3 *local_db_handle = emstorage_get_db_connection();
5901         EMSTORAGE_START_READ_TRANSACTION(transaction);
5902
5903         if (mailbox_id)
5904                 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);
5905         else
5906                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
5907
5908         EM_DEBUG_LOG(" sql_query_string : %s", sql_query_string);
5909  
5910         
5911         
5912         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
5913         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
5914         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5915                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5916
5917
5918         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5919         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5920                 ("sqlite3_step fail:%d", rc));
5921         
5922         char **result;
5923         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL); */
5924         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL), rc);
5925         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
5926                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5927
5928         sqlite3_free_table(result);
5929         if (*count == 0)  {
5930                 EM_DEBUG_LOG("No mail found in mail_read_mail_uid_tbl");
5931                 ret = true;
5932                 goto FINISH_OFF;
5933         }
5934
5935
5936         if (!(p_data_tbl = (emstorage_read_mail_uid_tbl_t*)malloc(sizeof(emstorage_read_mail_uid_tbl_t) * *count)))  {
5937                 EM_DEBUG_EXCEPTION(" malloc failed...");
5938                 error = EMAIL_ERROR_OUT_OF_MEMORY;
5939                 goto FINISH_OFF;
5940         }
5941
5942         memset(p_data_tbl, 0x00, sizeof(emstorage_read_mail_uid_tbl_t)* *count);
5943
5944         for (i = 0; i < *count; ++i)  {
5945                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5946                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id),LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5947                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].local_uid), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5948                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5949                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].s_uid), 0, S_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5950                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].data1), DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5951                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].data2), 0, DATA2_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5952                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag), FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5953
5954                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5955                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5956                         ("sqlite3_step fail:%d", rc));
5957         }
5958         
5959         ret = true;
5960
5961 FINISH_OFF:
5962         if (ret == true)
5963                 *read_mail_uid = p_data_tbl;
5964         else if (p_data_tbl)
5965                 emstorage_free_read_mail_uid(&p_data_tbl, *count, NULL);
5966         
5967         if (hStmt != NULL)  {
5968                 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
5969                 rc = sqlite3_finalize(hStmt);
5970                 if (rc != SQLITE_OK)  {
5971                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
5972                         
5973                         error = EMAIL_ERROR_DB_FAILURE;
5974                 }
5975         }
5976         
5977         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5978         _DISCONNECT_DB;
5979         
5980         if (err_code != NULL)
5981                 *err_code = error;
5982
5983         EM_PROFILE_END(emStorageGetDownloadList);
5984         EM_DEBUG_FUNC_END("ret [%d]", ret);
5985         return ret;
5986 }
5987
5988 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)
5989 {
5990         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);
5991         
5992         if (account_id < FIRST_ACCOUNT_ID || !mail_size)  {
5993                 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);
5994                 
5995                 if (err_code != NULL)
5996                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5997                 return false;
5998         }
5999         
6000         int rc, ret = false;
6001         int error = EMAIL_ERROR_NONE;
6002         DB_STMT hStmt = NULL;
6003         char sql_query_string[QUERY_SIZE] = {0, };
6004         
6005         
6006         sqlite3 *local_db_handle = emstorage_get_db_connection();
6007         EMSTORAGE_START_READ_TRANSACTION(transaction);
6008         
6009         if (mailbox_name) {
6010                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6011                         "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6012                         "WHERE account_id = %d "
6013                         "AND mailbox_id = '%s' "
6014                         "AND local_uid = %d "
6015                         "AND mailbox_name = '%s' "
6016                         "AND s_uid = '%s'",
6017                         account_id, mailbox_id, local_uid, mailbox_name, uid);
6018         }
6019         else {
6020                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6021                         "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6022                         "WHERE account_id = %d "
6023                         "AND mailbox_id = '%s' "
6024                         "AND local_uid = %d "
6025                         "AND s_uid = '%s'",
6026                         account_id, mailbox_id, local_uid, uid);
6027         }
6028         
6029         
6030         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
6031         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6032                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6033
6034
6035         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6036         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6037                 ("sqlite3_step fail:%d", rc));
6038
6039         if (rc == SQLITE_DONE) {
6040                 EM_DEBUG_EXCEPTION(" no matched mail found....");
6041                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
6042                 goto FINISH_OFF;                
6043         }
6044  
6045         _get_stmt_field_data_int(hStmt, mail_size, 0);
6046         
6047         ret = true;
6048         
6049 FINISH_OFF:
6050         if (hStmt != NULL)  {
6051                 rc = sqlite3_finalize(hStmt);
6052                 if (rc != SQLITE_OK)  {
6053                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6054                         
6055                         error = EMAIL_ERROR_DB_FAILURE;
6056                 }
6057         }
6058         
6059         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6060         _DISCONNECT_DB;
6061         
6062         if (err_code != NULL)
6063                 *err_code = error;
6064
6065         EM_DEBUG_FUNC_END("ret [%d]", ret);
6066         return ret;
6067 }
6068
6069 INTERNAL_FUNC int emstorage_add_downloaded_mail(emstorage_read_mail_uid_tbl_t* read_mail_uid, int transaction, int *err_code)
6070 {
6071         EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], transaction[%d], err_code[%p]", read_mail_uid, transaction, err_code);
6072         
6073         if (!read_mail_uid)  {
6074                 EM_DEBUG_EXCEPTION("read_mail_uid[%p]", read_mail_uid);
6075                 if (err_code != NULL)
6076                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6077                 return false;
6078         }
6079         
6080         int rc, rc2,  ret = false;
6081         int error = EMAIL_ERROR_NONE;
6082         DB_STMT hStmt = NULL;
6083         char sql_query_string[QUERY_SIZE] = {0, };
6084         
6085         sqlite3 *local_db_handle = emstorage_get_db_connection();
6086         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6087         
6088         char *sql = "SELECT max(rowid) FROM mail_read_mail_uid_tbl;";
6089         char **result = NULL;
6090
6091
6092         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6093         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6094                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6095
6096         if (NULL==result[1]) rc = 1;
6097         else rc = atoi(result[1])+1;
6098         sqlite3_free_table(result);
6099         
6100         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6101                 "INSERT INTO mail_read_mail_uid_tbl VALUES "
6102                 "( ?"
6103                 ", ?"
6104                 ", ?"
6105                 ", ?"
6106                 ", ?"
6107                 ", ?"
6108                 ", ?"
6109                 ", ?"
6110                 ", ? )");
6111         
6112         
6113         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc2);
6114         if (rc2 != SQLITE_OK)  {
6115                 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6116                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6117                 
6118                 error = EMAIL_ERROR_DB_FAILURE;
6119                 goto FINISH_OFF;
6120         }
6121
6122         EM_DEBUG_LOG("account_id VALUE [%d] ", read_mail_uid->account_id);
6123         EM_DEBUG_LOG("mailbox_id VALUE [%d] ", read_mail_uid->mailbox_id);
6124         EM_DEBUG_LOG("local_uid VALUE [%d] ", read_mail_uid->local_uid);
6125         EM_DEBUG_LOG("mailbox_name VALUE [%s] ", read_mail_uid->mailbox_name);
6126         EM_DEBUG_LOG("s_uid VALUE [%s] ", read_mail_uid->s_uid);
6127         EM_DEBUG_LOG("data1 VALUE [%d] ", read_mail_uid->data1);
6128         EM_DEBUG_LOG("data2 VALUE [%s] ", read_mail_uid->data2);
6129         EM_DEBUG_LOG("flag VALUE [%d] ", read_mail_uid->flag);
6130         EM_DEBUG_LOG("rc VALUE [%d] ", rc);
6131
6132         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->account_id);
6133         _bind_stmt_field_data_int(hStmt, LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->mailbox_id);
6134         _bind_stmt_field_data_int(hStmt, LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->local_uid);
6135         _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);
6136         _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);
6137         _bind_stmt_field_data_int(hStmt, DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->data1);
6138         _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);
6139         _bind_stmt_field_data_int(hStmt, FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flag);
6140         _bind_stmt_field_data_int(hStmt, IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, rc);
6141
6142
6143         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6144         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
6145                 ("sqlite3_step fail:%d", rc));
6146         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6147                 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
6148
6149         
6150         ret = true;
6151         
6152 FINISH_OFF:
6153         if (hStmt != NULL)  {
6154                 EM_DEBUG_LOG(" sqlite3_finalize hStmt = %p", hStmt);
6155
6156                 rc = sqlite3_finalize(hStmt);
6157                 if (rc != SQLITE_OK)  {
6158                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6159                         error = EMAIL_ERROR_DB_FAILURE;
6160                 }
6161         }
6162  
6163         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6164         _DISCONNECT_DB;
6165         
6166         if (err_code != NULL)
6167                 *err_code = error;
6168
6169         EM_DEBUG_FUNC_END("ret [%d]", ret);
6170         return ret;
6171 }
6172
6173 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)
6174 {
6175         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);
6176         
6177         if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid)  {
6178                 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);
6179                 
6180                 if (err_code != NULL)
6181                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6182                 return false;
6183         }
6184         
6185         int rc, ret = false;
6186         int error = EMAIL_ERROR_NONE;
6187         DB_STMT hStmt = NULL;
6188         char sql_query_string[QUERY_SIZE] = {0, };
6189         
6190         sqlite3 *local_db_handle = emstorage_get_db_connection();
6191
6192         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6193         
6194         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6195                 "UPDATE mail_read_mail_uid_tbl SET"
6196                 "  account_id = ?"
6197                 ", mailbox_id = ?"
6198                 ", local_uid  = ?"
6199                 ", mailbox_name = ?"
6200                 ", s_uid = ?"
6201                 ", data1 = ?"
6202                 ", data2 = ?"
6203                 ", flag  = ?"
6204                 " WHERE account_id = ?"
6205                 " AND mailbox_id  = ?"
6206                 " AND local_uid   = ?"
6207                 " AND mailbox_name= ?"
6208                 " AND s_uid = ?");
6209
6210         
6211         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
6212         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6213         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6214                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6215  
6216         
6217         int i = 0;
6218         
6219         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->account_id);
6220         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->mailbox_id);
6221         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->local_uid);
6222         _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6223         _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->s_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6224         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->data1);
6225         _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->data2, 0, DATA2_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6226         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flag);
6227         _bind_stmt_field_data_int(hStmt, i++, account_id);
6228         _bind_stmt_field_data_int(hStmt, i++, mailbox_id);
6229         _bind_stmt_field_data_int(hStmt, i++, local_uid);
6230         _bind_stmt_field_data_string(hStmt, i++, (char*)mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6231         _bind_stmt_field_data_string(hStmt, i++, (char*)uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6232  
6233
6234         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6235         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6236                 ("sqlite3_step fail:%d", rc));
6237         
6238         ret = true;
6239         
6240 FINISH_OFF:
6241         if (hStmt != NULL)  {
6242                 EM_DEBUG_LOG("sqlite3_finalize hStmt = %p", hStmt);
6243
6244                 rc = sqlite3_finalize(hStmt);
6245                 if (rc != SQLITE_OK)  {
6246                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6247                         
6248                         error = EMAIL_ERROR_DB_FAILURE;
6249                 }
6250         }
6251  
6252         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6253         _DISCONNECT_DB;
6254         
6255         if (err_code != NULL)
6256                 *err_code = error;
6257
6258         EM_DEBUG_FUNC_END("ret [%d]", ret);
6259         return ret;
6260 }
6261
6262 INTERNAL_FUNC int emstorage_remove_downloaded_mail(int account_id, char *mailbox_name, char *uid, int transaction, int *err_code)
6263 {
6264         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);
6265
6266         if (account_id < FIRST_ACCOUNT_ID)  {
6267                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%s], uid[%s]", account_id, mailbox_name, uid);
6268                 
6269                 if (err_code != NULL)
6270                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6271                 return false;
6272         }
6273         
6274         int rc, ret = false;
6275         int error = EMAIL_ERROR_NONE;
6276         char sql_query_string[QUERY_SIZE] = {0, };
6277         
6278         sqlite3 *local_db_handle = emstorage_get_db_connection();
6279
6280         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6281         
6282         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d ", account_id);
6283         
6284         if (mailbox_name) {             /*  NULL means all mailbox_name */
6285                 SNPRINTF(sql_query_string+strlen(sql_query_string), sizeof(sql_query_string) - (1 + strlen(sql_query_string)), "AND mailbox_name = '%s' ", mailbox_name);
6286         }
6287         
6288         if (uid) {              /*  NULL means all mail */
6289                 SNPRINTF(sql_query_string+strlen(sql_query_string), sizeof(sql_query_string) - (1 + strlen(sql_query_string)), "AND s_uid='%s' ", uid);
6290         }
6291         
6292         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
6293         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6294                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6295  
6296         
6297         ret = true;
6298         
6299 FINISH_OFF:
6300  
6301         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6302         _DISCONNECT_DB;
6303         
6304         if (err_code != NULL)
6305                 *err_code = error;
6306
6307         EM_DEBUG_FUNC_END("ret [%d]", ret);
6308         return ret;
6309 }
6310
6311 INTERNAL_FUNC int emstorage_free_read_mail_uid(emstorage_read_mail_uid_tbl_t** read_mail_uid, int count, int *err_code)
6312 {
6313         EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], count[%d], err_code[%p]", read_mail_uid, count, err_code);
6314         
6315         int ret = false;
6316         int error = EMAIL_ERROR_NONE;
6317         
6318         if (count > 0)  {
6319                 if (!read_mail_uid || !*read_mail_uid)  {       
6320                         EM_DEBUG_EXCEPTION(" read_mail_uid[%p], count[%d]", read_mail_uid, count);
6321                         
6322                         error = EMAIL_ERROR_INVALID_PARAM;
6323                         goto FINISH_OFF;
6324                 }
6325                 
6326                 emstorage_read_mail_uid_tbl_t* p = *read_mail_uid;
6327                 int i;
6328                 
6329                 for (i = 0; i < count; i++)  {
6330                         EM_SAFE_FREE(p[i].mailbox_name);
6331                         EM_SAFE_FREE(p[i].s_uid);
6332                         EM_SAFE_FREE(p[i].data2);
6333                 }
6334                 
6335                 EM_SAFE_FREE(p); *read_mail_uid = NULL;
6336         }
6337         
6338         ret = true;
6339         
6340 FINISH_OFF:
6341         if (err_code != NULL)
6342                 *err_code = error;
6343
6344         EM_DEBUG_FUNC_END("ret [%d]", ret);
6345         return ret;
6346 }
6347
6348 INTERNAL_FUNC int emstorage_get_rule_count(int account_id, int *count, int transaction, int *err_code)
6349 {
6350         EM_DEBUG_FUNC_BEGIN("account_id[%d], count[%p], transaction[%d], err_code[%p]", account_id, count, transaction, err_code);
6351         
6352         if (account_id != ALL_ACCOUNT || !count) {              /*  only global rule supported. */
6353                 EM_DEBUG_EXCEPTION(" account_id[%d], count[%p]", account_id, count);
6354                 
6355                 if (err_code != NULL)
6356                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6357                 return false;
6358         }
6359         
6360         int rc = -1, ret = false;
6361         int error =  EMAIL_ERROR_NONE;
6362         char sql_query_string[QUERY_SIZE] = {0, };
6363         
6364         sqlite3 *local_db_handle = emstorage_get_db_connection();
6365         EMSTORAGE_START_READ_TRANSACTION(transaction);
6366         
6367         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl WHERE account_id = %d", account_id);
6368         
6369         char **result;
6370
6371         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6372         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6373                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6374
6375         *count = atoi(result[1]);
6376         sqlite3_free_table(result);
6377         
6378         ret = true;
6379         
6380 FINISH_OFF:
6381         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6382         _DISCONNECT_DB;
6383         
6384         if (err_code != NULL)
6385                 *err_code = error;
6386
6387         EM_DEBUG_FUNC_END("ret [%d]", ret);
6388         return ret;
6389 }
6390
6391 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)
6392 {
6393         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);
6394         
6395         if (account_id != ALL_ACCOUNT || !select_num || !is_completed || !rule_list) {          /*  only global rule supported. */
6396                 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);
6397                 
6398                 if (err_code != NULL)
6399                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6400                 return false;
6401         }
6402         
6403         int ret = false;
6404         int error = EMAIL_ERROR_NONE;
6405         
6406         emstorage_rule_tbl_t* p_data_tbl = NULL;
6407         int i = 0, count = 0;
6408         DB_STMT hStmt = NULL;
6409         char sql_query_string[QUERY_SIZE] = {0, };
6410         
6411         int rc;
6412         
6413         sqlite3 *local_db_handle = emstorage_get_db_connection();
6414         EMSTORAGE_START_READ_TRANSACTION(transaction);
6415         
6416         if (type)
6417                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d AND type = %d", account_id, type);
6418         else
6419                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d ORDER BY type", account_id);
6420                 
6421         
6422         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
6423         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6424         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6425                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6426
6427
6428         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6429         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6430                 ("sqlite3_step fail:%d", rc));
6431
6432         char **result;
6433         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL); */
6434         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
6435         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6436                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6437
6438         sqlite3_free_table(result);
6439
6440         if (count == 0)  {
6441                 EM_DEBUG_LOG(" no matched rule found...");
6442                 ret = true;
6443                 goto FINISH_OFF;
6444         }
6445
6446         
6447         if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t) * count))) {
6448                 EM_DEBUG_EXCEPTION(" malloc failed...");
6449                 
6450                 error = EMAIL_ERROR_OUT_OF_MEMORY;
6451                 goto FINISH_OFF;
6452         }
6453         
6454         memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t) * count);
6455         
6456         for (i = 0; i < count; i++)  {
6457                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
6458                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
6459                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].type), TYPE_IDX_IN_MAIL_RULE_TBL);
6460                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
6461                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
6462                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
6463                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
6464                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
6465
6466                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6467                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6468                         ("sqlite3_step fail:%d", rc));
6469         }
6470         
6471         ret = true;
6472         
6473 FINISH_OFF:
6474
6475         EM_DEBUG_LOG("[%d] rules found.", count);
6476
6477         if (ret == true)  {
6478                 *rule_list = p_data_tbl;
6479                 *select_num = count;
6480         }
6481         else if (p_data_tbl != NULL)
6482                 emstorage_free_rule(&p_data_tbl, count, NULL); /* CID FIX */
6483         
6484         if (hStmt != NULL)  {
6485                 EM_DEBUG_LOG("  sqlite3_finalize hStmt = %p", hStmt);
6486
6487                 rc = sqlite3_finalize(hStmt);
6488                 if (rc != SQLITE_OK)  {
6489                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6490                         error = EMAIL_ERROR_DB_FAILURE;
6491                 }
6492         }
6493
6494         
6495         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6496         _DISCONNECT_DB;
6497         
6498         if (err_code != NULL)
6499                 *err_code = error;
6500
6501         EM_DEBUG_FUNC_END("ret [%d]", ret);
6502         return ret;
6503 }
6504
6505 INTERNAL_FUNC int emstorage_get_rule_by_id(int account_id, int rule_id, emstorage_rule_tbl_t** rule, int transaction, int *err_code)
6506 {
6507         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);
6508
6509         if (account_id != ALL_ACCOUNT || !rule)  {      
6510                 EM_DEBUG_EXCEPTION(" account_id[%d], rule_id[%d], rule[%p]", account_id, rule_id, rule);
6511                 
6512                 if (err_code != NULL)
6513                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6514                 return false;
6515         }
6516         
6517         emstorage_rule_tbl_t* p_data_tbl = NULL;
6518         int rc, ret = false;
6519         int error = EMAIL_ERROR_NONE;
6520  
6521         DB_STMT hStmt = NULL;
6522         char sql_query_string[QUERY_SIZE] = {0, };
6523         sqlite3 *local_db_handle = emstorage_get_db_connection();
6524         EMSTORAGE_START_READ_TRANSACTION(transaction);
6525         
6526         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);
6527         
6528         
6529         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
6530         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6531                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6532
6533
6534         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6535         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6536                 ("sqlite3_step fail:%d", rc));
6537
6538         if (rc == SQLITE_DONE)  {
6539                 EM_DEBUG_EXCEPTION(" no matched rule found...");
6540                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
6541                 goto FINISH_OFF;
6542         }
6543         
6544         if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t))))  {
6545                 EM_DEBUG_EXCEPTION(" malloc failed...");
6546                 error = EMAIL_ERROR_OUT_OF_MEMORY;
6547                 goto FINISH_OFF;
6548         }
6549         
6550         memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t));
6551         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
6552         _get_stmt_field_data_int(hStmt, &(p_data_tbl->rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
6553         _get_stmt_field_data_int(hStmt, &(p_data_tbl->type), TYPE_IDX_IN_MAIL_RULE_TBL);
6554         _get_stmt_field_data_string(hStmt, &(p_data_tbl->value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
6555         _get_stmt_field_data_int(hStmt, &(p_data_tbl->action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
6556         _get_stmt_field_data_int(hStmt, &(p_data_tbl->target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
6557         _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
6558         _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
6559         
6560         ret = true;
6561         
6562 FINISH_OFF:
6563         if (ret == true)
6564                 *rule = p_data_tbl;
6565         else if (p_data_tbl != NULL)
6566                 emstorage_free_rule(&p_data_tbl, 1, NULL);
6567         
6568         if (hStmt != NULL)  {
6569                 EM_DEBUG_LOG(" before sqlite3_finalize hStmt = %p", hStmt);
6570
6571                 rc = sqlite3_finalize(hStmt);
6572                 if (rc != SQLITE_OK)  {
6573                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6574                         
6575                         error = EMAIL_ERROR_DB_FAILURE;
6576                 }
6577         }
6578  
6579         
6580         
6581         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6582         _DISCONNECT_DB;
6583         
6584         if (err_code != NULL)
6585                 *err_code = error;
6586
6587         EM_DEBUG_FUNC_END("ret [%d]", ret);
6588         return ret;
6589 }
6590
6591 INTERNAL_FUNC int emstorage_change_rule(int account_id, int rule_id, emstorage_rule_tbl_t* new_rule, int transaction, int *err_code)
6592 {
6593         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);
6594
6595         if (account_id != ALL_ACCOUNT || !new_rule) {           /*  only global rule supported. */
6596                 EM_DEBUG_EXCEPTION(" account_id[%d], rule_id[%d], new_rule[%p]", account_id, rule_id, new_rule);
6597                 
6598                 if (err_code != NULL)
6599                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6600                 return false;
6601         }
6602         
6603         int rc, ret = false;
6604         int error = EMAIL_ERROR_NONE;
6605  
6606         DB_STMT hStmt = NULL;
6607         char sql_query_string[QUERY_SIZE] = {0, };
6608         sqlite3 *local_db_handle = emstorage_get_db_connection();
6609         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6610         
6611         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6612                 "UPDATE mail_rule_tbl SET"
6613                 "  type = ?"
6614                 ", value = ?"
6615                 ", action_type = ?"
6616                 ", target_mailbox_id = ?"
6617                 ", flag1 = ?"
6618                 ", flag2 = ?"
6619                 " WHERE account_id = ?"
6620                 " AND rule_id = ?");
6621  
6622         
6623         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
6624         EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
6625         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6626                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6627         
6628         int i = 0;
6629         
6630         _bind_stmt_field_data_int(hStmt, i++, new_rule->type);
6631         _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
6632         _bind_stmt_field_data_int(hStmt, i++, new_rule->action_type);
6633         _bind_stmt_field_data_int(hStmt, i++, new_rule->target_mailbox_id);
6634         _bind_stmt_field_data_int(hStmt, i++, new_rule->flag1);
6635         _bind_stmt_field_data_int(hStmt, i++, new_rule->flag2);
6636         _bind_stmt_field_data_int(hStmt, i++, account_id);
6637         _bind_stmt_field_data_int(hStmt, i++, rule_id);
6638  
6639
6640         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6641         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6642                 ("sqlite3_step fail:%d", rc));
6643         
6644         ret = true;
6645         
6646 FINISH_OFF:
6647         if (hStmt != NULL)  {
6648                 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
6649
6650                 rc = sqlite3_finalize(hStmt);
6651                 if (rc != SQLITE_OK)  {
6652                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6653                         
6654                         error = EMAIL_ERROR_DB_FAILURE;
6655                 }
6656         }
6657
6658         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6659         _DISCONNECT_DB;
6660         
6661         if (err_code != NULL)
6662                 *err_code = error;
6663
6664         EM_DEBUG_FUNC_END("ret [%d]", ret);
6665         return ret;
6666 }
6667
6668 INTERNAL_FUNC int emstorage_find_rule(emstorage_rule_tbl_t* rule, int transaction, int *err_code)
6669 {
6670         EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
6671
6672         if (!rule || rule->account_id != ALL_ACCOUNT) {         /*  only global rule supported. */
6673                 if (rule != NULL)
6674                         EM_DEBUG_EXCEPTION(" rule->account_id[%d]", rule->account_id);
6675                 
6676                 if (err_code != NULL)
6677                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6678                 return false;
6679         }
6680
6681         DB_STMT hStmt = NULL;
6682         char sql_query_string[QUERY_SIZE] = {0, };
6683         
6684         int error = EMAIL_ERROR_NONE;
6685         int rc, ret = false;
6686
6687         sqlite3 *local_db_handle = emstorage_get_db_connection();
6688         EMSTORAGE_START_READ_TRANSACTION(transaction);
6689
6690         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6691                 "SELECT rule_id FROM mail_rule_tbl WHERE type = %d AND UPPER(value) = UPPER('%s')",
6692                 rule->type, rule->value);
6693  
6694         
6695         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
6696         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6697                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6698
6699
6700         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6701         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6702                 ("sqlite3_step fail:%d", rc));
6703
6704         if (rc == SQLITE_DONE)  {
6705                 EM_DEBUG_EXCEPTION(" no matched rule found...");
6706                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
6707                 goto FINISH_OFF;
6708         }
6709
6710         
6711         ret = true;
6712
6713 FINISH_OFF:
6714         if (hStmt != NULL)  {
6715                 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
6716
6717                 rc = sqlite3_finalize(hStmt);
6718                 if (rc != SQLITE_OK)  {
6719                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
6720                         error = EMAIL_ERROR_DB_FAILURE;
6721                 }
6722         }
6723  
6724         
6725         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6726         _DISCONNECT_DB;
6727
6728         if (err_code)
6729                 *err_code = error;
6730
6731         EM_DEBUG_FUNC_END("ret [%d]", ret);
6732         return ret;
6733 }
6734
6735 INTERNAL_FUNC int emstorage_add_rule(emstorage_rule_tbl_t* rule, int transaction, int *err_code)
6736 {
6737         EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
6738
6739         if (!rule || rule->account_id != ALL_ACCOUNT)  {        /*  only global rule supported. */
6740                 if (rule != NULL)
6741                         EM_DEBUG_EXCEPTION(" rule->account_id[%d]", rule->account_id);
6742                 
6743                 if (err_code != NULL)
6744                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6745                 return false;
6746         }
6747         
6748         int rc, rc_2, ret = false;
6749         int error = EMAIL_ERROR_NONE;
6750         DB_STMT hStmt = NULL;
6751         char sql_query_string[QUERY_SIZE] = {0, };
6752
6753         
6754         sqlite3 *local_db_handle = emstorage_get_db_connection();
6755
6756         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6757         
6758         char *sql;
6759         char **result;
6760         sql = "SELECT max(rowid) FROM mail_rule_tbl;";
6761
6762         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6763         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6764                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6765         
6766         if (NULL==result[1])
6767                 rc = 1;
6768         else 
6769                 rc = atoi(result[1])+1;
6770         sqlite3_free_table(result);
6771
6772         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6773                 "INSERT INTO mail_rule_tbl VALUES "
6774                 "( ?"           /*  account id */
6775                 ", ?"           /*  rule_id */
6776                 ", ?"           /*  type */
6777                 ", ?"           /*  value */
6778                 ", ?"           /*  action_type */
6779                 ", ?"           /*  target_mailbox_id */
6780                 ", ?"           /*  flag1 */
6781                 ", ?)");        /*  flag2 */
6782  
6783         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc_2);
6784         if (rc_2 != SQLITE_OK)  {
6785                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc_2, sqlite3_errmsg(local_db_handle));
6786                 error = EMAIL_ERROR_DB_FAILURE;
6787                 goto FINISH_OFF;
6788         }
6789  
6790         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL, rule->account_id);
6791         _bind_stmt_field_data_int(hStmt, RULE_ID_IDX_IN_MAIL_RULE_TBL, rc);
6792         _bind_stmt_field_data_int(hStmt, TYPE_IDX_IN_MAIL_RULE_TBL, rule->type);
6793         _bind_stmt_field_data_string(hStmt, VALUE_IDX_IN_MAIL_RULE_TBL, (char*)rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
6794         _bind_stmt_field_data_int(hStmt, ACTION_TYPE_IDX_IN_MAIL_RULE_TBL, rule->action_type);
6795         _bind_stmt_field_data_int(hStmt, TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL, rule->target_mailbox_id);
6796         _bind_stmt_field_data_int(hStmt, FLAG1_IDX_IN_MAIL_RULE_TBL, rule->flag1);
6797         _bind_stmt_field_data_int(hStmt, FLAG2_IDX_IN_MAIL_RULE_TBL, rule->flag2);
6798  
6799         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6800         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6801                 ("sqlite3_step fail:%d", rc));
6802
6803         ret = true;
6804         
6805 FINISH_OFF:
6806         if (hStmt != NULL)  {
6807                 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
6808
6809                 rc = sqlite3_finalize(hStmt);
6810                 if (rc != SQLITE_OK) {
6811                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6812                         
6813                         error = EMAIL_ERROR_DB_FAILURE;
6814                 }
6815         }
6816
6817         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6818         _DISCONNECT_DB;
6819         
6820         if (err_code != NULL)
6821                 *err_code = error;
6822
6823         EM_DEBUG_FUNC_END("ret [%d]", ret);
6824         return ret;
6825 }
6826
6827 INTERNAL_FUNC int emstorage_delete_rule(int account_id, int rule_id, int transaction, int *err_code)
6828 {
6829         EM_DEBUG_FUNC_BEGIN("account_id[%d], rule_id[%d], transaction[%d], err_code[%p]", account_id, rule_id, transaction, err_code);
6830         
6831         if (account_id != ALL_ACCOUNT) {                /*  only global rule supported. */
6832                 EM_DEBUG_EXCEPTION(" account_id[%d], rule_id[%d]", account_id, rule_id);
6833                 
6834                 if (err_code != NULL)
6835                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6836                 return false;
6837         }
6838         
6839         int rc, ret = false;
6840         int error = EMAIL_ERROR_NONE;
6841         char sql_query_string[QUERY_SIZE] = {0, };
6842
6843         sqlite3 *local_db_handle = emstorage_get_db_connection();
6844         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6845         
6846         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);
6847         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
6848         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6849                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6850         rc = sqlite3_changes(local_db_handle);
6851         if (rc == 0) {
6852                 EM_DEBUG_EXCEPTION(" no matched rule found...");
6853                 
6854                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
6855                 goto FINISH_OFF;
6856         }
6857         
6858         ret = true;
6859         
6860 FINISH_OFF:
6861         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6862         _DISCONNECT_DB;
6863         
6864         if (err_code != NULL)
6865                 *err_code = error;
6866
6867         EM_DEBUG_FUNC_END("ret [%d]", ret);
6868         return ret;
6869 }
6870
6871 INTERNAL_FUNC int emstorage_free_rule(emstorage_rule_tbl_t** rule_list, int count, int *err_code)
6872 {
6873         EM_DEBUG_FUNC_BEGIN("rule_list[%p], conut[%d], err_code[%p]", rule_list, count, err_code);
6874
6875         int ret = false;
6876         int error = EMAIL_ERROR_NONE;
6877         
6878         if (count > 0) {
6879                 if (!rule_list || !*rule_list) {
6880                         EM_DEBUG_EXCEPTION(" rule_list[%p], conut[%d]", rule_list, count);
6881                         
6882                         error = EMAIL_ERROR_INVALID_PARAM;
6883                         goto FINISH_OFF;
6884                 }
6885                 
6886                 emstorage_rule_tbl_t* p = *rule_list;
6887                 int i = 0;
6888                 
6889                 for (; i < count; i++) {
6890                         EM_SAFE_FREE(p[i].value);
6891                 }
6892                 
6893                 EM_SAFE_FREE(p); *rule_list = NULL;
6894         }
6895         
6896 FINISH_OFF:
6897         if (err_code != NULL)
6898                 *err_code = error;
6899
6900         EM_DEBUG_FUNC_END("ret [%d]", ret);
6901         return ret;
6902 }
6903
6904 INTERNAL_FUNC int emstorage_get_mail_count(int account_id, const char *mailbox_name, int *total, int *unseen, int transaction, int *err_code)
6905 {
6906         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);
6907         
6908         if (!total && !unseen) {
6909                 EM_DEBUG_EXCEPTION(" accoun_id[%d], mailbox_name[%s], total[%p], unseen[%p]", account_id, mailbox_name, total, unseen);
6910                 if (err_code != NULL)
6911                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6912                 return false;
6913         }
6914         
6915         int rc = -1, ret = false;
6916         int error = EMAIL_ERROR_NONE;
6917         DB_STMT hStmt = NULL;
6918         char sql_query_string[QUERY_SIZE] = {0, };
6919         char err_msg[1024];
6920
6921         
6922         memset(&sql_query_string, 0x00, sizeof(sql_query_string));
6923         sqlite3 *local_db_handle = emstorage_get_db_connection();
6924         EMSTORAGE_START_READ_TRANSACTION(transaction);
6925         
6926         if (total)  {
6927                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl");
6928                 
6929                 if (account_id != ALL_ACCOUNT)  {
6930                         SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " WHERE account_id = %d", account_id);
6931                         if (mailbox_name)
6932                                 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " AND UPPER(mailbox_name) =  UPPER('%s')", mailbox_name);
6933                 }
6934                 else if (mailbox_name)
6935                         SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " WHERE UPPER(mailbox_name) =  UPPER('%s')", mailbox_name);
6936                 
6937 #ifdef USE_GET_RECORD_COUNT_API
6938                 char **result;
6939
6940                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6941                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF2; },
6942                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6943
6944                 *total = atoi(result[1]);
6945                 sqlite3_free_table(result);
6946 #else
6947                 
6948                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
6949                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF2; },
6950                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6951
6952                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6953                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF2; },
6954                         ("sqlite3_step fail:%d", rc));
6955                 _get_stmt_field_data_int(hStmt, total, 0);
6956 #endif          /*  USE_GET_RECORD_COUNT_API */
6957         }
6958         
6959         if (unseen)  {
6960                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl WHERE flags_seen_field = 0");               /*  fSEEN = 0x01 */
6961                 
6962                 if (account_id != ALL_ACCOUNT)
6963                         SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " AND account_id = %d", account_id);
6964                 
6965                 if (mailbox_name)
6966                         SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " AND mailbox_name = '%s'", mailbox_name);
6967                 else
6968                         SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " AND mailbox_type NOT IN (3, 5)");
6969                 
6970                 char **result;
6971                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6972                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6973                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6974
6975                 *unseen = atoi(result[1]);
6976                 sqlite3_free_table(result);
6977                 
6978         }
6979 FINISH_OFF:
6980         ret = true;
6981         
6982 FINISH_OFF2:
6983  
6984 #ifndef USE_PREPARED_QUERY_
6985         if (hStmt != NULL)  {
6986                 rc = sqlite3_finalize(hStmt);
6987                 if (rc != SQLITE_OK)  {
6988                         EM_DEBUG_LOG(" EDBStmtClearRow failed - %d: %s", rc, err_msg);
6989                         error = EMAIL_ERROR_DB_FAILURE;
6990                 }
6991         }
6992 #endif
6993
6994         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6995         _DISCONNECT_DB;
6996         
6997         if (err_code != NULL)
6998                 *err_code = error;
6999
7000         EM_DEBUG_FUNC_END("ret [%d]", ret);
7001         return ret;
7002 }
7003
7004 INTERNAL_FUNC int emstorage_get_mail_field_by_id(int mail_id, int type, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
7005 {
7006         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
7007         
7008         if (mail_id <= 0 || !mail)  {   
7009                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
7010                 if (err_code != NULL)
7011                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7012                 return false;
7013         }
7014         
7015         emstorage_mail_tbl_t* p_data_tbl = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
7016
7017         if (p_data_tbl == NULL)  {
7018                 EM_DEBUG_EXCEPTION("malloc failed...");
7019                 if (err_code != NULL)
7020                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
7021                 return false;
7022         }
7023         
7024         memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
7025         DB_STMT hStmt = NULL;
7026         char sql_query_string[QUERY_SIZE] = {0, };
7027
7028         int rc, ret = false;
7029         int error = EMAIL_ERROR_NONE;
7030         
7031         sqlite3 *local_db_handle = emstorage_get_db_connection();
7032         EMSTORAGE_START_READ_TRANSACTION(transaction);
7033         
7034         switch (type)  {
7035                 case RETRIEVE_SUMMARY:
7036                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7037                                 "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);
7038                         break;
7039                 
7040                 case RETRIEVE_FIELDS_FOR_DELETE:
7041                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7042                                 "SELECT account_id, mail_id, server_mail_status, server_mailbox_name, server_mail_id FROM mail_tbl WHERE mail_id = %d", mail_id);
7043                         break;                  
7044                 
7045                 case RETRIEVE_ACCOUNT:
7046                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7047                                 "SELECT account_id FROM mail_tbl WHERE mail_id = %d", mail_id);
7048                         break;
7049                 
7050                 case RETRIEVE_FLAG:
7051                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7052                                 "SELECT account_id, mailbox_name, flags_seen_field, thread_id, mailbox_id FROM mail_tbl WHERE mail_id = %d", mail_id);
7053                         break;
7054                 
7055                 default :
7056                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
7057                         error = EMAIL_ERROR_INVALID_PARAM;
7058                         goto FINISH_OFF;
7059         }
7060         
7061         EM_DEBUG_LOG("Query [%s]", sql_query_string);
7062         
7063         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
7064         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7065                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7066
7067
7068         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7069         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7070                 ("sqlite3_step fail:%d", rc));
7071
7072         if (rc == SQLITE_DONE)  {
7073                 EM_DEBUG_EXCEPTION("no matched mail found...");
7074                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7075                 goto FINISH_OFF;
7076         }
7077         switch (type)  {
7078                 case RETRIEVE_SUMMARY:
7079                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), 0);
7080                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), 1);
7081                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), 2);
7082                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->mailbox_name), 0, 3);
7083                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), 4);
7084                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, 5);
7085                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, 6);
7086                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, 7);
7087                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, 8);
7088                         _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), 9);
7089                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->save_status), 10);
7090                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), 11);
7091                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), 12);
7092                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_item_count), 13);
7093                         break;
7094                         
7095                 case RETRIEVE_FIELDS_FOR_DELETE:
7096                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), 0);
7097                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), 1);
7098                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), 2);
7099                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, 3);
7100                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, 4);
7101                         break;
7102                         
7103                 case RETRIEVE_ACCOUNT:
7104                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), 0);
7105                         break;
7106                         
7107                 case RETRIEVE_FLAG:
7108                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), 0);
7109                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->mailbox_name), 0, 1);
7110                         _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), 2);
7111                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), 3);
7112                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), 4);
7113                         break;
7114         }
7115         
7116         ret = true;
7117         
7118 FINISH_OFF:
7119         if (ret == true)
7120                 *mail = p_data_tbl;
7121         else if (p_data_tbl != NULL)
7122                 emstorage_free_mail(&p_data_tbl,  1, NULL);
7123
7124         if (hStmt != NULL)  {
7125                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
7126         
7127                 rc = sqlite3_finalize(hStmt);
7128                 if (rc != SQLITE_OK)  {
7129                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
7130                         error = EMAIL_ERROR_DB_FAILURE;
7131                 }
7132         }
7133         
7134
7135         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7136         _DISCONNECT_DB;
7137         
7138         if (err_code != NULL)
7139                 *err_code = error;
7140
7141         EM_DEBUG_FUNC_END("ret [%d]", ret);
7142         return ret;
7143 }
7144
7145 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)
7146 {
7147         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);
7148         
7149         if (number_of_mails <= 0 || !mail_ids)  {       
7150                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
7151                 if (err_code != NULL)
7152                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7153                 return false;
7154         }
7155         
7156         emstorage_mail_tbl_t* p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * number_of_mails);
7157
7158         if (p_data_tbl == NULL)  {
7159                 EM_DEBUG_EXCEPTION("malloc failed...");
7160                 if (err_code != NULL)
7161                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
7162                 return false;
7163         }
7164         
7165         DB_STMT hStmt = NULL;
7166         int ret = false;
7167         int error = EMAIL_ERROR_NONE;
7168         int i = 0, item_count = 0, rc = -1, field_count, col_index, cur_sql_query_string = 0;
7169         char **result = NULL;
7170         char sql_query_string[QUERY_SIZE] = {0, };
7171         
7172         sqlite3 *local_db_handle = emstorage_get_db_connection();
7173         EMSTORAGE_START_READ_TRANSACTION(transaction);
7174
7175         switch (type) {
7176                 case RETRIEVE_SUMMARY:
7177                         cur_sql_query_string = SNPRINTF(sql_query_string, sizeof(sql_query_string),
7178                                 "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 (");
7179                         field_count = 13;
7180                         break;
7181                 
7182                 case RETRIEVE_FIELDS_FOR_DELETE:
7183                         cur_sql_query_string = SNPRINTF(sql_query_string, sizeof(sql_query_string),
7184                                 "SELECT account_id, mail_id, server_mail_status, server_mailbox_name, server_mail_id FROM mail_tbl WHERE mail_id in (");
7185                         field_count = 5;
7186                         break;                  
7187                 
7188                 case RETRIEVE_ACCOUNT:
7189                         cur_sql_query_string = SNPRINTF(sql_query_string, sizeof(sql_query_string),
7190                                 "SELECT account_id FROM mail_tbl WHERE mail_id in (");
7191                         field_count = 1;
7192                         break;
7193                 
7194                 case RETRIEVE_FLAG:
7195                         cur_sql_query_string = SNPRINTF(sql_query_string, sizeof(sql_query_string),
7196                                 "SELECT account_id, mailbox_id, flags_seen_field, thread_id FROM mail_tbl WHERE mail_id in (");
7197                         field_count = 4;
7198                         break;
7199                 
7200                 default :
7201                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
7202                         error = EMAIL_ERROR_INVALID_PARAM;
7203                         goto FINISH_OFF;
7204         }
7205
7206         for(i = 0; i < number_of_mails; i++)            
7207                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, QUERY_SIZE, "%d,", mail_ids[i]);
7208         sql_query_string[strlen(sql_query_string) - 1] = ')';
7209         
7210         EM_DEBUG_LOG("Query [%s], Length [%d]", sql_query_string, strlen(sql_query_string));
7211
7212         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &item_count, 0, NULL), rc);
7213         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
7214                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7215
7216         if (rc == SQLITE_DONE)  {
7217                 EM_DEBUG_EXCEPTION("no matched mail found...");
7218                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7219                 goto FINISH_OFF;
7220         }
7221
7222         EM_DEBUG_LOG("item_count [%d]", item_count);
7223
7224         if(number_of_mails != item_count) {
7225                 EM_DEBUG_EXCEPTION("Can't find all emails");
7226                 /* error = EMAIL_ERROR_MAIL_NOT_FOUND;
7227                 goto FINISH_OFF; */ /* removed temporarily */
7228         }
7229
7230         col_index = field_count;
7231
7232         for(i = 0; i < item_count; i++) {
7233                 switch (type) {
7234                         case RETRIEVE_SUMMARY:
7235                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7236                                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
7237                                 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
7238                                 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
7239                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
7240                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
7241                                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
7242                                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
7243                                 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
7244                                 _get_table_field_data_int(result, &(p_data_tbl[i].save_status), col_index++);
7245                                 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
7246                                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
7247                                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
7248                                 break;
7249                                 
7250                         case RETRIEVE_FIELDS_FOR_DELETE:
7251                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7252                                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
7253                                 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
7254                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
7255                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
7256                                 break;
7257                                 
7258                         case RETRIEVE_ACCOUNT:
7259                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7260                                 break;
7261                                 
7262                         case RETRIEVE_FLAG:
7263                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7264                                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
7265                                 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
7266                                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
7267                                 break;
7268                 }
7269         }
7270
7271         sqlite3_free_table(result);
7272
7273         ret = true;
7274         
7275 FINISH_OFF:
7276         if (ret == true)
7277                 *mail = p_data_tbl;
7278         else if (p_data_tbl != NULL)
7279                 emstorage_free_mail(&p_data_tbl,  1, NULL);
7280
7281         if (hStmt != NULL) {
7282                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
7283         
7284                 rc = sqlite3_finalize(hStmt);
7285                 if (rc != SQLITE_OK) {
7286                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
7287                         error = EMAIL_ERROR_DB_FAILURE;
7288                 }
7289         }
7290
7291         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7292         _DISCONNECT_DB;
7293         
7294         if (err_code != NULL)
7295                 *err_code = error;
7296
7297         EM_DEBUG_FUNC_END("ret [%d]", ret);
7298         return ret;
7299 }
7300
7301 INTERNAL_FUNC int emstorage_get_mail_by_id(int mail_id, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
7302 {
7303         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
7304         
7305         if (mail_id <= 0 || !mail) {    
7306                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
7307                 if (err_code != NULL)
7308                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7309                 return false;
7310         }
7311         
7312         int ret = false, error = EMAIL_ERROR_NONE, count;
7313         char conditional_clause[QUERY_SIZE] = {0, };
7314         emstorage_mail_tbl_t* p_data_tbl = NULL;
7315         
7316         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
7317         EM_DEBUG_LOG("query = [%s]", conditional_clause);
7318
7319         if(!emstorage_query_mail_tbl(conditional_clause, transaction, &p_data_tbl, &count, &error)) {
7320                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
7321                 goto FINISH_OFF;
7322         }
7323
7324         ret = true;
7325         
7326 FINISH_OFF:
7327         if (ret == true)
7328                 *mail = p_data_tbl;
7329         else if (p_data_tbl != NULL)
7330                 emstorage_free_mail(&p_data_tbl, 1, &error);
7331         
7332         if (err_code != NULL)
7333                 *err_code = error;
7334
7335         EM_DEBUG_FUNC_END("ret [%d]", ret);
7336         return ret;
7337 }
7338
7339 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)
7340 {
7341         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);
7342
7343         if (!search_handle || !searched)  {     
7344                 if (err_code != NULL)
7345                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7346                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
7347                 EM_DEBUG_FUNC_END("false");
7348                 return false;
7349         }
7350         
7351         emstorage_search_filter_t* p = search;
7352         int error = EMAIL_ERROR_NONE;
7353         DB_STMT hStmt = NULL;
7354         char sql_query_string[QUERY_SIZE] = {0, };
7355         int rc, ret = false;
7356         int and = false, mail_count = 0;
7357         
7358         sqlite3 *local_db_handle = emstorage_get_db_connection();
7359         EMSTORAGE_START_READ_TRANSACTION(transaction);
7360         
7361         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl");
7362         
7363         if (account_id != ALL_ACCOUNT)  {
7364                 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " WHERE account_id = %d", account_id);
7365                 and = true;
7366         }
7367         
7368         if (mailbox_name)  {
7369                 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " %s mailbox_name = '%s'", and ? "AND" : "WHERE", mailbox_name);
7370                 and = true;
7371         }
7372         
7373         while (p)  {
7374
7375                 if (p->key_type) {
7376                         if (!strncmp(p->key_type, "subject", strlen("subject"))) {
7377                                 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " %s subject LIKE '%%%%%s%%%%'", and ? "AND" : "WHERE", p->key_value);
7378                                 and = true;
7379                         }
7380                         else if (!strncmp(p->key_type, "full_address_from", strlen("full_address_from"))) {
7381                                 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " %s full_address_from LIKE '%%%%%s%%%%'", and ? "AND" : "WHERE", p->key_value);
7382                                 and = true;
7383                         }
7384                         else if (!strncmp(p->key_type, "full_address_to", strlen("full_address_to"))) {
7385                                 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " %s full_address_to LIKE '%%%%%s%%%%'", and ? "AND" : "WHERE", p->key_value);
7386                                 and = true;
7387                         }
7388                         else if (!strncmp(p->key_type, "email_address", strlen("email_address"))) {
7389                                 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " %s email_address_sender = '%s' OR email_address_recipient = '%s'", and ? "AND" : "WHERE", p->key_value, p->key_value);
7390                                 and = true;
7391                         }
7392                         p = p->next;
7393                 }
7394         }
7395         
7396         if (sorting)
7397                 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " ORDER BY date_time");
7398
7399         EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
7400
7401         
7402         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
7403         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7404                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7405
7406
7407         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7408         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7409                 ("sqlite3_step fail:%d", rc));
7410         
7411         char **result;
7412
7413         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &mail_count, NULL, NULL), rc);
7414         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
7415                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7416
7417         sqlite3_free_table(result);
7418         
7419         ret = true;
7420         
7421 FINISH_OFF:
7422         if (ret == true)  {
7423                 *search_handle = (int)hStmt;
7424                 *searched = mail_count;
7425                 EM_DEBUG_LOG("mail_count [%d]", mail_count);
7426         }
7427         else  {
7428                 if (hStmt != NULL)  {
7429                         rc = sqlite3_finalize(hStmt);
7430                         if (rc != SQLITE_OK)  {
7431                                 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
7432                                 error = EMAIL_ERROR_DB_FAILURE;
7433                         }
7434                 }
7435                 
7436                 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7437                 _DISCONNECT_DB;
7438         }
7439         
7440         if (err_code != NULL)
7441                 *err_code = error;
7442
7443         EM_DEBUG_FUNC_END("ret [%d]", ret);
7444         return ret;
7445 }
7446
7447 INTERNAL_FUNC int emstorage_mail_search_result(int search_handle, emstorage_mail_field_type_t type, void** data, int transaction, int *err_code)
7448 {
7449         EM_DEBUG_FUNC_BEGIN("search_handle[%d], type[%d], data[%p], transaction[%d], err_code[%p]", search_handle, type, data, transaction, err_code);
7450
7451         if (search_handle < 0 || !data) {
7452                 EM_DEBUG_EXCEPTION(" search_handle[%d], type[%d], data[%p]", search_handle, type, data);
7453                 
7454                 if (err_code != NULL)
7455                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7456                 return false;
7457         }
7458         
7459         emstorage_mail_tbl_t* p_data_tbl = NULL;
7460         DB_STMT hStmt = (DB_STMT)search_handle;
7461         int rc, ret = false;
7462         int error = EMAIL_ERROR_NONE;
7463
7464         switch (type)  {
7465                 case RETRIEVE_ID:
7466                         _get_stmt_field_data_int(hStmt, (int *)data, MAIL_ID_IDX_IN_MAIL_TBL);
7467                         break;
7468                         
7469                 case RETRIEVE_ENVELOPE:
7470                 case RETRIEVE_ALL:
7471                         if (!(p_data_tbl = em_malloc(sizeof(emstorage_mail_tbl_t)))) {
7472                                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
7473                                 
7474                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7475                                 goto FINISH_OFF;
7476                         }
7477                         
7478                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
7479                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
7480                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_TBL);
7481                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->mail_size), MAIL_SIZE_IDX_IN_MAIL_TBL);
7482                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
7483                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_from), 1, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL);
7484                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_to), 1, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL);
7485                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject), 1, SUBJECT_IDX_IN_MAIL_TBL);
7486                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->body_download_status), BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL);
7487                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL);
7488                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
7489                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
7490                         _get_stmt_field_data_time_t(hStmt, &(p_data_tbl->date_time), DATETIME_IDX_IN_MAIL_TBL);
7491                         _get_stmt_field_data_char  (hStmt, &(p_data_tbl->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL);
7492                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->DRM_status), DRM_STATUS_IDX_IN_MAIL_TBL);
7493                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->priority), PRIORITY_IDX_IN_MAIL_TBL);
7494                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->save_status), SAVE_STATUS_IDX_IN_MAIL_TBL);
7495                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->lock_status), LOCK_STATUS_IDX_IN_MAIL_TBL);
7496                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->report_status), REPORT_STATUS_IDX_IN_MAIL_TBL);
7497                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
7498                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->meeting_request_status), MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL);
7499                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->message_class), MESSAGE_CLASS_IDX_IN_MAIL_TBL);
7500                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->digest_type), DIGEST_TYPE_IDX_IN_MAIL_TBL);
7501                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->smime_type), SMIME_TYPE_IDX_IN_MAIL_TBL);
7502                                 
7503                         if (type == RETRIEVE_ALL)  {
7504                                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
7505                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
7506                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_reply), 1, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL);
7507                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_cc), 1, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL);
7508                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_bcc), 1, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL);
7509                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_return), 1, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL);
7510                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->message_id), 0, MESSAGE_ID_IDX_IN_MAIL_TBL);
7511                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
7512                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
7513                                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl->attachment_count), ATTACHMENT_COUNT_IDX_IN_MAIL_TBL);
7514                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
7515                                 
7516                         }
7517                         
7518                         if (p_data_tbl->body_download_status)  {
7519                                 struct stat buf;
7520                                 
7521                                 if (p_data_tbl->file_path_html)  {
7522                                         if (stat(p_data_tbl->file_path_html, &buf) == -1)
7523                                                 p_data_tbl->body_download_status = 0;
7524                                 }
7525                                 else if (p_data_tbl->file_path_plain)  {
7526                                         if (stat(p_data_tbl->file_path_plain, &buf) == -1)
7527                                                 p_data_tbl->body_download_status = 0;
7528                                 }
7529                                 else 
7530                                         p_data_tbl->body_download_status = 0;
7531                         }
7532                         
7533                         *((emstorage_mail_tbl_t**)data) = p_data_tbl;
7534                         break;
7535                 
7536                 case RETRIEVE_SUMMARY:
7537                         if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t))))  {
7538                                 EM_DEBUG_EXCEPTION(" malloc failed...");
7539                                 
7540                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7541                                 goto FINISH_OFF;
7542                         }
7543                         
7544                         memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
7545                         
7546                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
7547                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
7548                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_TBL);
7549                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
7550                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
7551                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
7552                         
7553                         *((emstorage_mail_tbl_t**)data) = p_data_tbl;
7554                         break;
7555                 
7556                 case RETRIEVE_ADDRESS:
7557                         if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t))))  {
7558                                 EM_DEBUG_EXCEPTION(" malloc failed...");
7559                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7560                                 goto FINISH_OFF;
7561                         }
7562                         
7563                         memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
7564                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
7565                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
7566                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
7567                         *((emstorage_mail_tbl_t**)data) = p_data_tbl;
7568                         break;
7569                 
7570                 default:
7571                         break;
7572         }
7573         
7574
7575         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7576         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7577                 ("sqlite3_step fail:%d", rc));
7578         
7579         ret = true;
7580         
7581 FINISH_OFF:
7582         
7583         if (err_code != NULL)
7584                 *err_code = error;
7585
7586         EM_DEBUG_FUNC_END("ret [%d]", ret);
7587         return ret;
7588 }
7589
7590 INTERNAL_FUNC int emstorage_mail_search_end(int search_handle, int transaction, int *err_code)
7591 {
7592         EM_DEBUG_FUNC_BEGIN("search_handle[%d], transaction[%d], err_code[%p]", search_handle, transaction, err_code);
7593         
7594         int error = EMAIL_ERROR_NONE;
7595         int rc, ret = false;
7596         
7597         if (search_handle < 0)  {
7598                 EM_DEBUG_EXCEPTION(" search_handle[%d]", search_handle);
7599                 error = EMAIL_ERROR_INVALID_PARAM;
7600                 goto FINISH_OFF;
7601         }
7602
7603         DB_STMT hStmt = (DB_STMT)search_handle;
7604         
7605         EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
7606
7607         rc = sqlite3_finalize(hStmt);
7608         if (rc != SQLITE_OK)  {
7609                 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
7610                 error = EMAIL_ERROR_DB_FAILURE;
7611         }
7612  
7613         ret = true;
7614         
7615 FINISH_OFF:
7616         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7617         _DISCONNECT_DB;
7618         
7619         if (err_code != NULL)
7620                 *err_code = error;
7621
7622         EM_DEBUG_FUNC_END("ret [%d]", ret);
7623         return ret;
7624 }
7625
7626 INTERNAL_FUNC int emstorage_change_mail(int mail_id, emstorage_mail_tbl_t* mail, int transaction, int *err_code)
7627 {
7628         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
7629
7630         if (mail_id <= 0 || !mail) {
7631                 EM_DEBUG_EXCEPTION(" mail_id[%d], mail[%p]", mail_id, mail);
7632                 
7633                 if (err_code != NULL)
7634                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7635                 return false;
7636         }
7637
7638         DB_STMT hStmt = NULL;
7639         char sql_query_string[QUERY_SIZE] = {0, };
7640         int rc = -1;                            
7641         int ret = false;
7642         int error = EMAIL_ERROR_NONE;
7643         int i = 0;
7644         sqlite3 *local_db_handle = emstorage_get_db_connection();
7645         char mailbox_id_param_string[10] = {0,};
7646
7647         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
7648         
7649         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7650                 "UPDATE mail_tbl SET"
7651                 "  mail_id = ?"
7652                 ", account_id = ?"
7653                 ", mailbox_id = ?"
7654                 ", mailbox_name = ?"
7655                 ", mail_size = ?"
7656                 ", server_mail_status = ?"
7657                 ", server_mailbox_name = ?"
7658                 ", server_mail_id = ?"
7659                 ", full_address_from = ?"
7660                 ", full_address_reply = ?"  /* 10 */
7661                 ", full_address_to = ?"
7662                 ", full_address_cc = ?"
7663                 ", full_address_bcc = ?"
7664                 ", full_address_return = ?"
7665                 ", subject = ?"
7666                 ", body_download_status = ?"
7667                 ", file_path_plain = ?"
7668                 ", file_path_html = ?"
7669                 ", date_time = ?"
7670                 ", flags_seen_field      = ?"  
7671                 ", flags_deleted_field   = ?"  
7672                 ", flags_flagged_field   = ?"  
7673                 ", flags_answered_field  = ?"  
7674                 ", flags_recent_field    = ?"  
7675                 ", flags_draft_field     = ?"  
7676                 ", flags_forwarded_field = ?"  
7677                 ", DRM_status = ?"
7678                 ", priority = ?"
7679                 ", save_status = ?"
7680                 ", lock_status = ?"
7681                 ", message_id = ?"
7682                 ", report_status = ?"
7683                 ", preview_text = ?"
7684                 ", smime_type = ?"
7685                 " WHERE mail_id = %d AND account_id != 0 "
7686                 , mail_id);
7687
7688         
7689         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
7690         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7691                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7692         
7693         _bind_stmt_field_data_int   (hStmt, i++, mail->mail_id);
7694         _bind_stmt_field_data_int   (hStmt, i++, mail->account_id);
7695         _bind_stmt_field_data_int   (hStmt, i++, mail->mailbox_id);
7696         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->mailbox_name, 0, MAILBOX_LEN_IN_MAIL_TBL);
7697         _bind_stmt_field_data_int   (hStmt, i++, mail->mail_size);
7698         _bind_stmt_field_data_int   (hStmt, i++, mail->server_mail_status);
7699         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mailbox_name, 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
7700         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mail_id, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
7701         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
7702         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
7703         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
7704         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
7705         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
7706         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
7707         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
7708         _bind_stmt_field_data_int   (hStmt, i++, mail->body_download_status);
7709         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
7710         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
7711         _bind_stmt_field_data_int   (hStmt, i++, mail->date_time);
7712         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_seen_field);
7713         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_deleted_field);
7714         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_flagged_field);
7715         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_answered_field);
7716         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_recent_field);
7717         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_draft_field);
7718         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_forwarded_field);
7719         _bind_stmt_field_data_int   (hStmt, i++, mail->DRM_status);
7720         _bind_stmt_field_data_int   (hStmt, i++, mail->priority);
7721         _bind_stmt_field_data_int   (hStmt, i++, mail->save_status);
7722         _bind_stmt_field_data_int   (hStmt, i++, mail->lock_status);
7723         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->message_id, 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
7724         _bind_stmt_field_data_int   (hStmt, i++, mail->report_status);
7725         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
7726         _bind_stmt_field_data_int   (hStmt, i++, mail->smime_type);
7727
7728         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7729         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7730                 ("sqlite3_step fail:%d", rc));
7731         
7732         rc = sqlite3_changes(local_db_handle);
7733         if (rc == 0)  {
7734                 EM_DEBUG_EXCEPTION(" no matched mail found...");
7735                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7736                 goto FINISH_OFF;
7737         }
7738         SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
7739         if (!emstorage_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail->mail_id, mailbox_id_param_string, 0))
7740                 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event Failed [ NOTI_MAIL_UPDATE ] >>>> ");
7741         
7742         ret = true;
7743         
7744 FINISH_OFF:
7745         if (hStmt != NULL)  {
7746                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
7747                 rc = sqlite3_finalize(hStmt);
7748                 if (rc != SQLITE_OK)  {
7749                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
7750                         error = EMAIL_ERROR_DB_FAILURE;
7751                 }
7752         }
7753
7754         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
7755         _DISCONNECT_DB;
7756         
7757         if (err_code != NULL)
7758                 *err_code = error;
7759
7760         EM_DEBUG_FUNC_END("ret [%d]", ret);
7761         return ret;
7762 }
7763
7764 INTERNAL_FUNC int emstorage_modify_mailbox_of_mails(char *old_mailbox_name, char *new_mailbox_name, int transaction, int *err_code)
7765 {
7766         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);
7767
7768         if (!old_mailbox_name && !new_mailbox_name)  {
7769                 EM_DEBUG_EXCEPTION(" old_mailbox_name[%p], new_mailbox_name[%p]", old_mailbox_name, new_mailbox_name);
7770                 
7771                 if (err_code != NULL)
7772                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7773                 return false;
7774         }
7775         
7776         int rc = -1;
7777         int ret = false;
7778         int error = EMAIL_ERROR_NONE;
7779         char sql_query_string[QUERY_SIZE] = {0, };
7780         
7781         sqlite3 *local_db_handle = emstorage_get_db_connection();
7782
7783         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
7784
7785         EM_DEBUG_LOG("Old Mailbox Name  [ %s ] , New Mailbox name [ %s ] ", old_mailbox_name, new_mailbox_name);
7786         
7787         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);
7788  
7789         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
7790         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7791                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7792
7793         rc = sqlite3_changes(local_db_handle);
7794         if (rc == 0) {
7795                 EM_DEBUG_EXCEPTION(" no matched mail found...");
7796                 
7797                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7798                 goto FINISH_OFF;
7799         }
7800  
7801         EM_DEBUG_LOG(" Modification done in mail_read_mail_uid_tbl based on Mailbox name ");
7802         /* Modify the mailbox_name name in mail_read_mail_uid_tbl table */
7803         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);
7804
7805         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
7806         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7807                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7808
7809         rc = sqlite3_changes(local_db_handle);
7810         if (rc == 0)  {
7811                 EM_DEBUG_EXCEPTION(" no matched mail found...");
7812                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7813                 goto FINISH_OFF;
7814         }
7815         
7816         if (!emstorage_notify_storage_event(NOTI_MAILBOX_UPDATE, 1, 0, new_mailbox_name, 0))
7817                 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event[ NOTI_MAILBOX_UPDATE] : Notification Failed >>> ");
7818         
7819         ret = true;
7820         
7821 FINISH_OFF:
7822
7823         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
7824         _DISCONNECT_DB;
7825         
7826         if (err_code != NULL)
7827                 *err_code = error;
7828
7829         EM_DEBUG_FUNC_END("ret [%d]", ret);
7830         return ret;
7831 }
7832
7833 /**
7834   *  emstorage_clean_save_status(int save_status, int  *err_code) - set the all mail status to the set value
7835   *
7836   *
7837   **/
7838 INTERNAL_FUNC int emstorage_clean_save_status(int save_status, int  *err_code)
7839 {
7840         EM_DEBUG_FUNC_BEGIN("save_status[%d], err_code[%p]", save_status, err_code);
7841
7842         EM_IF_NULL_RETURN_VALUE(err_code, false);
7843
7844         int ret = false;
7845         int error = EMAIL_ERROR_NONE;
7846         int rc = 0;
7847         char sql_query_string[QUERY_SIZE] = {0, };
7848         sqlite3 *local_db_handle = emstorage_get_db_connection();
7849
7850         memset(sql_query_string, 0x00, sizeof(sql_query_string));
7851         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);
7852         EM_DEBUG_LOG("Query [%s]", sql_query_string);
7853
7854         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
7855         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7856                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7857
7858         rc = sqlite3_changes(local_db_handle);
7859         if (rc == 0) {
7860                 EM_DEBUG_LOG(" No Matched Mail Exists ");
7861                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7862         }
7863         
7864         ret = true;
7865
7866 FINISH_OFF:
7867         
7868         if (err_code != NULL)
7869                 *err_code = error;
7870
7871         EM_DEBUG_FUNC_END("ret [%d]", ret);
7872         return ret;
7873 }
7874
7875 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)
7876 {
7877         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);
7878         int i = 0;
7879         int error = EMAIL_ERROR_NONE;
7880         int rc = 0;
7881         int ret = false;
7882         int cur_mail_id_string = 0;
7883         int mail_id_string_buffer_length = 0;
7884         char  sql_query_string[QUERY_SIZE] = {0, };
7885         char *mail_id_string_buffer = NULL;
7886         char *parameter_string = NULL;
7887         sqlite3 *local_db_handle = emstorage_get_db_connection();
7888         email_mail_attribute_type target_mail_attribute_type = 0;
7889         
7890         if (!mail_ids  || !field_name || account_id == 0) {
7891                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
7892                 if (err_code != NULL)
7893                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7894                 return false;
7895         }
7896
7897         if( (error = _get_attribute_type_by_mail_field_name(field_name, &target_mail_attribute_type)) != EMAIL_ERROR_NONE) {
7898                 EM_DEBUG_EXCEPTION("emstorage_get_attribute_type_by_mail_field_name failed [%d]", error);
7899                 if (err_code != NULL)
7900                         *err_code = error;
7901                 return false;
7902         }
7903
7904         /* Generating mail id list string */
7905         mail_id_string_buffer_length = MAIL_ID_STRING_LENGTH * mail_ids_count;
7906
7907         mail_id_string_buffer = em_malloc(mail_id_string_buffer_length);
7908
7909         if(!mail_id_string_buffer) {
7910                 EM_DEBUG_EXCEPTION("em_malloc failed");
7911                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7912                 goto FINISH_OFF;
7913         }
7914         
7915         for(i = 0; i < mail_ids_count; i++) 
7916                 cur_mail_id_string += SNPRINTF_OFFSET(mail_id_string_buffer, cur_mail_id_string, mail_id_string_buffer_length, "%d,", mail_ids[i]);
7917
7918         if(strlen(mail_id_string_buffer) > 1)
7919                 mail_id_string_buffer[strlen(mail_id_string_buffer) - 1] = NULL_CHAR;
7920
7921         /* Generating notification parameter string */
7922         parameter_string = em_malloc(mail_id_string_buffer_length + strlen(field_name) + 2);
7923
7924         if(!parameter_string) {
7925                 EM_DEBUG_EXCEPTION("em_malloc failed");
7926                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7927                 goto FINISH_OFF;
7928         }
7929
7930         SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", field_name, 0x01, mail_id_string_buffer);
7931
7932         /* Write query string */
7933         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);
7934
7935         EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
7936
7937         /* Execute query */
7938         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
7939         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
7940         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7941                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7942         if (sqlite3_changes(local_db_handle) == 0) 
7943                 EM_DEBUG_LOG("no mail matched...");
7944
7945         ret = true;
7946
7947 FINISH_OFF:
7948         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
7949         _DISCONNECT_DB;
7950         
7951         if (ret && parameter_string && !emstorage_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, target_mail_attribute_type, parameter_string, value))
7952                 EM_DEBUG_EXCEPTION("emstorage_notify_storage_event failed : NOTI_MAIL_FIELD_UPDATE [%s,%d]", field_name, value);
7953         
7954         EM_SAFE_FREE(mail_id_string_buffer);
7955         EM_SAFE_FREE(parameter_string);
7956
7957         if (err_code != NULL)
7958                 *err_code = error;
7959
7960         EM_DEBUG_FUNC_END("error [%d]", error);
7961         return ret;
7962 }
7963
7964 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)
7965 {
7966         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
7967         
7968         if (mail_id <= 0 || !mail)  {
7969                 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], mail[%p]", mail_id, type, mail);
7970                 if (err_code != NULL)
7971                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7972                 return false;
7973         }
7974         
7975         int ret = false;
7976         int error = EMAIL_ERROR_NONE;
7977         int move_flag = 0;
7978         DB_STMT hStmt = NULL;
7979         char sql_query_string[QUERY_SIZE] = {0, };
7980
7981         int rc = 0;
7982         int i = 0;
7983
7984         char *mailbox_name = NULL;
7985         char mailbox_id_param_string[10] = {0,};
7986
7987         sqlite3 *local_db_handle = emstorage_get_db_connection();
7988         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
7989         
7990         switch (type) {
7991                 case APPEND_BODY:
7992                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7993                                 "UPDATE mail_tbl SET"
7994                                 "  body_download_status = ?"
7995                                 ", file_path_plain = ?"
7996                                 ", file_path_html = ?"
7997                                 ", flags_seen_field      = ?"  
7998                                 ", flags_deleted_field   = ?"  
7999                                 ", flags_flagged_field   = ?"  
8000                                 ", flags_answered_field  = ?"  
8001                                 ", flags_recent_field    = ?"  
8002                                 ", flags_draft_field     = ?"  
8003                                 ", flags_forwarded_field = ?"  
8004                                 ", DRM_status = ?"
8005                                 ", attachment_count = ?"
8006                                 ", preview_text= ?"
8007                                 ", meeting_request_status = ? "
8008                                 ", message_class = ? "
8009                                 ", digest_type = ? "
8010                                 ", smime_type = ? "
8011                                 " WHERE mail_id = %d AND account_id != 0"
8012                                 , mail_id);
8013
8014                         
8015                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
8016                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8017                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8018                          i = 0;
8019                         
8020                         _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8021                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8022                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8023                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_seen_field);
8024                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_deleted_field);
8025                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_flagged_field);
8026                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_answered_field);
8027                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_recent_field);
8028                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_draft_field);
8029                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_forwarded_field);
8030                         _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
8031                         _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
8032                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
8033                         _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
8034                         _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
8035                         _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
8036                         _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
8037                         break;
8038                         
8039                 case UPDATE_MAILBOX: {
8040                                 int err;
8041                                 emstorage_mailbox_tbl_t *mailbox_tbl;
8042                         
8043                                 if (!emstorage_get_mailbox_by_name(mail->account_id, -1, mail->mailbox_name, &mailbox_tbl, false, &err)) {
8044                                         EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_name failed - %d", err);
8045                         
8046                                         goto FINISH_OFF;
8047                                 }
8048
8049                                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8050                                         "UPDATE mail_tbl SET"
8051                                         " mailbox_id = '%d'"
8052                                         " mailbox_name = '%s'"
8053                                         ",mailbox_type = '%d'"
8054                                         " WHERE mail_id = %d AND account_id != 0"
8055                                         , mailbox_tbl->mailbox_id
8056                                         , mail->mailbox_name ? mail->mailbox_name : ""
8057                                         , mailbox_tbl->mailbox_type
8058                                         , mail_id);
8059                                         move_flag = 1;
8060
8061
8062                                 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8063                                 
8064                                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
8065                                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8066                                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8067                         }
8068                         break;
8069                         
8070                 case UPDATE_FLAG:
8071                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8072                                 "UPDATE mail_tbl SET"
8073                                 " flags_seen_field      = %d"
8074                                 ",flags_deleted_field   = %d"
8075                                 ",flags_flagged_field   = %d"
8076                                 ",flags_answered_field  = %d"
8077                                 ",flags_recent_field    = %d"
8078                                 ",flags_draft_field     = %d"
8079                                 ",flags_forwarded_field = %d"
8080                                 "  WHERE mail_id = %d AND account_id != 0"
8081                                 , mail->flags_seen_field
8082                                 , mail->flags_deleted_field
8083                                 , mail->flags_flagged_field
8084                                 , mail->flags_answered_field
8085                                 , mail->flags_recent_field
8086                                 , mail->flags_draft_field
8087                                 , mail->flags_forwarded_field
8088                                 , mail_id);
8089                         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8090  
8091                         
8092                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
8093                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8094                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8095
8096                         break;
8097                         
8098                 case UPDATE_EXTRA_FLAG:
8099                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8100                                 "UPDATE mail_tbl SET"
8101                                 "  priority = %d"
8102                                 ", save_status = %d"
8103                                 ", lock_status = %d"
8104                                 ", report_status = %d"
8105                                 ", DRM_status = %d"
8106                                 " WHERE mail_id = %d AND account_id != 0"
8107                                 , mail->priority
8108                                 , mail->save_status
8109                                 , mail->lock_status
8110                                 , mail->report_status
8111                                 , mail->DRM_status
8112                                 , mail_id);
8113                         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8114  
8115                         
8116                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
8117                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8118                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8119                         break;
8120
8121                 case UPDATE_STICKY_EXTRA_FLAG:
8122                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8123                                 "UPDATE mail_tbl SET"
8124                                 "  lock_status = %d"
8125                                 "  WHERE mail_id = %d AND account_id != 0"
8126                                 , mail->lock_status
8127                                 , mail_id);
8128                         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8129  
8130                         
8131                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
8132                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8133                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8134                         break;
8135                         
8136                 case UPDATE_MAIL:
8137                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8138                                 "UPDATE mail_tbl SET"
8139                                 "  full_address_from = ?"
8140                                 ", full_address_reply = ?"
8141                                 ", full_address_to = ?"
8142                                 ", full_address_cc = ?"
8143                                 ", full_address_bcc = ?"
8144                                 ", full_address_return = ?"
8145                                 ", subject = ?"
8146                                 ", file_path_plain = ?"
8147                                 ", date_time = ?"
8148                                 ", flags_seen_field = ?"
8149                                 ", flags_deleted_field = ?"
8150                                 ", flags_flagged_field = ?"
8151                                 ", flags_answered_field = ?"
8152                                 ", flags_recent_field = ?"
8153                                 ", flags_draft_field = ?"
8154                                 ", flags_forwarded_field = ?"
8155                                 ", priority = ?"
8156                                 ", save_status = ?"
8157                                 ", lock_status = ?"
8158                                 ", report_status = ?"
8159                                 ", DRM_status = ?"
8160                                 ", file_path_html = ?"
8161                                 ", mail_size = ?"
8162                                 ", preview_text = ?"
8163                                 ", body_download_status = ?"
8164                                 ", attachment_count = ?"
8165                                 ", inline_content_count = ?"
8166                                 ", meeting_request_status = ?"
8167                                 ", message_class = ?"
8168                                 ", digest_type = ?"
8169                                 ", smime_type = ?"
8170                                 " WHERE mail_id = %d AND account_id != 0"
8171                                 , mail_id);
8172  
8173                         
8174                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
8175                         EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
8176                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8177                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8178                         i = 0;  
8179                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
8180                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
8181                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
8182                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
8183                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
8184                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
8185                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8186                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8187                         _bind_stmt_field_data_time_t(hStmt, i++, mail->date_time);
8188                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_seen_field);
8189                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_deleted_field);
8190                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_flagged_field);
8191                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_answered_field);
8192                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_recent_field);
8193                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_draft_field);
8194                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_forwarded_field);
8195                         _bind_stmt_field_data_int   (hStmt, i++, mail->priority);
8196                         _bind_stmt_field_data_int   (hStmt, i++, mail->save_status);
8197                         _bind_stmt_field_data_int   (hStmt, i++, mail->lock_status);
8198                         _bind_stmt_field_data_int   (hStmt, i++, mail->report_status);
8199                         _bind_stmt_field_data_int   (hStmt, i++, mail->DRM_status);
8200                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8201                         _bind_stmt_field_data_int   (hStmt, i++, mail->mail_size);
8202                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8203                         _bind_stmt_field_data_int   (hStmt, i++, mail->body_download_status);
8204                         _bind_stmt_field_data_int   (hStmt, i++, mail->attachment_count);
8205                         _bind_stmt_field_data_int   (hStmt, i++, mail->inline_content_count);
8206                         _bind_stmt_field_data_int   (hStmt, i++, mail->meeting_request_status);
8207                         _bind_stmt_field_data_int   (hStmt, i++, mail->message_class);
8208                         _bind_stmt_field_data_int   (hStmt, i++, mail->digest_type);
8209                         _bind_stmt_field_data_int   (hStmt, i++, mail->smime_type);
8210                         break;
8211                         
8212                 case UPDATE_DATETIME:  {
8213                         time_t now = time(NULL);
8214
8215                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8216                                 "UPDATE mail_tbl SET"
8217                                 " date_time = '%d'"
8218                                 " WHERE mail_id = %d AND account_id != 0"
8219                                 , (int)now
8220                                 , mail_id);
8221
8222                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
8223                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8224                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8225                         break;
8226                 }
8227                         
8228                 case UPDATE_FROM_CONTACT_INFO:
8229                         EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_FROM_CONTACT_INFO");
8230                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8231                                 "UPDATE mail_tbl SET"
8232                                 " email_address_sender = ?,"
8233                                 " WHERE mail_id = %d",
8234                                 mail_id);
8235  
8236                         hStmt = NULL;
8237                         
8238                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
8239                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8240                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8241                         i = 0;
8242                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8243                         break;
8244                         
8245                 case UPDATE_TO_CONTACT_INFO:
8246                         EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_TO_CONTACT_INFO");
8247                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8248                                 "UPDATE mail_tbl SET"
8249                                 " email_address_recipient = ?,"
8250                                 " WHERE mail_id = %d",
8251                                 mail_id);
8252  
8253                         hStmt = NULL;
8254                         
8255                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
8256                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8257                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8258                         i = 0;
8259                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8260                         break;
8261
8262                         case UPDATE_ALL_CONTACT_INFO:
8263                                 EM_DEBUG_LOG("emstorage_change_mail_field - mail change type is UPDATE_ALL_CONTACT_INFO");
8264                                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8265                                         "UPDATE mail_tbl SET"
8266                                         " email_address_sender = ?,"
8267                                         " email_address_recipient = ?,"
8268                                         " WHERE mail_id = %d",
8269                                         mail_id);
8270
8271                                 hStmt = NULL;
8272                                 
8273                                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
8274                                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8275                                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8276                                 i = 0;
8277                                 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8278                                 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8279                                 break;
8280
8281                 
8282 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
8283                         case UPDATE_PARTIAL_BODY_DOWNLOAD:
8284
8285                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8286                         "UPDATE mail_tbl SET"
8287                         "  body_download_status = ?"
8288                         ", file_path_plain = ?"
8289                         ", file_path_html = ?"
8290                         ", attachment_count = ?"
8291                         ", inline_content_count = ?"
8292                         ", preview_text= ?"
8293                         " WHERE mail_id = %d"
8294                         , mail_id);
8295
8296                         
8297                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
8298                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8299                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8300                          i = 0;
8301                         
8302                         _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8303                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8304                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html,  0, TEXT_2_LEN_IN_MAIL_TBL);
8305                         _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
8306                         _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
8307                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->preview_text,    0, PREVIEWBODY_LEN_IN_MAIL_TBL);
8308         
8309                         break;
8310
8311 #endif
8312                 
8313                 default:
8314                         EM_DEBUG_LOG(" type[%d]", type);
8315                         
8316                         error = EMAIL_ERROR_INVALID_PARAM;
8317                         goto FINISH_OFF;
8318         }
8319  
8320         if (hStmt != NULL)  {
8321
8322         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8323                 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
8324                 ("sqlite3_step fail:%d", rc));
8325                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8326                         ("sqlite3_step fail:%d", rc));
8327                 rc = sqlite3_changes(local_db_handle);
8328                 if (rc == 0)  {
8329                         EM_DEBUG_EXCEPTION(" no matched mail found...");
8330         
8331                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
8332                         goto FINISH_OFF;
8333                 }
8334         }       
8335
8336         if (mail->account_id == 0) {
8337                 emstorage_mail_tbl_t* mail_for_account_tbl = NULL;
8338                 if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_ACCOUNT, &mail_for_account_tbl, true, &error) || !mail_for_account_tbl) {
8339                         EM_DEBUG_EXCEPTION(" emstorage_get_mail_field_by_id failed - %d", error);
8340 /*               */
8341                         goto FINISH_OFF;
8342                 }
8343                 mail->account_id = mail_for_account_tbl->account_id;
8344                 if (mail_for_account_tbl)
8345                         emstorage_free_mail(&mail_for_account_tbl, 1, NULL);
8346         }
8347
8348         ret = true;
8349         
8350 FINISH_OFF:
8351
8352         if (hStmt != NULL)  {
8353                 rc = sqlite3_finalize(hStmt);
8354                 if (rc != SQLITE_OK)  {
8355                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
8356                         error = EMAIL_ERROR_DB_FAILURE;
8357                 }
8358                 hStmt = NULL;
8359         }
8360
8361         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8362         _DISCONNECT_DB;
8363
8364         /*h.gahlaut@samsung.com: Moving publication of notification after commiting transaction to DB */
8365         
8366         if (ret == true &&  move_flag != 1) {
8367                 if (!emstorage_get_mailbox_name_by_mailbox_type(mail->account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &mailbox_name, false, &error))
8368                         EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_name_by_mailbox_type failed - %d", error);
8369
8370                 if (mail->mailbox_name && mailbox_name) {
8371                         if (strcmp(mail->mailbox_name, mailbox_name) != 0) {
8372                                 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
8373                                 if (!emstorage_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, mailbox_id_param_string, type))
8374                                         EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event Failed [ NOTI_MAIL_UPDATE ] >>>> ");
8375                         }
8376                 }
8377                 else {
8378                         /* h.gahlaut@samsung.com: Jan 10, 2011 Publishing noti to refresh outbox when email sending status changes */
8379                         if (!emstorage_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, NULL, type))
8380                                 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event Failed [ NOTI_MAIL_UPDATE ] ");
8381                 }
8382         }
8383
8384         EM_SAFE_FREE(mailbox_name);
8385         
8386         if (err_code != NULL)
8387                 *err_code = error;
8388
8389         EM_DEBUG_FUNC_END("ret [%d]", ret);
8390         return ret;
8391 }
8392 INTERNAL_FUNC int emstorage_increase_mail_id(int *mail_id, int transaction, int *err_code)
8393 {
8394         EM_DEBUG_FUNC_BEGIN("mail_id[%p], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
8395         
8396         int rc, ret = false;
8397         int error = EMAIL_ERROR_NONE;
8398         int latest_mail_id = 0;
8399         sqlite3 *local_db_handle = NULL;
8400         char *sql = "SELECT MAX(mail_id) FROM mail_tbl;";
8401         char **result = NULL;
8402
8403 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
8404         _timedlock_shm_mutex(&mapped_for_generating_mail_id, 2);
8405 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
8406
8407         ret = vconf_get_int(VCONF_KEY_LATEST_MAIL_ID, &latest_mail_id);
8408         if (ret < 0 || latest_mail_id == 0) {
8409                 EM_DEBUG_LOG("vconf_get_int() failed [%d] or latest_mail_id is zero", ret);
8410
8411                 local_db_handle = emstorage_get_db_connection();
8412
8413                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
8414                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
8415                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
8416                 if (NULL == result[1])
8417                         rc = 1;
8418                 else 
8419                         rc = atoi(result[1]) + 1;
8420
8421                 sqlite3_free_table(result);
8422                 latest_mail_id = rc; 
8423         }
8424
8425         latest_mail_id++;
8426
8427         ret = vconf_set_int(VCONF_KEY_LATEST_MAIL_ID, latest_mail_id);
8428
8429         if (mail_id)
8430                 *mail_id = latest_mail_id;
8431
8432 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
8433         _unlockshm_mutex(&mapped_for_generating_mail_id);
8434 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
8435
8436         ret = true;
8437         
8438 FINISH_OFF:
8439         _DISCONNECT_DB;
8440         
8441         if (err_code != NULL)
8442                 *err_code = error;
8443         
8444         EM_DEBUG_FUNC_END("ret [%d]", ret);
8445         return ret;
8446 }
8447
8448
8449 INTERNAL_FUNC int emstorage_add_mail(emstorage_mail_tbl_t *mail_tbl_data, int get_id, int transaction, int *err_code)
8450 {
8451         EM_PROFILE_BEGIN(profile_emstorage_add_mail);
8452         EM_DEBUG_FUNC_BEGIN("mail_tbl_data[%p], get_id[%d], transaction[%d], err_code[%p]", mail_tbl_data, get_id, transaction, err_code);
8453         
8454         if (!mail_tbl_data)  {
8455                 EM_DEBUG_EXCEPTION("mail_tbl_data[%p], get_id[%d]", mail_tbl_data, get_id);
8456                 if (err_code != NULL)
8457                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8458                 return false;
8459         }
8460         
8461         int rc, ret = false;
8462         int error = EMAIL_ERROR_NONE;
8463         char sql_query_string[QUERY_SIZE] = {0, };
8464         DB_STMT hStmt = NULL;
8465         sqlite3 *local_db_handle = emstorage_get_db_connection();
8466
8467         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8468         
8469         if (get_id)  {
8470                 /*  increase unique id */
8471                 char *sql = "SELECT max(rowid) FROM mail_tbl;";
8472                 char **result;
8473
8474                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
8475                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
8476                         ("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle)));
8477
8478                 if (NULL == result[1])
8479                         rc = 1;
8480                 else 
8481                         rc = atoi(result[1])+1;
8482
8483                 sqlite3_free_table(result);
8484
8485                 mail_tbl_data->mail_id   = rc;
8486                 mail_tbl_data->thread_id = rc;
8487         }
8488
8489         if (mail_tbl_data->date_time == 0)
8490                 mail_tbl_data->date_time = time(NULL);
8491
8492         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8493                 "INSERT INTO mail_tbl VALUES "
8494                 "( ?" /*  mail_id */
8495                 ", ?" /*  account_id */
8496                 ", ?" /*  mailbox_id */
8497                 ", ?" /*  mailbox_name */
8498                 ", ?" /*  mailbox_type */
8499                 ", ?" /*  subject */
8500                 ", ?" /*  date_time */
8501                 ", ?" /*  server_mail_status */
8502                 ", ?" /*  server_mailbox_name */
8503                 ", ?" /*  server_mail_id */
8504                 ", ?" /*  message_id */
8505                 ", ?" /*  full_address_from */
8506                 ", ?" /*  full_address_reply */
8507                 ", ?" /*  full_address_to */
8508                 ", ?" /*  full_address_cc */
8509                 ", ?" /*  full_address_bcc */
8510                 ", ?" /*  full_address_return */
8511                 ", ?" /*  email_address_sender */
8512                 ", ?" /*  email_address_recipient */
8513                 ", ?" /*  alias_sender */
8514                 ", ?" /*  alias_recipient */
8515                 ", ?" /*  body_download_status */
8516                 ", ?" /*  file_path_plain */
8517                 ", ?" /*  file_path_html */
8518                 ", ?" /*  file_path_mime_entity */
8519                 ", ?" /*  mail_size */
8520                 ", ?" /*  flags_seen_field */
8521                 ", ?" /*  flags_deleted_field */
8522                 ", ?" /*  flags_flagged_field */
8523                 ", ?" /*  flags_answered_field */
8524                 ", ?" /*  flags_recent_field */
8525                 ", ?" /*  flags_draft_field */
8526                 ", ?" /*  flags_forwarded_field */
8527                 ", ?" /*  DRM_status */
8528                 ", ?" /*  priority */
8529                 ", ?" /*  save_status */
8530                 ", ?" /*  lock_status */
8531                 ", ?" /*  report_status */
8532                 ", ?" /*  attachment_count */
8533                 ", ?" /*  inline_content_count */
8534                 ", ?" /*  thread_id */
8535                 ", ?" /*  thread_item_count */
8536                 ", ?" /*  preview_text */
8537                 ", ?" /*  meeting_request_status */
8538                 ", ?" /*  message_class */
8539                 ", ?" /*  digest_type */
8540                 ", ?" /*  smime_type */
8541                 ")");
8542         
8543         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
8544         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8545                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8546
8547         _bind_stmt_field_data_int   (hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mail_id);
8548         _bind_stmt_field_data_int   (hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, mail_tbl_data->account_id);
8549         _bind_stmt_field_data_int   (hStmt, MAILBOX_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_id);
8550         _bind_stmt_field_data_string(hStmt, MAILBOX_NAME_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->mailbox_name, 0, MAILBOX_LEN_IN_MAIL_TBL);
8551         _bind_stmt_field_data_int   (hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_type);
8552         _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8553         _bind_stmt_field_data_int   (hStmt, DATETIME_IDX_IN_MAIL_TBL, mail_tbl_data->date_time);
8554         _bind_stmt_field_data_int   (hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->server_mail_status);
8555         _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);
8556         _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);
8557         _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);
8558         _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);
8559         _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);
8560         _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);
8561         _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);
8562         _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);
8563         _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);
8564         _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);
8565         _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);
8566         _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);
8567         _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);
8568         _bind_stmt_field_data_int   (hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->body_download_status);
8569         _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);
8570         _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);
8571         _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);
8572         _bind_stmt_field_data_int   (hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, mail_tbl_data->mail_size);
8573         _bind_stmt_field_data_int   (hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_seen_field);
8574         _bind_stmt_field_data_int   (hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_deleted_field);
8575         _bind_stmt_field_data_int   (hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_flagged_field);
8576         _bind_stmt_field_data_int   (hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_answered_field);
8577         _bind_stmt_field_data_int   (hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_recent_field);
8578         _bind_stmt_field_data_int   (hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_draft_field);
8579         _bind_stmt_field_data_int   (hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_forwarded_field);
8580         _bind_stmt_field_data_int   (hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->DRM_status);
8581         _bind_stmt_field_data_int   (hStmt, PRIORITY_IDX_IN_MAIL_TBL, mail_tbl_data->priority);
8582         _bind_stmt_field_data_int   (hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->save_status);
8583         _bind_stmt_field_data_int   (hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->lock_status);
8584         _bind_stmt_field_data_int   (hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->report_status);
8585         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->attachment_count);
8586         _bind_stmt_field_data_int   (hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->inline_content_count);
8587         _bind_stmt_field_data_int   (hStmt, THREAD_ID_IDX_IN_MAIL_TBL, mail_tbl_data->thread_id);
8588         _bind_stmt_field_data_int   (hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->thread_item_count);
8589         _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8590         _bind_stmt_field_data_int   (hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->meeting_request_status);
8591         _bind_stmt_field_data_int   (hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, mail_tbl_data->message_class);
8592         _bind_stmt_field_data_int   (hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->digest_type);
8593         _bind_stmt_field_data_int   (hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->smime_type);
8594
8595         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8596         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
8597         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
8598         ret = true;
8599         
8600 FINISH_OFF:
8601
8602         if (hStmt != NULL)  {
8603                 rc = sqlite3_finalize(hStmt);
8604                 if (rc != SQLITE_OK)  {
8605                         EM_DEBUG_LOG("sqlite3_finalize failed [%d]", rc);
8606                         error = EMAIL_ERROR_DB_FAILURE;
8607                 }
8608         }
8609
8610         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8611         _DISCONNECT_DB;
8612         
8613
8614         if (err_code != NULL)
8615                 *err_code = error;
8616
8617         EM_PROFILE_END(profile_emstorage_add_mail);
8618         EM_DEBUG_FUNC_END("ret [%d]", ret);
8619         return ret;
8620 }
8621
8622 INTERNAL_FUNC int emstorage_move_multiple_mails(int account_id, int input_mailbox_id, int mail_ids[], int number_of_mails, int transaction, int *err_code)
8623 {
8624         EM_DEBUG_FUNC_BEGIN("account_id [%d], input_mailbox_id [%d], mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", account_id, input_mailbox_id, mail_ids, number_of_mails, transaction, err_code);
8625
8626         int rc, ret = false, i, cur_conditional_clause = 0;
8627         int error = EMAIL_ERROR_NONE;
8628         char sql_query_string[QUERY_SIZE] = {0, }, conditional_clause[QUERY_SIZE] = {0, };
8629         emstorage_mailbox_tbl_t *result_mailbox = NULL;
8630         email_mailbox_type_e target_mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED;
8631         char* target_mailbox_name = NULL;
8632
8633         if (!mail_ids || input_mailbox_id <= 0) {
8634                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8635                 if (err_code != NULL)
8636                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8637                 return false;
8638         }               
8639         
8640         sqlite3 *local_db_handle = emstorage_get_db_connection();
8641
8642         if ((error = emstorage_get_mailbox_by_id(input_mailbox_id, &result_mailbox)) != EMAIL_ERROR_NONE || !result_mailbox) {
8643                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", error);
8644                 goto FINISH_OFF;
8645         }
8646
8647         if(result_mailbox) {
8648                 target_mailbox_name = EM_SAFE_STRDUP(result_mailbox->mailbox_name);
8649                 target_mailbox_type = result_mailbox->mailbox_type;
8650                 emstorage_free_mailbox(&result_mailbox, 1, NULL);
8651         }
8652         
8653         cur_conditional_clause = SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id in (");
8654
8655         for(i = 0; i < number_of_mails; i++)            
8656                 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, QUERY_SIZE, "%d,", mail_ids[i]);
8657
8658         conditional_clause[strlen(conditional_clause) - 1] = ')';
8659
8660         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8661
8662         /* Updating a mail_tbl */
8663
8664         memset(sql_query_string, 0x00, QUERY_SIZE);
8665         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_tbl SET mailbox_name = '%s', mailbox_type = %d, mailbox_id = %d %s", target_mailbox_name, target_mailbox_type, input_mailbox_id, conditional_clause);
8666         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8667
8668         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8669         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
8670                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8671
8672         /* Updating a mail_attachment_tbl */
8673
8674         memset(sql_query_string, 0x00, QUERY_SIZE);
8675         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_attachment_tbl SET mailbox_id = '%d' %s", input_mailbox_id, conditional_clause);
8676         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8677
8678         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8679         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
8680                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8681
8682         /* Updating a mail_meeting_tbl */
8683         memset(sql_query_string, 0x00, QUERY_SIZE);
8684         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_meeting_tbl SET mailbox_id = %d %s", input_mailbox_id, conditional_clause);
8685         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8686
8687         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8688         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
8689                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8690
8691         /* Updating a mail_read_mail_uid_tbl */
8692         memset(conditional_clause, 0x00, QUERY_SIZE);
8693         cur_conditional_clause = SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE local_uid in (");
8694         
8695         for(i = 0; i < number_of_mails; i++)            
8696                 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, QUERY_SIZE, "%d,", mail_ids[i]);
8697
8698         conditional_clause[strlen(conditional_clause) - 1] = ')';
8699
8700         memset(sql_query_string, 0x00, QUERY_SIZE);
8701         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_read_mail_uid_tbl SET mailbox_name = '%s', mailbox_id = %d %s", target_mailbox_name, input_mailbox_id, conditional_clause);
8702         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8703
8704         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8705         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
8706                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8707
8708         ret = true;
8709         
8710 FINISH_OFF:
8711         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8712         _DISCONNECT_DB;
8713         
8714         EM_SAFE_FREE(target_mailbox_name);
8715
8716         if (err_code != NULL)
8717                 *err_code = error;
8718
8719         EM_DEBUG_FUNC_END("ret [%d]", ret);
8720         return ret;
8721 }
8722                 
8723 INTERNAL_FUNC int emstorage_delete_mail(int mail_id, int from_server, int transaction, int *err_code)
8724 {
8725         EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
8726
8727         if (!mail_id)  {
8728                 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
8729                 if (err_code != NULL)
8730                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8731                 return false;
8732         }               
8733         
8734         int rc, ret = false;
8735         int error = EMAIL_ERROR_NONE;
8736         char sql_query_string[QUERY_SIZE] = {0, };
8737         
8738         sqlite3 *local_db_handle = emstorage_get_db_connection();
8739         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8740         
8741         memset(sql_query_string, 0x00, sizeof(sql_query_string));
8742         
8743         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id = %d ", mail_id);
8744         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8745  
8746
8747         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8748         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
8749                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8750         
8751         ret = true;
8752         
8753 FINISH_OFF:
8754         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8755         _DISCONNECT_DB;
8756         
8757         if (err_code != NULL)
8758                 *err_code = error;
8759
8760         EM_DEBUG_FUNC_END("ret [%d]", ret);
8761         return ret;
8762 }
8763
8764 INTERNAL_FUNC int emstorage_delete_multiple_mails(int mail_ids[], int number_of_mails, int transaction, int *err_code)
8765 {
8766         EM_DEBUG_FUNC_BEGIN("mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", mail_ids, number_of_mails, transaction, err_code);
8767
8768         int rc, ret = false, i, cur_sql_query_string = 0;
8769         int error = EMAIL_ERROR_NONE;
8770         char sql_query_string[QUERY_SIZE] = {0, };
8771
8772         if (!mail_ids) {
8773                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8774                 if (err_code != NULL)
8775                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8776                 return false;
8777         }               
8778         
8779         sqlite3 *local_db_handle = emstorage_get_db_connection();
8780         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8781         
8782         cur_sql_query_string = SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id in (");
8783         
8784         for(i = 0; i < number_of_mails; i++)            
8785                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, QUERY_SIZE, "%d,", mail_ids[i]);
8786
8787         sql_query_string[strlen(sql_query_string) - 1] = ')';
8788         
8789         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8790
8791         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8792         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
8793                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8794         
8795         ret = true;
8796         
8797 FINISH_OFF:
8798         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8799         _DISCONNECT_DB;
8800         
8801         if (err_code != NULL)
8802                 *err_code = error;
8803
8804         EM_DEBUG_FUNC_END("ret [%d]", ret);
8805         return ret;
8806 }
8807
8808 INTERNAL_FUNC int emstorage_delete_mail_by_account(int account_id, int transaction, int *err_code)
8809 {
8810         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
8811         
8812         if (account_id < FIRST_ACCOUNT_ID)  {
8813                 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
8814                 
8815                 if (err_code != NULL)
8816                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8817                 return false;
8818         }
8819
8820         int rc, ret = false;
8821         int error = EMAIL_ERROR_NONE;
8822         char sql_query_string[QUERY_SIZE] = {0, };
8823         
8824         sqlite3 *local_db_handle = emstorage_get_db_connection();
8825         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8826         
8827         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d", account_id);
8828         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8829         
8830         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8831         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8832                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8833
8834         rc = sqlite3_changes(local_db_handle);
8835         if (rc == 0)  {
8836                 EM_DEBUG_EXCEPTION(" no mail found...");
8837                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8838         }
8839  
8840         /* Delete all mails  mail_read_mail_uid_tbl table based on account id */
8841         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
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, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8846                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8847
8848         rc = sqlite3_changes(local_db_handle);
8849         if (rc == 0)  {
8850                 EM_DEBUG_EXCEPTION("no mail found...");
8851                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8852         }
8853
8854         if (!emstorage_notify_storage_event(NOTI_MAIL_DELETE_WITH_ACCOUNT, account_id, 0 , NULL, 0))
8855                 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event Failed [ NOTI_MAIL_DELETE_ALL ]");
8856         
8857         ret = true;
8858         
8859 FINISH_OFF:
8860
8861         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8862         _DISCONNECT_DB;
8863         
8864         if (err_code != NULL)
8865                 *err_code = error;
8866
8867         EM_DEBUG_FUNC_END("ret [%d]", ret);
8868         return ret;
8869 }
8870
8871 INTERNAL_FUNC int emstorage_delete_mail_by_mailbox(int account_id, char *mailbox, int transaction, int *err_code)
8872 {
8873         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox, transaction, err_code);
8874         
8875         if (account_id < FIRST_ACCOUNT_ID || !mailbox)  {
8876                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox[%p]", account_id, mailbox);
8877                 if (err_code != NULL)
8878                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8879                 return false;
8880         }
8881
8882         int rc, ret = false;
8883         int error = EMAIL_ERROR_NONE;
8884         char sql_query_string[QUERY_SIZE] = {0, };
8885         
8886         sqlite3 *local_db_handle = emstorage_get_db_connection();
8887
8888         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8889         
8890         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d AND mailbox_name = '%s'", account_id, mailbox);
8891         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8892
8893         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8894         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8895                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8896                 
8897
8898         /* Delete Mails from mail_read_mail_uid_tbl */
8899         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);
8900         EM_DEBUG_LOG("Query [%s]", sql_query_string);
8901
8902         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8903         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8904                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8905         
8906         if (!emstorage_notify_storage_event(NOTI_MAIL_DELETE_ALL, account_id, 0 , mailbox, 0))
8907                 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event Failed [ NOTI_MAIL_DELETE_ALL ] >>>> ");
8908         
8909         ret = true;
8910         
8911 FINISH_OFF:
8912         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8913         _DISCONNECT_DB;
8914         
8915         if (err_code != NULL)
8916                 *err_code = error;
8917
8918         EM_DEBUG_FUNC_END("ret [%d]", ret);
8919         return ret;
8920 }
8921
8922 INTERNAL_FUNC int emstorage_free_mail(emstorage_mail_tbl_t** mail_list, int count, int *err_code)
8923 {
8924         EM_DEBUG_FUNC_BEGIN("mail_list[%p], count[%d], err_code[%p]", mail_list, count, err_code);
8925         
8926         if (count > 0)  {
8927                 if ((mail_list == NULL) || (*mail_list == NULL))  {
8928                         EM_DEBUG_EXCEPTION("mail_ilst[%p], count[%d]", mail_list, count);
8929                         
8930                         if (err_code)
8931                                 *err_code = EMAIL_ERROR_INVALID_PARAM;
8932                         return false;
8933                 }
8934
8935                 emstorage_mail_tbl_t* p = *mail_list;
8936                 int i = 0;
8937
8938                 for (; i < count; i++, p++) {
8939                         EM_SAFE_FREE(p->mailbox_name);
8940                         EM_SAFE_FREE(p->server_mailbox_name);
8941                         EM_SAFE_FREE(p->server_mail_id);
8942                         EM_SAFE_FREE(p->full_address_from);
8943                         EM_SAFE_FREE(p->full_address_reply);
8944                         EM_SAFE_FREE(p->full_address_to);
8945                         EM_SAFE_FREE(p->full_address_cc);
8946                         EM_SAFE_FREE(p->full_address_bcc);
8947                         EM_SAFE_FREE(p->full_address_return);
8948                         EM_SAFE_FREE(p->subject);
8949                         EM_SAFE_FREE(p->file_path_plain);
8950                         EM_SAFE_FREE(p->file_path_html);
8951                         EM_SAFE_FREE(p->file_path_mime_entity);
8952                         EM_SAFE_FREE(p->message_id);
8953                         EM_SAFE_FREE(p->email_address_sender);
8954                         EM_SAFE_FREE(p->email_address_recipient);
8955                         EM_SAFE_FREE(p->preview_text);
8956                         EM_SAFE_FREE(p->alias_sender);
8957                         EM_SAFE_FREE(p->alias_recipient);
8958                 }
8959                 EM_SAFE_FREE(*mail_list);
8960         }
8961         
8962         if (err_code != NULL)
8963                 *err_code = EMAIL_ERROR_NONE;
8964
8965         EM_DEBUG_FUNC_END();
8966         return true;
8967 }
8968
8969 INTERNAL_FUNC int emstorage_get_attachment_count(int mail_id, int *count, int transaction, int *err_code)
8970 {
8971         EM_DEBUG_FUNC_BEGIN("mail_id[%d], count[%p], transaction[%d], err_code[%p]", mail_id, count, transaction, err_code);
8972         
8973         if (mail_id <= 0 || !count)  {
8974                 EM_DEBUG_EXCEPTION("mail_id[%d], count[%p]", mail_id, count);
8975                 if (err_code != NULL)
8976                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8977                 return false;
8978         }
8979         
8980         int rc = -1, ret = false;
8981         int error = EMAIL_ERROR_NONE;
8982         char sql_query_string[QUERY_SIZE] = {0, };
8983         
8984         sqlite3 *local_db_handle = emstorage_get_db_connection();
8985         EMSTORAGE_START_READ_TRANSACTION(transaction);
8986         
8987         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
8988
8989         char **result;
8990
8991         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
8992         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
8993                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8994
8995         *count = atoi(result[1]);
8996         sqlite3_free_table(result);
8997  
8998         ret = true;
8999         
9000 FINISH_OFF:
9001  
9002         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
9003         _DISCONNECT_DB;
9004         
9005         if (err_code != NULL)
9006                 *err_code = error;
9007
9008         EM_DEBUG_FUNC_END("ret [%d]", ret);
9009         return ret;
9010 }
9011
9012 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)
9013 {
9014         EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], input_transaction[%d], output_attachment_list[%p], output_attachment_count[%p]", input_mail_id, output_attachment_list, input_transaction, output_attachment_count);
9015         
9016         if (input_mail_id <= 0 || !output_attachment_list || !output_attachment_count)  {
9017                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9018                 return EMAIL_ERROR_INVALID_PARAM;
9019         }
9020         
9021         int                         error = EMAIL_ERROR_NONE;
9022         int                         i = 0;
9023         int                         rc = -1;
9024         char                      **result = NULL;
9025         char                        sql_query_string[QUERY_SIZE] = {0, };
9026         emstorage_attachment_tbl_t* p_data_tbl = NULL;
9027         DB_STMT hStmt = NULL;
9028         sqlite3 *local_db_handle = emstorage_get_db_connection();
9029         
9030         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
9031         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", input_mail_id);
9032         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
9033         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9034                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9035
9036         *output_attachment_count = atoi(result[1]);
9037         sqlite3_free_table(result);
9038         
9039         if(*output_attachment_count == 0) {
9040                 error = EMAIL_ERROR_NONE;
9041                 goto FINISH_OFF;
9042         }
9043         
9044         p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * (*output_attachment_count));
9045         
9046         if (!p_data_tbl)  {
9047                 EM_DEBUG_EXCEPTION("em_malloc failed...");
9048                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9049                 goto FINISH_OFF;
9050         }
9051         
9052         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE mail_id = %d ORDER BY attachment_id", input_mail_id);
9053         EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
9054         
9055         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
9056         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)));
9057
9058         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9059         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },   ("sqlite3_step fail:%d", rc));
9060
9061         if (rc == SQLITE_DONE)  {
9062                 EM_DEBUG_EXCEPTION("no matched attachment found...");
9063                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
9064                 goto FINISH_OFF;
9065         }
9066         for (i = 0; i < *output_attachment_count; i++)  {
9067                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9068                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
9069                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
9070                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
9071                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9072                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9073                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9074                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9075                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9076                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
9077                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9078                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9079                 
9080                 EM_DEBUG_LOG("attachment[%d].attachment_id : %d", i, p_data_tbl[i].attachment_id);
9081                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9082                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9083         }
9084         
9085 FINISH_OFF:
9086
9087         if (error == EMAIL_ERROR_NONE)
9088                 *output_attachment_list = p_data_tbl;
9089         else if (p_data_tbl != NULL)
9090                 emstorage_free_attachment(&p_data_tbl, *output_attachment_count, NULL);
9091
9092         rc = sqlite3_finalize(hStmt);
9093         
9094         if (rc != SQLITE_OK)  {
9095                 EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
9096                 error = EMAIL_ERROR_DB_FAILURE;
9097         }
9098         
9099         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
9100         
9101         _DISCONNECT_DB;
9102
9103         EM_DEBUG_FUNC_END("error [%d]", error);
9104         return error;
9105 }
9106
9107 INTERNAL_FUNC int emstorage_get_attachment(int attachment_id, emstorage_attachment_tbl_t** attachment, int transaction, int *err_code)
9108 {
9109         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], transaction[%d], err_code[%p]", attachment_id, attachment, transaction, err_code);
9110         
9111         if (attachment_id <= 0 || !attachment)  {
9112                 EM_DEBUG_EXCEPTION("attachment_id[%d], attachment[%p]", attachment_id, attachment);
9113                 if (err_code != NULL)
9114                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9115                 return false;
9116         }
9117
9118         emstorage_attachment_tbl_t* p_data_tbl = NULL;
9119         int rc, ret = false;
9120         int error = EMAIL_ERROR_NONE;
9121         char sql_query_string[QUERY_SIZE] = {0, };
9122         
9123         sqlite3 *local_db_handle = emstorage_get_db_connection();
9124         EMSTORAGE_START_READ_TRANSACTION(transaction);
9125         
9126         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_id = %d",  attachment_id);
9127
9128         sqlite3_stmt* hStmt = NULL;
9129         
9130         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
9131         EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
9132         
9133         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9134                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9135         
9136
9137         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9138         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9139                 ("sqlite3_step fail:%d", rc));
9140
9141         if (rc == SQLITE_DONE)  {
9142                 EM_DEBUG_EXCEPTION("no matched attachment found...");
9143                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
9144                 goto FINISH_OFF;
9145         }
9146
9147         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1)))  {
9148                 EM_DEBUG_EXCEPTION("malloc failed...");
9149                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9150                 goto FINISH_OFF;
9151         }
9152
9153         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9154         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
9155         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
9156         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
9157         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9158         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9159         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9160         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9161         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9162         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
9163         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9164         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9165
9166 #ifdef __ATTACHMENT_OPTI__
9167                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
9168                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
9169 #endif  
9170  
9171         ret = true;
9172         
9173 FINISH_OFF:
9174         if (ret == true)
9175                 *attachment = p_data_tbl;
9176
9177         if (hStmt != NULL)  {
9178                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
9179
9180                 rc = sqlite3_finalize(hStmt);
9181                 if (rc != SQLITE_OK)  {
9182                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
9183                         error = EMAIL_ERROR_DB_FAILURE;
9184                 }
9185         }
9186
9187         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
9188         _DISCONNECT_DB;
9189         
9190         if (err_code != NULL)
9191                 *err_code = error;
9192
9193         EM_DEBUG_FUNC_END("ret [%d]", ret);
9194         return ret;
9195 }
9196
9197 INTERNAL_FUNC int emstorage_get_attachment_nth(int mail_id, int nth, emstorage_attachment_tbl_t** attachment_tbl, int transaction, int *err_code)
9198 {
9199         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);
9200
9201         if (mail_id <= 0 || nth <= 0 || !attachment_tbl)  {
9202                 EM_DEBUG_EXCEPTION(" mail_id[%d], nth[%d], attachment[%p]", mail_id, nth, attachment_tbl);
9203                 
9204                 if (err_code != NULL)
9205                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9206                 return false;
9207         }
9208         
9209         emstorage_attachment_tbl_t* p_data_tbl = NULL;
9210         char *p = NULL;
9211         int rc, ret = false;
9212         int error = EMAIL_ERROR_NONE;
9213         char sql_query_string[QUERY_SIZE] = {0, };
9214         
9215         sqlite3 *local_db_handle = emstorage_get_db_connection();
9216         EMSTORAGE_START_READ_TRANSACTION(transaction);
9217         
9218         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));
9219         EM_DEBUG_LOG("query = [%s]", sql_query_string);
9220         
9221         DB_STMT hStmt = NULL;
9222         
9223         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
9224         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9225                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9226
9227
9228         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9229         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9230                 ("sqlite3_step fail:%d", rc));
9231
9232         if (rc == SQLITE_DONE)  {
9233                 EM_DEBUG_EXCEPTION(" no matched attachment found...");
9234                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
9235                 goto FINISH_OFF;
9236         }
9237         
9238         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1)))  {
9239                 EM_DEBUG_EXCEPTION(" malloc failed...");
9240                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9241                 goto FINISH_OFF;
9242         }
9243
9244         p_data_tbl->attachment_id = sqlite3_column_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9245         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)strlen(p))
9246                 p_data_tbl->attachment_name = cpy_str(p);
9247         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)strlen(p))
9248                 p_data_tbl->attachment_path = cpy_str(p);
9249         p_data_tbl->attachment_size = sqlite3_column_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
9250         p_data_tbl->mail_id = sqlite3_column_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9251         p_data_tbl->account_id = sqlite3_column_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9252         p_data_tbl->mailbox_id = sqlite3_column_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9253         p_data_tbl->attachment_save_status = sqlite3_column_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9254         p_data_tbl->attachment_drm_type = sqlite3_column_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9255         p_data_tbl->attachment_drm_method = sqlite3_column_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
9256         p_data_tbl->attachment_inline_content_status = sqlite3_column_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9257         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)strlen(p))
9258                 p_data_tbl->attachment_mime_type = cpy_str(p);
9259 #ifdef __ATTACHMENT_OPTI__
9260                 p_data_tbl->encoding = sqlite3_column_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
9261                 if ((p = (char *)sqlite3_column_text(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)strlen(p))
9262                         p_data_tbl->section= cpy_str(p);
9263 #endif  
9264         ret = true;
9265         
9266 FINISH_OFF:
9267         if (ret == true)
9268                 *attachment_tbl = p_data_tbl;
9269         else if (p_data_tbl != NULL)
9270                 emstorage_free_attachment(&p_data_tbl, 1, NULL);
9271
9272         if (hStmt != NULL)  {
9273                 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
9274         
9275                 rc = sqlite3_finalize(hStmt);
9276                 if (rc != SQLITE_OK)  {
9277                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
9278                         error = EMAIL_ERROR_DB_FAILURE;
9279                 }
9280         }
9281         
9282         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
9283         _DISCONNECT_DB;
9284         
9285         if (err_code != NULL)
9286                 *err_code = error;
9287
9288         EM_DEBUG_FUNC_END("ret [%d]", ret);
9289         return ret;
9290 }
9291
9292 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)
9293 {
9294         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], attachment[%p], transaction[%d], err_code[%p]", mail_id, type, attachment, transaction, err_code);
9295         
9296         if (mail_id <= 0 || !attachment)  {
9297                 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], attachment[%p]", mail_id, type, attachment);
9298                 if (err_code != NULL)
9299                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9300                 return false;;
9301         }
9302         
9303         int rc, ret = false;
9304         int error = EMAIL_ERROR_NONE;
9305         DB_STMT hStmt = NULL;
9306         char sql_query_string[QUERY_SIZE] = {0, };
9307         
9308         int i = 0;
9309         
9310         sqlite3 *local_db_handle = emstorage_get_db_connection();
9311
9312         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9313         
9314         switch (type)  {
9315                 case UPDATE_MAILBOX:
9316                                 EM_DEBUG_LOG("UPDATE_MAILBOX");
9317                         if (!attachment->mailbox_id)  {
9318                                 EM_DEBUG_EXCEPTION(" attachment->mailbox_id[%d]", attachment->mailbox_id);
9319                                 error = EMAIL_ERROR_INVALID_PARAM;
9320                                 goto FINISH_OFF;
9321                         }
9322                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9323                                 "UPDATE mail_attachment_tbl SET mailbox_id = ? WHERE mail_id = %d", mail_id);
9324
9325                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
9326                         EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
9327                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9328                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9329
9330                         _bind_stmt_field_data_int(hStmt, i++, attachment->mailbox_id);
9331                         break;
9332                         
9333                 case UPDATE_SAVENAME:
9334                         EM_DEBUG_LOG("UPDATE_SAVENAME");
9335                         if (!attachment->attachment_path)  {
9336                                 EM_DEBUG_EXCEPTION(" attachment->attachment_path[%p]", attachment->attachment_path);
9337                                 error = EMAIL_ERROR_INVALID_PARAM;
9338                                 goto FINISH_OFF;
9339                         }
9340
9341                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9342                                 "UPDATE mail_attachment_tbl SET"
9343                                 "  attachment_size = ?"
9344                                 ", attachment_save_status = 1"
9345                                 ", attachment_path = ?"
9346                                 " WHERE mail_id = %d"
9347                                 " AND attachment_id = %d"
9348                                 , attachment->mail_id
9349                                 , attachment->attachment_id);
9350  
9351                         
9352                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
9353                         EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
9354                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9355                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9356                 
9357                         _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_size);
9358                         _bind_stmt_field_data_string(hStmt, i++, (char *)attachment->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
9359                         break;
9360                         
9361                 default:
9362                         EM_DEBUG_LOG("type[%d]", type);
9363                         error = EMAIL_ERROR_INVALID_PARAM;
9364                         goto FINISH_OFF;
9365         }
9366         EM_DEBUG_LOG("query = [%s]", sql_query_string);
9367
9368         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9369         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9370                 ("sqlite3_step fail:%d", rc));
9371         ret = true;
9372         
9373 FINISH_OFF:
9374         if (hStmt != NULL)  {
9375                 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
9376                 rc = sqlite3_finalize(hStmt);
9377                 if (rc != SQLITE_OK)  {
9378                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
9379                         error = EMAIL_ERROR_DB_FAILURE;
9380                 }
9381         }
9382
9383         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9384         _DISCONNECT_DB;
9385         
9386         if (err_code != NULL)
9387                 *err_code = error;
9388         EM_DEBUG_FUNC_END("ret [%d]", ret);
9389         return ret;
9390 }
9391
9392
9393 INTERNAL_FUNC int emstorage_rename_mailbox(int input_mailbox_id, char *input_new_mailbox_name, char *input_new_mailbox_alias, int input_transaction)
9394 {
9395         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);
9396
9397         int rc = 0;
9398         int ret = false;
9399         int error = EMAIL_ERROR_NONE;
9400         char sql_query_string[QUERY_SIZE] = {0, };
9401         sqlite3 *local_db_handle = NULL;
9402         int account_id = 0;
9403         emstorage_mailbox_tbl_t *old_mailbox_data = NULL;
9404
9405         if (input_mailbox_id <= 0 || !input_new_mailbox_name || !input_new_mailbox_alias)  {
9406                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9407                 return EMAIL_ERROR_INVALID_PARAM;
9408         }
9409
9410         local_db_handle = emstorage_get_db_connection();
9411
9412         if ((error = emstorage_get_mailbox_by_id(input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
9413                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", error);
9414                 goto FINISH_OFF;
9415         }
9416         account_id = old_mailbox_data->account_id;
9417
9418         EMSTORAGE_START_WRITE_TRANSACTION(input_transaction, error);
9419
9420         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9421                 "UPDATE mail_box_tbl SET"
9422                 " mailbox_name = '%s'"
9423                 ",alias = '%s'"
9424                 " WHERE mailbox_id = %d"
9425                 , input_new_mailbox_name
9426                 , input_new_mailbox_alias
9427                 , input_mailbox_id);
9428
9429         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9430
9431         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9432                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9433
9434         if (sqlite3_changes(local_db_handle) == 0)
9435                 EM_DEBUG_LOG("no mail_meeting_tbl matched...");
9436
9437         /* Update mail_tbl */
9438         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9439                 "UPDATE mail_tbl SET"
9440                 " mailbox_name = '%s'"
9441                 " WHERE mailbox_id = %d"
9442                 , input_new_mailbox_name
9443                 , input_mailbox_id);
9444
9445         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9446
9447         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9448                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9449
9450         if (sqlite3_changes(local_db_handle) == 0) 
9451                 EM_DEBUG_LOG("no mail matched...");
9452
9453         ret = true;
9454         
9455 FINISH_OFF:
9456
9457         EMSTORAGE_FINISH_WRITE_TRANSACTION(input_transaction, ret, error);
9458
9459         if (ret) {
9460                 if (!emstorage_notify_storage_event(NOTI_MAILBOX_RENAME, account_id, input_mailbox_id, input_new_mailbox_name, 0))
9461                         EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event Failed [ NOTI_MAILBOX_RENAME ] >>>> ");
9462         }
9463         else {
9464                 if (!emstorage_notify_storage_event(NOTI_MAILBOX_RENAME_FAIL, account_id, input_mailbox_id, input_new_mailbox_name, 0))
9465                         EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event Failed [ NOTI_MAILBOX_RENAME_FAIL ] >>>> ");
9466         }
9467
9468         if (old_mailbox_data)
9469                 emstorage_free_mailbox(&old_mailbox_data, 1, NULL);
9470
9471         _DISCONNECT_DB;
9472
9473         EM_DEBUG_FUNC_END("error [%d]", error);
9474         return error;
9475 }
9476         
9477 INTERNAL_FUNC int emstorage_get_new_attachment_no(int *attachment_no, int *err_code)
9478 {
9479         EM_DEBUG_FUNC_BEGIN("attachment_no [%p], err_code[%p]", attachment_no, err_code);
9480         int rc, ret = false;
9481         int error = EMAIL_ERROR_NONE;
9482         char *sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
9483         char **result;
9484         
9485         if (!attachment_no)  {
9486                 EM_DEBUG_EXCEPTION("Invalid attachment");
9487                 error = EMAIL_ERROR_INVALID_PARAM;
9488                 goto FINISH_OFF;
9489         }
9490
9491         *attachment_no = -1;
9492         
9493         sqlite3 *local_db_handle = emstorage_get_db_connection();
9494
9495
9496         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9497         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9498                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9499
9500         if (NULL == result[1])
9501                 rc = 1;
9502         else 
9503                 rc = atoi(result[1])+1;
9504
9505         sqlite3_free_table(result);
9506
9507         *attachment_no = rc;
9508         EM_DEBUG_LOG("attachment_no [%d]", *attachment_no);
9509         ret = true;
9510         
9511 FINISH_OFF:
9512         _DISCONNECT_DB;
9513         
9514         if (err_code != NULL)
9515                 *err_code = error;
9516
9517         EM_DEBUG_FUNC_END("ret [%d]", ret);
9518         return ret;
9519 }
9520         
9521 INTERNAL_FUNC int emstorage_add_attachment(emstorage_attachment_tbl_t* attachment_tbl, int iscopy, int transaction, int *err_code)
9522 {
9523         EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], iscopy[%d], transaction[%d], err_code[%p]", attachment_tbl, iscopy, transaction, err_code);
9524
9525         char *sql = NULL;
9526         char **result;
9527         int rc, ret = false;
9528         int error = EMAIL_ERROR_NONE;
9529         DB_STMT hStmt = NULL;
9530         char sql_query_string[QUERY_SIZE] = {0, };
9531         sqlite3 *local_db_handle = emstorage_get_db_connection();
9532         
9533         if (!attachment_tbl)  {
9534                 EM_DEBUG_EXCEPTION("attachment_tbl[%p], iscopy[%d]", attachment_tbl, iscopy);
9535                 if (err_code != NULL)
9536                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9537                 return false;
9538         }
9539         
9540         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9541  
9542         sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
9543
9544         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9545         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9546                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9547
9548         if (NULL==result[1]) rc = 1;
9549         else rc = atoi(result[1]) + 1;
9550         sqlite3_free_table(result);
9551
9552         attachment_tbl->attachment_id = rc;
9553         
9554         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9555                 "INSERT INTO mail_attachment_tbl VALUES "
9556                 "( ?"   /* attachment_id */
9557                 ", ?"   /* attachment_name */
9558                 ", ?"   /* attachment_path */
9559                 ", ?"   /* attachment_size */
9560                 ", ?"   /* mail_id */
9561                 ", ?"   /* account_id */
9562                 ", ?"   /* mailbox_id */
9563                 ", ?"   /* attachment_save_status */
9564                 ", ?"   /* attachment_drm_type */
9565                 ", ?"   /* attachment_drm_method */
9566                 ", ?"   /* attachment_inline_content_status */
9567                 ", ?"   /* attachment_mime_type */
9568 #ifdef __ATTACHMENT_OPTI__
9569                 ", ?"
9570                 ", ?"
9571 #endif          
9572                 ")");
9573         
9574         
9575         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
9576         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9577                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9578         
9579         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_id);
9580         _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);
9581         _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);
9582         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_size);
9583         _bind_stmt_field_data_int   (hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mail_id);
9584         _bind_stmt_field_data_int   (hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->account_id);
9585         _bind_stmt_field_data_int   (hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mailbox_id);
9586         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_save_status);
9587         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_type);
9588         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_method);
9589         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_inline_content_status);
9590         _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);
9591 #ifdef __ATTACHMENT_OPTI__
9592         _bind_stmt_field_data_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->encoding);
9593         _bind_stmt_field_data_string(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->section, 0, ATTACHMENT_LEN_IN_MAIL_ATTACHMENT_TBL);
9594 #endif
9595         
9596
9597         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9598         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
9599                 ("sqlite3_step fail:%d", rc));
9600         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9601                 ("sqlite3_step fail:%d", rc));
9602 /*
9603         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9604                 "UPDATE mail_tbl SET attachment_count = 1 WHERE mail_id = %d", attachment_tbl->mail_id);
9605         
9606         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9607
9608         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
9609                 ("sqlite3_exec fail:%d", rc));
9610         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9611                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9612 */
9613         rc = sqlite3_changes(local_db_handle);
9614         if (rc == 0)  {
9615                 EM_DEBUG_EXCEPTION(" no matched mail found...");
9616                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9617                 goto FINISH_OFF;
9618         }
9619         
9620         ret = true;
9621         
9622 FINISH_OFF:
9623         if (hStmt != NULL)  {
9624                 rc = sqlite3_finalize(hStmt);
9625                 if (rc != SQLITE_OK)  {
9626                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
9627                         error = EMAIL_ERROR_DB_FAILURE;
9628                 }
9629         }
9630
9631         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9632         _DISCONNECT_DB;
9633         
9634         if (err_code != NULL)
9635         *err_code = error;
9636
9637         EM_DEBUG_FUNC_END("ret [%d]", ret);
9638         return ret;
9639 }
9640
9641 INTERNAL_FUNC int emstorage_update_attachment(emstorage_attachment_tbl_t* attachment_tbl, int transaction, int *err_code)
9642 {
9643         EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], transaction[%d], err_code[%p]", attachment_tbl, transaction, err_code);
9644         
9645         int rc, ret = false, field_idx = 0;
9646         int error = EMAIL_ERROR_NONE;
9647         DB_STMT hStmt = NULL;
9648         char sql_query_string[QUERY_SIZE] = {0, };
9649
9650         if (!attachment_tbl)  {
9651                 EM_DEBUG_EXCEPTION(" attachment_tbl[%p] ", attachment_tbl);
9652                 error = EMAIL_ERROR_INVALID_PARAM;
9653                 goto FINISH_OFF;
9654         }
9655         
9656         sqlite3 *local_db_handle = emstorage_get_db_connection();
9657
9658         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9659  
9660         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9661                 "UPDATE mail_attachment_tbl SET  "
9662                 "  attachment_name = ?"   
9663                 ", attachment_path =  ?"
9664                 ", attachment_size = ?"
9665                 ", mail_id = ?"
9666                 ", account_id = ?"
9667                 ", mailbox_id = ?"
9668                 ", attachment_save_status = ?"
9669                 ", attachment_drm_type = ?"
9670                 ", attachment_drm_method = ?"
9671                 ", attachment_inline_content_status = ? "
9672                 ", attachment_mime_type = ? "
9673                 " WHERE attachment_id = ?;");
9674         
9675         
9676         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
9677
9678         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9679                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9680
9681         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
9682         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
9683         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_size);
9684         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->mail_id);
9685         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->account_id);
9686         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->mailbox_id);
9687         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_save_status);
9688         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_drm_type);
9689         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_drm_method);
9690         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_inline_content_status);
9691         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_mime_type, 0, ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL);
9692         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_id);
9693         
9694
9695         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9696         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
9697                 ("sqlite3_step fail:%d", rc));
9698         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9699                 ("sqlite3_step fail:%d", rc));
9700 /* 
9701         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9702                 "UPDATE mail_tbl SET attachment_count = 1 WHERE mail_id = %d", attachment_tbl->mail_id);
9703         
9704         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9705
9706         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
9707                 ("sqlite3_exec fail:%d", rc));
9708         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9709                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9710 */
9711         rc = sqlite3_changes(local_db_handle);
9712         if (rc == 0)  {
9713                 EM_DEBUG_EXCEPTION(" no matched mail found...");
9714                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9715                 goto FINISH_OFF;
9716         }
9717         
9718         ret = true;
9719         
9720 FINISH_OFF:
9721         if (hStmt != NULL)  {
9722                 rc = sqlite3_finalize(hStmt);
9723                 if (rc != SQLITE_OK)  {
9724                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
9725                         error = EMAIL_ERROR_DB_FAILURE;
9726                 }
9727         }
9728
9729         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9730         _DISCONNECT_DB;
9731         
9732         if (err_code != NULL)
9733         *err_code = error;
9734
9735         EM_DEBUG_FUNC_END("ret [%d]", ret);
9736         return ret;
9737 }
9738
9739 INTERNAL_FUNC int emstorage_delete_attachment_on_db(int attachment_id, int transaction, int *err_code)
9740 {
9741         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], transaction[%d], err_code[%p]", attachment_id, transaction, err_code);
9742         
9743         if (attachment_id < 0)  {
9744                 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
9745                 if (err_code != NULL)
9746                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9747                 return false;
9748         }
9749         
9750         int rc, ret = false;
9751         int error = EMAIL_ERROR_NONE;
9752         char sql_query_string[QUERY_SIZE] = {0, };
9753         
9754         sqlite3 *local_db_handle = emstorage_get_db_connection();
9755
9756         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9757         
9758         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
9759
9760         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9761         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9762                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9763
9764         ret = true;
9765
9766 FINISH_OFF:
9767         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9768         _DISCONNECT_DB;
9769
9770         if (err_code)
9771                 *err_code = error;
9772
9773         EM_DEBUG_FUNC_END("ret [%d]", ret);
9774         return ret;
9775 }
9776
9777 INTERNAL_FUNC int emstorage_delete_all_attachments_of_mail(int mail_id, int transaction, int *err_code)
9778 {
9779         EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9780
9781         if (mail_id < 0)  {
9782                 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
9783                 if (err_code != NULL)
9784                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9785                 return false;
9786         }
9787
9788         int rc, ret = false;
9789         int error = EMAIL_ERROR_NONE;
9790         char sql_query_string[QUERY_SIZE] = {0, };
9791
9792         sqlite3 *local_db_handle = emstorage_get_db_connection();
9793
9794         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9795
9796         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
9797
9798         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9799         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9800                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9801
9802         ret = true;
9803         
9804 FINISH_OFF:
9805         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9806         _DISCONNECT_DB;
9807         
9808         if (err_code)
9809                 *err_code = error;
9810         
9811         EM_DEBUG_FUNC_END("ret [%d]", ret);
9812         return ret;
9813 }
9814
9815 INTERNAL_FUNC int emstorage_delete_attachment_all_on_db(int account_id, char *mailbox, int transaction, int *err_code)
9816 {
9817         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox, transaction, err_code);
9818         
9819         int error = EMAIL_ERROR_NONE;
9820         int rc, ret = false;
9821         char sql_query_string[QUERY_SIZE] = {0, };
9822         
9823         sqlite3 *local_db_handle = emstorage_get_db_connection();
9824
9825         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9826         
9827         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl");
9828         
9829         if (account_id != ALL_ACCOUNT) /*  '0' means all account */
9830                 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " WHERE account_id = %d", account_id);
9831
9832         if (mailbox)    /*  NULL means all mailbox_name */
9833                 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " %s mailbox_name = '%s'", account_id != ALL_ACCOUNT ? "AND" : "WHERE", mailbox);
9834         
9835         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9836         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9837                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9838
9839         ret = true;
9840         
9841 FINISH_OFF:
9842         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9843         _DISCONNECT_DB;
9844         
9845         if (err_code != NULL)
9846                 *err_code = error;
9847
9848         EM_DEBUG_FUNC_END("ret [%d]", ret);
9849         return ret;
9850 }
9851
9852 INTERNAL_FUNC int emstorage_free_attachment(emstorage_attachment_tbl_t** attachment_tbl_list, int count, int *err_code)
9853 {
9854         EM_DEBUG_FUNC_BEGIN("attachment_tbl_list[%p], count[%d], err_code[%p]", attachment_tbl_list, count, err_code);
9855         
9856         if (count > 0)  {
9857                 if ((attachment_tbl_list == NULL) || (*attachment_tbl_list == NULL))  {
9858                         EM_DEBUG_EXCEPTION(" attachment_tbl_list[%p], count[%d]", attachment_tbl_list, count);
9859                         if (err_code != NULL)
9860                                 *err_code = EMAIL_ERROR_INVALID_PARAM;
9861                         return false;
9862                 }
9863                 
9864                 emstorage_attachment_tbl_t* p = *attachment_tbl_list;
9865                 int i;
9866                 
9867                 for (i = 0; i < count; i++)  {
9868                         EM_SAFE_FREE(p[i].attachment_name);
9869                         EM_SAFE_FREE(p[i].attachment_path);
9870 #ifdef __ATTACHMENT_OPTI__
9871                         EM_SAFE_FREE(p[i].section);
9872 #endif                  
9873                 }
9874                 
9875                 EM_SAFE_FREE(p); 
9876                 *attachment_tbl_list = NULL;
9877         }
9878         
9879         if (err_code != NULL)
9880                 *err_code = EMAIL_ERROR_NONE;
9881         EM_DEBUG_FUNC_END();
9882         return true;
9883 }
9884
9885
9886
9887 INTERNAL_FUNC int emstorage_begin_transaction(void *d1, void *d2, int *err_code)
9888 {
9889         EM_PROFILE_BEGIN(emStorageBeginTransaction);
9890         int ret = true;
9891
9892         ENTER_CRITICAL_SECTION(_transactionBeginLock);
9893         
9894         /*  wait for the trnasaction authority to be changed. */
9895         while (g_transaction)  {
9896                 EM_DEBUG_LOG(">>>>>>>> Wait for the transaction authority to be changed");
9897                 usleep(50000);
9898         }
9899
9900         /*  take the transaction authority. */
9901         g_transaction = true;
9902
9903         LEAVE_CRITICAL_SECTION(_transactionBeginLock);
9904  
9905         sqlite3 *local_db_handle = emstorage_get_db_connection();
9906         int rc;
9907         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN immediate;", NULL, NULL, NULL), rc);
9908         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
9909                 ("SQL(BEGIN) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
9910         if (ret == false && err_code != NULL)
9911                 *err_code = EMAIL_ERROR_DB_FAILURE;
9912
9913         EM_PROFILE_END(emStorageBeginTransaction);
9914         EM_DEBUG_FUNC_END("ret [%d]", ret);
9915         return ret;
9916 }
9917
9918 INTERNAL_FUNC int emstorage_commit_transaction(void *d1, void *d2, int *err_code)
9919 {
9920         EM_DEBUG_FUNC_BEGIN();
9921         int ret = true;
9922         sqlite3 *local_db_handle = emstorage_get_db_connection();
9923
9924         ENTER_CRITICAL_SECTION(_transactionEndLock);
9925
9926         int rc;
9927         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
9928         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; }, ("SQL(END) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
9929         /*  release the transaction authority. */
9930         g_transaction = false;
9931
9932         LEAVE_CRITICAL_SECTION(_transactionEndLock);
9933         if (ret == false && err_code != NULL)
9934                 *err_code = EMAIL_ERROR_DB_FAILURE;
9935
9936         EM_DEBUG_FUNC_END("ret [%d]", ret);
9937         return ret;
9938 }
9939
9940 INTERNAL_FUNC int emstorage_rollback_transaction(void *d1, void *d2, int *err_code)
9941 {
9942         EM_DEBUG_FUNC_BEGIN();
9943         int ret = true;
9944         sqlite3 *local_db_handle = emstorage_get_db_connection();
9945         int rc;
9946
9947         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "ROLLBACK;", NULL, NULL, NULL), rc);
9948
9949         ENTER_CRITICAL_SECTION(_transactionEndLock);
9950         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
9951                 ("SQL(ROLLBACK) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
9952
9953         /*  release the transaction authority. */
9954         g_transaction = false;
9955
9956         LEAVE_CRITICAL_SECTION(_transactionEndLock);
9957
9958         if (ret == false && err_code != NULL)
9959                 *err_code = EMAIL_ERROR_DB_FAILURE;
9960
9961         EM_DEBUG_FUNC_END("ret [%d]", ret);
9962         return ret;
9963 }
9964
9965 INTERNAL_FUNC int emstorage_is_mailbox_full(int account_id, email_mailbox_t *mailbox, int *result, int *err_code)
9966 {
9967         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], result[%p], err_code[%p]", account_id, mailbox, result, err_code);
9968         
9969         if (account_id < FIRST_ACCOUNT_ID || !mailbox || !result)  {
9970                 if (mailbox)
9971                         EM_DEBUG_EXCEPTION("Invalid Parameter. accoun_id[%d], mailbox[%p]", account_id, mailbox);
9972                 
9973                 if (err_code != NULL)
9974                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9975
9976                 return false;
9977         }
9978
9979         int ret = false;
9980         int error = EMAIL_ERROR_NONE;
9981         int mail_count = 0;
9982
9983         if (!emstorage_get_mail_count(account_id, mailbox->mailbox_name, &mail_count, NULL, true, &error)) {
9984                 EM_DEBUG_EXCEPTION("emstorage_get_mail_count failed [%d]", error);
9985                 goto FINISH_OFF;
9986         }
9987         
9988         if (mailbox) {
9989                 EM_DEBUG_LOG("mail_count[%d] mail_slot_size[%d]", mail_count, mailbox->mail_slot_size);
9990                 if (mail_count >= mailbox->mail_slot_size)
9991                         *result = true;
9992                 else
9993                         *result = false;
9994
9995                 ret = true;
9996         }
9997         
9998         ret = true;     
9999 FINISH_OFF:
10000
10001         if (err_code != NULL)
10002                 *err_code = error;
10003         
10004         EM_DEBUG_FUNC_END("ret [%d]", ret);
10005         return ret;
10006 }
10007
10008 INTERNAL_FUNC int emstorage_clear_mail_data(int transaction, int *err_code)
10009 {
10010         EM_DEBUG_FUNC_BEGIN("transaction[%d], err_code[%p]", transaction, err_code);
10011
10012         int rc, ret = false;
10013         int error = EMAIL_ERROR_NONE;
10014         char sql_query_string[QUERY_SIZE] = {0, };
10015         
10016         const email_db_object_t* tables = _g_db_tables;
10017         const email_db_object_t* indexes = _g_db_indexes;
10018         char data_path[256];
10019         
10020         sqlite3 *local_db_handle = emstorage_get_db_connection();
10021         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10022         
10023         if (!emstorage_delete_dir(MAILHOME, &error)) {
10024                 EM_DEBUG_EXCEPTION(" emstorage_delete_dir failed - %d", error);
10025
10026                 goto FINISH_OFF;
10027         }
10028         
10029         SNPRINTF(data_path, sizeof(data_path), "%s/%s", MAILHOME, MAILTEMP);
10030         
10031         mkdir(MAILHOME, DIRECTORY_PERMISSION);
10032         mkdir(data_path, DIRECTORY_PERMISSION);
10033         
10034         /*  first clear index. */
10035         while (indexes->object_name)  {
10036                 if (indexes->data_flag)  {
10037                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP index %s", indexes->object_name);
10038                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10039                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10040                                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10041                 }
10042                 indexes++;
10043         }
10044         
10045         while (tables->object_name)  {
10046                 if (tables->data_flag)  {
10047                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP table %s", tables->object_name);
10048                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10049                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10050                                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10051                 }
10052                 
10053                 tables++;
10054         }
10055         ret = true;
10056         
10057 FINISH_OFF:
10058         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10059         _DISCONNECT_DB;
10060         
10061         if (err_code != NULL)
10062                 *err_code = error;
10063
10064         EM_DEBUG_FUNC_END("ret [%d]", ret);
10065         return ret;
10066 }
10067 /*======================= DB File Utils =============================================*/
10068 #include <dirent.h>
10069 #include <sys/types.h>
10070 #define  DIR_SEPERATOR "/"
10071
10072 char *__em_create_dir_by_file_name(char *file_name)
10073 {
10074         EM_DEBUG_FUNC_BEGIN("Filename [ %p ]", file_name);
10075         char delims[] = "/";
10076         char *result = NULL;
10077
10078         result = strtok(file_name, delims);
10079
10080         if (result)
10081                 EM_DEBUG_LOG(">>>> Directory_name [ %s ]", result);
10082         else
10083                 EM_DEBUG_LOG(">>>> No Need to create Directory");
10084
10085         return result;
10086 }
10087
10088 INTERNAL_FUNC int emstorage_get_save_name(int account_id, int mail_id, int atch_id, char *fname, char *name_buf, int *err_code)
10089 {
10090         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);
10091         EM_PROFILE_BEGIN(profile_emstorage_get_save_name);
10092         
10093         int ret = false;
10094         int error = EMAIL_ERROR_NONE;
10095         char *dir_name = NULL;
10096         char create_dir[1024]={0};
10097         char *temp_file = NULL;
10098         
10099         if (!name_buf || account_id < FIRST_ACCOUNT_ID || mail_id < 0 || atch_id < 0)  {        
10100                 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);
10101                 error = EMAIL_ERROR_INVALID_PARAM;
10102                 goto FINISH_OFF;
10103         }
10104
10105         sprintf(name_buf, "%s", MAILHOME);
10106         sprintf(name_buf+strlen(name_buf),      "%s%d", DIR_SEPERATOR, account_id);
10107         
10108         if (mail_id > 0)
10109                 sprintf(name_buf+strlen(name_buf),      "%s%d", DIR_SEPERATOR, mail_id);
10110
10111         if (atch_id > 0)
10112                 sprintf(name_buf+strlen(name_buf),      "%s%d", DIR_SEPERATOR, atch_id);
10113
10114         if (fname) {
10115                 temp_file = EM_SAFE_STRDUP(fname);
10116                 if (strstr(temp_file, "/")) {
10117                         dir_name = __em_create_dir_by_file_name(temp_file);
10118                 }
10119         }
10120
10121         if (dir_name) {
10122                 sprintf(create_dir,     "%s%s%s", name_buf, DIR_SEPERATOR, dir_name);
10123                 EM_DEBUG_LOG(">>>>> DIR PATH [ %s ]", create_dir);
10124                 mkdir(create_dir, DIRECTORY_PERMISSION);
10125                 EM_SAFE_FREE(temp_file);
10126         }
10127
10128         
10129         if (fname) {
10130                 EM_DEBUG_LOG(">>>>> fname [ %s ]", fname);
10131                 sprintf(name_buf+strlen(name_buf),      "%s%s", DIR_SEPERATOR, fname);
10132         }
10133                 
10134
10135         EM_DEBUG_LOG(">>>>> name_buf [ %s ]", name_buf);
10136         
10137         ret = true;
10138         
10139 FINISH_OFF:
10140         EM_SAFE_FREE(temp_file);
10141
10142         if (err_code != NULL)
10143                 *err_code = error;
10144
10145         EM_PROFILE_END(profile_emstorage_get_save_name);
10146         EM_DEBUG_FUNC_END("ret [%d]", ret);
10147         return ret;
10148 }
10149
10150 INTERNAL_FUNC int emstorage_get_dele_name(int account_id, int mail_id, int atch_id, char *fname, char *name_buf, int *err_code)
10151 {
10152         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);
10153         
10154         if (!name_buf || account_id < FIRST_ACCOUNT_ID)  {
10155                 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);
10156                 if (err_code != NULL)
10157                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10158                 return false;
10159         }
10160
10161         sprintf(name_buf+strlen(name_buf),      "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
10162         
10163         if (mail_id > 0)
10164                 sprintf(name_buf+strlen(name_buf),      "%s%d", DIR_SEPERATOR, mail_id);
10165         else
10166                 goto FINISH_OFF;
10167         
10168         if (atch_id > 0)
10169                 sprintf(name_buf+strlen(name_buf),      "%s%d", DIR_SEPERATOR, atch_id);
10170         else
10171                 goto FINISH_OFF;
10172
10173 FINISH_OFF:
10174         sprintf(name_buf+strlen(name_buf),      ".DELE");
10175
10176         EM_DEBUG_FUNC_END();
10177         return true;
10178 }
10179
10180 INTERNAL_FUNC int emstorage_create_dir(int account_id, int mail_id, int atch_id, int *err_code)
10181 {
10182         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], err_code[%p]", account_id, mail_id, atch_id, err_code);
10183         EM_PROFILE_BEGIN(profile_emcore_save_create_dir);
10184         int ret = false;
10185         int error = EMAIL_ERROR_NONE;
10186         
10187         char buf[512];
10188         struct stat sbuf;
10189         if (account_id >= FIRST_ACCOUNT_ID)  {  
10190                 SNPRINTF(buf, sizeof(buf), "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
10191                 
10192                 if (stat(buf, &sbuf) == 0) {
10193                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR)  {
10194                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
10195                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10196                                 goto FINISH_OFF;
10197                         }
10198                 }
10199                 else  {
10200                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
10201                                 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
10202                                 EM_DEBUG_EXCEPTION("mkdir failed l(Errno=%d)][ErrStr=%s]", errno, strerror(errno));
10203                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10204                                 goto FINISH_OFF;
10205                         }
10206                 }
10207         }
10208
10209         if (mail_id > 0)  {
10210                 if (account_id < FIRST_ACCOUNT_ID)  {
10211                         EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
10212                         error = EMAIL_ERROR_INVALID_PARAM;
10213                         goto FINISH_OFF;
10214                 }
10215
10216                 SNPRINTF(buf+strlen(buf), sizeof(buf), "%s%d", DIR_SEPERATOR, mail_id);
10217                 
10218                 if (stat(buf, &sbuf) == 0) {
10219                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR)  {
10220                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
10221                                 
10222                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10223                                 goto FINISH_OFF;
10224                         }
10225                 }
10226                 else  {
10227                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
10228                                 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
10229                                 EM_DEBUG_EXCEPTION("mkdir failed l (Errno=%d)][ErrStr=%s]", errno, strerror(errno));
10230                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10231                                 goto FINISH_OFF;
10232                         }
10233                 }
10234         }
10235
10236         if (atch_id > 0)  {
10237                 if (account_id < FIRST_ACCOUNT_ID || mail_id <= 0)  {
10238                         EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
10239                         
10240                         error = EMAIL_ERROR_INVALID_PARAM;
10241                         goto FINISH_OFF;
10242                 }
10243
10244                 SNPRINTF(buf+strlen(buf), sizeof(buf)-(strlen(buf)+1), "%s%d", DIR_SEPERATOR, atch_id);
10245                 
10246                 if (stat(buf, &sbuf) == 0) {
10247                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR)  {
10248                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
10249                                 
10250                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10251                                 goto FINISH_OFF;
10252                         }
10253                 }
10254                 else  {
10255                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
10256                                 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
10257                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10258                                 goto FINISH_OFF;
10259                         }
10260                 }
10261         }
10262
10263         ret = true;
10264         
10265 FINISH_OFF:
10266         if (err_code != NULL)
10267                 *err_code = error;
10268
10269         EM_PROFILE_END(profile_emcore_save_create_dir);
10270         EM_DEBUG_FUNC_END("ret [%d]", ret);
10271         return ret;
10272 }
10273
10274 INTERNAL_FUNC int emstorage_copy_file(char *src_file, char *dst_file, int sync_status, int *err_code)
10275 {
10276         EM_DEBUG_FUNC_BEGIN("src_file[%s], dst_file[%s], err_code[%p]", src_file, dst_file, err_code);
10277         EM_DEBUG_LOG("Using the fsync function");
10278         int ret = false;
10279         int error = EMAIL_ERROR_NONE;
10280         struct stat st_buf;
10281
10282         int fp_src = 0;
10283         int fp_dst = 0;
10284         int nread = 0;
10285         int nwritten = 0;
10286         char *buf =  NULL;
10287         int buf_size = 0;
10288         
10289         if (!src_file || !dst_file)  {
10290                 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
10291                 
10292                 error = EMAIL_ERROR_INVALID_PARAM;
10293                 goto FINISH_OFF;
10294         }
10295         
10296         if (stat(src_file, &st_buf) < 0) {
10297                 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", src_file);
10298                 
10299                 error = EMAIL_ERROR_SYSTEM_FAILURE;             /* EMAIL_ERROR_INVALID_PATH; */
10300                 goto FINISH_OFF;
10301         }
10302         
10303         buf_size =  st_buf.st_size;
10304         EM_DEBUG_LOG(">>>> File Size [ %d ]", buf_size);
10305         buf = (char *)calloc(1, buf_size+1);
10306
10307         if (!buf) {
10308                 EM_DEBUG_EXCEPTION(">>> Memory cannot be allocated");
10309                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10310                 goto FINISH_OFF;
10311         }
10312
10313         if (buf) {
10314                 if (!(fp_src = open(src_file, O_RDONLY))) {
10315                         EM_DEBUG_EXCEPTION(">>>> Source Fail open %s Failed [ %d ] - Error [ %s ]", src_file, errno, strerror(errno));
10316                         error = EMAIL_ERROR_SYSTEM_FAILURE;
10317                         goto FINISH_OFF;                                
10318                 }
10319
10320                 if (!(fp_dst = open(dst_file, O_CREAT | O_WRONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH))) {
10321                         EM_DEBUG_EXCEPTION(">>>> Destination Fail open %s Failed [ %d ] - Error [ %s ]", dst_file, errno, strerror(errno));
10322                         error = EMAIL_ERROR_SYSTEM_FAILURE;
10323                         goto FINISH_OFF;                                
10324                 }
10325
10326                 while ((nread = read(fp_src, buf, buf_size)) > 0) {
10327                         if (nread > 0 && nread <= buf_size)  {          
10328                                 EM_DEBUG_LOG("Nread Value [%d]", nread);
10329                                 if ((nwritten = write(fp_dst, buf, nread)) != nread) {
10330                                         EM_DEBUG_EXCEPTION("fwrite failed...");
10331                                         error = EMAIL_ERROR_UNKNOWN;
10332                                         goto FINISH_OFF;
10333                                 }
10334                                 EM_DEBUG_LOG("NWRITTEN [%d]", nwritten);
10335                         }
10336                 }
10337         }
10338
10339         ret = true;
10340         
10341 FINISH_OFF:
10342         if (fp_src)
10343                 close(fp_src);                  
10344         
10345         if (fp_dst) {
10346                 if (sync_status) {
10347                         EM_DEBUG_LOG("Before fsync");
10348                         fsync(fp_dst);
10349                 }
10350                 close(fp_dst);                  
10351         }
10352         EM_SAFE_FREE(buf);
10353         if (nread < 0 || error == EMAIL_ERROR_UNKNOWN)
10354                 remove(dst_file);
10355         
10356         if (err_code != NULL)
10357                 *err_code = error;
10358         EM_DEBUG_FUNC_END("ret [%d]", ret);
10359         return ret;
10360 }
10361 /* create Directory if user has deleted [deepam.p@samsung.com] */
10362 INTERNAL_FUNC void emstorage_create_dir_if_delete()
10363 {
10364         EM_DEBUG_FUNC_BEGIN();
10365
10366         mkdir(EMAILPATH, DIRECTORY_PERMISSION);
10367         mkdir(DATA_PATH, DIRECTORY_PERMISSION);
10368
10369         mkdir(MAILHOME, DIRECTORY_PERMISSION);
10370         
10371         SNPRINTF(g_db_path, sizeof(g_db_path), "%s/%s", MAILHOME, MAILTEMP);
10372         mkdir(g_db_path, DIRECTORY_PERMISSION);
10373         
10374         /* _delete_temp_file(g_db_path); */
10375         EM_DEBUG_FUNC_END();
10376 }
10377 static int _get_temp_file_name(char **filename, int *err_code)
10378 {
10379         EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
10380         
10381         int ret = false;
10382         int error = EMAIL_ERROR_NONE;
10383         
10384         if (filename == NULL) {
10385                 EM_DEBUG_EXCEPTION(" filename[%p]", filename);
10386                 error = EMAIL_ERROR_INVALID_PARAM;
10387                 goto FINISH_OFF;
10388         }
10389         
10390         char tempname[512] = {0x00, };
10391         struct timeval tv;
10392         
10393         gettimeofday(&tv, NULL);
10394         srand(tv.tv_usec);
10395         
10396         SNPRINTF(tempname, sizeof(tempname), "%s%c%s%c%d", MAILHOME, '/', MAILTEMP, '/', rand());
10397         
10398         char *p = EM_SAFE_STRDUP(tempname);
10399         if (p == NULL)  {
10400                 EM_DEBUG_EXCEPTION(" strdup failed...");
10401                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10402                 goto FINISH_OFF;
10403         }
10404         
10405         *filename = p;
10406         
10407         ret = true;
10408
10409 FINISH_OFF:
10410         if (err_code != NULL)
10411                 *err_code = error;
10412         
10413         EM_DEBUG_FUNC_END("ret [%d]", ret);
10414         return ret;
10415 }
10416
10417 INTERNAL_FUNC int emstorage_add_content_type(char *file_path, char *char_set, int *err_code)
10418 {
10419         EM_DEBUG_FUNC_BEGIN("File path [ %p ]  Character Set [ %p ] err_code [ %p]", file_path, char_set, err_code);
10420
10421         EM_IF_NULL_RETURN_VALUE(file_path, false);
10422         EM_IF_NULL_RETURN_VALUE(char_set, false);
10423         EM_IF_NULL_RETURN_VALUE(err_code, false);
10424
10425         char *buf =  NULL;
10426         char *buf1 = NULL;
10427         struct stat st_buf;
10428         int buf_size = 0;
10429         char *low_char_set = NULL;
10430         char *match_str = NULL;
10431         int nwritten = 0;
10432         int ret = false;
10433         int error = EMAIL_ERROR_NONE;
10434         int data_count_to_written = 0;
10435         char *temp_file_name = NULL;
10436         int err = 0;
10437         
10438         FILE* fp_src = NULL;
10439         FILE* fp_dest = NULL;
10440         int nread = 0;
10441         
10442
10443         if (stat(file_path, &st_buf) < 0) {
10444                 EM_DEBUG_EXCEPTION(" stat(\"%s\") failed...", file_path);
10445                 
10446                 error = EMAIL_ERROR_SYSTEM_FAILURE;             /* EMAIL_ERROR_INVALID_PATH; */
10447                 goto FINISH_OFF;
10448         }
10449
10450         buf_size =  st_buf.st_size;
10451
10452         EM_DEBUG_LOG(">>>> File Size [ %d ] ", buf_size);
10453
10454         buf = (char *)calloc(1, buf_size+1);
10455
10456         if (!buf) {
10457                 EM_DEBUG_LOG(">>> Memory cannot be allocated ");
10458                 goto FINISH_OFF;
10459         }
10460
10461         if (!(fp_src = fopen(file_path, "rb"))) {
10462                 EM_DEBUG_EXCEPTION(" file_path fopen failed - %s", file_path);
10463                         
10464                         error = EMAIL_ERROR_SYSTEM_FAILURE;
10465                         goto FINISH_OFF;
10466                 }
10467                                 
10468                 if ((nread = fread(buf, 1, buf_size, fp_src)) > 0) {
10469                         if (nread > 0 && nread <= buf_size)  {          
10470                                 EM_DEBUG_LOG(">>>> Nread Value [ %d ] ", nread);
10471                                 
10472                                 /**
10473                                   *   1.Add check for whether content type is there. 
10474                                   *   2. If not based on the character set, Append it in File
10475                                   **/
10476
10477                                 low_char_set = calloc(1, strlen(char_set) + strlen(" \" /></head>") +1);
10478                                 
10479                                 strncat(low_char_set, char_set, strlen(char_set));
10480                                 
10481                                 EM_DEBUG_LOG(">>>> CHAR SET [ %s ] ", low_char_set);
10482                                 
10483                                 strncat(low_char_set, " \" /></head>", strlen(" \" /></head>"));
10484                                         
10485                                 EM_DEBUG_LOG(">>> CHARSET [ %s ] ", low_char_set);
10486
10487                                 EM_DEBUG_LOG(">>>>emstorage_add_content_type 1 ");
10488                                 
10489                                 match_str = strstr(buf, CONTENT_TYPE_DATA);
10490                                 EM_DEBUG_LOG(">>>>emstorage_add_content_type 2 ");
10491                                 
10492                                 if (match_str == NULL) {
10493                                         EM_DEBUG_LOG(">>>>emstorage_add_content_type 3 ");
10494                                         if (fp_src !=NULL) {
10495                                                 fclose(fp_src);fp_src = NULL;
10496                                         }
10497                                 data_count_to_written = strlen(low_char_set)+strlen(CONTENT_DATA)+1;
10498                                         EM_DEBUG_LOG(">>>>emstorage_add_content_type 4 ");
10499                                 buf1 = (char *)calloc(1, data_count_to_written);
10500                                         EM_DEBUG_LOG(">>>>emstorage_add_content_type 5 ");
10501
10502                                         if (buf1) {
10503                                                 EM_DEBUG_LOG(">>>>emstorage_add_content_type 6 ");
10504                                                 strncat(buf1, CONTENT_DATA, strlen(CONTENT_DATA));
10505
10506                                                 EM_DEBUG_LOG(">>>>> BUF 1 [ %s ] ", buf1);
10507
10508                                                 strncat(buf1, low_char_set, strlen(low_char_set));
10509
10510                                                 EM_DEBUG_LOG(">>>> HTML TAG DATA  [ %s ] ", buf1);
10511
10512
10513                                         /* 1. Create a temporary file name */
10514                                         if (!_get_temp_file_name(&temp_file_name, &err)) {
10515                                                         EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed - %d", err);
10516                                                         if (err_code != NULL) *err_code = err;
10517                                                         EM_SAFE_FREE(temp_file_name);
10518                                                         goto FINISH_OFF;
10519                                         }
10520                                         EM_DEBUG_LOG(">>>>>>> TEMP APPEND FILE PATH [ %s ] ", temp_file_name);
10521                                         
10522                                         /* Open the Temp file in Append mode */
10523                                         if (!(fp_dest = fopen(temp_file_name, "ab"))) {
10524                                                 EM_DEBUG_EXCEPTION(" fopen failed - %s", temp_file_name);
10525                                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10526                                                 goto FINISH_OFF;
10527                                         }
10528
10529                                         /* 2. write the Latest data */
10530                                         nwritten = fwrite(buf1, data_count_to_written-1, 1, fp_dest);
10531
10532                                         if (nwritten > 0) {
10533                                                 EM_DEBUG_LOG(" Latest Data  : [%d ] bytes written ", nwritten);
10534                                                 nwritten = 0;
10535                                                 /* 3. Append old data */
10536                                                 nwritten = fwrite(buf, nread-1, 1, fp_dest);
10537
10538                                                 if (nwritten <= 0) {
10539                                                         EM_DEBUG_EXCEPTION(" Error Occured while writing Old data : [%d ] bytes written ", nwritten);
10540                                                         error = EMAIL_ERROR_SYSTEM_FAILURE;
10541                                                         goto FINISH_OFF;
10542                                                 }
10543                                                 else {
10544                                                         EM_DEBUG_LOG(">>>> OLD data appended [ %d ] ", nwritten);
10545
10546                                                         if (!emstorage_move_file(temp_file_name, file_path, false, &err)) {
10547                                                                 EM_DEBUG_EXCEPTION(" emstorage_move_file failed - %d", err);
10548                                                                 goto FINISH_OFF;
10549                                                         }
10550                                                 }
10551                                                         
10552                                         }
10553                                         else {
10554                                                 EM_DEBUG_EXCEPTION(" Error Occured while writing New data : [%d ] bytes written ", nwritten);
10555                                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
10556                                                 goto FINISH_OFF;
10557                                         }
10558                                                         
10559                                         }
10560                 
10561                                 }
10562                                 EM_DEBUG_LOG(">>>>emstorage_add_content_type 15 ");
10563
10564         
10565                         }
10566                 }
10567
10568         ret = true;
10569 FINISH_OFF:
10570
10571         EM_SAFE_FREE(buf);
10572         EM_SAFE_FREE(buf1);
10573         EM_SAFE_FREE(low_char_set);
10574
10575         if (fp_src != NULL) {
10576                 fclose(fp_src);
10577                 fp_src = NULL;
10578         }
10579         
10580         if (fp_dest != NULL) {
10581                 fclose(fp_dest);
10582                 fp_dest = NULL;
10583         }
10584         EM_DEBUG_FUNC_END("ret [%d]", ret);
10585         return ret;
10586
10587 }
10588
10589 INTERNAL_FUNC int emstorage_move_file(char *src_file, char *dst_file, int sync_status, int *err_code)
10590 {
10591         EM_DEBUG_FUNC_BEGIN("src_file[%p], dst_file[%p], err_code[%p]", src_file, dst_file, err_code);
10592         
10593         int ret = false;
10594         int error = EMAIL_ERROR_NONE;
10595         
10596         if (src_file == NULL || dst_file == NULL)  {
10597                 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
10598                 error = EMAIL_ERROR_INVALID_PARAM;
10599                 goto FINISH_OFF;
10600         }
10601         
10602         EM_DEBUG_LOG("src_file[%s], dst_file[%s]", src_file, dst_file);
10603
10604         if (strcmp(src_file, dst_file) != 0) {
10605                 if (rename(src_file, dst_file) != 0) {
10606                         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() */
10607                                 /*  does not work across different mount points, even if the same file system is mounted on both.)       */
10608                                 EM_DEBUG_LOG("oldpath and newpath are not on the same mounted file system.");
10609                                 if (!emstorage_copy_file(src_file, dst_file, sync_status, &error)) {
10610                                         EM_DEBUG_EXCEPTION("emstorage_copy_file failed - %d", error);
10611                                         goto FINISH_OFF;
10612                                 }
10613                                 remove(src_file);
10614                                 EM_DEBUG_LOG("src[%s] removed", src_file);
10615                 
10616                         }
10617                         else  {
10618                                 if (errno == ENOENT)  {
10619                                         struct stat temp_file_stat;
10620                                         if (stat(src_file, &temp_file_stat) < 0)
10621                                                 EM_DEBUG_EXCEPTION("no src file found [%s]", src_file);
10622                                         if (stat(dst_file, &temp_file_stat) < 0)
10623                                                 EM_DEBUG_EXCEPTION("no dst file found [%s]", src_file);
10624
10625                                         EM_DEBUG_EXCEPTION("no file found [%d]", errno);
10626                                         error = EMAIL_ERROR_FILE_NOT_FOUND;
10627                                         goto FINISH_OFF;
10628                                         
10629                                 }
10630                                 else  {
10631                                         EM_DEBUG_EXCEPTION("rename failed [%d]", errno);
10632                                         error = EMAIL_ERROR_SYSTEM_FAILURE;
10633                                         goto FINISH_OFF;
10634                                 }
10635                         }
10636                 }
10637         }
10638         else {
10639                 EM_DEBUG_LOG("src[%s] = dst[%d]", src_file, dst_file);
10640         }
10641         
10642         ret = true;
10643
10644 FINISH_OFF:
10645         if (err_code != NULL)
10646                 *err_code = error;
10647
10648         EM_DEBUG_FUNC_END("ret [%d]", ret);
10649         return ret;
10650 }
10651
10652 INTERNAL_FUNC int emstorage_delete_file(char *src_file, int *err_code)
10653 {
10654         EM_DEBUG_FUNC_BEGIN("src_file[%p], err_code[%p]", src_file, err_code);
10655         
10656         int ret = false;
10657         int error = EMAIL_ERROR_NONE;
10658         
10659         if (src_file == NULL) {
10660                 EM_DEBUG_EXCEPTION(" src_file[%p]", src_file);
10661                 
10662                 error = EMAIL_ERROR_INVALID_PARAM;
10663                 goto FINISH_OFF;
10664         }
10665         
10666         if (remove(src_file) != 0) {
10667                 if (errno != ENOENT) {
10668                         EM_DEBUG_EXCEPTION(" remove failed - %d", errno);
10669                         
10670                         error = EMAIL_ERROR_SYSTEM_FAILURE;
10671                         goto FINISH_OFF;
10672                 }
10673                 else {
10674                         EM_DEBUG_EXCEPTION(" no file found...");
10675                         
10676                         error = EMAIL_ERROR_FILE_NOT_FOUND;
10677                 }
10678         }
10679         
10680         ret = true;
10681         
10682 FINISH_OFF:
10683         if (err_code != NULL)
10684                 *err_code = error;
10685
10686         EM_DEBUG_FUNC_END("ret [%d]", ret);
10687         return ret;
10688 }
10689
10690
10691 INTERNAL_FUNC int emstorage_delete_dir(char *src_dir, int *err_code)
10692 {
10693         EM_DEBUG_FUNC_BEGIN("src_dir[%p], err_code[%p]", src_dir, err_code);
10694         
10695         if (src_dir == NULL) {
10696                 EM_DEBUG_EXCEPTION(" src_dir[%p]", src_dir);
10697                 
10698                 if (err_code != NULL)
10699                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10700                 return false;
10701         }
10702         
10703         int error = EMAIL_ERROR_NONE;
10704         
10705         DIR *dirp;
10706         struct dirent *dp;
10707         struct stat sbuf;
10708         char buf[512];
10709         
10710         dirp = opendir(src_dir);
10711         
10712         if (dirp == NULL)  {
10713                 if (errno == ENOENT)  {
10714                         EM_DEBUG_EXCEPTION("directory[%s] does not exist...", src_dir);
10715                         if (err_code != NULL)
10716                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
10717                         return true;
10718                 }
10719                 else  {
10720                         EM_DEBUG_EXCEPTION("opendir failed - %d", errno);
10721                         if (err_code != NULL)
10722                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
10723                         return false;
10724                 }
10725         }
10726
10727         while ((dp=readdir(dirp)))  {
10728                 if (strncmp(dp->d_name, ".", strlen(".")) == 0 || strncmp(dp->d_name, "..", strlen("..")) == 0)
10729                         continue;
10730                 
10731                 SNPRINTF(buf, sizeof(buf), "%s/%s", src_dir, dp->d_name);
10732                 
10733                 if (lstat(buf, &sbuf) == 0 || stat(buf, &sbuf) == 0) {
10734                         /*  check directory */
10735                         if ((sbuf.st_mode & S_IFMT) == S_IFDIR)  {      /*  directory */
10736                                 /*  recursive call */
10737                                 if (!emstorage_delete_dir(buf, &error)) {
10738                                         closedir(dirp); 
10739                                         if (err_code != NULL)
10740                                                 *err_code = error;
10741                                         return false;
10742                                 }
10743                         }
10744                         else  { /*  file */
10745                                 if (remove(buf) < 0)  {
10746                                         EM_DEBUG_EXCEPTION("remove failed - %s", buf);
10747                                         closedir(dirp);
10748                                         if (err_code != NULL)
10749                                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
10750                                         return false;
10751                                 }
10752                         }
10753                 }
10754                 else 
10755                         EM_DEBUG_EXCEPTION("content does not exist...");
10756         }
10757         
10758         closedir(dirp);
10759         
10760         EM_DEBUG_LOG("remove direcotory [%s]", src_dir);
10761         
10762         /* EM_DEBUG_FUNC_BEGIN(); */
10763         
10764         if (remove(src_dir) < 0)  {
10765                 EM_DEBUG_EXCEPTION("remove failed [%s]", src_dir);
10766                 if (err_code != NULL)
10767                         *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
10768                 return false;
10769         }
10770         
10771         if (err_code != NULL)
10772                 *err_code = error;
10773         
10774         return true;
10775 }
10776
10777 /* faizan.h@samsung.com */
10778 INTERNAL_FUNC int emstorage_update_server_uid(char *old_server_uid, char *new_server_uid, int *err_code)
10779 {
10780         EM_DEBUG_FUNC_BEGIN("new_server_uid[%s], old_server_uid[%s]", new_server_uid, old_server_uid);
10781         int rc, ret = false;
10782         int error = EMAIL_ERROR_NONE;
10783         char sql_query_string[QUERY_SIZE] = {0, };
10784
10785         int transaction = true;
10786         
10787         if (!old_server_uid || !new_server_uid) {       
10788                 EM_DEBUG_EXCEPTION("Invalid parameters");
10789                 error = EMAIL_ERROR_INVALID_PARAM;
10790                 return false;
10791         }
10792         sqlite3 *local_db_handle = emstorage_get_db_connection();
10793
10794         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10795         
10796         SNPRINTF(sql_query_string, sizeof(sql_query_string),
10797                  "UPDATE mail_tbl SET server_mail_id=\'%s\' WHERE server_mail_id=%s ", new_server_uid, old_server_uid);
10798         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10799
10800         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
10801                 ("sqlite3_exec fail:%d", rc));
10802         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10803                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10804         
10805         ret = true;
10806           
10807 FINISH_OFF:
10808         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10809         _DISCONNECT_DB;
10810
10811         if (err_code != NULL)
10812                 *err_code = error;
10813         
10814         EM_DEBUG_FUNC_END("ret [%d]", ret);
10815         return ret;
10816         
10817 }
10818
10819 INTERNAL_FUNC int emstorage_update_read_mail_uid(int mail_id, char *new_server_uid, char *mbox_name, int *err_code)
10820 {
10821         EM_DEBUG_FUNC_BEGIN("mail_id[%d], new_server_uid[%s], mbox_name[%s]", mail_id, new_server_uid, mbox_name);
10822
10823         int rc, ret = false;
10824         int error = EMAIL_ERROR_NONE;
10825         char sql_query_string[QUERY_SIZE] = {0, };
10826
10827         int transaction = true;
10828         
10829         if (!mail_id || !new_server_uid || !mbox_name)  {               
10830                 EM_DEBUG_EXCEPTION("Invalid parameters");
10831                 error = EMAIL_ERROR_INVALID_PARAM;
10832                 return false;
10833         }
10834         sqlite3 *local_db_handle = emstorage_get_db_connection();
10835         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10836
10837         
10838         SNPRINTF(sql_query_string, sizeof(sql_query_string),
10839                  "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);
10840         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10841         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10842                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10843         ret     = true;
10844           
10845 FINISH_OFF:
10846
10847         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10848         _DISCONNECT_DB;
10849
10850         if (err_code != NULL)
10851                 *err_code = error;
10852         
10853         EM_DEBUG_FUNC_END("ret [%d]", ret);
10854         return ret;
10855         
10856 }
10857
10858
10859 int emstorage_get_latest_unread_mailid(int account_id, int *mail_id, int *err_code)
10860 {
10861         EM_DEBUG_FUNC_BEGIN();
10862
10863         if ((!mail_id) ||(account_id <= 0 &&  account_id != -1)) {
10864                 EM_DEBUG_EXCEPTION(" mail_id[%p], account_id[%d] ", mail_id, account_id);
10865                 if (err_code != NULL)
10866                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10867                 return false;
10868         }       
10869
10870         int ret = false;
10871         int rc = -1;
10872         int error = EMAIL_ERROR_NONE;
10873         DB_STMT hStmt = NULL;
10874         int count = 0;
10875         int mailid = 0;
10876         int transaction = false;
10877         char sql_query_string[QUERY_SIZE] = {0, };
10878
10879         sqlite3 *local_db_handle = emstorage_get_db_connection();
10880         EMSTORAGE_START_READ_TRANSACTION(transaction);
10881
10882         memset(sql_query_string, 0x00, sizeof(sql_query_string));
10883
10884         if (account_id == -1)
10885                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl WHERE flags_seen_field = 0 ORDER BY mail_id DESC");
10886         else
10887                 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);
10888                 
10889         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
10890         EM_DEBUG_LOG("  sqlite3_prepare hStmt = %p", hStmt);
10891         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10892                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10893
10894         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10895         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10896                 ("sqlite3_step fail:%d", rc));
10897
10898         char **result;
10899
10900         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
10901         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
10902                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10903
10904         sqlite3_free_table(result);
10905         if (count == 0)  {
10906                 EM_DEBUG_EXCEPTION("no Mails found...");
10907                 ret = false;
10908                 error= EMAIL_ERROR_MAIL_NOT_FOUND;
10909                 goto FINISH_OFF;
10910         }
10911         
10912                 _get_stmt_field_data_int(hStmt, &mailid, 0);
10913
10914         ret = true;
10915
10916 FINISH_OFF:
10917         if (hStmt != NULL)  {
10918                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
10919
10920                 rc = sqlite3_finalize(hStmt);
10921                 if (rc != SQLITE_OK)  {
10922                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
10923                         
10924                         error = EMAIL_ERROR_DB_FAILURE;
10925                 }
10926         }
10927         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10928         _DISCONNECT_DB;
10929
10930         if (err_code != NULL)
10931                 *err_code = error;
10932
10933         EM_DEBUG_FUNC_END("ret [%d]", ret);
10934         return ret;
10935 }
10936
10937 int setting_system_command(const char *command)
10938
10939  {
10940         int pid = 0, status = 0;
10941         char *const environ[] = { NULL };
10942
10943         if (command == 0)
10944                 return 1;
10945
10946         pid = fork();
10947
10948         if (pid == -1)
10949                 return -1;
10950
10951         if (pid == 0) {
10952                 char *argv[4];
10953                 
10954                 argv[0] = "sh";
10955                 argv[1] = "-c";
10956                 argv[2] = (char *)command;
10957                 argv[3] = 0;
10958
10959                 execve("/bin/sh", argv, environ);
10960                 abort();
10961         }
10962         do{
10963                 if (waitpid(pid, &status, 0) == -1) {
10964                         if (errno != EINTR)
10965                                 return -1;
10966                 }
10967                 else {
10968                         return status;
10969                 }
10970         } while (1);
10971
10972         return 0;
10973 }
10974
10975
10976 INTERNAL_FUNC int emstorage_mail_get_total_diskspace_usage(unsigned long *total_usage,  int transaction, int *err_code)
10977 {
10978         EM_DEBUG_FUNC_BEGIN("total_usage[%p],  transaction[%d], err_code[%p]", total_usage, transaction, err_code);
10979
10980         if (!total_usage) {
10981                 EM_DEBUG_EXCEPTION("total_usage[%p]", total_usage);
10982                 
10983                 if (err_code != NULL)
10984                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10985                 return false;
10986         }
10987
10988         int   ret = false;
10989         int   error = EMAIL_ERROR_NONE;
10990         char  syscmd[256] = {0, };
10991         char  line[256] = {0, };
10992         char *line_from_file = NULL;
10993         FILE *fp = NULL;
10994         unsigned long total_diskusage = 0;
10995
10996         SNPRINTF(syscmd, sizeof(syscmd), "touch %s", SETTING_MEMORY_TEMP_FILE_PATH);
10997         if (setting_system_command(syscmd) == -1) {
10998                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
10999
11000                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11001                 goto FINISH_OFF;                
11002         }
11003
11004         SNPRINTF(syscmd, sizeof(syscmd), "du -hsk %s > %s", EMAILPATH, SETTING_MEMORY_TEMP_FILE_PATH);
11005         EM_DEBUG_LOG(" cmd : %s", syscmd);
11006         if (setting_system_command(syscmd) == -1) {
11007                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : Setting > Memory] System Command [%s] is failed", syscmd);
11008                 
11009                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11010                 goto FINISH_OFF;        
11011         }
11012
11013         fp = fopen(SETTING_MEMORY_TEMP_FILE_PATH, "r");
11014         if (fp == NULL) {
11015                 perror(SETTING_MEMORY_TEMP_FILE_PATH);
11016                 
11017                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11018                 goto FINISH_OFF;        
11019         }
11020  
11021         line_from_file = fgets(line, sizeof(line), fp);
11022
11023         if(line_from_file == NULL) {
11024                 EM_DEBUG_EXCEPTION("fgets failed");
11025                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11026                 goto FINISH_OFF;
11027         }
11028         total_diskusage = strtoul(line, NULL, 10);
11029  
11030         fclose(fp);
11031
11032         memset(syscmd, 0, sizeof(syscmd));
11033         SNPRINTF(syscmd, sizeof(syscmd), "rm -f %s", SETTING_MEMORY_TEMP_FILE_PATH);
11034         if (setting_system_command(syscmd) == -1) {
11035                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage :  [Setting > Memory] System Command [%s] is failed", syscmd);
11036                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11037                 goto FINISH_OFF;        
11038         }
11039
11040         EM_DEBUG_LOG("[Setting > Memory] @@@@@ Size of Directory [%s] is %ld KB", EMAILPATH, total_diskusage);
11041
11042         ret = true;
11043
11044 FINISH_OFF:     
11045         if (err_code != NULL)
11046                 *err_code = error;
11047
11048         if (ret)
11049                 *total_usage = total_diskusage;
11050         else
11051                 *total_usage = 0;
11052
11053         EM_DEBUG_FUNC_END("ret [%d]", ret);
11054         return ret;
11055 }
11056
11057
11058 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)
11059 {
11060         DB_STMT hStmt = NULL;
11061         int ret = false;
11062         int error = EMAIL_ERROR_NONE;
11063         int rc = 0;
11064         char sql_query_string[QUERY_SIZE] = {0, };
11065
11066         SNPRINTF(sql_query_string, sizeof(sql_query_string),
11067                 "INSERT INTO mail_tbl VALUES "
11068                 "( ?" /*  mail_id */
11069                 ", ?" /*  account_id */
11070                 ", ?" /*  mailbox_name */
11071                 ", ?" /*  mail_size */
11072                 ", ?" /*  server_mail_status */
11073                 ", ?" /*  server_mailbox_name */
11074                 ", ?" /*  server_mail_id */
11075                 ", ?" /*  full_address_from */
11076                 ", ?" /*  full_address_reply */
11077                 ", ?" /*  full_address_to */
11078                 ", ?" /*  full_address_cc */
11079                 ", ?" /*  full_address_bcc */
11080                 ", ?" /*  full_address_return */
11081                 ", ?" /*  subject */
11082                 ", ?" /*  body_download_status */
11083                 ", ?" /*  file_path_plain */
11084                 ", ?" /*  file_path_html */
11085                 ", ?" /*  date_time */
11086                 ", ?" /*  flags_seen_field */
11087                 ", ?" /*  flags_deleted_field */
11088                 ", ?" /*  flags_flagged_field */
11089                 ", ?" /*  flags_answered_field */
11090                 ", ?" /*  flags_recent_field */
11091                 ", ?" /*  flags_draft_field */
11092                 ", ?" /*  flags_forwarded_field */
11093                 ", ?" /*  DRM_status */
11094                 ", ?" /*  priority */
11095                 ", ?" /*  save_status */
11096                 ", ?" /*  lock_status */
11097                 ", ?" /*  message_id */
11098                 ", ?" /*  report_status */
11099                 ", ?" /*  email_address_sender */
11100                 ", ?" /*  email_address_recipient */
11101                 ", ?" /*  attachment_count */
11102                 ", ?" /*  inline_content_count */
11103                 ", ?" /*  preview_text */
11104                 ", ?" /*  thread_id */
11105                 ", ?" /*  mailbox_type */
11106                 ", ?" /*  alias_sender */
11107                 ", ?" /*  alias_recipient */
11108                 ", ?" /*  thread_item_count */
11109                 ", ?" /*  meeting_request_status */
11110                 ", ?" /*  message_class */
11111                 ", ?" /*  digest_type */
11112                 ", ?" /*  smime_type */
11113                 ")");
11114
11115         int transaction = true;
11116         sqlite3 *local_db_handle = emstorage_get_db_connection();
11117
11118         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
11119  
11120         
11121         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
11122         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11123                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11124
11125         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_id);
11126         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, account_id);
11127         _bind_stmt_field_data_string(hStmt, MAILBOX_NAME_IDX_IN_MAIL_TBL, "OUTBOX", 0, MAILBOX_LEN_IN_MAIL_TBL);
11128         _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, EMAIL_MAILBOX_TYPE_OUTBOX);
11129         _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, "save test - long", 1, SUBJECT_LEN_IN_MAIL_TBL);
11130         _bind_stmt_field_data_string(hStmt, DATETIME_IDX_IN_MAIL_TBL, "20100316052908", 0, DATETIME_LEN_IN_MAIL_TBL);
11131         _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, 0);
11132         _bind_stmt_field_data_string(hStmt, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL, "", 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
11133         _bind_stmt_field_data_string(hStmt, SERVER_MAIL_ID_IDX_IN_MAIL_TBL, "", 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
11134         _bind_stmt_field_data_string(hStmt, MESSAGE_ID_IDX_IN_MAIL_TBL, "", 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
11135         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL, "<test08@streaming.s3glab.net>", 1, FROM_LEN_IN_MAIL_TBL);
11136         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL, "", 1, REPLY_TO_LEN_IN_MAIL_TBL);
11137         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL, full_address_to, 1, TO_LEN_IN_MAIL_TBL);
11138         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL, full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
11139         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL, full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
11140         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL, "", 1, RETURN_PATH_LEN_IN_MAIL_TBL);
11141         _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);
11142         _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);
11143         _bind_stmt_field_data_string(hStmt, ALIAS_SENDER_IDX_IN_MAIL_TBL, "send_alias", 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
11144         _bind_stmt_field_data_string(hStmt, ALIAS_RECIPIENT_IDX_IN_MAIL_TBL, "recipient_alias", 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
11145         _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, 1);
11146         _bind_stmt_field_data_string(hStmt, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL, "/opt/system/rsr/email/.emfdata/7/348/UTF-8", 0, TEXT_1_LEN_IN_MAIL_TBL);
11147         _bind_stmt_field_data_string(hStmt, FILE_PATH_HTML_IDX_IN_MAIL_TBL, "", 0, TEXT_2_LEN_IN_MAIL_TBL);
11148         _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, 4);
11149         _bind_stmt_field_data_char(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, 0);
11150         _bind_stmt_field_data_char(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, 0);
11151         _bind_stmt_field_data_char(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, 0);
11152         _bind_stmt_field_data_char(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, 0);
11153         _bind_stmt_field_data_char(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, 0);
11154         _bind_stmt_field_data_char(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, 0);
11155         _bind_stmt_field_data_char(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, 0);
11156         _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, 0);
11157         _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, 0);
11158         _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, 0);
11159         _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, 0);
11160         _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, 0);
11161         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
11162         _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, 0);
11163         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
11164         _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, 0);
11165         _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, 0);
11166         _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, "preview body", 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
11167         _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, 0);
11168         _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, 0);
11169         _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, 0);
11170         _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, 0);
11171
11172         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11173         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
11174                 ("sqlite3_step fail:%d", rc));
11175         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11176                 ("sqlite3_step fail:%d", rc));
11177         ret = true;
11178
11179 FINISH_OFF:
11180         if (hStmt != NULL)  {
11181                 rc = sqlite3_finalize(hStmt);
11182                 if (rc != SQLITE_OK)  {
11183                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
11184                         error = EMAIL_ERROR_DB_FAILURE;
11185                 }
11186         }
11187  
11188         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
11189         _DISCONNECT_DB;
11190         
11191         if (err_code != NULL)
11192                 *err_code = error;
11193
11194         EM_DEBUG_FUNC_END("ret [%d]", ret);
11195         return ret;
11196 }
11197
11198
11199 INTERNAL_FUNC int emstorage_get_max_mail_count()
11200 {
11201         return EMAIL_MAIL_MAX_COUNT;
11202 }
11203
11204 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)
11205 {
11206         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);
11207         EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
11208         int      rc = 0, query_size = 0, query_size_account = 0;
11209         int      account_id = 0;
11210         int      err_code = EMAIL_ERROR_NONE;
11211         int      count = 0, result_thread_id = -1, latest_mail_id_in_thread = -1;
11212         time_t   latest_date_time = 0;
11213         time_t   date_time = 0;
11214         char    *mailbox_name = NULL, *subject = NULL;
11215         char    *sql_query_string = NULL, *sql_account = NULL;
11216         char    *sql_format = "SELECT thread_id, date_time, mail_id FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_type NOT IN (3, 5, 7, 8)";
11217         char    *sql_format_account = " AND account_id = %d ";
11218         char    *sql_format_order_by = " ORDER BY date_time DESC ";
11219         char   **result = NULL;
11220         char     stripped_subject[1025];
11221         sqlite3 *local_db_handle = emstorage_get_db_connection();
11222
11223         EM_IF_NULL_RETURN_VALUE(mail_tbl, EMAIL_ERROR_INVALID_PARAM);
11224         EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
11225         EM_IF_NULL_RETURN_VALUE(result_latest_mail_id_in_thread, EMAIL_ERROR_INVALID_PARAM);
11226         EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
11227
11228         if (mail_tbl->mailbox_type == EMAIL_MAILBOX_TYPE_TRASH ||
11229             mail_tbl->mailbox_type == EMAIL_MAILBOX_TYPE_SPAMBOX ||
11230             mail_tbl->mailbox_type == EMAIL_MAILBOX_TYPE_ALL_EMAILS) {
11231                 EM_DEBUG_LOG("the mail in trash, spambox, all email could not be thread mail.");
11232                 goto FINISH_OFF;
11233         }
11234
11235         account_id   = mail_tbl->account_id;
11236         mailbox_name = mail_tbl->mailbox_name;
11237         subject      = mail_tbl->subject;
11238         date_time    = mail_tbl->date_time;
11239         
11240         EM_DEBUG_LOG("subject : %s", subject);
11241
11242         if (em_find_pos_stripped_subject_for_thread_view(subject, stripped_subject) != EMAIL_ERROR_NONE)        {
11243                 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view  is failed");
11244                 err_code =  EMAIL_ERROR_UNKNOWN;
11245                 result_thread_id = -1;
11246                 goto FINISH_OFF;
11247         }
11248
11249         if (strlen(stripped_subject) < 2) {
11250                 result_thread_id = -1;
11251                 goto FINISH_OFF;
11252         }
11253         
11254         EM_DEBUG_LOG("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s", strlen(stripped_subject), stripped_subject);
11255         
11256         if (account_id > 0)     {
11257                 query_size_account = 3 + strlen(sql_format_account);
11258                 sql_account = malloc(query_size_account);
11259                 if (sql_account == NULL) {
11260                         EM_DEBUG_EXCEPTION("malloc for sql_account  is failed %d", query_size_account);
11261                         err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
11262                         goto FINISH_OFF;
11263                 }
11264                 snprintf(sql_account, query_size_account, sql_format_account, account_id);
11265         }
11266         
11267         query_size = strlen(sql_format) + strlen(stripped_subject) + 50 + query_size_account + strlen(sql_format_order_by); /*  + query_size_mailbox; */
11268         
11269         sql_query_string = malloc(query_size);
11270         
11271         if (sql_query_string == NULL) {
11272                 EM_DEBUG_EXCEPTION("malloc for sql  is failed %d", query_size);
11273                 err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
11274                 goto FINISH_OFF;
11275         }
11276
11277         sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject);
11278
11279         if (account_id > 0)
11280                 strcat(sql_query_string, sql_account);
11281
11282         strcat(sql_query_string, sql_format_order_by);
11283         strcat(sql_query_string, ";");
11284
11285         EM_DEBUG_LOG("Query : %s", sql_query_string);
11286
11287         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
11288
11289         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11290                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11291
11292         EM_DEBUG_LOG("Result rows count : %d", count);
11293
11294         if (count == 0)
11295                 result_thread_id = -1;
11296         else {
11297                 _get_table_field_data_int   (result, &result_thread_id, 3);
11298                 _get_table_field_data_time_t(result, &latest_date_time, 4);
11299                 _get_table_field_data_int   (result, &latest_mail_id_in_thread, 5);
11300
11301                 if (latest_date_time < mail_tbl->date_time)
11302                         *result_latest_mail_id_in_thread = latest_mail_id_in_thread;
11303                 else 
11304                         *result_latest_mail_id_in_thread = mail_tbl->mail_id;
11305                 EM_DEBUG_LOG("latest_mail_id_in_thread [%d], mail_id [%d]", latest_mail_id_in_thread, mail_tbl->mail_id);
11306         }
11307
11308 FINISH_OFF:
11309         *thread_id = result_thread_id;
11310         *thread_item_count = count;
11311         
11312         EM_DEBUG_LOG("Result thread id : %d", *thread_id);
11313         EM_DEBUG_LOG("Result count : %d", *thread_item_count);
11314         EM_DEBUG_LOG("err_code : %d", err_code);
11315
11316         EM_SAFE_FREE(sql_account);
11317         EM_SAFE_FREE(sql_query_string);
11318
11319         sqlite3_free_table(result);
11320         
11321         EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
11322         
11323         return err_code;
11324 }
11325
11326
11327 INTERNAL_FUNC int emstorage_get_thread_information(int thread_id, emstorage_mail_tbl_t** mail_tbl, int transaction, int *err_code)
11328 {
11329         EM_DEBUG_FUNC_BEGIN();
11330         
11331         int count = 0, ret = false;
11332         int error = EMAIL_ERROR_NONE;
11333         emstorage_mail_tbl_t *p_data_tbl = NULL;
11334         char conditional_clause[QUERY_SIZE] = {0, };
11335
11336         EM_IF_NULL_RETURN_VALUE(mail_tbl, false);
11337
11338         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE thread_id = %d AND thread_item_count > 0", thread_id);
11339         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
11340
11341         if(!emstorage_query_mail_tbl(conditional_clause, transaction, &p_data_tbl, &count, &error)) {
11342                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
11343                 goto FINISH_OFF;
11344         }
11345
11346         if(p_data_tbl)
11347                 EM_DEBUG_LOG("thread_id : %d, thread_item_count : %d", p_data_tbl[0].thread_id, p_data_tbl[0].thread_item_count);
11348         
11349         ret = true;
11350         
11351 FINISH_OFF:
11352         if (ret == true) 
11353                 *mail_tbl = p_data_tbl;
11354         else if (p_data_tbl != NULL)
11355                 emstorage_free_mail(&p_data_tbl, 1, NULL);
11356         
11357         if (err_code != NULL)
11358                 *err_code = error;
11359
11360         EM_DEBUG_FUNC_END("ret [%d]", ret);
11361         return ret;
11362 }
11363
11364
11365 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)
11366 {
11367         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]"
11368                 , account_id , mailbox_name , search_type , search_value , sorting , sender_list, sender_count, err_code);
11369         
11370         if ((!sender_list) ||(!sender_count)) {
11371                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
11372                 if (err_code != NULL)
11373                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11374                 return false;
11375         }
11376
11377         int rc = -1, ret = false;
11378         int error = EMAIL_ERROR_NONE;
11379         int count = 0;
11380         int i, col_index = 0;
11381         int read_count = 0;
11382         email_sender_list_t *p_sender_list = NULL;
11383         char sql_query_string[QUERY_SIZE] = {0, };
11384         char **result = NULL;
11385         sqlite3 *local_db_handle = emstorage_get_db_connection();
11386
11387         SNPRINTF(sql_query_string, sizeof(sql_query_string),
11388                 "SELECT email_address_sender, alias_sender, COUNT(email_address_sender), SUM(flags_seen_field = 1) "
11389                 "FROM mail_tbl ");
11390
11391         /*  mailbox_name */
11392         if (mailbox_name)
11393                 SNPRINTF(sql_query_string + strlen(sql_query_string), QUERY_SIZE-(strlen(sql_query_string)+1), " WHERE UPPER(mailbox_name) = UPPER('%s') ", mailbox_name);
11394         else    /*  NULL  means all mailbox_name. but except for trash(3), spambox(5), all emails(for GMail, 7) */
11395                 SNPRINTF(sql_query_string + strlen(sql_query_string), QUERY_SIZE-(strlen(sql_query_string)+1), " WHERE mailbox_type not in (3, 5, 7, 8) ");
11396
11397         /*  account id */
11398         /*  '0' (ALL_ACCOUNT) means all account */
11399         if (account_id > ALL_ACCOUNT)
11400                 SNPRINTF(sql_query_string + strlen(sql_query_string), QUERY_SIZE-(strlen(sql_query_string)+1), " AND account_id = %d ", account_id);
11401
11402         if (search_value) {
11403                 switch (search_type) {
11404                         case EMAIL_SEARCH_FILTER_SUBJECT:
11405                                 SNPRINTF(sql_query_string + strlen(sql_query_string), QUERY_SIZE-(strlen(sql_query_string)+1),
11406                                         " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\')) ", search_value);
11407                                 break;
11408                         case EMAIL_SEARCH_FILTER_SENDER:
11409                                 SNPRINTF(sql_query_string + strlen(sql_query_string), QUERY_SIZE-(strlen(sql_query_string)+1),
11410                                         " AND  ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
11411                                         ") ", search_value);
11412                                 break;
11413                         case EMAIL_SEARCH_FILTER_RECIPIENT:
11414                                 SNPRINTF(sql_query_string + strlen(sql_query_string), QUERY_SIZE-(strlen(sql_query_string)+1),
11415                                         " AND ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "    
11416                                         "       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
11417                                         "       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
11418                                         ") ", search_value, search_value, search_value);
11419                                 break;
11420                         case EMAIL_SEARCH_FILTER_ALL:
11421                                 SNPRINTF(sql_query_string + strlen(sql_query_string), QUERY_SIZE-(strlen(sql_query_string)+1),
11422                                         " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') "
11423                                         "       OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
11424                                         "                       OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
11425                                         "                       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
11426                                         "                       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
11427                                         "               ) "
11428                                         "       )"
11429                                         ")", search_value, search_value, search_value, search_value, search_value);
11430                                 break;
11431                 }
11432         }
11433
11434
11435         /*  sorting option is not available now. The order of sender list is ascending order by display name */
11436         SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1),
11437                 "GROUP BY email_address_sender "
11438                 "ORDER BY UPPER(alias_sender) ");
11439
11440         EM_DEBUG_LOG("query[%s]", sql_query_string);
11441         
11442         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
11443         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = 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("Count of Sender [%d]", count);
11447         
11448         if (!(p_sender_list = (email_sender_list_t*)em_malloc(sizeof(email_sender_list_t) * count))) {
11449                 EM_DEBUG_EXCEPTION("em_malloc failed...");
11450                 error = EMAIL_ERROR_OUT_OF_MEMORY;
11451                 goto FINISH_OFF;
11452         }
11453
11454         col_index = 4;
11455
11456         EM_DEBUG_LOG(">>>> DATA ASSIGN START >>");      
11457         for (i = 0; i < count; i++)  {
11458                 _get_table_field_data_string(result, &(p_sender_list[i].address), 1, col_index++);
11459                 _get_table_field_data_string(result, &(p_sender_list[i].display_name), 1, col_index++);
11460                 _get_table_field_data_int(result, &(p_sender_list[i].total_count), col_index++);
11461                 _get_table_field_data_int(result, &(read_count), col_index++);
11462                 p_sender_list[i].unread_count = p_sender_list[i].total_count - read_count;              /*  unread count = total - read          */
11463         }
11464         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >>", count);
11465
11466         sqlite3_free_table(result);
11467         result = NULL;
11468
11469         ret = true;
11470         
11471 FINISH_OFF:
11472         if (ret == true)  {
11473                 *sender_list = p_sender_list;
11474                 *sender_count = count;
11475                 EM_DEBUG_LOG(">>>> COUNT : %d >>", count);
11476         }
11477         else if (p_sender_list != NULL)
11478                 emstorage_free_sender_list(&p_sender_list, count);
11479                 
11480         _DISCONNECT_DB;
11481         
11482         if (err_code != NULL)
11483                 *err_code = error;
11484
11485         EM_DEBUG_FUNC_END("ret [%d]", ret);
11486         return ret;
11487 }
11488
11489 INTERNAL_FUNC int emstorage_free_sender_list(email_sender_list_t **sender_list, int count)
11490 {
11491         EM_DEBUG_FUNC_BEGIN("sender_list[%p], count[%d]", sender_list, count);
11492
11493         int err = EMAIL_ERROR_NONE;
11494         
11495         if (count > 0)  {
11496                 if (!sender_list || !*sender_list)  {
11497                         EM_DEBUG_EXCEPTION("sender_list[%p], count[%d]", sender_list, count);
11498                         err = EMAIL_ERROR_INVALID_PARAM;
11499                         return err;
11500                 }
11501                 
11502                 email_sender_list_t* p = *sender_list;
11503                 int i = 0;
11504                 
11505                 for (; i < count; i++)  {
11506                         EM_SAFE_FREE(p[i].address);
11507                         EM_SAFE_FREE(p[i].display_name);
11508                 }
11509                 
11510                 EM_SAFE_FREE(p); 
11511                 *sender_list = NULL;
11512         }       
11513         
11514         return err;
11515 }
11516
11517
11518 INTERNAL_FUNC int emstorage_free_address_info_list(email_address_info_list_t **address_info_list)
11519 {
11520         EM_DEBUG_FUNC_BEGIN("address_info_list[%p]", address_info_list);
11521
11522         int err = EMAIL_ERROR_NONE;
11523         email_address_info_t *p_address_info = NULL;
11524         GList *list = NULL;
11525         GList *node = NULL;
11526         int i = 0;
11527         
11528         if (!address_info_list || !*address_info_list)  {
11529                 EM_DEBUG_EXCEPTION("address_info_list[%p]", address_info_list);
11530                 err = EMAIL_ERROR_INVALID_PARAM;
11531                 return err;
11532         }
11533
11534         /*  delete GLists */
11535         for (i = EMAIL_ADDRESS_TYPE_FROM; i <= EMAIL_ADDRESS_TYPE_BCC; i++) {
11536                 switch (i) {
11537                         case EMAIL_ADDRESS_TYPE_FROM:
11538                                 list = (*address_info_list)->from;
11539                                 break;
11540                         case EMAIL_ADDRESS_TYPE_TO:
11541                                 list = (*address_info_list)->to;
11542                                 break;
11543                         case EMAIL_ADDRESS_TYPE_CC:
11544                                 list = (*address_info_list)->cc;
11545                                 break;
11546                         case EMAIL_ADDRESS_TYPE_BCC:
11547                                 list = (*address_info_list)->bcc;
11548                                 break;
11549                 }
11550
11551                 /*  delete dynamic-allocated memory for each item */
11552                 node = g_list_first(list);
11553                 while (node != NULL) {
11554                         p_address_info = (email_address_info_t*)node->data;
11555                         EM_SAFE_FREE(p_address_info->address);
11556                         EM_SAFE_FREE(p_address_info->display_name);
11557                         EM_SAFE_FREE(node->data);
11558                         
11559                         node = g_list_next(node);
11560                 }
11561         }
11562
11563         EM_SAFE_FREE(*address_info_list); 
11564         *address_info_list = NULL;
11565
11566         EM_DEBUG_FUNC_END("err [%d]", err);
11567         return err;
11568 }
11569 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
11570
11571 INTERNAL_FUNC int emstorage_add_pbd_activity(email_event_partial_body_thd* local_activity, int *activity_id, int transaction, int *err_code)
11572 {
11573         EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
11574
11575         if (!local_activity || !activity_id) {
11576                 EM_DEBUG_EXCEPTION("local_activity[%p], transaction[%d], activity_id[%p], err_code[%p]", local_activity, activity_id, transaction, err_code);
11577                 if (err_code != NULL)
11578                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11579                 return false;
11580         }
11581
11582         int rc = -1;
11583         int ret = false;
11584         int error = EMAIL_ERROR_NONE;
11585         int i = 0;
11586
11587         char sql_query_string[QUERY_SIZE] = {0, };
11588         DB_STMT hStmt = NULL;
11589         sqlite3 *local_db_handle = emstorage_get_db_connection();
11590         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
11591         memset(sql_query_string, 0x00, sizeof(sql_query_string));
11592         SNPRINTF(sql_query_string, sizeof(sql_query_string),
11593                 "INSERT INTO mail_partial_body_activity_tbl VALUES "
11594                 "( "
11595                 "? "  /* Account ID */
11596                 ",?"  /* Local Mail ID */
11597                 ",?"  /* Server mail ID */
11598                 ",?"  /* Activity ID */
11599                 ",?"  /* Activity type*/
11600                 ",?"  /* Mailbox ID*/
11601                 ",?"  /* Mailbox name*/
11602                 ") ");
11603
11604         char *sql = "SELECT max(rowid) FROM mail_partial_body_activity_tbl;";
11605         char **result = NULL;
11606
11607
11608         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
11609
11610         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11611                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
11612
11613         if (NULL==result[1]) rc = 1;
11614         else rc = atoi(result[1])+1;
11615         sqlite3_free_table(result);
11616         result = NULL;
11617
11618         *activity_id = local_activity->activity_id = rc;
11619
11620         EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ], MAIL ID [ %d ], ACTIVITY TYPE [ %d ], SERVER MAIL ID [ %lu ]", \
11621                 local_activity->activity_id, local_activity->mail_id, local_activity->activity_type, local_activity->server_mail_id);
11622
11623         if (local_activity->mailbox_id)
11624                 EM_DEBUG_LOG(" MAILBOX ID [ %d ]", local_activity->mailbox_id);
11625         
11626         
11627         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
11628         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11629                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11630
11631         EM_DEBUG_LOG(">>>> SQL STMT [ %s ]", sql_query_string);
11632         
11633         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
11634         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
11635         _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
11636         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
11637         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
11638         _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
11639         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->mailbox_name, 0, 3999);
11640
11641
11642         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11643
11644         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
11645                 ("sqlite3_step fail:%d", rc));
11646         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11647                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
11648         
11649         ret = true;
11650
11651 FINISH_OFF:
11652
11653         if (hStmt != NULL) {
11654                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
11655
11656                 rc = sqlite3_finalize(hStmt);
11657                 hStmt = NULL;
11658                 if (rc != SQLITE_OK) {
11659                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
11660                         error = EMAIL_ERROR_DB_FAILURE;
11661                 }
11662         }
11663
11664          EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
11665         _DISCONNECT_DB;
11666          if (err_code != NULL)
11667                  *err_code = error;
11668
11669          EM_DEBUG_FUNC_END("ret [%d]", ret);
11670         return ret;
11671 }
11672
11673
11674 INTERNAL_FUNC int emstorage_get_pbd_mailbox_list(int account_id, int **mailbox_list, int *count, int transaction, int *err_code)
11675 {
11676         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
11677
11678         if (account_id < FIRST_ACCOUNT_ID || NULL == &mailbox_list || NULL == count) {
11679                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
11680                 if (err_code != NULL)
11681                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11682                 return false;
11683         }
11684
11685         int ret = false;
11686         int error = EMAIL_ERROR_NONE;
11687         char **result;
11688         int i = 0, rc = -1;
11689         int *mbox_list = NULL;
11690         DB_STMT hStmt = NULL;
11691         char sql_query_string[QUERY_SIZE] = {0, };
11692
11693         sqlite3 *local_db_handle = emstorage_get_db_connection();
11694         
11695         EMSTORAGE_START_READ_TRANSACTION(transaction);
11696
11697         memset(sql_query_string, 0x00, sizeof(sql_query_string));
11698         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);
11699
11700
11701         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
11702         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11703                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11704
11705         *count = atoi(result[1]);
11706         sqlite3_free_table(result);
11707
11708         if (!*count) {
11709                 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
11710                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
11711                 ret = true;
11712                 goto FINISH_OFF;
11713         }
11714         EM_DEBUG_LOG("Mailbox count = %d", *count);
11715         
11716         memset(sql_query_string, 0x00, sizeof(sql_query_string));
11717
11718         /* 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); */
11719         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);
11720
11721         EM_DEBUG_LOG(" Query [%s]", sql_query_string);
11722         
11723         
11724         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
11725
11726         EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
11727         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11728                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11729
11730
11731         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11732         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11733                 ("sqlite3_step fail:%d", rc));
11734         
11735         if (NULL == (mbox_list = (int *)em_malloc(sizeof(int *) * (*count)))) {
11736                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
11737                 error = EMAIL_ERROR_OUT_OF_MEMORY;
11738                 goto FINISH_OFF;
11739         }
11740
11741         memset(mbox_list, 0x00, sizeof(int) * (*count));
11742         
11743         for (i = 0; i < (*count); i++) {
11744                 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
11745                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11746                 /* EM_DEBUG_LOG("In emstorage_get_pdb_mailbox_list() loop, After sqlite3_step(), , i = %d, rc = %d.", i,  rc); */
11747                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11748                         ("sqlite3_step fail:%d", rc));
11749                 EM_DEBUG_LOG("mbox_list %d", mbox_list + i);
11750         }
11751
11752         ret = true;
11753
11754 FINISH_OFF:
11755         if (ret == true)
11756                 *mailbox_list = mbox_list;
11757
11758         if (hStmt != NULL) {
11759                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
11760
11761                 rc = sqlite3_finalize(hStmt);
11762                 hStmt = NULL;
11763                 if (rc != SQLITE_OK) {
11764                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
11765                         error = EMAIL_ERROR_DB_FAILURE;
11766                 }
11767         }
11768
11769         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
11770         _DISCONNECT_DB;
11771         if (err_code != NULL)
11772                 *err_code = error;
11773         EM_DEBUG_FUNC_END("ret [%d]", ret);
11774         return ret;
11775 }
11776
11777
11778 INTERNAL_FUNC int emstorage_get_pbd_account_list(int **account_list, int *count, int transaction, int *err_code)
11779 {
11780         EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
11781
11782         if (NULL == &account_list || NULL == count) {
11783                 EM_DEBUG_EXCEPTION("mailbox_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
11784                 if (err_code != NULL)
11785                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11786                 return false;
11787         }
11788
11789         int ret = false;
11790         int error = EMAIL_ERROR_NONE;
11791         char *sql;
11792         char **result;
11793         int i = 0, rc = -1;
11794         int *result_account_list = NULL;
11795         DB_STMT hStmt = NULL;
11796         char sql_query_string[QUERY_SIZE] = {0, };
11797
11798         sqlite3 *local_db_handle = emstorage_get_db_connection();
11799         
11800         EMSTORAGE_START_READ_TRANSACTION(transaction);
11801
11802
11803         sql = "SELECT count(distinct account_id) FROM mail_partial_body_activity_tbl";  
11804
11805
11806         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
11807         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11808                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
11809
11810         *count = atoi(result[1]);
11811         sqlite3_free_table(result);
11812
11813         if (!*count) {
11814                 EM_DEBUG_EXCEPTION("no account found...");
11815                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
11816                 ret = true;
11817                 goto FINISH_OFF;
11818         }
11819
11820         EM_DEBUG_LOG("Account count [%d]", *count);
11821         
11822         memset(sql_query_string, 0x00, sizeof(sql_query_string));
11823
11824         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_partial_body_activity_tbl");
11825
11826         EM_DEBUG_LOG("Query [%s]", sql_query_string);
11827         
11828         
11829         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
11830
11831         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
11832         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11833                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11834
11835
11836         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11837         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11838                 ("sqlite3_step fail:%d", rc));
11839         
11840         if (NULL == (result_account_list = (int *)em_malloc(sizeof(int) * (*count)))) {
11841                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
11842                 error = EMAIL_ERROR_OUT_OF_MEMORY;
11843                 goto FINISH_OFF;
11844         }
11845
11846         memset(result_account_list, 0x00, sizeof(int) * (*count));
11847         
11848         for (i = 0; i < (*count); i++) {
11849                 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
11850
11851                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11852                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11853                         ("sqlite3_step fail:%d", rc));
11854                 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
11855         }
11856
11857         ret = true;
11858
11859 FINISH_OFF:
11860         if (ret == true)
11861                 *account_list = result_account_list;
11862
11863         if (hStmt != NULL) {
11864                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
11865
11866                 rc = sqlite3_finalize(hStmt);
11867                 hStmt = NULL;
11868                 if (rc != SQLITE_OK) {
11869                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
11870                         error = EMAIL_ERROR_DB_FAILURE;
11871                 }
11872         }
11873
11874         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
11875         _DISCONNECT_DB;
11876         if (err_code != NULL)
11877                 *err_code = error;
11878         EM_DEBUG_FUNC_END("ret [%d]", ret);
11879         return ret;
11880 }
11881
11882
11883
11884 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)
11885 {
11886         EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
11887
11888         if (account_id < FIRST_ACCOUNT_ID || NULL == event_start || 0 == input_mailbox_id || NULL == count) {
11889                 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);
11890
11891                 if (err_code != NULL)
11892                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11893                 return false;
11894         }
11895
11896         int rc = -1;
11897         int ret = false;
11898         char **result;
11899         int error = EMAIL_ERROR_NONE;
11900         int i = 0;
11901         DB_STMT hStmt = NULL;
11902         email_event_partial_body_thd* event_list = NULL;
11903         char sql_query_string[QUERY_SIZE] = {0, };
11904
11905         sqlite3 *local_db_handle = emstorage_get_db_connection();
11906         
11907         EMSTORAGE_START_READ_TRANSACTION(transaction);
11908
11909         memset(sql_query_string, 0x00, sizeof(sql_query_string));
11910         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);
11911         
11912
11913         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
11914         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11915                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11916
11917         *count = atoi(result[1]);
11918         sqlite3_free_table(result);
11919
11920         EM_DEBUG_LOG("Query = [%s]", sql_query_string);
11921
11922         if (!*count) {
11923                 EM_DEBUG_EXCEPTION("No matched activity found in mail_partial_body_activity_tbl");
11924
11925                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11926                 ret = true;
11927                 goto FINISH_OFF;
11928         }
11929         EM_DEBUG_LOG("Activity Count = %d", *count);
11930         
11931         memset(sql_query_string, 0x00, sizeof(sql_query_string));
11932         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);
11933
11934         EM_DEBUG_LOG("Query [%s]", sql_query_string);
11935
11936         
11937         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
11938
11939         EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
11940         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11941                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11942
11943
11944         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11945         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11946                 ("sqlite3_step fail:%d", rc));
11947         
11948         if (!(event_list = (email_event_partial_body_thd*)em_malloc(sizeof(email_event_partial_body_thd) * (*count)))) {
11949                 EM_DEBUG_EXCEPTION("Malloc failed");
11950
11951                 error = EMAIL_ERROR_OUT_OF_MEMORY;
11952                 goto FINISH_OFF;
11953         }
11954         memset(event_list, 0x00, sizeof(email_event_partial_body_thd) * (*count));
11955         
11956         for (i=0; i < (*count); i++) {
11957                 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_IDX_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
11958                 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
11959                 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
11960                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
11961                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_type), ACTIVITY_TYPE_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
11962                 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
11963                 _get_stmt_field_data_string(hStmt, &(event_list[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
11964
11965                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11966                 /* EM_DEBUG_LOG("In emstorage_get_pbd_activity_data() loop, After sqlite3_step(), , i = %d, rc = %d.", i,  rc); */
11967                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11968                         ("sqlite3_step fail:%d", rc));
11969
11970                 event_list[i].event_type = 0;
11971         }
11972
11973         ret = true;
11974
11975 FINISH_OFF:
11976         if (true == ret)
11977           *event_start = event_list;
11978         else {
11979                 EM_SAFE_FREE(event_list);
11980         }
11981
11982         if (hStmt != NULL) {
11983                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
11984
11985                 rc = sqlite3_finalize(hStmt);
11986                 hStmt = NULL;
11987                 if (rc != SQLITE_OK) {
11988                         EM_DEBUG_LOG("sqlite3_finalize failed - %d", rc);
11989                         error = EMAIL_ERROR_DB_FAILURE;
11990                 }
11991         }
11992
11993         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
11994         _DISCONNECT_DB;
11995         if (err_code != NULL)
11996                 *err_code = error;
11997
11998         EM_DEBUG_FUNC_END("ret [%d]", ret);
11999         return ret;
12000 }
12001
12002
12003
12004 INTERNAL_FUNC int emstorage_delete_pbd_activity(int account_id, int mail_id, int activity_id, int transaction, int *err_code)
12005 {
12006         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);
12007
12008
12009         if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
12010                 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);
12011
12012                 if (err_code != NULL)
12013                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12014                 return false;
12015         }
12016
12017         int rc = -1;
12018         int ret = false;
12019         int error = EMAIL_ERROR_NONE;
12020         char sql_query_string[QUERY_SIZE] = {0, };
12021         
12022         sqlite3 *local_db_handle = emstorage_get_db_connection();
12023         
12024         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12025         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12026         
12027         if (activity_id == 0)
12028                 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);
12029         else
12030                 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);
12031
12032         EM_DEBUG_LOG("Query [%s]", sql_query_string);
12033
12034         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12035         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12036                 ("sqlite3_exec fail:%d", rc));
12037         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12038                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12039
12040         /*  validate activity existence */
12041         rc = sqlite3_changes(local_db_handle);
12042         if (rc == 0)  {
12043                 EM_DEBUG_EXCEPTION("No matching activity found");
12044                 error = EMAIL_ERROR_DATA_NOT_FOUND;
12045                 ret = true;
12046                 goto FINISH_OFF;
12047         }
12048
12049         ret = true;
12050
12051 FINISH_OFF:
12052         
12053         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12054         
12055         _DISCONNECT_DB;
12056         if (err_code != NULL)
12057                 *err_code = error;
12058
12059         EM_DEBUG_FUNC_END("ret [%d]", ret);
12060         return ret;
12061 }
12062
12063 INTERNAL_FUNC int emstorage_get_mailbox_pbd_activity_count(int account_id, int input_mailbox_id, int *activity_count, int transaction, int *err_code)
12064 {
12065         EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
12066
12067         if (account_id < FIRST_ACCOUNT_ID || NULL == activity_count || NULL == err_code) {
12068                 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
12069                 if (err_code != NULL)
12070                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12071                 return false;
12072         }
12073         int rc = -1;
12074         int ret = false;
12075         int error = EMAIL_ERROR_NONE;
12076         char sql_query_string[QUERY_SIZE] = {0, };
12077
12078         DB_STMT hStmt = NULL;
12079
12080         sqlite3 *local_db_handle = emstorage_get_db_connection();
12081
12082         EMSTORAGE_START_READ_TRANSACTION(transaction);
12083         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12084         
12085         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);
12086
12087         EM_DEBUG_LOG(" Query [%s]", sql_query_string);
12088
12089         
12090         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
12091         EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
12092         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12093                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12094
12095
12096         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12097         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12098                 ("sqlite3_step fail:%d", rc));
12099
12100         _get_stmt_field_data_int(hStmt, activity_count, 0);
12101
12102         EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
12103
12104         ret = true;
12105
12106 FINISH_OFF:
12107
12108         if (hStmt != NULL) {
12109                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12110                 rc = sqlite3_finalize(hStmt);
12111                 hStmt=NULL;
12112                 if (rc != SQLITE_OK) {
12113                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
12114                         error = EMAIL_ERROR_DB_FAILURE;
12115                 }
12116                 EM_DEBUG_LOG("sqlite3_finalize- %d", rc);
12117         }
12118
12119         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12120         _DISCONNECT_DB;
12121         
12122         if (err_code != NULL)
12123                 *err_code = error;
12124
12125         EM_DEBUG_FUNC_END("ret [%d]", ret);
12126         return ret;
12127 }
12128
12129
12130 INTERNAL_FUNC int emstorage_get_pbd_activity_count(int *activity_count, int transaction, int *err_code)
12131 {
12132         EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
12133
12134         if (NULL == activity_count || NULL == err_code) {
12135                 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
12136                 if (err_code != NULL)
12137                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12138                 return false;
12139         }
12140         int rc = -1;
12141         int ret = false;
12142         int error = EMAIL_ERROR_NONE;
12143         DB_STMT hStmt = NULL;
12144         char sql_query_string[QUERY_SIZE] = {0, };
12145
12146         sqlite3 *local_db_handle = emstorage_get_db_connection();
12147
12148         EMSTORAGE_START_READ_TRANSACTION(transaction);
12149         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12150         
12151         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl;");
12152
12153         EM_DEBUG_LOG(" Query [%s]", sql_query_string);
12154
12155         
12156         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
12157         EM_DEBUG_LOG("  before sqlite3_prepare hStmt = %p", hStmt);
12158         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12159                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12160
12161
12162         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12163         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12164                 ("sqlite3_step fail:%d", rc));
12165
12166         _get_stmt_field_data_int(hStmt, activity_count, 0);
12167
12168         EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
12169
12170         ret = true;
12171
12172 FINISH_OFF:
12173
12174
12175         if (hStmt != NULL) {
12176                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12177
12178                 rc = sqlite3_finalize(hStmt);
12179                 hStmt=NULL;
12180                 if (rc != SQLITE_OK) {
12181                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
12182                         error = EMAIL_ERROR_DB_FAILURE;
12183                 }
12184                 EM_DEBUG_LOG("sqlite3_finalize- %d", rc);
12185         }
12186
12187         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12188         _DISCONNECT_DB;
12189         if (err_code != NULL)
12190                 *err_code = error;
12191
12192         EM_DEBUG_FUNC_END("ret [%d]", ret);
12193         return ret;
12194 }
12195
12196 INTERNAL_FUNC int emstorage_delete_full_pbd_activity_data(int account_id, int transaction, int *err_code)
12197 {
12198         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
12199         if (account_id < FIRST_ACCOUNT_ID) {
12200                 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
12201                 if (err_code != NULL)
12202                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12203                 return false;
12204         }
12205
12206         int rc = -1;
12207         int ret = false;
12208         int error = EMAIL_ERROR_NONE;
12209         char sql_query_string[QUERY_SIZE] = {0, };
12210         
12211         sqlite3 *local_db_handle = emstorage_get_db_connection();
12212         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12213         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12214         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d", account_id);
12215
12216         EM_DEBUG_LOG("Query [%s]", sql_query_string);
12217
12218         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12219         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12220                 ("sqlite3_exec fail:%d", rc));
12221         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12222                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12223
12224         
12225         rc = sqlite3_changes(local_db_handle);
12226         if (rc == 0) {
12227                 EM_DEBUG_EXCEPTION("No matching activities found in mail_partial_body_activity_tbl");
12228                 error = EMAIL_ERROR_DATA_NOT_FOUND;
12229                 ret = true;
12230                 goto FINISH_OFF;
12231         }
12232
12233         ret = true;
12234
12235 FINISH_OFF:
12236         
12237         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12238         _DISCONNECT_DB;
12239         if (err_code != NULL)
12240                 *err_code = error;
12241
12242         EM_DEBUG_FUNC_END("ret [%d]", ret);
12243         return ret;
12244 }
12245
12246 /*Himanshu[h.gahlaut]-> Added below API to update mail_partial_body_activity_tbl
12247 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*/
12248
12249 INTERNAL_FUNC int emstorage_update_pbd_activity(char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code)
12250 {
12251         EM_DEBUG_FUNC_BEGIN("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
12252
12253         int rc = -1, ret = false;
12254         int error = EMAIL_ERROR_NONE;
12255         char sql_query_string[QUERY_SIZE] = {0, };
12256
12257         int transaction = true;
12258
12259         if (!old_server_uid || !new_server_uid || !mbox_name)  {
12260                 EM_DEBUG_EXCEPTION("Invalid parameters");
12261                 error = EMAIL_ERROR_INVALID_PARAM;
12262                 return false;
12263         }
12264
12265         sqlite3 *local_db_handle = emstorage_get_db_connection();
12266         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12267         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12268         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12269                  "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);
12270
12271         EM_DEBUG_LOG("Query [%s]", sql_query_string);
12272
12273         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12274         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12275                 ("sqlite3_exec fail:%d", rc));
12276         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12277                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12278         
12279         rc = sqlite3_changes(local_db_handle);
12280         if (rc == 0) {
12281                 EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
12282                 error = EMAIL_ERROR_DATA_NOT_FOUND;
12283                 goto FINISH_OFF;
12284         }
12285
12286         ret = true;
12287
12288 FINISH_OFF:
12289
12290         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12291         _DISCONNECT_DB;
12292         if (err_code != NULL)
12293                 *err_code = error;
12294
12295         EM_DEBUG_FUNC_END("ret [%d]", ret);
12296         return ret;
12297 }
12298
12299
12300 INTERNAL_FUNC int emstorage_create_file(char *data_string, size_t file_size, char *dst_file_name, int *err_code)
12301 {
12302         EM_DEBUG_FUNC_BEGIN("file_size[%d] , dst_file_name[%s], err_code[%p]", file_size, dst_file_name, err_code);
12303         
12304         int ret = false;
12305         int error = EMAIL_ERROR_NONE;
12306         
12307         FILE* fp_dst = NULL;
12308         
12309         if (!data_string || !dst_file_name)  {                          
12310                 EM_DEBUG_EXCEPTION("data_string[%p], dst_file_name[%p]", data_string, dst_file_name);
12311                 error = EMAIL_ERROR_INVALID_PARAM;
12312                 goto FINISH_OFF;
12313         }
12314
12315         fp_dst = fopen(dst_file_name, "w");
12316         
12317         if (!fp_dst)  {
12318                 EM_DEBUG_EXCEPTION("fopen failed - %s", dst_file_name);
12319                 if (errno == 28)
12320                         error = EMAIL_ERROR_MAIL_MEMORY_FULL;
12321                 else
12322                         error = EMAIL_ERROR_SYSTEM_FAILURE;
12323                 goto FINISH_OFF;                        
12324         }
12325         
12326         if (fwrite(data_string, 1, file_size, fp_dst) < 0) {
12327                 EM_DEBUG_EXCEPTION("fwrite failed...");
12328                 error = EMAIL_ERROR_UNKNOWN;
12329                 goto FINISH_OFF;
12330         }
12331         
12332         ret = true;
12333         
12334 FINISH_OFF:
12335         
12336         if (fp_dst != NULL)
12337                 fclose(fp_dst);                 
12338
12339         if (err_code != NULL)
12340                 *err_code = error;
12341
12342         EM_DEBUG_FUNC_END("ret [%d]", ret);
12343         return ret;
12344 }
12345
12346 #endif /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
12347
12348
12349
12350 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
12351
12352 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)
12353 {
12354         EM_DEBUG_FUNC_BEGIN();
12355         int rc = -1;                               
12356         int ret = false;
12357         int error = EMAIL_ERROR_NONE;
12358         char sql_query_string[QUERY_SIZE] = {0, };
12359
12360         int transaction = true;
12361                 
12362         if (!old_server_uid || !new_server_uid || !mbox_name)  {                
12363                 EM_DEBUG_EXCEPTION("Invalid parameters");
12364                 error = EMAIL_ERROR_INVALID_PARAM;
12365                 return false;
12366         }
12367
12368         EM_DEBUG_LOG("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
12369         
12370         sqlite3 *local_db_handle = emstorage_get_db_connection();
12371         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12372
12373         
12374         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12375                  "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);
12376
12377          EM_DEBUG_LOG(" Query [%s]", sql_query_string);
12378          
12379         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12380          EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12381                  ("sqlite3_exec fail:%d", rc));
12382          EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12383                  ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12384          
12385          
12386          rc = sqlite3_changes(local_db_handle);
12387          if (rc == 0)
12388          {
12389                  EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
12390                  error = EMAIL_ERROR_DATA_NOT_FOUND;
12391                  goto FINISH_OFF;
12392          }
12393
12394           
12395          ret = true;
12396           
12397 FINISH_OFF:
12398           
12399         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12400         _DISCONNECT_DB;
12401
12402         if (err_code != NULL)
12403                 *err_code = error;
12404         
12405         EM_DEBUG_FUNC_END("ret [%d]", ret);
12406         return ret;
12407         
12408 }
12409
12410
12411 /**
12412  * @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);
12413  * Prepare an array of mail_id and corresponding server mail id.
12414  *
12415  *@author                                       h.gahlaut@samsung.com
12416  * @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)
12417  *                                                      where 88 is the length of fixed keywords including ending null character in the QUERY to be formed
12418  * @param[out] idset                    Returns the array of mail_id and corresponding server_mail_id sorted by server_mail_ids
12419  * @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
12420  * @param[out] err_code         Returns the error code.
12421  * @remarks                                     An Example of Query to be exexuted in this API:
12422  *                                                      SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (12, 13, 56, 78);
12423  * @return This function returns true on success or false on failure.
12424  */
12425  
12426 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)
12427 {
12428         EM_DEBUG_FUNC_BEGIN();
12429         EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds);
12430
12431         int error = EMAIL_ERROR_NONE;
12432         int ret = false;
12433         email_id_set_t* p_id_set = NULL;
12434         int count = 0;
12435         const int buf_size = QUERY_SIZE;
12436         char sql_query_string[QUERY_SIZE] = {0, };
12437         int space_left_in_query_buffer = buf_size;
12438         int i = 0;
12439         int rc = -1;
12440         char *server_mail_id = NULL;
12441         char **result = NULL;
12442         int col_index = 0;
12443         
12444         
12445         if (NULL == mail_ids || NULL == idset || NULL == id_set_count) {
12446                 EM_DEBUG_EXCEPTION("Invalid Parameters mail_ids[%p] idset[%p]  id_set_count [%p]", mail_ids, idset, id_set_count);
12447                 error = EMAIL_ERROR_INVALID_PARAM;
12448                 goto FINISH_OFF;
12449         }
12450
12451         sqlite3 *local_db_handle = emstorage_get_db_connection();
12452
12453         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);
12454
12455         EM_DEBUG_LOG("SQL Query formed [%s] ", sql_query_string);
12456         
12457         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
12458         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
12459         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12460                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12461
12462         EM_DEBUG_LOG(" Count of mails [%d ]", count);
12463
12464         if (count <= 0) {
12465                 EM_DEBUG_EXCEPTION("Can't find proper mail");
12466                 error = EMAIL_ERROR_DATA_NOT_FOUND;
12467                 goto FINISH_OFF;
12468         }
12469
12470
12471         if (NULL == (p_id_set = (email_id_set_t*)em_malloc(sizeof(email_id_set_t) * count)))  {
12472                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
12473                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12474                 goto FINISH_OFF;
12475         }
12476
12477         col_index = 2;
12478
12479         EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds_Loop);
12480         EM_DEBUG_LOG(">>>> DATA ASSIGN START"); 
12481         for (i = 0; i < count; i++)  {
12482                 _get_table_field_data_int(result, &(p_id_set[i].mail_id), col_index++);
12483                 _get_table_field_data_string(result, &server_mail_id, 1, col_index++);
12484                 p_id_set[i].server_mail_id = strtoul(server_mail_id, NULL, 10);
12485                 EM_SAFE_FREE(server_mail_id);           
12486         }
12487         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d]", count);
12488         EM_PROFILE_END(EmStorageGetIdSetFromMailIds_Loop);
12489
12490         sqlite3_free_table(result);
12491         result = NULL;
12492
12493         ret = true;
12494
12495         FINISH_OFF:
12496
12497         if (ret == true)  {
12498                 *idset = p_id_set;
12499                 *id_set_count = count;
12500                 EM_DEBUG_LOG(" idset[%p] id_set_count [%d]", *idset, *id_set_count);
12501         }
12502         else 
12503                 EM_SAFE_FREE(p_id_set);
12504         
12505         _DISCONNECT_DB;
12506         
12507         if (err_code != NULL)
12508                 *err_code = error;
12509
12510         EM_PROFILE_END(EmStorageGetIdSetFromMailIds);
12511
12512         EM_DEBUG_FUNC_END("ret [%d]", ret);
12513         return ret;
12514 }
12515
12516
12517
12518 #endif
12519
12520 INTERNAL_FUNC int emstorage_delete_triggers_from_lucene()
12521 {
12522         EM_DEBUG_FUNC_BEGIN();
12523         int rc, ret = true, transaction = true;
12524         int error = EMAIL_ERROR_NONE;
12525         char sql_query_string[QUERY_SIZE] = {0, };
12526         
12527         sqlite3 *local_db_handle = emstorage_get_db_connection();
12528         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12529
12530         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerDelete;");
12531         
12532         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12533         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12534                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12535
12536         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerInsert;");
12537         
12538         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12539         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12540                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12541         
12542
12543         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerUpdate;");
12544         
12545         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12546         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12547                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12548         
12549         ret = true;
12550         
12551 FINISH_OFF:
12552
12553         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12554
12555         _DISCONNECT_DB;
12556
12557         EM_DEBUG_FUNC_END("ret [%d]", ret);
12558         return ret;
12559 }
12560
12561 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)
12562 {
12563         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);
12564         
12565         if ((account_id <= 0) || (dest_mailbox_id <= 0) || (!rule) || (!rule->value)|| (!filtered_mail_id_list) || (!count_of_mails)) {
12566                 EM_DEBUG_EXCEPTION("Invalid Parameter");
12567                 
12568                 if (err_code != NULL)
12569                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12570                 return false;
12571         }
12572
12573         int rc = -1, ret = false, error = EMAIL_ERROR_NONE;
12574         int count = 0, col_index = 0, i = 0, where_pararaph_length = 0, *mail_list = NULL;
12575         char **result = NULL, *where_pararaph = NULL;
12576         char sql_query_string[QUERY_SIZE] = {0, };
12577         
12578         sqlite3 *local_db_handle = emstorage_get_db_connection();
12579
12580         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl ");
12581         
12582         EM_DEBUG_LOG("rule->value [%s]", rule->value);
12583
12584         where_pararaph_length = strlen(rule->value) + 100;
12585         where_pararaph = malloc(sizeof(char) * where_pararaph_length);
12586
12587         if (where_pararaph == NULL) {
12588                 EM_DEBUG_EXCEPTION("malloc failed for where_pararaph.");
12589                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12590                 goto FINISH_OFF;
12591         }
12592
12593         memset(where_pararaph, 0, sizeof(char) * where_pararaph_length);
12594         
12595         EM_DEBUG_LOG("rule->type [%d], rule->flag2[%d]", rule->type, rule->flag2);
12596
12597         if (rule->type == EMAIL_FILTER_FROM) {
12598                 if (rule->flag2 == RULE_TYPE_INCLUDES)
12599                         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);
12600                 else /*  RULE_TYPE_EXACTLY */
12601                         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);
12602         }
12603         else if (rule->type == EMAIL_FILTER_SUBJECT) {
12604                 if (rule->flag2 == RULE_TYPE_INCLUDES)
12605                         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);
12606                 else /*  RULE_TYPE_EXACTLY */
12607                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (3,5) AND subject = \'%s\'", account_id, rule->value);
12608         }
12609         else {
12610                 error = EMAIL_ERROR_INVALID_PARAM;
12611                 EM_DEBUG_EXCEPTION("rule->type is invald");
12612                 goto FINISH_OFF;
12613         }
12614         
12615         if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
12616                 strcat(sql_query_string, where_pararaph);
12617
12618         EM_DEBUG_LOG("query[%s]", sql_query_string);
12619         
12620         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
12621         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
12622         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12623                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12624
12625         EM_DEBUG_LOG("Count of mails [%d]", count);
12626
12627         if (count) {
12628                 mail_list = malloc(sizeof(int) * count);
12629                 if (mail_list == NULL) {
12630                         EM_DEBUG_EXCEPTION("malloc failed for mail_list.");
12631                         error = EMAIL_ERROR_OUT_OF_MEMORY;
12632                         goto FINISH_OFF;
12633                 }
12634                 
12635                 col_index = 1;
12636
12637                 for (i = 0; i < count; i++) 
12638                         _get_table_field_data_int(result, &(mail_list[i]), col_index++);
12639
12640                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET mailbox_id = %d, mailbox_type = 5 ", dest_mailbox_id);
12641
12642                 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
12643                 strcat(sql_query_string, where_pararaph);
12644
12645                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12646                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12647                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12648         }       
12649         
12650         *filtered_mail_id_list = mail_list;
12651         
12652         ret = true;
12653         
12654 FINISH_OFF:
12655                 
12656         sqlite3_free_table(result);
12657         result = NULL;
12658
12659         _DISCONNECT_DB;
12660
12661         EM_SAFE_FREE(where_pararaph);
12662
12663         if (ret && count_of_mails)
12664                 *count_of_mails = count;
12665
12666         if (err_code != NULL)
12667                 *err_code = error;
12668
12669         EM_DEBUG_FUNC_END("ret [%d]", ret);
12670         return ret;
12671 }
12672
12673 #define EMAIL_SLOT_UNIT 25
12674
12675 INTERNAL_FUNC int emstorage_set_mail_slot_size(int account_id, int mailbox_id, int new_slot_size, int transaction, int *err_code)
12676 {
12677         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);
12678         int rc = -1, ret = false, err = EMAIL_ERROR_NONE;
12679         int where_pararaph_length = 0;
12680         char *where_pararaph = NULL;
12681         char sql_query_string[QUERY_SIZE] = {0, };
12682         int and = 0;
12683         sqlite3 *local_db_handle = emstorage_get_db_connection();
12684
12685         EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
12686
12687         if (new_slot_size > 0)
12688                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = %d ", new_slot_size);
12689         else if (new_slot_size == 0)
12690                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", EMAIL_SLOT_UNIT);
12691         else
12692                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", new_slot_size * -1);
12693
12694
12695         if (mailbox_id)
12696                 where_pararaph_length = 80;
12697         else
12698                 where_pararaph_length = 50;
12699
12700         if (new_slot_size == 0)
12701                 where_pararaph_length += 70;
12702
12703         where_pararaph = malloc(sizeof(char) * where_pararaph_length);
12704         if (where_pararaph == NULL) {
12705                 EM_DEBUG_EXCEPTION("Memory allocation failed for where_pararaph");
12706                 err = EMAIL_ERROR_OUT_OF_MEMORY;
12707                 goto FINISH_OFF;
12708         }
12709         memset(where_pararaph, 0x00, where_pararaph_length);
12710
12711         if (account_id > ALL_ACCOUNT) {
12712                 and = 1;
12713                 if (mailbox_id)
12714                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_id = %d ", mailbox_id);
12715                 else
12716                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d ", account_id);
12717         }
12718
12719         if (new_slot_size == 0)
12720                 SNPRINTF(where_pararaph + strlen(where_pararaph), where_pararaph_length - strlen(where_pararaph), " %s total_mail_count_on_server > mail_slot_size ", (and ? "AND" : "WHERE"));
12721
12722         if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
12723                 strcat(sql_query_string, where_pararaph);
12724         else {
12725                 EM_DEBUG_EXCEPTION("Query buffer overflowed !!!");
12726                 err = EMAIL_ERROR_OUT_OF_MEMORY;
12727                 goto FINISH_OFF;                        
12728         }
12729
12730         EM_DEBUG_LOG("query[%s]", sql_query_string);
12731
12732         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12733         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12734         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12735         
12736         ret = true;
12737         
12738 FINISH_OFF:
12739
12740         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
12741
12742         _DISCONNECT_DB;
12743
12744         EM_SAFE_FREE(where_pararaph);
12745
12746         if (err_code != NULL)
12747                 *err_code = err;
12748
12749         EM_DEBUG_FUNC_END("ret [%d]", ret);
12750         return ret;
12751 }
12752
12753 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)
12754 {
12755         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);
12756         
12757         if (!meeting_req || meeting_req->mail_id <= 0) {
12758                 if (meeting_req)
12759                 EM_DEBUG_EXCEPTION("mail_id[%]d", meeting_req->mail_id);
12760                 
12761                 if (err_code != NULL)
12762                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12763
12764                 return false;
12765         }
12766         
12767         int rc = -1;
12768         int ret = false;
12769         int error = EMAIL_ERROR_NONE;
12770         DB_STMT hStmt = NULL;
12771         char sql_query_string[QUERY_SIZE] = {0, };
12772         int col_index = 0;
12773         time_t temp_unix_time = 0;
12774         
12775         sqlite3 *local_db_handle = emstorage_get_db_connection();
12776         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12777         
12778         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12779                 "INSERT INTO mail_meeting_tbl VALUES "
12780                 "( ?"           /*  mail_id */
12781                 ", ?"           /*  account_id */
12782                 ", ?"           /*  mailbox_id */
12783                 ", ?"           /*  meeting_response */
12784                 ", ?"           /*  start_time */
12785                 ", ?"           /*  end_time */
12786                 ", ?"           /*  location */
12787                 ", ?"           /*  global_object_id */
12788                 ", ?"           /*  offset */
12789                 ", ?"           /*  standard_name */
12790                 ", ?"           /*  standard_time_start_date */
12791                 ", ?"           /*  standard_biad */
12792                 ", ?"           /*  daylight_name */
12793                 ", ?"           /*  daylight_time_start_date */
12794                 ", ?"           /*  daylight_bias */
12795                 " )");
12796         
12797         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
12798         if (rc != SQLITE_OK)  {
12799                 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
12800                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
12801                 
12802                 error = EMAIL_ERROR_DB_FAILURE;
12803                 goto FINISH_OFF;
12804         }
12805
12806         col_index = 0;
12807         /*
12808         EM_DEBUG_LOG(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
12809         EM_DEBUG_LOG(">>>>> account_id[%d]", account_id);
12810         EM_DEBUG_LOG(">>>>> mailbox_name[%s]", mailbox_name);
12811         EM_DEBUG_LOG(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
12812         EM_DEBUG_LOG(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
12813         EM_DEBUG_LOG(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
12814         EM_DEBUG_LOG(">>>>> meeting_req->location[%s]", meeting_req->location);
12815         EM_DEBUG_LOG(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
12816         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
12817         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
12818         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
12819         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
12820         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
12821         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
12822         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
12823         */
12824         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->mail_id);
12825         _bind_stmt_field_data_int(hStmt, col_index++, account_id);
12826         _bind_stmt_field_data_int(hStmt, col_index++, input_mailbox_id);
12827         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
12828
12829         temp_unix_time = timegm(&(meeting_req->start_time));
12830         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
12831         temp_unix_time = timegm(&(meeting_req->end_time));
12832         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
12833
12834         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->location, 0, LOCATION_LEN_IN_MAIL_MEETING_TBL);
12835         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->global_object_id, 0, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
12836         
12837         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
12838         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.standard_name, 0, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
12839         temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
12840         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
12841         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
12842
12843         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.daylight_name, 0, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
12844         temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
12845         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
12846         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
12847
12848
12849         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12850         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12851                 ("sqlite3_step fail:%d", rc));
12852         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12853                 ("sqlite3_step fail:%d", rc));
12854         
12855         ret = true;
12856         
12857 FINISH_OFF:
12858         if (hStmt != NULL)  {
12859                 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
12860
12861                 rc = sqlite3_finalize(hStmt);
12862                 if (rc != SQLITE_OK)  {
12863                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
12864                         error = EMAIL_ERROR_DB_FAILURE;
12865                 }
12866         }
12867  
12868         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12869         _DISCONNECT_DB;
12870         
12871         if (err_code != NULL)
12872                 *err_code = error;
12873
12874         EM_DEBUG_FUNC_END("ret [%d]", ret);
12875         return ret;
12876 }
12877
12878 INTERNAL_FUNC int emstorage_get_meeting_request(int mail_id, email_meeting_request_t ** meeting_req, int transaction, int *err_code)
12879 {
12880         EM_DEBUG_FUNC_BEGIN();
12881         
12882         int count = 0;
12883         int rc = -1;
12884         int ret = false;
12885         int error = EMAIL_ERROR_NONE;
12886         email_meeting_request_t *p_temp_meeting_req = NULL;
12887         int col_index = 0;
12888         time_t temp_unix_time;
12889
12890         EM_IF_NULL_RETURN_VALUE(meeting_req, false);
12891
12892         DB_STMT hStmt = NULL;
12893         char sql_query_string[QUERY_SIZE] = {0, };
12894
12895         sqlite3 *local_db_handle = emstorage_get_db_connection();
12896         EMSTORAGE_START_READ_TRANSACTION(transaction);
12897
12898         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12899                 "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 "
12900                 " FROM mail_meeting_tbl "
12901                 " WHERE mail_id = %d", mail_id);
12902         EM_DEBUG_LOG("sql : %s ", sql_query_string);
12903
12904         
12905         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
12906
12907         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12908                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12909
12910
12911         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12912         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12913                 ("sqlite3_step fail:%d", rc));
12914         
12915         if (rc == SQLITE_DONE)  {
12916                 EM_DEBUG_EXCEPTION(" no Meeting request found...");
12917                 count = 0;
12918                 ret = false;
12919                 error= EMAIL_ERROR_DATA_NOT_FOUND;
12920                 goto FINISH_OFF;
12921         }
12922         
12923         if (!(p_temp_meeting_req = (email_meeting_request_t*)malloc(sizeof(email_meeting_request_t)))) {
12924                 EM_DEBUG_EXCEPTION(" malloc failed...");
12925                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12926                 goto FINISH_OFF;
12927         }
12928
12929         memset(p_temp_meeting_req, 0x00, sizeof(email_meeting_request_t));
12930                 
12931         col_index = 0;
12932
12933         _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->mail_id), col_index++);
12934         _get_stmt_field_data_int(hStmt, (int *)&(p_temp_meeting_req->meeting_response), col_index++);
12935         _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
12936         gmtime_r(&temp_unix_time, &(p_temp_meeting_req->start_time));
12937         _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
12938         gmtime_r(&temp_unix_time, &(p_temp_meeting_req->end_time));
12939         _get_stmt_field_data_string(hStmt, &p_temp_meeting_req->location, 1, col_index++);
12940         _get_stmt_field_data_string(hStmt, &p_temp_meeting_req->global_object_id, 1, col_index++);
12941         _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->time_zone.offset_from_GMT), col_index++);
12942
12943         _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++);
12944         _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
12945         gmtime_r(&temp_unix_time, &(p_temp_meeting_req->time_zone.standard_time_start_date));
12946         _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->time_zone.standard_bias), col_index++);
12947
12948         _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++);
12949         _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
12950         gmtime_r(&temp_unix_time, &(p_temp_meeting_req->time_zone.daylight_time_start_date));
12951         _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->time_zone.daylight_bias), col_index++);
12952
12953         /*
12954         EM_DEBUG_LOG(">>>>> meeting_req->mail_id[%d]", p_temp_meeting_req->mail_id);
12955         EM_DEBUG_LOG(">>>>> meeting_req->meeting_response[%d]", p_temp_meeting_req->meeting_response);
12956         EM_DEBUG_LOG(">>>>> meeting_req->start_time[%s]", asctime(&(p_temp_meeting_req->start_time)));
12957         EM_DEBUG_LOG(">>>>> meeting_req->end_time[%s]", asctime(&(p_temp_meeting_req->end_time)));
12958         EM_DEBUG_LOG(">>>>> meeting_req->location[%s]", p_temp_meeting_req->location);
12959         EM_DEBUG_LOG(">>>>> meeting_req->global_object_id[%s]", p_temp_meeting_req->global_object_id);
12960         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", p_temp_meeting_req->time_zone.offset_from_GMT);
12961         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_name[%s]", p_temp_meeting_req->time_zone.standard_name);
12962         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(p_temp_meeting_req->time_zone.standard_time_start_date)));
12963         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_bias[%d]", p_temp_meeting_req->time_zone.standard_bias);
12964         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_name[%s]", p_temp_meeting_req->time_zone.daylight_name);
12965         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(p_temp_meeting_req->time_zone.daylight_time_start_date)));
12966         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_bias[%d]", p_temp_meeting_req->time_zone.daylight_bias);
12967         */
12968         ret = true;
12969         
12970 FINISH_OFF:
12971         if (ret == true) 
12972                 *meeting_req = p_temp_meeting_req;
12973         else  { 
12974                 EM_SAFE_FREE(p_temp_meeting_req);
12975         }
12976                 
12977         if (hStmt != NULL)  {
12978                 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
12979
12980                 rc = sqlite3_finalize(hStmt);
12981                 hStmt = NULL;
12982                 if (rc != SQLITE_OK)  {
12983                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
12984                         error = EMAIL_ERROR_DB_FAILURE;
12985                 }
12986         }
12987
12988         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12989         _DISCONNECT_DB;
12990         
12991         if (err_code != NULL)
12992                 *err_code = error;
12993
12994         EM_DEBUG_FUNC_END("ret [%d]", ret);
12995         return ret;
12996 }
12997
12998
12999 INTERNAL_FUNC int emstorage_update_meeting_request(email_meeting_request_t* meeting_req, int transaction, int *err_code)
13000 {
13001         EM_DEBUG_FUNC_BEGIN("meeting_req[%p], transaction[%d], err_code[%p]", meeting_req, transaction, err_code);
13002         
13003         int ret = false;
13004         int error = EMAIL_ERROR_NONE;
13005         int rc;
13006         DB_STMT hStmt = NULL;
13007         char sql_query_string[QUERY_SIZE] = {0, };
13008         time_t temp_unix_time = 0;
13009
13010         if (!meeting_req) {
13011                 EM_DEBUG_EXCEPTION("Invalid Parameter!");
13012                 error = EMAIL_ERROR_INVALID_PARAM;
13013                 goto FINISH_OFF;
13014         }
13015         
13016         sqlite3 *local_db_handle = emstorage_get_db_connection();
13017         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13018
13019         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13020         SNPRINTF(sql_query_string, sizeof(sql_query_string),
13021                 "UPDATE mail_meeting_tbl "
13022                 "SET "
13023                 /* "  account_id = ?, "         //  not update here, this can be changed when move or copy */
13024                 /* "  mailbox_name = ?, "               //  not update here, this can be changed when move or copy */
13025                 "  meeting_response = ?, "
13026                 "  start_time = ?, "
13027                 "  end_time = ?, "
13028                 "  location = ?, "
13029                 "  global_object_id = ?, "
13030                 "  offset = ?, "
13031                 "  standard_name = ?, "
13032                 "  standard_time_start_date = ?, "
13033                 "  standard_bias = ?, "
13034                 "  daylight_name = ?, "
13035                 "  daylight_time_start_date = ?, "
13036                 "  daylight_bias = ? "
13037                 "WHERE mail_id = %d",
13038                 meeting_req->mail_id);  
13039         
13040         EM_DEBUG_LOG("SQL(%s)", sql_query_string);
13041         
13042         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
13043         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13044                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13045 /*
13046         EM_DEBUG_LOG(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
13047         EM_DEBUG_LOG(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
13048         EM_DEBUG_LOG(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
13049         EM_DEBUG_LOG(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
13050         EM_DEBUG_LOG(">>>>> meeting_req->location[%s]", meeting_req->location);
13051         EM_DEBUG_LOG(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
13052         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
13053         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
13054         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
13055         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
13056         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
13057         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
13058         EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
13059 */
13060         int col_index = 0;
13061
13062         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
13063         temp_unix_time = timegm(&(meeting_req->start_time));
13064         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13065         temp_unix_time = timegm(&(meeting_req->end_time));
13066         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13067         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->location, 1, LOCATION_LEN_IN_MAIL_MEETING_TBL);
13068         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->global_object_id, 1, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
13069         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
13070         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.standard_name, 1, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
13071         temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
13072         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13073         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
13074         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.daylight_name, 1, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
13075         temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
13076         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13077         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
13078
13079
13080         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13081         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13082                 ("sqlite3_step fail:%d", rc));
13083         ret = true;
13084
13085
13086 FINISH_OFF:
13087         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13088         
13089         if (hStmt != NULL)  {
13090                 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
13091
13092                 rc = sqlite3_finalize(hStmt);
13093                 if (rc != SQLITE_OK)  {
13094                         EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
13095
13096                         error = EMAIL_ERROR_DB_FAILURE;
13097                 }
13098         }
13099
13100         _DISCONNECT_DB;
13101         
13102         if (err_code != NULL)
13103                 *err_code = error;
13104
13105         EM_DEBUG_FUNC_END("ret [%d]", ret);
13106         return ret;     
13107 }
13108
13109 INTERNAL_FUNC int emstorage_delete_meeting_request(int account_id, int mail_id, int input_mailbox_id, int transaction, int *err_code)
13110 {
13111         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);
13112         
13113         if (account_id < ALL_ACCOUNT || mail_id < 0) {
13114                 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d]", account_id, mail_id);
13115                 
13116                 if (err_code != NULL)
13117                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13118                 return false;
13119         }
13120         
13121         int rc;
13122         int ret = false;
13123         int error = EMAIL_ERROR_NONE;
13124         int and = false;
13125         char sql_query_string[QUERY_SIZE] = {0, };
13126         
13127         sqlite3 *local_db_handle = emstorage_get_db_connection();
13128         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13129         
13130         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_meeting_tbl ");
13131
13132         if (account_id != ALL_ACCOUNT) {                /*  NOT '0' means a specific account. '0' means all account */
13133                 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " WHERE account_id = %d", account_id);
13134                 and = true;
13135         }
13136         if (mail_id > 0) {      
13137                 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " %s mail_id = %d", (and ? "AND" : "WHERE"), mail_id);
13138                 and = true;
13139         }
13140         if (input_mailbox_id > 0) {             /*  0 means all mailbox_id */
13141                 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " %s mailbox_id = '%d'",  (and ? "AND" : "WHERE"), input_mailbox_id);
13142         }
13143         
13144         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13145         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13146                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13147
13148         ret = true;     
13149         
13150 FINISH_OFF:
13151         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13152         _DISCONNECT_DB;
13153         
13154         if (err_code)
13155                 *err_code = error;
13156         
13157         EM_DEBUG_FUNC_END("ret [%d]", ret);
13158         return ret;
13159 }
13160
13161
13162 INTERNAL_FUNC void emstorage_free_meeting_request(email_meeting_request_t *meeting_req)
13163 {
13164         EM_DEBUG_FUNC_BEGIN();
13165         
13166         if (!meeting_req ) {
13167                 EM_DEBUG_EXCEPTION("NULL PARAM");
13168                 return;
13169                 }
13170                 
13171         EM_SAFE_FREE(meeting_req->location);
13172         EM_SAFE_FREE(meeting_req->global_object_id);
13173
13174         EM_DEBUG_FUNC_END();
13175 }
13176
13177
13178 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)
13179 {
13180         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);
13181         EM_PROFILE_BEGIN(profile_emstorage_get_overflowed_mail_id_list);
13182         char sql_query_string[QUERY_SIZE] = {0, };
13183         char **result = NULL;
13184         int rc = -1, ret = false;
13185         int error = EMAIL_ERROR_NONE;
13186         int counter = 0, col_index = 0;
13187         int result_mail_id_count = 0;
13188         int *result_mail_id_list = NULL;
13189
13190         if (input_mailbox_id <= 0 || !mail_id_list || !mail_id_count || account_id < 1) {
13191                 EM_DEBUG_EXCEPTION("Invalid Parameter");
13192                 error = EMAIL_ERROR_INVALID_PARAM;
13193                 goto FINISH_OFF;
13194         }
13195         
13196         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);
13197         
13198         EM_DEBUG_LOG("query[%s].", sql_query_string);
13199
13200         sqlite3 *local_db_handle = emstorage_get_db_connection();
13201         EMSTORAGE_START_READ_TRANSACTION(transaction);
13202         
13203         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_mail_id_count, 0, NULL), rc);
13204         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13205                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13206
13207         if (!result_mail_id_count) {
13208                 EM_DEBUG_LOG("No mail found...");                       
13209                 ret = false;
13210                 error= EMAIL_ERROR_MAIL_NOT_FOUND;
13211                 goto FINISH_OFF;
13212         }
13213         
13214         EM_DEBUG_LOG("There are [%d] overflowed mails in mailbox_id [%d]", result_mail_id_count, input_mailbox_id);
13215         
13216         if (!(result_mail_id_list = (int *)malloc(sizeof(int) * result_mail_id_count))) {
13217                 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
13218                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13219                 sqlite3_free_table(result);
13220                 goto FINISH_OFF;
13221         }
13222
13223         memset(result_mail_id_list, 0x00, sizeof(int) * result_mail_id_count);
13224
13225         col_index = 1;
13226
13227         for (counter = 0; counter < result_mail_id_count; counter++) 
13228                 _get_table_field_data_int(result, result_mail_id_list + counter, col_index++);
13229
13230         ret = true;
13231         
13232 FINISH_OFF:
13233         EM_DEBUG_LOG("finish off [%d]", ret);
13234
13235         if (result)
13236                 sqlite3_free_table(result);
13237
13238         if (ret == true)  {
13239                 *mail_id_list = result_mail_id_list;
13240                 *mail_id_count = result_mail_id_count;
13241         }
13242         else 
13243                 EM_SAFE_FREE(result_mail_id_list);
13244         
13245         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13246         _DISCONNECT_DB;
13247         
13248         if (err_code != NULL)
13249                 *err_code = error;
13250
13251         EM_PROFILE_END(profile_emstorage_get_overflowed_mail_id_list);
13252         EM_DEBUG_FUNC_END("ret [%d]", ret);
13253         return ret;
13254 }
13255
13256
13257 INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(int mail_id, int *thread_id, int *err_code)
13258 {
13259         EM_DEBUG_FUNC_BEGIN("mail_id[%d], thread_id[%p], err_code[%p]", mail_id, thread_id, err_code);
13260         
13261         int rc = -1, ret = false;
13262         int err = EMAIL_ERROR_NONE;
13263         char sql_query_string[QUERY_SIZE] = {0, };
13264         char **result;
13265         int result_count = 0;
13266
13267         if (mail_id == 0 || thread_id == NULL) {
13268                 EM_DEBUG_EXCEPTION("Invalid Parameter");
13269                 if (err_code != NULL)
13270                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13271                 goto FINISH_OFF;
13272         }
13273
13274         sqlite3 *local_db_handle = emstorage_get_db_connection();
13275
13276         memset(sql_query_string, 0, QUERY_SIZE);
13277         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT thread_id FROM mail_tbl WHERE mail_id = %d", mail_id);
13278
13279         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
13280         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
13281         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13282                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13283
13284         if (!result_count) {
13285                 EM_DEBUG_EXCEPTION("No mail found...");                 
13286                 ret = false;
13287                 err= EMAIL_ERROR_MAIL_NOT_FOUND;
13288                 /* sqlite3_free_table(result); */
13289                 goto FINISH_OFF;
13290         }
13291
13292         _get_table_field_data_int(result, thread_id, 1);
13293
13294         sqlite3_free_table(result);
13295         
13296         ret = true;
13297         
13298 FINISH_OFF:
13299
13300         _DISCONNECT_DB;
13301
13302         if (err_code != NULL)
13303                 *err_code = err;
13304
13305         EM_DEBUG_FUNC_END("ret [%d]", ret);
13306         return ret;
13307 }
13308
13309
13310 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)
13311 {
13312         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);
13313         
13314         int rc = -1, ret = false;
13315         int err = EMAIL_ERROR_NONE;
13316         char sql_query_string[QUERY_SIZE] = {0, };
13317         char **result;
13318         int result_count = 0;
13319
13320         if (thread_id == 0) {
13321                 EM_DEBUG_EXCEPTION("Invalid Parameter");
13322                 if (err_code != NULL)
13323                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13324                 goto FINISH_OFF;
13325         }
13326         
13327         sqlite3 *local_db_handle = emstorage_get_db_connection();
13328
13329         if (thread_item_count == 0 || latest_mail_id == 0) {
13330                 memset(sql_query_string, 0, QUERY_SIZE);
13331                 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);
13332
13333                 /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
13334                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
13335                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13336                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13337                 EM_DEBUG_LOG("result_count[%d]", result_count);
13338                 if (result_count == 0) {
13339                         EM_DEBUG_EXCEPTION("No mail found...");                 
13340                         ret = false;
13341                         err= EMAIL_ERROR_MAIL_NOT_FOUND;
13342                         sqlite3_free_table(result);
13343                         goto FINISH_OFF;
13344                 }
13345
13346                 _get_table_field_data_int(result, &latest_mail_id, 2);
13347                 _get_table_field_data_int(result, &thread_item_count, 3);
13348
13349                 EM_DEBUG_LOG("latest_mail_id[%d]", latest_mail_id);
13350                 EM_DEBUG_LOG("thread_item_count[%d]", thread_item_count);
13351
13352                 sqlite3_free_table(result);
13353         }
13354
13355         EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
13356
13357         /* if (thread_item_count > 1) */
13358         /* { */
13359                 memset(sql_query_string, 0, QUERY_SIZE);
13360                 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);
13361                 EM_DEBUG_LOG("query[%s]", sql_query_string);
13362
13363                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13364                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13365                         ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13366         /* } */
13367
13368         memset(sql_query_string, 0, QUERY_SIZE);
13369         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);
13370         EM_DEBUG_LOG("query[%s]", sql_query_string);
13371
13372         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13373                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13374                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13375         
13376         ret = true;
13377         
13378 FINISH_OFF:
13379                 
13380         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
13381         
13382         _DISCONNECT_DB;
13383         
13384         if (err_code != NULL)
13385                 *err_code = err;
13386
13387         EM_DEBUG_FUNC_END("ret [%d]", ret);
13388         return ret;
13389 }
13390
13391
13392 INTERNAL_FUNC void emstorage_flush_db_cache()
13393 {
13394         sqlite3_release_memory(-1);
13395 }
13396
13397 #ifdef __FEATURE_LOCAL_ACTIVITY__
13398 /**
13399   *     emstorage_add_activity - Add Email Local activity during OFFLINE mode
13400   *
13401   */
13402 INTERNAL_FUNC int emstorage_add_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
13403 {
13404         EM_DEBUG_FUNC_BEGIN();
13405         
13406         EM_DEBUG_LOG(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
13407
13408         int rc = -1, ret = false;
13409         int error = EMAIL_ERROR_NONE;
13410         DB_STMT hStmt = NULL;
13411         char sql_query_string[8192] = { 0x00, };
13412         int i = 0;
13413         
13414         if (!local_activity) {
13415                 EM_DEBUG_EXCEPTION(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
13416                 if (err_code != NULL)
13417                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13418                 return false;
13419         }
13420
13421         sqlite3 *local_db_handle = emstorage_get_db_connection();
13422         memset(sql_query_string, 0x00 , sizeof(sql_query_string));
13423         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13424         
13425         SNPRINTF(sql_query_string, sizeof(sql_query_string), "INSERT INTO mail_local_activity_tbl VALUES (?, ?, ?, ?, ?, ?, ?)");
13426
13427         EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ] ", local_activity->activity_id);
13428         EM_DEBUG_LOG(">>>>> MAIL ID [ %d ] ", local_activity->mail_id);
13429         EM_DEBUG_LOG(">>>>> ACCOUNT ID [ %d ] ", local_activity->account_id);
13430         EM_DEBUG_LOG(">>>>> ACTIVITY TYPE [ %d ] ", local_activity->activity_type);
13431         EM_DEBUG_LOG(">>>>> SERVER MAIL ID [ %s ] ", local_activity->server_mailid);
13432         EM_DEBUG_LOG(">>>>> SOURCE MAILBOX [ %s ] ", local_activity->src_mbox);
13433         EM_DEBUG_LOG(">>>>> DEST MAILBOX   [ %s ] ", local_activity->dest_mbox);
13434
13435         EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
13436         
13437         
13438         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
13439         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13440                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13441
13442         EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
13443
13444
13445
13446
13447         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
13448         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
13449         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
13450         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
13451         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->server_mailid, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
13452         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->src_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
13453         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->dest_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
13454         
13455
13456         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13457
13458         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
13459                 ("sqlite3_step fail:%d", rc));
13460         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13461                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
13462
13463         ret = true;
13464         
13465 FINISH_OFF:
13466         
13467         if (hStmt != NULL)  {
13468                 rc = sqlite3_finalize(hStmt);
13469                 if (rc != SQLITE_OK)  {
13470                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
13471                         
13472                         error = EMAIL_ERROR_DB_FAILURE;
13473                 }               
13474         }
13475         else {
13476                 EM_DEBUG_LOG(" >>>>>>>>>> hStmt is NULL!!!");
13477         }
13478
13479         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13480         _DISCONNECT_DB;
13481
13482         
13483         if (err_code != NULL)
13484                 *err_code = error;
13485
13486         EM_DEBUG_FUNC_END("ret [%d]", ret);
13487         return ret;
13488 }
13489
13490 /**
13491   *     emstorage_get_activity - Get the Local activity Information
13492   *     
13493   *
13494   */
13495 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)
13496 {
13497         EM_DEBUG_FUNC_BEGIN();
13498         
13499         int i = 0, count = 0, rc = -1, ret = false;
13500         int error = EMAIL_ERROR_NONE;
13501         emstorage_activity_tbl_t *p_activity_tbl = NULL;
13502         char sql_query_string[1024] = {0x00, };
13503         char **result = NULL;
13504         int col_index ;
13505
13506         EM_IF_NULL_RETURN_VALUE(activity_list, false);
13507         EM_IF_NULL_RETURN_VALUE(select_num, false);
13508
13509         
13510         if (!select_num || !activity_list || account_id <= 0 || activityid < 0) {
13511                 EM_DEBUG_LOG(" select_num[%p], activity_list[%p] account_id [%d] activityid [%d] ", select_num, activity_list, account_id, activityid);
13512                 if (err_code != NULL)
13513                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13514                 return false;
13515         }
13516         
13517         EMSTORAGE_START_READ_TRANSACTION(transaction);
13518
13519         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13520
13521         if (activityid == ALL_ACTIVITIES) {
13522                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d order by activity_id", account_id);
13523         }
13524         else {
13525                 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);
13526         }
13527
13528         EM_DEBUG_LOG("Query [%s]", sql_query_string);
13529
13530                 
13531
13532         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
13533         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13534                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13535
13536
13537         
13538         col_index = 7;
13539         
13540         if (!(p_activity_tbl = (emstorage_activity_tbl_t*)em_malloc(sizeof(emstorage_activity_tbl_t) * count))) {
13541                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
13542                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13543                 goto FINISH_OFF;
13544         }
13545
13546         
13547         for (i = 0; i < count; i++)  {
13548                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13549                 if (result[col_index])
13550                         p_activity_tbl[i].activity_id = atoi(result[col_index++]);
13551
13552                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13553                 if (result[col_index])
13554                         p_activity_tbl[i].account_id = atoi(result[col_index++]);
13555
13556                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13557                 if (result[col_index])
13558                         p_activity_tbl[i].mail_id = atoi(result[col_index++]);
13559
13560                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13561                 if (result[col_index])
13562                         p_activity_tbl[i].activity_type = atoi(result[col_index++]);
13563
13564
13565                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13566                 if (result[col_index] && strlen(result[col_index])>0)
13567                         p_activity_tbl[i].server_mailid = EM_SAFE_STRDUP(result[col_index++]);
13568                 else
13569                         col_index++;
13570
13571                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13572                 if (result[col_index] && strlen(result[col_index])>0)
13573                         p_activity_tbl[i].src_mbox = EM_SAFE_STRDUP(result[col_index++]);
13574                 else
13575                         col_index++;            
13576
13577                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13578                 if (result[col_index] && strlen(result[col_index])>0)
13579                         p_activity_tbl[i].dest_mbox = EM_SAFE_STRDUP(result[col_index++]);
13580                 else
13581                         col_index++;
13582
13583         }
13584
13585         if (result)
13586                 sqlite3_free_table(result);
13587
13588         ret = true;
13589         
13590 FINISH_OFF:
13591
13592         
13593         if (ret == true)  {
13594                 *activity_list = p_activity_tbl;
13595                 *select_num = count;
13596                 EM_DEBUG_LOG(">>>> COUNT : %d >> ", count);
13597         }
13598         else if (p_activity_tbl != NULL) {
13599                 emstorage_free_local_activity(&p_activity_tbl, count, NULL);
13600         }
13601         
13602         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13603         if (err_code != NULL)
13604                 *err_code = error;
13605
13606         EM_DEBUG_FUNC_END("ret [%d]", ret);
13607         return ret;
13608 }
13609
13610
13611 INTERNAL_FUNC int emstorage_get_next_activity_id(int *activity_id, int *err_code)
13612 {
13613
13614         EM_DEBUG_FUNC_BEGIN();
13615         
13616         int ret = false;
13617         int err = EMAIL_ERROR_NONE;
13618         int rc = -1;
13619         char *sql = NULL;
13620         char **result = NULL;   
13621
13622         if (NULL == activity_id)
13623         {
13624                 EM_DEBUG_EXCEPTION(" activity_id[%p]", activity_id);
13625                 
13626                 err = EMAIL_ERROR_INVALID_PARAM;
13627                 goto FINISH_OFF;
13628         }
13629
13630         /*  increase unique id */
13631
13632         sql = "SELECT max(rowid) FROM mail_local_activity_tbl;";
13633         
13634         /*  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); */
13635         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13636                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13637
13638         if (NULL==result[1])
13639                 rc = 1;
13640         else 
13641                 rc = atoi(result[1])+1;
13642
13643         *activity_id = rc;
13644
13645         if (result)
13646                 sqlite3_free_table(result);
13647         
13648         ret = true;
13649         
13650         FINISH_OFF:
13651                 
13652         if (NULL != err_code) {
13653                 *err_code = err;
13654         }
13655         
13656         EM_DEBUG_FUNC_END("ret [%d]", ret);
13657         return ret;
13658
13659 }
13660
13661 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)
13662 {
13663
13664         EM_DEBUG_FUNC_BEGIN();
13665         
13666         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);
13667         
13668         if (account_id <= 0|| NULL == activity_id_list || NULL == activity_id_count ||lowest_activity_type <=0 || highest_activity_type <= 0)  {
13669                 if (err_code != NULL)
13670                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13671                 return false;
13672         }
13673         
13674         int ret = false;
13675         int error = EMAIL_ERROR_NONE;
13676         int i = 0, rc = -1, count = 0;
13677         char sql_query_string[1024] = {0x00, };
13678         int *activity_ids = NULL;
13679         int col_index = 0;
13680         char **result = NULL;
13681         
13682         EMSTORAGE_START_READ_TRANSACTION(transaction);
13683
13684         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13685         
13686         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);
13687
13688         EM_DEBUG_LOG(" Query [%s]", sql_query_string);
13689
13690
13691         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
13692         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13693                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13694
13695         col_index = 1;
13696
13697         EM_DEBUG_LOG(" Activity COUNT : %d ... ", count);
13698
13699         if (NULL == (activity_ids = (int *)em_malloc(sizeof(int) * count))) {
13700                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
13701                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13702                 goto FINISH_OFF;
13703         }
13704
13705         for (i = 0; i < count; i++)  {
13706                 activity_ids[i] = atoi(result[col_index]);
13707                 col_index++;
13708                 EM_DEBUG_LOG("activity_id %d", activity_ids[i]);
13709         }
13710         
13711         ret = true;
13712         
13713 FINISH_OFF:
13714
13715         
13716         if (ret == true)  {
13717                 *activity_id_count = count;
13718                 *activity_id_list = activity_ids;
13719                 
13720         }
13721         else if (activity_ids != NULL) /* Prevent defect - 216566 */
13722                 EM_SAFE_FREE(activity_ids);
13723         
13724
13725         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13726         if (err_code != NULL) {
13727                 *err_code = error;
13728         }
13729
13730         EM_DEBUG_FUNC_END("ret [%d]", ret);
13731         return ret;
13732 }
13733
13734 INTERNAL_FUNC int emstorage_free_activity_id_list(int *activity_id_list, int *error_code)
13735 {
13736         EM_DEBUG_FUNC_BEGIN();
13737
13738         int error = EMAIL_ERROR_NONE;
13739         int ret = false;
13740
13741         EM_DEBUG_LOG(" activity_id_list [%p]", activity_id_list);
13742
13743         if (NULL == activity_id_list) {
13744                 error = EMAIL_ERROR_INVALID_PARAM;
13745                 goto FINISH_OFF;
13746         }
13747         else {
13748                 free(activity_id_list);
13749                 activity_id_list = NULL;
13750         }
13751         
13752
13753         ret= true;
13754
13755         FINISH_OFF:
13756
13757         if (NULL != error_code) {
13758                 *error_code = error;
13759         }
13760
13761         EM_DEBUG_FUNC_END("ret [%d]", ret);
13762         return ret;
13763 }
13764
13765 /**
13766  * emstorage_delete_local_activity - Deletes the Local acitivity Generated based on activity_type
13767  * or based on server mail id
13768  *
13769  */
13770 INTERNAL_FUNC int emstorage_delete_local_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
13771 {
13772         EM_DEBUG_FUNC_BEGIN();
13773
13774
13775         EM_DEBUG_LOG(" local_activity[%p] ", local_activity);
13776         
13777         if (!local_activity)  {
13778                 EM_DEBUG_EXCEPTION(" local_activity[%p] ", local_activity);
13779                 if (err_code != NULL)
13780                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13781                 return false;
13782         }               
13783         
13784         int rc = -1, ret = false;                       /* Prevent_FIX  */
13785         int err = EMAIL_ERROR_NONE;
13786         int query_and = 0;
13787         int query_where = 0;
13788         char sql_query_string[8192] = { 0x00, };
13789         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13790
13791         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13792         
13793         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_local_activity_tbl ");
13794
13795         EM_DEBUG_LOG(">>> Query [ %s ] ", sql_query_string);
13796
13797         if (local_activity->account_id) {
13798                 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1),
13799                 " WHERE account_id = %d ", local_activity->account_id);
13800                 query_and = 1;
13801                 query_where = 1;
13802         }
13803
13804         if (local_activity->server_mailid) {
13805                 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1),
13806                 " %s %s server_mailid = '%s' ", query_where? "": "WHERE", query_and? "AND":"", local_activity->server_mailid);
13807                 query_and = 1;
13808                 query_where = 1;
13809         }
13810
13811
13812         if (local_activity->mail_id) {
13813                 EM_DEBUG_LOG(">>>> MAIL ID [ %d ] , ACTIVITY TYPE [%d ]", local_activity->mail_id, local_activity->activity_type);
13814                 
13815                 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1),
13816                 " %s %s mail_id = %d  ", query_where? "": "WHERE", query_and? "AND":"", local_activity->mail_id);
13817                 
13818                 query_and = 1;
13819                 query_where = 1;
13820                 
13821         }
13822
13823         if (local_activity->activity_type > 0) {
13824                 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1),
13825                 " %s %s activity_type = %d ", query_where? "": "WHERE", query_and? "AND" : "" , local_activity->activity_type);
13826         }
13827
13828         if (local_activity->activity_id > 0) {
13829                 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1),
13830                 " %s %s activity_id = %d ", query_where? "": "WHERE", query_and? "AND" : "" , local_activity->activity_id);
13831
13832         }       
13833
13834         EM_DEBUG_LOG(">>>>> Query [ %s ] ", sql_query_string);
13835         
13836         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13837         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13838                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13839         
13840         rc = sqlite3_changes(local_db_handle);
13841         if (rc == 0)  {
13842                 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
13843                 err = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13844         }
13845
13846         ret = true;
13847         
13848 FINISH_OFF:
13849         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13850         if (err_code != NULL)
13851                 *err_code = err;
13852         EM_DEBUG_FUNC_END("ret [%d]", ret);
13853         return ret;
13854 }
13855
13856 /**
13857 *       emstorage_free_local_activity - Free the Local Activity data
13858 */
13859 INTERNAL_FUNC int emstorage_free_local_activity(emstorage_activity_tbl_t **local_activity_list, int count, int *err_code)
13860 {
13861         EM_DEBUG_FUNC_BEGIN();
13862         
13863         EM_DEBUG_LOG(" local_activity_list[%p], count[%d], err_code[%p]", local_activity_list, count, err_code);
13864         
13865         int ret = false;
13866         int error = EMAIL_ERROR_INVALID_PARAM;
13867         
13868         if (count > 0) {
13869                 if (!local_activity_list || !*local_activity_list) {
13870                         EM_DEBUG_EXCEPTION(" local_activity_list[%p], count[%d]", local_activity_list, count);
13871                         
13872                         error = EMAIL_ERROR_INVALID_PARAM;
13873                         goto FINISH_OFF;
13874                 }
13875                 
13876                 emstorage_activity_tbl_t* p = *local_activity_list;
13877                 int i = 0;
13878                 if (p) {
13879                         for (; i < count; i++)  {
13880                                 EM_SAFE_FREE(p[i].dest_mbox);
13881                                 EM_SAFE_FREE(p[i].src_mbox);
13882                                 EM_SAFE_FREE(p[i].server_mailid);
13883                         }
13884                 
13885                         free(p);        *local_activity_list = NULL;
13886                 }
13887         }
13888         
13889         ret = true;
13890         
13891 FINISH_OFF:
13892         if (err_code != NULL)
13893                 *err_code = error;
13894
13895         EM_DEBUG_FUNC_END("ret [%d]", ret);
13896         return ret;
13897
13898 }
13899 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
13900
13901 static char *_mail_field_name_list[] = {
13902                 "mail_id",
13903                 "account_id",
13904                 "mailbox_id",
13905                 "mailbox_name",
13906                 "mailbox_type",
13907                 "subject",
13908                 "date_time",
13909                 "server_mail_status",
13910                 "server_mailbox_name",
13911                 "server_mail_id",
13912                 "message_id",
13913                 "full_address_from",
13914                 "full_address_to",
13915                 "full_address_cc",
13916                 "full_address_bcc",
13917                 "body_download_status",
13918                 "file_path_plain",
13919                 "file_path_html",
13920                 "file_size",
13921                 "flags_seen_field",
13922                 "flags_deleted_field",
13923                 "flags_flagged_field",
13924                 "flags_answered_field",
13925                 "flags_recent_field",
13926                 "flags_draft_field",
13927                 "flags_forwarded_field",
13928                 "drm_status",
13929                 "priority",
13930                 "save_status",
13931                 "lock_status",
13932                 "report_status",
13933                 "attachment_count",
13934                 "inline_content_count",
13935                 "thread_id",
13936                 "thread_item_count",
13937                 "preview_text",
13938                 "meeting_request_status",
13939                 "message_class",
13940                 "digest_type",
13941                 "smime_type"
13942 };
13943
13944 static char*_get_mail_field_name_by_attribute_type(email_mail_attribute_type input_attribute_type)
13945 {
13946         EM_DEBUG_FUNC_BEGIN("input_attribute_type [%d]", input_attribute_type);
13947
13948         if(input_attribute_type > EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS || input_attribute_type < 0) {
13949                 EM_DEBUG_EXCEPTION("Invalid input_attribute_type [%d]", input_attribute_type);
13950                 return NULL;
13951         }
13952
13953         EM_DEBUG_FUNC_END("return [%s]", _mail_field_name_list[input_attribute_type]);
13954         return _mail_field_name_list[input_attribute_type];
13955 }
13956
13957 static int _get_attribute_type_by_mail_field_name(char *input_mail_field_name, email_mail_attribute_type *output_mail_attribute_type)
13958 {
13959         EM_DEBUG_FUNC_BEGIN("input_mail_field_name [%p], output_mail_attribute_type [%p]", input_mail_field_name, output_mail_attribute_type);
13960         int i = 0;
13961         int err = EMAIL_ERROR_DATA_NOT_FOUND;
13962
13963         if(!input_mail_field_name || !output_mail_attribute_type) {
13964                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
13965                 return EMAIL_ERROR_INVALID_PARAM;
13966         }
13967
13968         for (i = 0; i < EMAIL_MAIL_ATTRIBUTE_END; i++) {
13969                 if (EM_SAFE_STRCMP(input_mail_field_name, _mail_field_name_list[i]) == 0) {
13970                         *output_mail_attribute_type = i;
13971                         err = EMAIL_ERROR_NONE;
13972                         break;
13973                 }
13974         }
13975
13976         EM_DEBUG_FUNC_END("found mail attribute type [%d]", (int)*output_mail_attribute_type);
13977         return err;
13978 }
13979
13980 static int _get_key_value_string_for_list_filter_rule(email_list_filter_rule_t *input_list_filter_rule,char **output_key_value_string)
13981 {
13982         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_key_value_string [%p]", input_list_filter_rule, output_key_value_string);
13983
13984         int  ret = EMAIL_ERROR_NONE;
13985         char key_value_string[QUERY_SIZE] = { 0, };
13986         char *temp_key_value_1 = NULL;
13987         char *temp_key_value_2 = NULL;
13988
13989         if(input_list_filter_rule == NULL || output_key_value_string == NULL) {
13990                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
13991                 return EMAIL_ERROR_INVALID_PARAM;
13992         }
13993
13994         switch(input_list_filter_rule->target_attribute) {
13995         case EMAIL_MAIL_ATTRIBUTE_MAIL_ID                 :
13996         case EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID              :
13997         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID              :
13998         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE            :
13999         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_STATUS      :
14000         case EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS    :
14001         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN         :
14002         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML          :
14003         case EMAIL_MAIL_ATTRIBUTE_FILE_SIZE               :
14004         case EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD        :
14005         case EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD     :
14006         case EMAIL_MAIL_ATTRIBUTE_FLAGS_FLAGGED_FIELD     :
14007         case EMAIL_MAIL_ATTRIBUTE_FLAGS_ANSWERED_FIELD    :
14008         case EMAIL_MAIL_ATTRIBUTE_FLAGS_RECENT_FIELD      :
14009         case EMAIL_MAIL_ATTRIBUTE_FLAGS_DRAFT_FIELD       :
14010         case EMAIL_MAIL_ATTRIBUTE_FLAGS_FORWARDED_FIELD   :
14011         case EMAIL_MAIL_ATTRIBUTE_DRM_STATUS              :
14012         case EMAIL_MAIL_ATTRIBUTE_PRIORITY                :
14013         case EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS             :
14014         case EMAIL_MAIL_ATTRIBUTE_LOCK_STATUS             :
14015         case EMAIL_MAIL_ATTRIBUTE_REPORT_STATUS           :
14016         case EMAIL_MAIL_ATTRIBUTE_ATTACHMENT_COUNT        :
14017         case EMAIL_MAIL_ATTRIBUTE_INLINE_CONTENT_COUNT    :
14018         case EMAIL_MAIL_ATTRIBUTE_THREAD_ID               :
14019         case EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT       :
14020         case EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS  :
14021         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_CLASS           :
14022         case EMAIL_MAIL_ATTRIBUTE_DIGEST_TYPE             :
14023         case EMAIL_MAIL_ATTRIBUTE_SMIME_TYPE              :
14024                 SNPRINTF(key_value_string, QUERY_SIZE, "%d", input_list_filter_rule->key_value.integer_type_value);
14025                 break;
14026
14027         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME            :
14028         case EMAIL_MAIL_ATTRIBUTE_SUBJECT                 :
14029         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME     :
14030         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID          :
14031         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID              :
14032         case EMAIL_MAIL_ATTRIBUTE_FROM                    :
14033         case EMAIL_MAIL_ATTRIBUTE_TO                      :
14034         case EMAIL_MAIL_ATTRIBUTE_CC                      :
14035         case EMAIL_MAIL_ATTRIBUTE_BCC                     :
14036         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT            :
14037                 if(input_list_filter_rule->key_value.string_type_value == NULL) {
14038                         EM_DEBUG_EXCEPTION("Invalid string_type_value [%p]", input_list_filter_rule->key_value.string_type_value);
14039                         ret = EMAIL_ERROR_INVALID_PARAM;
14040                         goto FINISH_OFF;
14041                 }
14042
14043                 temp_key_value_1 = input_list_filter_rule->key_value.string_type_value;
14044
14045                 temp_key_value_2 = em_replace_all_string(temp_key_value_1, "_", "\\_");
14046                 temp_key_value_1 = em_replace_all_string(temp_key_value_2, "%", "\\%");
14047
14048                 if(input_list_filter_rule->rule_type == EMAIL_LIST_FILTER_RULE_INCLUDE)
14049                         SNPRINTF(key_value_string, QUERY_SIZE, "\'%%%s%%\'", temp_key_value_1);
14050                 else
14051                         SNPRINTF(key_value_string, QUERY_SIZE, "\'%s\'", temp_key_value_1);
14052                 break;
14053
14054         case EMAIL_MAIL_ATTRIBUTE_DATE_TIME               :
14055                 SNPRINTF(key_value_string, QUERY_SIZE, "%d", (int)input_list_filter_rule->key_value.datetime_type_value);
14056                 break;
14057
14058         default :
14059                 ret = EMAIL_ERROR_INVALID_PARAM;
14060                 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
14061                 break;
14062         }
14063
14064         if(ret == EMAIL_ERROR_NONE && strlen(key_value_string) > 0) {
14065                 *output_key_value_string = strdup(key_value_string);
14066         }
14067
14068 FINISH_OFF:
14069
14070         EM_SAFE_FREE(temp_key_value_1);
14071         EM_SAFE_FREE(temp_key_value_2);
14072
14073         EM_DEBUG_FUNC_END("ret [%d]", ret);
14074         return ret;
14075 }
14076
14077 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14078 static int _get_cert_password_file_name(int index, char *cert_password_file_name)
14079 {
14080         EM_DEBUG_FUNC_BEGIN("index : [%d]", index);
14081
14082         if (index <= 0 || !cert_password_file_name) {
14083                 EM_DEBUG_EXCEPTION("Invalid parameter");
14084                 return EMAIL_ERROR_INVALID_PARAM;
14085         }
14086
14087         sprintf(cert_password_file_name, ".email_cert_%d", index);
14088
14089         EM_DEBUG_FUNC_END();
14090         return EMAIL_ERROR_NONE;
14091 }
14092 #endif
14093
14094 static int _make_filter_rule_string(email_list_filter_rule_t *input_list_filter_rule, char **output_string)
14095 {
14096         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
14097
14098         int   ret = EMAIL_ERROR_NONE;
14099         int   is_alpha = 0;
14100         int   length_field_name = 0;
14101         int   length_value = 0;
14102         char  result_rule_string[QUERY_SIZE] = { 0 , };
14103         char *mod_field_name_string = NULL;
14104         char *mod_value_string = NULL;
14105         char *temp_field_name_string = NULL;
14106         char *temp_key_value_string = NULL;
14107
14108         if(input_list_filter_rule == NULL || output_string == NULL) {
14109                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14110                 return  EMAIL_ERROR_INVALID_PARAM;
14111         }
14112
14113         temp_field_name_string = _get_mail_field_name_by_attribute_type(input_list_filter_rule->target_attribute);
14114
14115         if(temp_field_name_string == NULL) {
14116                 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
14117                 return EMAIL_ERROR_INVALID_PARAM;
14118         }
14119
14120         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) {
14121                 EM_DEBUG_EXCEPTION("_get_key_value_string_for_list_filter_rule failed");
14122                 return EMAIL_ERROR_INVALID_PARAM;
14123         }
14124
14125         length_field_name = strlen(temp_field_name_string);
14126         length_value      = strlen(temp_key_value_string);
14127
14128         switch(input_list_filter_rule->target_attribute) {
14129         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME            :
14130         case EMAIL_MAIL_ATTRIBUTE_SUBJECT                 :
14131         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME     :
14132         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID          :
14133         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID              :
14134         case EMAIL_MAIL_ATTRIBUTE_FROM                    :
14135         case EMAIL_MAIL_ATTRIBUTE_TO                      :
14136         case EMAIL_MAIL_ATTRIBUTE_CC                      :
14137         case EMAIL_MAIL_ATTRIBUTE_BCC                     :
14138         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT            :
14139                 is_alpha = 1;
14140                 break;
14141         default :
14142                 is_alpha = 0;
14143                 break;
14144         }
14145
14146         if(is_alpha == 1 && input_list_filter_rule->case_sensitivity == false) {
14147                 length_field_name += strlen("UPPER() ");
14148                 length_value      += strlen("UPPER() ");
14149                 mod_field_name_string = em_malloc(sizeof(char) * length_field_name);
14150                 mod_value_string      = em_malloc(sizeof(char) * length_value);
14151                 SNPRINTF(mod_field_name_string, length_field_name, "UPPER(%s)", temp_field_name_string);
14152                 SNPRINTF(mod_value_string,      length_value, "UPPER(%s)", temp_key_value_string);
14153                 EM_SAFE_FREE(temp_key_value_string);
14154         }
14155         else {
14156                 mod_field_name_string = strdup(temp_field_name_string);
14157                 mod_value_string      = temp_key_value_string;
14158         }
14159
14160         switch (input_list_filter_rule->rule_type) {
14161         case EMAIL_LIST_FILTER_RULE_EQUAL     :
14162                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = %s ", mod_field_name_string, mod_value_string);
14163                 break;
14164         case EMAIL_LIST_FILTER_RULE_NOT_EQUAL :
14165                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = %s ", mod_field_name_string, mod_value_string);
14166                 break;
14167         case EMAIL_LIST_FILTER_RULE_LESS_THAN :
14168                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s < %s ", mod_field_name_string, mod_value_string);
14169                 break;
14170         case EMAIL_LIST_FILTER_RULE_GREATER_THAN :
14171                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s > %s ", mod_field_name_string, mod_value_string);
14172                 break;
14173         case EMAIL_LIST_FILTER_RULE_LESS_THAN_OR_EQUAL :
14174                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s <= %s ", mod_field_name_string, mod_value_string);
14175                 break;
14176         case EMAIL_LIST_FILTER_RULE_GREATER_THAN_OR_EQUAL :
14177                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s >= %s ", mod_field_name_string, mod_value_string);
14178                 break;
14179         case EMAIL_LIST_FILTER_RULE_INCLUDE   :
14180                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s LIKE %s ", mod_field_name_string, mod_value_string);
14181                 break;
14182         case EMAIL_LIST_FILTER_RULE_IN        :
14183                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s IN (%s) ", mod_field_name_string, mod_value_string);
14184                 break;
14185         case EMAIL_LIST_FILTER_RULE_NOT_IN    :
14186                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s NOT IN (%s) ", mod_field_name_string, mod_value_string);
14187                 break;
14188         default :
14189                 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
14190                 ret = EMAIL_ERROR_INVALID_PARAM;
14191                 goto FINISH_OFF;
14192         }
14193
14194         *output_string = strdup(result_rule_string);
14195
14196 FINISH_OFF:
14197         EM_SAFE_FREE(mod_field_name_string);
14198         EM_SAFE_FREE(mod_value_string);
14199
14200         EM_DEBUG_FUNC_END("ret [%d]", ret);
14201         return ret;
14202 }
14203
14204 static int _make_order_rule_string(email_list_sorting_rule_t *input_sorting_rule, char **output_string) {
14205         EM_DEBUG_FUNC_BEGIN("input_sorting_rule [%p], output_string [%p]", input_sorting_rule, output_string);
14206
14207         char  result_rule_string[QUERY_SIZE] = { 0 , };
14208         int   ret = EMAIL_ERROR_NONE;
14209
14210         if(input_sorting_rule->force_boolean_check) {
14211                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = 0 ", _get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
14212         }
14213         else
14214                 EM_SAFE_STRCPY(result_rule_string, _get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
14215
14216         switch (input_sorting_rule->sort_order) {
14217                 case EMAIL_SORT_ORDER_ASCEND     :
14218                         EM_SAFE_STRCAT(result_rule_string, " ASC ");
14219                         break;
14220
14221                 case EMAIL_SORT_ORDER_DESCEND    :
14222                         EM_SAFE_STRCAT(result_rule_string, " DESC ");
14223                         break;
14224
14225                 default :
14226                         EM_DEBUG_EXCEPTION("Invalid sort_order [%d]", input_sorting_rule->sort_order);
14227                         ret = EMAIL_ERROR_INVALID_PARAM;
14228                         goto FINISH_OFF;
14229         }
14230
14231         *output_string = strdup(result_rule_string);
14232
14233 FINISH_OFF:
14234         EM_DEBUG_FUNC_END("ret [%d]", ret);
14235         return ret;
14236 }
14237
14238 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)
14239 {
14240         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);
14241         int ret = EMAIL_ERROR_NONE;
14242         int i = 0;
14243         char conditional_clause_string[QUERY_SIZE] = {0, };
14244         char *result_string_for_a_item = NULL;
14245
14246         if ( (input_filter_count > 0 && !input_filter_list) || (input_sorting_rule_count > 0 && !input_sorting_rule_list) || output_conditional_clause == NULL) {
14247                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14248                 return EMAIL_ERROR_INVALID_PARAM;
14249         }
14250
14251         if(input_filter_count > 0) {
14252                 strcpy(conditional_clause_string,  " WHERE ");
14253
14254                 for ( i = 0; i < input_filter_count; i++) {
14255                         switch (input_filter_list[i].list_filter_item_type) {
14256                         case EMAIL_LIST_FILTER_ITEM_RULE :
14257                                 EM_DEBUG_LOG("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE", i);
14258                                 _make_filter_rule_string(&(input_filter_list[i].list_filter_item.rule), &result_string_for_a_item);
14259                                 break;
14260
14261                         case EMAIL_LIST_FILTER_ITEM_OPERATOR :
14262                                 EM_DEBUG_LOG("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_OPERATOR", i);
14263                                 switch(input_filter_list[i].list_filter_item.operator_type) {
14264                                 case EMAIL_LIST_FILTER_OPERATOR_AND :
14265                                         result_string_for_a_item = strdup("AND ");
14266                                         break;
14267                                 case EMAIL_LIST_FILTER_OPERATOR_OR :
14268                                         result_string_for_a_item = strdup("OR ");
14269                                         break;
14270                                 case EMAIL_LIST_FILTER_OPERATOR_LEFT_PARENTHESIS :
14271                                         result_string_for_a_item = strdup(" (");
14272                                         break;
14273                                 case EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS :
14274                                         result_string_for_a_item = strdup(") ");
14275                                         break;
14276                                 }
14277                                 break;
14278
14279                         default :
14280                                 EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", input_filter_list[i].list_filter_item_type);
14281                                 ret = EMAIL_ERROR_INVALID_PARAM;
14282                                 goto FINISH_OFF;
14283                         }
14284
14285                         if(result_string_for_a_item == NULL) {
14286                                 EM_DEBUG_EXCEPTION("result_string_for_a_item is null");
14287                                 ret = EMAIL_ERROR_INVALID_PARAM;
14288                                 goto FINISH_OFF;
14289                         }
14290
14291                         if(strlen(conditional_clause_string) + strlen(result_string_for_a_item) >= QUERY_SIZE) {
14292                                 EM_DEBUG_EXCEPTION("Query is too long");
14293                                 ret = EMAIL_ERROR_DATA_TOO_LONG;
14294                                 goto FINISH_OFF;
14295                         }
14296                         strcat(conditional_clause_string, result_string_for_a_item);
14297                         EM_SAFE_FREE(result_string_for_a_item);
14298                 }
14299         }
14300
14301         if(input_sorting_rule_count > 0) {
14302                 strcat(conditional_clause_string, "ORDER BY ");
14303
14304                 for ( i = 0; i < input_sorting_rule_count; i++) {
14305                         if( (ret = _make_order_rule_string(&input_sorting_rule_list[i], &result_string_for_a_item)) != EMAIL_ERROR_NONE) {
14306                                 EM_DEBUG_EXCEPTION("_make_order_rule_string failed. [%d]", ret);
14307                                 goto FINISH_OFF;
14308                         }
14309                         if(i > 0)
14310                                 strcat(conditional_clause_string, ", ");
14311                         strcat(conditional_clause_string, result_string_for_a_item);
14312                         EM_SAFE_FREE(result_string_for_a_item);
14313                 }
14314         }
14315
14316         *output_conditional_clause = strdup(conditional_clause_string);
14317
14318 FINISH_OFF:
14319         EM_SAFE_FREE(result_string_for_a_item);
14320
14321         EM_DEBUG_FUNC_END("ret [%d]", ret);
14322         return ret;
14323 }
14324
14325 INTERNAL_FUNC int emstorage_free_list_filter(email_list_filter_t **input_filter_list, int input_filter_count)
14326 {
14327         EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d]", input_filter_list, input_filter_count);
14328         int err = EMAIL_ERROR_NONE;
14329         int i = 0;
14330         email_list_filter_t *temp_filter_list = NULL;
14331
14332         EM_IF_NULL_RETURN_VALUE(input_filter_list, EMAIL_ERROR_INVALID_PARAM);
14333
14334         for ( i = 0; i < input_filter_count; i++) {
14335                 temp_filter_list = (*input_filter_list) + i;
14336                 if(temp_filter_list && temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE) {
14337                         switch(temp_filter_list->list_filter_item.rule.target_attribute) {
14338                         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME :
14339                         case EMAIL_MAIL_ATTRIBUTE_SUBJECT :
14340                         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME :
14341                         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID :
14342                         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID :
14343                         case EMAIL_MAIL_ATTRIBUTE_FROM :
14344                         case EMAIL_MAIL_ATTRIBUTE_TO :
14345                         case EMAIL_MAIL_ATTRIBUTE_CC :
14346                         case EMAIL_MAIL_ATTRIBUTE_BCC :
14347                         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN :
14348                         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML :
14349                         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT :
14350                                 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule.key_value.string_type_value);
14351                                 break;
14352                         default :
14353                                 break;
14354                         }
14355                 }
14356         }
14357
14358         free(*input_filter_list);
14359         *input_filter_list = NULL;
14360
14361         EM_DEBUG_FUNC_END("err [%d]", err);
14362         return err;
14363 }
14364
14365 INTERNAL_FUNC int emstorage_add_certificate(emstorage_certificate_tbl_t *certificate, int transaction, int *err_code)
14366 {
14367         EM_DEBUG_FUNC_BEGIN("certificate:[%p], transaction:[%d], err_code:[%p]", certificate, transaction, err_code);
14368
14369         if (!certificate) {
14370                 EM_DEBUG_EXCEPTION("certificate:[%p], transaction:[%d], err_code:[%p]", certificate, transaction, err_code);
14371                 if (err_code != NULL)
14372                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14373
14374                 return false;
14375         }
14376
14377         int rc = -1, ret = false;
14378         int error = EMAIL_ERROR_NONE;
14379         DB_STMT hStmt = NULL;
14380         char sql_query_string[QUERY_SIZE] = {0, };
14381 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14382         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
14383 #endif
14384         sqlite3 *local_db_handle = emstorage_get_db_connection();
14385
14386         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
14387
14388         char *sql = "SELECT max(rowid) FROM mail_certificate_tbl;";
14389         char **result = NULL;
14390
14391         /*  rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
14392         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
14393         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14394                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
14395
14396         if (NULL==result[1]) rc = 1;
14397         else rc = atoi(result[1])+1;
14398         sqlite3_free_table(result);
14399
14400         certificate->certificate_id = rc;
14401 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14402         if ((error = _get_cert_password_file_name(certificate->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
14403                 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
14404                 goto FINISH_OFF;
14405         }
14406 #endif
14407         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14408                 "INSERT INTO mail_certificate_tbl VALUES "
14409                 "(        "
14410                 "   ?  "                /* Index of certificate */
14411                 "  , ? "                /* Select the account */
14412                 "  , ? "                /* Year of issue */
14413                 "  , ? "                /* Month of issue */
14414                 "  , ? "                /* Day of issue */
14415                 "  , ? "                /* Year of expiration */
14416                 "  , ? "                /* Month of expiration */
14417                 "  , ? "                /* Day of expiration */
14418                 "  , ? "                /* Organization of issue */
14419                 "  , ? "                /* Email address */
14420                 "  , ? "                /* Subject of certificate */
14421                 "  , ? "                /* Name of saved certificate */
14422                 ") ");
14423
14424
14425         /*  rc = sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL); */
14426         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
14427         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14428                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14429
14430         EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
14431         _bind_stmt_field_data_int(hStmt, CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->certificate_id);
14432         _bind_stmt_field_data_int(hStmt, ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_year);
14433         _bind_stmt_field_data_int(hStmt, ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_month);
14434         _bind_stmt_field_data_int(hStmt, ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_day);
14435         _bind_stmt_field_data_int(hStmt, EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_year);
14436         _bind_stmt_field_data_int(hStmt, EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_month);
14437         _bind_stmt_field_data_int(hStmt, EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_day);
14438         _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);
14439         _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->email_address, 0, EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL);
14440         _bind_stmt_field_data_string(hStmt, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->subject_str, 0, SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL);
14441         _bind_stmt_field_data_string(hStmt, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->filepath, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
14442         /*  rc = sqlite3_step(hStmt); */
14443         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14444
14445         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
14446                 ("sqlite3_step fail:%d", rc));
14447         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14448                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
14449 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14450         if (ssm_write_buffer(certificate->password, strlen(certificate->password), cert_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
14451                 EM_DEBUG_EXCEPTION("ssm_write_buffer failed - Private certificate password : [%s]", cert_password_file_name);
14452                 error = EMAIL_ERROR_SYSTEM_FAILURE;
14453                 goto FINISH_OFF;
14454         }
14455 #endif
14456         ret = true;
14457
14458         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
14459
14460         if (!emstorage_notify_storage_event(NOTI_CERTIFICATE_ADD, certificate->certificate_id, 0, NULL, 0))
14461                 EM_DEBUG_EXCEPTION("emstorage_notify_storage_event(NOTI_CERTIFICATE_ADD] : Notification failed");
14462
14463 FINISH_OFF:
14464
14465         if (hStmt != NULL)  {
14466                 rc = sqlite3_finalize(hStmt);
14467                 if (rc != SQLITE_OK)  {
14468                         EM_DEBUG_LOG("sqlite3_finalize failed - %d", rc);
14469                         error = EMAIL_ERROR_DB_FAILURE;
14470                 }
14471         }
14472         else
14473                 EM_DEBUG_LOG("hStmt is NULL!!!");
14474
14475         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
14476         _DISCONNECT_DB;
14477
14478         if (err_code != NULL)
14479                 *err_code = error;
14480
14481         EM_DEBUG_FUNC_END("ret [%d]", ret);
14482         return ret;
14483 }
14484
14485 INTERNAL_FUNC int emstorage_free_certificate(emstorage_certificate_tbl_t **certificate_list, int count, int *err_code)
14486 {
14487         EM_DEBUG_FUNC_BEGIN("certificate_list[%p], count[%d], err_code[%p]", certificate_list, count, err_code);
14488         
14489         int ret = false;
14490         int error = EMAIL_ERROR_NONE;
14491         
14492         if (count > 0)  {
14493                 if (!certificate_list || !*certificate_list)  {
14494                         EM_DEBUG_EXCEPTION("certificate_list[%p], count[%d]", certificate_list, count);
14495                         error = EMAIL_ERROR_INVALID_PARAM;
14496                         goto FINISH_OFF;
14497                 }
14498                 
14499                 emstorage_certificate_tbl_t *p = *certificate_list;
14500                 int i = 0;
14501                 
14502                 for (; i < count; i++)  {
14503                         EM_SAFE_FREE(p[i].issue_organization_name);
14504                         EM_SAFE_FREE(p[i].email_address);
14505                         EM_SAFE_FREE(p[i].subject_str);
14506                         EM_SAFE_FREE(p[i].filepath);
14507                         EM_SAFE_FREE(p[i].password);
14508                 }
14509                 
14510                 EM_SAFE_FREE(p); 
14511                 *certificate_list = NULL;
14512         }
14513         
14514         ret = true;
14515         
14516 FINISH_OFF:
14517         if (err_code != NULL)
14518                 *err_code = error;
14519
14520         EM_DEBUG_FUNC_END("ret [%d]", ret);
14521         return ret;
14522 }
14523
14524 INTERNAL_FUNC int emstorage_get_certificate_list(int *select_num, emstorage_certificate_tbl_t **certificate_list, int transaction, int with_password, int *err_code)
14525 {
14526         EM_DEBUG_FUNC_BEGIN();
14527
14528         int i = 0, count = 0, rc = -1, ret = false;
14529         int error = EMAIL_ERROR_NONE;
14530         emstorage_certificate_tbl_t *p_data_tbl = NULL;
14531
14532         DB_STMT hStmt = NULL;
14533
14534         if (!select_num || !certificate_list)  {
14535                 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, certificate_list);
14536                 if (err_code != NULL)
14537                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14538                 return false;
14539         }
14540
14541         sqlite3 *local_db_handle = emstorage_get_db_connection();
14542         EMSTORAGE_START_READ_TRANSACTION(transaction);
14543
14544         char sql_query_string[QUERY_SIZE] = {0, };
14545         char *sql = "SELECT count(*) FROM mail_certificate_tbl;";
14546         char **result;
14547
14548         /*  rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
14549         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
14550         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14551                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
14552
14553         count = atoi(result[1]);
14554         sqlite3_free_table(result);
14555
14556         if (!count) {
14557                 EM_DEBUG_EXCEPTION("no account found...");
14558                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
14559                 ret = true;
14560                 goto FINISH_OFF;
14561         }
14562         EM_DEBUG_LOG("count = %d", rc);
14563         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_certificate_tbl ORDER BY account_id");
14564
14565         /*  rc = sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL);   */
14566         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
14567
14568         EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
14569         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14570                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14571
14572         /*  rc = sqlite3_step(hStmt); */
14573         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14574         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14575                 ("sqlite3_step fail:%d", rc));
14576         if (rc == SQLITE_DONE)  {
14577                 EM_DEBUG_EXCEPTION("no account found...");
14578
14579                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
14580                 count = 0;
14581                 ret = true;
14582                 goto FINISH_OFF;
14583         }
14584
14585         if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t) * count)))  {
14586                 EM_DEBUG_EXCEPTION("malloc failed...");
14587                 error = EMAIL_ERROR_OUT_OF_MEMORY;
14588                 goto FINISH_OFF;
14589         }
14590         memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t) * count);
14591         for (i = 0; i < count; i++)  {
14592                 /*  get recordset */
14593                 _get_stmt_field_data_int(hStmt,  &(p_data_tbl[i].certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
14594                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
14595                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
14596                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
14597                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
14598                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
14599                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
14600                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
14601                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
14602                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
14603                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
14604                 if (with_password == true) {
14605 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14606                         /*  get password from the secure storage */
14607                         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
14608
14609                         EM_SAFE_FREE(p_data_tbl[i].password);
14610
14611                         /*  get password file name */
14612                         if ((error = _get_cert_password_file_name(p_data_tbl[i].certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
14613                                 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
14614                                 goto FINISH_OFF;
14615                         }
14616
14617                         /*  read password from secure storage */
14618                         if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl[i].password))) < 0) {
14619                                 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage() failed...");
14620                                 goto FINISH_OFF;
14621                         }
14622                         EM_DEBUG_LOG("recv_password_file_name[%s], password[%s]", cert_password_file_name,  p_data_tbl[i].password);
14623 #endif
14624                 }
14625
14626                 /*  rc = sqlite3_step(hStmt); */
14627                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14628                 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i,  rc);
14629                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14630                         ("sqlite3_step fail:%d", rc));
14631         }
14632
14633         ret = true;
14634
14635 FINISH_OFF:
14636         if (ret == true)  {
14637                 *certificate_list = p_data_tbl;
14638                 *select_num = count;
14639                 EM_DEBUG_LOG("COUNT : %d", count);
14640         }
14641         else if (p_data_tbl != NULL)
14642                 emstorage_free_certificate(&p_data_tbl, count, NULL);
14643         if (hStmt != NULL)  {
14644                 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
14645
14646                 rc = sqlite3_finalize(hStmt);
14647                 hStmt = NULL;
14648                 if (rc != SQLITE_OK)  {
14649                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
14650                         error = EMAIL_ERROR_DB_FAILURE;
14651                 }
14652         }
14653
14654         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14655         _DISCONNECT_DB;
14656
14657         if (err_code != NULL)
14658                 *err_code = error;
14659
14660         EM_DEBUG_FUNC_END("ret [%d]", ret);
14661         return ret;
14662 }
14663
14664 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)
14665 {
14666         EM_DEBUG_FUNC_BEGIN("email_address[%s], certificate[%p], transaction[%d], err_code[%p]", email_address, certificate, transaction, err_code);
14667
14668         if (!certificate)  {
14669                 EM_DEBUG_EXCEPTION("email_address[%s], certificate[%p]", email_address, certificate);
14670                 if (err_code != NULL)
14671                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14672                 return false;
14673         }
14674
14675         int ret = false;
14676         int error = EMAIL_ERROR_NONE;
14677         emstorage_certificate_tbl_t *p_data_tbl = NULL;
14678         DB_STMT hStmt = NULL;
14679         char sql_query_string[QUERY_SIZE] = {0, };
14680         int rc = -1;
14681         int sql_len = 0;
14682 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14683         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
14684 #endif          
14685         sqlite3 *local_db_handle = emstorage_get_db_connection();
14686         EMSTORAGE_START_READ_TRANSACTION(transaction);
14687
14688         /*  Make query string */
14689         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
14690         sql_len = strlen(sql_query_string);
14691                 
14692         /*  dummy value, FROM WHERE clause */
14693         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "* FROM mail_certificate_tbl WHERE email_address = '%s'", email_address);
14694
14695         /*  FROM clause */
14696         EM_DEBUG_LOG("query = [%s]", sql_query_string);
14697
14698         /*  execute a sql and count rows */
14699         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
14700         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14701                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14702         
14703         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14704         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14705                 ("sqlite3_step fail:%d", rc));
14706
14707         if (rc == SQLITE_DONE)  {
14708                 EM_DEBUG_EXCEPTION("no matched certificate found...");
14709                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
14710                 goto FINISH_OFF;
14711         }
14712
14713         /*  Assign query result to structure */
14714         if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t))))  {
14715                 EM_DEBUG_EXCEPTION("malloc failed...");
14716                 error = EMAIL_ERROR_OUT_OF_MEMORY;
14717                 goto FINISH_OFF;
14718         }
14719
14720         memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t));
14721         _get_stmt_field_data_int(hStmt,  &(p_data_tbl->certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
14722         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
14723         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
14724         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
14725         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
14726         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
14727         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
14728         _get_stmt_field_data_string(hStmt, &(p_data_tbl->issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
14729         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
14730         _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
14731         _get_stmt_field_data_string(hStmt, &(p_data_tbl->filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
14732
14733         if (with_password) {
14734 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14735                 /*  get password file name */
14736                 if ((error = _get_cert_password_file_name(p_data_tbl->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
14737                         EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
14738                         goto FINISH_OFF;
14739                 }               
14740
14741                 /*  read password from secure storage */
14742                 if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl->password))) != EMAIL_ERROR_NONE) {
14743                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed...");
14744                         goto FINISH_OFF;
14745                 }
14746                 EM_DEBUG_LOG("cert_password_file_name[%s], password[%s]", cert_password_file_name,  p_data_tbl->password);
14747 #endif
14748         }
14749         ret = true;
14750
14751 FINISH_OFF:
14752         if (ret == true)
14753                 *certificate = p_data_tbl;
14754         else {
14755                 if (p_data_tbl)
14756                         emstorage_free_certificate((emstorage_certificate_tbl_t **)&p_data_tbl, 1, NULL);
14757         }
14758         if (hStmt != NULL)  {
14759                 rc = sqlite3_finalize(hStmt);
14760                 if (rc != SQLITE_OK)  {
14761                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
14762                         error = EMAIL_ERROR_DB_FAILURE;
14763                 }
14764         }
14765         
14766         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14767         _DISCONNECT_DB;
14768
14769         if (err_code != NULL)
14770                 *err_code = error;
14771
14772         EM_DEBUG_FUNC_END("ret [%d]", ret);
14773         return ret;
14774 }
14775
14776 INTERNAL_FUNC int emstorage_get_certificate_by_index(int index, emstorage_certificate_tbl_t **certificate, int transaction, int with_password, int *err_code)
14777 {
14778         EM_DEBUG_FUNC_BEGIN("index[%d], certificate[%p], transaction[%d], err_code[%p]", index, certificate, transaction, err_code);
14779
14780         if (!certificate)  {
14781                 EM_DEBUG_EXCEPTION("index[%d], account[%p]", index, certificate);
14782                 if (err_code != NULL)
14783                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14784                 return false;
14785         }
14786
14787         int ret = false;
14788         int error = EMAIL_ERROR_NONE;
14789         emstorage_certificate_tbl_t *p_data_tbl = NULL;
14790         DB_STMT hStmt = NULL;
14791         char sql_query_string[QUERY_SIZE] = {0, };
14792         int rc = -1;
14793         int sql_len = 0;
14794 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14795         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
14796 #endif          
14797         sqlite3 *local_db_handle = emstorage_get_db_connection();
14798         EMSTORAGE_START_READ_TRANSACTION(transaction);
14799
14800         /*  Make query string */
14801         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
14802         sql_len = strlen(sql_query_string);
14803                 
14804         /*  dummy value, FROM WHERE clause */
14805         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "* FROM mail_certificate_tbl WHERE certificate_id = %d", index);
14806
14807         /*  FROM clause */
14808         EM_DEBUG_LOG("query = [%s]", sql_query_string);
14809
14810         /*  execute a sql and count rows */
14811         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
14812         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14813                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14814         
14815         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14816         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14817                 ("sqlite3_step fail:%d", rc));
14818
14819         if (rc == SQLITE_DONE)  {
14820                 EM_DEBUG_EXCEPTION("no matched certificate found...");
14821                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
14822                 goto FINISH_OFF;
14823         }
14824
14825         /*  Assign query result to structure */
14826         if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t))))  {
14827                 EM_DEBUG_EXCEPTION("malloc failed...");
14828                 error = EMAIL_ERROR_OUT_OF_MEMORY;
14829                 goto FINISH_OFF;
14830         }
14831
14832         memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t));
14833
14834         _get_stmt_field_data_int(hStmt,  &(p_data_tbl->certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
14835         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
14836         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
14837         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
14838         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
14839         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
14840         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
14841         _get_stmt_field_data_string(hStmt, &(p_data_tbl->issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
14842         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
14843         _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
14844         _get_stmt_field_data_string(hStmt, &(p_data_tbl->filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
14845
14846         if (with_password) {
14847 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14848                 /*  get password file name */
14849                 if ((error = _get_cert_password_file_name(p_data_tbl->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
14850                         EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
14851                         goto FINISH_OFF;
14852                 }               
14853
14854                 /*  read password from secure storage */
14855                 if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl->password))) != EMAIL_ERROR_NONE) {
14856                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed...");
14857                         goto FINISH_OFF;
14858                 }
14859                 EM_DEBUG_LOG("cert_password_file_name[%s], password[%s]", cert_password_file_name,  p_data_tbl->password);
14860 #endif
14861         }
14862         ret = true;
14863
14864 FINISH_OFF:
14865         if (ret == true)
14866                 *certificate = p_data_tbl;
14867         else {
14868                 if (p_data_tbl)
14869                         emstorage_free_certificate((emstorage_certificate_tbl_t **)&p_data_tbl, 1, NULL);
14870         }
14871         if (hStmt != NULL)  {
14872                 rc = sqlite3_finalize(hStmt);
14873                 if (rc != SQLITE_OK)  {
14874                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
14875                         error = EMAIL_ERROR_DB_FAILURE;
14876                 }
14877         }
14878         
14879         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14880         _DISCONNECT_DB;
14881
14882         if (err_code != NULL)
14883                 *err_code = error;
14884
14885         EM_DEBUG_FUNC_END("ret [%d]", ret);
14886         return ret;
14887 }
14888
14889 INTERNAL_FUNC int emstorage_delete_certificate(int certificate_id, int transaction, int *err_code)
14890 {
14891         EM_DEBUG_FUNC_BEGIN("certificate_id[%d], transaction[%d], err_code[%p]", certificate_id, transaction, err_code);
14892         
14893         if (certificate_id < 1)  {      
14894                 EM_DEBUG_EXCEPTION(" certificate_id[%d]", certificate_id);
14895                 
14896                 if (err_code != NULL)
14897                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14898                 return false;
14899         }
14900         
14901         int rc = -1, ret = false;
14902         int error = EMAIL_ERROR_NONE;
14903         sqlite3 *local_db_handle = emstorage_get_db_connection();
14904         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
14905
14906         /*  TODO : delete password files - file names can be obtained from db or a rule that makes a name */
14907         DB_STMT hStmt = NULL;
14908         char sql_query_string[QUERY_SIZE] = {0, };
14909 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__  
14910         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
14911         /*  get password file name */
14912         if ((error = _get_cert_password_file_name(certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
14913                 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
14914                 goto FINISH_OFF;
14915         }
14916 #endif
14917         /*  delete from db */
14918         memset(sql_query_string, 0x00, sizeof(sql_query_string));
14919         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_certificate_tbl WHERE certificate_id = %d", certificate_id);
14920  
14921         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
14922         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
14923                 ("sqlite3_exec fail:%d", rc));
14924         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14925                 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14926
14927         /*  validate account existence */
14928         rc = sqlite3_changes(local_db_handle);
14929         if (rc == 0)  {
14930                 EM_DEBUG_EXCEPTION(" no matched certificate found...");
14931                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
14932                 goto FINISH_OFF;
14933         }
14934 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__  
14935         /*  delete from secure storage */
14936         if (ssm_delete_file(cert_password_file_name,  SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
14937                 EM_DEBUG_EXCEPTION(" ssm_delete_file failed -cert password : file[%s]", cert_password_file_name);
14938                 error = EMAIL_ERROR_SYSTEM_FAILURE;
14939                 goto FINISH_OFF;                
14940         }
14941 #endif  
14942         ret = true;
14943
14944 FINISH_OFF:
14945  
14946         if (hStmt != NULL)  {
14947                 rc = sqlite3_finalize(hStmt);
14948                 if (rc != SQLITE_OK)  {
14949                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
14950                         error = EMAIL_ERROR_DB_FAILURE;
14951                 }
14952         }
14953
14954         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
14955         _DISCONNECT_DB;
14956
14957         if (err_code != NULL)
14958                 *err_code = error;
14959
14960         EM_DEBUG_FUNC_END("ret [%d]", ret);
14961         return ret;
14962 }
14963
14964 INTERNAL_FUNC int emstorage_update_certificate(int certificate_id, emstorage_certificate_tbl_t *certificate, int transaction, int *err_code)
14965 {
14966         EM_DEBUG_FUNC_BEGIN("certificate_id[%d], certificate[%p], transaction[%d], err_code[%p]", certificate_id, certificate, transaction, err_code);
14967         
14968         if (certificate_id < 1)  {      
14969                 EM_DEBUG_EXCEPTION(" certificate_id[%d]", certificate_id);
14970                 
14971                 if (err_code != NULL)
14972                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14973                 return false;
14974         }
14975         
14976         int error = EMAIL_ERROR_NONE;
14977         int rc, ret = false;
14978  
14979         DB_STMT hStmt = NULL;
14980         char sql_query_string[QUERY_SIZE] = {0, };
14981 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__  
14982         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
14983 #endif
14984         sqlite3 *local_db_handle = emstorage_get_db_connection();
14985         EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
14986
14987         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14988                 "UPDATE mail_certificate_tbl SET"
14989                 " issue_year = ?"
14990                 ", issue_month = ?"             /* Index of certificate */
14991                 ", issue_day = ?"               /* Select the account */
14992                 ", expiration_year = ?"         /* Year of issue */
14993                 ", expiration_month = ?"                /* Month of issue */
14994                 ", expiration_day = ?"          /* Day of issue */
14995                 ", issue_organization_name = ?"         /* Year of expiration */
14996                 ", email_address = ?"           /* Month of expiration */
14997                 ", subject_str = ?"             /* Day of expiration */
14998                 ", filepath = ?"                /* Organization of issue */
14999                 ", password = ?"
15000                 " WHERE certificate_id = ?");
15001         
15002         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
15003         EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
15004         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15005                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15006         
15007         _bind_stmt_field_data_int(hStmt, ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_year);
15008         _bind_stmt_field_data_int(hStmt, ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_month);
15009         _bind_stmt_field_data_int(hStmt, ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_day);
15010         _bind_stmt_field_data_int(hStmt, EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_year);
15011         _bind_stmt_field_data_int(hStmt, EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_month);
15012         _bind_stmt_field_data_int(hStmt, EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_day);
15013         _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);
15014         _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->email_address, 0, EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL);
15015         _bind_stmt_field_data_string(hStmt, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->subject_str, 0, SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL);
15016         _bind_stmt_field_data_string(hStmt, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->filepath, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
15017  
15018         /*  rc = sqlite3_step(hStmt); */
15019         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15020         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
15021                 ("sqlite3_step fail:%d", rc));
15022         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15023                 ("sqlite3_step fail:%d", rc));
15024         
15025         /*  validate account existence */
15026         rc = sqlite3_changes(local_db_handle);
15027         if (rc == 0) {
15028                 EM_DEBUG_EXCEPTION(" no matched account found...");
15029         
15030                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
15031                 goto FINISH_OFF;
15032         }
15033         
15034 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__  
15035         /*  get password file name */
15036         if ((error = _get_cert_password_file_name(certificate->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
15037                 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
15038                 goto FINISH_OFF;
15039         }               
15040
15041         /*  save passwords to the secure storage */
15042         if (ssm_write_buffer(certificate->password, strlen(certificate->password), cert_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
15043                 EM_DEBUG_EXCEPTION("ssm_write_buffer failed - Private certificate password : [%s]", cert_password_file_name);
15044                 error = EMAIL_ERROR_SYSTEM_FAILURE;
15045                 goto FINISH_OFF;
15046         }
15047 #endif  
15048         if (!emstorage_notify_storage_event(NOTI_CERTIFICATE_UPDATE, certificate->certificate_id, 0, NULL, 0))
15049                 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event[ NOTI_CERTIFICATE_UPDATE] : Notification Failed >>> ");
15050         
15051         ret = true;
15052         
15053 FINISH_OFF:
15054  
15055         if (hStmt != NULL)  {
15056                 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
15057
15058                 rc = sqlite3_finalize(hStmt);
15059                 if (rc != SQLITE_OK)  {
15060                         EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
15061                         error = EMAIL_ERROR_DB_FAILURE;
15062                 }
15063         }
15064
15065         EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
15066         
15067         _DISCONNECT_DB;
15068         
15069         if (err_code != NULL)
15070                 *err_code = error;
15071
15072         EM_DEBUG_FUNC_END("ret [%d]", ret);
15073         return ret;
15074 }
15075
15076 /*EOF*/