| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | #include "util/simpleserializer.h"
 | 
					
						
							|  |  |  | #include "settings/preset.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-09 10:33:04 +02:00
										 |  |  | #include <QDebug>
 | 
					
						
							| 
									
										
										
										
											2015-06-07 03:30:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | Preset::Preset() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	resetToDefaults(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Preset::resetToDefaults() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	m_group = "default"; | 
					
						
							|  |  |  | 	m_description = "no name"; | 
					
						
							|  |  |  | 	m_centerFrequency = 0; | 
					
						
							|  |  |  | 	m_spectrumConfig.clear(); | 
					
						
							|  |  |  | 	m_layout.clear(); | 
					
						
							|  |  |  | 	m_spectrumConfig.clear(); | 
					
						
							|  |  |  | 	m_channelConfigs.clear(); | 
					
						
							| 
									
										
										
										
											2015-09-30 06:57:40 +02:00
										 |  |  | 	m_sourceId.clear(); | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	m_sourceConfig.clear(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | QByteArray Preset::serialize() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-10-02 02:22:56 +02:00
										 |  |  | 	qDebug("Preset::serialize: m_group: %s m_description: %s m_centerFrequency: %llu", | 
					
						
							| 
									
										
										
										
											2015-10-01 16:57:33 +02:00
										 |  |  | 			qPrintable(m_group), | 
					
						
							|  |  |  | 			qPrintable(m_description), | 
					
						
							|  |  |  | 			m_centerFrequency); | 
					
						
							| 
									
										
										
										
											2015-06-07 03:30:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	SimpleSerializer s(1); | 
					
						
							| 
									
										
										
										
											2015-10-01 16:57:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	s.writeString(1, m_group); | 
					
						
							|  |  |  | 	s.writeString(2, m_description); | 
					
						
							|  |  |  | 	s.writeU64(3, m_centerFrequency); | 
					
						
							| 
									
										
										
										
											2015-09-30 03:46:46 +02:00
										 |  |  | 	s.writeBlob(4, m_layout); | 
					
						
							| 
									
										
										
										
											2015-10-02 04:04:38 +02:00
										 |  |  | 	s.writeBlob(5, m_spectrumConfig); | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-01 16:57:33 +02:00
										 |  |  | 	s.writeS32(20, m_sourceConfigs.size()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (int i = 0; i < m_sourceConfigs.size(); i++) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		s.writeString(24 + i*4, m_sourceConfigs[i].m_sourceId); | 
					
						
							|  |  |  | 		s.writeString(25 + i*4, m_sourceConfigs[i].m_sourceSerial); | 
					
						
							|  |  |  | 		s.writeS32(26 + i*4, m_sourceConfigs[i].m_sourceSequence); | 
					
						
							|  |  |  | 		s.writeBlob(27 + i*4, m_sourceConfigs[i].m_config); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-02 04:04:38 +02:00
										 |  |  | 		qDebug("Preset::serialize:  source: id: %s, ser: %s, seq: %d", | 
					
						
							| 
									
										
										
										
											2015-10-02 02:22:56 +02:00
										 |  |  | 			qPrintable(m_sourceConfigs[i].m_sourceId), | 
					
						
							|  |  |  | 			qPrintable(m_sourceConfigs[i].m_sourceSerial), | 
					
						
							|  |  |  | 			m_sourceConfigs[i].m_sourceSequence); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-01 16:57:33 +02:00
										 |  |  | 		if (i >= (200-23)/4) // full!
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-06-07 03:30:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-02 04:04:38 +02:00
										 |  |  | 	s.writeS32(200, m_channelConfigs.size()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for(int i = 0; i < m_channelConfigs.size(); i++) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		qDebug("Preset::serialize:  channel: id: %s", qPrintable(m_channelConfigs[i].m_channel)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		s.writeString(201 + i * 2, m_channelConfigs[i].m_channel); | 
					
						
							|  |  |  | 		s.writeBlob(202 + i * 2, m_channelConfigs[i].m_config); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	return s.final(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool Preset::deserialize(const QByteArray& data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	SimpleDeserializer d(data); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-02 02:22:56 +02:00
										 |  |  | 	if (!d.isValid()) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 		resetToDefaults(); | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-02 02:22:56 +02:00
										 |  |  | 	if (d.getVersion() == 1) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 		d.readString(1, &m_group, "default"); | 
					
						
							|  |  |  | 		d.readString(2, &m_description, "no name"); | 
					
						
							|  |  |  | 		d.readU64(3, &m_centerFrequency, 0); | 
					
						
							| 
									
										
										
										
											2015-09-30 03:46:46 +02:00
										 |  |  | 		d.readBlob(4, &m_layout); | 
					
						
							| 
									
										
										
										
											2015-10-02 04:04:38 +02:00
										 |  |  | 		d.readBlob(5, &m_spectrumConfig); | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-01 16:57:33 +02:00
										 |  |  | 		qDebug("Preset::deserialize: m_group: %s m_description: %s m_centerFrequency: %llu", | 
					
						
							|  |  |  | 				qPrintable(m_group), | 
					
						
							|  |  |  | 				qPrintable(m_description), | 
					
						
							|  |  |  | 				m_centerFrequency); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		qint32 sourcesCount = 0; | 
					
						
							|  |  |  | 		d.readS32(20, &sourcesCount, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-02 02:22:56 +02:00
										 |  |  | 		if (sourcesCount >= (200-23)/4) // limit was hit!
 | 
					
						
							| 
									
										
										
										
											2015-10-01 16:57:33 +02:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2015-10-02 02:22:56 +02:00
										 |  |  | 			sourcesCount = ((200-23)/4) - 1; | 
					
						
							| 
									
										
										
										
											2015-10-01 16:57:33 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-02 04:04:38 +02:00
										 |  |  | 		m_sourceConfigs.clear(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for (int i = 0; i < sourcesCount; i++) | 
					
						
							| 
									
										
										
										
											2015-10-01 16:57:33 +02:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			QString sourceId, sourceSerial; | 
					
						
							|  |  |  | 			int sourceSequence; | 
					
						
							|  |  |  | 			QByteArray sourceConfig; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-02 02:22:56 +02:00
										 |  |  | 			d.readString(24 + i*4, &sourceId, ""); | 
					
						
							|  |  |  | 			d.readString(25 + i*4, &sourceSerial, ""); | 
					
						
							|  |  |  | 			d.readS32(26 + i*4, &sourceSequence, 0); | 
					
						
							|  |  |  | 			d.readBlob(27 + i*4, &sourceConfig); | 
					
						
							| 
									
										
										
										
											2015-10-01 16:57:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-02 02:22:56 +02:00
										 |  |  | 			if (!sourceId.isEmpty()) | 
					
						
							| 
									
										
										
										
											2015-10-01 16:57:33 +02:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2015-10-09 01:50:09 +02:00
										 |  |  | 				qDebug("Preset::deserialize:  source: id: %s, ser: %s, seq: %d", | 
					
						
							| 
									
										
										
										
											2015-10-02 02:22:56 +02:00
										 |  |  | 					qPrintable(sourceId), | 
					
						
							|  |  |  | 					qPrintable(sourceSerial), | 
					
						
							|  |  |  | 					sourceSequence); | 
					
						
							| 
									
										
										
										
											2015-10-01 16:57:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-02 02:22:56 +02:00
										 |  |  | 				m_sourceConfigs.append(SourceConfig(sourceId, sourceSerial, sourceSequence, sourceConfig)); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-10-01 16:57:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-02 04:04:38 +02:00
										 |  |  |         qint32 channelCount; | 
					
						
							|  |  |  |         d.readS32(200, &channelCount, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		m_channelConfigs.clear(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for (int i = 0; i < channelCount; i++) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			QString channel; | 
					
						
							|  |  |  | 			QByteArray config; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			d.readString(201 + i * 2, &channel, "unknown-channel"); | 
					
						
							|  |  |  | 			d.readBlob(202 + i * 2, &config); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			qDebug("Preset::deserialize:  channel: id: %s", qPrintable(channel)); | 
					
						
							|  |  |  | 			m_channelConfigs.append(ChannelConfig(channel, config)); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-10-09 01:50:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		return true; | 
					
						
							| 
									
										
										
										
											2015-10-01 16:57:33 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 		resetToDefaults(); | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-10-01 08:54:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | void Preset::addOrUpdateSourceConfig(const QString& sourceId, | 
					
						
							|  |  |  | 		const QString& sourceSerial, | 
					
						
							|  |  |  | 		int sourceSequence, | 
					
						
							|  |  |  | 		const QByteArray& config) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	SourceConfigs::iterator it = m_sourceConfigs.begin(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (; it != m_sourceConfigs.end(); ++it) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (it->m_sourceId == sourceId) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			if (sourceSerial.isNull() || sourceSerial.isEmpty()) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				if (it->m_sourceSequence == sourceSequence) | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				if (it->m_sourceSerial == sourceSerial) | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (it == m_sourceConfigs.end()) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-10-01 16:57:33 +02:00
										 |  |  | 		m_sourceConfigs.append(SourceConfig(sourceId, sourceSerial, sourceSequence, config)); | 
					
						
							| 
									
										
										
										
											2015-10-01 08:54:56 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		it->m_config = config; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const QByteArray* Preset::findBestSourceConfig(const QString& sourceId, | 
					
						
							|  |  |  | 		const QString& sourceSerial, | 
					
						
							| 
									
										
										
										
											2015-10-02 04:04:38 +02:00
										 |  |  | 		int sourceSequence) const | 
					
						
							| 
									
										
										
										
											2015-10-01 08:54:56 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	SourceConfigs::const_iterator it = m_sourceConfigs.begin(); | 
					
						
							|  |  |  | 	SourceConfigs::const_iterator itFirstOfKind = m_sourceConfigs.end(); | 
					
						
							|  |  |  | 	SourceConfigs::const_iterator itMatchSequence = m_sourceConfigs.end(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (; it != m_sourceConfigs.end(); ++it) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (it->m_sourceId == sourceId) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			if (itFirstOfKind == m_sourceConfigs.end()) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				itFirstOfKind = it; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (sourceSerial.isNull() || sourceSerial.isEmpty()) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				if (it->m_sourceSequence == sourceSequence) | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				if (it->m_sourceSerial == sourceSerial) | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				else if(it->m_sourceSequence == sourceSequence) | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					itMatchSequence = it; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (it == m_sourceConfigs.end()) // no exact match
 | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (itMatchSequence != m_sourceConfigs.end()) // match sequence ?
 | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2015-10-03 10:21:48 +02:00
										 |  |  | 			qDebug("Preset::findBestSourceConfig: sequence matched: id: %s seq: %d", qPrintable(itMatchSequence->m_sourceId), itMatchSequence->m_sourceSequence); | 
					
						
							| 
									
										
										
										
											2015-10-01 08:54:56 +02:00
										 |  |  | 			return &(itMatchSequence->m_config); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else if (itFirstOfKind != m_sourceConfigs.end()) // match source type ?
 | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2015-10-03 10:21:48 +02:00
										 |  |  | 			qDebug("Preset::findBestSourceConfig: first of kind matched: id: %s", qPrintable(itFirstOfKind->m_sourceId)); | 
					
						
							| 
									
										
										
										
											2015-10-01 08:54:56 +02:00
										 |  |  | 			return &(itFirstOfKind->m_config); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else // definitely not found !
 | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2015-10-01 16:57:33 +02:00
										 |  |  | 			qDebug("Preset::findBestSourceConfig: no match"); | 
					
						
							| 
									
										
										
										
											2015-10-01 08:54:56 +02:00
										 |  |  | 			return 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else // exact match
 | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-10-02 02:22:56 +02:00
										 |  |  | 		qDebug("Preset::findBestSourceConfig: serial matched (exact): id: %s ser: %s", qPrintable(it->m_sourceId), qPrintable(it->m_sourceSerial)); | 
					
						
							| 
									
										
										
										
											2015-10-01 08:54:56 +02:00
										 |  |  | 		return &(it->m_config); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |