| 
									
										
										
										
											2019-06-12 08:39:25 +02:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | // Copyright (C) 2019 Edouard Griffiths, F4EXB                                   //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // This program is free software; you can redistribute it and/or modify          //
 | 
					
						
							|  |  |  | // it under the terms of the GNU General Public License as published by          //
 | 
					
						
							|  |  |  | // the Free Software Foundation as version 3 of the License, or                  //
 | 
					
						
							|  |  |  | // (at your option) any later version.                                           //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful,               //
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of                //
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                  //
 | 
					
						
							|  |  |  | // GNU General Public License V3 for more details.                               //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU General Public License             //
 | 
					
						
							|  |  |  | // along with this program. If not, see <http://www.gnu.org/licenses/>.          //
 | 
					
						
							|  |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <QDataStream>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "util/simpleserializer.h"
 | 
					
						
							| 
									
										
										
										
											2019-06-12 18:50:53 +02:00
										 |  |  | #include "deviceuserargs.h"
 | 
					
						
							| 
									
										
										
										
											2019-06-12 08:39:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-13 17:28:21 +02:00
										 |  |  | QDataStream &operator<<(QDataStream &ds, const DeviceUserArgs::Args &inObj) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-12-15 00:19:50 +01:00
										 |  |  | 	ds << inObj.m_id << inObj.m_sequence << inObj.m_args << inObj.m_nonDiscoverable; | 
					
						
							| 
									
										
										
										
											2019-06-17 23:57:34 +02:00
										 |  |  |     return ds; | 
					
						
							| 
									
										
										
										
											2019-06-13 17:28:21 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | QDataStream &operator>>(QDataStream &ds, DeviceUserArgs::Args &outObj) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-12-15 00:19:50 +01:00
										 |  |  | 	ds >> outObj.m_id >> outObj.m_sequence >> outObj.m_args >> outObj.m_nonDiscoverable; | 
					
						
							| 
									
										
										
										
											2019-06-17 23:57:34 +02:00
										 |  |  |     return ds; | 
					
						
							| 
									
										
										
										
											2019-06-13 17:28:21 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-12 18:50:53 +02:00
										 |  |  | QByteArray DeviceUserArgs::serialize() const | 
					
						
							| 
									
										
										
										
											2019-06-12 08:39:25 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     SimpleSerializer s(1); | 
					
						
							|  |  |  |     QByteArray data; | 
					
						
							|  |  |  |     QDataStream *stream = new QDataStream(&data, QIODevice::WriteOnly); | 
					
						
							| 
									
										
										
										
											2019-06-12 18:50:53 +02:00
										 |  |  |     *stream << m_argsByDevice; | 
					
						
							| 
									
										
										
										
											2019-06-12 08:39:25 +02:00
										 |  |  |     s.writeBlob(1, data); | 
					
						
							|  |  |  |     return s.final(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-12 18:50:53 +02:00
										 |  |  | bool DeviceUserArgs::deserialize(const QByteArray& data) | 
					
						
							| 
									
										
										
										
											2019-06-12 08:39:25 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     SimpleDeserializer d(data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!d.isValid()) { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if(d.getVersion() == 1) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         QByteArray data; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         d.readBlob(1, &data); | 
					
						
							|  |  |  |         QDataStream readStream(&data, QIODevice::ReadOnly); | 
					
						
							| 
									
										
										
										
											2019-06-12 18:50:53 +02:00
										 |  |  |         readStream >> m_argsByDevice; | 
					
						
							| 
									
										
										
										
											2019-06-12 08:39:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-30 04:20:18 +02:00
										 |  |  | QString DeviceUserArgs::findUserArgs(const QString& id, int sequence) | 
					
						
							| 
									
										
										
										
											2019-06-13 17:28:21 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-06-30 04:20:18 +02:00
										 |  |  |     for (int i = 0; i < m_argsByDevice.size(); i++) | 
					
						
							| 
									
										
										
										
											2019-06-13 17:28:21 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-06-30 04:20:18 +02:00
										 |  |  |         if ((m_argsByDevice.at(i).m_id == id) && (m_argsByDevice.at(i).m_sequence == sequence)) { | 
					
						
							|  |  |  |             return m_argsByDevice.at(i).m_args; | 
					
						
							| 
									
										
										
										
											2019-06-13 17:28:21 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-30 04:20:18 +02:00
										 |  |  |     return ""; | 
					
						
							| 
									
										
										
										
											2019-06-13 18:43:06 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-15 00:19:50 +01:00
										 |  |  | void DeviceUserArgs::addDeviceArgs(const QString& id, int sequence, const QString& deviceArgs, bool nonDiscoverable) | 
					
						
							| 
									
										
										
										
											2019-06-12 08:39:25 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-06-30 04:20:18 +02:00
										 |  |  |     int i = 0; | 
					
						
							| 
									
										
										
										
											2019-06-13 17:28:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-30 04:20:18 +02:00
										 |  |  |     for (; i < m_argsByDevice.size(); i++) | 
					
						
							| 
									
										
										
										
											2019-06-13 17:28:21 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-06-30 04:20:18 +02:00
										 |  |  |         if ((m_argsByDevice.at(i).m_id == id) && (m_argsByDevice.at(i).m_sequence == sequence)) { | 
					
						
							| 
									
										
										
										
											2019-06-13 17:28:21 +02:00
										 |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-12 08:39:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-30 04:20:18 +02:00
										 |  |  |     if (i == m_argsByDevice.size()) { | 
					
						
							| 
									
										
										
										
											2019-12-15 00:19:50 +01:00
										 |  |  |         m_argsByDevice.push_back(Args(id, sequence, deviceArgs, nonDiscoverable)); | 
					
						
							| 
									
										
										
										
											2019-06-12 08:39:25 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-13 17:28:21 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-15 00:19:50 +01:00
										 |  |  | void DeviceUserArgs::addOrUpdateDeviceArgs(const QString& id, int sequence, const QString& deviceArgs, bool nonDiscoverable) | 
					
						
							| 
									
										
										
										
											2019-06-13 17:28:21 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-06-30 04:20:18 +02:00
										 |  |  |     int i = 0; | 
					
						
							| 
									
										
										
										
											2019-06-13 17:28:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-30 04:20:18 +02:00
										 |  |  |     for (; i < m_argsByDevice.size(); i++) | 
					
						
							| 
									
										
										
										
											2019-06-12 08:39:25 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-06-30 04:20:18 +02:00
										 |  |  |         if ((m_argsByDevice.at(i).m_id == id) && (m_argsByDevice.at(i).m_sequence == sequence)) { | 
					
						
							|  |  |  |             m_argsByDevice[i].m_args = deviceArgs; | 
					
						
							| 
									
										
										
										
											2019-06-13 17:28:21 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-30 04:20:18 +02:00
										 |  |  |     if (i == m_argsByDevice.size()) { | 
					
						
							| 
									
										
										
										
											2019-12-15 00:19:50 +01:00
										 |  |  |         m_argsByDevice.push_back(Args(id, sequence, deviceArgs, nonDiscoverable)); | 
					
						
							| 
									
										
										
										
											2019-06-13 17:28:21 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-06-12 08:39:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-15 00:19:50 +01:00
										 |  |  | void DeviceUserArgs::updateDeviceArgs(const QString& id, int sequence, const QString& deviceArgs, bool nonDiscoverable) | 
					
						
							| 
									
										
										
										
											2019-06-13 17:28:21 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-06-30 04:20:18 +02:00
										 |  |  |     int i = 0; | 
					
						
							| 
									
										
										
										
											2019-06-13 17:28:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-30 04:20:18 +02:00
										 |  |  |     for (; i < m_argsByDevice.size(); i++) | 
					
						
							| 
									
										
										
										
											2019-06-13 17:28:21 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-12-15 00:19:50 +01:00
										 |  |  |         if ((m_argsByDevice.at(i).m_id == id) && (m_argsByDevice.at(i).m_sequence == sequence)) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-06-30 04:20:18 +02:00
										 |  |  |             m_argsByDevice[i].m_args = deviceArgs; | 
					
						
							| 
									
										
										
										
											2019-12-15 00:19:50 +01:00
										 |  |  |             m_argsByDevice[i].m_nonDiscoverable = nonDiscoverable; | 
					
						
							| 
									
										
										
										
											2019-06-12 08:39:25 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-13 17:28:21 +02:00
										 |  |  | void DeviceUserArgs::deleteDeviceArgs(const QString& id, int sequence) | 
					
						
							| 
									
										
										
										
											2019-06-12 08:39:25 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-06-30 04:20:18 +02:00
										 |  |  |     int i = 0; | 
					
						
							| 
									
										
										
										
											2019-06-13 17:28:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-30 04:20:18 +02:00
										 |  |  |     for (; i < m_argsByDevice.size(); i++) | 
					
						
							| 
									
										
										
										
											2019-06-13 17:28:21 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-06-30 04:20:18 +02:00
										 |  |  |         if ((m_argsByDevice.at(i).m_id == id) && (m_argsByDevice.at(i).m_sequence == sequence)) | 
					
						
							| 
									
										
										
										
											2019-06-13 17:28:21 +02:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-06-30 04:20:18 +02:00
										 |  |  |             m_argsByDevice.takeAt(i); | 
					
						
							| 
									
										
										
										
											2019-06-13 17:28:21 +02:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-12 08:39:25 +02:00
										 |  |  | } |