Fixed the invalid casting and remove the flash noti
[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 <vconf-keys.h>
38 #include <glib.h>
39 #include <gio/gio.h>
40 #include <glib-object.h>
41 #include <sys/smack.h>
42 #include <notification.h>
43 #include <notification_type.h>
44
45 #include "email-daemon.h"
46 #include "email-daemon-account.h"
47 #include "email-debug-log.h"
48 #include "email-internal-types.h"
49 #include "email-core-account.h"
50 #include "email-core-event.h"
51 #include "email-core-utils.h"
52 #include "email-core-alarm.h"
53 #include "email-core-smtp.h"
54 #include "email-utilities.h"
55 #include "email-storage.h"
56 #include "email-ipc.h"
57 #include "email-dbus-activation.h"
58
59 extern pthread_mutex_t sound_mutex;
60 extern pthread_cond_t sound_condition;
61
62 int emdaemon_register_event_callback(email_action_t action, email_event_callback callback, void* event_data)
63 {
64         return emcore_register_event_callback(action, callback, event_data);
65 }
66
67 int emdaemon_unregister_event_callback(email_action_t action, email_event_callback callback)
68 {
69         return emcore_unregister_event_callback(action, callback);
70 }
71
72 INTERNAL_FUNC int emdaemon_cancel_job(int account_id, int handle, int* err_code)
73 {
74         EM_DEBUG_FUNC_BEGIN("account_id[%d], handle[%d], err_code[%p]", account_id, handle, err_code);
75         
76         int ret = false;
77         int err = EMAIL_ERROR_NONE;
78         
79         
80         if (!emcore_cancel_thread(handle, NULL, &err))  {
81                 EM_DEBUG_EXCEPTION("emcore_cancel_thread failed [%d]", err);
82                 goto FINISH_OFF;
83         }
84         
85         ret = true;
86         
87 FINISH_OFF:
88         if (err_code != NULL)
89                 *err_code = err;
90         EM_DEBUG_FUNC_END();
91         return ret;
92 }
93
94
95 INTERNAL_FUNC int emdaemon_cancel_sending_mail_job(int account_id, int mail_id, int* err_code)
96 {
97         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], err_code[%p]", account_id, mail_id, err_code);
98         
99         int ret = false;
100         int err = EMAIL_ERROR_NONE;     
101         int handle = 0;
102         
103         if (account_id <= 0)  {
104                 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d]", account_id, mail_id);
105                 err = EMAIL_ERROR_INVALID_PARAM;
106                 goto FINISH_OFF;
107         }
108
109 #ifdef __FEATURE_PROGRESS_IN_OUTBOX__
110
111         /* Removed below code, as it is causing struck in composer */
112 #if 0
113         /*      h.gahlaut@samsung.com: Moved this code from email_cancel_sending_mail API to email-service engine
114                 since this code has update DB operation which is failing in context of email application process 
115                 with an sqlite error -> sqlite3_step fail:8 */
116                 
117         /*      which means #define SQLITE_READONLY   8 */  /* Attempt to write a readonly database */ 
118         emstorage_mail_tbl_t *mail_tbl_data = NULL;
119
120         if (!emstorage_get_mail_by_id(mail_id, &mail_tbl_data, false, &err))  {
121                 EM_DEBUG_EXCEPTION("emcore_get_mail failed [%d]", err);
122                 goto FINISH_OFF;
123         }
124
125         if (mail_tbl_data) {
126                 if (mail_tbl_data->save_status == EMAIL_MAIL_STATUS_SEND_CANCELED) {
127                         EM_DEBUG_EXCEPTION(">>>> EMAIL_MAIL_STATUS_SEND_CANCELED Already set for Mail ID [ %d ]", mail_id);
128                         goto FINISH_OFF;
129                 }
130                 else {                  
131                         mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SEND_CANCELED;
132
133                         if(!emstorage_set_field_of_mails_with_integer_value(multi_user_name, mail_tbl_data->account_id, &mail_id, 1, "save_status", EMAIL_MAIL_STATUS_SEND_CANCELED, true, &err)) {
134                                 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]",err);
135                                 goto FINISH_OFF;
136                         }
137                 }
138
139
140         }
141 #endif
142
143         if ((err = emcore_delete_alram_data_by_reference_id(EMAIL_ALARM_CLASS_SCHEDULED_SENDING, mail_id)) != EMAIL_ERROR_NONE) {
144                 EM_DEBUG_LOG("emcore_delete_alram_data_by_reference_id failed [%d]",err);
145         }
146
147 #endif
148
149         if(!emcore_get_handle_by_mailId_from_transaction_info(mail_id , &handle)) {
150                 EM_DEBUG_EXCEPTION("emcore_get_handle_by_mailId_from_transaction_info failed for mail_id[%d]", mail_id);
151                 ret = true;
152                 goto FINISH_OFF;
153         }
154
155         if (!emcore_cancel_send_mail_thread(handle, NULL, &err)) {
156                 EM_DEBUG_EXCEPTION("emcore_cancel_send_mail_thread failed [%d]", err);
157         }
158         
159         if(!emcore_delete_transaction_info_by_mailId(mail_id))
160                 EM_DEBUG_EXCEPTION("emcore_delete_transaction_info_by_mailId failed for mail_id[%d]", mail_id);
161         
162         ret = true;
163         
164 FINISH_OFF:
165         if(err_code != NULL)
166                 *err_code = err;
167 #if 0
168 #ifdef __FEATURE_PROGRESS_IN_OUTBOX__
169         if(!emstorage_free_mail(&mail_tbl_data, 1, &err))
170                 EM_DEBUG_EXCEPTION("emcore_free_mail Failed [%d ]", err);       
171 #endif
172 #endif
173         EM_DEBUG_FUNC_END();
174         return ret;
175 }       
176
177 INTERNAL_FUNC int emdaemon_search_mail_on_server(char *multi_user_name, 
178                                                                                                 int input_account_id, 
179                                                                                                 int input_mailbox_id, 
180                                                                                                 email_search_filter_t *input_search_filter, 
181                                                                                                 int input_search_filter_count, 
182                                                                                                 unsigned int *output_handle, 
183                                                                                                 int *err_code)
184 {
185         EM_DEBUG_FUNC_BEGIN("input_account_id [%d], mailbox_id [%d], input_search_filter [%p], " 
186                                                 "input_search_filter_count [%d], output_handle [%p]", 
187                                                 input_account_id, input_mailbox_id, input_search_filter, 
188                                                 input_search_filter_count, output_handle);
189         int error = EMAIL_ERROR_NONE;
190         int ret = false;
191         email_event_t *event_data = NULL;
192         
193         if (input_mailbox_id == 0 || input_account_id < 0) {
194                 EM_DEBUG_EXCEPTION("Invalid parameter");
195                 error = EMAIL_ERROR_INVALID_PARAM;
196                 return false;
197         }
198
199         event_data = em_malloc(sizeof(email_event_t));
200         if (event_data == NULL) {
201                 EM_DEBUG_EXCEPTION("Out of memory");
202                 error = EMAIL_ERROR_OUT_OF_MEMORY;
203                 goto FINISH_OFF;
204         }
205
206         event_data->type = EMAIL_EVENT_SEARCH_ON_SERVER;
207         event_data->account_id = input_account_id;
208         event_data->event_param_data_1 = (void *)input_search_filter;
209         event_data->event_param_data_5 = input_search_filter_count;
210         event_data->event_param_data_4 = input_mailbox_id;
211         event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
212
213         if (!emcore_insert_event(event_data, (int *)output_handle, &error)) {
214                 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", error);
215                 error = EMAIL_ERROR_NONE;
216                 goto FINISH_OFF;
217         }
218
219         ret = true;
220
221 FINISH_OFF:
222
223         if (ret == false && event_data) {
224                 emcore_free_event(event_data);
225                 free(event_data);
226         }
227
228         if (err_code != NULL)
229                 *err_code = error;
230
231         EM_DEBUG_FUNC_END("error [%d]", error);
232         return ret;
233 }
234
235 #if 0
236 INTERNAL_FUNC int emdaemon_reschedule_sending_mail()
237 {
238         EM_DEBUG_FUNC_BEGIN();
239         int err = EMAIL_ERROR_NONE;
240         char *conditional_clause_string = NULL;
241         email_list_filter_t filter_list[7];
242         email_mail_list_item_t *result_mail_list = NULL;
243         int filter_rule_count = 7;
244         int result_mail_count = 0;
245         int i = 0;
246
247         memset(filter_list, 0 , sizeof(email_list_filter_t) * filter_rule_count);
248
249         filter_list[0].list_filter_item_type                               = EMAIL_LIST_FILTER_ITEM_RULE;
250         filter_list[0].list_filter_item.rule.target_attribute              = EMAIL_MAIL_ATTRIBUTE_SCHEDULED_SENDING_TIME;
251         filter_list[0].list_filter_item.rule.rule_type                     = EMAIL_LIST_FILTER_RULE_NOT_EQUAL;
252         filter_list[0].list_filter_item.rule.key_value.integer_type_value  = 0;
253
254         filter_list[1].list_filter_item_type                               = EMAIL_LIST_FILTER_ITEM_OPERATOR;
255         filter_list[1].list_filter_item.operator_type                      = EMAIL_LIST_FILTER_OPERATOR_AND;
256
257         filter_list[2].list_filter_item_type                               = EMAIL_LIST_FILTER_ITEM_OPERATOR;
258         filter_list[2].list_filter_item.operator_type                      = EMAIL_LIST_FILTER_OPERATOR_LEFT_PARENTHESIS;
259
260         filter_list[3].list_filter_item_type                               = EMAIL_LIST_FILTER_ITEM_RULE;
261         filter_list[3].list_filter_item.rule.target_attribute              = EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS;
262         filter_list[3].list_filter_item.rule.rule_type                     = EMAIL_LIST_FILTER_RULE_EQUAL;
263         filter_list[3].list_filter_item.rule.key_value.integer_type_value  = EMAIL_MAIL_STATUS_SEND_SCHEDULED;
264
265         filter_list[4].list_filter_item_type                               = EMAIL_LIST_FILTER_ITEM_OPERATOR;
266         filter_list[4].list_filter_item.operator_type                      = EMAIL_LIST_FILTER_OPERATOR_OR;
267
268         filter_list[5].list_filter_item_type                               = EMAIL_LIST_FILTER_ITEM_RULE;
269         filter_list[5].list_filter_item.rule.target_attribute              = EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS;
270         filter_list[5].list_filter_item.rule.rule_type                     = EMAIL_LIST_FILTER_RULE_EQUAL;
271         filter_list[5].list_filter_item.rule.key_value.integer_type_value  = EMAIL_MAIL_STATUS_SEND_DELAYED;
272
273         filter_list[6].list_filter_item_type                               = EMAIL_LIST_FILTER_ITEM_OPERATOR;
274         filter_list[6].list_filter_item.operator_type                      = EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS;
275
276         /* Get scheduled mail list */
277         if( (err = emstorage_write_conditional_clause_for_getting_mail_list(multi_user_name, filter_list, filter_rule_count, NULL, 0, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
278                 EM_DEBUG_EXCEPTION("emstorage_write_conditional_clause_for_getting_mail_list failed[%d]", err);
279                 goto FINISH_OFF;
280         }
281
282         EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
283
284         if(!emstorage_query_mail_list(NULL, conditional_clause_string, true, &result_mail_list, &result_mail_count, &err) && !result_mail_list) {
285                 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", err);
286                 goto FINISH_OFF;
287         }
288
289         /* Add alarm for scheduled mail */
290         for(i = 0; i < result_mail_count; i++) {
291                 if((err = emcore_schedule_sending_mail(multi_user_name, result_mail_list[i].mail_id, result_mail_list[i].scheduled_sending_time)) != EMAIL_ERROR_NONE) {
292                         EM_DEBUG_EXCEPTION("emcore_schedule_sending_mail failed [%d]", err);
293                         goto FINISH_OFF;
294                 }
295         }
296
297 FINISH_OFF:
298         EM_SAFE_FREE (conditional_clause_string); /* detected by valgrind */
299         EM_SAFE_FREE(result_mail_list);
300
301         EM_DEBUG_FUNC_END("err [%d]", err);
302         return err;
303 }
304 #endif
305
306 INTERNAL_FUNC int emdaemon_clear_all_mail_data(char *multi_user_name, int* err_code)
307 {
308         EM_DEBUG_FUNC_BEGIN();
309         
310         int ret = false;
311         int error = EMAIL_ERROR_NONE;
312         
313         if (emdaemon_initialize(multi_user_name, &error)) {
314                 if (!emstorage_clear_mail_data(multi_user_name, true, &error))
315                         EM_DEBUG_EXCEPTION("emstorage_clear_mail_data failed [%d]", error);
316         }
317         else {
318                 EM_DEBUG_EXCEPTION("emdaemon_initialize failed [%d]", error);
319                 if (err_code)
320                         *err_code = error;
321                 return false;
322         }
323
324         emcore_display_unread_in_badge(multi_user_name);
325
326         ret = true;
327
328         if (!emstorage_create_table(multi_user_name, EMAIL_CREATE_DB_NORMAL, &error)) 
329                 EM_DEBUG_EXCEPTION("emstorage_create_table failed [%d]", error);
330         
331         emdaemon_finalize(&error);
332         
333         if (err_code)
334                 *err_code = error;
335         EM_DEBUG_FUNC_END();
336     return ret;
337 }
338
339
340 INTERNAL_FUNC int emdaemon_check_smack_rule(int app_sockfd, char *file_path)
341 {
342         EM_DEBUG_FUNC_BEGIN_SEC("app_sockfd[%d], file_path[%s]", app_sockfd, file_path);
343
344         if (app_sockfd < 0 || !file_path) {
345                 EM_DEBUG_LOG("Invalid parameter");
346                 return false;
347         }
348
349         char *app_label = NULL;
350         char *file_label = NULL;
351         char *real_file_path = NULL;
352         char errno_buf[ERRNO_BUF_SIZE] = {0};
353         int ret = 0;
354         int result = false;
355
356         static int have_smack = -1;
357
358         if (-1 == have_smack) {
359                 if (NULL == smack_smackfs_path()) {
360                         have_smack = 0;
361                 } else {
362                         have_smack = 1;
363                 }
364         }
365
366         if(!have_smack) {
367                 EM_DEBUG_LOG("smack is disabled");
368                 result = true;
369                 goto FINISH_OFF;
370         }
371
372         /* Smack is enabled */
373
374         ret = smack_new_label_from_socket(app_sockfd, &app_label);
375         if (ret < 0) {
376                 EM_DEBUG_LOG("smack_new_label_from_socket failed");
377                 result = false;
378                 goto FINISH_OFF;
379         }
380         
381         real_file_path = realpath(file_path, NULL);
382         if (!real_file_path) {
383                 EM_DEBUG_LOG("realpath failed [%d][%s]", errno, EM_STRERROR(errno_buf));
384                 result = false;
385                 goto FINISH_OFF;
386         }
387
388         ret = smack_getlabel(real_file_path, &file_label, SMACK_LABEL_ACCESS);
389         if (ret < 0) {
390                 EM_DEBUG_LOG("smack_getlabel failed");
391                 result = false;
392                 goto FINISH_OFF;
393         }
394
395         EM_DEBUG_LOG("APP_LABEL[%s], FILE_LABEL[%s]", app_label, file_label);
396
397         ret = smack_have_access(app_label, file_label, "r");
398         if (ret == 0) {
399                 /* Access Denied */
400                 result = false;
401                 goto FINISH_OFF;
402         }
403
404         /* Access granted */
405         result = true;
406
407 FINISH_OFF:
408
409         EM_SAFE_FREE(app_label);
410         EM_SAFE_FREE(file_label);
411         EM_SAFE_FREE(real_file_path);
412
413         EM_DEBUG_FUNC_END();
414         return result;
415 }
416
417 INTERNAL_FUNC int emdaemon_set_smack_label(char *file_path, char *label)
418 {
419         EM_DEBUG_FUNC_BEGIN();
420         int ret = 0;
421         int result = true;
422
423         if ((ret = smack_setlabel(file_path, label, SMACK_LABEL_ACCESS)) < 0) {
424                 EM_DEBUG_LOG("smack_setlabel failed");
425                 result = false;
426         }
427
428         EM_DEBUG_FUNC_END();
429         return result;
430 }
431
432 INTERNAL_FUNC int emdaemon_finalize_sync(char *multi_user_name, int account_id, int total_mail_count, int unread_mail_count, int vip_total_mail_count, int vip_unread_mail_count, int input_from_eas, int *error)
433 {
434         EM_DEBUG_FUNC_BEGIN("account_id [%d], total_mail_count [%d], unread_mail_count [%d], error [%p]", account_id, total_mail_count, unread_mail_count, error);
435         int err = EMAIL_ERROR_NONE, ret = true, result_sync_status = SYNC_STATUS_FINISHED;
436         int topmost = false;
437
438         if ((err = emcore_update_sync_status_of_account(multi_user_name, account_id, SET_TYPE_MINUS, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
439                 EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
440
441         if (!emstorage_get_sync_status_of_account(multi_user_name, account_id, &result_sync_status, &err))
442                 EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", err);
443
444         /* Check the topmost of email app */
445         if (input_from_eas) {
446                 if (vconf_get_int(VCONF_KEY_TOPMOST_WINDOW, &topmost) != 0) {
447                         EM_DEBUG_EXCEPTION("vconf_get_int failed");
448                 }
449         }
450 /*
451         if (result_sync_status == SYNC_STATUS_SYNCING) {
452                 if (topmost) {
453                         EM_DEBUG_LOG("The email app is topmost");
454                         if (!emstorage_update_save_status(multi_user_name, account_id, &err)) {
455                                 EM_DEBUG_EXCEPTION("emstorage_update_save_status failed : [%d]", err);
456                         }
457                 } else {
458                         if ((err = emcore_add_notification(multi_user_name, account_id, 0, unread_mail_count, vip_unread_mail_count, 0, EMAIL_ERROR_NONE, NOTIFICATION_DISPLAY_APP_ALL^NOTIFICATION_DISPLAY_APP_TICKER)) != EMAIL_ERROR_NONE)
459                                 EM_DEBUG_EXCEPTION("emcore_add_notification failed : [%d]", err);
460                 }
461         } else*/ if (result_sync_status == SYNC_STATUS_HAVE_NEW_MAILS) {
462                 if (topmost) {
463                         EM_DEBUG_LOG("The email app is topmost");
464                 } else {
465
466
467                         if ((err = emcore_add_notification(multi_user_name, account_id, 0, unread_mail_count, vip_unread_mail_count, 1, EMAIL_ERROR_NONE, NOTIFICATION_DISPLAY_APP_ALL)) != EMAIL_ERROR_NONE)
468                                 EM_DEBUG_EXCEPTION("emcore_add_notification failed : [%d]", err);
469
470 #ifdef __FEATURE_BLOCKING_MODE__
471                         emcore_set_blocking_mode_status(false);
472 #endif /* __FEATURE_BLOCKING_MODE__ */
473
474                 
475                         if ((err = emcore_update_sync_status_of_account(multi_user_name, account_id, SET_TYPE_MINUS, SYNC_STATUS_HAVE_NEW_MAILS)) != EMAIL_ERROR_NONE)
476                                 EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
477                 }
478
479         } else {
480                 EM_DEBUG_LOG("sync status : [%d]", result_sync_status);
481         }
482
483         EM_DEBUG_FUNC_END();
484         return ret;
485 }
486 /* --------------------------------------------------------------------------------*/