2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file AttachmentFactory.h
20 * @author Lukasz Marek (l.marek@samsung.com)
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>
34 using namespace WrtDeviceApis;
35 using namespace WrtDeviceApis::Commons;
36 using namespace TizenApis::Platform::Messaging;
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";
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";
54 const char* COMMAND_NAME = "/bin/cp";
55 const char* COMMAND_SWITCH_RECURSIVE = "-r";
56 const char* COMMAND_SWITCH_FORCE = "-f";
64 const char AttachmentFactory::m_pathSeparator = '/';
66 IAttachmentPtr AttachmentFactory::createAttachment(const std::string& fullPath,
70 IAttachmentPtr attachment(new Attachment(fullPath, isVirtualPath));
71 if (!attachment->getIsCreatedProperly()) {
72 ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException,
73 "Attachment couldn't be created");
78 IAttachmentPtr AttachmentFactory::createAttachment(const std::string& path,
79 const std::string& mimeType)
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");
92 attachment->setMimeType(mimeType);
97 std::string AttachmentFactory::getVirtualPathFromRealPath(JSContextRef context,
98 const std::string& realpath)
100 LogDebug("path = " << realpath);
101 IWrtWrapperPtr wrt = WrtWrappersMgr::getInstance().getWrtWrapper(context);
102 Assert(wrt && "WrtWrapper not found, not a GLOBAL context supplied?");
108 std::string::size_type virtualrootpathposition;
110 virtualrootpathposition = realpath.find(PATH_DOWNLOADS);
111 if (virtualrootpathposition != std::string::npos)
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 ;
123 virtualrootpathposition = realpath.find(PATH_DOCUMENTS);
124 if (virtualrootpathposition != std::string::npos)
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 ;
136 virtualrootpathposition = realpath.find(PATH_SOUNDS);
137 if (virtualrootpathposition != std::string::npos)
139 std::string vroot(PATH_SOUNDS);
140 tail = realpath.substr(virtualrootpathposition + vroot.size() + 1, realpath.size() - 1);
141 LogDebug("tail = " << tail);
143 std::string virtualpath = vroot + AttachmentFactory::m_pathSeparator + tail ;
149 virtualrootpathposition = realpath.find(PATH_IMAGES);
150 if (virtualrootpathposition != std::string::npos)
152 std::string vroot(PATH_IMAGES);
153 tail = realpath.substr(virtualrootpathposition + vroot.size() + 1, realpath.size() - 1);
154 LogDebug("tail = " << tail);
156 std::string virtualpath = vroot + AttachmentFactory::m_pathSeparator + tail ;
157 LogDebug("virtualpath = " << virtualpath);
162 virtualrootpathposition = realpath.find(PATH_VIDEOS);
163 if (virtualrootpathposition != std::string::npos)
165 std::string vroot(PATH_VIDEOS);
166 tail = realpath.substr(virtualrootpathposition + vroot.size() + 1, realpath.size() - 1);
167 LogDebug("tail = " << tail);
169 std::string virtualpath = vroot + AttachmentFactory::m_pathSeparator + tail ;
170 LogDebug("virtualpath = " << virtualpath);
174 WidgetDB::Api::IWidgetDBPtr widgetDB =
175 WidgetDB::Api::getWidgetDB(wrt->getWidgetId());
178 virtualrootpathposition = realpath.find(widgetDB->getWidgetInstallationPath());
179 if (virtualrootpathposition != std::string::npos)
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);
191 virtualrootpathposition = realpath.find(widgetDB->getWidgetPersistentStoragePath());
192 if (virtualrootpathposition != std::string::npos)
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);
204 virtualrootpathposition = realpath.find(widgetDB->getWidgetTemporaryStoragePath());
205 if (virtualrootpathposition != std::string::npos)
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);
219 size_t pos = realpath.find_last_of("/");
220 if ((pos + 1) >= realpath.size()) {
221 LogError("Problem with short name creation");
222 Throw(InvalidArgumentException);
224 std::string attachShortName = realpath.substr(pos + 1);
225 std::string privat_dir = widgetDB->getWidgetPersistentStoragePath();
226 if ( privat_dir.empty())
228 LogError("Problem with short name creation");
229 Throw(InvalidArgumentException);
232 std::stringstream to_oss;
233 to_oss << "/email/" << attachShortName;
234 LogDebug("temp file=" << to_oss.str());
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() << "\"";
242 std::string vrootpath(VPATH_WGT_PRIVATE);
244 return vrootpath+to_oss.str();
248 std::string AttachmentFactory::getRealPathFromVirtualPath(JSContextRef context,
249 const std::string& path)
251 LogDebug("path = " << path);
252 IWrtWrapperPtr wrt = WrtWrappersMgr::getInstance().getWrtWrapper(context);
253 Assert(wrt && "WrtWrapper not found, not a GLOBAL context supplied?");
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);
264 LogDebug("root = " << root);
265 LogDebug("tail = " << tail);
267 WidgetDB::Api::IWidgetDBPtr widgetDB =
268 WidgetDB::Api::getWidgetDB(wrt->getWidgetId());
270 std::string realroot;
272 if ( root == VPATH_DOWNLOADS )
274 realroot = PATH_DOWNLOADS;
276 else if ( root == VPATH_DOCUMENTS )
278 realroot = PATH_DOCUMENTS;
280 else if ( root == VPATH_SOUNDS )
282 realroot = PATH_SOUNDS;
284 else if ( root == VPATH_IMAGES )
286 realroot = PATH_IMAGES;
288 else if ( root == VPATH_VIDEOS )
290 realroot = PATH_VIDEOS;
292 else if ( root == VPATH_WGT_PACKAGE )
294 realroot = widgetDB->getWidgetInstallationPath();
296 else if ( root == VPATH_WGT_PRIVATE )
298 realroot = widgetDB->getWidgetPersistentStoragePath();
300 else if ( root == VPATH_WGT_PRIVATE_TEMP )
302 realroot = widgetDB->getWidgetTemporaryStoragePath();
306 LogDebug("Can't find root path");
307 ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException,
308 "Attachment couldn't be created");
311 std::string ret = realroot + AttachmentFactory::m_pathSeparator + tail;