4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 /******************************************************************************
26 * Desc: email-daemon Etc Implementations
31 * 2006.08.16 : created
32 *****************************************************************************/
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-core-alarm.h"
45 #include "email-core-smtp.h"
46 #include "email-utilities.h"
47 #include "email-storage.h"
50 int emdaemon_register_event_callback(email_action_t action, email_event_callback callback, void* event_data)
52 return emcore_register_event_callback(action, callback, event_data);
55 int emdaemon_unregister_event_callback(email_action_t action, email_event_callback callback)
57 return emcore_unregister_event_callback(action, callback);
60 INTERNAL_FUNC void emdaemon_get_event_queue_status(int* on_sending, int* on_receiving)
62 emcore_get_event_queue_status(on_sending, on_receiving);
65 INTERNAL_FUNC int emdaemon_get_pending_job(email_action_t action, int account_id, int mail_id, email_event_status_type_t* status)
67 EM_DEBUG_FUNC_BEGIN("action[%d], account_id[%d], mail_id[%d]", action, account_id, mail_id);
69 return emcore_get_pending_event(action, account_id, mail_id, status);
72 INTERNAL_FUNC int emdaemon_cancel_job(int account_id, int handle, int* err_code)
74 EM_DEBUG_FUNC_BEGIN("account_id[%d], handle[%d], err_code[%p]", account_id, handle, err_code);
77 int err = EMAIL_ERROR_NONE;
80 if (!emcore_cancel_thread(handle, NULL, &err)) {
81 EM_DEBUG_EXCEPTION("emcore_cancel_thread failed [%d]", err);
95 INTERNAL_FUNC int emdaemon_cancel_sending_mail_job(int account_id, int mail_id, int* err_code)
97 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], err_code[%p]", account_id, mail_id, err_code);
100 int err = EMAIL_ERROR_NONE;
102 email_account_t* ref_account = NULL;
104 if (account_id <= 0) {
105 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d]", account_id, mail_id);
106 err = EMAIL_ERROR_INVALID_PARAM;
110 #ifdef __FEATURE_PROGRESS_IN_OUTBOX__
112 /* h.gahlaut@samsung.com: Moved this code from email_cancel_sending_mail API to email-service engine
113 since this code has update DB operation which is failing in context of email application process
114 with an sqlite error -> sqlite3_step fail:8 */
116 /* which means #define SQLITE_READONLY 8 */ /* Attempt to write a readonly database */
117 emstorage_mail_tbl_t *mail_tbl_data = NULL;
119 if (!emstorage_get_mail_by_id(mail_id, &mail_tbl_data, false, &err)) {
120 EM_DEBUG_EXCEPTION("emcore_get_mail failed [%d]", err);
125 if (mail_tbl_data->save_status == EMAIL_MAIL_STATUS_SEND_CANCELED) {
126 EM_DEBUG_EXCEPTION(">>>> EMAIL_MAIL_STATUS_SEND_CANCELED Already set for Mail ID [ %d ]", mail_id);
130 mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SEND_CANCELED;
132 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)) {
133 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]",err);
141 if ((err = emcore_delete_alram_data_by_reference_id(EMAIL_ALARM_CLASS_SCHEDULED_SENDING, mail_id)) != EMAIL_ERROR_NONE) {
142 EM_DEBUG_LOG("emcore_delete_alram_data_by_reference_id failed [%d]",err);
147 if(!emcore_get_handle_by_mailId_from_transaction_info(mail_id , &handle )) {
148 EM_DEBUG_EXCEPTION("emcore_get_handle_by_mailId_from_transaction_info failed for mail_id[%d]", mail_id);
149 err = EMAIL_ERROR_HANDLE_NOT_FOUND;
153 if (!(ref_account = emcore_get_account_reference(account_id))) {
154 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
155 err = EMAIL_ERROR_INVALID_ACCOUNT;
159 if (!emcore_cancel_send_mail_thread(handle, NULL, &err)) {
160 EM_DEBUG_EXCEPTION("emcore_cancel_send_mail_thread failed [%d]", err);
164 if(!emcore_delete_transaction_info_by_mailId(mail_id))
165 EM_DEBUG_EXCEPTION("emcore_delete_transaction_info_by_mailId failed for mail_id[%d]", mail_id);
174 emcore_free_account(ref_account);
175 EM_SAFE_FREE(ref_account);
178 #ifdef __FEATURE_PROGRESS_IN_OUTBOX__
179 if(!emstorage_free_mail(&mail_tbl_data, 1, &err))
180 EM_DEBUG_EXCEPTION("emcore_free_mail Failed [%d ]", err);
187 static char *_make_time_string_to_time_t(time_t time)
189 char *time_string = NULL;
190 struct tm *struct_time = NULL;
193 EM_DEBUG_EXCEPTION("Invalid paramter");
197 time_string = em_malloc(MAX_DATETIME_STRING_LENGTH);
198 if (time_string == NULL) {
199 EM_DEBUG_EXCEPTION("em_malloc failed");
203 struct_time = localtime(&time);
204 SNPRINTF(time_string, MAX_DATETIME_STRING_LENGTH, "%d/%d/%d", struct_time->tm_mon + 1, struct_time->tm_mday, struct_time->tm_year + 1900);
206 EM_DEBUG_LOG("time string = [%s]", time_string);
210 static char *_make_criteria_to_search_filter(email_search_filter_t *search_filter, int search_filter_count, int *err_code)
212 EM_DEBUG_FUNC_BEGIN("search_filter : [%p], search_filter_count : [%d]", search_filter, search_filter_count);
215 int err = EMAIL_ERROR_NONE;
216 char *criteria = NULL;
217 char *temp_criteria = NULL;
218 char *time_string = NULL;
220 if (search_filter == NULL || search_filter_count < 0) {
221 EM_DEBUG_EXCEPTION("Invalid paramter");
222 err = EMAIL_ERROR_INVALID_PARAM;
226 criteria = (char *)em_malloc(STRING_LENGTH_FOR_DISPLAY * search_filter_count);
227 if (criteria == NULL) {
228 EM_DEBUG_EXCEPTION("em_malloc failed");
229 err = EMAIL_ERROR_OUT_OF_MEMORY;
233 temp_criteria = (char *)em_malloc(STRING_LENGTH_FOR_DISPLAY);
234 if (temp_criteria == NULL) {
235 EM_DEBUG_EXCEPTION("em_malloc failed");
236 err = EMAIL_ERROR_OUT_OF_MEMORY;
240 for (i = 0; i < search_filter_count; i++) {
241 EM_DEBUG_LOG("search_filter_type [%d]", search_filter[i].search_filter_type);
242 memset(temp_criteria, 0x00, STRING_LENGTH_FOR_DISPLAY);
244 switch (search_filter[i].search_filter_type) {
245 case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_NO :
246 case EMAIL_SEARCH_FILTER_TYPE_UID :
247 case EMAIL_SEARCH_FILTER_TYPE_SIZE_LARSER :
248 case EMAIL_SEARCH_FILTER_TYPE_SIZE_SMALLER :
249 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_ANSWERED :
250 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DELETED :
251 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DRAFT :
252 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_FLAGED :
253 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_RECENT :
254 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_SEEN :
255 EM_DEBUG_LOG("integer_type_key_value [%d]", search_filter[i].search_filter_key_value.integer_type_key_value);
258 case EMAIL_SEARCH_FILTER_TYPE_BCC :
259 case EMAIL_SEARCH_FILTER_TYPE_CC :
260 case EMAIL_SEARCH_FILTER_TYPE_TO :
261 case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID :
262 EM_DEBUG_LOG("string_type_key_value [%s]", search_filter[i].search_filter_key_value.string_type_key_value);
265 case EMAIL_SEARCH_FILTER_TYPE_FROM :
266 EM_DEBUG_LOG("string_type_key_value [%s]", search_filter[i].search_filter_key_value.string_type_key_value);
267 SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "from %s ", search_filter[i].search_filter_key_value.string_type_key_value);
268 strncat(criteria, temp_criteria, EM_SAFE_STRLEN(temp_criteria));
271 case EMAIL_SEARCH_FILTER_TYPE_SUBJECT :
272 EM_DEBUG_LOG("string_type_key_value [%s]", search_filter[i].search_filter_key_value.string_type_key_value);
273 SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "subject %s ", search_filter[i].search_filter_key_value.string_type_key_value);
274 strncat(criteria, temp_criteria, EM_SAFE_STRLEN(temp_criteria));
277 case EMAIL_SEARCH_FILTER_TYPE_KEYWORD :
278 EM_DEBUG_LOG("string_type_key_value [%s]", search_filter[i].search_filter_key_value.string_type_key_value);
279 SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "keyword %s ", search_filter[i].search_filter_key_value.string_type_key_value);
280 strncat(criteria, temp_criteria, EM_SAFE_STRLEN(temp_criteria));
283 case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_BEFORE :
284 EM_DEBUG_LOG("time_type_key_value [%d]", search_filter[i].search_filter_key_value.time_type_key_value);
285 time_string = _make_time_string_to_time_t(search_filter[i].search_filter_key_value.time_type_key_value);
286 SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "before %s ", time_string);
287 strncat(criteria, temp_criteria, EM_SAFE_STRLEN(temp_criteria));
290 case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_ON :
291 EM_DEBUG_LOG("time_type_key_value [%d]", search_filter[i].search_filter_key_value.time_type_key_value);
294 case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_SINCE :
295 EM_DEBUG_LOG("time_type_key_value [%d]", search_filter[i].search_filter_key_value.time_type_key_value);
296 time_string = _make_time_string_to_time_t(search_filter[i].search_filter_key_value.time_type_key_value);
297 SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "since %s ", time_string);
298 strncat(criteria, temp_criteria, EM_SAFE_STRLEN(temp_criteria));
302 EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", search_filter);
303 err = EMAIL_ERROR_INVALID_PARAM;
306 EM_SAFE_FREE(time_string); /*prevent 26258*/
311 EM_SAFE_FREE(temp_criteria);
312 EM_SAFE_FREE(time_string);
314 if (err_code != NULL)
321 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)
323 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);
324 int error = EMAIL_ERROR_NONE;
326 char *criteria = NULL;
328 if (input_mailbox_id == 0 || input_account_id < 0) {
329 EM_DEBUG_EXCEPTION("Invalid parameter");
330 error = EMAIL_ERROR_INVALID_PARAM;
334 email_event_t event_data;
336 memset(&event_data, 0x00, sizeof(email_event_t));
338 criteria = _make_criteria_to_search_filter(input_search_filter, input_search_filter_count, &error);
339 if (criteria == NULL) {
340 EM_DEBUG_EXCEPTION("_make_criteria_to_search_filter failed");
344 event_data.type = EMAIL_EVENT_SEARCH_ON_SERVER;
345 event_data.account_id = input_account_id;
346 event_data.event_param_data_1 = EM_SAFE_STRDUP(criteria);
347 event_data.event_param_data_4 = input_mailbox_id;
349 if (!emcore_insert_event(&event_data, (int *)output_handle, &error)) {
350 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", error);
351 error = EMAIL_ERROR_NONE;
359 EM_SAFE_FREE(event_data.event_param_data_1);
362 EM_SAFE_FREE(criteria);
364 if (err_code != NULL)
367 EM_DEBUG_FUNC_END("error [%d]", error);
371 INTERNAL_FUNC int emdaemon_reschedule_sending_mail()
373 EM_DEBUG_FUNC_BEGIN();
374 int err = EMAIL_ERROR_NONE;
375 char *conditional_clause_string = NULL;
376 email_list_filter_t filter_list[3];
377 email_mail_list_item_t *result_mail_list = NULL;
378 int filter_rule_count = 3;
379 int result_mail_count = 0;
382 memset(filter_list, 0 , sizeof(email_list_filter_t) * filter_rule_count);
384 filter_list[0].list_filter_item_type = EMAIL_LIST_FILTER_ITEM_RULE;
385 filter_list[0].list_filter_item.rule.target_attribute = EMAIL_MAIL_ATTRIBUTE_SCHEDULED_SENDING_TIME;
386 filter_list[0].list_filter_item.rule.rule_type = EMAIL_LIST_FILTER_RULE_NOT_EQUAL;
387 filter_list[0].list_filter_item.rule.key_value.integer_type_value = 0;
389 filter_list[1].list_filter_item_type = EMAIL_LIST_FILTER_ITEM_OPERATOR;
390 filter_list[1].list_filter_item.operator_type = EMAIL_LIST_FILTER_OPERATOR_AND;
392 filter_list[2].list_filter_item_type = EMAIL_LIST_FILTER_ITEM_RULE;
393 filter_list[2].list_filter_item.rule.target_attribute = EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS;
394 filter_list[2].list_filter_item.rule.rule_type = EMAIL_LIST_FILTER_RULE_EQUAL;
395 filter_list[2].list_filter_item.rule.key_value.integer_type_value = EMAIL_MAIL_STATUS_SEND_SCHEDULED;
397 /* Get scheduled mail list */
398 if( (err = emstorage_write_conditional_clause_for_getting_mail_list(filter_list, filter_rule_count, NULL, 0, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
399 EM_DEBUG_EXCEPTION("emstorage_write_conditional_clause_for_getting_mail_list failed[%d]", err);
403 EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
405 if(!emstorage_query_mail_list(conditional_clause_string, true, &result_mail_list, &result_mail_count, &err) && !result_mail_list) {
406 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", err);
410 /* Add alarm for scheduled mail */
411 for(i = 0; i < result_mail_count; i++) {
412 if((err = emcore_schedule_sending_mail(result_mail_list[i].mail_id, result_mail_list[i].scheduled_sending_time)) != EMAIL_ERROR_NONE) {
413 EM_DEBUG_EXCEPTION("emcore_schedule_sending_mail failed [%d]", err);
419 EM_SAFE_FREE(result_mail_list);
421 EM_DEBUG_FUNC_END("err [%d]", err);
425 INTERNAL_FUNC int emdaemon_clear_all_mail_data(int* err_code)
427 EM_DEBUG_FUNC_BEGIN();
430 int error = EMAIL_ERROR_NONE;
432 if (emdaemon_initialize(&error)) {
433 if (!emstorage_clear_mail_data(true, &error))
434 EM_DEBUG_EXCEPTION("emstorage_clear_mail_data failed [%d]", error);
437 EM_DEBUG_EXCEPTION("emdaemon_initialize failed [%d]", error);
443 emcore_display_unread_in_badge();
447 if (!emstorage_create_table(EMAIL_CREATE_DB_NORMAL, &error))
448 EM_DEBUG_EXCEPTION("emstorage_create_table failed [%d]", error);
450 emdaemon_finalize(&error);
458 /* --------------------------------------------------------------------------------*/