Update change log and spec for wrt-plugins-tizen_0.4.65
[platform/framework/web/wrt-plugins-tizen.git] / src / Messaging / EmailService.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 #include <string>
19 #include <sstream>
20
21 #include <dpl/scoped_free.h>
22
23 #include <email-api.h>
24
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"
31
32 #define LOG_ENTER LoggerD("---> ENTER");
33 #define LOG_EXIT LoggerD("---> EXIT");
34
35 namespace DeviceAPI {
36 namespace Messaging {
37 namespace EmailService {
38
39 email_mail_data_t* createMailData(const EmailAccountInfo& account, const std::string& filePath)
40 {
41     LOG_ENTER
42     int error = 0;
43     ScopedMail result(alloc<email_mail_data_t>());
44
45     result->account_id = account.getIntId();
46     result->flags_draft_field = 1;
47     result->priority = EMAIL_MAIL_PRIORITY_NORMAL;
48
49     std::string from = EmailUtils::formatAddress(account.getAddress(), account.getName());
50     result->full_address_from = strdup(from.c_str());
51
52     char buf[] = "XXXXXX";
53     mode_t mask = umask(S_IWGRP | S_IWOTH);
54     error = mkstemp(buf);
55     umask(mask);
56     std::string bodyFile = buf;
57     bodyFile = filePath+EmailUtils::TMP_DIR_SEPERATOR+bodyFile;
58     LoggerD("bodyFile : " << bodyFile);
59
60     FILE* f = fopen(bodyFile.c_str(), "w");
61     if (NULL != f) {
62         fclose(f);
63     }
64
65     result->file_path_plain = strdup(bodyFile.c_str());
66
67 // fix email empty plain text
68 /*
69     std::string htmlFile = tmpnam(NULL);
70     f = fopen(htmlFile.c_str(), "w");
71     fclose(f);
72     result->file_path_html = strdup(htmlFile.c_str());
73 */
74     result->file_path_html = NULL;
75
76     LOG_EXIT
77     return result.Release();
78 }
79
80 email_mail_data_t* readMail(int mailId)
81 {
82     LOG_ENTER
83     email_mail_data_t* result = NULL;
84     
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 << "]");
89     }
90
91     LOG_EXIT
92     return result;
93 }
94
95 void deleteMail(int accountId,
96         int mailId)
97 {
98     LOG_ENTER
99     int error;
100     email_mail_data_t *mail = NULL;
101
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 << "]");
106     }
107
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 << "]");
113     }
114     email_free_mail_data(&mail,1);
115     LOG_EXIT
116 }
117
118 email_mailbox_t* getMailboxByType(int accountId,
119         email_mailbox_type_e type)
120 {
121     LOG_ENTER
122     email_mailbox_t* result = NULL;
123
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 << "]");
128     }
129
130     LOG_ENTER
131     return result;
132 }
133
134 int addMail(email_mail_data_t* mail, email_attachment_data_t* attachment)
135 {
136     LOG_ENTER
137     if (!mail) {
138         LoggerE("mail is NULL");
139         Throw(WrtDeviceApis::Commons::PlatformException);
140     }
141
142     int error = 0;
143     if (!mail->mailbox_id)
144     {
145                 //get mail box
146                 if ( mail->mailbox_type > 0)
147                 {
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 << "]");
153                         }
154
155 //                      if ( mailbox->mailbox_name )
156 //                              mail->mailbox_name = strdup(mailbox->mailbox_name);
157
158 //                      LoggerD("mail MailBox Name :" << mail->mailbox_name);
159                         
160                         error = email_free_mailbox(&mailbox, 1);
161                    if (EMAIL_ERROR_NONE != error) {
162                        LoggerE("Failed to destroy mailbox: " << error);
163                    }
164                 }                                       
165     }
166
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 << "]");
171     }
172    
173     LOG_EXIT
174     LoggerD("Mail to MailBox, mail id = " << mail->mail_id );
175     return mail->mail_id;
176 }
177
178 #if 0
179 int addMailToMailbox(email_mail_data_t* mail,
180         email_attachment_data_t* attachment, int attachment_count )
181 {
182     LOG_ENTER
183     Assert(mail);
184
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 << "]");
189     }
190
191     LOG_EXIT
192     LoggerD("Mail to MailBox, mail id = " << mail->mail_id );
193     return mail->mail_id;
194 }
195 #endif
196
197 void freeAttachment(email_attachment_data_t* attachment)
198 {
199     LOG_ENTER
200     if (NULL == attachment) { return; }
201
202     int error = email_free_attachment_data(&attachment, 1);
203     if (EMAIL_ERROR_NONE != error) {
204         LoggerW("Couldn't free attachment. [" << error << "]");
205     }
206     LOG_EXIT
207 }
208 /*
209 email_mailbox_t* createMailbox(int accountId,
210         const char* name)
211 {
212     LOG_ENTER
213     email_mailbox_t* result = alloc<email_mailbox_t>();
214     result->account_id = accountId;
215     result->name = (NULL != name ? strdup(name) : NULL);
216
217     LOG_EXIT
218     return result;
219 }
220 */
221         
222 // TODO This clonning is not efficent.
223 email_mail_data_t* cloneMail(const email_mail_data_t* mail)
224 {
225     LOG_ENTER
226     email_mail_data_t* result = readMail(mail->mail_id);
227     result->mail_id = 0;
228     result->account_id = 0;
229
230     LOG_EXIT
231     return result;
232 }
233
234 email_mailbox_t* getMailboxByMailId(int accountId, int mailId)
235 {
236     LOG_ENTER
237 /*              
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 << "]");
245     }
246
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 << "]");
252     }
253 */
254     int error;
255
256     email_mailbox_t* result = NULL;
257     email_mail_data_t* mail_data = NULL;
258                 
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 << "]");
263     }
264
265         if (mail_data == NULL) {
266                 ThrowMsg(WrtDeviceApis::Commons::PlatformException,     "Couldn't get mail_data");
267         }
268
269     error = email_get_mailbox_by_mailbox_id(mail_data->mailbox_id, &result);
270     if (EMAIL_ERROR_NONE != error) {
271         if(mail_data)
272         {
273             email_free_mail_data(&mail_data, 1);
274         }
275         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
276                  "Couldn't get mailbox " << mail_data->mailbox_id << ". [" << error << "]");
277     }
278     if(mail_data)
279     {
280         email_free_mail_data(&mail_data, 1);
281     }
282
283     LOG_EXIT
284     return result;
285 }
286
287 void updateSeenFlag(int accountId, int mailId, bool isReadChangeStatus)
288 {
289     LOG_ENTER
290         
291     if ( accountId > -1 )
292     {
293             if (EMAIL_ERROR_NONE != 
294                         email_set_flags_field(accountId, &mailId, 0, EMAIL_FLAGS_SEEN_FIELD, isReadChangeStatus, 1) )
295     {
296         LoggerW("email_modify_seen_flag failed\n");
297     }
298     else
299     {
300         LoggerW("email_modify_seen_flag SUCCESS\n");
301     }
302     }
303     else
304     {
305                 LoggerW("Invaild Account ID\n");
306     }
307         
308     LOG_EXIT
309 }
310
311
312 }
313 }
314 }