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.
17 * @author Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
22 #include <dpl/assert.h>
23 #include <dpl/scoped_free.h>
27 #include <Commons/Exception.h>
28 #include "EmailUtils.h"
29 #include "EmailService.h"
30 #include "ScopedMail.h"
31 #include "ScopedMailbox.h"
33 #define LOG_ENTER LogDebug("---> ENTER");
34 #define LOG_EXIT LogDebug("---> EXIT");
36 namespace WrtDeviceApis {
38 namespace EmailService {
39 emf_mail_t* createMail(const Api::EmailAccountInfo& account)
42 ScopedMail result(alloc<emf_mail_t>());
44 result->info = alloc<emf_mail_info_t>();
45 result->info->account_id = account.getIntId();
46 result->info->flags.draft = 1;
47 result->info->extra_flags.priority = EMF_MAIL_PRIORITY_NORMAL;
49 result->head = alloc<emf_mail_head_t>();
50 std::string from = EmailUtils::formatAddress(account.getAddress(),
52 result->head->from = strdup(from.c_str());
54 result->body = alloc<emf_mail_body_t>();
55 std::string bodyFile = tmpnam(NULL);
56 // TODO: change following two lines to some (not implemented yet) call
57 // to Filesystem module (e.g. Filesystem::IManager::getInstance().touch(PATH)).
58 FILE* f = fopen(bodyFile.c_str(), "w");
60 result->body->plain = strdup(bodyFile.c_str());
63 return result.Release();
66 emf_mail_t* readMail(int accountId,
70 emf_mail_t* result = NULL;
72 emf_mailbox_t mailbox;
73 memset(&mailbox, 0, sizeof(emf_mailbox_t));
74 mailbox.account_id = accountId;
76 int error = email_get_mail(&mailbox, mailId, &result);
77 if (EMF_ERROR_NONE != error) {
78 ThrowMsg(Commons::PlatformException,
79 "Couldn't find message " << mailId << ". [" << error << "]");
86 void deleteMail(int accountId,
90 emf_mailbox_t mailbox;
91 memset(&mailbox, 0, sizeof(emf_mailbox_t));
92 mailbox.account_id = accountId;
94 int error = email_delete_message(&mailbox, &mailId, 1, 0);
95 if (EMF_ERROR_NONE != error) {
96 ThrowMsg(Commons::PlatformException,
97 "Error while deleting mail. [" << error << "]");
102 emf_mailbox_t* getMailboxByType(int accountId,
103 emf_mailbox_type_e type)
106 emf_mailbox_t* result = NULL;
108 int error = email_get_mailbox_by_mailbox_type(accountId, type, &result);
109 if (EMF_ERROR_NONE != error) {
110 ThrowMsg(Commons::PlatformException,
111 "Couldn't retrieve mailbox. [" << error << "]");
118 int addMailToMailbox(emf_mail_t* mail,
119 emf_mailbox_t* mailbox)
122 Assert(mail && mailbox);
124 int error = email_add_message(mail, mailbox, 1);
125 if (EMF_ERROR_NONE != error) {
126 ThrowMsg(Commons::PlatformException,
127 "Couldn't add message to mailbox. [" << error << "]");
131 return mail->info->uid;
134 void freeAttachment(emf_attachment_info_t* attachment)
137 if (NULL == attachment) { return; }
139 int error = email_free_attachment_info(&attachment);
140 if (EMF_ERROR_NONE != error) {
141 LogWarning("Couldn't free attachment. [" << error << "]");
146 emf_mailbox_t* createMailbox(int accountId,
150 emf_mailbox_t* result = alloc<emf_mailbox_t>();
151 result->account_id = accountId;
152 result->name = (NULL != name ? strdup(name) : NULL);
158 // TODO This clonning is not efficent.
159 emf_mail_t* cloneMail(const emf_mail_t* mail)
162 emf_mail_t* result = readMail(mail->info->account_id, mail->info->uid);
163 result->info->uid = 0;
169 emf_mailbox_t* getMailboxByMailId(int accountId,
173 char* mailboxName = NULL;
174 int error = email_get_mailbox_name_by_mail_id(mailId, &mailboxName);
175 // Platform may allocate mailboxName and yet return an error code.
176 DPL::ScopedFree<char> freeGuard(mailboxName);
177 if (EMF_ERROR_NONE != error) {
178 ThrowMsg(Commons::PlatformException,
179 "Couldn't get mailbox name. [" << error << "]");
182 emf_mailbox_t* result = NULL;
183 error = email_get_mailbox_by_name(accountId, mailboxName, &result);
184 if (EMF_ERROR_NONE != error) {
185 ThrowMsg(Commons::PlatformException,
186 "Couldn't get mailbox. [" << error << "]");