summaryrefslogtreecommitdiff
path: root/email-api/email-api-etc.c
blob: 258dd6a8a3a38ff7117fac62db93f02f5f97d249 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
*  email-service
*
* Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
*
* Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/


/**
 *
 * This file contains the data structures and interfaces needed for application,
 * to interact with email-service.
 * @file		email-api-etc.c
 * @brief		This file contains the data structures and interfaces of functionalities provided by email-service .
 */

#include "email-api.h"
#include "email-types.h"
#include "email-ipc.h"
#include "email-debug-log.h"
#include "email-core-utils.h"
#include "email-core-smtp.h"
#include "email-core-mime.h"
#include "email-convert.h"
#include "email-utilities.h"

EXPORT_API int email_show_user_message(int id, email_action_t action, int error_code)
{
	EM_DEBUG_FUNC_BEGIN("id [%d], action [%d], error_code [%d]", id, action, error_code);
	int err = EMAIL_ERROR_NONE;

	if(id < 0 || action < 0) {
		EM_DEBUG_LOG("EMAIL_ERROR_INVALID_PARAM");
		return EMAIL_ERROR_INVALID_PARAM;
	}

	HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_SHOW_USER_MESSAGE);

	/* id */
	if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&id, sizeof(int))) {
		EM_DEBUG_LOG("emipc_add_parameter failed  ");
		EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
	}

	/* action */
	if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&action, sizeof(int))) {
		EM_DEBUG_LOG("emipc_add_parameter failed  ");
		EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
	}

	/* error_code */
	if(!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&error_code, sizeof(int))) {
		EM_DEBUG_LOG("emipc_add_parameter failed  ");
		EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
	}

	if(!emipc_execute_proxy_api(hAPI))  {
		EM_DEBUG_LOG("ipcProxy_ExecuteAsyncAPI failed");
		EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
	}

	emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);

	emipc_destroy_email_api(hAPI);
	hAPI = NULL;

	EM_DEBUG_FUNC_END("err [%d]", err);
	return err;
}

EXPORT_API int email_parse_mime_file(char *eml_file_path, email_mail_data_t **output_mail_data, email_attachment_data_t **output_attachment_data, int *output_attachment_count)
{
	EM_DEBUG_FUNC_BEGIN("eml_file_path : [%s], output_mail_data : [%p], output_attachment_data : [%p]", eml_file_path, output_mail_data, output_attachment_data);
	int err = EMAIL_ERROR_NONE;

	EM_IF_NULL_RETURN_VALUE(eml_file_path, EMAIL_ERROR_INVALID_PARAM);

	if (!emcore_parse_mime_file_to_mail(eml_file_path, output_mail_data, output_attachment_data, output_attachment_count, &err) || !*output_mail_data)
		EM_DEBUG_EXCEPTION("emcore_parse_mime_file_to_mail failed [%d]", err);

	EM_DEBUG_FUNC_END("err [%d]", err);
	return err;
}

