Remoto - VFS: VFS_qrc.cpp Source File
Remoto - VFS
VFS_qrc.cpp
Go to the documentation of this file.
1 
2 #include <QDirIterator>
3 #include <QFile>
4 #include <QFileInfo>
5 #include <QDir>
6 
7 #include <QJsonDocument>
8 //#include <QJsonObject>
9 //#include <QJsonArray>
10 
11 #include "VFS.h"
12 #include "VFS_qrc.h"
13 #include "utilities/rutils.h"
14 #include "VFS_base/VFS_node_type.h"
15 
33 VFS_QRC::VFS_QRC(QString path, bool raw)
34 : VFS_node()
35 , _path(path)
36 , _raw(raw)
37 {
38  if (_path.at(0)!=':' || !QFile::exists(_path))
39  { VFS::ERROR( QString("VFS_QRC nodes require a QT valid resource path. Invalid path: '%1'").arg(_path) );
40  _path = "/XXX";
41  }
42 }
43 
45 {
46 }
47 
56 {
57  Q_UNUSED(r);
58 
59  //QMutexLocker l(&_lock);
60 
61  return this;
62 }
63 
65 {
66  //QMutexLocker l(&_lock);
67 
68  QString p = _path+"/"+r->_path;
69 
70  QFileInfo i(p);
71 
72  if (!i.exists())
73  {
74  r->_reason = "Path does not exist: "+r->_path;
75  r->_success = false;
76  return;
77  }
78 
79  if (i.isDir())
80  {
81  QDir dir(p);
82  QFileInfoList list = dir.entryInfoList( QDir::AllEntries | QDir::Readable | QDir::NoDotAndDotDot );
83 
84  QJsonObject o;
85  QFileInfo info;
86 
87  for (int i=0;i<list.length();i++)
88  {
89  info = list[i];
90  o[info.fileName()] = info.isDir();
91  }
92 
93  r->_data = QJsonDocument(o);
94  r->_success = true;
95  return;
96  }
97  else
98  {
99  QFile file(p);
100  if(file.open(QFile::ReadOnly))
101  {
102  QJsonDocument d;
103  QByteArray f = file.readAll();
104 
105  if (!_raw)
106  {
107  QJsonParseError e;
108  d = QJsonDocument::fromJson(f,&e);
109  if (e.error != QJsonParseError::NoError)
110  {
111  VFS::ERROR( r->_reason = e.errorString() );
112  return;
113  }
114  }
115  else
116  { QJsonObject o;
117  o["data"] = QString(f);
118  d.setObject(o);
119  }
120 
121  r->_data = d;
122  r->_success = true;
123  return;
124  }
125  else
126  {
127  VFS::ERROR( r->_reason = QString("Unable to open '%1'.").arg(p) );
128  return;
129  }
130  }
131 
132  r->_reason = "Unknown QRC error.";
133  r->_success = false;
134 }
135 
146 {
147  VFS::ERROR(QString("VFS_QRC nodes are read-only. '%1' is unwritable.").arg(r->_path));
148 
149  r->_success = false;
150 }
151 
162 {
163  QString p = _path+"/"+r->_path;
164 
166 
167  r->_metadata["type"] = VFS_node_type::getType(p);
168  r->_success = true;
169 }
170 
171 //virtual void report(VFS_request *r);
172 
181 {
182  VFS::ERROR(QString("VFS_QRC nodes are read-only. '%1' is unwritable.").arg(r->_path));
183 
184  r->_success = false;
185 }
186 
195 {
196  VFS::ERROR(QString("VFS_QRC nodes are read-only. '%1' is not deletable.").arg(r->_path));
197 
198  r->_success = false;
199 }
200 
209 {
210  QString p = _path+"/"+r->_path;
211  QFileInfo i(p);
212 
213  if (!i.exists())
214  {
215  r->_reason = QString("Can't subscribe to non-existent resource: '%1'").arg(p);
216  r->_success = false;
217  }
218  else
220 }
221 
230 {
231  //QMutexLocker _l(&_lock);
232 
233  QString p = _path+"/"+r->_path;
234 
235  QString n,m;
236  QFileInfo i(p);
237 
238  if (!i.exists())
239  {
240  VFS::WARN( r->_reason = QString("Illegal path: %1\n").arg(p) );
241  r->_success = false;
242  return;
243  }
244 
245  //QJsonArray a;
246  QJsonObject a;
247 
248  QDirIterator::IteratorFlags f = QDirIterator::FollowSymlinks;
249  //if (recursive) f |= QDirIterator::Subdirectories;
250 
251  QDirIterator it(p,f);
252  while (it.hasNext())
253  {
254  n = it.next();
255  m = it.fileName();
256  i = it.fileInfo();
257  if (m!="." && m!="..")
258  {
259  //a.push_back( m+(i.isDir()?"/":"") );
260  a[m] = i.isDir();
261  }
262  }
263 
264  if (r->_metadata.value("sequence").toBool(false))
266 
267  //r->_data = QJsonDocument(a);
268  r->_data.setObject(a);
269  r->_success = true;
270 }
271 
278 {
279  return true;
280 }
virtual void submit(VFS_request *r)
Attempt to submit.
Definition: VFS_qrc.cpp:180
bool _raw
Whether to fetch data in JSON mode or RAW mode.
Definition: VFS_qrc.h:33
virtual void subscribe(VFS_request *r)
Subscribe to a path.
Definition: VFS_qrc.cpp:208
virtual void ls(VFS_request *r)
List the contents of a path in this resource.
Definition: VFS_qrc.cpp:229
virtual VFS_node * find(VFS_request *r)
Definition: VFS_qrc.cpp:55
virtual void write(VFS_request *r)
Attempt to write.
Definition: VFS_qrc.cpp:145
QString _path
The Qt resource path prefix. Must start with ":/".
Definition: VFS_qrc.h:32
virtual void metadata(VFS_request *r)
Retrieve the metadata for a path.
Definition: VFS_qrc.cpp:161
Q_INVOKABLE VFS_QRC(QString path, bool raw=false)
Definition: VFS_qrc.cpp:33
virtual ~VFS_QRC()
Definition: VFS_qrc.cpp:44
virtual void rm(VFS_request *r)
Attempt to rm.
Definition: VFS_qrc.cpp:194
virtual void read(VFS_request *r)
Return the data contents of this node, or if it's a container call ls()
Definition: VFS_qrc.cpp:64
virtual bool isContainer()
The VFS_QRC class is a read-only container.
Definition: VFS_qrc.cpp:277
static QString getType(QString _path, QString _default="unknownType")
Fetch the type of a file, based on path/filename.ext.
VFS_node is the base class from which all other VFS_node classes derive.
Definition: VFS_node.h:143
virtual void metadata(VFS_request *r)
Fetch the metadata of this node.
Definition: VFS_node.cpp:797
virtual void subscribe(VFS_request *r)
Add an entry to this node's _subscription list.
Definition: VFS_node.cpp:1204
The base class for all requests between nodes.
Definition: VFS_node.h:54
QString _reason
if something (probably bad) happened, this is the reason
Definition: VFS_node.h:108
QString _path
the target path remnant... the remaining path element once the request has found its target
Definition: VFS_node.h:95
bool _success
if the request was successfully completed
Definition: VFS_node.h:107
QJsonDocument _data
the request payload
Definition: VFS_node.h:102
QJsonObject _metadata
the request payload
Definition: VFS_node.h:101
static void ERROR(QString message, int level=0, QString user="server")
Send a message to the VFS::_errors VFS_stream.
Definition: VFS.cpp:307
static void WARN(QString message, int level=0, QString user="server")
Send a message to the VFS::_warnings VFS_stream.
Definition: VFS.cpp:258
getter path
a getter DOCME
QJsonObject sequenceListing(QJsonObject l, QStringList types=sequenceTypes)
Given a list of filenames and a regex list, collapse the listing to sequences when possible.
Definition: rutils.cpp:183