tizen beta release
[framework/web/wrt-plugins-common.git] / src / modules / tizen / Filesystem / Node.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_NODE_H_
17 #define WRTDEVICEAPIS_FILESYSTEM_NODE_H_
18
19 #include <ctime>
20 #include <cstddef>
21 #include <sys/stat.h>
22 #include <set>
23 #include <dpl/shared_ptr.h>
24 #include <dpl/enable_shared_from_this.h>
25 #include <dpl/mutex.h>
26
27 #include <Filesystem/Enums.h>
28 #include <Filesystem/INode.h>
29 #include "Path.h"
30 #include "Stream.h"
31
32 namespace WrtDeviceApis {
33 namespace Filesystem {
34
35 class Node :
36     public Api::INode,
37     public DPL::EnableSharedFromThis<Node>
38 {
39   public:
40     static Api::INodePtr resolve(const Api::IPathPtr& path);
41
42   public:
43     Api::IPathPtr getPath() const;
44     Api::NodeType getType() const;
45     int getPermissions() const;
46     void setPermissions(int perms);
47     std::size_t getSize() const;
48     std::time_t getCreated() const;
49     std::time_t getModified() const;
50     Api::INodePtr getParent() const;
51     int getMode() const;
52
53     Api::INodePtr getChild(const Api::IPathPtr& path);
54     NameList getChildNames() const;
55     Api::NodeList getChildNodes(
56             const Api::NodeFilterPtr& filter = Api::NodeFilterPtr()) const;
57     void getChildNodes(const Api::EventListNodesPtr& event);
58     Api::INodePtr createChild(const Api::IPathPtr & path,
59                               Api::NodeType,
60                               int options);
61     Api::IStreamPtr open(int mode);
62     void open(const Api::EventOpenPtr& event);
63     void remove(int options);
64     void read(const Api::EventReadTextPtr& event);
65
66     void onStreamClose(const StreamPtr& stream);
67
68   private:
69     typedef std::set<StreamPtr> StreamList;
70
71   private:
72     static bool exists(const Api::IPathPtr& path);
73     static struct stat stat(const Api::IPathPtr& path);
74
75   private:
76     Node(const Api::IPathPtr& path, Api::NodeType type);
77
78     Node* createAsFile(const Api::IPathPtr& path,
79                        int options);
80     void createAsFileInternal(const Api::IPathPtr& path);
81
82     Node* createAsDirectory(const Api::IPathPtr& path,
83                             int options);
84     void createAsDirectoryInternal(const Api::IPathPtr& path);
85
86     void removeAsFile(const Api::IPathPtr& path);
87     void removeAsDirectory(const Api::IPathPtr& path,
88             bool recursive);
89
90     void OnRequestReceived(const Api::EventListNodesPtr& event);
91     void OnRequestReceived(const Api::EventOpenPtr& event);
92
93     void OnRequestReceived(const Api::EventReadTextPtr& event);
94     std::string readText();
95
96     std::string toUri(int widgetId) const;
97
98   private:
99     Api::IPathPtr m_path;
100     Api::NodeType m_type;
101     int m_perms;
102     StreamList m_openStreams;
103     mutable DPL::Mutex m_openStreamsMutex;
104 };
105
106 typedef DPL::SharedPtr<Node> NodePtr;
107
108 } // Filesystem
109 } // WrtDeviceApis
110
111 #endif /* WRTDEVICEAPIS_FILESYSTEM_NODE_H_ */