diff --git a/sdrbase/settings/mainsettings.cpp b/sdrbase/settings/mainsettings.cpp index 66062a442..d4f395c1f 100644 --- a/sdrbase/settings/mainsettings.cpp +++ b/sdrbase/settings/mainsettings.cpp @@ -139,6 +139,13 @@ void MainSettings::save() const s.setValue("hwDeviceUserArgs", qCompress(m_hardwareDeviceUserArgs.serialize()).toBase64()); } +void MainSettings::initialize() +{ + resetToDefaults(); + clearCommands(); + clearPresets(); +} + void MainSettings::resetToDefaults() { m_preferences.resetToDefaults(); @@ -150,10 +157,15 @@ Preset* MainSettings::newPreset(const QString& group, const QString& description Preset* preset = new Preset(); preset->setGroup(group); preset->setDescription(description); - m_presets.append(preset); + addPreset(preset); return preset; } +void MainSettings::addPreset(Preset *preset) +{ + m_presets.append(preset); +} + void MainSettings::deletePreset(const Preset* preset) { m_presets.removeAll((Preset*)preset); @@ -214,6 +226,15 @@ const Preset* MainSettings::getPreset(const QString& groupName, quint64 centerFr return 0; } +void MainSettings::clearPresets() +{ + foreach (Preset *preset, m_presets) { + delete preset; + } + + m_presets.clear(); +} + void MainSettings::addCommand(Command *command) { m_commands.append(command); @@ -273,3 +294,12 @@ const Command* MainSettings::getCommand(const QString& groupName, const QString& return 0; } + +void MainSettings::clearCommands() +{ + foreach (Command *command, m_commands) { + delete command; + } + + m_commands.clear(); +} diff --git a/sdrbase/settings/mainsettings.h b/sdrbase/settings/mainsettings.h index fb0ba306b..6d3bf251b 100644 --- a/sdrbase/settings/mainsettings.h +++ b/sdrbase/settings/mainsettings.h @@ -20,12 +20,15 @@ public: void save() const; void resetToDefaults(); + void initialize(); QString getFileLocation() const; int getFileFormat() const; //!< see QSettings::Format for the values const Preferences& getPreferences() const { return m_preferences; } + void setPreferences(const Preferences& preferences) { m_preferences = preferences; } Preset* newPreset(const QString& group, const QString& description); + void addPreset(Preset *preset); void deletePreset(const Preset* preset); int getPresetCount() const { return m_presets.count(); } const Preset* getPreset(int index) const { return m_presets[index]; } @@ -33,6 +36,7 @@ public: void sortPresets(); void renamePresetGroup(const QString& oldGroupName, const QString& newGroupName); void deletePresetGroup(const QString& groupName); + void clearPresets(); void addCommand(Command *command); void deleteCommand(const Command* command); @@ -42,6 +46,7 @@ public: void sortCommands(); void renameCommandGroup(const QString& oldGroupName, const QString& newGroupName); void deleteCommandGroup(const QString& groupName); + void clearCommands(); const Preset& getWorkingPresetConst() const { return m_workingPreset; } Preset* getWorkingPreset() { return &m_workingPreset; } diff --git a/sdrbase/settings/preferences.h b/sdrbase/settings/preferences.h index cf8d72b96..598e96e22 100644 --- a/sdrbase/settings/preferences.h +++ b/sdrbase/settings/preferences.h @@ -13,14 +13,14 @@ public: QByteArray serialize() const; bool deserialize(const QByteArray& data); - void setSourceDevice(const QString& value) { m_sourceDevice= value; } + void setSourceDevice(const QString& value) { m_sourceDevice = value; } const QString& getSourceDevice() const { return m_sourceDevice; } void setSourceIndex(const int value) { m_sourceIndex = value; } int getSourceIndex() const { return m_sourceIndex; } void setAudioType(const QString& value) { m_audioType = value; } const QString& getAudioType() const { return m_audioType; } - void setAudioDevice(const QString& value) { m_audioDevice= value; } + void setAudioDevice(const QString& value) { m_audioDevice = value; } const QString& getAudioDevice() const { return m_audioDevice; } void setLatitude(float latitude) { m_latitude = latitude; } diff --git a/sdrbase/webapi/webapiadapterbase.cpp b/sdrbase/webapi/webapiadapterbase.cpp index 438ac9625..b7fda89ca 100644 --- a/sdrbase/webapi/webapiadapterbase.cpp +++ b/sdrbase/webapi/webapiadapterbase.cpp @@ -51,6 +51,38 @@ void WebAPIAdapterBase::webapiFormatPreferences( apiPreferences->setFileMinLogLevel((int) preferences.getFileMinLogLevel()); } +void WebAPIAdapterBase::webapiInitConfig( + MainSettings& mainSettings +) +{ + mainSettings.initialize(); +} + +void WebAPIAdapterBase::webapiUpdatePreferences( + SWGSDRangel::SWGPreferences *apiPreferences, + Preferences& preferences +) +{ + if (apiPreferences->getSourceDevice()) { + preferences.setSourceDevice(*apiPreferences->getSourceDevice()); + } + preferences.setSourceIndex(apiPreferences->getSourceIndex()); + if (apiPreferences->getAudioType()) { + preferences.setAudioType(*apiPreferences->getAudioType()); + } + if (apiPreferences->getAudioDevice()) { + preferences.setAudioDevice(*apiPreferences->getAudioDevice()); + } + preferences.setLatitude(apiPreferences->getLatitude()); + preferences.setLongitude(apiPreferences->getLongitude()); + preferences.setConsoleMinLogLevel((QtMsgType) apiPreferences->getConsoleMinLogLevel()); + preferences.setUseLogFile(apiPreferences->getUseLogFile() != 0); + if (apiPreferences->getLogFileName()) { + preferences.setLogFileName(*apiPreferences->getLogFileName()); + } + preferences.setFileMinLogLevel((QtMsgType) apiPreferences->getFileMinLogLevel()); +} + void WebAPIAdapterBase::webapiFormatPreset( SWGSDRangel::SWGPreset *apiPreset, const Preset& preset diff --git a/sdrbase/webapi/webapiadapterbase.h b/sdrbase/webapi/webapiadapterbase.h index d25dff6d4..39eda866d 100644 --- a/sdrbase/webapi/webapiadapterbase.h +++ b/sdrbase/webapi/webapiadapterbase.h @@ -27,6 +27,7 @@ #include "SWGCommand.h" #include "settings/preferences.h" #include "settings/preset.h" +#include "settings/mainsettings.h" #include "commands/command.h" class PluginManager; @@ -56,6 +57,13 @@ public: SWGSDRangel::SWGCommand *apiCommand, const Command& command ); + static void webapiInitConfig( + MainSettings& mainSettings + ); + static void webapiUpdatePreferences( + SWGSDRangel::SWGPreferences *apiPreferences, + Preferences& preferences + ); private: class WebAPIChannelAdapters diff --git a/sdrbase/webapi/webapiadapterinterface.h b/sdrbase/webapi/webapiadapterinterface.h index d56d15ad8..bdb22f5e5 100644 --- a/sdrbase/webapi/webapiadapterinterface.h +++ b/sdrbase/webapi/webapiadapterinterface.h @@ -33,6 +33,7 @@ namespace SWGSDRangel class SWGInstanceConfigResponse; class SWGInstanceDevicesResponse; class SWGInstanceChannelsResponse; + class SWGPreferences; class SWGLoggingInfo; class SWGAudioDevices; class SWGAudioInputDevice; @@ -104,6 +105,27 @@ public: return 501; } + /** + * Handler of /sdrangel/config (PUT) swagger/sdrangel/code/html2/index.html#api-Default-instanceSummary + * returns the Http status code (default 501: not implemented) + */ + virtual void instanceConfigInit() + { + } + + virtual int instanceConfigPutPreferences( + SWGSDRangel::SWGPreferences& preferences, + const QStringList& preferenceKeys, + SWGSDRangel::SWGErrorResponse& error + ) + { + (void) preferences; + (void) preferenceKeys; + error.init(); + *error.getMessage() = QString("Function not implemented"); + return 501; + } + /** * Handler of /sdrangel/devices (GET) swagger/sdrangel/code/html2/index.html#api-Default-instanceDevices * returns the Http status code (default 501: not implemented) diff --git a/sdrbase/webapi/webapirequestmapper.cpp b/sdrbase/webapi/webapirequestmapper.cpp index 668e44738..290aae920 100644 --- a/sdrbase/webapi/webapirequestmapper.cpp +++ b/sdrbase/webapi/webapirequestmapper.cpp @@ -217,6 +217,8 @@ void WebAPIRequestMapper::instanceSummaryService(qtwebapp::HttpRequest& request, void WebAPIRequestMapper::instanceConfigService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response) { + SWGSDRangel::SWGInstanceConfigResponse query; + SWGSDRangel::SWGSuccessResponse normalResponse; SWGSDRangel::SWGErrorResponse errorResponse; response.setHeader("Content-Type", "application/json"); response.setHeader("Access-Control-Allow-Origin", "*"); @@ -233,6 +235,30 @@ void WebAPIRequestMapper::instanceConfigService(qtwebapp::HttpRequest& request, response.write(errorResponse.asJson().toUtf8()); } } + else if (request.getMethod() == "PUT") + { + QString jsonStr = request.getBody(); + QJsonObject jsonObject; + + if (parseJsonBody(jsonStr, jsonObject, response)) + { + m_adapter->instanceConfigInit(); + } + else + { + response.setStatus(400,"Invalid JSON format"); + errorResponse.init(); + *errorResponse.getMessage() = "Invalid JSON format"; + response.write(errorResponse.asJson().toUtf8()); + } + } + else + { + response.setStatus(405,"Invalid HTTP method"); + errorResponse.init(); + *errorResponse.getMessage() = "Invalid HTTP method"; + response.write(errorResponse.asJson().toUtf8()); + } } void WebAPIRequestMapper::instanceDevicesService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response) @@ -2809,96 +2835,96 @@ void WebAPIRequestMapper::appendSettingsArrayKeys( void WebAPIRequestMapper::resetDeviceSettings(SWGSDRangel::SWGDeviceSettings& deviceSettings) { deviceSettings.cleanup(); - deviceSettings.setDeviceHwType(0); - deviceSettings.setAirspySettings(0); - deviceSettings.setAirspyHfSettings(0); - deviceSettings.setBladeRf1InputSettings(0); - deviceSettings.setBladeRf1OutputSettings(0); - deviceSettings.setFcdProPlusSettings(0); - deviceSettings.setFcdProSettings(0); - deviceSettings.setFileInputSettings(0); - deviceSettings.setHackRfInputSettings(0); - deviceSettings.setHackRfOutputSettings(0); - deviceSettings.setLimeSdrInputSettings(0); - deviceSettings.setLimeSdrOutputSettings(0); - deviceSettings.setPerseusSettings(0); - deviceSettings.setPlutoSdrInputSettings(0); - deviceSettings.setPlutoSdrOutputSettings(0); - deviceSettings.setRtlSdrSettings(0); - deviceSettings.setRemoteOutputSettings(0); - deviceSettings.setRemoteInputSettings(0); - deviceSettings.setSdrPlaySettings(0); - deviceSettings.setTestSourceSettings(0); + deviceSettings.setDeviceHwType(nullptr); + deviceSettings.setAirspySettings(nullptr); + deviceSettings.setAirspyHfSettings(nullptr); + deviceSettings.setBladeRf1InputSettings(nullptr); + deviceSettings.setBladeRf1OutputSettings(nullptr); + deviceSettings.setFcdProPlusSettings(nullptr); + deviceSettings.setFcdProSettings(nullptr); + deviceSettings.setFileInputSettings(nullptr); + deviceSettings.setHackRfInputSettings(nullptr); + deviceSettings.setHackRfOutputSettings(nullptr); + deviceSettings.setLimeSdrInputSettings(nullptr); + deviceSettings.setLimeSdrOutputSettings(nullptr); + deviceSettings.setPerseusSettings(nullptr); + deviceSettings.setPlutoSdrInputSettings(nullptr); + deviceSettings.setPlutoSdrOutputSettings(nullptr); + deviceSettings.setRtlSdrSettings(nullptr); + deviceSettings.setRemoteOutputSettings(nullptr); + deviceSettings.setRemoteInputSettings(nullptr); + deviceSettings.setSdrPlaySettings(nullptr); + deviceSettings.setTestSourceSettings(nullptr); } void WebAPIRequestMapper::resetDeviceReport(SWGSDRangel::SWGDeviceReport& deviceReport) { deviceReport.cleanup(); - deviceReport.setDeviceHwType(0); - deviceReport.setAirspyHfReport(0); - deviceReport.setAirspyReport(0); - deviceReport.setFileInputReport(0); - deviceReport.setLimeSdrInputReport(0); - deviceReport.setLimeSdrOutputReport(0); - deviceReport.setPerseusReport(0); - deviceReport.setPlutoSdrInputReport(0); - deviceReport.setPlutoSdrOutputReport(0); - deviceReport.setRtlSdrReport(0); - deviceReport.setRemoteOutputReport(0); - deviceReport.setRemoteInputReport(0); - deviceReport.setSdrPlayReport(0); + deviceReport.setDeviceHwType(nullptr); + deviceReport.setAirspyHfReport(nullptr); + deviceReport.setAirspyReport(nullptr); + deviceReport.setFileInputReport(nullptr); + deviceReport.setLimeSdrInputReport(nullptr); + deviceReport.setLimeSdrOutputReport(nullptr); + deviceReport.setPerseusReport(nullptr); + deviceReport.setPlutoSdrInputReport(nullptr); + deviceReport.setPlutoSdrOutputReport(nullptr); + deviceReport.setRtlSdrReport(nullptr); + deviceReport.setRemoteOutputReport(nullptr); + deviceReport.setRemoteInputReport(nullptr); + deviceReport.setSdrPlayReport(nullptr); } void WebAPIRequestMapper::resetChannelSettings(SWGSDRangel::SWGChannelSettings& channelSettings) { channelSettings.cleanup(); - channelSettings.setChannelType(0); - channelSettings.setAmDemodSettings(0); - channelSettings.setAmModSettings(0); - channelSettings.setAtvModSettings(0); - channelSettings.setBfmDemodSettings(0); - channelSettings.setDsdDemodSettings(0); - channelSettings.setNfmDemodSettings(0); - channelSettings.setNfmModSettings(0); - channelSettings.setRemoteSinkSettings(0); - channelSettings.setRemoteSourceSettings(0); - channelSettings.setSsbDemodSettings(0); - channelSettings.setSsbModSettings(0); - channelSettings.setUdpSourceSettings(0); - channelSettings.setUdpSinkSettings(0); - channelSettings.setWfmDemodSettings(0); - channelSettings.setWfmModSettings(0); + channelSettings.setChannelType(nullptr); + channelSettings.setAmDemodSettings(nullptr); + channelSettings.setAmModSettings(nullptr); + channelSettings.setAtvModSettings(nullptr); + channelSettings.setBfmDemodSettings(nullptr); + channelSettings.setDsdDemodSettings(nullptr); + channelSettings.setNfmDemodSettings(nullptr); + channelSettings.setNfmModSettings(nullptr); + channelSettings.setRemoteSinkSettings(nullptr); + channelSettings.setRemoteSourceSettings(nullptr); + channelSettings.setSsbDemodSettings(nullptr); + channelSettings.setSsbModSettings(nullptr); + channelSettings.setUdpSourceSettings(nullptr); + channelSettings.setUdpSinkSettings(nullptr); + channelSettings.setWfmDemodSettings(nullptr); + channelSettings.setWfmModSettings(nullptr); } void WebAPIRequestMapper::resetChannelReport(SWGSDRangel::SWGChannelReport& channelReport) { channelReport.cleanup(); - channelReport.setChannelType(0); - channelReport.setAmDemodReport(0); - channelReport.setAmModReport(0); - channelReport.setAtvModReport(0); - channelReport.setBfmDemodReport(0); - channelReport.setDsdDemodReport(0); - channelReport.setNfmDemodReport(0); - channelReport.setNfmModReport(0); - channelReport.setRemoteSourceReport(0); - channelReport.setSsbDemodReport(0); - channelReport.setSsbModReport(0); - channelReport.setUdpSourceReport(0); - channelReport.setUdpSinkReport(0); - channelReport.setWfmDemodReport(0); - channelReport.setWfmModReport(0); + channelReport.setChannelType(nullptr); + channelReport.setAmDemodReport(nullptr); + channelReport.setAmModReport(nullptr); + channelReport.setAtvModReport(nullptr); + channelReport.setBfmDemodReport(nullptr); + channelReport.setDsdDemodReport(nullptr); + channelReport.setNfmDemodReport(nullptr); + channelReport.setNfmModReport(nullptr); + channelReport.setRemoteSourceReport(nullptr); + channelReport.setSsbDemodReport(nullptr); + channelReport.setSsbModReport(nullptr); + channelReport.setUdpSourceReport(nullptr); + channelReport.setUdpSinkReport(nullptr); + channelReport.setWfmDemodReport(nullptr); + channelReport.setWfmModReport(nullptr); } void WebAPIRequestMapper::resetAudioInputDevice(SWGSDRangel::SWGAudioInputDevice& audioInputDevice) { audioInputDevice.cleanup(); - audioInputDevice.setName(0); + audioInputDevice.setName(nullptr); } void WebAPIRequestMapper::resetAudioOutputDevice(SWGSDRangel::SWGAudioOutputDevice& audioOutputDevice) { audioOutputDevice.cleanup(); - audioOutputDevice.setName(0); - audioOutputDevice.setUdpAddress(0); + audioOutputDevice.setName(nullptr); + audioOutputDevice.setUdpAddress(nullptr); } diff --git a/sdrgui/webapi/webapiadaptergui.cpp b/sdrgui/webapi/webapiadaptergui.cpp index 78b366559..82fd10142 100644 --- a/sdrgui/webapi/webapiadaptergui.cpp +++ b/sdrgui/webapi/webapiadaptergui.cpp @@ -151,6 +151,13 @@ int WebAPIAdapterGUI::instanceConfigGet( return 200; } +void WebAPIAdapterGUI::instanceConfigInit() +{ + WebAPIAdapterBase webAPIAdapterBase; + webAPIAdapterBase.setPluginManager(m_mainWindow.getPluginManager()); + webAPIAdapterBase.webapiInitConfig(m_mainWindow.m_settings); +} + int WebAPIAdapterGUI::instanceDevices( int direction, SWGSDRangel::SWGInstanceDevicesResponse& response, diff --git a/sdrgui/webapi/webapiadaptergui.h b/sdrgui/webapi/webapiadaptergui.h index 32c68fb21..4df9be61f 100644 --- a/sdrgui/webapi/webapiadaptergui.h +++ b/sdrgui/webapi/webapiadaptergui.h @@ -45,6 +45,8 @@ public: SWGSDRangel::SWGInstanceConfigResponse& response, SWGSDRangel::SWGErrorResponse& error); + virtual void instanceConfigInit(); + virtual int instanceDevices( int direction, SWGSDRangel::SWGInstanceDevicesResponse& response,