summaryrefslogtreecommitdiff
path: root/mobile_src/Messaging/IAttachment.cpp
blob: 20028b654ac17ef8ecc741ace4360fe4d3d06292 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//
// Tizen Web Device API
// Copyright (c) 2012 Samsung Electronics Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the License);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#include <algorithm>
#include <dpl/errno_string.h>
#include <Commons/Exception.h>
#include "IAttachment.h"

extern "C" {
#include <sys/stat.h>
#include <stdlib.h>
}

#include <Logger.h>

using namespace WrtDeviceApis::Commons;
using namespace std;

//--------------------------------------------------------------------------
namespace DeviceAPI {
namespace Messaging {

IAttachment::IAttachment() :
    m_validAttachment(false),
    m_hasDummyFile(false),
    m_attachmentID(-1)
{
}

void IAttachment::init(const string& fullPath,
        bool isVirtualPath)
{
	Try
	{
		struct stat buffer;
		std::string l_fullPath;
		LoggerD("FULL Path : " << fullPath);

		if (isVirtualPath) {
			LoggerD("translating path");
			l_fullPath = getRealPath(fullPath);
		} else {
			l_fullPath = fullPath;
		}
		LoggerD("real path " << l_fullPath);

		char buff[PATH_MAX + 1];
		if ((NULL == realpath(l_fullPath.c_str(), buff)) || (l_fullPath.size() > PATH_MAX + 1)) {
			std::string errnoString = DPL::GetErrnoString();
			LoggerE("get full path problem " << errnoString);
			m_fileSize = 0;
			m_attachFullPath = "";
			m_attachShortName = "";
			m_isCreatedProperly = true;
			m_validAttachment = false;
		}
		else
		{
			if (-1 == lstat(buff, &buffer)) {
				LoggerE("Attachment file not exist");
				m_validAttachment = false;
			}

			if (!S_ISREG(buffer.st_mode)) {
				LoggerE("Attachment file not exist");
				m_validAttachment = false;
			}
			m_fileSize = buffer.st_size;
			m_attachFullPath = l_fullPath;
			makeShortName();
			m_isCreatedProperly = true;
			m_validAttachment = true;			
		}
	}
	Catch(WrtDeviceApis::Commons::Exception) {
		LoggerE("attachment not created properly");
		m_isCreatedProperly = false;
	}
}

IAttachment::~IAttachment()
{
    LoggerD("enter");
}

string IAttachment::getShortName() const
{
    return m_attachShortName;
}

string IAttachment::getFullPath() const
{
    return m_attachFullPath;
}

unsigned int IAttachment::getFileSize() const
{
    return m_fileSize;
}

std::string IAttachment::getMimeType() const
{
    return m_mimeType;
}

void IAttachment::setMimeType(const std::string &mimeType)
{
    m_mimeType = mimeType;
}

void IAttachment::setDummyFilePath(const std::string& dummyFilePath)
{
   m_dummyFilePath = dummyFilePath;

   char buff[PATH_MAX + 1];
   if ( NULL != realpath(m_dummyFilePath.c_str(), buff))
   {
	LoggerD("dummy File is valided " << m_dummyFilePath);
	m_hasDummyFile = true;
   }
   else
   {
        std::string errnoString = DPL::GetErrnoString();
        LoggerE("get full path problem " << errnoString);
	m_hasDummyFile = false;
   }
   
}

std::string IAttachment::getDummyFilePath() const
{
	return m_dummyFilePath;
}

bool IAttachment::hasDummyFile() const
{
	return m_hasDummyFile;
}


#if 0
//for security
void IAttachment::setVirtualPath(const std::string& virtualPath)
{
	m_virtualPath = m_virtualPath;
}

std::string IAttachment::getVirtualPath()
{
	return m_virtualPath;
}
#endif

IMessagePtr IAttachment::getMessage() const
{
	return m_message;
}

void IAttachment::setMessage(const IMessagePtr& message)
{
	m_message = message;
}

int IAttachment::getAttachmentID() const
{
	return m_attachmentID;
}

void IAttachment::setAttachmentID(int id)
{
	m_attachmentID = id;
}

bool IAttachment::getDownloaded() const
{
	return m_isDownloaded;
}

void IAttachment::setDownloaded(bool downloaded)
{
	m_isDownloaded = downloaded;
}

bool IAttachment::getIsInlineAttachment() const
{
	return m_isInlineAttachment;
}

void IAttachment::setIsInlineAttachment(bool isInlineAttachment)
{
	m_isInlineAttachment = isInlineAttachment;
}


bool IAttachment::getIsCreatedProperly() const
{
    return m_isCreatedProperly;
}

bool IAttachment::getIsValidAttachment() const
{
	LoggerE("m_validAttachment : " << m_validAttachment);
	return m_validAttachment;
}

void IAttachment::rename(const string& newName)
{
    // path for attachment is still not changed
    m_attachShortName = newName;
    m_validAttachment = false;
}

void IAttachment::makeShortName()
{
    size_t pos;
    // find position of last occurence of / sign (get only file name from all path
    pos = m_attachFullPath.find_last_of("/");
    if ((pos + 1) >= m_attachFullPath.size()) {
        LoggerE("Problem with short name creation");
        Throw(InvalidArgumentException);
    }
    m_attachShortName = m_attachFullPath.substr(pos + 1);
}
}
}