Tizen 2.0 Release
[platform/core/messaging/email-service.git] / email-daemon / email-daemon-etc.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21
22
23
24 /******************************************************************************
25  * File: emf-etc.c
26  * Desc: email-daemon Etc Implementations
27  *
28  * Auth:
29  *
30  * History:
31  *    2006.08.16 : created
32  *****************************************************************************/
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <vconf.h>
37 #include "email-daemon.h"
38 #include "email-daemon-account.h"
39 #include "email-debug-log.h"
40 #include "email-internal-types.h"
41 #include "email-core-account.h"
42 #include "email-core-event.h"
43 #include "email-core-utils.h"
44 #include "email-utilities.h"
45 #include "email-storage.h"
46
47
48 int emdaemon_register_event_callback(email_action_t action, email_event_callback callback, void* event_data)
49 {
50         return emcore_register_event_callback(action, callback, event_data);
51 }
52
53 int emdaemon_unregister_event_callback(email_action_t action, email_event_callback callback)
54 {
55         return emcore_unregister_event_callback(action, callback);
56 }
57
58 INTERNAL_FUNC void emdaemon_get_event_queue_status(int* on_sending, int* on_receiving)
59 {
60         emcore_get_event_queue_status(on_sending, on_receiving);
61 }
62
63 INTERNAL_FUNC int emdaemon_get_pending_job(email_action_t action, int account_id, int mail_id, email_event_status_type_t* status)
64 {
65         EM_DEBUG_FUNC_BEGIN("action[%d], account_id[%d], mail_id[%d]", action, account_id, mail_id);
66
67         return emcore_get_pending_event(action, account_id, mail_id, status);
68 }
69
70 INTERNAL_FUNC int emdaemon_cancel_job(int account_id, int handle, int* err_code)
71 {
72         EM_DEBUG_FUNC_BEGIN("account_id[%d], handle[%d], err_code[%p]", account_id, handle, err_code);
73         
74         int ret = false;
75         int err = EMAIL_ERROR_NONE;
76         
77         
78         if (!emcore_cancel_thread(handle, NULL, &err))  {
79                 EM_DEBUG_EXCEPTION("emcore_cancel_thread failed [%d]", err);
80                 goto FINISH_OFF;
81         }
82         
83         ret = true;
84         
85 FINISH_OFF:
86         if (err_code != NULL)
87                 *err_code = err;
88         EM_DEBUG_FUNC_END();
89         return ret;
90 }
91
92
93 INTERNAL_FUNC int emdaemon_cancel_sending_mail_job(int account_id, int mail_id, int* err_code)
94 {
95         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], err_code[%p]", account_id, mail_id, err_code);
96         
97         int ret = false;
98         int err = EMAIL_ERROR_NONE;     
99         int handle = 0;
100         email_account_t* ref_account = NULL;
101         
102         if (account_id <= 0)  {
103                 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d]", account_id, mail_id);
104                 err = EMAIL_ERROR_INVALID_PARAM;
105                 goto FINISH_OFF;
106         }
107
108 #ifdef __FEATURE_PROGRESS_IN_OUTBOX__
109
110         /*      h.gahlaut@samsung.com: Moved this code from email_cancel_sending_mail API to email-service engine
111                 since this code has update DB operation which is failing in context of email application process 
112                 with an sqlite error -> sqlite3_step fail:8 */
113                 
114         /*      which means #define SQLITE_READONLY   8 */  /* Attempt to write a readonly database */ 
115         emstorage_mail_tbl_t *mail_tbl_data = NULL;
116
117         if (!emstorage_get_mail_by_id(mail_id, &mail_tbl_data, false, &err))  {
118                 EM_DEBUG_EXCEPTION("emcore_get_mail failed [%d]", err);
119                 goto FINISH_OFF;
120         }
121
122         if (mail_tbl_data) {
123                 if (mail_tbl_data->save_status == EMAIL_MAIL_STATUS_SEND_CANCELED) {
124                         EM_DEBUG_EXCEPTION(">>>> EMAIL_MAIL_STATUS_SEND_CANCELED Already set for Mail ID [ %d ]", mail_id);
125                         goto FINISH_OFF;
126                 }
127                 else {                  
128                         mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SEND_CANCELED;
129
130                         if(!emstorage_set_field_of_mails_with_integer_value(mail_tbl_data->account_id, &mail_id, 1, "save_status", EMAIL_MAIL_STATUS_SEND_CANCELED, true, &err)) {
131                                 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]",err);
132                                 goto FINISH_OFF;
133                         }
134                 }
135         }
136
137 #endif
138
139         if(!emcore_get_handle_by_mailId_from_transaction_info(mail_id , &handle )) {
140                 EM_DEBUG_EXCEPTION("emcore_get_handle_by_mailId_from_transaction_info failed for mail_id[%d]", mail_id);
141                 err = EMAIL_ERROR_HANDLE_NOT_FOUND;
142                 goto FINISH_OFF;
143         }
144
145         if (!(ref_account = emcore_get_account_reference(account_id)))  {
146                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
147                 err = EMAIL_ERROR_INVALID_ACCOUNT;
148                 goto FINISH_OFF;
149         }
150
151         if (!emcore_cancel_send_mail_thread(handle, NULL, &err))  {
152                 EM_DEBUG_EXCEPTION("emcore_cancel_send_mail_thread failed [%d]", err);
153                 goto FINISH_OFF;
154         }
155         
156         if(!emcore_delete_transaction_info_by_mailId(mail_id))
157                 EM_DEBUG_EXCEPTION("emcore_delete_transaction_info_by_mailId failed for mail_id[%d]", mail_id);
158         
159         ret = true;
160         
161 FINISH_OFF:
162         if(err_code != NULL)
163                 *err_code = err;
164         
165         if (ref_account) {
166                 emcore_free_account(ref_account);
167                 EM_SAFE_FREE(ref_account);
168         }
169
170 #ifdef __FEATURE_PROGRESS_IN_OUTBOX__
171         if(!emstorage_free_mail(&mail_tbl_data, 1, &err))
172                 EM_DEBUG_EXCEPTION("emcore_free_mail Failed [%d ]", err);       
173
174 #endif
175         EM_DEBUG_FUNC_END();
176         return ret;
177 }       
178
179 static char *_make_time_string_to_time_t(time_t time)
180 {
181         char *time_string = NULL;
182         struct tm *struct_time = NULL;
183
184         if (!time) {
185                 EM_DEBUG_EXCEPTION("Invalid paramter");
186                 return NULL;
187         }
188
189         time_string = em_malloc(MAX_DATETIME_STRING_LENGTH);
190         if (time_string == NULL) {
191                 EM_DEBUG_EXCEPTION("em_malloc failed");
192                 return NULL;
193         }
194
195         struct_time = localtime(&time);
196         SNPRINTF(time_string, MAX_DATETIME_STRING_LENGTH, "%d/%d/%d", struct_time->tm_mon + 1, struct_time->tm_mday, struct_time->tm_year + 1900);
197
198         EM_DEBUG_LOG("time string = [%s]", time_string);
199         return time_string;     
200 }
201
202 static char *_make_criteria_to_search_filter(email_search_filter_t *search_filter, int search_filter_count, int *err_code)
203 {
204         EM_DEBUG_FUNC_BEGIN("search_filter : [%p], search_filter_count : [%d]", search_filter, search_filter_count);
205
206         int i = 0;
207         int err = EMAIL_ERROR_NONE;
208         char *criteria = NULL;
209         char *temp_criteria = NULL;
210         char *time_string = NULL;
211         
212         if (search_filter == NULL || search_filter_count < 0) {
213                 EM_DEBUG_EXCEPTION("Invalid paramter");
214                 err = EMAIL_ERROR_INVALID_PARAM;
215                 goto FINISH_OFF;
216         }
217
218         criteria = (char *)em_malloc(STRING_LENGTH_FOR_DISPLAY * search_filter_count);
219         if (criteria == NULL) {
220                 EM_DEBUG_EXCEPTION("em_malloc failed");
221                 err = EMAIL_ERROR_OUT_OF_MEMORY;
222                 goto FINISH_OFF;
223         }
224
225         temp_criteria = (char *)em_malloc(STRING_LENGTH_FOR_DISPLAY);
226         if (temp_criteria == NULL) {
227                 EM_DEBUG_EXCEPTION("em_malloc failed");
228                 err = EMAIL_ERROR_OUT_OF_MEMORY;
229                 goto FINISH_OFF;
230         }
231
232         for (i = 0; i < search_filter_count; i++) {
233                 EM_DEBUG_LOG("search_filter_type [%d]", search_filter[i].search_filter_type);
234                 memset(temp_criteria, 0x00, STRING_LENGTH_FOR_DISPLAY);
235
236                 switch (search_filter[i].search_filter_type) {
237                 case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_NO :
238                 case EMAIL_SEARCH_FILTER_TYPE_UID :
239                 case EMAIL_SEARCH_FILTER_TYPE_SIZE_LARSER :
240                 case EMAIL_SEARCH_FILTER_TYPE_SIZE_SMALLER :
241                 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_ANSWERED :
242                 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DELETED :
243                 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DRAFT :
244                 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_FLAGED :
245                 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_RECENT :
246                 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_SEEN :
247                         EM_DEBUG_LOG("integer_type_key_value [%d]", search_filter[i].search_filter_key_value.integer_type_key_value);
248                         break;
249
250                 case EMAIL_SEARCH_FILTER_TYPE_BCC :
251                 case EMAIL_SEARCH_FILTER_TYPE_CC :
252                 case EMAIL_SEARCH_FILTER_TYPE_TO :
253                 case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID :
254                         EM_DEBUG_LOG("string_type_key_value [%s]", search_filter[i].search_filter_key_value.string_type_key_value);
255                         break;
256
257                 case EMAIL_SEARCH_FILTER_TYPE_FROM :
258                         EM_DEBUG_LOG("string_type_key_value [%s]", search_filter[i].search_filter_key_value.string_type_key_value);
259                         SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "from %s ", search_filter[i].search_filter_key_value.string_type_key_value);
260                         strncat(criteria, temp_criteria, EM_SAFE_STRLEN(temp_criteria));
261                         break;
262
263                 case EMAIL_SEARCH_FILTER_TYPE_SUBJECT :
264                         EM_DEBUG_LOG("string_type_key_value [%s]", search_filter[i].search_filter_key_value.string_type_key_value);
265                         SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "subject %s ", search_filter[i].search_filter_key_value.string_type_key_value);
266                         strncat(criteria, temp_criteria, EM_SAFE_STRLEN(temp_criteria));
267                         break;
268
269                 case EMAIL_SEARCH_FILTER_TYPE_KEYWORD :
270                         EM_DEBUG_LOG("string_type_key_value [%s]", search_filter[i].search_filter_key_value.string_type_key_value);
271                         SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "keyword %s ", search_filter[i].search_filter_key_value.string_type_key_value);
272                         strncat(criteria, temp_criteria, EM_SAFE_STRLEN(temp_criteria));
273                         break;
274
275                 case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_BEFORE :
276                         EM_DEBUG_LOG("time_type_key_value [%d]", search_filter[i].search_filter_key_value.time_type_key_value);
277                         time_string = _make_time_string_to_time_t(search_filter[i].search_filter_key_value.time_type_key_value);
278                         SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "before %s ", time_string);
279                         strncat(criteria, temp_criteria, EM_SAFE_STRLEN(temp_criteria));
280                         break;
281         
282                 case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_ON :
283                         EM_DEBUG_LOG("time_type_key_value [%d]", search_filter[i].search_filter_key_value.time_type_key_value);
284                         break;
285
286                 case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_SINCE :
287                         EM_DEBUG_LOG("time_type_key_value [%d]", search_filter[i].search_filter_key_value.time_type_key_value);
288                         time_string = _make_time_string_to_time_t(search_filter[i].search_filter_key_value.time_type_key_value);
289                         SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "since %s ", time_string);
290                         strncat(criteria, temp_criteria, EM_SAFE_STRLEN(temp_criteria));
291                         break;
292
293                 default :
294                         EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", search_filter);
295                         err = EMAIL_ERROR_INVALID_PARAM;
296                         goto FINISH_OFF;
297                 }
298                 EM_SAFE_FREE(time_string); /*prevent 26258*/
299         }
300
301 FINISH_OFF:
302         
303         EM_SAFE_FREE(temp_criteria);
304         EM_SAFE_FREE(time_string);
305
306         if (err_code != NULL)
307                 *err_code = err;
308                 
309         EM_DEBUG_FUNC_END();
310         return criteria;
311 }
312
313 INTERNAL_FUNC int emdaemon_search_mail_on_server(int input_account_id, int input_mailbox_id, email_search_filter_t *input_search_filter, int input_search_filter_count, unsigned int *output_handle, int *err_code)
314 {
315         EM_DEBUG_FUNC_BEGIN("input_account_id [%d], mailbox_id [%d], input_search_filter [%p], input_search_filter_count [%d], output_handle [%p]", input_account_id, input_mailbox_id, input_search_filter, input_search_filter_count, output_handle);
316         int error = EMAIL_ERROR_NONE;
317         int ret = false;
318         char *criteria = NULL;
319         
320         if (input_mailbox_id == 0 || input_account_id < 0) {
321                 EM_DEBUG_EXCEPTION("Invalid parameter");
322                 error = EMAIL_ERROR_INVALID_PARAM;
323                 return false;
324         }
325
326         email_event_t event_data;
327
328         memset(&event_data, 0x00, sizeof(email_event_t));
329
330         criteria = _make_criteria_to_search_filter(input_search_filter, input_search_filter_count, &error);
331         if (criteria == NULL) {
332                 EM_DEBUG_EXCEPTION("_make_criteria_to_search_filter failed");
333                 goto FINISH_OFF;
334         }
335                 
336         event_data.type = EMAIL_EVENT_SEARCH_ON_SERVER;
337         event_data.account_id = input_account_id;
338         event_data.event_param_data_1 = EM_SAFE_STRDUP(criteria);
339         event_data.event_param_data_4 = input_mailbox_id;
340
341         if (!emcore_insert_event(&event_data, (int *)output_handle, &error)) {
342                 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", error);
343                 error = EMAIL_ERROR_NONE;
344                 goto FINISH_OFF;
345         }
346
347         ret = true;
348
349 FINISH_OFF:
350         if (!ret) {
351                 EM_SAFE_FREE(event_data.event_param_data_1);
352         }
353
354         EM_SAFE_FREE(criteria);
355         
356         if (err_code != NULL)
357                 *err_code = error;
358
359         EM_DEBUG_FUNC_END("error [%d]", error);
360         return ret;
361 }
362
363 INTERNAL_FUNC int emdaemon_clear_all_mail_data(int* err_code)
364 {
365         EM_DEBUG_FUNC_BEGIN();
366         
367         int ret = false;
368         int error = EMAIL_ERROR_NONE;
369         
370         if (emdaemon_initialize(&error)) {
371                 if (!emstorage_clear_mail_data(true, &error))
372                         EM_DEBUG_EXCEPTION("emstorage_clear_mail_data failed [%d]", error);
373         }
374         else {
375                 EM_DEBUG_EXCEPTION("emdaemon_initialize failed [%d]", error);
376                 if (err_code)
377                         *err_code = error;
378                 return false;
379         }
380
381         emcore_display_unread_in_badge();
382
383         ret = true;
384
385         if (!emstorage_create_table(EMAIL_CREATE_DB_NORMAL, &error)) 
386                 EM_DEBUG_EXCEPTION("emstorage_create_table failed [%d]", error);
387         
388         emdaemon_finalize(&error);
389         
390         if (err_code)
391                 *err_code = error;
392         EM_DEBUG_FUNC_END();
393     return ret;
394 }
395
396         
397 /* --------------------------------------------------------------------------------*/