Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / platform / API / Messaging / IAttachment.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       IAttachment.cpp
22  * @author     Pawel Misiak (p.misiak@samsung.com)
23  * @version    0.1
24  * @brief
25  */
26 #include <algorithm>
27 #include <dpl/log/log.h>
28 #include <dpl/errno_string.h>
29 #include <Commons/Exception.h>
30 #include "IAttachment.h"
31
32 extern "C" {
33 #include <sys/stat.h>
34 #include <stdlib.h>
35 }
36
37 using namespace WrtDeviceApis::Commons;
38 using namespace std;
39
40 //--------------------------------------------------------------------------
41 namespace TizenApis {
42 namespace Api {
43 namespace Messaging {
44
45 IAttachment::IAttachment() :
46     m_validAttachment(false),
47     m_attachmentID(-1)
48 {
49 }
50
51 void IAttachment::init(const string& fullPath,
52         bool isVirtualPath)
53 {
54     Try
55     {
56         struct stat buffer;
57         std::string l_fullPath;
58         LogDebug("FULL Path : " << fullPath);
59
60         if (isVirtualPath) {
61             LogDebug("translating path");
62             l_fullPath = getRealPath(fullPath);
63         } else {
64             l_fullPath = fullPath;
65         }
66         LogDebug("real path " << l_fullPath);
67
68           if ( l_fullPath.size() > PATH_MAX + 1)
69           {
70                 ThrowMsg(InvalidArgumentException, "Attachment file name is too long");
71           }
72         
73         char buff[PATH_MAX + 1];
74         if (NULL == realpath(l_fullPath.c_str(), buff)) {
75             std::string errnoString = DPL::GetErrnoString();
76             LogError("get full path problem " << errnoString);
77             ThrowMsg(InvalidArgumentException,
78                      "get full path problem " << errnoString);
79         }
80            
81         if (-1 == lstat(buff, &buffer)) {
82             LogError("Attachment file not exist");
83             ThrowMsg(InvalidArgumentException, "Attachment file not exist");
84         }
85                 
86         if (!S_ISREG(buffer.st_mode)) {
87             LogError("Attachment file not exist");
88             ThrowMsg(InvalidArgumentException, "Attachment file not exist");
89         }
90         m_fileSize = buffer.st_size;
91         m_attachFullPath = l_fullPath;
92         makeShortName();
93         m_isCreatedProperly = true;
94
95     }
96
97     Catch(WrtDeviceApis::Commons::Exception) {
98         LogError("attachment not created properly");
99         m_isCreatedProperly = false;
100     }
101 }
102
103 IAttachment::~IAttachment()
104 {
105     LogDebug("enter");
106 }
107
108 string IAttachment::getShortName() const
109 {
110     return m_attachShortName;
111 }
112
113 string IAttachment::getFullPath() const
114 {
115     return m_attachFullPath;
116 }
117
118 unsigned int IAttachment::getFileSize() const
119 {
120     return m_fileSize;
121 }
122
123 std::string IAttachment::getMimeType() const
124 {
125     return m_mimeType;
126 }
127
128 void IAttachment::setMimeType(const std::string &mimeType)
129 {
130     m_mimeType = mimeType;
131 }
132
133 Api::Messaging::IMessagePtr IAttachment::getMessage() const
134 {
135         return m_message;
136 }
137
138 void IAttachment::setMessage(const Api::Messaging::IMessagePtr& message)
139 {
140         m_message = message;
141 }
142
143 int IAttachment::getAttachmentID() const
144 {
145         return m_attachmentID;
146 }
147
148 void IAttachment::setAttachmentID(int id)
149 {
150         m_attachmentID = id;
151 }
152
153 bool IAttachment::getDownloaded() const
154 {
155         return m_isDownloaded;
156 }
157
158 void IAttachment::setDownloaded(bool downloaded)
159 {
160         m_isDownloaded = downloaded;
161 }
162
163 bool IAttachment::getIsCreatedProperly() const
164 {
165     return m_isCreatedProperly;
166 }
167
168 void IAttachment::rename(const string& newName)
169 {
170     // path for attachment is still not changed
171     m_attachShortName = newName;
172     m_validAttachment = false;
173 }
174
175 void IAttachment::makeShortName()
176 {
177     size_t pos;
178     // find position of last occurence of / sign (get only file name from all path
179     pos = m_attachFullPath.find_last_of("/");
180     if ((pos + 1) >= m_attachFullPath.size()) {
181         LogError("Problem with short name creation");
182         Throw(InvalidArgumentException);
183     }
184     m_attachShortName = m_attachFullPath.substr(pos + 1);
185 }
186 }
187 }
188 }