Wittyshare  0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WsNode.cpp
Go to the documentation of this file.
1 
14 #include "WsNode.h"
15 #include <Include/WsGlobalConfig.h>
16 
19 {
20  m_size = 0;
21  m_relPath = "/";
22 }
23 
25 {
26 }
27 
28 WsNode::WsNode(const path& fullPath, const path& rootPath):
30 {
31  m_mutexSort = new boost::mutex();
32  try {
33  m_size = 0;
34  m_fullPath = fullPath;
35  string relp = fullPath.string().substr(rootPath.string().length());
36  path rp(relp);
37  if (rp.string() == "")
38  m_relPath = path("/");
39  else
40  m_relPath = rp;
41  } catch (std::out_of_range& exception) {
42  LOG(ERROR) << "WsNode::WsNode() : Exception raised: " << exception.what();
43  }
44 }
45 
46 
48 {
49  NodePtr spThis = shared_from_this();
50  f.get()->setParent(spThis);
51  try {
52  m_dirVect.push_back(f);
53  } catch (bad_alloc e) {
54  LOG(ERROR) << "WsNode::addChildDirectory() : " << e.what() << endl;
55  return ErrorCode::Failure;
56  }
57  return ErrorCode::Success;
58 }
59 
61 {
62  NodePtr spThis = shared_from_this();
63  f.get()->setParent(spThis);
64  try {
65  m_fileVect.push_back(f);
66  } catch (bad_alloc e) {
67  LOG(ERROR) << "WsNode::addChildFile() : " << e.what() << endl;
68  return ErrorCode::Failure;
69  }
70  return ErrorCode::Success;
71 }
72 
74 {
75  try {
76  m_combinedVect.push_back(n);
77  } catch (bad_alloc e) {
78  return ErrorCode::Failure;
79  }
80  return ErrorCode::Success;
81 }
82 
84 {
85  m_parent = n;
86 }
87 
89 {
90  NodePtr parent = m_parent.lock();
91  return parent;
92 }
93 
94 const path& WsNode::getPath()
95 {
96  return m_relPath;
97 }
98 
99 const path& WsNode::getFullPath()
100 {
101  return m_fullPath;
102 }
103 
104 string WsNode::getName(const bool noExt)
105 {
106  if ( noExt )
107  return this->getPath().stem().string();
108  return this->getPath().filename().string();
109 }
110 
111 const string WsNode::getDisplayName(const bool noExt)
112 {
113  if (m_properties.get()->get("global", "display_name", "") != "" )
114  return m_properties.get()->get("global", "display_name", "");
115  return getName(noExt);
116 }
117 
118 const vector<NodePtr> WsNode::getFiles()
119 {
120  sort();
121  return m_fileVect;
122 }
123 
124 const vector<NodePtr> WsNode::getDirectories()
125 {
126  sort();
127  return m_dirVect;
128 }
129 
130 const vector<NodePtr> WsNode::getAll()
131 {
132  if (m_combinedVect.size() == 0 && (m_fileVect.size() != 0 || m_dirVect.size() != 0)) {
133  this->sort();
134  }
135  return m_combinedVect;
136 }
137 
138 NodePtr WsNode::getNodeByName(const string& name)
139 {
140  /* Check if node exist in the vector list */
141  for (vector<NodePtr>::iterator it = m_dirVect.begin(); it != m_dirVect.end(); it++) {
142  if ((*it).get()->getName() == name) {
143  return (*it);
144  }
145  }
146  /* Check if node exist in the filelist */
147  for (vector<NodePtr>::iterator it2 = m_fileVect.begin(); it2 != m_fileVect.end(); it2++) {
148  if ((*it2).get()->getName() == name) {
149  return (*it2);
150  }
151  }
152  LOG(DEBUG) << "WsNode::getNodeByName() : Node not found " << name;
153  return NodePtr();
154 }
155 
157 {
158  string r = m_properties.get()->get("global", "in_menu", "false");
159  if (r == "true")
160  return true;
161  return false;
162 }
163 
164 
166 {
167  m_properties = properties;
168 }
169 
170 void WsNode::setSize(const uintmax_t& size)
171 {
172  m_size = size;
173 }
174 
176 {
177  return m_properties;
178 }
179 
180 string WsNode::getProperty(const std::string& section, const std::string& id, const std::string& def, bool recurse)
181 {
182  //TODO recursion ??
183  string prop = m_properties.get()->get(section, id, "");
184  if ( prop == "" && recurse) {
185  NodePtr parent = m_parent.lock();
186  if (parent.get() != 0) {
187  return parent.get()->getProperty(section, id, def, recurse);
188  } else return def;
189  } else return prop != "" ? prop : def;
190 }
191 
193 {
194  boost::mutex::scoped_lock lock(*m_mutexSort);
195  vector<NodePtr> all;
196  all.reserve(m_fileVect.size() + m_dirVect.size());
197  all.insert(all.end(), m_dirVect.begin(), m_dirVect.end());
198  all.insert(all.end(), m_fileVect.begin(), m_fileVect.end());
199  if (this->getProperty("global", "sort", "true") == "true") {
200  std::sort(m_fileVect.begin(), m_fileVect.end(), compareNodes());
201  std::sort(m_dirVect.begin(), m_dirVect.end(), compareNodes());
202  std::sort(all.begin(), all.end(), compareNodes());
203  }
204  m_combinedVect = all;
205 }
206 
207 NodePtr WsNode::eatPath(const string& path)
208 {
209  string name;
210  string sub;
211  //We are searching for this node
212  if (path == "/") {
213  NodePtr sThis = shared_from_this();
214  return sThis;
215  }
216  //remove trailing '/'
217  if (path[0] == '/')
218  name = path.substr(1);
219  else name = path;
220  //get the next chunck of the name
221  if (name.find("/") != string::npos) {
222  sub = name.substr(0, name.find_first_of("/"));
223  name = name.substr(name.find_first_of("/"));
224  } else {
225  sub = name;
226  name = "";
227  }
228  NodePtr cur = this->getNodeByName(sub);
229  if (cur.get() == 0) {
230  LOG(ERROR) << "WsNode::eatPath() : No such path " << path;
231  return NodePtr();
232  }
233  if (name == "" || name == "/") {
234  return cur;
235  }
236  return cur.get()->eatPath(name);
237 }
238 
239 const time_t& WsNode::getModifyDate()
240 {
241  return m_modifyTime;
242 }
243 
244 const time_t& WsNode::getCreateDate()
245 {
246  return m_createTime;
247 }
248 
249 void WsNode::setCreateDate(const time_t& t)
250 {
251  m_createTime = t;
252 }
253 
254 void WsNode::setModifyDate(const time_t& t)
255 {
256  m_modifyTime = t;
257 }
boost::shared_ptr< WsAbstractNode > NodePtr
WsNode()
Definition: WsNode.cpp:17
#define DEBUG
Definition: WsLogger.h:27
NodePtr eatPath(const std::string &path)
resolves the path and returns the node reprenseted by this path The path should be starting from the ...
Definition: WsNode.cpp:207
const string getDisplayName(const bool noExt=false)
returns the display name of the node
Definition: WsNode.cpp:111
const time_t & getCreateDate()
WsAbstractNode::getCreateDate()
Definition: WsNode.cpp:244
std::string getProperty(const std::string &section, const std::string &id, const std::string &def, bool recurse=false)
get a node property reprensented by section/id If no value found, the value def is returned ...
Definition: WsNode.cpp:180
const int Failure
const path & getFullPath()
returns the full path of the node
Definition: WsNode.cpp:99
Contains Abstract metods on nodes and variables.
void setModifyDate(const time_t &t)
sets the modification date of the file. This method does not change the actual modification date of t...
Definition: WsNode.cpp:254
int addChildNode(NodePtr f)
add a Node to the combined vect WsAbstractNode::m_combinedVect
Definition: WsNode.cpp:73
Node structure, must be inherited.
vector< NodePtr > m_combinedVect
boost::mutex * m_mutexSort
Definition: WsNode.h:228
const vector< NodePtr > getFiles()
returns the list of all the file contained in the directory represented by this
Definition: WsNode.cpp:118
void sort()
sort the nodes using the sort number or the name if not provided
Definition: WsNode.cpp:192
virtual ~WsNode()
Definition: WsNode.cpp:24
NodePropertiesPtr getProperties()
get the properties of the node If the properties are not set (no conf found, or node retrieved from s...
Definition: WsNode.cpp:175
void setProperties(NodePropertiesPtr properties)
set the properties of the node. The properties are a WsAbstractProperties object. ...
Definition: WsNode.cpp:165
#define LOG
Definition: WsLogger.h:22
const time_t & getModifyDate()
returns the modification date of the file reprenseted by the node
Definition: WsNode.cpp:239
vector< NodePtr > m_fileVect
const vector< NodePtr > getAll()
combine the vector of files and directories into a single one
Definition: WsNode.cpp:130
int addChildDirectory(NodePtr f)
adds a node as a child directory The Directory is added to the WsAbstractNode::m_dirVect ...
Definition: WsNode.cpp:47
NodePropertiesPtr m_properties
Definition: WsNode.h:218
bool getDisplayInMenu()
returns true if the item should be displayed in the menu. This value is set in the configuration file...
Definition: WsNode.cpp:156
void setParent(NodePtr n)
sets the parent of the node represented by this
Definition: WsNode.cpp:83
const path & getPath()
returns the relative path of the node
Definition: WsNode.cpp:94
NodePtr getParent()
returns the parent node of this
Definition: WsNode.cpp:88
boost::shared_ptr< WsNodeProperties > NodePropertiesPtr
string getName(const bool noExt=false)
returns the name of the node
Definition: WsNode.cpp:104
void setSize(const uintmax_t &size)
sets the file size in the node. Typically called by WsFileSystemTree when building the FileSystemTree...
Definition: WsNode.cpp:170
uintmax_t m_size
const vector< NodePtr > getDirectories()
returns the list of all the file contained in the file reprensented by this
Definition: WsNode.cpp:124
time_t m_modifyTime
Definition: WsNode.h:226
Compares two nodes and returns true or false depending on : If Sort order is asc in the node configur...
Definition: WsNode.h:38
vector< NodePtr > m_dirVect
void setCreateDate(const time_t &t)
sets the creation date of the file. This method does not change the actual creation date of the file ...
Definition: WsNode.cpp:249
const int Success
NodePtr getNodeByName(const std::string &name)
Searches for the NodePtr reprenting the filename.
Definition: WsNode.cpp:138
int addChildFile(NodePtr f)
adds a node as a child file The Directory is added to the WsAbstractNode::m_fileVect ...
Definition: WsNode.cpp:60
#define ERROR
Definition: WsLogger.h:42
time_t m_createTime
Definition: WsNode.h:222