[content] Fix a buf on ContentTransfer
[platform/framework/native/content.git] / src / FCntImageContentInfo.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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  * @file                FCntImageContentInfo.cpp
18  * @brief               This is the implementation file for the %ImageContentInfo class.
19  *
20  * This file contains implementation of the %ImageContentInfo class.
21  */
22
23 #include <FBaseSysLog.h>
24 #include <FCntImageContentInfo.h>
25 #include <FIoFile.h>
26 #include <FIoDirectory.h>
27 #include <FSysEnvironment.h>
28 #include <FIo_FileImpl.h>
29 #include <FApp_AppInfo.h>
30
31 using namespace Tizen::Base;
32 using namespace Tizen::Io;
33 using namespace Tizen::Locations;
34 using namespace Tizen::App;
35 using namespace Tizen::System;
36 using namespace std;
37
38 namespace Tizen { namespace Content
39 {
40
41 ImageContentInfo::ImageContentInfo(void)
42         : ContentInfo()
43         , __pImageContentData(null)
44         , __pImpl(null)
45 {
46
47 }
48
49 ImageContentInfo::~ImageContentInfo(void)
50 {
51         if (__pImageContentData != null)
52         {
53                 delete __pImageContentData;
54                 __pImageContentData = null;
55         }
56 }
57
58 /**
59  * E_SUCCESS
60  * E_FILE_NOT_FOUND
61  * E_INVALID_ARG
62  * E_OUT_OF_MEMORY
63  * - E_IO
64  */
65 result
66 ImageContentInfo::Construct(const String& contentPath, const String& thumbnailPath, bool setGps)
67 {
68         ClearLastResult();
69
70         SysAssertf(__pImageContentData == null,
71                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
72
73         result r = E_SUCCESS;
74         _ContentData contentData;
75         int contentLength = 0;
76         FileAttributes attribute;
77
78         // checks parameters
79         contentLength = contentPath.GetLength();
80         SysTryReturnResult(NID_CNT, _FileImpl::IsMediaPath(contentPath), E_INVALID_ARG,
81                         "The contentPath should start with /Media or /Storagecard/Media.");
82         SysTryReturnResult(NID_CNT, File::IsFileExist(contentPath), E_FILE_NOT_FOUND,
83                         "The file corresponding to contentPath could not be found.");
84
85         if (!thumbnailPath.IsEmpty())
86         {
87                 SysLog(NID_CNT,
88                                 "The thumbnailPath is not supported but you can get the thumbnail managed by Tizen from ContentInfo::GetThumbnailN().");
89         }
90
91         if (setGps)
92         {
93                 SysLog(NID_CNT, "The setGps is not supported.");
94         }
95
96         // Sets the content path
97         contentData.contentPath = contentPath;
98
99         // Sets the content type
100         contentData.contentType = CONTENT_TYPE_IMAGE;
101
102         // E_INVALID_ARG, E_OUT_OF_MEMORY
103         r = SetContentData(&contentData);
104         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Construct() failed.");
105
106         _ImageContentData* pImageContentData = new (nothrow) _ImageContentData();
107         SysTryReturnResult(NID_CNT, pImageContentData != null, E_OUT_OF_MEMORY, "Construct() failed.");
108
109         __pImageContentData = pImageContentData;
110
111         return r;
112 }
113
114 /**
115  * E_SUCCESS
116  * E_FILE_NOT_FOUND
117  * E_INVALID_ARG
118  * E_OUT_OF_MEMORY
119  * - E_IO
120  * - E_SYSTEM
121  */
122 result
123 ImageContentInfo::Construct(const String* pContentPath)
124 {
125         ClearLastResult();
126
127         SysAssertf(__pImageContentData == null,
128                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
129
130         result r = E_SUCCESS;
131         _ContentData contentData;
132
133         if (pContentPath != null)
134         {
135                 if (!_AppInfo::IsOspCompat())
136                 {
137                         if (pContentPath->StartsWith(OSP_MEDIA_PHONE, 0) || pContentPath->StartsWith(OSP_MEDIA_MMC, 0))
138                         {
139                                 SysLogException(NID_CNT, E_INVALID_ARG,
140                                                 "[E_INVALID_ARG] /Media/ and /Storagecard/Media/ are not supported from Tizen 2.0.");
141                                 return E_INVALID_ARG;
142                         }
143                         if (!(pContentPath->StartsWith(Environment::GetMediaPath(), 0)
144                                 || pContentPath->StartsWith(Environment::GetExternalStoragePath(), 0)))
145                         {
146                                 SysLogException(NID_CNT, E_INVALID_ARG,
147                                                 "[E_INVALID_ARG] %ls is not supported.", pContentPath->GetPointer());
148                                 return E_INVALID_ARG;
149                         }
150                 }
151                 else
152                 {
153                         // prior to 2.0
154                         if (pContentPath->StartsWith(OSP_MEDIA_PHONE, 0))
155                         {
156                                 // Because the content path is saved like /opt/media or /opt/storage/sdcard/ in SLP database,
157                                 // it should be converted in 2.0.
158                                 r = (const_cast<String*>(pContentPath))->Replace(OSP_MEDIA_PHONE, Environment::GetMediaPath());
159                                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Construct() failed.");
160                         }
161                         else if (pContentPath->StartsWith(OSP_MEDIA_MMC, 0))
162                         {
163                                 r = (const_cast<String*>(pContentPath))->Replace(OSP_MEDIA_MMC, Environment::GetExternalStoragePath());
164                                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Construct() failed.");
165                         }
166                         else
167                         {
168                                 SysLogException(NID_CNT, E_INVALID_ARG,
169                                                 "[E_INVALID_ARG] The contentPath should start with /Media or /Storagecard/Media.");
170                                 return E_INVALID_ARG;
171                         }
172                 }
173
174                 int length = pContentPath->GetLength();
175                 SysTryReturnResult(NID_CNT, length != 0, E_INVALID_ARG,
176                                 "The length of pContentPath is 0.");
177                 SysTryReturnResult(NID_CNT, File::IsFileExist(*pContentPath), E_FILE_NOT_FOUND,
178                                 "The file corresponding to pContentPath could not be found.");
179
180                 // Sets the content path
181                 contentData.contentPath = *pContentPath;
182
183                 // Sets the content type
184                 contentData.contentType = CONTENT_TYPE_IMAGE;
185
186                 r = SetContentData(&contentData);
187                 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Construct() failed.");
188         }
189         else
190         {
191                 contentData.contentType = CONTENT_TYPE_IMAGE;
192
193                 r = SetContentData(&contentData);
194                 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Construct() failed.");
195         }
196
197         _ImageContentData* pImageContentData = new (nothrow) _ImageContentData();
198         SysTryReturnResult(NID_CNT, pImageContentData != null, E_OUT_OF_MEMORY, "Construct() failed.");
199
200         __pImageContentData = pImageContentData;
201
202         return r;
203 }
204
205 result
206 ImageContentInfo::SetImageContentData(const _ImageContentData* pImageContentData)
207 {
208         ClearLastResult();
209
210         SysTryReturnResult(NID_CNT, pImageContentData != null, E_INVALID_ARG, "pImageContentData is null.");
211
212         // _ContentManagerImpl::GetContentInfoN makes an instance of this class. at that time it does not call Construct().
213         // Because __pImageContentData is created in Construct(), If there is no codes for allocation, crash will occur.
214         if (__pImageContentData == null)
215         {
216                 __pImageContentData = new (nothrow) _ImageContentData;
217                 SysTryReturnResult(NID_CNT, __pImageContentData != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
218         }
219
220         __pImageContentData->width = pImageContentData->width;
221         __pImageContentData->height = pImageContentData->height;
222         __pImageContentData->orientationType = pImageContentData->orientationType;
223         __pImageContentData->title = pImageContentData->title;
224
225         return E_SUCCESS;
226 }
227
228 ImageContentInfo::_ImageContentData*
229 ImageContentInfo::GetImageContentData(void)
230 {
231         if (__pImageContentData == null)
232         {
233                 __pImageContentData = new (nothrow) ImageContentInfo::_ImageContentData;
234                 SysTryReturn(NID_CNT, __pImageContentData != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
235         }
236         return __pImageContentData;
237 }
238
239 int
240 ImageContentInfo::GetWidth(void) const
241 {
242         SysAssertf(__pImageContentData != null, "Not yet constructed. Construct() should be called before use.");
243
244         return __pImageContentData->width;
245 }
246
247 int
248 ImageContentInfo::GetHeight(void) const
249 {
250         SysAssertf(__pImageContentData != null, "Not yet constructed. Construct() should be called before use.");
251
252         return __pImageContentData->height;
253 }
254
255 ImageOrientationType
256 ImageContentInfo::GetOrientation(void) const
257 {
258         SysAssertf(__pImageContentData != null, "Not yet constructed. Construct() should be called before use.");
259
260         return __pImageContentData->orientationType;
261 }
262
263 String
264 ImageContentInfo::GetTitle(void) const
265 {
266         SysAssertf(__pImageContentData != null, "Not yet constructed. Construct() should be called before use.");
267
268         if ((__pImageContentData->title).IsEmpty())
269         {
270                 return String(L"Unknown");
271         }
272
273         return __pImageContentData->title;
274 }
275
276 }}