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.
19 * @file IAttachment.cpp
20 * @author Pawel Misiak (p.misiak@samsung.com)
25 #include <dpl/log/log.h>
26 #include <dpl/errno_string.h>
27 #include <Commons/Exception.h>
28 #include "IAttachment.h"
35 using namespace WrtDeviceApis::Commons;
38 //--------------------------------------------------------------------------
39 namespace WrtDeviceApis {
42 IAttachment::IAttachment() :
43 m_validAttachment(false)
47 void IAttachment::init(const string& fullPath,
53 std::string l_fullPath;
56 LogDebug("translating path");
57 l_fullPath = getRealPath(fullPath);
59 l_fullPath = fullPath;
62 LogDebug("real path " << l_fullPath);
64 char buff[PATH_MAX + 1];
65 if (NULL == realpath(l_fullPath.c_str(), buff)) {
66 std::string errnoString = DPL::GetErrnoString();
67 LogError("get full path problem " << errnoString);
68 ThrowMsg(InvalidArgumentException,
69 "get full path problem " << errnoString);
71 if (-1 == lstat(buff, &buffer)) {
72 LogError("Attachment file not exist");
73 ThrowMsg(InvalidArgumentException, "Attachment file not exist");
75 if (!S_ISREG(buffer.st_mode)) {
76 LogError("Attachment file not exist");
77 ThrowMsg(InvalidArgumentException, "Attachment file not exist");
79 m_fileSize = buffer.st_size;
80 m_attachFullPath = l_fullPath;
82 m_isCreatedProperly = true;
85 Catch(Commons::Exception) {
86 LogError("attachment not created properly");
87 m_isCreatedProperly = false;
91 IAttachment::~IAttachment()
96 string IAttachment::getShortName() const
98 return m_attachShortName;
101 string IAttachment::getFullPath() const
103 return m_attachFullPath;
106 unsigned int IAttachment::getFileSize() const
111 std::string IAttachment::getMimeType() const
116 void IAttachment::setMimeType(const std::string &mimeType)
118 m_mimeType = mimeType;
121 bool IAttachment::getIsCreatedProperly() const
123 return m_isCreatedProperly;
126 void IAttachment::rename(const string& newName)
128 // path for attachment is still not changed
129 m_attachShortName = newName;
130 m_validAttachment = false;
133 void IAttachment::makeShortName()
136 // find position of last occurence of / sign (get only file name from all path
137 pos = m_attachFullPath.find_last_of("/");
138 if ((pos + 1) >= m_attachFullPath.size()) {
139 LogError("Problem with short name creation");
140 Throw(InvalidArgumentException);
142 m_attachShortName = m_attachFullPath.substr(pos + 1);