Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / platform / API / Messaging / MessageFactory.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 /**
19  *
20  *
21  * @file       MessageFactory.cpp
22  * @author     Pawel Misiak (p.misiak@samsung.com)
23  * @version    0.1
24  * @brief
25  */
26 #include <dpl/log/log.h>
27 #include <Commons/Exception.h>
28 #include <Messaging/Sms.h>
29 #include <Messaging/BinarySms.h>
30 #include <Messaging/Mms.h>
31 #include <Messaging/Email.h>
32 #include "MessageFactory.h"
33 #include <API/Messaging/IAttachment.h>
34
35 using namespace std;
36 using namespace WrtDeviceApis::Commons;
37 using namespace TizenApis::Platform::Messaging;
38
39 namespace TizenApis {
40 namespace Api {
41 namespace Messaging {
42
43 IMessagePtr MessageFactory::createMessage(const MessageType msgType,
44         const std::string& id)
45 {
46     LogInfo("enter");
47     IMessage* retVal = NULL;
48     switch (msgType) {
49     case SMS:
50         LogDebug("creation sms message");
51         retVal = new Sms(id);
52         break;
53
54     case BINARYSMS:
55         LogDebug("creation binary sms message");
56         retVal = new BinarySms(id);
57         break;
58
59     case MMS:
60         LogDebug("creation mms message");
61         retVal = new Mms(id);
62         break;
63
64     case EMAIL:
65         LogDebug("creation email message");
66         retVal = new Email(id);
67         break;
68
69     default:
70         LogError("creation error, unknown message type");
71         break;
72     }
73     return IMessagePtr(retVal);
74 }
75
76 IMessagePtr MessageFactory::createMessage(const MessageType msgType,
77         const int id)
78 {
79     std::ostringstream stream;
80     stream << id;
81     std::string idStr = stream.str();
82     return createMessage(msgType, idStr);
83 }
84
85 IMessagePtr MessageFactory::createMessage(const MessageType msgType,
86            Api::Messaging::EmailAccountInfo& account, const std::string& id )
87 {
88         LogInfo("enter, CreateMessage with account");
89                 
90         std::ostringstream stream;
91         stream << id;
92         std::string idStr = stream.str();
93         
94         LogInfo("createMessage with , idstr =" << idStr);
95         
96         if (msgType == EMAIL)
97         {       
98                 LogDebug("Account Address:" << &account);
99                 IMessage* retVal = NULL;
100                 LogDebug("creation email message with account");
101                 retVal = new Email(account);
102                 return IMessagePtr(retVal);
103         }
104         
105         return createMessage(msgType, idStr);
106         
107 }
108
109 IMessagePtr MessageFactory::createEmailMessage(const int accountId, const int mailId)
110 {
111         LogInfo("enter, Create Email Message with account ID");
112         IEmail* email = NULL;
113         LogDebug("creation email message with account ID");
114         std::stringstream stream;
115         stream << mailId;
116         if (stream.fail()) {
117                 ThrowMsg(UnknownException,  "Couldn't convert e-mail account id");
118         }
119         
120         email = new Email(stream.str(), accountId);
121
122         IMessagePtr imsg(email);
123
124         std::vector<IAttachmentPtr> attachments = email->getAttachments();
125
126         int idx = 0;
127         for (; idx < attachments.size() ; idx++ )
128         {
129                 LogDebug("set Messsage ID = " << attachments[idx]->getAttachmentID());
130                 attachments[idx]->setMessage(imsg);
131         }
132
133         return imsg;
134         
135         
136 #if 0
137         IMessage* retVal = NULL;
138         LogDebug("creation email message with account ID");
139
140         
141         std::stringstream stream;
142         stream << mailId;
143         if (stream.fail()) {
144                 ThrowMsg(UnknownException,  "Couldn't convert e-mail account id");
145         }
146         
147         retVal = new Email(stream.str(), accountId);
148
149         
150         
151         
152                 
153         return IMessagePtr(retVal);
154 #endif
155 }
156
157 IMessagePtr MessageFactory::createEmailMessage()
158 {
159         LogInfo("enter, Create Temporary Email Message without account");
160
161         IEmail* email = NULL;
162
163         email = new Email(Api::Messaging::TEMP_FOLDER); //create dumy email.
164
165         IMessagePtr imsg(email);
166         
167         return imsg;
168 }
169
170 IMessagePtr MessageFactory::createVirtualMessage()
171 {
172     return IMessagePtr(new VirtualMessage());
173 }
174
175 VirtualMessagePtr MessageFactory::convertToVirtualMessage(IMessagePtr msg)
176 {
177     VirtualMessagePtr tmp = DPL::DynamicPointerCast<VirtualMessage >(msg);
178     if (!tmp) {
179         ThrowMsg(ConversionException,
180                  "Conversion IMessage to VirtualMessagePtr error");
181     }
182     return tmp;
183 }
184
185 ISmsPtr MessageFactory::convertToSms(IMessagePtr msg)
186 {
187     ISmsPtr tmp = DPL::DynamicPointerCast<ISms >(msg);
188     if (!tmp) {
189         ThrowMsg(ConversionException, "Conversion IMessage to ISms error");
190     }
191     return tmp;
192 }
193
194 IBinarySmsPtr MessageFactory::convertToBinarySms(IMessagePtr msg)
195 {
196     IBinarySmsPtr tmp = DPL::DynamicPointerCast<IBinarySms >(msg);
197     if (!tmp) {
198         ThrowMsg(ConversionException, "Conversion IMessage to IBinarySms error");
199     }
200     return tmp;
201 }
202
203 IMmsPtr MessageFactory::convertToMms(IMessagePtr msg)
204 {
205     IMmsPtr tmp = DPL::DynamicPointerCast<IMms >(msg);
206     if (!tmp) {
207         ThrowMsg(ConversionException, "Conversion IMessage to IMms error");
208     }
209     return tmp;
210 }
211
212 IEmailPtr MessageFactory::convertToEmail(IMessagePtr msg)
213 {
214     IEmailPtr tmp = DPL::DynamicPointerCast<IEmail >(msg);
215     if (!tmp) {
216         ThrowMsg(ConversionException, "Conversion IMessage to IEmail error");
217     }
218     return tmp;
219 }
220 }
221 }
222 }