update for beta universally
[framework/messaging/email-service.git] / email-engine / emf-etc.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 /******************************************************************************
25  * File: emf-etc.c
26  * Desc: Mail Framework 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 "emflib.h"
38 #include "emf-account.h"
39 #include "emf-dbglog.h"
40 #include "emf-global.h"
41 #include "em-core-types.h"
42 #include "em-core-event.h"
43 #include "em-core-utils.h"
44 #include "em-storage.h"
45
46
47 int emf_register_event_callback(emf_action_t action, emf_event_callback callback, void* event_data)
48 {
49         return em_core_register_event_callback(action, callback, event_data);
50 }
51
52 int emf_unregister_event_callback(emf_action_t action, emf_event_callback callback)
53 {
54         return em_core_unregister_event_callback(action, callback);
55 }
56
57
58 EXPORT_API void emf_get_event_queue_status(int* on_sending, int* on_receiving)
59 {
60         em_core_get_event_queue_status(on_sending, on_receiving);
61 }
62
63 EXPORT_API int emf_get_pending_job(emf_action_t action, int account_id, int mail_id, emf_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 em_core_get_pending_event(action, account_id, mail_id, status);
68 }
69
70 EXPORT_API int emf_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 = EMF_ERROR_NONE;
76         
77         
78         if (!em_core_cancel_thread(handle, NULL, &err))  {
79                 EM_DEBUG_EXCEPTION("em_core_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 EXPORT_API int emf_mail_send_mail_cancel_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 = EMF_ERROR_NONE;       
99         int handle = 0;
100         emf_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 = EMF_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_send_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         emf_mail_t* mail = NULL;
116
117         if (!em_core_mail_get_mail(mail_id, &mail, &err))  {
118                 EM_DEBUG_EXCEPTION("em_core_mail_get_mail failed [%d]", err);
119                 goto FINISH_OFF;
120         }
121
122         if (mail && mail->info) {
123                 if (mail->info->extra_flags.status == EMF_MAIL_STATUS_SEND_CANCELED) {
124                         EM_DEBUG_EXCEPTION(">>>> EMF_MAIL_STATUS_SEND_CANCELED Already set for Mail ID [ %d ]", mail_id);
125                         goto FINISH_OFF;
126                 }
127                 else {                  
128                         mail->info->extra_flags.status = EMF_MAIL_STATUS_SEND_CANCELED;
129
130                         if(!em_core_mail_modify_extra_flag(mail_id, mail->info->extra_flags, &err)) {
131                                 EM_DEBUG_EXCEPTION("Failed to modify extra flag [%d]",err);
132                                 goto FINISH_OFF;
133                         }
134                 }
135         }
136
137 #endif
138
139         if(!em_core_get_handle_by_mailId_from_transaction_info(mail_id , &handle )) {
140                 EM_DEBUG_EXCEPTION("em_core_get_handle_by_mailId_from_transaction_info failed for mail_id[%d]", mail_id);
141                 err = EMF_ERROR_HANDLE_NOT_FOUND;
142                 goto FINISH_OFF;
143         }
144
145         if (!(ref_account = emf_get_account_reference(account_id)))  {
146                 EM_DEBUG_EXCEPTION("emf_get_account_reference failed [%d]", account_id);
147                 err = EMF_ERROR_INVALID_ACCOUNT;
148                 goto FINISH_OFF;
149         }
150
151         switch (ref_account->account_bind_type)  {
152                 case EMF_BIND_TYPE_EM_CORE :
153                         if (!em_core_cancel_send_mail_thread(handle, NULL, &err))  {
154                                 EM_DEBUG_EXCEPTION("em_core_cancel_send_mail_thread failed [%d]", err);
155                                 goto FINISH_OFF;
156                         }
157                         break;
158
159                 default:
160                         EM_DEBUG_EXCEPTION("unknown account bind type...");
161                         err = EMF_ERROR_INVALID_ACCOUNT;
162                         goto FINISH_OFF;
163         }       
164         
165         if(!em_core_delete_transaction_info_by_mailId(mail_id))
166                 EM_DEBUG_EXCEPTION("em_core_delete_transaction_info_by_mailId failed for mail_id[%d]", mail_id);
167         
168         ret = true;
169         
170 FINISH_OFF:
171         if(err_code != NULL)
172                 *err_code = err;
173         
174 #ifdef __FEATURE_PROGRESS_IN_OUTBOX__
175         if(!em_core_mail_free(&mail, 1, &err))
176                 EM_DEBUG_EXCEPTION("em_core_mail_free Failed [%d ]", err);      
177
178 #endif
179         EM_DEBUG_FUNC_END();
180         return ret;
181 }       
182
183
184 EXPORT_API int
185 emf_clear_mail_data(int* err_code)
186 {
187         EM_DEBUG_FUNC_BEGIN();
188         
189         int ret = false;
190         int error = EMF_ERROR_NONE;
191         
192         if (emf_init(&error)) {
193                 if (!em_storage_clear_mail_data(true, &error))
194                         EM_DEBUG_EXCEPTION("em_storage_clear_mail_data failed [%d]", error);
195         }
196         else {
197                 EM_DEBUG_EXCEPTION("emf_init failed [%d]", error);
198                 if (err_code)
199                         *err_code = error;
200                 return false;
201         }
202
203         em_core_check_unread_mail();
204
205         ret = true;
206
207         if (!em_storage_create_table(EMF_CREATE_DB_NORMAL, &error)) 
208                 EM_DEBUG_EXCEPTION("em_storage_create_table failed [%d]", error);
209         
210         emf_close(&error);
211         
212         if (err_code)
213                 *err_code = error;
214         EM_DEBUG_FUNC_END();
215     return ret;
216 }
217
218         
219 /* --------------------------------------------------------------------------------*/