Add facility for user configuration of Hamlib settings

A user  written JSON file may  be used to define  Hamlib configuration
settings, for  example to increase  the communications time out  to 10
seconds create the file hamlib_settings.json containing:

{
  "config": {
    "timeout": 10000
  }
}

in the application specific configuration  directory. On Windows it is
%LOCALAPPDATA%\<app-name>\,  on  Linux   it  is  ~/.config/<app-name>/
(~/.config/    before    Qt    5.5)    and   on    OS    X    it    is
~/Librry/Preferences/<app-name>   (~/Library/Preferences/  before   Qt
5.5).

git-svn-id: svn+ssh://svn.code.sf.net/p/wsjt/wsjt/branches/wsjtx@6213 ab8295b8-cf94-4d9e-aec4-7959e3be5d79
This commit is contained in:
Bill Somerville 2015-12-01 23:03:54 +00:00
parent a5b5321335
commit 540fbc87ea

View File

@ -4,6 +4,11 @@
#include <QByteArray>
#include <QString>
#include <QStandardPaths>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QDebug>
#include "moc_HamlibTransceiver.cpp"
@ -230,6 +235,52 @@ HamlibTransceiver::HamlibTransceiver (int model_number, TransceiverFactory::Para
// rig_->state.obj = this;
{
//
// user defined Hamlib settings
//
QFile settings_file {QStandardPaths::locate (
#if QT_VERSION >= 0x050500
QStandardPaths::AppConfigLocation
#else
QStandardPaths::ConfigLocation
#endif
, "hamlib_settings.json")};
if (settings_file.open (QFile::ReadOnly))
{
QJsonParseError status;
auto settings_doc = QJsonDocument::fromJson (settings_file.readAll (), &status);
if (status.error)
{
throw error {tr ("Hamlib settings file error: %1 at character offset %2")
.arg (status.errorString ()).arg (status.offset)};
}
if (!settings_doc.isObject ())
{
throw error {tr ("Hamlib settings file error: top level must be a JSON object")};
}
auto const& settings = settings_doc.object ();
//
// configuration settings
//
auto const& config = settings["config"];
if (!config.isUndefined ())
{
if (!config.isObject ())
{
throw error {tr ("Hamlib settings file error: config must be a JSON object")};
}
auto const& config_list = config.toObject ();
for (auto item = config_list.constBegin (); item != config_list.constEnd (); ++item)
{
set_conf (item.key ().toLocal8Bit ().constData ()
, (*item).toVariant ().toString ().toLocal8Bit ().constData ());
}
}
}
}
if (RIG_MODEL_DUMMY != model_number)
{
switch (rig_->caps->port_type)