2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @author Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
23 #include <dpl/assert.h>
24 #include <dpl/scoped_free.h>
26 #include <email-api.h>
28 #include <Commons/Exception.h>
29 #include "EmailUtils.h"
30 #include "EmailService.h"
31 #include "ScopedMail.h"
32 #include "ScopedMailbox.h"
34 #define LOG_ENTER LogDebug("---> ENTER");
35 #define LOG_EXIT LogDebug("---> EXIT");
40 namespace EmailService {
42 email_mail_data_t* createMailData(const Api::Messaging::EmailAccountInfo& account)
45 ScopedMail result(alloc<email_mail_data_t>());
47 result->account_id = account.getIntId();
48 result->flags_draft_field = 1;
49 result->priority = EMAIL_MAIL_PRIORITY_NORMAL;
51 std::string from = EmailUtils::formatAddress(account.getAddress(), account.getName());
52 result->full_address_from = strdup(from.c_str());
54 std::string bodyFile = tmpnam(NULL);
55 FILE* f = fopen(bodyFile.c_str(), "w");
58 result->file_path_plain = strdup(bodyFile.c_str());
60 // fix email empty plain text
62 std::string htmlFile = tmpnam(NULL);
63 f = fopen(htmlFile.c_str(), "w");
65 result->file_path_html = strdup(htmlFile.c_str());
67 result->file_path_html = NULL;
70 return result.Release();
73 email_mail_data_t* readMail(int mailId)
76 email_mail_data_t* result = NULL;
78 int error = email_get_mail_data(mailId, &result);
79 if (EMAIL_ERROR_NONE != error) {
80 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
81 "Couldn't find message " << mailId << ". [" << error << "]");
88 void deleteMail(int accountId,
93 email_mail_data_t *mail = NULL;
95 error = email_get_mail_data(mailId, &mail);
96 if (EMAIL_ERROR_NONE != error) {
97 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
98 "Error while deleting mail. [" << error << "]");
101 error = email_delete_mail(mail->mailbox_id, &mailId, 1, 0);
102 if (EMAIL_ERROR_NONE != error) {
103 email_free_mail_data(&mail,1);
104 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
105 "Error while deleting mail. [" << error << "]");
107 email_free_mail_data(&mail,1);
111 email_mailbox_t* getMailboxByType(int accountId,
112 email_mailbox_type_e type)
115 email_mailbox_t* result = NULL;
117 int error = email_get_mailbox_by_mailbox_type(accountId, type, &result);
118 if (EMAIL_ERROR_NONE != error) {
119 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
120 "Couldn't retrieve mailbox. [" << error << "]");
127 int addMail(email_mail_data_t* mail, email_attachment_data_t* attachment)
133 if (!mail->mailbox_id)
136 if ( mail->mailbox_type > 0)
138 email_mailbox_t* mailbox;
139 error = email_get_mailbox_by_mailbox_type(mail->account_id, EMAIL_MAILBOX_TYPE_DRAFT, &mailbox );
140 if (EMAIL_ERROR_NONE != error) {
141 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
142 "Couldn't add message to mailbox. [" << error << "]");
145 // if ( mailbox->mailbox_name )
146 // mail->mailbox_name = strdup(mailbox->mailbox_name);
148 // LogDebug("mail MailBox Name :" << mail->mailbox_name);
150 error = email_free_mailbox(&mailbox, 1);
151 if (EMAIL_ERROR_NONE != error) {
152 LogError("Failed to destroy mailbox: " << error);
157 error = email_add_mail(mail, attachment, mail->attachment_count, NULL, 0);
158 if (EMAIL_ERROR_NONE != error) {
159 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
160 "Couldn't add message to mailbox. [" << error << "]");
164 LogDebug("Mail to MailBox, mail id = " << mail->mail_id );
165 return mail->mail_id;
169 int addMailToMailbox(email_mail_data_t* mail,
170 email_attachment_data_t* attachment, int attachment_count )
175 int error = email_add_mail(mail, attachment, attachment_count, NULL, 0);
176 if (EMAIL_ERROR_NONE != error) {
177 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
178 "Couldn't add message to mailbox. [" << error << "]");
182 LogDebug("Mail to MailBox, mail id = " << mail->mail_id );
183 return mail->mail_id;
187 void freeAttachment(email_attachment_data_t* attachment)
190 if (NULL == attachment) { return; }
192 int error = email_free_attachment_data(&attachment, 1);
193 if (EMAIL_ERROR_NONE != error) {
194 LogWarning("Couldn't free attachment. [" << error << "]");
199 email_mailbox_t* createMailbox(int accountId,
203 email_mailbox_t* result = alloc<email_mailbox_t>();
204 result->account_id = accountId;
205 result->name = (NULL != name ? strdup(name) : NULL);
212 // TODO This clonning is not efficent.
213 email_mail_data_t* cloneMail(const email_mail_data_t* mail)
216 email_mail_data_t* result = readMail(mail->mail_id);
218 result->account_id = 0;
224 email_mailbox_t* getMailboxByMailId(int accountId, int mailId)
228 char* mailboxName = NULL;
229 int error = email_get_mailbox_name_by_mail_id(mailId, &mailboxName);
230 // Platform may allocate mailboxName and yet return an error code.
231 DPL::ScopedFree<char> freeGuard(mailboxName);
232 if (EMAIL_ERROR_NONE != error) {
233 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
234 "Couldn't get mailbox name. [" << error << "]");
237 email_mailbox_t* result = NULL;
238 error = email_get_mailbox_by_name(accountId, mailboxName, &result);
239 if (EMAIL_ERROR_NONE != error) {
240 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
241 "Couldn't get mailbox. [" << error << "]");
246 email_mailbox_t* result = NULL;
247 email_mail_data_t* mail_data = NULL;
249 error = email_get_mail_data(mailId, &mail_data);
250 if (EMAIL_ERROR_NONE != error) {
251 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
252 "Couldn't get mailbox data " << mailId << ". [" << error << "]");
255 error = email_get_mailbox_by_mailbox_id(mail_data->mailbox_id, &result);
256 if (EMAIL_ERROR_NONE != error) {
259 email_free_mail_data(&mail_data, 1);
261 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
262 "Couldn't get mailbox " << mail_data->mailbox_id << ". [" << error << "]");
266 email_free_mail_data(&mail_data, 1);
273 void updateSeenFlag(int accountId, int mailId, bool isReadChangeStatus)
277 if ( accountId > -1 )
279 if (EMAIL_ERROR_NONE !=
280 email_set_flags_field(accountId, &mailId, 0, EMAIL_FLAGS_SEEN_FIELD, isReadChangeStatus, 1) )
282 LogWarning("email_modify_seen_flag failed\n");
286 LogWarning("email_modify_seen_flag SUCCESS\n");
291 LogWarning("Invaild Account ID\n");