ArnLib  4.0.x
Active Registry Network
ArnItemB.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2022 Michael Wiklund.
2 // All rights reserved.
3 // Contact: arnlib@wiklunden.se
4 //
5 // This file is part of the ArnLib - Active Registry Network.
6 // Parts of ArnLib depend on Qt and/or other libraries that have their own
7 // licenses. Usage of these other libraries is subject to their respective
8 // license agreements.
9 //
10 // GNU Lesser General Public License Usage
11 // This file may be used under the terms of the GNU Lesser General Public
12 // License version 2.1 as published by the Free Software Foundation and
13 // appearing in the file LICENSE_LGPL.txt included in the packaging of this
14 // file. In addition, as a special exception, you may use the rights described
15 // in the Nokia Qt LGPL Exception version 1.1, included in the file
16 // LGPL_EXCEPTION.txt in this package.
17 //
18 // GNU General Public License Usage
19 // Alternatively, this file may be used under the terms of the GNU General Public
20 // License version 3.0 as published by the Free Software Foundation and appearing
21 // in the file LICENSE_GPL.txt included in the packaging of this file.
22 //
23 // Other Usage
24 // Alternatively, this file may be used in accordance with the terms and conditions
25 // contained in a signed written agreement between you and Michael Wiklund.
26 //
27 // This program is distributed in the hope that it will be useful, but WITHOUT ANY
28 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
29 // PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
30 //
31 
32 #include "ArnInc/ArnItemB.hpp"
33 #include "private/ArnItemB_p.hpp"
34 #include "private/ArnBasicItem_p.hpp"
35 #include "ArnInc/ArnEvent.hpp"
36 #include "ArnInc/ArnLib.hpp"
37 #include <QCoreApplication>
38 #include <QDebug>
39 
40 
41 
42 ArnItemBPrivate::ArnItemBPrivate()
43 {
44  _blockEcho = false;
45  _enableUpdNotify = true;
46  _enableSetValue = true;
47 }
48 
49 
50 ArnItemBPrivate::~ArnItemBPrivate()
51 {
52 }
53 
54 
55 void ArnItemB::init()
56 {
57  addHeritage( ArnCoreItem::Heritage::ItemB);
58 }
59 
60 
61 ArnItemB::ArnItemB( QObject *parent)
62  : QObject( parent)
63  , d_ptr( new ArnItemBPrivate)
64 {
65  init();
66 }
67 
68 
69 ArnItemB::ArnItemB( ArnItemBPrivate& dd, QObject* parent)
70  : QObject( parent)
71  , d_ptr( &dd)
72 {
73  init();
74 }
75 
76 
78 {
79  delete d_ptr;
80 }
81 
82 
83 bool ArnItemB::openWithFlags( const QString& path, Arn::LinkFlags linkFlags)
84 {
85  bool retVal = ArnBasicItem::openWithFlags( path, linkFlags);
86  modeUpdate( getMode(), true);
87  return retVal;
88 }
89 
90 
91 bool ArnItemB::open( const QString& path)
92 {
93  return openWithFlags( path, Arn::LinkFlags::CreateAllowed);
94 }
95 
96 
97 bool ArnItemB::openUuid( const QString& path)
98 {
99  QString uuidPath = Arn::uuidPath( path);
100  bool stat = open( uuidPath);
101 
102  return stat;
103 }
104 
105 
106 bool ArnItemB::openUuidPipe( const QString& path)
107 {
108  bool stat = openUuid( path);
109 
110  setPipeMode();
111  return stat;
112 }
113 
114 
115 bool ArnItemB::openFolder( const QString& path)
116 {
117  QString folderPath = path;
118  if (!Arn::isFolderPath( folderPath))
119  folderPath += '/';
120 
121  return open( folderPath);
122 }
123 
124 
125 void ArnItemB::modeUpdate( Arn::ObjectMode mode, bool isSetup)
126 {
127  Q_UNUSED(mode)
128  Q_UNUSED(isSetup)
129 }
130 
131 
132 void ArnItemB::setBlockEcho( bool blockEcho)
133 {
134  Q_D(ArnItemB);
135 
136  d->_blockEcho = blockEcho;
137 }
138 
139 
140 void ArnItemB::setEnableSetValue( bool enable)
141 {
142  Q_D(ArnItemB);
143 
144  d->_enableSetValue = enable;
145 }
146 
147 
148 void ArnItemB::setEnableUpdNotify( bool enable)
149 {
150  Q_D(ArnItemB);
151 
152  d->_enableUpdNotify = enable;
153 }
154 
155 
156 void ArnItemB::arnImport( const QByteArray& data, int ignoreSame)
157 {
158  ArnLinkHandle handle;
159  arnImport( data, ignoreSame, handle);
160 }
161 
162 
163 void ArnItemB::arnImport( const QByteArray& data, int ignoreSame, ArnLinkHandle& handleData)
164 {
165  Q_D(ArnItemB);
166 
167  if (!d->_enableSetValue) return;
168 
169  ArnBasicItem::arnImport( data, ignoreSame, handleData);
170 }
171 
172 
173 void ArnItemB::setValue( const ArnItemB& other, int ignoreSame)
174 {
175  Q_D(ArnItemB);
176 
177  if (!d->_enableSetValue) return;
178 
179  ArnBasicItem::setValue( other, ignoreSame);
180 }
181 
182 
183 void ArnItemB::setValue( int value, int ignoreSame)
184 {
185  Q_D(ArnItemB);
186 
187  if (!d->_enableSetValue) return;
188 
189  ArnBasicItem::setValue( value, ignoreSame);
190 }
191 
192 
193 void ArnItemB::setValue( ARNREAL value, int ignoreSame)
194 {
195  Q_D(ArnItemB);
196 
197  if (!d->_enableSetValue) return;
198 
199  ArnBasicItem::setValue( value, ignoreSame);
200 }
201 
202 
203 void ArnItemB::setValue( bool value, int ignoreSame)
204 {
205  Q_D(ArnItemB);
206 
207  if (!d->_enableSetValue) return;
208 
209  ArnBasicItem::setValue( value, ignoreSame);
210 }
211 
212 
213 void ArnItemB::setValue( const QString& value, int ignoreSame)
214 {
215  Q_D(ArnItemB);
216 
217  if (!d->_enableSetValue) return;
218 
219  ArnBasicItem::setValue( value, ignoreSame);
220 }
221 
222 
223 void ArnItemB::setValue( const QByteArray& value, int ignoreSame)
224 {
225  Q_D(ArnItemB);
226 
227  if (!d->_enableSetValue) return;
228 
229  ArnBasicItem::setValue( value, ignoreSame);
230 }
231 
232 
233 void ArnItemB::setValue( const QVariant& value, int ignoreSame)
234 {
235  Q_D(ArnItemB);
236 
237  if (!d->_enableSetValue) return;
238 
239  ArnBasicItem::setValue( value, ignoreSame);
240 }
241 
242 
243 void ArnItemB::setValue( const char* value, int ignoreSame)
244 {
245  Q_D(ArnItemB);
246 
247  if (!d->_enableSetValue) return;
248 
249  ArnBasicItem::setValue( value, ignoreSame);
250 }
251 
252 
253 void ArnItemB::setValue( uint value, int ignoreSame)
254 {
255  Q_D(ArnItemB);
256 
257  if (!d->_enableSetValue) return;
258 
259  ArnBasicItem::setValue( value, ignoreSame);
260 }
261 
262 
263 void ArnItemB::setValue( qint64 value, int ignoreSame)
264 {
265  Q_D(ArnItemB);
266 
267  if (!d->_enableSetValue) return;
268 
269  ArnBasicItem::setValue( value, ignoreSame);
270 }
271 
272 
273 void ArnItemB::setValue( quint64 value, int ignoreSame)
274 {
275  Q_D(ArnItemB);
276 
277  if (!d->_enableSetValue) return;
278 
279  ArnBasicItem::setValue( value, ignoreSame);
280 }
281 
282 
283 void ArnItemB::setBits( int mask, int value, int ignoreSame)
284 {
285  Q_D(ArnItemB);
286 
287  if (!d->_enableSetValue) return;
288 
289  ArnBasicItem::setBits( mask, value, ignoreSame);
290 }
291 
292 
293 void ArnItemB::addValue( int value)
294 {
295  Q_D(ArnItemB);
296 
297  if (!d->_enableSetValue) return;
298 
299  ArnBasicItem::addValue( value);
300 }
301 
302 
303 void ArnItemB::addValue( ARNREAL value)
304 {
305  Q_D(ArnItemB);
306 
307  if (!d->_enableSetValue) return;
308 
309  ArnBasicItem::addValue( value);
310 }
311 
312 
313 void ArnItemB::itemUpdated( const ArnLinkHandle& handleData, const QByteArray* value)
314 {
315  Q_UNUSED(handleData);
316  Q_UNUSED(value);
317 }
318 
319 
320 void ArnItemB::itemCreatedBelow( const QString& path)
321 {
322  Q_UNUSED(path);
323 }
324 
325 
326 void ArnItemB::itemModeChangedBelow( const QString& path, uint linkId, Arn::ObjectMode mode)
327 {
328  Q_UNUSED(path);
329  Q_UNUSED(linkId);
330  Q_UNUSED(mode);
331 }
332 
333 
334 void ArnItemB::setValue( const QByteArray& value, int ignoreSame, ArnLinkHandle& handleData)
335 {
336  Q_D(ArnItemB);
337 
338  if (!d->_enableSetValue) return;
339 
340  ArnBasicItem::setValue( value, ignoreSame, handleData);
341 }
342 
343 
344 void ArnItemB::arnEvent( QEvent* ev, bool isAlienThread)
345 {
346  if (!ev) return; // No event ...
347 
348  if (isAlienThread) {
349  QCoreApplication::postEvent( this, ev);
350  }
351  else {
352  customEvent( ev);
353  }
354 }
355 
356 
357 void ArnItemB::customEvent( QEvent* ev)
358 {
359  // This has to handle all demands of ArnEvent handling, i.e finish with ArnBasicItem::arnEvent(...)
360  // customEvent() in inherited class to ArnItemB must call this member at end.
361 
362  doEvent( ev);
363  ArnBasicItem::arnEvent( ev, false); // Will call selected ArnEventhandler
364 }
365 
366 
367 void ArnItemB::doEvent( QEvent* ev)
368 {
369  Q_D(ArnItemB);
370 
371  int evIdx = ev->type() - ArnEvent::baseType();
372  switch (evIdx) {
374  {
375  ArnEvValueChange* e = static_cast<ArnEvValueChange*>( ev);
376  // qDebug() << "ArnEvValueChange: inItemPath=" << path();
377  quint32 sendId = e->sendId();
378  quint32 id = itemId();
379  if (d->_blockEcho && sendId == id) // Update was initiated from this Item, it can be blocked ...
380  return;
381 
382  addIsOnlyEcho( sendId);
383 
384  if (d->_enableUpdNotify) {
385  const QByteArray* valueData = e->valueData();
386  if (valueData && !valueData->isEmpty()) {
387  QByteArray value = QByteArray::fromRawData( valueData->constData() + 1, valueData->size() - 1);
388  itemUpdated( e->handleData(), &value);
389  }
390  else
391  itemUpdated( e->handleData());
392  }
393  return;
394  }
396  {
397  ArnEvLinkCreate* e = static_cast<ArnEvLinkCreate*>( ev);
398  // qDebug() << "ArnEvLinkCreate: path=" << e->path() << " inItemPath=" << path();
399  if (!Arn::isFolderPath( e->path())) { // Only created leaves are passed on
400  itemCreatedBelow( e->path());
401  }
402  return;
403  }
405  {
406  ArnEvModeChange* e = static_cast<ArnEvModeChange*>( ev);
407  // qDebug() << "ArnEvModeChange: path=" << e->path() << " mode=" << e->mode()
408  // << " inItemPath=" << path();
409  if (isFolder())
410  itemModeChangedBelow( e->path(), e->linkId(),e->mode());
411  else
412  modeUpdate( e->mode());
413  return;
414  }
416  {
417  ArnEvRetired* e = static_cast<ArnEvRetired*>( ev);
418  if (!e->isBelow()) {
419  if (Arn::debugLinkDestroy) qDebug() << "Item arnLinkDestroyed: path=" << path();
420  emit arnLinkDestroyed();
421  }
422  return;
423  }
424  default:;
425  }
426 }
static int baseType(int setVal=-1)
Definition: ArnEvent.cpp:62
void setValue(const ArnBasicItem &other, int ignoreSame=Arn::SameValue::DefaultAction)
ArnBasicItem & setPipeMode()
Set general mode as Pipe for this Arn Data Object
const QByteArray * valueData() const
Definition: ArnEvent.hpp:250
bool debugLinkDestroy
Definition: ArnLib.cpp:40
const QString & path() const
Definition: ArnEvent.hpp:162
Arn::ObjectMode getMode() const
Use with care, link must be "referenced" before use, otherwise it might have been deleted...
uint linkId() const
Definition: ArnEvent.hpp:165
uint itemId() const
Get the id for this ArnItem.
virtual ~ArnItemB()
Definition: ArnItemB.cpp:77
Arn::ObjectMode mode() const
Definition: ArnEvent.hpp:168
const ArnLinkHandle & handleData() const
Definition: ArnEvent.hpp:253
int sendId() const
Definition: ArnEvent.hpp:247
void addValue(int value)
AtomicOp adds an integer to an Arn Data Object
void setBits(int mask, int value, int ignoreSame=Arn::SameValue::DefaultAction)
AtomicOp assign an integer to specified bits in an Arn Data Object
void arnImport(const QByteArray &data, int ignoreSame=Arn::SameValue::DefaultAction)
Import data to an Arn Data Object
bool open(const QString &path)
Open a handle to an Arn Data Object
Definition: ArnItemB.cpp:91
QString uuidPath(const QString &path)
Get a path to an Arn Object with a unique uuid name.
Definition: Arn.cpp:222
QString path(Arn::NameF nameF=Arn::NameF::EmptyOk) const
Path of the Arn Data Object
#define ARNREAL
Definition: Arn.hpp:44
bool isBelow() const
Definition: ArnEvent.hpp:213
Base class handle for an Arn Data Object.
Definition: ArnItemB.hpp:59
bool isFolder() const
uint linkId() const
Get the id for this Arn Data Object
void arnLinkDestroyed()
Signal emitted when the Arn Data Object is destroyed.
bool isFolderPath(const QString &path)
Test if path is a folder path
Definition: Arn.cpp:210
ArnItemB(QObject *parent=arnNullptr)
Standard constructor of a closed handle.
Definition: ArnItemB.cpp:61