6df400bb6089d81b0fd1b73bcbf162a9d51a06ab
[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 {
48 }
49
50 void IAttachment::init(const string& fullPath,
51         bool isVirtualPath)
52 {
53     Try
54     {
55         struct stat buffer;
56         std::string l_fullPath;
57         LogDebug("FULL Path : " << fullPath);
58
59         if (isVirtualPath) {
60             LogDebug("translating path");
61             l_fullPath = getRealPath(fullPath);
62         } else {
63             l_fullPath = fullPath;
64         }
65         LogDebug("real path " << l_fullPath);
66
67         char buff[PATH_MAX + 1];
68         if (NULL == realpath(l_fullPath.c_str(), buff)) {
69             std::string errnoString = DPL::GetErrnoString();
70             LogError("get full path problem " << errnoString);
71             ThrowMsg(InvalidArgumentException,
72                      "get full path problem " << errnoString);
73         }
74            
75         if (-1 == lstat(buff, &buffer)) {
76             LogError("Attachment file not exist");
77             ThrowMsg(InvalidArgumentException, "Attachment file not exist");
78         }
79                 
80         if (!S_ISREG(buffer.st_mode)) {
81             LogError("Attachment file not exist");
82             ThrowMsg(InvalidArgumentException, "Attachment file not exist");
83         }
84         m_fileSize = buffer.st_size;
85         m_attachFullPath = l_fullPath;
86         makeShortName();
87         m_isCreatedProperly = true;
88
89     }
90
91     Catch(WrtDeviceApis::Commons::Exception) {
92         LogError("attachment not created properly");
93         m_isCreatedProperly = false;
94     }
95 }
96
97 IAttachment::~IAttachment()
98 {
99     LogDebug("enter");
100 }
101
102 string IAttachment::getShortName() const
103 {
104     return m_attachShortName;
105 }
106
107 string IAttachment::getFullPath() const
108 {
109     return m_attachFullPath;
110 }
111
112 unsigned int IAttachment::getFileSize() const
113 {
114     return m_fileSize;
115 }
116
117 std::string IAttachment::getMimeType() const
118 {
119     return m_mimeType;
120 }
121
122 void IAttachment::setMimeType(const std::string &mimeType)
123 {
124     m_mimeType = mimeType;
125 }
126
127 Api::Messaging::IMessagePtr IAttachment::getMessage() const
128 {
129         return m_message;
130 }
131
132 void IAttachment::setMessage(const Api::Messaging::IMessagePtr& message)
133 {
134         m_message = message;
135 }
136
137 int IAttachment::getAttachmentID() const
138 {
139         return m_attachmentID;
140 }
141
142 void IAttachment::setAttachmentID(int id)
143 {
144         m_attachmentID = id;
145 }
146
147 bool IAttachment::getDownloaded() const
148 {
149         return m_isDownloaded;
150 }
151
152 void IAttachment::setDownloaded(bool downloaded)
153 {
154         m_isDownloaded = downloaded;
155 }
156
157 bool IAttachment::getIsCreatedProperly() const
158 {
159     return m_isCreatedProperly;
160 }
161
162 void IAttachment::rename(const string& newName)
163 {
164     // path for attachment is still not changed
165     m_attachShortName = newName;
166     m_validAttachment = false;
167 }
168
169 void IAttachment::makeShortName()
170 {
171     size_t pos;
172     // find position of last occurence of / sign (get only file name from all path
173     pos = m_attachFullPath.find_last_of("/");
174     if ((pos + 1) >= m_attachFullPath.size()) {
175         LogError("Problem with short name creation");
176         Throw(InvalidArgumentException);
177     }
178     m_attachShortName = m_attachFullPath.substr(pos + 1);
179 }
180 }
181 }
182 }