Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_wearable / modules / tizen / Filesystem / Manager.h
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 #ifndef WRTDEVICEAPIS_FILESYSTEM_MANAGER_H_
17 #define WRTDEVICEAPIS_FILESYSTEM_MANAGER_H_
18
19 #include <map>
20 #include <vector>
21 #include <cstddef>
22 #include <dpl/shared_ptr.h>
23 #include <Filesystem/IManager.h>
24 #include <Filesystem/IPath.h>
25 #include "Node.h"
26 #include "Path.h"
27
28 namespace WrtDeviceApis {
29 namespace Filesystem {
30 class Manager : public Api::IManager
31 {
32   public:
33     /**
34      * Checks if file exists.
35      * @param real file path.
36      * @return true when file exists, false otherwise.
37      * @throw PlatformException If unable to validate if file exists.
38      */
39     static bool fileExists(const std::string &file);
40
41   public:
42     Manager();
43     ~Manager();
44
45     Api::IPathPtr getBasePath() const;
46
47     Api::IPathPtr getLocationPath(Api::LocationType type) const;
48
49     Api::LocationPaths getLocationPaths() const;
50
51     Api::LocationTypes getLocations() const;
52
53     void getNode(const Api::EventResolvePtr& event);
54
55     std::size_t getMaxPathLength() const;
56
57     void copy(const Api::EventCopyPtr& event);
58
59     void move(const Api::EventMovePtr& event);
60
61     void remove(const Api::EventRemovePtr& event);
62
63     void find(const Api::EventFindPtr& event);
64
65     /**
66      * Finds files in the filesystem whish match specified filters.
67      * @param path Starting path.
68      * @param filter Filters @see FindFilters.
69      * @param result List with found files.
70      * @param event DPL event, if set then search process can be cancelled
71      * @remarks For filter FF_NAME value is treated as perl like regex pattern.
72      *          Use @Regex::escpae() if you want to treat it as normal string.
73      */
74     void find(const Api::IPathPtr& path,
75               const Api::FiltersMap& filter,
76               Api::NodeList& result,
77               const Api::EventFindPtr& event = Api::EventFindPtr(NULL));
78
79     /**
80      * @warning Operates on specified as it is - it DOES NOT take its real path.
81      *          This is because all filesystem operation will probably change
82      *          their approach to real path and will allow to refer to nodes
83      *          only by virtual roots.
84      */
85     bool access(const Api::IPathPtr& path,
86                 int accessType) const;
87
88     void addOpenedNode(const Api::INodePtr& node);
89     void removeOpenedNode(const Api::INodePtr& node);
90     bool checkIfOpened(const Api::IPathPtr& path) const;
91
92   protected:
93     bool matchFilters(const std::string& name,
94                       const struct stat& info,
95                       const Api::FiltersMap& filter);
96
97     void OnRequestReceived(const Api::EventResolvePtr& event);
98     void OnRequestReceived(const Api::EventCopyPtr& event);
99     void OnRequestReceived(const Api::EventMovePtr& event);
100     void OnRequestReceived(const Api::EventRemovePtr& event);
101     void OnRequestReceived(const Api::EventFindPtr& event);
102
103   private:
104     typedef std::map<Api::LocationType, Api::IPathPtr> Locations;
105
106   private:
107     /**
108      * Initializes statics of Manager class.
109      * Used only internally.
110      * @return True on success, false otherwsie.
111      */
112     static bool init();
113
114     static void setupLocation(Api::LocationType location,
115                               const char* path);
116
117     void copyElement(const std::string &src,
118                      const std::string &dest,
119                      bool recursive = true) const;
120
121     /**
122      * Check two paths to copy/move. Checks if they are not the same and
123      * required permissions.
124      * @param src
125      * @param dest
126      */
127     void checkPaths(Api::IPathPtr &src, Api::IPathPtr &dest);
128     bool pathExists(const std::string &path);
129
130   private:
131     static Locations m_locations; ///< Paths to default locations.
132     static const std::size_t m_maxPathLength; ///< Maximum path length.
133     static Api::NodeList m_openedNodes; ///< List of nodes that has opened
134                                         // streams.
135 };
136 } // Filesystem
137 } // WrtDeviceApis
138
139 #endif // WRTDEVICEAPIS_FILESYSTEM_MANAGER_H_