Update change log and spec for wrt-plugins-tizen_0.4.70
[framework/web/wrt-plugins-tizen.git] / src / Messaging / Attachments.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 <algorithm>
19 #include <Commons/Exception.h>
20 #include "Attachments.h"
21 #include "AttachmentFactory.h"
22 #include <Logger.h>
23
24 extern "C" {
25 #include <sys/stat.h>
26 #include <stdlib.h>
27 }
28
29 using namespace WrtDeviceApis::Commons;
30 using namespace std;
31
32 //--------------------------------------------------------------------------
33 namespace DeviceAPI {
34 namespace Messaging {
35
36 Attachments::Attachments() :
37     m_validAttachments(false)
38 {
39     LoggerD("enter");
40 }
41
42 Attachments::~Attachments()
43 {
44     LoggerD("enter");
45 }
46
47 IAttachmentPtr Attachments::appendAttachment(const std::string& fullPath,
48         bool isVirtualPath)
49 {
50     LoggerD("enter, fullPath=" << fullPath);
51     IAttachmentPtr tmpAttach = AttachmentFactory::createAttachment(
52             fullPath,
53             isVirtualPath);
54     m_validAttachments = false;
55     m_attachments.push_back(tmpAttach);
56     return tmpAttach;
57 }
58
59 void Attachments::appendAttachment(const IAttachmentPtr& attachment)
60 {
61     LoggerD("enter");
62     m_validAttachments = false;
63     m_attachments.push_back(attachment);
64 }
65
66 void Attachments::appendInlineAttachment(const IAttachmentPtr& attachment)
67 {
68     LoggerD("enter");
69     m_validInineAttachments = false;
70     m_inlineAttachments.push_back(attachment);
71 }
72
73 IAttachmentPtr Attachments::appendAttachment(const IMessagePtr& message, const std::string& fullPath,
74                   bool isVirtualPath)
75 {
76         LoggerD("enter");
77         IAttachmentPtr iAttachment = appendAttachment(fullPath, isVirtualPath);
78
79         if (message && iAttachment)
80         {
81                 iAttachment->setMessage(message);
82         }
83
84         return iAttachment;
85 }
86
87 void Attachments::appendAttachment(const IMessagePtr& message, const IAttachmentPtr& attachment)
88 {
89         LoggerD("enter");
90
91     m_validAttachments = false;
92
93     if (attachment && message)
94                 attachment->setMessage(message);
95         
96     m_attachments.push_back(attachment);
97 }
98
99 void Attachments::appendAttachments(const vector<IAttachmentPtr>& attachments)
100 {
101     LoggerD("enter");
102     back_insert_iterator< vector<IAttachmentPtr> >biit(m_attachments);
103     copy(attachments.begin(), attachments.end(), biit);
104 }
105
106 size_t Attachments::getAttachmentsCount() const
107 {
108     LoggerD("enter");
109     return m_attachments.size();
110 }
111
112 size_t Attachments::getInlineAttachmentsCount() const
113 {
114     LoggerD("enter");
115     return m_inlineAttachments.size();
116 }
117
118 IAttachmentPtr Attachments::getAttachment(const size_t index) const
119 {
120     LoggerD("enter");
121     if (index >= m_attachments.size()) {
122         ThrowMsg(OutOfRangeException, "Trying to get attachment out of range");
123     }
124     return m_attachments[index];
125 }
126
127 IAttachmentPtr Attachments::getInlineAttachment(const size_t index) const
128 {
129     LoggerD("enter");
130     if (index >= m_inlineAttachments.size()) {
131         ThrowMsg(OutOfRangeException, "Trying to get attachment out of range");
132     }
133     return m_inlineAttachments[index];
134 }
135
136 void Attachments::removeAttachment(const size_t index)
137 {
138     LoggerD("enter");
139     if (index >= m_attachments.size()) {
140         ThrowMsg(OutOfRangeException, "Trying to get attachment out of range");
141     }
142     m_validAttachments = false;
143     m_attachments.erase(m_attachments.begin() + index);
144 }
145
146 void Attachments::removeAttachment(const IAttachmentPtr& attachment)
147 {
148     m_attachments.erase(
149         remove(m_attachments.begin(), m_attachments.end(), attachment),
150         m_attachments.end());
151 }
152
153 vector<IAttachmentPtr> Attachments::getAttachments() const
154 {
155     LoggerD("enter");
156     return m_attachments;
157 }
158
159 vector<IAttachmentPtr> Attachments::getInlineAttachments() const
160 {
161     LoggerD("enter");
162     return m_inlineAttachments;
163 }
164
165 vector<string> Attachments::getAttachmentsFullPaths() const
166 {
167     vector<string> retVal;
168
169     for (size_t i = 0; i < m_attachments.size(); i++) {
170         retVal.push_back(m_attachments[i]->getFullPath());
171     }
172     return retVal;
173 }
174
175 vector<string> Attachments::getAttachmentsShortNames() const
176 {
177     vector<string> retVal;
178
179     for (size_t i = 0; i < m_attachments.size(); i++) {
180         retVal.push_back(m_attachments[i]->getShortName());
181     }
182     return retVal;
183 }
184
185 void Attachments::setAttachments(const vector<string>& attachments,
186         bool isVirtualPath)
187 {
188     vector<string>::const_iterator it = attachments.begin();
189
190     m_attachments.clear();
191     while (it != attachments.end()) {
192         appendAttachment(*it, isVirtualPath);
193         ++it;
194     }
195 }
196
197 void Attachments::setAttachments(const vector<IAttachmentPtr>& attachments)
198 {
199     m_attachments = attachments;
200     m_validAttachments = false;
201 }
202
203 void Attachments::setInlineAttachments(const vector<IAttachmentPtr>& attachments)
204 {
205     m_inlineAttachments = attachments;
206     m_validInineAttachments = false;
207 }
208
209 bool Attachments::isAttachmentsValid() const
210 {
211     LoggerD("m_validAttachments : " << m_validAttachments);
212     return m_validAttachments;
213 }
214
215 void Attachments::setAttachmentsValidity(bool state)
216 {
217     m_validAttachments = state;
218 }
219
220 bool Attachments::isInlineAttachmentsValid() const
221 {
222     return m_validInineAttachments;
223 }
224
225 void Attachments::setInlineAttachmentsValidity(bool state)
226 {
227     m_validInineAttachments = state;
228 }
229
230 const vector<IAttachmentPtr>& Attachments::getAttachmentsRef() const
231 {
232     return m_attachments;
233 }
234
235 void Attachments::saveAttachment(const string& destFileName,
236         const IAttachmentPtr& attachment)
237 {
238     //  possible move it as a virtual function - to be clarified
239     LoggerE("N/A");
240     //TODO
241 }
242
243 void Attachments::setAttachmentWithExpand(const size_t index,
244         const std::string& fullPath,
245         bool isVirtualPath)
246 {
247     // expand if needed
248     if (index >= m_attachments.size()) {
249         m_attachments.resize(index + 1);    // resize with empty ptr
250     }
251     IAttachmentPtr tmpAttach = AttachmentFactory::createAttachment(
252             fullPath,
253             isVirtualPath);
254     m_attachments[index] = tmpAttach;
255     m_validAttachments = false;
256 }
257
258 void Attachments::reverse()
259 {
260     std::reverse(m_attachments.begin(), m_attachments.end());
261 }
262
263 void Attachments::insertAttachment(const size_t index,
264         const std::string& fullPath,
265         bool isVirtualPath)
266 {
267     IAttachmentPtr tmpAttach = AttachmentFactory::createAttachment(
268             fullPath,
269             isVirtualPath);
270     m_attachments.insert(m_attachments.begin() + index, tmpAttach);
271     m_validAttachments = false;
272 }
273
274 void Attachments::insertAttachmentWithExpand(const size_t index,
275         const std::string& fullPath,
276         bool isVirtualPath)
277 {
278     // expand if needed
279     if (index >= m_attachments.size()) {
280         m_attachments.resize(index);    // resize with empty ptr
281     }
282     IAttachmentPtr tmpAttach = AttachmentFactory::createAttachment(
283             fullPath,
284             isVirtualPath);
285     m_attachments.insert(m_attachments.begin() + index, tmpAttach);
286     m_validAttachments = false;
287 }
288 }
289 }