summaryrefslogtreecommitdiff
path: root/common/src/email-engine.c
blob: 91d8eb92ba34503b250057750c1021f71e1f0ea4 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
 * Copyright 2012  Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
 *
 * 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.
 */

#include <ctype.h>
#include <string.h>
#include <glib.h>
#include <glib/gprintf.h>
#include <email-api.h>
#include <db-util.h>
#include <vconf.h>

#include "email-common-types.h"
#include "email-debug.h"
#include "email-utils.h"
#include "email-engine.h"

#define ACCOUNT_MIN						-1

static guint _g_edb_ref_count = 0;


gboolean email_engine_initialize(void)
{
	debug_log("");
	++_g_edb_ref_count;

	if (_g_edb_ref_count > 1) {
		debug_log("already opened - EDB ref_count(%d)", _g_edb_ref_count);
		return TRUE;
	}

	int err = 0;

	debug_log("email_service_begin");

	/*err = ipcEmailProxy_Initialize(0);*/
	err = email_service_begin();
	if (err != EMAIL_ERROR_NONE) {
		debug_critical("fail to email_service_begin - err(%d)", err);
		/*
		g_assert(0);
		*/
		return FALSE;
	}

	err = email_open_db();
	if (err != EMAIL_ERROR_NONE) {
		debug_critical("fail to open db - err(%d)", err);
		return FALSE;
	}

	return TRUE;
}

void email_engine_finalize(void)
{
	debug_log("");
	--_g_edb_ref_count;

	if (_g_edb_ref_count > 0) {
		debug_log("remain EDB ref_count(%d)", _g_edb_ref_count);
		return;
	}

	int err = 0;

	err = email_close_db();
	if (err != EMAIL_ERROR_NONE) {
		debug_critical("fail to close db - err(%d)", err);
	}

	debug_log("email_service_end");

	err = email_service_end();
	if (err != EMAIL_ERROR_NONE) {
		debug_critical("fail to email_service_end - err(%d)", err);
	}
}

gboolean email_engine_get_account_full_data(int acctid, email_account_t **account)
{
	debug_log("");
	debug_log("email_engine_get_account_full_data. acctid:%d", acctid);
	RETURN_VAL_IF_FAIL(acctid > ACCOUNT_MIN, FALSE);
	int err = 0;

	err = email_get_account(acctid, EMAIL_ACC_GET_OPT_FULL_DATA, account);
	if (err != EMAIL_ERROR_NONE) {
		debug_critical("email_get_account full data error Err(%d)", err);
		return FALSE;
	}
	if (*account) {
		debug_log("Account name: %s", (*account)->account_name);
		if ((*account)->options.signature)
			debug_log("Signature: %s", (*account)->options.signature);
	} else {
		debug_critical("account is NULL");
		return FALSE;
	}

	return TRUE;
}

gboolean email_engine_set_default_account(gint account_id)
{
	debug_log("");
	RETURN_VAL_IF_FAIL(account_id > ACCOUNT_MIN, FALSE);
	int err = 0;

	err = email_save_default_account_id(account_id);
	debug_log("email_save_default_account_id returns %d.", err);
	if (err != EMAIL_ERROR_NONE) {
		debug_critical("email_save_default_account_id: Err(%d)", err);
		return FALSE;
	} else {
		debug_log("default account is set as account_id %d.", account_id);
		return TRUE;
	}
}

gboolean email_engine_get_default_account(gint *account_id)
{
	debug_log("");
	RETURN_VAL_IF_FAIL(account_id != NULL, FALSE);

	email_account_t *_account = NULL;
	int count = 0;
	int err = 0;

	err = email_load_default_account_id(account_id);
	debug_log("email_load_default_account_id returns %d.", err);

	/* if account_id is default account, then check account_id whether it is valid or not */
	if (err == EMAIL_ERROR_NONE) {
		debug_log("default account id is %d.", *account_id);
		if (email_get_account(*account_id, EMAIL_ACC_GET_OPT_DEFAULT, &_account) == EMAIL_ERROR_NONE) {
			email_free_account(&_account, 1);
			return TRUE;
		}
	}

	/* if slp_ret have no value or account id is not valid */
	err = email_get_account_list(&_account, &count);
	if (err != EMAIL_ERROR_NONE) {
		debug_critical("fail to get account list - err(%d)", err);
		return FALSE;
	}

	/* no account */
	if (_account == NULL || count == 0) {
		debug_log("account info is null");
		return FALSE;
	}

	*account_id = _account[0].account_id;
	debug_log("account id (%d)", *account_id);

	err = email_free_account(&_account, count);
	if (err != EMAIL_ERROR_NONE) {
		debug_critical("fail to free account - err(%d)", err);
		return FALSE;
	}

	RETURN_VAL_IF_FAIL((*account_id) > 0, FALSE);
	email_engine_set_default_account(*account_id);

	return TRUE;
}

void email_engine_stop_working(gint account_id, unsigned handle)
{
	debug_log("");

	RETURN_IF_FAIL(account_id > ACCOUNT_MIN);
	RETURN_IF_FAIL(handle > 0);

	int err = 0;

	debug_log("handle (%d)", handle);

	err = email_cancel_job(account_id, handle, EMAIL_CANCELED_BY_USER);

	if (err != EMAIL_ERROR_NONE) {
		debug_warning("fail to cancel job");
	}
}

gboolean email_engine_delete_mail(gint account_id, int mailbox_id, gint mail_id, int sync)
{
	debug_log("");
	RETURN_VAL_IF_FAIL(account_id > ACCOUNT_MIN, FALSE);
	RETURN_VAL_IF_FAIL(mailbox_id > 0, FALSE);
	RETURN_VAL_IF_FAIL(mail_id > 0, FALSE);

	int err = 0;
	int mail_ids[1] = { 0 };
	gboolean res = TRUE;	/* MUST BE initialized TRUE. */

	mail_ids[0] = mail_id;

	debug_log("account_id : %d", account_id);
	debug_log("mail_ids[0] : %d", mail_ids[0]);
	debug_log("sync : %d", sync);
	err = email_delete_mail(mailbox_id, mail_ids, 1, sync);

	if (err != EMAIL_ERROR_NONE) {
		debug_warning("failed to delete message - err (%d)", err);
		res = FALSE;
	}

	return res;
}

gchar *email_engine_get_attachment_path(gint attach_id)
{
	debug_log("");
	RETURN_VAL_IF_FAIL(attach_id > 0, FALSE);

	int err = 0;
	email_attachment_data_t *attachments = NULL;
	gchar *attachment_path = NULL;

	err = email_get_attachment_data(attach_id, &attachments);

	if (err != EMAIL_ERROR_NONE) {
		debug_critical("fail to get attachment info - err (%d)", err);
		goto error;
	}

	if (attachments == NULL) {
		debug_critical("attachments is @niL");
		goto error;
	}

	if (STR_VALID(attachments->attachment_path)) {
		debug_log("attachment path (%s)", attachments->attachment_path);
		attachment_path = g_strdup(attachments->attachment_path);
	}

	err = email_free_attachment_data(&attachments, 1);

	if (err != EMAIL_ERROR_NONE) {
		debug_warning("fail to free attachment info - err(%d)", err);
	}

 error:
	return attachment_path;
}

/* EOF */