EXPORT_API int email_write_mime_file(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data, int input_attachment_count, char **output_file_path)
{
	EM_DEBUG_FUNC_BEGIN("input_mail_data : [%p], input_attachment_data : [%p], input_attachment_count : [%d]", input_mail_data, input_attachment_data, input_attachment_count);

	int err = EMAIL_ERROR_NONE;
	int size = 0;
	char *p_output_file_path = NULL;
	char *mail_data_stream = NULL;
	char *attachment_data_list_stream = NULL;
	HIPC_API hAPI = NULL;

	EM_IF_NULL_RETURN_VALUE(input_mail_data, EMAIL_ERROR_INVALID_PARAM);

	hAPI = emipc_create_email_api(_EMAIL_API_WRITE_MIME_FILE);
	if (!hAPI) {
		EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
		err = EMAIL_ERROR_NULL_VALUE;
		goto FINISH_OFF;
	}

	mail_data_stream = em_convert_mail_data_to_byte_stream(input_mail_data, &size);
	if (!mail_data_stream) {
		EM_DEBUG_EXCEPTION("em_convert_mail_data_to_byte_stream failed");
		err = EMAIL_ERROR_NULL_VALUE;
		goto FINISH_OFF;
	}

	if (!emipc_add_dynamic_parameter(hAPI, ePARAMETER_IN, mail_data_stream, size)) {
		EM_DEBUG_EXCEPTION("emipc_add_dynamic_parameter failed");
		err = EMAIL_ERROR_OUT_OF_MEMORY;
		goto FINISH_OFF;
	}

	attachment_data_list_stream = em_convert_attachment_data_to_byte_stream(input_attachment_data, input_attachment_count, &size);
	if ((size > 0) && !emipc_add_dynamic_parameter(hAPI, ePARAMETER_IN, attachment_data_list_stream, size)) {
		EM_DEBUG_EXCEPTION("emipc_add_dynamic_parameter failed");
		err = EMAIL_ERROR_OUT_OF_MEMORY;
		goto FINISH_OFF;
	}

	if (*output_file_path && (size = EM_SAFE_STRLEN(*output_file_path)) > 0) {
		EM_DEBUG_LOG("output_file_path : [%s] size : [%d]", *output_file_path, size);
		size = size + 1;
	} else {
		size = 0;
	}

	if (!emipc_add_dynamic_parameter(hAPI, ePARAMETER_IN, *output_file_path, size)) {
		EM_DEBUG_EXCEPTION("emipc_add_dynamic_parameter failed");
		err = EMAIL_ERROR_OUT_OF_MEMORY;
		goto FINISH_OFF;
	}	

	if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
		EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
		err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
		goto FINISH_OFF;
	}

	size = emipc_get_parameter_length(hAPI, ePARAMETER_OUT, 0);
	if (size > 0) {
		p_output_file_path = (char *)em_malloc(size);
		if (p_output_file_path == NULL) {
			err = EMAIL_ERROR_OUT_OF_MEMORY;
			goto FINISH_OFF;
		}

		if ((err = emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, size, p_output_file_path)) != EMAIL_ERROR_NONE) {
			EM_DEBUG_EXCEPTION("emipc_get_parameter failed : [%d]", err);
			goto FINISH_OFF;
		}
	}
	
	*output_file_path = p_output_file_path;	

FINISH_OFF:

	if (hAPI)
		emipc_destroy_email_api(hAPI);

	if (err != EMAIL_ERROR_NONE)
		EM_SAFE_FREE(p_output_file_path);

	EM_DEBUG_FUNC_END("err : [%d]", err);
	return err;
}

EXPORT_API int email_delete_parsed_data(email_mail_data_t *input_mail_data)
{
	EM_DEBUG_FUNC_BEGIN("mail_data : [%p]", input_mail_data);
	int err = EMAIL_ERROR_NONE;

	EM_IF_NULL_RETURN_VALUE(input_mail_data, EMAIL_ERROR_INVALID_PARAM);

	if (!emcore_delete_parsed_data(input_mail_data, &err))
		EM_DEBUG_EXCEPTION("emcore_delete_parsed_data failed [%d]", err);

	EM_DEBUG_FUNC_END("err [%d]", err);
	return err;
}

EXPORT_API int email_get_mime_entity(char *mime_path, char **mime_entity)
{
	EM_DEBUG_FUNC_BEGIN("mime_path : [%s]", mime_path);
	int err = EMAIL_ERROR_NONE;

	EM_IF_NULL_RETURN_VALUE(mime_path, EMAIL_ERROR_INVALID_PARAM);

	if (!emcore_get_mime_entity(mime_path, mime_entity, &err)) 
		EM_DEBUG_EXCEPTION("emcore_get_mime_entity failed [%d]", err);

	EM_DEBUG_FUNC_END();
	return err;
}