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-utilities.h"
45 #include "email-storage.h"
48 int emdaemon_register_event_callback(email_action_t action, email_event_callback callback, void* event_data)
50 return emcore_register_event_callback(action, callback, event_data);
53 int emdaemon_unregister_event_callback(email_action_t action, email_event_callback callback)
55 return emcore_unregister_event_callback(action, callback);
58 INTERNAL_FUNC void emdaemon_get_event_queue_status(int* on_sending, int* on_receiving)
60 emcore_get_event_queue_status(on_sending, on_receiving);
63 INTERNAL_FUNC int emdaemon_get_pending_job(email_action_t action, int account_id, int mail_id, email_event_status_type_t* status)
65 EM_DEBUG_FUNC_BEGIN("action[%d], account_id[%d], mail_id[%d]", action, account_id, mail_id);
67 return emcore_get_pending_event(action, account_id, mail_id, status);
70 INTERNAL_FUNC int emdaemon_cancel_job(int account_id, int handle, int* err_code)
72 EM_DEBUG_FUNC_BEGIN("account_id[%d], handle[%d], err_code[%p]", account_id, handle, err_code);
75 int err = EMAIL_ERROR_NONE;
78 if (!emcore_cancel_thread(handle, NULL, &err)) {
79 EM_DEBUG_EXCEPTION("emcore_cancel_thread failed [%d]", err);
93 INTERNAL_FUNC int emdaemon_cancel_sending_mail_job(int account_id, int mail_id, int* err_code)
95 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], err_code[%p]", account_id, mail_id, err_code);
98 int err = EMAIL_ERROR_NONE;
100 email_account_t* ref_account = NULL;
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;
108 #ifdef __FEATURE_PROGRESS_IN_OUTBOX__
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 */
114 /* which means #define SQLITE_READONLY 8 */ /* Attempt to write a readonly database */
115 emstorage_mail_tbl_t *mail_tbl_data = NULL;
117 if (!emstorage_get_mail_by_id(mail_id, &mail_tbl_data, false, &err)) {
118 EM_DEBUG_EXCEPTION("emcore_get_mail failed [%d]", err);
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);
128 mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SEND_CANCELED;
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);
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;
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;
151 if (!emcore_cancel_send_mail_thread(handle, NULL, &err)) {
152 EM_DEBUG_EXCEPTION("emcore_cancel_send_mail_thread failed [%d]", err);
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);
166 emcore_free_account(ref_account);
167 EM_SAFE_FREE(ref_account);
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);
179 static char *_make_time_string_to_time_t(time_t time)
181 char *time_string = NULL;
182 struct tm *struct_time = NULL;
185 EM_DEBUG_EXCEPTION("Invalid paramter");
189 time_string = em_malloc(MAX_DATETIME_STRING_LENGTH);
190 if (time_string == NULL) {
191 EM_DEBUG_EXCEPTION("em_malloc failed");
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);
198 EM_DEBUG_LOG("time string = [%s]", time_string);
202 static char *_make_criteria_to_search_filter(email_search_filter_t *search_filter, int search_filter_count, int *err_code)
204 EM_DEBUG_FUNC_BEGIN("search_filter : [%p], search_filter_count : [%d]", search_filter, search_filter_count);
207 int err = EMAIL_ERROR_NONE;
208 char *criteria = NULL;
209 char *temp_criteria = NULL;
210 char *time_string = NULL;
212 if (search_filter == NULL || search_filter_count < 0) {
213 EM_DEBUG_EXCEPTION("Invalid paramter");
214 err = EMAIL_ERROR_INVALID_PARAM;
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;
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;
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);
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);
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);
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));
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));
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));
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));
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);
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));
294 EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", search_filter);
295 err = EMAIL_ERROR_INVALID_PARAM;
298 EM_SAFE_FREE(time_string); /*prevent 26258*/
303 EM_SAFE_FREE(temp_criteria);
304 EM_SAFE_FREE(time_string);
306 if (err_code != NULL)
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)
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;
318 char *criteria = NULL;
320 if (input_mailbox_id == 0 || input_account_id < 0) {
321 EM_DEBUG_EXCEPTION("Invalid parameter");
322 error = EMAIL_ERROR_INVALID_PARAM;
326 email_event_t event_data;
328 memset(&event_data, 0x00, sizeof(email_event_t));
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");
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;
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;
351 EM_SAFE_FREE(event_data.event_param_data_1);
354 EM_SAFE_FREE(criteria);
356 if (err_code != NULL)
359 EM_DEBUG_FUNC_END("error [%d]", error);
363 INTERNAL_FUNC int emdaemon_clear_all_mail_data(int* err_code)
365 EM_DEBUG_FUNC_BEGIN();
368 int error = EMAIL_ERROR_NONE;
370 if (emdaemon_initialize(&error)) {
371 if (!emstorage_clear_mail_data(true, &error))
372 EM_DEBUG_EXCEPTION("emstorage_clear_mail_data failed [%d]", error);
375 EM_DEBUG_EXCEPTION("emdaemon_initialize failed [%d]", error);
381 emcore_display_unread_in_badge();
385 if (!emstorage_create_table(EMAIL_CREATE_DB_NORMAL, &error))
386 EM_DEBUG_EXCEPTION("emstorage_create_table failed [%d]", error);
388 emdaemon_finalize(&error);
397 /* --------------------------------------------------------------------------------*/