wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Messaging / MessageFactory.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 <Commons/Exception.h>
19 #include "Sms.h"
20 #include "BinarySms.h"
21 #include "Mms.h"
22 #include "Email.h"
23 #include "EmptyMessage.h"
24 #include "MessageFactory.h"
25 #include "IAttachment.h"
26 #include <Logger.h>
27
28 using namespace std;
29 using namespace WrtDeviceApis::Commons;
30 using namespace DeviceAPI::Messaging;
31
32 namespace DeviceAPI {
33 namespace Messaging {
34
35 IMessagePtr MessageFactory::createMessage(const MessageType msgType,
36         const std::string& id)
37 {
38
39         LoggerI("enter");
40
41         ISms* retSms = NULL;
42         IMms* retMms =NULL;
43         IEmail* retEmail = NULL;
44         IBinarySms* retBinarySms = NULL;
45         IEmptyMessage* retEmptyMessage = NULL;
46
47         IMessagePtr imsg(NULL);
48
49         switch (msgType) {
50                 case SMS:
51                 {
52                         LoggerD("creation sms message with id");
53                         retSms = new Sms(id);
54                         imsg = (IMessagePtr) retSms;
55                         break;
56                 }
57
58                 case BINARYSMS:
59                 {
60                         LoggerD("creation binary sms message with id");
61                         retBinarySms = new BinarySms(id);
62                         imsg = (IMessagePtr) retBinarySms;
63                 break;
64                 }
65
66                 case MMS:
67                 {
68                         LoggerD("creation mms message with id");
69                         retMms =        new Mms(id);
70                         imsg = (IMessagePtr) retMms;
71
72                         std::vector<IAttachmentPtr> attachments = retMms->getAttachments();
73
74                         unsigned int idx = 0;
75                         for (; idx < attachments.size() ; idx++ )
76                         {
77                         LoggerD("set Messsage ID = " << attachments[idx]->getAttachmentID());
78                         attachments[idx]->setMessage(imsg);
79                         }
80                         break;
81                 }
82                 case EMAIL:
83                 {
84                         LoggerD("creation email message with id");
85                         retEmail= new Email(id);
86                         imsg = (IMessagePtr) retEmail;
87                         break;
88                 }
89                 case EMPTY_MESSAGE:
90                 {
91                         LoggerD("creation empty message with id");
92                         retEmptyMessage = new EmptyMessage(id);
93                         imsg = (IMessagePtr) retEmptyMessage;
94                         break;
95                 }
96                 default:
97                         LoggerE("creation error, unknown message type");
98                         break;
99         }
100         LoggerI("end");
101         return imsg;
102
103 }
104
105 IMessagePtr MessageFactory::createMessage(const MessageType msgType,
106         const int id)
107 {
108     std::ostringstream stream;
109     stream << id;
110     std::string idStr = stream.str();
111     return createMessage(msgType, idStr);
112 }
113
114 IMessagePtr MessageFactory::createMessage(const MessageType msgType,
115         const int id, const msg_struct_t msg_data)
116 {
117     LoggerI("enter");
118     IMessage* retVal = NULL;
119     std::ostringstream stream;
120     stream << id;
121     std::string idStr = stream.str();
122         
123     switch (msgType) {
124     case SMS:
125         LoggerD("creation sms message with data");
126         retVal = new Sms(idStr, msg_data);
127         break;
128
129     case MMS:
130         LoggerD("creation mms message with data");
131         retVal = new Mms(idStr, msg_data);
132         break;
133
134     case EMPTY_MESSAGE:
135         LoggerD("creation empty message with data");
136         retVal = new EmptyMessage(idStr);
137         break;
138                 
139     default:
140         LoggerE("creation error, unknown message type");
141         break;
142     }
143     LoggerI("end");
144     return IMessagePtr(retVal);
145 }
146
147 IMessagePtr MessageFactory::createMessage(const MessageType msgType,
148            EmailAccountInfo& account, const std::string& id )
149 {
150         LoggerI("enter, CreateMessage with account");
151                 
152         std::ostringstream stream;
153         stream << id;
154         std::string idStr = stream.str();
155         
156         LoggerI("createMessage with , idstr =" << idStr);
157         
158         if (msgType == EMAIL)
159         {       
160                 LoggerD("Account Address:" << &account);
161                 IMessage* retVal = NULL;
162                 LoggerD("creation email message with account");
163                 retVal = new Email(account);
164                 return IMessagePtr(retVal);
165         }
166         
167         return createMessage(msgType, idStr);
168         
169 }
170
171 IMessagePtr MessageFactory::createEmailMessage(const int accountId, const int mailId)
172 {
173         LoggerI("enter, Create Email Message with account ID");
174         IEmail* email = NULL;
175         LoggerD("creation email message with account ID");
176         std::stringstream stream;
177         unsigned int idx;
178         
179         stream << mailId;
180         if (stream.fail()) {
181                 ThrowMsg(UnknownException,  "Couldn't convert e-mail account id");
182         }
183         
184         email = new Email(stream.str(), accountId);
185
186         IMessagePtr imsg(email);
187
188         std::vector<IAttachmentPtr> attachments = email->getAttachments();
189         std::vector<IAttachmentPtr> inlineAttachments = email->getInlineAttachments();
190
191         for (idx = 0; idx < attachments.size() ; idx++ )
192         {
193                 LoggerD("set Attachment ID = " << attachments[idx]->getAttachmentID());
194                 attachments[idx]->setMessage(imsg);
195         }
196
197         for (idx = 0; idx < inlineAttachments.size() ; idx++ )
198         {
199                 LoggerD("set inline Attachment ID = " << inlineAttachments[idx]->getAttachmentID());
200                 inlineAttachments[idx]->setMessage(imsg);
201         }
202
203         return imsg;
204         
205         
206 #if 0
207         IMessage* retVal = NULL;
208         LoggerD("creation email message with account ID");
209
210         
211         std::stringstream stream;
212         stream << mailId;
213         if (stream.fail()) {
214                 ThrowMsg(UnknownException,  "Couldn't convert e-mail account id");
215         }
216         
217         retVal = new Email(stream.str(), accountId);
218
219         
220         
221         
222                 
223         return IMessagePtr(retVal);
224 #endif
225 }
226
227 IMessagePtr MessageFactory::createEmailMessage()
228 {
229         LoggerI("enter, Create Temporary Email Message without account");
230
231         IEmail* email = NULL;
232
233         email = new Email(TEMP_FOLDER); //create dumy email.
234
235         IMessagePtr imsg(email);
236         
237         return imsg;
238 }
239
240 IMessagePtr MessageFactory::createVirtualMessage()
241 {
242     return IMessagePtr(new VirtualMessage());
243 }
244
245 VirtualMessagePtr MessageFactory::convertToVirtualMessage(IMessagePtr msg)
246 {
247     VirtualMessagePtr tmp = DPL::DynamicPointerCast<VirtualMessage >(msg);
248     if (!tmp) {
249         ThrowMsg(ConversionException,
250                  "Conversion IMessage to VirtualMessagePtr error");
251     }
252     return tmp;
253 }
254
255 ISmsPtr MessageFactory::convertToSms(IMessagePtr msg)
256 {
257     ISmsPtr tmp = DPL::DynamicPointerCast<ISms >(msg);
258     if (!tmp) {
259         ThrowMsg(ConversionException, "Conversion IMessage to ISms error");
260     }
261     return tmp;
262 }
263
264 IBinarySmsPtr MessageFactory::convertToBinarySms(IMessagePtr msg)
265 {
266     IBinarySmsPtr tmp = DPL::DynamicPointerCast<IBinarySms >(msg);
267     if (!tmp) {
268         ThrowMsg(ConversionException, "Conversion IMessage to IBinarySms error");
269     }
270     return tmp;
271 }
272
273 IMmsPtr MessageFactory::convertToMms(IMessagePtr msg)
274 {
275     IMmsPtr tmp = DPL::DynamicPointerCast<IMms >(msg);
276     if (!tmp) {
277         ThrowMsg(ConversionException, "Conversion IMessage to IMms error");
278     }
279     return tmp;
280 }
281
282 IEmailPtr MessageFactory::convertToEmail(IMessagePtr msg)
283 {
284     IEmailPtr tmp = DPL::DynamicPointerCast<IEmail >(msg);
285     if (!tmp) {
286         ThrowMsg(ConversionException, "Conversion IMessage to IEmail error");
287     }
288     return tmp;
289 }
290 }
291 }