Update change log and spec for wrt-plugins-tizen_0.4.52
[framework/web/wrt-plugins-tizen.git] / src / Filesystem / Manager.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18
19 #include "Manager.h"
20
21 #include <unistd.h>
22 #include <sys/stat.h>
23 #include <errno.h>
24 #include <pcrecpp.h>
25 #include <ctime>
26 #include <sstream>
27 #include <dirent.h>
28 #include <dpl/scoped_ptr.h>
29
30 #include <Commons/Exception.h>
31 #include <Commons/Regex.h>
32 #include "PathUtils.h"
33 #include "StorageProperties.h"
34 #include "Node.h"
35 #include "Utils.h"
36 #include <app.h>
37 #include <Logger.h>
38
39 namespace {
40 const char* PATH_DOWNLOADS = "/opt/usr/media/Downloads";
41 const char* PATH_DOCUMENTS = "/opt/usr/media/Documents";
42 const char* PATH_SOUNDS = "/opt/usr/media/Sounds";
43 const char* PATH_IMAGES = "/opt/usr/media/Images";
44 const char* PATH_VIDEOS = "/opt/usr/media/Videos";
45 const char* PATH_RINGTONE = "/opt/usr/share/settings/Ringtones";
46 }
47
48 using namespace WrtDeviceApis;
49 using namespace WrtDeviceApis::Commons;
50
51 namespace DeviceAPI {
52 namespace Filesystem {
53 Manager::Locations Manager::m_locations;
54 Manager::RootList Manager::m_rootlist;
55 Manager::SubRootList Manager::m_subrootlist;
56 const std::size_t Manager::m_maxPathLength = PATH_MAX;
57         
58 NodeList Manager::m_openedNodes;
59 std::vector<Manager::WatcherPtr> Manager::m_watchers;
60
61 bool Manager::fileExists(const std::string &file)
62 {
63     errno = 0;
64     struct stat info;
65     memset(&info, 0, sizeof(struct stat));
66     int status = lstat(file.c_str(), &info);
67     if (status == 0) {
68         return true;
69     } else if (errno == ENOENT) {
70         return false;
71     }
72     ThrowMsg(Commons::PlatformException, "Cannot stat file.");
73 }
74
75 bool Manager::getSupportedDeviceCB(int storage, storage_type_e type, storage_state_e state, const char *path, void *user_data)
76 {
77         std::vector<StoragePropertiesPtr>* storageVector = (std::vector<StoragePropertiesPtr>*)user_data;
78         StoragePropertiesPtr storageItem(new StorageProperties());
79
80         int size = 12;
81         char buf[size];
82         std::string lable;
83         std::string fullpath(path);
84         if (type == STORAGE_TYPE_INTERNAL) {
85                 snprintf(buf, size, "internal%d", storage);
86                 lable.append(buf);
87         } else if (type == STORAGE_TYPE_EXTERNAL) {
88                 snprintf(buf, size, "removable%d", storage);
89                 lable.append(buf);
90         }
91         LoggerD(lable << "state" << state);
92
93         storageItem->setId(storage);
94         storageItem->setLabel(lable);
95         storageItem->setType((short)type);
96         storageItem->setState((short)state);
97         storageItem->setFullPath(fullpath);
98
99         storageVector->insert(storageVector->end(), storageItem);
100
101         return true;
102 }
103
104 void Manager::storageStateChangedCB(int storage, storage_state_e state, void *user_data)
105 {
106         if (user_data != NULL) {
107                 watcherList* m_watcherList = (watcherList *)user_data;
108                 watcherList::iterator it = m_watcherList->begin();
109                 for(; it!= m_watcherList->end(); ++it) {
110                         (*it)->StorageStateHasChanged(storage, state);
111                 }
112         }
113 }
114
115 void Manager::addOpenedNode(const INodePtr& node)
116 {
117     NodeListIterator it = m_openedNodes.begin();
118     for (; it != m_openedNodes.end(); ++it) {
119         if (node.Get() == (*it).Get()) {
120             //node is added already
121             return;
122         }
123     }
124     m_openedNodes.push_back(node);
125 }
126
127 void Manager::removeOpenedNode(const INodePtr& node)
128 {
129     NodeListIterator it = m_openedNodes.begin();
130     for (; it != m_openedNodes.end(); ++it) {
131         if ((*it).Get() == node.Get()) {
132             m_openedNodes.erase(it);
133             break;
134         }
135     }
136 }
137
138 bool Manager::checkIfOpened(const IPathPtr& path) const
139 {
140     NodeListIterator it = m_openedNodes.begin();
141     for (; it != m_openedNodes.end(); ++it) {
142         if (*path == *(*it)->getPath()) {
143             return true;
144         }
145     }
146     return false;
147 }
148
149 Manager::Manager()
150 {
151     static bool initialized = init();
152     (void) initialized;
153 }
154
155 Manager::~Manager()
156 {
157 }
158
159 StorageList Manager::getStorageList() const
160 {
161     return m_locations;
162 }
163
164 IPathPtr Manager::getBasePath() const
165 {
166     Locations::const_iterator it = m_locations.find(m_subrootlist.find(LT_ROOT)->second);
167     if (it == m_locations.end()) {
168         ThrowMsg(Commons::PlatformException, "Base path not available.");
169     }
170     return it->second;
171 }
172
173 IPathPtr Manager::getLocationPath(LocationType type) const
174 {
175     Locations::const_iterator it = m_locations.find(m_subrootlist.find(type)->second);
176     if (it != m_locations.end()) {
177         return it->second->clone();
178     }
179     return IPathPtr();
180 }
181
182 LocationPaths Manager::getLocationPaths() const
183 {
184     LocationPaths result;
185     Locations::const_iterator it = m_locations.begin();
186     for (; it != m_locations.end(); ++it) {
187         result.push_back(it->second->clone());
188     }
189     return result;
190 }
191
192 LocationTypes Manager::getLocations() const
193 {
194     LocationTypes result;
195     SubRootList::const_iterator it = m_subrootlist.begin();
196     for (; it != m_subrootlist.end(); ++it) {
197         result.push_back(it->first);
198     }
199     return result;
200 }
201
202 void Manager::getNode(const EventResolvePtr& event)
203 {
204     EventRequestReceiver<EventResolve>::PostRequest(event);
205 }
206
207 void Manager::getStorage(const EventGetStoragePtr& event)
208 {
209         EventRequestReceiver<EventGetStorage>::PostRequest(event);
210 }
211
212 void Manager::listStorages(const EventListStoragesPtr& event)
213 {
214         EventRequestReceiver<EventListStorages>::PostRequest(event);
215 }
216
217 std::size_t Manager::getMaxPathLength() const
218 {
219     return m_maxPathLength;
220 }
221
222 void Manager::copy(const EventCopyPtr& event)
223 {
224     EventRequestReceiver<EventCopy>::PostRequest(event);
225 }
226
227 void Manager::move(const EventMovePtr& event)
228 {
229     EventRequestReceiver<EventMove>::PostRequest(event);
230 }
231
232 void Manager::create(const EventCreatePtr& event)
233 {
234     EventRequestReceiver<EventCreate>::PostRequest(event);
235 }
236
237 void Manager::remove(const EventRemovePtr& event)
238 {
239     EventRequestReceiver<EventRemove>::PostRequest(event);
240 }
241
242 void Manager::find(const EventFindPtr& event)
243 {
244     EventRequestReceiver<EventFind>::PostRequest(event);
245 }
246
247 void Manager::find(const IPathPtr& path,
248         const FiltersMap& filters,
249         NodeList& result,
250         const EventFindPtr& event)
251 {
252     Try {
253         DIR* dir = opendir(path->getFullPath().c_str());
254         if (!dir) {
255             return;
256         }
257
258         errno = 0;
259         struct dirent* entry = NULL;
260         while ((entry = readdir(dir))) {
261             if (event && event->checkCancelled()) {
262                 break;
263             }
264             if (!strcmp(entry->d_name, ".") || !strncmp(entry->d_name, "..", 2)) {
265                 continue;
266             }
267             IPathPtr childPath = *path + entry->d_name;
268             struct stat info;
269             memset(&info, 0, sizeof(struct stat));
270             if (lstat(childPath->getFullPath().c_str(), &info) == 0) {
271                 if (matchFilters(entry->d_name, info, filters)) {
272                     result.push_back(Node::resolve(childPath));
273                 }
274                 if (S_ISDIR(info.st_mode)) {
275                     find(childPath, filters, result, event);
276                 }
277             }
278         }
279
280         if (errno != 0) {
281             ThrowMsg(Commons::PlatformException,
282                      "Error while reading directory.");
283         }
284
285         if (closedir(dir) != 0) {
286             ThrowMsg(Commons::PlatformException,
287                      "Could not close platform node.");
288         }
289     }
290     Catch(WrtDeviceApis::Commons::Exception) {
291     }
292 }
293
294 void Manager::copyElement(
295         const std::string &src, const std::string &dest, bool recursive) const
296 {
297     LoggerD("Copying src: " << src << " to: " << dest);
298
299     //element is a file:
300     if (EINA_TRUE != ecore_file_is_dir(src.c_str())) {
301         if (EINA_TRUE != ecore_file_cp(src.c_str(), dest.c_str())) {
302             ThrowMsg(Commons::PlatformException, "Failed to copy file");
303         }
304         return;
305     }
306     //element is a directory -> create it:
307     if (EINA_TRUE != ecore_file_is_dir(dest.c_str())) {
308         if (EINA_TRUE != ecore_file_mkdir(dest.c_str())) {
309             LoggerD("Failed to create destination directory");
310             ThrowMsg(Commons::PlatformException, "Failed to copy directory");
311         }
312     }
313     //copy all elements of directory:
314     if (recursive) {
315         Eina_List* list = ecore_file_ls(src.c_str());
316         void* data;
317         EINA_LIST_FREE(list, data)
318         {
319             Try
320             {
321                 copyElement((src + '/' + static_cast<char*>(data)).c_str(),
322                             (dest + '/' + static_cast<char*>(data)).c_str());
323             }
324                         
325             Catch (Commons::Exception) 
326             {
327                  //remove rest of the list
328                 EINA_LIST_FREE(list, data)
329                 {
330                     free(data);
331                 }
332                 LoggerE("Exception: " << _rethrown_exception.GetMessage());
333                 ReThrowMsg(Commons::PlatformException, "Failed to copy element");
334             }
335             free(data);
336         }
337     }
338
339 }
340
341 /*bool Manager::access(const IPathPtr& path,
342         int accessType) const
343 {
344     int amode = 0;
345     if (accessType & AT_EXISTS) { amode |= F_OK; }
346     if (accessType & AT_READ) { amode |= R_OK; }
347     if (accessType & AT_WRITE) { amode |= W_OK; }
348     if (accessType & AT_EXEC) { amode |= X_OK; }
349     return (::access(path->getFullPath().c_str(), amode) == 0);
350 }*/
351
352 long Manager::addStorageStateChangeListener(
353         const EventStorageStateChangedEmitterPtr& emitter)
354 {
355         RootList::const_iterator it = m_rootlist.begin();
356         WatcherPtr watcher(new Watcher(emitter));
357         Manager::m_watchers.push_back(watcher);
358
359         for (; it != m_rootlist.end(); ++it) {
360                 storage_set_state_changed_cb(it->second, Manager::storageStateChangedCB, (void *)(&m_watchers));
361         }
362
363         watcher->getCurrentStoargeStateForWatch();
364         return static_cast<long>(emitter->getId());
365 }
366
367 void Manager::removeStorageStateChangeListener(EventStorageStateChangedEmitter::IdType id)
368 {
369         watcherList::iterator itWatcher = Manager::m_watchers.begin();
370         bool found = false;
371         for (;itWatcher != Manager::m_watchers.end();) {
372                 if (id == (*itWatcher)->getEmitter()->getId()) {
373                         itWatcher = Manager::m_watchers.erase(itWatcher);
374                         found = true;
375                         continue;
376                 }
377                 ++itWatcher;
378         }
379
380         if (Manager::m_watchers.size() == 0) {
381                 RootList::const_iterator itRoot;
382                 for (itRoot = m_rootlist.begin(); itRoot != m_rootlist.end(); ++itRoot) {
383                         storage_unset_state_changed_cb(itRoot->second);
384                 }
385         }
386
387         if (found == false) {
388 //              LoggerD("no exist" << id);
389                 ThrowMsg(Commons::NotFoundException, "The " << id << "is not exist");
390         }
391 }
392
393 bool Manager::matchFilters(const std::string& name,
394         const struct stat& info,
395         const FiltersMap& filters)
396 {
397     FiltersMap::const_iterator it = filters.begin();
398     for (; it != filters.end(); ++it) {
399         if (it->first == FF_NAME) {
400             if (!pcrecpp::RE(it->second).PartialMatch(name)) { return false; }
401         } else if (it->first == FF_SIZE) {
402             std::size_t size;
403             std::stringstream ss(it->second);
404             ss >> size;
405             if (!ss ||
406                 (size != static_cast<size_t>(info.st_size))) { return false; }
407         } else if (it->first == FF_CREATED) {
408             std::time_t created;
409             std::stringstream ss(it->second);
410             ss >> created;
411             if (!ss || (created != info.st_ctime)) { return false; }
412         } else if (it->first == FF_MODIFIED) {
413             std::time_t modified;
414             std::stringstream ss(it->second);
415             ss >> modified;
416             if (!ss || (modified != info.st_mtime)) { return false; }
417         }
418     }
419     return true;
420 }
421
422 void Manager::OnRequestReceived(const EventResolvePtr& event)
423 {
424     try {
425         INodePtr node = Node::resolve(event->getPath());
426         if (node->checkPermission(event->getPath(), event->getMode(), node->getType()) == false)
427         {
428             ThrowMsg(Commons::SecurityException, "Permission Denied Error");
429         }
430         event->setResult(node);
431     }
432     catch (const Commons::Exception& ex) 
433     {
434         LoggerE("Exception: " << ex.GetMessage());
435         event->setExceptionCode(ex.getCode());
436     }
437 }
438
439 void Manager::OnRequestReceived(const EventGetStoragePtr& event)
440 {
441         try {
442
443                 StoragePropertiesPtr storage(new StorageProperties());
444
445                 RootList::const_iterator it = m_rootlist.find(event->getLabel());
446                 if (it == m_rootlist.end()) {
447                         Locations::const_iterator itL = m_locations.find(event->getLabel());
448                         if (itL == m_locations.end()) {
449                                 ThrowMsg(Commons::NotFoundException, "Base path not available.");
450                         } else {
451                                 storage->setId(0xff);
452                                 storage->setLabel(event->getLabel());
453                                 storage->setType(StorageProperties::TYPE_INTERNAL);
454                                 storage->setState(StorageProperties::STATE_MOUNTED);
455                         }
456                 } else {
457                         int id = it->second;
458
459                         storage_type_e currentType;
460                         storage_state_e currentState;
461
462                         storage_get_type(id, &currentType);
463                         storage_get_state(id, &currentState);
464
465                         storage->setId(id);
466                         storage->setLabel(event->getLabel());
467                         storage->setType((short)currentType);
468                         storage->setState((short)currentState);
469                 }
470
471                 event->setResult(storage);
472     } catch (const Commons::Exception& ex) 
473     {
474         event->setExceptionCode(ex.getCode());
475     }
476 }
477
478 void Manager::OnRequestReceived(const EventListStoragesPtr& event)
479 {
480         try {
481                 std::vector<StoragePropertiesPtr> storageList;
482                         
483                 storage_foreach_device_supported(Manager::getSupportedDeviceCB, &storageList);
484
485                 SubRootList::const_iterator it = m_subrootlist.begin();
486                 for (; it != m_subrootlist.end(); ++it) {
487                         if (it->first == LT_ROOT) 
488                                 continue;
489                         if (it->first == LT_SDCARD) 
490                                 continue;
491                         if (it->first == LT_USBHOST) 
492                                 continue;                       
493
494                         addLocalStorage(it->second, storageList);
495                 }
496                 
497
498                 event->setResult(storageList);
499     }   catch (const Commons::Exception& ex) 
500     {
501         event->setExceptionCode(ex.getCode());
502     }
503 }
504
505 void Manager::OnRequestReceived(const EventCopyPtr& event)
506 {
507     Try {
508         INodePtr srcNode = Node::resolve(event->getSource());
509                 LoggerD(std::hex << srcNode->getMode() << " " << std::hex << PM_USER_READ);
510         if ((srcNode->getMode() & PM_USER_READ/*PERM_READ*/) == 0) {
511             ThrowMsg(Commons::SecurityException,
512                      "Not enough permissions to read source node.");
513         }
514
515         IPathPtr src = event->getSource();
516         IPathPtr dest = event->getDestination();
517         if (!dest->isAbsolute()) {
518             dest = src->getPath() + *dest;
519         }
520   
521         INodePtr parent;
522         Try {
523             parent = Node::resolve(IPath::create(dest->getPath()));
524         }
525         Catch(Commons::NotFoundException) 
526         {
527             event->setExceptionCode(_rethrown_exception.getCode());
528             ReThrowMsg(Commons::NotFoundException, "could not find a destination path.");
529         }
530         Catch (Commons::Exception) 
531         {
532             event->setExceptionCode(_rethrown_exception.getCode());
533             ReThrowMsg(Commons::PlatformException,
534                        "Could not get destination's parent node.");
535         }
536
537         if (parent->getType() != NT_DIRECTORY) {
538             ThrowMsg(Commons::PlatformException,
539                      "Destination's parent node is not directory.");
540         }
541
542                 std::string realSrc = src->getFullPath();
543                 std::string realDest = dest->getFullPath();
544
545                 if (realSrc == realDest) {
546                         ThrowMsg(Commons::PlatformException,
547                         "Destination is same as source.");
548                 }
549                 
550
551         errno = 0;
552         struct stat info;
553         memset(&info, 0, sizeof(struct stat));
554         int status = lstat(realDest.c_str(), &info);
555         if ((status != 0) && (errno != ENOENT)) {
556             ThrowMsg(Commons::PlatformException,
557                      "No access to platform destination node.");
558         }
559                 
560         if (S_ISDIR(info.st_mode) && srcNode->getType() == NT_FILE) {
561             dest->append("/" + src->getName());
562             realDest = dest->getFullPath();
563             memset(&info, 0, sizeof(struct stat));
564             status = lstat(realDest.c_str(), &info);
565             if ((status != 0) && (errno != ENOENT)) {
566                 ThrowMsg(Commons::PlatformException,
567                              "No access to platform destination node.");
568             }
569         }
570
571         if (0 == status) {
572             //no owerwrite flag setted -> exception
573             if ((event->getOptions() & OPT_OVERWRITE) == 0) {
574                 ThrowMsg(Commons::PlatformException, "Overwrite is not set.");
575             }
576
577             if (event->checkCancelled()) {
578                 //file is not copied yet, so we can cancel it now.
579                 event->setCancelAllowed(true);
580                 return;
581             }
582
583             //destination exist. Need to be removed
584             Try {
585                 INodePtr node = Node::resolve(dest);
586
587                 // only remove if dest is file.
588                 if (node->getType() == NT_FILE) {
589                     node->remove(event->getOptions());
590                 } 
591                 else {
592                         // destination exist and src & dest are directory and dest path ends with '/'
593                     if (srcNode->getType() == NT_DIRECTORY && realDest[realDest.length() - 1] == '/') {
594                         realDest += src->getName();
595                     }
596                 }
597             }
598             catch (const Commons::Exception& ex) 
599             {
600                 LoggerE("Exception: " << ex.GetMessage());
601                 event->setExceptionCode(ex.getCode());
602              }
603         }
604         //Destination is not exist. Start copy now.
605                 LoggerD(dest->getFullPath().c_str());
606         copyElement(realSrc, realDest);
607
608         event->setResult(Node::resolve(dest));
609     }
610     catch (const Commons::Exception& ex) 
611     {
612         LoggerE("Exception: " << ex.GetMessage());
613         event->setExceptionCode(ex.getCode());
614     }
615     //file is copied already so we don't allow cancelling anymore.
616     event->setCancelAllowed(false);
617 }
618
619 void Manager::OnRequestReceived(const EventMovePtr& event)
620 {
621     try {
622         IPathPtr src = event->getSource();
623         IPathPtr dest = event->getDestination();
624
625         INodePtr srcNode = Node::resolve(src);
626                 LoggerD(std::hex << srcNode->getMode() << " " << std::hex << PM_USER_WRITE);
627         if ((srcNode->getMode() & PM_USER_WRITE/*PERM_WRITE*/) == 0)
628         {
629             ThrowMsg(Commons::SecurityException,
630                      "Not enough permissions to move source node.");
631         }
632
633         if (!dest->isAbsolute()) {
634             dest = src->getPath() + *dest;
635         }
636
637         if (src == dest) {
638             ThrowMsg(Commons::PlatformException,
639                      "Destination is same as source.");
640         }
641
642         INodePtr parent;
643         Try {
644             parent = Node::resolve(IPath::create(dest->getPath()));
645         }
646                 
647         Catch(Commons::NotFoundException) 
648         {
649             event->setExceptionCode(_rethrown_exception.getCode());
650             ReThrowMsg(Commons::NotFoundException, "could not find a destination path.");
651         }
652         Catch(Commons::Exception) 
653         {
654             LoggerE("Exception: " << _rethrown_exception.GetMessage());
655             ReThrowMsg(Commons::PlatformException,
656                        "Could not get destination's parent node.");
657         }
658
659         if (parent->getType() != NT_DIRECTORY) {
660             ThrowMsg(Commons::PlatformException,
661                      "Destination's parent node is not directory.");
662         }
663
664         errno = 0;
665         struct stat info;
666         memset(&info, 0, sizeof(info));
667         int status = lstat(dest->getFullPath().c_str(), &info);
668         if ((status != 0) && (errno != ENOENT)) {
669             ThrowMsg(Commons::PlatformException,
670                      "No access to platform destination node.");
671         }
672
673         LoggerD(dest->getFullPath().c_str());
674                 
675         if (S_ISDIR(info.st_mode) && srcNode->getType() == NT_FILE) {
676                         dest->append("/" + src->getName());
677                         memset(&info, 0, sizeof(info));
678                         status = lstat(dest->getFullPath().c_str(), &info);
679                         if ((status != 0) && (errno != ENOENT)) {
680                     ThrowMsg(Commons::PlatformException,
681                             "No access to platform destination node.");
682                         }
683         } 
684
685                 if (status == 0 && 0 == (event->getOptions() & OPT_OVERWRITE)) {
686             ThrowMsg(Commons::PlatformException, "Overwrite is not set.");
687         }
688
689         if (event->checkCancelled()) {
690             //file is not moved yet, so we can cancel it now.
691             event->setCancelAllowed(true);
692             return;
693         }
694
695         errno = 0;
696         
697         LoggerD(dest->getFullPath().c_str());
698                 
699         if (0 != ::rename(src->getFullPath().c_str(), dest->getFullPath().c_str()))
700         {
701             int error = errno;
702             switch (error)
703             {
704             case EXDEV:
705                 {
706                                         LoggerD(std::hex << srcNode->getMode() << " " << std::hex << PM_USER_READ);
707                     if ((srcNode->getMode() & PM_USER_READ /*PERM_READ*/) == 0)
708                     {
709                         ThrowMsg(Commons::SecurityException,
710                                  "Not enough permissions to move source node.");
711                     }
712                     if (0 == status) {
713                         //destination exist. Need to be removed
714                         Try {
715                             INodePtr node = Node::resolve(dest);
716                             node->remove(event->getOptions());
717                         }
718                         catch (const Commons::Exception& ex) 
719                         {
720                             LoggerE("Exception: " << ex.GetMessage());
721                             event->setExceptionCode(ex.getCode());
722                             LoggerE("Exception while removing dest directory");
723                         }
724                     }
725
726                     copyElement(src->getFullPath(),
727                                 dest->getFullPath());
728                     //remove source files
729                     Try {
730                         INodePtr node = Node::resolve(event->getSource());
731                         node->remove(event->getOptions());
732                     }
733                     Catch(Commons::Exception) {
734                         LoggerE("Exception: "
735                                  << _rethrown_exception.GetMessage());
736                     }
737                     break;
738                 }
739             default:
740                                 // needtofix
741                 ThrowMsg(Commons::PlatformException,
742                          "Error on rename: " /*<< DPL::GetErrnoString(error)*/);
743                 break;
744             }
745         }
746
747         event->setResult(Node::resolve(dest));
748     }
749     catch (const Commons::Exception& ex) 
750     {
751         LoggerE("Exception: " << ex.GetMessage());
752         event->setExceptionCode(ex.getCode());
753     }
754
755     event->setCancelAllowed(false);
756 }
757
758 void Manager::OnRequestReceived(const EventCreatePtr& event)
759 {
760     Try {
761     }
762     catch (const Commons::Exception& ex) 
763     {
764         LoggerE("Exception: " << ex.GetMessage());
765         event->setExceptionCode(ex.getCode());
766     }
767 }
768
769 void Manager::OnRequestReceived(const EventRemovePtr& event)
770 {
771     if (!event->checkCancelled()) {
772         Try {
773             INodePtr node = Node::resolve(event->getPath());
774             node->remove(event->getOptions());
775         }
776         catch (const Commons::Exception& ex) 
777         {
778             LoggerE("Exception: " << ex.GetMessage());
779             event->setExceptionCode(ex.getCode());
780         }
781         event->setCancelAllowed(false);
782     } else {
783         event->setCancelAllowed(true);
784     }
785 }
786
787 void Manager::OnRequestReceived(const EventFindPtr& event)
788 {
789     try {
790         NodeList result;
791         find(event->getPath(), event->getFilters(), result, event);
792         event->setResult(result);
793     }
794     catch (const Commons::Exception& ex) {
795         LoggerE("Exception: " << ex.GetMessage());
796         event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
797     }
798     event->setCancelAllowed(true);
799 }
800
801 void Manager::addLocalStorage(std::string label, std::vector<StoragePropertiesPtr> &storageList)
802 {
803         StoragePropertiesPtr storage(new StorageProperties());
804         storage->setId(0xff);
805         storage->setLabel(label);
806         storage->setType(StorageProperties::TYPE_INTERNAL);
807
808         storage_state_e currentState;
809         storage_get_state(0, &currentState);
810         storage->setState((short)currentState);
811
812         storageList.insert(storageList.end(), storage);
813 }
814
815 void Manager::addWidgetStorage(const std::string &key, const std::string &value)
816 {
817         setupLocation(key, value.c_str());
818
819         if (key == "wgt-package")
820         {
821                 m_subrootlist[LT_WGTPKG] = key;
822         }
823         else if (key == "wgt-private")
824         {
825                 m_subrootlist[LT_WGTPRV] = key;
826         }
827         else if (key == "wgt-private-tmp")
828         {
829                 m_subrootlist[LT_WGTPRVTMP] = key;
830         }
831 }
832
833 bool Manager::init()
834 {
835         std::vector<StoragePropertiesPtr> storageList;
836         storage_foreach_device_supported(Manager::getSupportedDeviceCB, &storageList);
837
838         for (size_t i = 0; i < storageList.size(); i++) {
839                 m_locations[storageList[i]->getLabel()] = IPath::create(storageList[i]->getFullPath());
840                 m_rootlist[storageList[i]->getLabel()] = storageList[i]->getId();
841         }
842
843         /* for Tizen */
844         setupLocation("downloads", PATH_DOWNLOADS);
845         setupLocation("documents", PATH_DOCUMENTS);
846         setupLocation("music", PATH_SOUNDS);
847         setupLocation("images", PATH_IMAGES);
848         setupLocation("videos", PATH_VIDEOS);
849         setupLocation("ringtones", PATH_RINGTONE);
850
851         m_subrootlist[LT_ROOT] = "internal0";
852         m_subrootlist[LT_SDCARD] = "removable1";
853         m_subrootlist[LT_USBHOST] = "removable2";
854         m_subrootlist[LT_DOWNLOADS] = "downloads";
855         m_subrootlist[LT_DOCUMENTS] = "documents";
856         m_subrootlist[LT_SOUNDS] = "music";
857         m_subrootlist[LT_IMAGES] = "images";
858         m_subrootlist[LT_VIDEOS] = "videos";
859         m_subrootlist[LT_RINGTONES] = "ringtones";
860         return true;
861
862 }
863
864 void Manager::setupLocation(std::string location, const char* path)
865 {
866     if (!nodeExists(path)) {
867         try {
868             makePath(path, 0755);
869         }
870         catch (const Commons::Exception& ex) 
871         {
872             LoggerE("Exception: " << ex.GetMessage());
873             return;
874         }
875     }
876     m_locations[location] = IPath::create(path);
877 }
878
879 void Manager::Watcher::getCurrentStoargeStateForWatch()
880 {
881         std::string label("");
882         storage_type_e type;
883         storage_state_e state;
884         RootList::const_iterator it = m_rootlist.begin();
885         for (; it != m_rootlist.end(); ++it) {
886                 label = it->first;
887                 if (label.compare("") != 0) {
888                         StoragePropertiesPtr storageItem(new StorageProperties());
889
890                         if (storage_get_type(it->second, &type) != STORAGE_ERROR_NONE) {
891                                 Throw(Commons::PlatformException);
892                         }
893                         if (storage_get_state(it->second, &state) != STORAGE_ERROR_NONE) {
894                                 Throw(Commons::PlatformException);
895                         }
896
897                         storageItem->setId(it->second);
898                         storageItem->setLabel(it->first);
899                         storageItem->setType(static_cast<short>(type));
900                         storageItem->setState(static_cast<short>(state));
901
902                         EventStorageStateChangedPtr event(new EventStorageStateChanged());
903
904                         event->setResult(storageItem);
905                         emit(event);
906                 }
907         }
908 }
909
910 void Manager::Watcher::StorageStateHasChanged(int storage, storage_state_e state)
911 {
912         StoragePropertiesPtr storageItem(new StorageProperties());
913
914         std::string label;
915         storage_type_e type;
916
917         RootList::const_iterator it = m_rootlist.begin();
918         for (; it != m_rootlist.end(); ++it) {
919                 if (it->second == storage) {
920                         label = it->first;
921                         break;
922                 }
923         }
924
925         if (storage_get_type(storage, &type) != STORAGE_ERROR_NONE) {
926                 Throw(Commons::PlatformException);
927         }
928
929         if (label.compare("") != 0) {
930                 storageItem->setId(storage);
931                 storageItem->setLabel(label);
932                 storageItem->setType(static_cast<short>(type));
933                 storageItem->setState(static_cast<short>(state));
934
935                 EventStorageStateChangedPtr event(new EventStorageStateChanged());
936
937                 event->setResult(storageItem);
938                 emit(event);
939         }
940 }
941
942 }
943 }