Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / platform / API / Messaging / AttachmentFactory.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       AttachmentFactory.h
20  * @author     Lukasz Marek (l.marek@samsung.com)
21  * @version    0.1
22  * @brief
23  */
24
25 #include <dpl/log/log.h>
26 #include <Commons/Exception.h>
27 #include "AttachmentFactory.h"
28 #include "Attachments.h"
29 #include <Messaging/Attachment.h>
30 #include <Commons/WrtWrapper/WrtWrappersMgr.h>
31 #include <WidgetDB/WidgetDBMgr.h>
32
33 using namespace std;
34 using namespace WrtDeviceApis;
35 using namespace WrtDeviceApis::Commons;
36 using namespace TizenApis::Platform::Messaging;
37
38 namespace {
39 const char* PATH_DOWNLOADS = "/opt/media/Downloads";
40 const char* PATH_DOCUMENTS = "/opt/media/Documents";
41 const char* PATH_SOUNDS = "/opt/media/Music";
42 const char* PATH_IMAGES = "/opt/media/Images";
43 const char* PATH_VIDEOS = "/opt/media/Videos";
44
45 const char* VPATH_DOWNLOADS = "download";
46 const char* VPATH_DOCUMENTS = "documents";
47 const char* VPATH_SOUNDS = "music";
48 const char* VPATH_IMAGES = "images";
49 const char* VPATH_VIDEOS = "videos";
50 const char* VPATH_WGT_PACKAGE = "wgt-package";
51 const char* VPATH_WGT_PRIVATE = "wgt-private";
52 const char* VPATH_WGT_PRIVATE_TEMP = "wgt-package-tmp";
53
54 const char* COMMAND_NAME = "/bin/cp";
55 const char* COMMAND_SWITCH_RECURSIVE = "-r";
56 const char* COMMAND_SWITCH_FORCE = "-f";
57
58 }
59
60 namespace TizenApis {
61 namespace Api {
62 namespace Messaging {
63
64 const char AttachmentFactory::m_pathSeparator = '/';
65
66 IAttachmentPtr AttachmentFactory::createAttachment(const std::string& fullPath,
67         bool isVirtualPath)
68 {
69     LogDebug("enter");
70     IAttachmentPtr attachment(new Attachment(fullPath, isVirtualPath));
71     if (!attachment->getIsCreatedProperly()) {
72         ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException,
73                  "Attachment couldn't be created");
74     }
75     return attachment;
76 }
77
78 IAttachmentPtr AttachmentFactory::createAttachment(const std::string& path,
79         const std::string& mimeType)
80 {
81     LogDebug("enter");
82
83     //check virtualPath
84     bool isVirtualPath = true;
85     IAttachmentPtr attachment(new Attachment(path, isVirtualPath));
86     if (!attachment->getIsCreatedProperly()) {
87         ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException,
88                  "Attachment couldn't be created");
89     }
90
91     if ( attachment )
92         attachment->setMimeType(mimeType);
93                 
94     return attachment;
95 }
96
97 std::string AttachmentFactory::getVirtualPathFromRealPath(JSContextRef context, 
98         const std::string& realpath)
99 {
100         LogDebug("path = " << realpath);        
101         IWrtWrapperPtr wrt = WrtWrappersMgr::getInstance().getWrtWrapper(context);
102         Assert(wrt && "WrtWrapper not found, not a GLOBAL context supplied?");
103
104         //find virtual root
105         std::string root;
106         std::string tail;
107
108         std::string::size_type virtualrootpathposition;
109         //download
110         virtualrootpathposition = realpath.find(PATH_DOWNLOADS);
111         if (virtualrootpathposition != std::string::npos)
112         {
113                 std::string vroot(PATH_DOWNLOADS);
114                 tail = realpath.substr(virtualrootpathposition + vroot.size() + 1, realpath.size() - 1);
115                 LogDebug("tail = " << tail);
116                 vroot=VPATH_DOWNLOADS;
117                 std::string virtualpath = vroot +  AttachmentFactory::m_pathSeparator + tail ;
118
119                 return virtualpath;
120         }
121
122         //document
123         virtualrootpathposition = realpath.find(PATH_DOCUMENTS);
124         if (virtualrootpathposition != std::string::npos)
125         {
126                 std::string vroot(PATH_DOCUMENTS);
127                 tail = realpath.substr(virtualrootpathposition + vroot.size() + 1, realpath.size() - 1);
128                 LogDebug("tail = " << tail);
129                 vroot=VPATH_DOCUMENTS;
130                 std::string virtualpath = vroot +  AttachmentFactory::m_pathSeparator + tail ;
131
132                 return virtualpath;
133         }
134
135         //sound
136         virtualrootpathposition = realpath.find(PATH_SOUNDS);
137         if (virtualrootpathposition != std::string::npos)
138         {
139                 std::string vroot(PATH_SOUNDS);
140                 tail = realpath.substr(virtualrootpathposition + vroot.size() + 1, realpath.size() - 1);
141                 LogDebug("tail = " << tail);
142                 vroot=VPATH_SOUNDS;
143                 std::string virtualpath = vroot +  AttachmentFactory::m_pathSeparator + tail ;
144
145                 return virtualpath;
146         }
147
148         //image
149         virtualrootpathposition = realpath.find(PATH_IMAGES);
150         if (virtualrootpathposition != std::string::npos)
151         {
152                 std::string vroot(PATH_IMAGES);
153                 tail = realpath.substr(virtualrootpathposition + vroot.size() + 1, realpath.size() - 1);
154                 LogDebug("tail = " << tail);
155                 vroot=VPATH_IMAGES;
156                 std::string virtualpath = vroot +  AttachmentFactory::m_pathSeparator + tail ;
157                 LogDebug("virtualpath = " << virtualpath);
158                 return virtualpath;
159         }
160
161         //video
162         virtualrootpathposition = realpath.find(PATH_VIDEOS);
163         if (virtualrootpathposition != std::string::npos)
164         {
165                 std::string vroot(PATH_VIDEOS);
166                 tail = realpath.substr(virtualrootpathposition + vroot.size() + 1, realpath.size() - 1);
167                 LogDebug("tail = " << tail);
168                 vroot=VPATH_VIDEOS;
169                 std::string virtualpath = vroot +  AttachmentFactory::m_pathSeparator + tail ;
170                 LogDebug("virtualpath = " << virtualpath);
171                 return virtualpath;
172         }
173
174         WidgetDB::Api::IWidgetDBPtr widgetDB =
175         WidgetDB::Api::getWidgetDB(wrt->getWidgetId());
176
177         //wgt-package
178         virtualrootpathposition = realpath.find(widgetDB->getWidgetInstallationPath());
179         if (virtualrootpathposition != std::string::npos)
180         {
181                 std::string vroot(widgetDB->getWidgetInstallationPath());
182                 tail = realpath.substr(virtualrootpathposition + vroot.size() + 1, realpath.size() - 1);
183                 LogDebug("tail = " << tail);
184                 vroot=VPATH_WGT_PACKAGE;
185                 std::string virtualpath = vroot +  AttachmentFactory::m_pathSeparator + tail ;
186                 LogDebug("virtualpath = " << virtualpath);
187                 return virtualpath;
188         }       
189         
190         //wgt-private
191         virtualrootpathposition = realpath.find(widgetDB->getWidgetPersistentStoragePath());
192         if (virtualrootpathposition != std::string::npos)
193         {
194                 std::string vroot(widgetDB->getWidgetPersistentStoragePath());
195                 tail = realpath.substr(virtualrootpathposition + vroot.size() + 1, realpath.size() - 1);
196                 LogDebug("tail = " << tail);
197                 vroot=VPATH_WGT_PRIVATE;
198                 std::string virtualpath = vroot +  AttachmentFactory::m_pathSeparator + tail ;
199                 LogDebug("virtualpath = " << virtualpath);
200                 return virtualpath;
201         }
202
203         //wgt-package-tmp
204         virtualrootpathposition = realpath.find(widgetDB->getWidgetTemporaryStoragePath());
205         if (virtualrootpathposition != std::string::npos)
206         {
207                 std::string vroot(widgetDB->getWidgetTemporaryStoragePath());
208                 tail = realpath.substr(virtualrootpathposition + vroot.size() + 1, realpath.size() - 1);
209                 LogDebug("tail = " << tail);
210                 vroot=VPATH_WGT_PRIVATE_TEMP;
211                 std::string virtualpath = vroot +  AttachmentFactory::m_pathSeparator + tail ;
212                 LogDebug("virtualpath = " << virtualpath);
213                 return virtualpath;
214         }       
215
216         //other file.
217         
218         //copy file
219         size_t pos = realpath.find_last_of("/");
220         if ((pos + 1) >= realpath.size()) {
221             LogError("Problem with short name creation");
222             Throw(InvalidArgumentException);
223         }
224         std::string attachShortName = realpath.substr(pos + 1);
225         std::string privat_dir = widgetDB->getWidgetPersistentStoragePath();
226         if ( privat_dir.empty())
227         {
228                 LogError("Problem with short name creation");
229                 Throw(InvalidArgumentException);
230         }       
231         
232      std::stringstream to_oss;
233                 to_oss << "/email/" << attachShortName;
234                 LogDebug("temp file=" << to_oss.str());
235         
236         std::stringstream cp_oss;
237         cp_oss << COMMAND_NAME;
238         cp_oss << " " << COMMAND_SWITCH_RECURSIVE;
239         cp_oss << " \"" << realpath << "\"";
240         cp_oss << " \"" << privat_dir << to_oss.str() << "\"";
241         
242         std::string vrootpath(VPATH_WGT_PRIVATE);
243         
244         return vrootpath+to_oss.str();
245 }
246
247
248 std::string AttachmentFactory::getRealPathFromVirtualPath(JSContextRef context, 
249         const std::string& path)
250 {
251         LogDebug("path = " << path);    
252         IWrtWrapperPtr wrt = WrtWrappersMgr::getInstance().getWrtWrapper(context);
253         Assert(wrt && "WrtWrapper not found, not a GLOBAL context supplied?");
254
255         std::string root;
256         std::string tail;
257         std::string::size_type separatorPosition = path.find(AttachmentFactory::m_pathSeparator);
258         if (separatorPosition != std::string::npos) {
259                 root = path.substr(0, separatorPosition);
260                 tail = path.substr(separatorPosition + 1, path.size() - 1);
261         } else {
262                 root = path;
263         }
264         LogDebug("root = " << root);
265         LogDebug("tail = " << tail);
266         
267         WidgetDB::Api::IWidgetDBPtr widgetDB =
268         WidgetDB::Api::getWidgetDB(wrt->getWidgetId());
269
270         std::string realroot;
271
272         if ( root == VPATH_DOWNLOADS )
273         {
274                 realroot = PATH_DOWNLOADS;
275         }
276         else if ( root == VPATH_DOCUMENTS )
277         {
278                 realroot = PATH_DOCUMENTS;
279         }
280         else if ( root == VPATH_SOUNDS )
281         {
282                 realroot = PATH_SOUNDS;
283         }
284         else if ( root == VPATH_IMAGES )
285         {
286                 realroot = PATH_IMAGES;
287         }
288         else if ( root == VPATH_VIDEOS )
289         {
290                 realroot = PATH_VIDEOS;
291         }
292         else if ( root == VPATH_WGT_PACKAGE )
293         {
294                 realroot = widgetDB->getWidgetInstallationPath();
295         }
296         else if ( root == VPATH_WGT_PRIVATE )
297         {
298                 realroot = widgetDB->getWidgetPersistentStoragePath();
299         }
300         else if ( root == VPATH_WGT_PRIVATE_TEMP )
301         {
302                 realroot = widgetDB->getWidgetTemporaryStoragePath();
303         }
304         else
305         {       //exception
306                 LogDebug("Can't find root path");
307                 ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException,
308                  "Attachment couldn't be created");
309         }
310
311         std::string ret = realroot + AttachmentFactory::m_pathSeparator + tail;
312                                         
313         return ret;
314 }
315
316
317
318 }
319 }
320 }