tizen 2.3.1 release
[framework/web/wearable/wrt-plugins-tizen.git] / src / Filesystem / Manager.h
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 #ifndef TIZENAPIS_FILESYSTEMMANAGER_H_
20 #define TIZENAPIS_FILESYSTEMMANAGER_H_
21
22 #include <string>
23 #include <map>
24 #include <vector>
25 #include <cstddef>
26 #include <map>
27 #include <vector>
28 #include <string>
29 #include <cstddef>
30 #include <Commons/EventReceiver.h>
31 #include <storage/storage.h>
32 #include "EventResolve.h"
33 #include "EventGetStorage.h"
34 #include "EventListStorages.h"
35 #include "EventStorageStateChanged.h"
36 #include "EventCopy.h"
37 #include "EventMove.h"
38 #include "EventRemove.h"
39 #include "Enums.h"
40 #include "Path.h"
41 #include "Node.h"
42 #include "FilesystemErrorUtil.h"
43 #include <SecurityAccessor.h>
44
45 namespace DeviceAPI {
46 namespace Filesystem {
47
48 typedef std::vector<PathPtr> LocationPaths;
49 typedef std::vector<LocationType> LocationTypes;
50 typedef std::map<std::string, PathPtr> StorageList;
51
52 class Manager : public Common::SecurityAccessor
53 {
54 public:
55     //! \brief Singleton interface
56     //! \return reference to Manager
57     static Manager& getInstance();
58     //! \brief Checks if file exists.
59     //! @param real file path.
60     //! @return true when file exists, false otherwise.
61     //! @throw PlatformException If unable to validate if file exists.
62     static bool fileExists(const std::string& file);
63     static bool getSupportedDeviceCB(int storage,
64                                      storage_type_e type,
65                                      storage_state_e state,
66                                      const char* path,
67                                      void* user_data);
68
69     static bool getSupportedVirtualPathCB(int storage,
70                                      storage_type_e type,
71                                      storage_state_e state,
72                                      const char* path,
73                                      void* user_data);
74     ~Manager();
75
76     //! \brief Gets base path.
77     //! @return Valid path or empty shared pointer.
78     PathPtr getBasePath() const;
79
80     StorageList getStorageList() const;
81     //! \brief Gets path for specified location type.
82     //! @param type Location type @see WrtPlugins::Api::Filesystem::LocationType.
83     //! @return Valid path or empty shared pointer.
84     PathPtr getLocationPath(
85         LocationType type) const;
86     //! \brief Gets paths to default locations.
87     //! @return Paths to predefined virtual locations.
88     LocationPaths getLocationPaths() const;
89     //! \biref Gets locations supported by platform.
90     //! @return Supported locations.
91     LocationTypes getLocations() const;
92     //! \brief Gets filesystem node.
93     //! @param event Get node event @see Api::Filesystem::EventGetNode.
94     //! @remarks Asynchronous.
95     void getNode(const EventResolvePtr& event);
96
97     void getStorage(const EventGetStoragePtr& event);
98
99     static void listStorages(const EventListStoragesPtr& event);
100
101     //! \brief Gets maximum length of filesystem path.
102     //! @return Maximum path length.
103     std::size_t getMaxPathLength() const;
104     //! \brief Copies node to specified destination.
105     //! @param event Copy node event @see Api::Filesystem::EventCopy.
106     //! @remarks Asynchronous.
107     void copy(EventCopyPtr event);
108     //! \brief Moves node to specified destination.
109     //! @param event Move node event @see Api::Filesystem::EventMove.
110     //! @remarks Asynchronous.
111     void move(const EventMovePtr& event);
112     //! \brief Removes node.
113     //! @param event Remove node event @see Api::Filesystem::EventRemove.
114     //! @remarks Asynchronous.
115     void remove(EventRemovePtr removeData);
116     //! \brief Finds nodes.
117     //! @param event Find nodes event @see Api::Filesystem::EventFind.
118     //! @remarks Asynchronous.
119
120     long addStorageStateChangeListener(EventStorageStateChangedPtr callback);
121     void removeStorageStateChangeListener(long id);
122     static void storageStateChangedCB(int storage,
123                                       storage_state_e state,
124                                       void* /*user_data*/);
125     static void getCurrentStorageStateForWatch();
126     static void StorageStateHasChanged(
127             int storage,
128             storage_state_e state);
129     static void emitStorageChange(StoragePropertiesPtr aStorageChange);
130
131     void addWidgetStorage(const std::string& key, const std::string& value);
132
133     JSContextRef getContext() const;
134     void setContext(JSContextRef context);
135
136 protected:
137
138     bool matchFilters(const std::string& name,
139                       const struct stat& info,
140                       const FiltersMap& filter);
141
142     void addLocalStorage(std::string label,
143                          std::vector<StoragePropertiesPtr>& storageList);
144
145 private:
146     //! Private constructor for Singleton pattern
147     Manager();
148     //! Private copy constructor for Noncopyable pattern
149     Manager(const Manager &);
150     //! Private copy operator for Noncopyable pattern
151     const Manager &operator=(const Manager &);
152
153     typedef std::map<std::string, PathPtr> Locations;
154     typedef std::map<std::string, int> RootList;
155     typedef std::map<LocationType, std::string> SubRootList;
156     static void setupLocation(std::string location, const char* path);
157
158     static void copyElement(const std::string& src,
159                             const std::string& dest,
160                             bool recursive = true);
161
162     static void removeBegin(EventRemovePtr aEvent);
163     static void removeWorker(EventRemovePtr aEvent);
164     static void removeEnd(EventRemovePtr aEvent);
165     static void getStorageBegin(EventGetStoragePtr aEvent);
166     static void getStorageEnd(EventGetStoragePtr aEvent);
167     static void listStoragesEnd(EventListStoragesPtr aEvent);
168     static void copyBegin(EventCopyPtr aEvent);
169     static void copyWorker(EventCopyPtr aEvent);
170     static void copyEnd(EventCopyPtr aEvent);
171     static void resolveWorker(EventResolvePtr aEvent);
172     static void resolveEnd(EventResolvePtr aEvent);
173     static void moveWorker(EventMovePtr aEvent);
174     static void moveEnd(EventMovePtr aEvent);
175     static void storageChangeEnd(EventStorageStateChangedPtr aEvent);
176
177     typedef std::map<long, EventStorageStateChangedPtr> WatcherContainer;
178     static WatcherContainer m_watchers;
179     static RootList m_rootlist;
180     static SubRootList m_subrootlist;
181     static Locations m_locations; ///< Paths to default locations.
182     static const std::size_t m_maxPathLength; ///< Maximum path length.
183
184     JSContextRef m_context;
185
186 };
187
188 } // namespace Filesystem
189 } // namespace DeviceAPI
190
191 #endif // TIZENAPIS_FILESYSTEMMANAGER_H_
192