Remoto - VFS: VFS_stream.cpp Source File
Remoto - VFS
VFS_stream.cpp
Go to the documentation of this file.
1 
2 #include "VFS.h"
3 #include "VFS_base/VFS_icons.h"
4 #include "VFS_stream.h"
5 
6 #include <QJsonObject>
7 
43 VFS_stream::VFS_stream(quint32 maxSize, bool newlineTrim)
44 : VFS_node()
45 , _maxSize(maxSize)
46 , _newlineTrim(newlineTrim)
47 {
48 }
49 
51 {
52 }
53 
61 QByteArray VFS_stream::icon()
62 {
63  return VFS_icons::get("stream");
64 }
65 
72 {
73  QString s = QString("%1 / %2").arg(_data.size()).arg(_maxSize);
74  return s;
75 }
76 
91 {
92  //QMutexLocker l(&_lock);
93 
94  QJsonObject o;
95  o["data"] = _data;
96  QJsonDocument d(o);
97 
98  r->_data = d;
99  r->_success = true;
100 }
101 
110 {
112 
113  r->_metadata["type"] = "textStream";
114 
115  r->_success = true;
116 }
117 
138 {
139  //QMutexLocker l(&_lock);
140 
141  //printf("STREAM WRITE: %p\n",r->_origin);
142 
143  QJsonObject o = r->_data.object();
144  if (o.contains("data"))
145  {
146  QJsonValue v = o["data"];
147 
148  if (v.isString())
149  {
150  QString data = v.toString();
151 
152  _data += data + (data.right(1)!="\n"? "\n" : "");
153 
154  if (_maxSize)
155  { if (_newlineTrim)
156  {
157  while( _data.size() > _maxSize)
158  {
159  int i = _data.indexOf("\n");
160  if (i > -1)
161  _data = _data.remove(0,i+1);
162  else
163  _data = "";
164  }
165  }
166  else
167  _data = _data.right(_maxSize);
168  }
169 
170  r->_success = true;
171 
172  emit diff(this,r);
173 
174  return;
175  }
176 
177  if (v.isNull()) //clear the stream
178  {
179  _data = "";
180 
181  r->_success = true;
182 
183  emit diff(this,r);
184 
185  return;
186  }
187  }
188 
189  //printf("BAD DATA!\n");
190  r->_success = false;
191  r->_reason = "Bad stream data... only strings and null are allowed, and a 'data' field must exist.";
192 }
193 
202 {
203  write(r);
204 }
205 
214 {
215  write(r);
216 
217  //VFS_node::applyDiff(r);
218 }
219 
226 {
227  return false;
228 }
229 
static char * get(QString which="")
Fetch an icon from the library.
Definition: VFS_icons.cpp:34
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
void diff(VFS_node *origin, VFS_request *t)
Emit a diff, which will trigger notifySubscribers() for a mounted node.
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
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
virtual void metadata(VFS_request *r)
Fetch the metadata for this node.
Definition: VFS_stream.cpp:109
bool _newlineTrim
Buffer will be truncated by bytes or newlines.
Definition: VFS_stream.h:25
virtual void read(VFS_request *r)
Read the contents of the buffer.
Definition: VFS_stream.cpp:90
virtual bool isContainer()
A VFS_stream node cannot contain children.
Definition: VFS_stream.cpp:225
virtual ~VFS_stream()
Definition: VFS_stream.cpp:50
QString _data
The actual buffer.
Definition: VFS_stream.h:26
Q_INVOKABLE VFS_stream(quint32 maxSize=0, bool newlineTrim=false)
Definition: VFS_stream.cpp:43
virtual void applyDiff(VFS_request *r)
Apply a diff to the buffer.
Definition: VFS_stream.cpp:213
virtual QByteArray icon()
The "stream" icon found in the VFS_icons library.
Definition: VFS_stream.cpp:61
virtual void write(VFS_request *r)
Write to the buffer.
Definition: VFS_stream.cpp:137
virtual void submit(VFS_request *r)
Submit data to the buffer.
Definition: VFS_stream.cpp:201
int _maxSize
The maximum size in bytes.
Definition: VFS_stream.h:24
virtual QString reportDetails()
Report the current stream size.
Definition: VFS_stream.cpp:71