Update change log and spec for wrt-plugins-tizen_0.4.70
[framework/web/wrt-plugins-tizen.git] / src / Messaging / IAttachment.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 <dpl/errno_string.h>
20 #include <Commons/Exception.h>
21 #include "IAttachment.h"
22
23 extern "C" {
24 #include <sys/stat.h>
25 #include <stdlib.h>
26 }
27
28 #include <Logger.h>
29
30 using namespace WrtDeviceApis::Commons;
31 using namespace std;
32
33 //--------------------------------------------------------------------------
34 namespace DeviceAPI {
35 namespace Messaging {
36
37 IAttachment::IAttachment() :
38     m_validAttachment(false),
39     m_hasDummyFile(false),
40     m_attachmentID(-1)
41 {
42 }
43
44 void IAttachment::init(const string& fullPath,
45         bool isVirtualPath)
46 {
47         Try
48         {
49                 struct stat buffer;
50                 std::string l_fullPath;
51                 LoggerD("FULL Path : " << fullPath);
52
53                 if (isVirtualPath) {
54                         LoggerD("translating path");
55                         l_fullPath = getRealPath(fullPath);
56                 } else {
57                         l_fullPath = fullPath;
58                 }
59                 LoggerD("real path " << l_fullPath);
60
61                 char buff[PATH_MAX + 1];
62                 if ((NULL == realpath(l_fullPath.c_str(), buff)) || (l_fullPath.size() > PATH_MAX + 1)) {
63                         std::string errnoString = DPL::GetErrnoString();
64                         LoggerE("get full path problem " << errnoString);
65                         m_fileSize = 0;
66                         m_attachFullPath = "";
67                         m_attachShortName = "";
68                         m_isCreatedProperly = true;
69                         m_validAttachment = false;
70                 }
71                 else
72                 {
73                         if (-1 == lstat(buff, &buffer)) {
74                                 LoggerE("Attachment file not exist");
75                                 m_validAttachment = false;
76                         }
77
78                         if (!S_ISREG(buffer.st_mode)) {
79                                 LoggerE("Attachment file not exist");
80                                 m_validAttachment = false;
81                         }
82                         m_fileSize = buffer.st_size;
83                         m_attachFullPath = l_fullPath;
84                         makeShortName();
85                         m_isCreatedProperly = true;
86                         m_validAttachment = true;                       
87                 }
88         }
89         Catch(WrtDeviceApis::Commons::Exception) {
90                 LoggerE("attachment not created properly");
91                 m_isCreatedProperly = false;
92         }
93 }
94
95 IAttachment::~IAttachment()
96 {
97     LoggerD("enter");
98 }
99
100 string IAttachment::getShortName() const
101 {
102     return m_attachShortName;
103 }
104
105 string IAttachment::getFullPath() const
106 {
107     return m_attachFullPath;
108 }
109
110 unsigned int IAttachment::getFileSize() const
111 {
112     return m_fileSize;
113 }
114
115 std::string IAttachment::getMimeType() const
116 {
117     return m_mimeType;
118 }
119
120 void IAttachment::setMimeType(const std::string &mimeType)
121 {
122     m_mimeType = mimeType;
123 }
124
125 void IAttachment::setDummyFilePath(const std::string& dummyFilePath)
126 {
127    m_dummyFilePath = dummyFilePath;
128
129    char buff[PATH_MAX + 1];
130    if ( NULL != realpath(m_dummyFilePath.c_str(), buff))
131    {
132         LoggerD("dummy File is valided " << m_dummyFilePath);
133         m_hasDummyFile = true;
134    }
135    else
136    {
137         std::string errnoString = DPL::GetErrnoString();
138         LoggerE("get full path problem " << errnoString);
139         m_hasDummyFile = false;
140    }
141    
142 }
143
144 std::string IAttachment::getDummyFilePath() const
145 {
146         return m_dummyFilePath;
147 }
148
149 bool IAttachment::hasDummyFile() const
150 {
151         return m_hasDummyFile;
152 }
153
154
155 #if 0
156 //for security
157 void IAttachment::setVirtualPath(const std::string& virtualPath)
158 {
159         m_virtualPath = m_virtualPath;
160 }
161
162 std::string IAttachment::getVirtualPath()
163 {
164         return m_virtualPath;
165 }
166 #endif
167
168 IMessagePtr IAttachment::getMessage() const
169 {
170         return m_message;
171 }
172
173 void IAttachment::setMessage(const IMessagePtr& message)
174 {
175         m_message = message;
176 }
177
178 int IAttachment::getAttachmentID() const
179 {
180         return m_attachmentID;
181 }
182
183 void IAttachment::setAttachmentID(int id)
184 {
185         m_attachmentID = id;
186 }
187
188 bool IAttachment::getDownloaded() const
189 {
190         return m_isDownloaded;
191 }
192
193 void IAttachment::setDownloaded(bool downloaded)
194 {
195         m_isDownloaded = downloaded;
196 }
197
198 bool IAttachment::getIsInlineAttachment() const
199 {
200         return m_isInlineAttachment;
201 }
202
203 void IAttachment::setIsInlineAttachment(bool isInlineAttachment)
204 {
205         m_isInlineAttachment = isInlineAttachment;
206 }
207
208
209 bool IAttachment::getIsCreatedProperly() const
210 {
211     return m_isCreatedProperly;
212 }
213
214 bool IAttachment::getIsValidAttachment() const
215 {
216         LoggerE("m_validAttachment : " << m_validAttachment);
217         return m_validAttachment;
218 }
219
220 void IAttachment::rename(const string& newName)
221 {
222     // path for attachment is still not changed
223     m_attachShortName = newName;
224     m_validAttachment = false;
225 }
226
227 void IAttachment::makeShortName()
228 {
229     size_t pos;
230     // find position of last occurence of / sign (get only file name from all path
231     pos = m_attachFullPath.find_last_of("/");
232     if ((pos + 1) >= m_attachFullPath.size()) {
233         LoggerE("Problem with short name creation");
234         Throw(InvalidArgumentException);
235     }
236     m_attachShortName = m_attachFullPath.substr(pos + 1);
237 }
238 }
239 }