| 
									
										
										
										
											2015-10-02 04:04:38 +02:00
										 |  |  | #include <QSettings>
 | 
					
						
							|  |  |  | #include <QStringList>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "settings/mainsettings.h"
 | 
					
						
							| 
									
										
										
										
											2018-01-04 11:11:53 +01:00
										 |  |  | #include "commands/command.h"
 | 
					
						
							| 
									
										
										
										
											2015-10-02 04:04:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 18:08:38 +01:00
										 |  |  | MainSettings::MainSettings() : m_audioDeviceManager(0) | 
					
						
							| 
									
										
										
										
											2015-10-02 04:04:38 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	resetToDefaults(); | 
					
						
							| 
									
										
										
										
											2019-01-17 11:17:28 +01:00
										 |  |  |     qInfo("MainSettings::MainSettings: settings file: format: %d location: %s", getFileFormat(), qPrintable(getFileLocation())); | 
					
						
							| 
									
										
										
										
											2015-10-02 04:04:38 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MainSettings::~MainSettings() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	for(int i = 0; i < m_presets.count(); ++i) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		delete m_presets[i]; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-01-04 11:11:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for(int i = 0; i < m_commands.count(); ++i) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         delete m_commands[i]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-10-02 04:04:38 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-17 11:22:38 +01:00
										 |  |  | QString MainSettings::getFileLocation() const | 
					
						
							| 
									
										
										
										
											2019-01-17 11:17:28 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     QSettings s; | 
					
						
							|  |  |  |     return s.fileName(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int MainSettings::getFileFormat() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     QSettings s; | 
					
						
							|  |  |  |     return (int) s.format(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-02 04:04:38 +02:00
										 |  |  | void MainSettings::load() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QSettings s; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	m_preferences.deserialize(qUncompress(QByteArray::fromBase64(s.value("preferences").toByteArray()))); | 
					
						
							|  |  |  | 	m_workingPreset.deserialize(qUncompress(QByteArray::fromBase64(s.value("current").toByteArray()))); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 18:08:38 +01:00
										 |  |  | 	if (m_audioDeviceManager) | 
					
						
							| 
									
										
										
										
											2017-01-06 18:56:46 +01:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-03-23 18:08:38 +01:00
										 |  |  | 	    m_audioDeviceManager->deserialize(qUncompress(QByteArray::fromBase64(s.value("audio").toByteArray()))); | 
					
						
							| 
									
										
										
										
											2017-01-06 18:56:46 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-02 04:04:38 +02:00
										 |  |  | 	QStringList groups = s.childGroups(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for(int i = 0; i < groups.size(); ++i) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-01-04 11:11:53 +01:00
										 |  |  | 		if (groups[i].startsWith("preset")) | 
					
						
							| 
									
										
										
										
											2015-10-02 04:04:38 +02:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			s.beginGroup(groups[i]); | 
					
						
							|  |  |  | 			Preset* preset = new Preset; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if(preset->deserialize(qUncompress(QByteArray::fromBase64(s.value("data").toByteArray())))) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				m_presets.append(preset); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				delete preset; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			s.endGroup(); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-01-04 11:11:53 +01:00
										 |  |  | 		else if (groups[i].startsWith("command")) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             s.beginGroup(groups[i]); | 
					
						
							|  |  |  |             Command* command = new Command; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if(command->deserialize(qUncompress(QByteArray::fromBase64(s.value("data").toByteArray())))) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 m_commands.append(command); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 delete command; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             s.endGroup(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-10-02 04:04:38 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void MainSettings::save() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QSettings s; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	s.setValue("preferences", qCompress(m_preferences.serialize()).toBase64()); | 
					
						
							|  |  |  | 	s.setValue("current", qCompress(m_workingPreset.serialize()).toBase64()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 18:08:38 +01:00
										 |  |  | 	if (m_audioDeviceManager) | 
					
						
							| 
									
										
										
										
											2017-01-06 18:56:46 +01:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-03-23 18:08:38 +01:00
										 |  |  | 	    s.setValue("audio", qCompress(m_audioDeviceManager->serialize()).toBase64()); | 
					
						
							| 
									
										
										
										
											2017-01-06 18:56:46 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-02 04:04:38 +02:00
										 |  |  | 	QStringList groups = s.childGroups(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for(int i = 0; i < groups.size(); ++i) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-01-04 11:11:53 +01:00
										 |  |  | 		if ((groups[i].startsWith("preset")) || (groups[i].startsWith("command"))) | 
					
						
							| 
									
										
										
										
											2015-10-02 04:04:38 +02:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			s.remove(groups[i]); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-04 11:11:53 +01:00
										 |  |  | 	for (int i = 0; i < m_presets.count(); ++i) | 
					
						
							| 
									
										
										
										
											2015-10-02 04:04:38 +02:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		QString group = QString("preset-%1").arg(i + 1); | 
					
						
							|  |  |  | 		s.beginGroup(group); | 
					
						
							|  |  |  | 		s.setValue("data", qCompress(m_presets[i]->serialize()).toBase64()); | 
					
						
							|  |  |  | 		s.endGroup(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-01-04 11:11:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (int i = 0; i < m_commands.count(); ++i) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         QString group = QString("command-%1").arg(i + 1); | 
					
						
							|  |  |  |         s.beginGroup(group); | 
					
						
							|  |  |  |         s.setValue("data", qCompress(m_commands[i]->serialize()).toBase64()); | 
					
						
							|  |  |  |         s.endGroup(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-10-02 04:04:38 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void MainSettings::resetToDefaults() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	m_preferences.resetToDefaults(); | 
					
						
							|  |  |  | 	m_workingPreset.resetToDefaults(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Preset* MainSettings::newPreset(const QString& group, const QString& description) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	Preset* preset = new Preset(); | 
					
						
							|  |  |  | 	preset->setGroup(group); | 
					
						
							|  |  |  | 	preset->setDescription(description); | 
					
						
							|  |  |  | 	m_presets.append(preset); | 
					
						
							|  |  |  | 	return preset; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void MainSettings::deletePreset(const Preset* preset) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	m_presets.removeAll((Preset*)preset); | 
					
						
							|  |  |  | 	delete (Preset*)preset; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-09-11 18:58:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-06 20:02:08 +01:00
										 |  |  | void MainSettings::deletePresetGroup(const QString& groupName) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Presets::iterator it = m_presets.begin(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while (it != m_presets.end()) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ((*it)->getGroup() == groupName) { | 
					
						
							|  |  |  |             it = m_presets.erase(it); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             ++it; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-11 18:58:40 +02:00
										 |  |  | void MainSettings::sortPresets() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     qSort(m_presets.begin(), m_presets.end(), Preset::presetCompare); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-11-25 16:08:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-05 11:45:20 +01:00
										 |  |  | void MainSettings::renamePresetGroup(const QString& oldGroupName, const QString& newGroupName) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int nbPresets = getPresetCount(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (int i = 0; i < nbPresets; i++) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (getPreset(i)->getGroup() == oldGroupName) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             Preset *preset_mod = const_cast<Preset*>(getPreset(i)); | 
					
						
							|  |  |  |             preset_mod->setGroup(newGroupName); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-09 01:12:40 +02:00
										 |  |  | const Preset* MainSettings::getPreset(const QString& groupName, quint64 centerFrequency, const QString& description, const QString& type) const | 
					
						
							| 
									
										
										
										
											2017-11-25 16:08:18 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     int nbPresets = getPresetCount(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (int i = 0; i < nbPresets; i++) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ((getPreset(i)->getGroup() == groupName) && | 
					
						
							|  |  |  |             (getPreset(i)->getCenterFrequency() == centerFrequency) && | 
					
						
							|  |  |  |             (getPreset(i)->getDescription() == description)) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-05-09 01:12:40 +02:00
										 |  |  |             if (type == "R" && getPreset(i)->isSourcePreset()) { | 
					
						
							|  |  |  |                 return getPreset(i); | 
					
						
							|  |  |  |             } else if (type == "T" && !getPreset(i)->isSourcePreset()) { | 
					
						
							|  |  |  |                 return getPreset(i); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-11-25 16:08:18 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-01-04 11:11:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | void MainSettings::addCommand(Command *command) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_commands.append(command); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void MainSettings::deleteCommand(const Command* command) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_commands.removeAll((Command*)command); | 
					
						
							|  |  |  |     delete (Command*)command; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-06 20:02:08 +01:00
										 |  |  | void MainSettings::deleteCommandGroup(const QString& groupName) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Commands::iterator it = m_commands.begin(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while (it != m_commands.end()) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ((*it)->getGroup() == groupName) { | 
					
						
							|  |  |  |             it = m_commands.erase(it); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             ++it; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-04 11:11:53 +01:00
										 |  |  | void MainSettings::sortCommands() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     qSort(m_commands.begin(), m_commands.end(), Command::commandCompare); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-05 11:45:20 +01:00
										 |  |  | void MainSettings::renameCommandGroup(const QString& oldGroupName, const QString& newGroupName) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int nbCommands = getCommandCount(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (int i = 0; i < nbCommands; i++) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (getCommand(i)->getGroup() == oldGroupName) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             Command *command_mod = const_cast<Command*>(getCommand(i)); | 
					
						
							|  |  |  |             command_mod->setGroup(newGroupName); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-04 11:11:53 +01:00
										 |  |  | const Command* MainSettings::getCommand(const QString& groupName, const QString& description) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int nbCommands = getCommandCount(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (int i = 0; i < nbCommands; i++) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ((getCommand(i)->getGroup() == groupName) && | 
					
						
							|  |  |  |             (getCommand(i)->getDescription() == description)) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             return getCommand(i); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } |