2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <Commons/Exception.h>
20 #include "Attachments.h"
21 #include "AttachmentFactory.h"
29 using namespace WrtDeviceApis::Commons;
32 //--------------------------------------------------------------------------
36 Attachments::Attachments() :
37 m_validAttachments(false)
42 Attachments::~Attachments()
47 IAttachmentPtr Attachments::appendAttachment(const std::string& fullPath,
50 LoggerD("enter, fullPath=" << fullPath);
51 IAttachmentPtr tmpAttach = AttachmentFactory::createAttachment(
54 m_validAttachments = false;
55 m_attachments.push_back(tmpAttach);
59 void Attachments::appendAttachment(const IAttachmentPtr& attachment)
62 m_validAttachments = false;
63 m_attachments.push_back(attachment);
66 void Attachments::appendInlineAttachment(const IAttachmentPtr& attachment)
69 m_validInineAttachments = false;
70 m_inlineAttachments.push_back(attachment);
73 IAttachmentPtr Attachments::appendAttachment(const IMessagePtr& message, const std::string& fullPath,
77 IAttachmentPtr iAttachment = appendAttachment(fullPath, isVirtualPath);
79 if (message && iAttachment)
81 iAttachment->setMessage(message);
87 void Attachments::appendAttachment(const IMessagePtr& message, const IAttachmentPtr& attachment)
91 m_validAttachments = false;
93 if (attachment && message)
94 attachment->setMessage(message);
96 m_attachments.push_back(attachment);
99 void Attachments::appendAttachments(const vector<IAttachmentPtr>& attachments)
102 back_insert_iterator< vector<IAttachmentPtr> >biit(m_attachments);
103 copy(attachments.begin(), attachments.end(), biit);
106 size_t Attachments::getAttachmentsCount() const
109 return m_attachments.size();
112 size_t Attachments::getInlineAttachmentsCount() const
115 return m_inlineAttachments.size();
118 IAttachmentPtr Attachments::getAttachment(const size_t index) const
121 if (index >= m_attachments.size()) {
122 ThrowMsg(OutOfRangeException, "Trying to get attachment out of range");
124 return m_attachments[index];
127 IAttachmentPtr Attachments::getInlineAttachment(const size_t index) const
130 if (index >= m_inlineAttachments.size()) {
131 ThrowMsg(OutOfRangeException, "Trying to get attachment out of range");
133 return m_inlineAttachments[index];
136 void Attachments::removeAttachment(const size_t index)
139 if (index >= m_attachments.size()) {
140 ThrowMsg(OutOfRangeException, "Trying to get attachment out of range");
142 m_validAttachments = false;
143 m_attachments.erase(m_attachments.begin() + index);
146 void Attachments::removeAttachment(const IAttachmentPtr& attachment)
149 remove(m_attachments.begin(), m_attachments.end(), attachment),
150 m_attachments.end());
153 vector<IAttachmentPtr> Attachments::getAttachments() const
156 return m_attachments;
159 vector<IAttachmentPtr> Attachments::getInlineAttachments() const
162 return m_inlineAttachments;
165 vector<string> Attachments::getAttachmentsFullPaths() const
167 vector<string> retVal;
169 for (size_t i = 0; i < m_attachments.size(); i++) {
170 retVal.push_back(m_attachments[i]->getFullPath());
175 vector<string> Attachments::getAttachmentsShortNames() const
177 vector<string> retVal;
179 for (size_t i = 0; i < m_attachments.size(); i++) {
180 retVal.push_back(m_attachments[i]->getShortName());
185 void Attachments::setAttachments(const vector<string>& attachments,
188 vector<string>::const_iterator it = attachments.begin();
190 m_attachments.clear();
191 while (it != attachments.end()) {
192 appendAttachment(*it, isVirtualPath);
197 void Attachments::setAttachments(const vector<IAttachmentPtr>& attachments)
199 m_attachments = attachments;
200 m_validAttachments = false;
203 void Attachments::setInlineAttachments(const vector<IAttachmentPtr>& attachments)
205 m_inlineAttachments = attachments;
206 m_validInineAttachments = false;
209 bool Attachments::isAttachmentsValid() const
211 LoggerD("m_validAttachments : " << m_validAttachments);
212 return m_validAttachments;
215 void Attachments::setAttachmentsValidity(bool state)
217 m_validAttachments = state;
220 bool Attachments::isInlineAttachmentsValid() const
222 return m_validInineAttachments;
225 void Attachments::setInlineAttachmentsValidity(bool state)
227 m_validInineAttachments = state;
230 const vector<IAttachmentPtr>& Attachments::getAttachmentsRef() const
232 return m_attachments;
235 void Attachments::saveAttachment(const string& destFileName,
236 const IAttachmentPtr& attachment)
238 // possible move it as a virtual function - to be clarified
243 void Attachments::setAttachmentWithExpand(const size_t index,
244 const std::string& fullPath,
248 if (index >= m_attachments.size()) {
249 m_attachments.resize(index + 1); // resize with empty ptr
251 IAttachmentPtr tmpAttach = AttachmentFactory::createAttachment(
254 m_attachments[index] = tmpAttach;
255 m_validAttachments = false;
258 void Attachments::reverse()
260 std::reverse(m_attachments.begin(), m_attachments.end());
263 void Attachments::insertAttachment(const size_t index,
264 const std::string& fullPath,
267 IAttachmentPtr tmpAttach = AttachmentFactory::createAttachment(
270 m_attachments.insert(m_attachments.begin() + index, tmpAttach);
271 m_validAttachments = false;
274 void Attachments::insertAttachmentWithExpand(const size_t index,
275 const std::string& fullPath,
279 if (index >= m_attachments.size()) {
280 m_attachments.resize(index); // resize with empty ptr
282 IAttachmentPtr tmpAttach = AttachmentFactory::createAttachment(
285 m_attachments.insert(m_attachments.begin() + index, tmpAttach);
286 m_validAttachments = false;