2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * @file IAttachment.cpp
22 * @author Pawel Misiak (p.misiak@samsung.com)
27 #include <dpl/log/log.h>
28 #include <dpl/errno_string.h>
29 #include <Commons/Exception.h>
30 #include "IAttachment.h"
37 using namespace WrtDeviceApis::Commons;
40 //--------------------------------------------------------------------------
45 IAttachment::IAttachment() :
46 m_validAttachment(false),
51 void IAttachment::init(const string& fullPath,
57 std::string l_fullPath;
58 LogDebug("FULL Path : " << fullPath);
61 LogDebug("translating path");
62 l_fullPath = getRealPath(fullPath);
64 l_fullPath = fullPath;
66 LogDebug("real path " << l_fullPath);
68 if ( l_fullPath.size() > PATH_MAX + 1)
70 ThrowMsg(InvalidArgumentException, "Attachment file name is too long");
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);
81 if (-1 == lstat(buff, &buffer)) {
82 LogError("Attachment file not exist");
83 ThrowMsg(InvalidArgumentException, "Attachment file not exist");
86 if (!S_ISREG(buffer.st_mode)) {
87 LogError("Attachment file not exist");
88 ThrowMsg(InvalidArgumentException, "Attachment file not exist");
90 m_fileSize = buffer.st_size;
91 m_attachFullPath = l_fullPath;
93 m_isCreatedProperly = true;
97 Catch(WrtDeviceApis::Commons::Exception) {
98 LogError("attachment not created properly");
99 m_isCreatedProperly = false;
103 IAttachment::~IAttachment()
108 string IAttachment::getShortName() const
110 return m_attachShortName;
113 string IAttachment::getFullPath() const
115 return m_attachFullPath;
118 unsigned int IAttachment::getFileSize() const
123 std::string IAttachment::getMimeType() const
128 void IAttachment::setMimeType(const std::string &mimeType)
130 m_mimeType = mimeType;
133 Api::Messaging::IMessagePtr IAttachment::getMessage() const
138 void IAttachment::setMessage(const Api::Messaging::IMessagePtr& message)
143 int IAttachment::getAttachmentID() const
145 return m_attachmentID;
148 void IAttachment::setAttachmentID(int id)
153 bool IAttachment::getDownloaded() const
155 return m_isDownloaded;
158 void IAttachment::setDownloaded(bool downloaded)
160 m_isDownloaded = downloaded;
163 bool IAttachment::getIsCreatedProperly() const
165 return m_isCreatedProperly;
168 void IAttachment::rename(const string& newName)
170 // path for attachment is still not changed
171 m_attachShortName = newName;
172 m_validAttachment = false;
175 void IAttachment::makeShortName()
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);
184 m_attachShortName = m_attachFullPath.substr(pos + 1);