6a1d7f318dcc544aef6102e556536cc1e257a934
[framework/web/wrt-plugins-common.git] / src / modules / 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  * @file       IAttachment.cpp
20  * @author     Pawel Misiak (p.misiak@samsung.com)
21  * @version    0.1
22  * @brief
23  */
24 #include <algorithm>
25 #include <dpl/log/log.h>
26 #include <dpl/errno_string.h>
27 #include <Commons/Exception.h>
28 #include "IAttachment.h"
29
30 extern "C" {
31 #include <sys/stat.h>
32 #include <stdlib.h>
33 }
34
35 using namespace WrtDeviceApis::Commons;
36 using namespace std;
37
38 //--------------------------------------------------------------------------
39 namespace WrtDeviceApis {
40 namespace Messaging {
41 namespace Api {
42 IAttachment::IAttachment() :
43     m_validAttachment(false)
44 {
45 }
46
47 void IAttachment::init(const string& fullPath,
48         bool isVirtualPath)
49 {
50     Try
51     {
52         struct stat buffer;
53         std::string l_fullPath;
54
55         if (isVirtualPath) {
56             LogDebug("translating path");
57             l_fullPath = getRealPath(fullPath);
58         } else {
59             l_fullPath = fullPath;
60         }
61
62         LogDebug("real path " << l_fullPath);
63
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);
70         }
71         if (-1 == lstat(buff, &buffer)) {
72             LogError("Attachment file not exist");
73             ThrowMsg(InvalidArgumentException, "Attachment file not exist");
74         }
75         if (!S_ISREG(buffer.st_mode)) {
76             LogError("Attachment file not exist");
77             ThrowMsg(InvalidArgumentException, "Attachment file not exist");
78         }
79         m_fileSize = buffer.st_size;
80         m_attachFullPath = l_fullPath;
81         makeShortName();
82         m_isCreatedProperly = true;
83     }
84
85     Catch(Commons::Exception) {
86         LogError("attachment not created properly");
87         m_isCreatedProperly = false;
88     }
89 }
90
91 IAttachment::~IAttachment()
92 {
93     LogDebug("enter");
94 }
95
96 string IAttachment::getShortName() const
97 {
98     return m_attachShortName;
99 }
100
101 string IAttachment::getFullPath() const
102 {
103     return m_attachFullPath;
104 }
105
106 unsigned int IAttachment::getFileSize() const
107 {
108     return m_fileSize;
109 }
110
111 std::string IAttachment::getMimeType() const
112 {
113     return m_mimeType;
114 }
115
116 void IAttachment::setMimeType(const std::string &mimeType)
117 {
118     m_mimeType = mimeType;
119 }
120
121 bool IAttachment::getIsCreatedProperly() const
122 {
123     return m_isCreatedProperly;
124 }
125
126 void IAttachment::rename(const string& newName)
127 {
128     // path for attachment is still not changed
129     m_attachShortName = newName;
130     m_validAttachment = false;
131 }
132
133 void IAttachment::makeShortName()
134 {
135     size_t pos;
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);
141     }
142     m_attachShortName = m_attachFullPath.substr(pos + 1);
143 }
144
145 }
146 }
147 }