921de111a051cd4b8ffe3f9856dc61f74b24ef28
[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 mailId, const int accountId)
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::createVirtualMessage()
158 {
159     return IMessagePtr(new VirtualMessage());
160 }
161
162 VirtualMessagePtr MessageFactory::convertToVirtualMessage(IMessagePtr msg)
163 {
164     VirtualMessagePtr tmp = DPL::DynamicPointerCast<VirtualMessage >(msg);
165     if (!tmp) {
166         ThrowMsg(ConversionException,
167                  "Conversion IMessage to VirtualMessagePtr error");
168     }
169     return tmp;
170 }
171
172 ISmsPtr MessageFactory::convertToSms(IMessagePtr msg)
173 {
174     ISmsPtr tmp = DPL::DynamicPointerCast<ISms >(msg);
175     if (!tmp) {
176         ThrowMsg(ConversionException, "Conversion IMessage to ISms error");
177     }
178     return tmp;
179 }
180
181 IBinarySmsPtr MessageFactory::convertToBinarySms(IMessagePtr msg)
182 {
183     IBinarySmsPtr tmp = DPL::DynamicPointerCast<IBinarySms >(msg);
184     if (!tmp) {
185         ThrowMsg(ConversionException, "Conversion IMessage to IBinarySms error");
186     }
187     return tmp;
188 }
189
190 IMmsPtr MessageFactory::convertToMms(IMessagePtr msg)
191 {
192     IMmsPtr tmp = DPL::DynamicPointerCast<IMms >(msg);
193     if (!tmp) {
194         ThrowMsg(ConversionException, "Conversion IMessage to IMms error");
195     }
196     return tmp;
197 }
198
199 IEmailPtr MessageFactory::convertToEmail(IMessagePtr msg)
200 {
201     IEmailPtr tmp = DPL::DynamicPointerCast<IEmail >(msg);
202     if (!tmp) {
203         ThrowMsg(ConversionException, "Conversion IMessage to IEmail error");
204     }
205     return tmp;
206 }
207 }
208 }
209 }