tizen beta release
[framework/web/wrt-plugins-common.git] / src / modules / API / Messaging / Attachments.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  * @file       IAttachment.cpp
20  * @author     Pawel Misiak (p.misiak@samsung.com)
21  * @version    0.1
22  * @brief
23  */
24 #include <algorithm>
25 #include <dpl/log/log.h>
26 #include <Commons/Exception.h>
27 #include "Attachments.h"
28 #include "AttachmentFactory.h"
29
30 extern "C" {
31 #include <sys/stat.h>
32 #include <stdlib.h>
33 }
34
35 using namespace WrtDeviceApis::Commons;
36 using namespace std;
37
38 //--------------------------------------------------------------------------
39 namespace WrtDeviceApis {
40 namespace Messaging{
41 namespace Api {
42
43 Attachments::Attachments() :
44     m_validAttachments(false)
45 {
46     LogDebug("enter");
47 }
48
49 Attachments::~Attachments()
50 {
51     LogDebug("enter");
52 }
53
54 IAttachmentPtr Attachments::appendAttachment(const std::string& fullPath,
55         bool isVirtualPath)
56 {
57     LogDebug("enter, fullPath=" << fullPath);
58     IAttachmentPtr tmpAttach = AttachmentFactory::createAttachment(
59             fullPath,
60             isVirtualPath);
61     m_validAttachments = false;
62     m_attachments.push_back(tmpAttach);
63     return tmpAttach;
64 }
65
66 void Attachments::appendAttachment(const IAttachmentPtr& attachment)
67 {
68     LogDebug("enter");
69     m_validAttachments = false;
70     m_attachments.push_back(attachment);
71 }
72
73 void Attachments::appendAttachments(const vector<IAttachmentPtr>& attachments)
74 {
75     LogDebug("enter");
76     back_insert_iterator< vector<IAttachmentPtr> >biit(m_attachments);
77     copy(attachments.begin(), attachments.end(), biit);
78 }
79
80 size_t Attachments::getAttachmentsCount() const
81 {
82     LogDebug("enter");
83     return m_attachments.size();
84 }
85
86 IAttachmentPtr Attachments::getAttachment(const size_t index) const
87 {
88     LogDebug("enter");
89     if (index >= m_attachments.size()) {
90         ThrowMsg(OutOfRangeException, "Trying to get attachment out of range");
91     }
92     return m_attachments[index];
93 }
94
95 void Attachments::removeAttachment(const size_t index)
96 {
97     LogDebug("enter");
98     if (index >= m_attachments.size()) {
99         ThrowMsg(OutOfRangeException, "Trying to get attachment out of range");
100     }
101     m_validAttachments = false;
102     m_attachments.erase(m_attachments.begin() + index);
103 }
104
105 void Attachments::removeAttachment(const IAttachmentPtr& attachment)
106 {
107     m_attachments.erase(
108         remove(m_attachments.begin(), m_attachments.end(), attachment),
109         m_attachments.end());
110 }
111
112 vector<IAttachmentPtr> Attachments::getAttachments() const
113 {
114     LogDebug("enter");
115     return m_attachments;
116 }
117
118 vector<string> Attachments::getAttachmentsFullPaths() const
119 {
120     vector<string> retVal;
121
122     for (size_t i = 0; i < m_attachments.size(); i++) {
123         retVal.push_back(m_attachments[i]->getFullPath());
124     }
125     return retVal;
126 }
127
128 vector<string> Attachments::getAttachmentsShortNames() const
129 {
130     vector<string> retVal;
131
132     for (size_t i = 0; i < m_attachments.size(); i++) {
133         retVal.push_back(m_attachments[i]->getShortName());
134     }
135     return retVal;
136 }
137
138 void Attachments::setAttachments(const vector<string>& attachments,
139         bool isVirtualPath)
140 {
141     vector<string>::const_iterator it = attachments.begin();
142
143     m_attachments.clear();
144     while (it != attachments.end()) {
145         appendAttachment(*it, isVirtualPath);
146         ++it;
147     }
148 }
149
150 void Attachments::setAttachments(const vector<IAttachmentPtr>& attachments)
151 {
152     m_attachments = attachments;
153     m_validAttachments = false;
154 }
155
156 bool Attachments::isAttachmentsValid() const
157 {
158     return m_validAttachments;
159 }
160
161 void Attachments::setAttachmentsValidity(bool state)
162 {
163     m_validAttachments = state;
164 }
165
166 const vector<IAttachmentPtr>& Attachments::getAttachmentsRef() const
167 {
168     return m_attachments;
169 }
170
171 void Attachments::saveAttachment(const string& /*destFileName*/,
172                                  const IAttachmentPtr& /*attachment*/)
173 {
174     //  possible move it as a virtual function - to be clarified
175     LogError("N/A");
176     //TODO
177 }
178
179 void Attachments::setAttachmentWithExpand(const size_t index,
180         const std::string& fullPath,
181         bool isVirtualPath)
182 {
183     // expand if needed
184     if (index >= m_attachments.size()) {
185         m_attachments.resize(index + 1);    // resize with empty ptr
186     }
187     IAttachmentPtr tmpAttach = AttachmentFactory::createAttachment(
188             fullPath,
189             isVirtualPath);
190     m_attachments[index] = tmpAttach;
191     m_validAttachments = false;
192 }
193
194 void Attachments::reverse()
195 {
196     std::reverse(m_attachments.begin(), m_attachments.end());
197 }
198
199 void Attachments::insertAttachment(const size_t index,
200         const std::string& fullPath,
201         bool isVirtualPath)
202 {
203     IAttachmentPtr tmpAttach = AttachmentFactory::createAttachment(
204             fullPath,
205             isVirtualPath);
206     m_attachments.insert(m_attachments.begin() + index, tmpAttach);
207     m_validAttachments = false;
208 }
209
210 void Attachments::insertAttachmentWithExpand(const size_t index,
211         const std::string& fullPath,
212         bool isVirtualPath)
213 {
214     // expand if needed
215     if (index >= m_attachments.size()) {
216         m_attachments.resize(index);    // resize with empty ptr
217     }
218     IAttachmentPtr tmpAttach = AttachmentFactory::createAttachment(
219             fullPath,
220             isVirtualPath);
221     m_attachments.insert(m_attachments.begin() + index, tmpAttach);
222     m_validAttachments = false;
223 }
224
225 }
226 }
227 }
228