2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
21 #include <dpl/scoped_free.h>
23 #include <email-api.h>
25 #include <Commons/Exception.h>
26 #include "EmailUtils.h"
27 #include "EmailService.h"
28 #include "ScopedMail.h"
29 #include "ScopedMailbox.h"
30 #include "WidgetFilePathMgr.h"
32 #define LOG_ENTER LoggerD("---> ENTER");
33 #define LOG_EXIT LoggerD("---> EXIT");
37 namespace EmailService {
39 email_mail_data_t* createMailData(const EmailAccountInfo& account, const std::string& filePath)
43 ScopedMail result(alloc<email_mail_data_t>());
45 result->account_id = account.getIntId();
46 result->flags_draft_field = 1;
47 result->priority = EMAIL_MAIL_PRIORITY_NORMAL;
49 std::string from = EmailUtils::formatAddress(account.getAddress(), account.getName());
50 result->full_address_from = strdup(from.c_str());
52 char buf[] = "XXXXXX";
53 mode_t mask = umask(S_IWGRP | S_IWOTH);
56 std::string bodyFile = buf;
57 bodyFile = filePath+EmailUtils::TMP_DIR_SEPERATOR+bodyFile;
58 LoggerD("bodyFile : " << bodyFile);
60 FILE* f = fopen(bodyFile.c_str(), "w");
65 result->file_path_plain = strdup(bodyFile.c_str());
67 // fix email empty plain text
69 std::string htmlFile = tmpnam(NULL);
70 f = fopen(htmlFile.c_str(), "w");
72 result->file_path_html = strdup(htmlFile.c_str());
74 result->file_path_html = NULL;
77 return result.Release();
80 email_mail_data_t* readMail(int mailId)
83 email_mail_data_t* result = NULL;
85 int error = email_get_mail_data(mailId, &result);
86 if (EMAIL_ERROR_NONE != error) {
87 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
88 "Couldn't find message " << mailId << ". [" << error << "]");
95 void deleteMail(int accountId,
100 email_mail_data_t *mail = NULL;
102 error = email_get_mail_data(mailId, &mail);
103 if (EMAIL_ERROR_NONE != error) {
104 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
105 "Error while deleting mail. [" << error << "]");
108 error = email_delete_mail(mail->mailbox_id, &mailId, 1, 0);
109 if (EMAIL_ERROR_NONE != error) {
110 email_free_mail_data(&mail,1);
111 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
112 "Error while deleting mail. [" << error << "]");
114 email_free_mail_data(&mail,1);
118 email_mailbox_t* getMailboxByType(int accountId,
119 email_mailbox_type_e type)
122 email_mailbox_t* result = NULL;
124 int error = email_get_mailbox_by_mailbox_type(accountId, type, &result);
125 if (EMAIL_ERROR_NONE != error) {
126 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
127 "Couldn't retrieve mailbox. [" << error << "]");
134 int addMail(email_mail_data_t* mail, email_attachment_data_t* attachment)
138 LoggerE("mail is NULL");
139 Throw(WrtDeviceApis::Commons::PlatformException);
143 if (!mail->mailbox_id)
146 if ( mail->mailbox_type > 0)
148 email_mailbox_t* mailbox;
149 error = email_get_mailbox_by_mailbox_type(mail->account_id, EMAIL_MAILBOX_TYPE_DRAFT, &mailbox );
150 if (EMAIL_ERROR_NONE != error) {
151 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
152 "Couldn't add message to mailbox. [" << error << "]");
155 // if ( mailbox->mailbox_name )
156 // mail->mailbox_name = strdup(mailbox->mailbox_name);
158 // LoggerD("mail MailBox Name :" << mail->mailbox_name);
160 error = email_free_mailbox(&mailbox, 1);
161 if (EMAIL_ERROR_NONE != error) {
162 LoggerE("Failed to destroy mailbox: " << error);
167 error = email_add_mail(mail, attachment, mail->attachment_count, NULL, 0);
168 if (EMAIL_ERROR_NONE != error) {
169 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
170 "Couldn't add message to mailbox. [" << error << "]");
174 LoggerD("Mail to MailBox, mail id = " << mail->mail_id );
175 return mail->mail_id;
179 int addMailToMailbox(email_mail_data_t* mail,
180 email_attachment_data_t* attachment, int attachment_count )
185 int error = email_add_mail(mail, attachment, attachment_count, NULL, 0);
186 if (EMAIL_ERROR_NONE != error) {
187 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
188 "Couldn't add message to mailbox. [" << error << "]");
192 LoggerD("Mail to MailBox, mail id = " << mail->mail_id );
193 return mail->mail_id;
197 void freeAttachment(email_attachment_data_t* attachment)
200 if (NULL == attachment) { return; }
202 int error = email_free_attachment_data(&attachment, 1);
203 if (EMAIL_ERROR_NONE != error) {
204 LoggerW("Couldn't free attachment. [" << error << "]");
209 email_mailbox_t* createMailbox(int accountId,
213 email_mailbox_t* result = alloc<email_mailbox_t>();
214 result->account_id = accountId;
215 result->name = (NULL != name ? strdup(name) : NULL);
222 // TODO This clonning is not efficent.
223 email_mail_data_t* cloneMail(const email_mail_data_t* mail)
226 email_mail_data_t* result = readMail(mail->mail_id);
228 result->account_id = 0;
234 email_mailbox_t* getMailboxByMailId(int accountId, int mailId)
238 char* mailboxName = NULL;
239 int error = email_get_mailbox_name_by_mail_id(mailId, &mailboxName);
240 // Platform may allocate mailboxName and yet return an error code.
241 DPL::ScopedFree<char> freeGuard(mailboxName);
242 if (EMAIL_ERROR_NONE != error) {
243 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
244 "Couldn't get mailbox name. [" << error << "]");
247 email_mailbox_t* result = NULL;
248 error = email_get_mailbox_by_name(accountId, mailboxName, &result);
249 if (EMAIL_ERROR_NONE != error) {
250 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
251 "Couldn't get mailbox. [" << error << "]");
256 email_mailbox_t* result = NULL;
257 email_mail_data_t* mail_data = NULL;
259 error = email_get_mail_data(mailId, &mail_data);
260 if (EMAIL_ERROR_NONE != error) {
261 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
262 "Couldn't get mailbox data " << mailId << ". [" << error << "]");
265 if (mail_data == NULL) {
266 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Couldn't get mail_data");
269 error = email_get_mailbox_by_mailbox_id(mail_data->mailbox_id, &result);
270 if (EMAIL_ERROR_NONE != error) {
273 email_free_mail_data(&mail_data, 1);
275 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
276 "Couldn't get mailbox " << mail_data->mailbox_id << ". [" << error << "]");
280 email_free_mail_data(&mail_data, 1);
287 void updateSeenFlag(int accountId, int mailId, bool isReadChangeStatus)
291 if ( accountId > -1 )
293 if (EMAIL_ERROR_NONE !=
294 email_set_flags_field(accountId, &mailId, 0, EMAIL_FLAGS_SEEN_FIELD, isReadChangeStatus, 1) )
296 LoggerW("email_modify_seen_flag failed\n");
300 LoggerW("email_modify_seen_flag SUCCESS\n");
305 LoggerW("Invaild Account ID\n");