tizen beta release
[framework/web/wrt-plugins-common.git] / src / modules / tizen / Messaging / EmailService.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  * @author          Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
18  */
19 #include <string>
20 #include <sstream>
21
22 #include <dpl/assert.h>
23 #include <dpl/scoped_free.h>
24
25 #include <Emf_Mapi.h>
26
27 #include <Commons/Exception.h>
28 #include "EmailUtils.h"
29 #include "EmailService.h"
30 #include "ScopedMail.h"
31 #include "ScopedMailbox.h"
32
33 #define LOG_ENTER LogDebug("---> ENTER");
34 #define LOG_EXIT LogDebug("---> EXIT");
35
36 namespace WrtDeviceApis {
37 namespace Messaging {
38 namespace EmailService {
39 emf_mail_t* createMail(const Api::EmailAccountInfo& account)
40 {
41     LOG_ENTER
42     ScopedMail result(alloc<emf_mail_t>());
43
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;
48
49     result->head = alloc<emf_mail_head_t>();
50     std::string from = EmailUtils::formatAddress(account.getAddress(),
51                                                  account.getName());
52     result->head->from = strdup(from.c_str());
53
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");
59     fclose(f);
60     result->body->plain = strdup(bodyFile.c_str());
61
62     LOG_EXIT
63     return result.Release();
64 }
65
66 emf_mail_t* readMail(int accountId,
67         int mailId)
68 {
69     LOG_ENTER
70     emf_mail_t* result = NULL;
71
72     emf_mailbox_t mailbox;
73     memset(&mailbox, 0, sizeof(emf_mailbox_t));
74     mailbox.account_id = accountId;
75
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 << "]");
80     }
81
82     LOG_EXIT
83     return result;
84 }
85
86 void deleteMail(int accountId,
87         int mailId)
88 {
89     LOG_ENTER
90     emf_mailbox_t mailbox;
91     memset(&mailbox, 0, sizeof(emf_mailbox_t));
92     mailbox.account_id = accountId;
93
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 << "]");
98     }
99     LOG_EXIT
100 }
101
102 emf_mailbox_t* getMailboxByType(int accountId,
103         emf_mailbox_type_e type)
104 {
105     LOG_ENTER
106     emf_mailbox_t* result = NULL;
107
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 << "]");
112     }
113
114     LOG_ENTER
115     return result;
116 }
117
118 int addMailToMailbox(emf_mail_t* mail,
119         emf_mailbox_t* mailbox)
120 {
121     LOG_ENTER
122     Assert(mail && mailbox);
123
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 << "]");
128     }
129
130     LOG_EXIT
131     return mail->info->uid;
132 }
133
134 void freeAttachment(emf_attachment_info_t* attachment)
135 {
136     LOG_ENTER
137     if (NULL == attachment) { return; }
138
139     int error = email_free_attachment_info(&attachment);
140     if (EMF_ERROR_NONE != error) {
141         LogWarning("Couldn't free attachment. [" << error << "]");
142     }
143     LOG_ENTER
144 }
145
146 emf_mailbox_t* createMailbox(int accountId,
147         const char* name)
148 {
149     LOG_ENTER
150     emf_mailbox_t* result = alloc<emf_mailbox_t>();
151     result->account_id = accountId;
152     result->name = (NULL != name ? strdup(name) : NULL);
153
154     LOG_EXIT
155     return result;
156 }
157
158 // TODO This clonning is not efficent.
159 emf_mail_t* cloneMail(const emf_mail_t* mail)
160 {
161     LOG_ENTER
162     emf_mail_t* result = readMail(mail->info->account_id, mail->info->uid);
163     result->info->uid = 0;
164
165     LOG_EXIT
166     return result;
167 }
168
169 emf_mailbox_t* getMailboxByMailId(int accountId,
170         int mailId)
171 {
172     LOG_ENTER
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 << "]");
180     }
181
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 << "]");
187     }
188
189     LOG_EXIT
190     return result;
191 }
192 }
193 }
194 }