4 * Copyright (c) 2000 - 2011 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-event.h"
42 #include "email-core-utils.h"
43 #include "email-utilities.h"
44 #include "email-storage.h"
47 int emdaemon_register_event_callback(email_action_t action, email_event_callback callback, void* event_data)
49 return emcore_register_event_callback(action, callback, event_data);
52 int emdaemon_unregister_event_callback(email_action_t action, email_event_callback callback)
54 return emcore_unregister_event_callback(action, callback);
57 INTERNAL_FUNC void emdaemon_get_event_queue_status(int* on_sending, int* on_receiving)
59 emcore_get_event_queue_status(on_sending, on_receiving);
62 INTERNAL_FUNC int emdaemon_get_pending_job(email_action_t action, int account_id, int mail_id, email_event_status_type_t* status)
64 EM_DEBUG_FUNC_BEGIN("action[%d], account_id[%d], mail_id[%d]", action, account_id, mail_id);
66 return emcore_get_pending_event(action, account_id, mail_id, status);
69 INTERNAL_FUNC int emdaemon_cancel_job(int account_id, int handle, int* err_code)
71 EM_DEBUG_FUNC_BEGIN("account_id[%d], handle[%d], err_code[%p]", account_id, handle, err_code);
74 int err = EMAIL_ERROR_NONE;
77 if (!emcore_cancel_thread(handle, NULL, &err)) {
78 EM_DEBUG_EXCEPTION("emcore_cancel_thread failed [%d]", err);
92 INTERNAL_FUNC int emdaemon_cancel_sending_mail_job(int account_id, int mail_id, int* err_code)
94 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], err_code[%p]", account_id, mail_id, err_code);
97 int err = EMAIL_ERROR_NONE;
99 email_account_t* ref_account = NULL;
101 if (account_id <= 0) {
102 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d]", account_id, mail_id);
103 err = EMAIL_ERROR_INVALID_PARAM;
107 #ifdef __FEATURE_PROGRESS_IN_OUTBOX__
109 /* h.gahlaut@samsung.com: Moved this code from email_cancel_sending_mail API to email-service engine
110 since this code has update DB operation which is failing in context of email application process
111 with an sqlite error -> sqlite3_step fail:8 */
113 /* which means #define SQLITE_READONLY 8 */ /* Attempt to write a readonly database */
114 emstorage_mail_tbl_t *mail_tbl_data = NULL;
116 if (!emstorage_get_mail_by_id(mail_id, &mail_tbl_data, false, &err)) {
117 EM_DEBUG_EXCEPTION("emcore_get_mail failed [%d]", err);
122 if (mail_tbl_data->save_status == EMAIL_MAIL_STATUS_SEND_CANCELED) {
123 EM_DEBUG_EXCEPTION(">>>> EMAIL_MAIL_STATUS_SEND_CANCELED Already set for Mail ID [ %d ]", mail_id);
127 mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SEND_CANCELED;
129 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)) {
130 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]",err);
138 if(!emcore_get_handle_by_mailId_from_transaction_info(mail_id , &handle )) {
139 EM_DEBUG_EXCEPTION("emcore_get_handle_by_mailId_from_transaction_info failed for mail_id[%d]", mail_id);
140 err = EMAIL_ERROR_HANDLE_NOT_FOUND;
144 if (!(ref_account = emdaemon_get_account_reference(account_id))) {
145 EM_DEBUG_EXCEPTION("emdaemon_get_account_reference failed [%d]", account_id);
146 err = EMAIL_ERROR_INVALID_ACCOUNT;
150 if (!emcore_cancel_send_mail_thread(handle, NULL, &err)) {
151 EM_DEBUG_EXCEPTION("emcore_cancel_send_mail_thread failed [%d]", err);
155 if(!emcore_delete_transaction_info_by_mailId(mail_id))
156 EM_DEBUG_EXCEPTION("emcore_delete_transaction_info_by_mailId failed for mail_id[%d]", mail_id);
164 #ifdef __FEATURE_PROGRESS_IN_OUTBOX__
165 if(!emstorage_free_mail(&mail_tbl_data, 1, &err))
166 EM_DEBUG_EXCEPTION("emcore_free_mail Failed [%d ]", err);
173 static char *_make_criteria_to_search_filter(email_search_filter_t *search_filter, int search_filter_count, int *err_code)
175 EM_DEBUG_FUNC_BEGIN("search_filter : [%p], search_filter_count : [%d]", search_filter, search_filter_count);
178 int err = EMAIL_ERROR_NONE;
179 char *criteria = NULL;
180 char *temp_criteria = NULL;
182 if (search_filter == NULL || search_filter_count < 0) {
183 EM_DEBUG_EXCEPTION("Invalid paramter");
184 err = EMAIL_ERROR_INVALID_PARAM;
188 criteria = (char *)em_malloc(STRING_LENGTH_FOR_DISPLAY * search_filter_count);
189 if (criteria == NULL) {
190 EM_DEBUG_EXCEPTION("em_malloc failed");
191 err = EMAIL_ERROR_OUT_OF_MEMORY;
195 temp_criteria = (char *)em_malloc(STRING_LENGTH_FOR_DISPLAY);
196 if (temp_criteria == NULL) {
197 EM_DEBUG_EXCEPTION("em_malloc failed");
198 err = EMAIL_ERROR_OUT_OF_MEMORY;
202 for (i = 0; i < search_filter_count; i++) {
203 EM_DEBUG_LOG("search_filter_type [%d]", search_filter[i].search_filter_type);
204 memset(temp_criteria, 0x00, STRING_LENGTH_FOR_DISPLAY);
206 switch (search_filter[i].search_filter_type) {
207 case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_NO :
208 case EMAIL_SEARCH_FILTER_TYPE_UID :
209 case EMAIL_SEARCH_FILTER_TYPE_SIZE_LARSER :
210 case EMAIL_SEARCH_FILTER_TYPE_SIZE_SMALLER :
211 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_ANSWERED :
212 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DELETED :
213 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DRAFT :
214 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_FLAGED :
215 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_RECENT :
216 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_SEEN :
217 EM_DEBUG_LOG("integer_type_key_value [%d]", search_filter[i].search_filter_key_value.integer_type_key_value);
220 case EMAIL_SEARCH_FILTER_TYPE_BCC :
221 case EMAIL_SEARCH_FILTER_TYPE_CC :
222 case EMAIL_SEARCH_FILTER_TYPE_TO :
223 case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID :
224 EM_DEBUG_LOG("string_type_key_value [%s]", search_filter[i].search_filter_key_value.string_type_key_value);
227 case EMAIL_SEARCH_FILTER_TYPE_FROM :
228 EM_DEBUG_LOG("string_type_key_value [%s]", search_filter[i].search_filter_key_value.string_type_key_value);
229 SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "from %s ", search_filter[i].search_filter_key_value.string_type_key_value);
230 strncat(criteria, temp_criteria, strlen(temp_criteria));
233 case EMAIL_SEARCH_FILTER_TYPE_SUBJECT :
234 EM_DEBUG_LOG("string_type_key_value [%s]", search_filter[i].search_filter_key_value.string_type_key_value);
235 SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "subject %s ", search_filter[i].search_filter_key_value.string_type_key_value);
236 strncat(criteria, temp_criteria, strlen(temp_criteria));
239 case EMAIL_SEARCH_FILTER_TYPE_KEYWORD :
240 EM_DEBUG_LOG("string_type_key_value [%s]", search_filter[i].search_filter_key_value.string_type_key_value);
241 SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "keyword %s ", search_filter[i].search_filter_key_value.string_type_key_value);
242 strncat(criteria, temp_criteria, strlen(temp_criteria));
245 case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_BEFORE :
246 case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_ON :
247 case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_SINCE :
248 EM_DEBUG_LOG("time_type_key_value [%d]", search_filter[i].search_filter_key_value.time_type_key_value);
252 EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", search_filter);
253 err = EMAIL_ERROR_INVALID_PARAM;
262 EM_SAFE_FREE(temp_criteria);
264 if (err_code != NULL)
271 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)
273 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);
274 int error = EMAIL_ERROR_NONE;
276 char *criteria = NULL;
278 if (input_mailbox_id == 0 || input_account_id < 0) {
279 EM_DEBUG_EXCEPTION("Invalid parameter");
280 error = EMAIL_ERROR_INVALID_PARAM;
284 email_event_t event_data;
286 memset(&event_data, 0x00, sizeof(email_event_t));
288 criteria = _make_criteria_to_search_filter(input_search_filter, input_search_filter_count, &error);
289 if (criteria == NULL) {
290 EM_DEBUG_EXCEPTION("_make_criteria_to_search_filter failed");
294 event_data.type = EMAIL_EVENT_SEARCH_ON_SERVER;
295 event_data.account_id = input_account_id;
296 event_data.event_param_data_1 = EM_SAFE_STRDUP(criteria);
297 event_data.event_param_data_4 = input_mailbox_id;
299 if (!emcore_insert_event(&event_data, (int *)output_handle, &error)) {
300 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", error);
301 error = EMAIL_ERROR_NONE;
309 EM_SAFE_FREE(event_data.event_param_data_1);
310 EM_SAFE_FREE(event_data.event_param_data_2);
313 EM_SAFE_FREE(criteria);
315 if (err_code != NULL)
318 EM_DEBUG_FUNC_END("error [%d]", error);
322 INTERNAL_FUNC int emdaemon_clear_all_mail_data(int* err_code)
324 EM_DEBUG_FUNC_BEGIN();
327 int error = EMAIL_ERROR_NONE;
329 if (emdaemon_initialize(&error)) {
330 if (!emstorage_clear_mail_data(true, &error))
331 EM_DEBUG_EXCEPTION("emstorage_clear_mail_data failed [%d]", error);
334 EM_DEBUG_EXCEPTION("emdaemon_initialize failed [%d]", error);
340 emcore_check_unread_mail();
344 if (!emstorage_create_table(EMAIL_CREATE_DB_NORMAL, &error))
345 EM_DEBUG_EXCEPTION("emstorage_create_table failed [%d]", error);
347 emdaemon_finalize(&error);
356 /* --------------------------------------------------------------------------------*/