Update change log and spec for wrt-plugins-tizen_0.2.73
[profile/ivi/wrt-plugins-tizen.git] / src / platform / 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 /**
18  * @author          Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
19  */
20 #include <string>
21 #include <sstream>
22
23 #include <dpl/assert.h>
24 #include <dpl/scoped_free.h>
25
26 #include <email-api.h>
27
28 #include <Commons/Exception.h>
29 #include "EmailUtils.h"
30 #include "EmailService.h"
31 #include "ScopedMail.h"
32 #include "ScopedMailbox.h"
33
34 #define LOG_ENTER LogDebug("---> ENTER");
35 #define LOG_EXIT LogDebug("---> EXIT");
36
37 namespace TizenApis {
38 namespace Platform {
39 namespace Messaging {
40 namespace EmailService {
41
42 email_mail_data_t* createMailData(const Api::Messaging::EmailAccountInfo& account)
43 {
44     LOG_ENTER
45     ScopedMail result(alloc<email_mail_data_t>());
46
47     result->account_id = account.getIntId();
48     result->flags_draft_field = 1;
49     result->priority = EMAIL_MAIL_PRIORITY_NORMAL;
50
51     std::string from = EmailUtils::formatAddress(account.getAddress(), account.getName());
52     result->full_address_from = strdup(from.c_str());
53
54     std::string bodyFile = tmpnam(NULL);
55     FILE* f = fopen(bodyFile.c_str(), "w");
56     fclose(f);
57
58     result->file_path_plain = strdup(bodyFile.c_str());
59
60 // fix email empty plain text
61 /*
62     std::string htmlFile = tmpnam(NULL);
63     f = fopen(htmlFile.c_str(), "w");
64     fclose(f);
65     result->file_path_html = strdup(htmlFile.c_str());
66 */
67     result->file_path_html = NULL;
68
69     LOG_EXIT
70     return result.Release();
71 }
72
73 email_mail_data_t* readMail(int mailId)
74 {
75     LOG_ENTER
76     email_mail_data_t* result = NULL;
77     
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 << "]");
82     }
83
84     LOG_EXIT
85     return result;
86 }
87
88 void deleteMail(int accountId,
89         int mailId)
90 {
91     LOG_ENTER
92     int error;
93     email_mail_data_t *mail = NULL;
94
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 << "]");
99     }
100
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 << "]");
106     }
107     email_free_mail_data(&mail,1);
108     LOG_EXIT
109 }
110
111 email_mailbox_t* getMailboxByType(int accountId,
112         email_mailbox_type_e type)
113 {
114     LOG_ENTER
115     email_mailbox_t* result = NULL;
116
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 << "]");
121     }
122
123     LOG_ENTER
124     return result;
125 }
126
127 int addMail(email_mail_data_t* mail, email_attachment_data_t* attachment)
128 {
129     LOG_ENTER
130     Assert(mail);
131
132     int error = 0;
133     if (!mail->mailbox_id)
134     {
135                 //get mail box
136                 if ( mail->mailbox_type > 0)
137                 {
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 << "]");
143                         }
144
145 //                      if ( mailbox->mailbox_name )
146 //                              mail->mailbox_name = strdup(mailbox->mailbox_name);
147
148 //                      LogDebug("mail MailBox Name :" << mail->mailbox_name);
149                         
150                         error = email_free_mailbox(&mailbox, 1);
151                    if (EMAIL_ERROR_NONE != error) {
152                        LogError("Failed to destroy mailbox: " << error);
153                    }
154                 }                                       
155     }
156
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 << "]");
161     }
162    
163     LOG_EXIT
164     LogDebug("Mail to MailBox, mail id = " << mail->mail_id );
165     return mail->mail_id;
166 }
167
168 #if 0
169 int addMailToMailbox(email_mail_data_t* mail,
170         email_attachment_data_t* attachment, int attachment_count )
171 {
172     LOG_ENTER
173     Assert(mail);
174
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 << "]");
179     }
180
181     LOG_EXIT
182     LogDebug("Mail to MailBox, mail id = " << mail->mail_id );
183     return mail->mail_id;
184 }
185 #endif
186
187 void freeAttachment(email_attachment_data_t* attachment)
188 {
189     LOG_ENTER
190     if (NULL == attachment) { return; }
191
192     int error = email_free_attachment_data(&attachment, 1);
193     if (EMAIL_ERROR_NONE != error) {
194         LogWarning("Couldn't free attachment. [" << error << "]");
195     }
196     LOG_EXIT
197 }
198 /*
199 email_mailbox_t* createMailbox(int accountId,
200         const char* name)
201 {
202     LOG_ENTER
203     email_mailbox_t* result = alloc<email_mailbox_t>();
204     result->account_id = accountId;
205     result->name = (NULL != name ? strdup(name) : NULL);
206
207     LOG_EXIT
208     return result;
209 }
210 */
211         
212 // TODO This clonning is not efficent.
213 email_mail_data_t* cloneMail(const email_mail_data_t* mail)
214 {
215     LOG_ENTER
216     email_mail_data_t* result = readMail(mail->mail_id);
217     result->mail_id = 0;
218     result->account_id = 0;
219
220     LOG_EXIT
221     return result;
222 }
223
224 email_mailbox_t* getMailboxByMailId(int accountId, int mailId)
225 {
226     LOG_ENTER
227 /*              
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 << "]");
235     }
236
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 << "]");
242     }
243 */
244     int error;
245
246     email_mailbox_t* result = NULL;
247     email_mail_data_t* mail_data = NULL;
248                 
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 << "]");
253     }
254
255     error = email_get_mailbox_by_mailbox_id(mail_data->mailbox_id, &result);
256     if (EMAIL_ERROR_NONE != error) {
257         if(mail_data)
258         {
259             email_free_mail_data(&mail_data, 1);
260         }
261         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
262                  "Couldn't get mailbox " << mail_data->mailbox_id << ". [" << error << "]");
263     }
264     if(mail_data)
265     {
266         email_free_mail_data(&mail_data, 1);
267     }
268
269     LOG_EXIT
270     return result;
271 }
272
273 void updateSeenFlag(int accountId, int mailId, bool isReadChangeStatus)
274 {
275     LOG_ENTER
276         
277     if ( accountId > -1 )
278     {
279             if (EMAIL_ERROR_NONE != 
280                         email_set_flags_field(accountId, &mailId, 0, EMAIL_FLAGS_SEEN_FIELD, isReadChangeStatus, 1) )
281     {
282         LogWarning("email_modify_seen_flag failed\n");
283     }
284     else
285     {
286         LogWarning("email_modify_seen_flag SUCCESS\n");
287     }
288     }
289     else
290     {
291                 LogWarning("Invaild Account ID\n");
292     }
293         
294     LOG_EXIT
295 }
296
297
298 }
299 }
300 }
301 }