SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
deviceuserargs.cpp
Go to the documentation of this file.
1 // Copyright (C) 2019 Edouard Griffiths, F4EXB //
3 // //
4 // This program is free software; you can redistribute it and/or modify //
5 // it under the terms of the GNU General Public License as published by //
6 // the Free Software Foundation as version 3 of the License, or //
7 // (at your option) any later version. //
8 // //
9 // This program is distributed in the hope that it will be useful, //
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
12 // GNU General Public License V3 for more details. //
13 // //
14 // You should have received a copy of the GNU General Public License //
15 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
17 
18 #include <QDataStream>
19 
20 #include "util/simpleserializer.h"
21 #include "deviceuserargs.h"
22 
23 QDataStream &operator<<(QDataStream &ds, const DeviceUserArgs::Args &inObj)
24 {
25  ds << inObj.m_id << inObj.m_sequence << inObj.m_args;
26  return ds;
27 }
28 
29 QDataStream &operator>>(QDataStream &ds, DeviceUserArgs::Args &outObj)
30 {
31  ds >> outObj.m_id >> outObj.m_sequence >> outObj.m_args;
32  return ds;
33 }
34 
35 QByteArray DeviceUserArgs::serialize() const
36 {
37  SimpleSerializer s(1);
38  QByteArray data;
39  QDataStream *stream = new QDataStream(&data, QIODevice::WriteOnly);
40  *stream << m_argsByDevice;
41  s.writeBlob(1, data);
42  return s.final();
43 }
44 
45 bool DeviceUserArgs::deserialize(const QByteArray& data)
46 {
47  SimpleDeserializer d(data);
48 
49  if (!d.isValid()) {
50  return false;
51  }
52 
53  if(d.getVersion() == 1)
54  {
55  QByteArray data;
56 
57  d.readBlob(1, &data);
58  QDataStream readStream(&data, QIODevice::ReadOnly);
59  readStream >> m_argsByDevice;
60 
61  return true;
62  }
63  else
64  {
65  return false;
66  }
67 }
68 
69 QString DeviceUserArgs::findUserArgs(const QString& id, int sequence)
70 {
71  for (int i = 0; i < m_argsByDevice.size(); i++)
72  {
73  if ((m_argsByDevice.at(i).m_id == id) && (m_argsByDevice.at(i).m_sequence == sequence)) {
74  return m_argsByDevice.at(i).m_args;
75  }
76  }
77 
78  return "";
79 }
80 
81 void DeviceUserArgs::addDeviceArgs(const QString& id, int sequence, const QString& deviceArgs)
82 {
83  int i = 0;
84 
85  for (; i < m_argsByDevice.size(); i++)
86  {
87  if ((m_argsByDevice.at(i).m_id == id) && (m_argsByDevice.at(i).m_sequence == sequence)) {
88  break;
89  }
90  }
91 
92  if (i == m_argsByDevice.size()) {
93  m_argsByDevice.push_back(Args(id, sequence, deviceArgs));
94  }
95 }
96 
97 void DeviceUserArgs::addOrUpdateDeviceArgs(const QString& id, int sequence, const QString& deviceArgs)
98 {
99  int i = 0;
100 
101  for (; i < m_argsByDevice.size(); i++)
102  {
103  if ((m_argsByDevice.at(i).m_id == id) && (m_argsByDevice.at(i).m_sequence == sequence)) {
104  m_argsByDevice[i].m_args = deviceArgs;
105  }
106  }
107 
108  if (i == m_argsByDevice.size()) {
109  m_argsByDevice.push_back(Args(id, sequence, deviceArgs));
110  }
111 }
112 
113 void DeviceUserArgs::updateDeviceArgs(const QString& id, int sequence, const QString& deviceArgs)
114 {
115  int i = 0;
116 
117  for (; i < m_argsByDevice.size(); i++)
118  {
119  if ((m_argsByDevice.at(i).m_id == id) && (m_argsByDevice.at(i).m_sequence == sequence)) {
120  m_argsByDevice[i].m_args = deviceArgs;
121  }
122  }
123 }
124 
125 void DeviceUserArgs::deleteDeviceArgs(const QString& id, int sequence)
126 {
127  int i = 0;
128 
129  for (; i < m_argsByDevice.size(); i++)
130  {
131  if ((m_argsByDevice.at(i).m_id == id) && (m_argsByDevice.at(i).m_sequence == sequence))
132  {
133  m_argsByDevice.takeAt(i);
134  return;
135  }
136  }
137 }
void deleteDeviceArgs(const QString &id, int sequence)
QDataStream & operator<<(QDataStream &ds, const DeviceUserArgs::Args &inObj)
void addOrUpdateDeviceArgs(const QString &id, int sequence, const QString &args)
Add or update if it exists for same reference.
void writeBlob(quint32 id, const QByteArray &value)
QDataStream & operator>>(QDataStream &ds, DeviceUserArgs::Args &outObj)
QByteArray serialize() const
QList< Args > m_argsByDevice
args corresponding to a device
bool isValid() const
bool readBlob(quint32 id, QByteArray *result, const QByteArray &def=QByteArray()) const
void addDeviceArgs(const QString &id, int sequence, const QString &args)
Will not add if it exists for same reference.
int32_t i
Definition: decimators.h:244
quint32 getVersion() const
bool deserialize(const QByteArray &data)
void updateDeviceArgs(const QString &id, int sequence, const QString &args)
Will not update if reference does not exist.
const QByteArray & final()
QString findUserArgs(const QString &id, int sequence)