From 992d4b4b8d3f3a630a2cf00d593ee4ce4dfa7ce8 Mon Sep 17 00:00:00 2001 From: Christoph Berg Date: Thu, 9 Jan 2025 22:12:06 +0100 Subject: New upstream version 2.7.0~rc8+repack --- logbook/AD1CCty.cpp | 926 ++++++++++++++-------------- logbook/AD1CCty.hpp | 120 ++-- logbook/Multiplier.cpp | 88 +-- logbook/Multiplier.hpp | 60 +- logbook/WorkedBefore.cpp | 1426 +++++++++++++++++++++---------------------- logbook/WorkedBefore.hpp | 96 +-- logbook/countriesworked.cpp | 48 +- logbook/countriesworked.h | 48 +- logbook/logbook.cpp | 430 ++++++------- logbook/logbook.h | 130 ++-- logbook/logbook.pri | 26 +- 11 files changed, 1699 insertions(+), 1699 deletions(-) (limited to 'logbook') diff --git a/logbook/AD1CCty.cpp b/logbook/AD1CCty.cpp index 44dd1aaa..ef98b936 100755 --- a/logbook/AD1CCty.cpp +++ b/logbook/AD1CCty.cpp @@ -1,463 +1,463 @@ -#include "AD1CCty.hpp" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "Configuration.hpp" -#include "Radio.hpp" -#include "pimpl_impl.hpp" -#include "Logger.hpp" - -#include "moc_AD1CCty.cpp" - -using namespace boost::multi_index; - -namespace -{ - auto const file_name = "cty.dat"; -} - -struct entity -{ - using Continent = AD1CCty::Continent; - - explicit entity (int id - , QString const& name - , bool WAE_only - , int CQ_zone - , int ITU_zone - , Continent continent - , float latitude - , float longtitude - , int UTC_offset - , QString const& primary_prefix) - : id_ {id} - , name_ {name} - , WAE_only_ {WAE_only} - , CQ_zone_ {CQ_zone} - , ITU_zone_ {ITU_zone} - , continent_ {continent} - , lat_ {latitude} - , long_ {longtitude} - , UTC_offset_ {UTC_offset} - , primary_prefix_ {primary_prefix} - { - } - - int id_; - QString name_; - bool WAE_only_; // DARC WAE only, not valid for ARRL awards - int CQ_zone_; - int ITU_zone_; - Continent continent_; - float lat_; // degrees + is North - float long_; // degrees + is West - int UTC_offset_; // seconds - QString primary_prefix_; -}; - -#if !defined (QT_NO_DEBUG_STREAM) -QDebug operator << (QDebug dbg, entity const& e) -{ - QDebugStateSaver saver {dbg}; - dbg.nospace () << "entity(" - << e.id_ << ", " - << e.name_ << ", " - << e.WAE_only_ << ", " - << e.CQ_zone_ << ", " - << e.ITU_zone_ << ", " - << e.continent_ << ", " - << e.lat_ << ", " - << e.long_ << ", " - << (e.UTC_offset_ / (60. * 60.)) << ", " - << e.primary_prefix_ << ')'; - return dbg; -} -#endif - -// tags -struct id {}; -struct primary_prefix {}; - -// hash operation for QString object instances -struct hash_QString -{ - std::size_t operator () (QString const& qs) const - { - return qHash (qs); - } -}; - -// set with hashed unique index that allow for efficient lookup of -// entity by internal id -typedef multi_index_container< - entity, - indexed_by< - hashed_unique, member >, - hashed_unique, member, hash_QString> > - > entities_type; - -struct prefix -{ - explicit prefix (QString const& prefix, bool exact_match_only, int entity_id) - : prefix_ {prefix} - , exact_ {exact_match_only} - , entity_id_ {entity_id} - { - } - - // extract key which is the prefix ignoring the trailing override - // information - QString prefix_key () const - { - auto const& prefix = prefix_.toStdString (); - return QString::fromStdString (prefix.substr (0, prefix.find_first_of ("({[<~"))); - } - - QString prefix_; // call or prefix with optional - // trailing override information - bool exact_; - int entity_id_; -}; - -#if !defined (QT_NO_DEBUG_STREAM) -QDebug operator << (QDebug dbg, prefix const& p) -{ - QDebugStateSaver saver {dbg}; - dbg.nospace () << "prefix(" - << p.prefix_ << ", " - << p.exact_ << ", " - << p.entity_id_ << ')'; - return dbg; -} -#endif - -// set with ordered unique index that allow for efficient -// determination of entity and entity overrides for a call or call -// prefix -typedef multi_index_container< - prefix, - indexed_by< - ordered_unique > > - > prefixes_type; - -class AD1CCty::impl final -{ -public: - using entity_by_id = entities_type::index::type; - - explicit impl (Configuration const * configuration) - : configuration_ {configuration} - { - } - - QString get_cty_path(const Configuration *configuration); - void load_cty(QFile &file); - - entity_by_id::iterator lookup_entity (QString call, prefix const& p) const - { - call = call.toUpper (); - entity_by_id::iterator e; // iterator into entity set - - // - // deal with special rules that cty.dat does not cope with - // - if (call.startsWith ("KG4") && call.size () != 5 && call.size () != 3) - { - // KG4 2x1 and 2x3 calls that map to Gitmo are mainland US not Gitmo - return entities_.project (entities_.get ().find ("K")); - } - else - { - return entities_.get ().find (p.entity_id_); - } - } - - Record fixup (prefix const& p, entity const& e) const - { - Record result; - result.continent = e.continent_; - result.CQ_zone = e.CQ_zone_; - result.ITU_zone = e.ITU_zone_; - result.entity_name = e.name_; - result.WAE_only = e.WAE_only_; - result.latitude = e.lat_; - result.longtitude = e.long_; - result.UTC_offset = e.UTC_offset_; - result.primary_prefix = e.primary_prefix_; - - // check for overrides - bool ok1 {true}, ok2 {true}, ok3 {true}, ok4 {true}, ok5 {true}; - QString value; - if (override_value (p.prefix_, '(', ')', value)) result.CQ_zone = value.toInt (&ok1); - if (override_value (p.prefix_, '[', ']', value)) result.ITU_zone = value.toInt (&ok2); - if (override_value (p.prefix_, '<', '>', value)) - { - auto const& fix = value.split ('/'); - result.latitude = fix[0].toFloat (&ok3); - result.longtitude = fix[1].toFloat (&ok4); - } - if (override_value (p.prefix_, '{', '}', value)) result.continent = continent (value); - if (override_value (p.prefix_, '~', '~', value)) result.UTC_offset = static_cast (value.toFloat (&ok5) * 60 * 60); - if (!(ok1 && ok2 && ok3 && ok4 && ok5)) - { - throw std::domain_error {"Invalid number in cty.dat for override of " + p.prefix_.toStdString ()}; - } - return result; - } - - static bool override_value (QString const& s, QChar lb, QChar ub, QString& v) - { - auto pos = s.indexOf (lb); - if (pos >= 0) - { - v = s.mid (pos + 1, s.indexOf (ub, pos + 1) - pos - 1); - return true; - } - return false; - } - - Configuration const * configuration_; - QString path_; - QString cty_version_; - QString cty_version_date_; - - entities_type entities_; - prefixes_type prefixes_; -}; - -AD1CCty::Record::Record () - : continent {Continent::UN} - , CQ_zone {0} - , ITU_zone {0} - , WAE_only {false} - , latitude {NAN} - , longtitude {NAN} - , UTC_offset {0} -{ -} - -#if !defined (QT_NO_DEBUG_STREAM) - QDebug operator << (QDebug dbg, AD1CCty::Record const& r) - { - QDebugStateSaver saver {dbg}; - dbg.nospace () << "AD1CCty::Record(" - << r.continent << ", " - << r.CQ_zone << ", " - << r.ITU_zone << ", " - << r.entity_name << ", " - << r.WAE_only << ", " - << r.latitude << ", " - << r.longtitude << ", " - << (r.UTC_offset / (60. * 60.)) << ", " - << r.primary_prefix << ')'; - return dbg; - } -#endif - -auto AD1CCty::continent (QString const& continent_id) -> Continent -{ - Continent continent; - if ("AF" == continent_id) - { - continent = Continent::AF; - } - else if ("AN" == continent_id) - { - continent = Continent::AN; - } - else if ("AS" == continent_id) - { - continent = Continent::AS; - } - else if ("EU" == continent_id) - { - continent = Continent::EU; - } - else if ("NA" == continent_id) - { - continent = Continent::NA; - } - else if ("OC" == continent_id) - { - continent = Continent::OC; - } - else if ("SA" == continent_id) - { - continent = Continent::SA; - } - else - { - throw std::domain_error {"Invalid continent id: " + continent_id.toStdString ()}; - } - return continent; -} - -char const * AD1CCty::continent (Continent c) -{ - switch (c) - { - case Continent::AF: return "AF"; - case Continent::AN: return "AN"; - case Continent::AS: return "AS"; - case Continent::EU: return "EU"; - case Continent::NA: return "NA"; - case Continent::OC: return "OC"; - case Continent::SA: return "SA"; - default: return "UN"; - } -} - -QString AD1CCty::impl::get_cty_path(Configuration const * configuration) -{ - QDir dataPath {QStandardPaths::writableLocation (QStandardPaths::DataLocation)}; - auto path = dataPath.exists (file_name) - ? dataPath.absoluteFilePath (file_name) // user override - : configuration->data_dir ().absoluteFilePath (file_name); // or original - return path; -} - -void AD1CCty::impl::load_cty(QFile &file) -{ - QRegularExpression version_pattern{R"(VER\d{8})"}; - int entity_id = 0; - int line_number{0}; - - entities_.clear(); - prefixes_.clear(); - cty_version_ = QString{}; - cty_version_date_ = QString{}; - - QTextStream in{&file}; - while (!in.atEnd()) - { - auto const &entity_line = in.readLine(); - ++line_number; - if (!in.atEnd()) - { - auto const &entity_parts = entity_line.split(':'); - if (entity_parts.size() >= 8) - { - auto primary_prefix = entity_parts[7].trimmed(); - bool WAE_only{false}; - if (primary_prefix.startsWith('*')) - { - primary_prefix = primary_prefix.mid(1); - WAE_only = true; - } - bool ok1, ok2, ok3, ok4, ok5; - entities_.emplace(++entity_id, entity_parts[0].trimmed(), WAE_only, entity_parts[1].trimmed().toInt(&ok1), - entity_parts[2].trimmed().toInt(&ok2), continent(entity_parts[3].trimmed()), - entity_parts[4].trimmed().toFloat(&ok3), entity_parts[5].trimmed().toFloat(&ok4), - static_cast (entity_parts[6].trimmed().toFloat(&ok5) * 60 * 60), primary_prefix); - if (!(ok1 && ok2 && ok3 && ok4 && ok5)) - { - throw std::domain_error{"Invalid number in cty.dat line " + boost::lexical_cast(line_number)}; - } - QString line; - QString detail; - do - { - in.readLineInto(&line); - ++line_number; - } while (detail += line, !detail.endsWith(';')); - for (auto prefix: detail.left(detail.size() - 1).split(',')) - { - prefix = prefix.trimmed(); - bool exact{false}; - if (prefix.startsWith('=')) - { - prefix = prefix.mid(1); - exact = true; - // match version pattern to prefix - if (version_pattern.match(prefix).hasMatch()) - { - cty_version_date_ = prefix; - } - } - prefixes_.emplace(prefix, exact, entity_id); - } - } - } - } -} - -AD1CCty::AD1CCty (Configuration const * configuration) - : m_ {configuration} -{ - Q_ASSERT (configuration); - // TODO: G4WJS - consider doing the following asynchronously to - // speed up startup. Not urgent as it takes less than 0.5s on a Core - // i7 reading BIG CTY.DAT. - AD1CCty::reload (configuration); - } - -void AD1CCty::reload(Configuration const * configuration) -{ - m_->path_ = m_->impl::get_cty_path(configuration); - QFile file {m_->path_}; - - LOG_INFO(QString{"Loading CTY.DAT from %1"}.arg (m_->path_)); - - if (file.open (QFile::ReadOnly)) - { - m_->impl::load_cty(file); - m_->cty_version_ = AD1CCty::lookup("VERSION").entity_name; - Q_EMIT cty_loaded(m_->cty_version_); - LOG_INFO(QString{"Loaded CTY.DAT version %1, %2"}.arg (m_->cty_version_date_).arg (m_->cty_version_)); - } -} - -AD1CCty::~AD1CCty () -{ -} - -auto AD1CCty::lookup (QString const& call) const -> Record -{ - auto const& exact_search = call.toUpper (); - if (!(exact_search.endsWith ("/MM") || exact_search.endsWith ("/AM"))) - { - auto search_prefix = Radio::effective_prefix (exact_search); - if (search_prefix != exact_search) - { - auto p = m_->prefixes_.find (exact_search); - if (p != m_->prefixes_.end () && p->exact_) - { - return m_->fixup (*p, *m_->lookup_entity (call, *p)); - } - } - while (search_prefix.size ()) - { - auto p = m_->prefixes_.find (search_prefix); - if (p != m_->prefixes_.end ()) - { - impl::entity_by_id::iterator e = m_->lookup_entity (call, *p); - // always lookup WAE entities, we substitute them later in displaytext.cpp if "Include extra WAE entites" is not selected - if (!p->exact_ || call.size () == search_prefix.size ()) - { - return m_->fixup (*p, *e); - } - } - search_prefix = search_prefix.left (search_prefix.size () - 1); - } - } - return Record {}; -} -auto AD1CCty::version () const -> QString -{ - return m_->cty_version_date_; -} +#include "AD1CCty.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "Configuration.hpp" +#include "Radio.hpp" +#include "pimpl_impl.hpp" +#include "Logger.hpp" + +#include "moc_AD1CCty.cpp" + +using namespace boost::multi_index; + +namespace +{ + auto const file_name = "cty.dat"; +} + +struct entity +{ + using Continent = AD1CCty::Continent; + + explicit entity (int id + , QString const& name + , bool WAE_only + , int CQ_zone + , int ITU_zone + , Continent continent + , float latitude + , float longtitude + , int UTC_offset + , QString const& primary_prefix) + : id_ {id} + , name_ {name} + , WAE_only_ {WAE_only} + , CQ_zone_ {CQ_zone} + , ITU_zone_ {ITU_zone} + , continent_ {continent} + , lat_ {latitude} + , long_ {longtitude} + , UTC_offset_ {UTC_offset} + , primary_prefix_ {primary_prefix} + { + } + + int id_; + QString name_; + bool WAE_only_; // DARC WAE only, not valid for ARRL awards + int CQ_zone_; + int ITU_zone_; + Continent continent_; + float lat_; // degrees + is North + float long_; // degrees + is West + int UTC_offset_; // seconds + QString primary_prefix_; +}; + +#if !defined (QT_NO_DEBUG_STREAM) +QDebug operator << (QDebug dbg, entity const& e) +{ + QDebugStateSaver saver {dbg}; + dbg.nospace () << "entity(" + << e.id_ << ", " + << e.name_ << ", " + << e.WAE_only_ << ", " + << e.CQ_zone_ << ", " + << e.ITU_zone_ << ", " + << e.continent_ << ", " + << e.lat_ << ", " + << e.long_ << ", " + << (e.UTC_offset_ / (60. * 60.)) << ", " + << e.primary_prefix_ << ')'; + return dbg; +} +#endif + +// tags +struct id {}; +struct primary_prefix {}; + +// hash operation for QString object instances +struct hash_QString +{ + std::size_t operator () (QString const& qs) const + { + return qHash (qs); + } +}; + +// set with hashed unique index that allow for efficient lookup of +// entity by internal id +typedef multi_index_container< + entity, + indexed_by< + hashed_unique, member >, + hashed_unique, member, hash_QString> > + > entities_type; + +struct prefix +{ + explicit prefix (QString const& prefix, bool exact_match_only, int entity_id) + : prefix_ {prefix} + , exact_ {exact_match_only} + , entity_id_ {entity_id} + { + } + + // extract key which is the prefix ignoring the trailing override + // information + QString prefix_key () const + { + auto const& prefix = prefix_.toStdString (); + return QString::fromStdString (prefix.substr (0, prefix.find_first_of ("({[<~"))); + } + + QString prefix_; // call or prefix with optional + // trailing override information + bool exact_; + int entity_id_; +}; + +#if !defined (QT_NO_DEBUG_STREAM) +QDebug operator << (QDebug dbg, prefix const& p) +{ + QDebugStateSaver saver {dbg}; + dbg.nospace () << "prefix(" + << p.prefix_ << ", " + << p.exact_ << ", " + << p.entity_id_ << ')'; + return dbg; +} +#endif + +// set with ordered unique index that allow for efficient +// determination of entity and entity overrides for a call or call +// prefix +typedef multi_index_container< + prefix, + indexed_by< + ordered_unique > > + > prefixes_type; + +class AD1CCty::impl final +{ +public: + using entity_by_id = entities_type::index::type; + + explicit impl (Configuration const * configuration) + : configuration_ {configuration} + { + } + + QString get_cty_path(const Configuration *configuration); + void load_cty(QFile &file); + + entity_by_id::iterator lookup_entity (QString call, prefix const& p) const + { + call = call.toUpper (); + entity_by_id::iterator e; // iterator into entity set + + // + // deal with special rules that cty.dat does not cope with + // + if (call.startsWith ("KG4") && call.size () != 5 && call.size () != 3) + { + // KG4 2x1 and 2x3 calls that map to Gitmo are mainland US not Gitmo + return entities_.project (entities_.get ().find ("K")); + } + else + { + return entities_.get ().find (p.entity_id_); + } + } + + Record fixup (prefix const& p, entity const& e) const + { + Record result; + result.continent = e.continent_; + result.CQ_zone = e.CQ_zone_; + result.ITU_zone = e.ITU_zone_; + result.entity_name = e.name_; + result.WAE_only = e.WAE_only_; + result.latitude = e.lat_; + result.longtitude = e.long_; + result.UTC_offset = e.UTC_offset_; + result.primary_prefix = e.primary_prefix_; + + // check for overrides + bool ok1 {true}, ok2 {true}, ok3 {true}, ok4 {true}, ok5 {true}; + QString value; + if (override_value (p.prefix_, '(', ')', value)) result.CQ_zone = value.toInt (&ok1); + if (override_value (p.prefix_, '[', ']', value)) result.ITU_zone = value.toInt (&ok2); + if (override_value (p.prefix_, '<', '>', value)) + { + auto const& fix = value.split ('/'); + result.latitude = fix[0].toFloat (&ok3); + result.longtitude = fix[1].toFloat (&ok4); + } + if (override_value (p.prefix_, '{', '}', value)) result.continent = continent (value); + if (override_value (p.prefix_, '~', '~', value)) result.UTC_offset = static_cast (value.toFloat (&ok5) * 60 * 60); + if (!(ok1 && ok2 && ok3 && ok4 && ok5)) + { + throw std::domain_error {"Invalid number in cty.dat for override of " + p.prefix_.toStdString ()}; + } + return result; + } + + static bool override_value (QString const& s, QChar lb, QChar ub, QString& v) + { + auto pos = s.indexOf (lb); + if (pos >= 0) + { + v = s.mid (pos + 1, s.indexOf (ub, pos + 1) - pos - 1); + return true; + } + return false; + } + + Configuration const * configuration_; + QString path_; + QString cty_version_; + QString cty_version_date_; + + entities_type entities_; + prefixes_type prefixes_; +}; + +AD1CCty::Record::Record () + : continent {Continent::UN} + , CQ_zone {0} + , ITU_zone {0} + , WAE_only {false} + , latitude {NAN} + , longtitude {NAN} + , UTC_offset {0} +{ +} + +#if !defined (QT_NO_DEBUG_STREAM) + QDebug operator << (QDebug dbg, AD1CCty::Record const& r) + { + QDebugStateSaver saver {dbg}; + dbg.nospace () << "AD1CCty::Record(" + << r.continent << ", " + << r.CQ_zone << ", " + << r.ITU_zone << ", " + << r.entity_name << ", " + << r.WAE_only << ", " + << r.latitude << ", " + << r.longtitude << ", " + << (r.UTC_offset / (60. * 60.)) << ", " + << r.primary_prefix << ')'; + return dbg; + } +#endif + +auto AD1CCty::continent (QString const& continent_id) -> Continent +{ + Continent continent; + if ("AF" == continent_id) + { + continent = Continent::AF; + } + else if ("AN" == continent_id) + { + continent = Continent::AN; + } + else if ("AS" == continent_id) + { + continent = Continent::AS; + } + else if ("EU" == continent_id) + { + continent = Continent::EU; + } + else if ("NA" == continent_id) + { + continent = Continent::NA; + } + else if ("OC" == continent_id) + { + continent = Continent::OC; + } + else if ("SA" == continent_id) + { + continent = Continent::SA; + } + else + { + throw std::domain_error {"Invalid continent id: " + continent_id.toStdString ()}; + } + return continent; +} + +char const * AD1CCty::continent (Continent c) +{ + switch (c) + { + case Continent::AF: return "AF"; + case Continent::AN: return "AN"; + case Continent::AS: return "AS"; + case Continent::EU: return "EU"; + case Continent::NA: return "NA"; + case Continent::OC: return "OC"; + case Continent::SA: return "SA"; + default: return "UN"; + } +} + +QString AD1CCty::impl::get_cty_path(Configuration const * configuration) +{ + QDir dataPath {QStandardPaths::writableLocation (QStandardPaths::DataLocation)}; + auto path = dataPath.exists (file_name) + ? dataPath.absoluteFilePath (file_name) // user override + : configuration->data_dir ().absoluteFilePath (file_name); // or original + return path; +} + +void AD1CCty::impl::load_cty(QFile &file) +{ + QRegularExpression version_pattern{R"(VER\d{8})"}; + int entity_id = 0; + int line_number{0}; + + entities_.clear(); + prefixes_.clear(); + cty_version_ = QString{}; + cty_version_date_ = QString{}; + + QTextStream in{&file}; + while (!in.atEnd()) + { + auto const &entity_line = in.readLine(); + ++line_number; + if (!in.atEnd()) + { + auto const &entity_parts = entity_line.split(':'); + if (entity_parts.size() >= 8) + { + auto primary_prefix = entity_parts[7].trimmed(); + bool WAE_only{false}; + if (primary_prefix.startsWith('*')) + { + primary_prefix = primary_prefix.mid(1); + WAE_only = true; + } + bool ok1, ok2, ok3, ok4, ok5; + entities_.emplace(++entity_id, entity_parts[0].trimmed(), WAE_only, entity_parts[1].trimmed().toInt(&ok1), + entity_parts[2].trimmed().toInt(&ok2), continent(entity_parts[3].trimmed()), + entity_parts[4].trimmed().toFloat(&ok3), entity_parts[5].trimmed().toFloat(&ok4), + static_cast (entity_parts[6].trimmed().toFloat(&ok5) * 60 * 60), primary_prefix); + if (!(ok1 && ok2 && ok3 && ok4 && ok5)) + { + throw std::domain_error{"Invalid number in cty.dat line " + boost::lexical_cast(line_number)}; + } + QString line; + QString detail; + do + { + in.readLineInto(&line); + ++line_number; + } while (detail += line, !detail.endsWith(';')); + for (auto prefix: detail.left(detail.size() - 1).split(',')) + { + prefix = prefix.trimmed(); + bool exact{false}; + if (prefix.startsWith('=')) + { + prefix = prefix.mid(1); + exact = true; + // match version pattern to prefix + if (version_pattern.match(prefix).hasMatch()) + { + cty_version_date_ = prefix; + } + } + prefixes_.emplace(prefix, exact, entity_id); + } + } + } + } +} + +AD1CCty::AD1CCty (Configuration const * configuration) + : m_ {configuration} +{ + Q_ASSERT (configuration); + // TODO: G4WJS - consider doing the following asynchronously to + // speed up startup. Not urgent as it takes less than 0.5s on a Core + // i7 reading BIG CTY.DAT. + AD1CCty::reload (configuration); + } + +void AD1CCty::reload(Configuration const * configuration) +{ + m_->path_ = m_->impl::get_cty_path(configuration); + QFile file {m_->path_}; + + LOG_INFO(QString{"Loading CTY.DAT from %1"}.arg (m_->path_)); + + if (file.open (QFile::ReadOnly)) + { + m_->impl::load_cty(file); + m_->cty_version_ = AD1CCty::lookup("VERSION").entity_name; + Q_EMIT cty_loaded(m_->cty_version_); + LOG_INFO(QString{"Loaded CTY.DAT version %1, %2"}.arg (m_->cty_version_date_).arg (m_->cty_version_)); + } +} + +AD1CCty::~AD1CCty () +{ +} + +auto AD1CCty::lookup (QString const& call) const -> Record +{ + auto const& exact_search = call.toUpper (); + if (!(exact_search.endsWith ("/MM") || exact_search.endsWith ("/AM"))) + { + auto search_prefix = Radio::effective_prefix (exact_search); + if (search_prefix != exact_search) + { + auto p = m_->prefixes_.find (exact_search); + if (p != m_->prefixes_.end () && p->exact_) + { + return m_->fixup (*p, *m_->lookup_entity (call, *p)); + } + } + while (search_prefix.size ()) + { + auto p = m_->prefixes_.find (search_prefix); + if (p != m_->prefixes_.end ()) + { + impl::entity_by_id::iterator e = m_->lookup_entity (call, *p); + // always lookup WAE entities, we substitute them later in displaytext.cpp if "Include extra WAE entites" is not selected + if (!p->exact_ || call.size () == search_prefix.size ()) + { + return m_->fixup (*p, *e); + } + } + search_prefix = search_prefix.left (search_prefix.size () - 1); + } + } + return Record {}; +} +auto AD1CCty::version () const -> QString +{ + return m_->cty_version_date_; +} diff --git a/logbook/AD1CCty.hpp b/logbook/AD1CCty.hpp index 0908bb56..982ff7ce 100755 --- a/logbook/AD1CCty.hpp +++ b/logbook/AD1CCty.hpp @@ -1,60 +1,60 @@ -#ifndef AD1C_CTY_HPP_ -#define AD1C_CTY_HPP_ - -#include -#include -#include "pimpl_h.hpp" - -class QString; -class Configuration; - -// -// AD1CCty - Fast access database of Jim Reisert, AD1C's, cty.dat -// entity and entity override information file. -// -class AD1CCty final - : public QObject -{ - Q_OBJECT - -public: - // - // Continent enumeration - // - enum class Continent {UN, AF, AN, AS, EU, NA, OC, SA}; - static Continent continent (QString const& continent_id); - static char const * continent (Continent); - Q_ENUM (Continent) - - struct Record - { - explicit Record (); - - Continent continent; - int CQ_zone; - int ITU_zone; - QString entity_name; - bool WAE_only; - float latitude; - float longtitude; - int UTC_offset; - QString primary_prefix; - }; - - explicit AD1CCty (Configuration const *); - void reload(Configuration const * configuration); - ~AD1CCty (); - Record lookup (QString const& call) const; - QString version () const; - Q_SIGNAL void cty_loaded (QString const& version) const; - -private: - class impl; - pimpl m_; -}; - -#if !defined (QT_NO_DEBUG_STREAM) -QDebug operator << (QDebug, AD1CCty::Record const&); -#endif - -#endif +#ifndef AD1C_CTY_HPP_ +#define AD1C_CTY_HPP_ + +#include +#include +#include "pimpl_h.hpp" + +class QString; +class Configuration; + +// +// AD1CCty - Fast access database of Jim Reisert, AD1C's, cty.dat +// entity and entity override information file. +// +class AD1CCty final + : public QObject +{ + Q_OBJECT + +public: + // + // Continent enumeration + // + enum class Continent {UN, AF, AN, AS, EU, NA, OC, SA}; + static Continent continent (QString const& continent_id); + static char const * continent (Continent); + Q_ENUM (Continent) + + struct Record + { + explicit Record (); + + Continent continent; + int CQ_zone; + int ITU_zone; + QString entity_name; + bool WAE_only; + float latitude; + float longtitude; + int UTC_offset; + QString primary_prefix; + }; + + explicit AD1CCty (Configuration const *); + void reload(Configuration const * configuration); + ~AD1CCty (); + Record lookup (QString const& call) const; + QString version () const; + Q_SIGNAL void cty_loaded (QString const& version) const; + +private: + class impl; + pimpl m_; +}; + +#if !defined (QT_NO_DEBUG_STREAM) +QDebug operator << (QDebug, AD1CCty::Record const&); +#endif + +#endif diff --git a/logbook/Multiplier.cpp b/logbook/Multiplier.cpp index 542ba8e6..6323e4fe 100755 --- a/logbook/Multiplier.cpp +++ b/logbook/Multiplier.cpp @@ -1,44 +1,44 @@ -#include "Multiplier.hpp" - -#include -#include -#include -#include "models/CabrilloLog.hpp" -#include "pimpl_impl.hpp" - -class Multiplier::impl -{ -public: - impl (AD1CCty const * countries) - : countries_ {countries} - { - } - - AD1CCty const * countries_; - worked_set entities_worked_; - worked_set grids_worked_; -}; - -Multiplier::Multiplier (AD1CCty const * countries) - : m_ {countries} -{ -} - -Multiplier::~Multiplier () -{ -} - -void Multiplier::reload (CabrilloLog const * log) -{ - m_->entities_worked_ = log->unique_DXCC_entities (m_->countries_); -} - -auto Multiplier::entities_worked () const -> worked_set const& -{ - return m_->entities_worked_; -} - -auto Multiplier::grids_worked () const -> worked_set const& -{ - return m_->grids_worked_; -} +#include "Multiplier.hpp" + +#include +#include +#include +#include "models/CabrilloLog.hpp" +#include "pimpl_impl.hpp" + +class Multiplier::impl +{ +public: + impl (AD1CCty const * countries) + : countries_ {countries} + { + } + + AD1CCty const * countries_; + worked_set entities_worked_; + worked_set grids_worked_; +}; + +Multiplier::Multiplier (AD1CCty const * countries) + : m_ {countries} +{ +} + +Multiplier::~Multiplier () +{ +} + +void Multiplier::reload (CabrilloLog const * log) +{ + m_->entities_worked_ = log->unique_DXCC_entities (m_->countries_); +} + +auto Multiplier::entities_worked () const -> worked_set const& +{ + return m_->entities_worked_; +} + +auto Multiplier::grids_worked () const -> worked_set const& +{ + return m_->grids_worked_; +} diff --git a/logbook/Multiplier.hpp b/logbook/Multiplier.hpp index 75edd7bb..2271dd77 100755 --- a/logbook/Multiplier.hpp +++ b/logbook/Multiplier.hpp @@ -1,30 +1,30 @@ -#ifndef MULTIPLIER_HPP_ -#define MULTIPLIER_HPP_ - -#include -#include -#include "pimpl_h.hpp" - -class QString; -class AD1CCty; -class CabrilloLog; - -class Multiplier final - : private boost::noncopyable -{ -public: - using worked_item = QPair; - using worked_set = QSet; - - explicit Multiplier (AD1CCty const *); - ~Multiplier (); - void reload (CabrilloLog const *); - worked_set const& entities_worked () const; - worked_set const& grids_worked () const; - -private: - class impl; - pimpl m_; -}; - -#endif +#ifndef MULTIPLIER_HPP_ +#define MULTIPLIER_HPP_ + +#include +#include +#include "pimpl_h.hpp" + +class QString; +class AD1CCty; +class CabrilloLog; + +class Multiplier final + : private boost::noncopyable +{ +public: + using worked_item = QPair; + using worked_set = QSet; + + explicit Multiplier (AD1CCty const *); + ~Multiplier (); + void reload (CabrilloLog const *); + worked_set const& entities_worked () const; + worked_set const& grids_worked () const; + +private: + class impl; + pimpl m_; +}; + +#endif diff --git a/logbook/WorkedBefore.cpp b/logbook/WorkedBefore.cpp index 0186bea5..b5dedc3d 100755 --- a/logbook/WorkedBefore.cpp +++ b/logbook/WorkedBefore.cpp @@ -1,713 +1,713 @@ -#include "WorkedBefore.hpp" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "Configuration.hpp" -#include "revision_utils.hpp" -#include "Logger.hpp" -#include "qt_helpers.hpp" -#include "pimpl_impl.hpp" - -#include "moc_WorkedBefore.cpp" - -using namespace boost::multi_index; - -// hash function for QString members in hashed indexes -inline -std::size_t hash_value (QString const& s) -{ - return std::hash {} (s); -} - -// -// worked before set element -// -struct worked_entry -{ - explicit worked_entry (QString const& call, QString const& grid, QString const& band - , QString const& mode, QString const& country, AD1CCty::Continent continent - , int CQ_zone, int ITU_zone) - : call_ {call} - , grid_ {grid} - , band_ {band} - , mode_ {mode} - , country_ {country} - , continent_ {continent} - , CQ_zone_ {CQ_zone} - , ITU_zone_ {ITU_zone} - { - } - - QString call_; - QString grid_; - QString band_; - QString mode_; - QString country_; - AD1CCty::Continent continent_; - int CQ_zone_; - int ITU_zone_; -}; - -bool operator == (worked_entry const& lhs, worked_entry const& rhs) -{ - return - lhs.continent_ == rhs.continent_ // check 1st as it is fast - && lhs.CQ_zone_ == rhs.CQ_zone_ // ditto - && lhs.ITU_zone_ == rhs.ITU_zone_ // ditto - && lhs.call_ == rhs.call_ // check the rest in decreasing - && lhs.grid_ == rhs.grid_ // domain size order to shortcut - && lhs.country_ == rhs.country_ // differences as quickly as possible - && lhs.band_ == rhs.band_ - && lhs.mode_ == rhs.mode_; -} - -std::size_t hash_value (worked_entry const& we) -{ - std::size_t seed {0}; - boost::hash_combine (seed, we.call_); - boost::hash_combine (seed, we.grid_); - boost::hash_combine (seed, we.band_); - boost::hash_combine (seed, we.mode_); - boost::hash_combine (seed, we.country_); - boost::hash_combine (seed, we.continent_); - boost::hash_combine (seed, we.CQ_zone_); - boost::hash_combine (seed, we.ITU_zone_); - return seed; -} - -#if !defined (QT_NO_DEBUG_STREAM) -QDebug operator << (QDebug dbg, worked_entry const& e) -{ - QDebugStateSaver saver {dbg}; - dbg.nospace () << "worked_entry(" - << e.call_ << ", " - << e.grid_ << ", " - << e.band_ << ", " - << e.mode_ << ", " - << e.country_ << ", " - << e.continent_ << ", " - << e.CQ_zone_ << ", " - << e.ITU_zone_ << ')'; - return dbg; -} -#endif - -// less then predidate for the Continent enum class, needed for -// ordered indexes -struct Continent_less -{ - bool operator () (AD1CCty::Continent lhs, AD1CCty::Continent rhs) const - { - return static_cast (lhs) < static_cast (rhs); - } -}; - -// index tags -struct call_mode_band {}; -struct call_band {}; -struct grid_mode_band {}; -struct grid_band {}; -struct entity_mode_band {}; -struct entity_band {}; -struct continent_mode_band {}; -struct continent_band {}; -struct CQ_zone_mode_band {}; -struct CQ_zone_band {}; -struct ITU_zone_mode_band {}; -struct ITU_zone_band {}; - -// set with multiple ordered unique indexes that allow for optimally -// efficient determination of various categories of worked before -// status -typedef multi_index_container< - worked_entry, - indexed_by< - // basic unordered set constraint - we don't need duplicate worked entries - hashed_unique>, - - // - // The following indexes are used to discover worked before stuff. - // - // They are ordered so as to support partial lookups and - // non-unique because container inserts must be valid for all - // indexes. - // - - // call+mode+band - ordered_non_unique, - composite_key, - member, - member > >, - // call+band - ordered_non_unique, - composite_key, - member > >, - // grid+mode+band - ordered_non_unique, - composite_key, - member, - member > >, - // grid+band - ordered_non_unique, - composite_key, - member > >, - // country+mode+band - ordered_non_unique, - composite_key, - member, - member > >, - // country+band - ordered_non_unique, - composite_key, - member > >, - // continent+mode+band - ordered_non_unique, - composite_key, - member, - member >, - composite_key_compare, std::less > >, - // continent+band - ordered_non_unique, - composite_key, - member >, - composite_key_compare > >, - // CQ-zone+mode+band - ordered_non_unique, - composite_key, - member, - member > >, - // CQ-zone+band - ordered_non_unique, - composite_key, - member > >, - // ITU-zone+mode+band - ordered_non_unique, - composite_key, - member, - member > >, - // ITU-zone+band - ordered_non_unique, - composite_key, - member > > > - > worked_before_database_type; - -namespace -{ - auto const logFileName = "wsjtx_log.adi"; - - // Exception class suitable for using with QtConcurrent across - // thread boundaries - class LoaderException final - : public QException - { - public: - LoaderException (std::exception const& e) : error_ {e.what ()} {} - QString error () const {return error_;} - void raise () const override {throw *this;} - LoaderException * clone () const override {return new LoaderException {*this};} - private: - QString error_; - }; - - QString extractField (QString const& record, QString const& fieldName) - { - int fieldNameIndex = record.indexOf ('<' + fieldName + ':', 0, Qt::CaseInsensitive); - if (fieldNameIndex >=0) - { - int closingBracketIndex = record.indexOf('>',fieldNameIndex); - int fieldLengthIndex = record.indexOf(':',fieldNameIndex); // find the size delimiter - int dataTypeIndex = -1; - if (fieldLengthIndex >= 0) - { - dataTypeIndex = record.indexOf(':',fieldLengthIndex+1); // check for a second : indicating there is a data type - if (dataTypeIndex > closingBracketIndex) - dataTypeIndex = -1; // second : was found but it was beyond the closing > - } - else - { - throw LoaderException (std::runtime_error {QCoreApplication::translate ("WorkedBefore", "Invalid ADIF field %0: %1").arg (fieldName).arg (record).toLocal8Bit ()}); - } - - if (closingBracketIndex > fieldNameIndex && fieldLengthIndex > fieldNameIndex && fieldLengthIndex < closingBracketIndex) - { - int fieldLengthCharCount = closingBracketIndex - fieldLengthIndex -1; - if (dataTypeIndex >= 0) - fieldLengthCharCount -= 2; // data type indicator is always a colon followed by a single character - QString fieldLengthString = record.mid(fieldLengthIndex+1,fieldLengthCharCount); - int fieldLength = fieldLengthString.toInt(); - if (fieldLength > 0) - { - return record.mid(closingBracketIndex+1,fieldLength); - } - } - else - { - throw LoaderException (std::runtime_error {QCoreApplication::translate ("WorkedBefore", "Malformed ADIF field %0: %1").arg (fieldName).arg (record).toLocal8Bit ()}); - } - } - return QString {}; - } - - worked_before_database_type loader (QString const& path, AD1CCty const * prefixes) - { - worked_before_database_type worked; - QFile inputFile {path}; - if (inputFile.exists ()) - { - if (inputFile.open (QFile::ReadOnly)) - { - QTextStream in {&inputFile}; - QString buffer; - bool pre_read {false}; - int end_position {-1}; - - // skip optional header record - do - { - buffer += in.readLine () + '\n'; - if (buffer.startsWith (QChar {'<'})) // denotes no header - { - pre_read = true; - } - else - { - end_position = buffer.indexOf ("", 0, Qt::CaseInsensitive); - } - } - while (!in.atEnd () && !pre_read && end_position < 0); - if (!pre_read) // found header - { - if (end_position < 0) - { - throw LoaderException (std::runtime_error {QCoreApplication::translate ("WorkedBefore", "Invalid ADIF header").toLocal8Bit ()}); - } - buffer.remove (0, end_position + 5); - } - while (!in.atEnd ()) - { - end_position = buffer.indexOf ("", 0, Qt::CaseInsensitive); - do - { - if (!in.atEnd () && end_position < 0) - { - buffer += in.readLine () + '\n'; - } - } - while ((end_position = buffer.indexOf ("", 0, Qt::CaseInsensitive)) < 0 && !in.atEnd ()); - if (end_position >= 0) // require valid ADIF record - // with terminator - { - auto record = buffer.left (end_position + 5).trimmed (); - auto next_record = buffer.indexOf (QChar {'<'}, end_position + 5); - buffer.remove (0, next_record >=0 ? next_record : buffer.size ()); - record = record.mid (record.indexOf (QChar {'<'})); - auto call = extractField (record, "CALL"); - if (call.size ()) // require CALL field before we - // will parse a record - { - auto const& entity = prefixes->lookup (call); - auto mode = extractField (record, "MODE").toUpper (); - if (!mode.size () || "MFSK" == mode) - { - mode = extractField (record, "SUBMODE").toUpper (); - } - worked.emplace (call.toUpper () - , extractField (record, "GRIDSQUARE").left (4).toUpper () // not interested in 6-digit grids - , extractField (record, "BAND").toUpper () - , mode - , entity.entity_name - , entity.continent - , entity.CQ_zone - , entity.ITU_zone); - } - } - } - } - else - { - throw LoaderException (std::runtime_error {QCoreApplication::translate ("WorkedBefore", "Error opening ADIF log file for read: %0").arg (inputFile.errorString ()).toLocal8Bit ()}); - } - } - return worked; - } -} - -class WorkedBefore::impl final -{ -public: - impl (Configuration const * configuration) - : configuration_ {configuration} - , path_ {QDir {QStandardPaths::writableLocation (QStandardPaths::DataLocation)}.absoluteFilePath (logFileName)} - , prefixes_ {configuration} - { - } - - void reload () - { - prefixes_.reload (configuration_); - async_loader_ = QtConcurrent::run (loader, path_, &prefixes_); - loader_watcher_.setFuture (async_loader_); - } - - Configuration const * configuration_; - QString path_; - AD1CCty prefixes_; - QFutureWatcher loader_watcher_; - QFuture async_loader_; - worked_before_database_type worked_; -}; - -WorkedBefore::WorkedBefore (Configuration const * configuration) - : m_ {configuration} -{ - Q_ASSERT (configuration); - connect (&m_->loader_watcher_, &QFutureWatcher::finished, [this] () { - QString error; - size_t n {0}; - try - { - m_->worked_ = m_->loader_watcher_.result (); - n = m_->worked_.size (); - } - catch (LoaderException const& e) - { - error = e.error (); - } - QString cty_ver = m_->prefixes_.version(); - LOG_DEBUG(QString{"WorkedBefore::reload: CTY.DAT version %1"}.arg (cty_ver)); - Q_EMIT finished_loading (n, cty_ver, error); - }); - reload (); -} - -QString WorkedBefore::cty_version () const -{ - return m_->prefixes_.version (); -} - -void WorkedBefore::reload () -{ - m_->reload (); -} - -WorkedBefore::~WorkedBefore () -{ -} - -QString const& WorkedBefore::path () const -{ - return m_->path_; -} - -AD1CCty const * WorkedBefore::countries () const -{ - return &m_->prefixes_; -} - -bool WorkedBefore::add (QString const& call - , QString const& grid - , QString const& band - , QString const& mode - , QByteArray const& ADIF_record) -{ - if (call.size ()) - { - auto const& entity = m_->prefixes_.lookup (call); - QFile file {m_->path_}; - if (!file.open(QIODevice::Text | QIODevice::Append)) - { - return false; - } - else - { - QTextStream out {&file}; - if (!file.size ()) - { - auto ts = QDateTime::currentDateTimeUtc ().toString ("yyyyMMdd HHmmss"); - auto ver = version (true); - out << // new file - QString { - "ADIF Export\n" - "3.1.1\n" - "%0\n" - "WSJT-X\n" - "%2\n" - "" - }.arg (ts).arg (ver.size ()).arg (ver) - << -#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) - endl -#else - Qt::endl -#endif - ; - } - out << ADIF_record << " " << -#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) - endl -#else - Qt::endl -#endif - ; - } - m_->worked_.emplace (call.toUpper (), grid.left (4).toUpper (), band.toUpper (), mode.toUpper () - , entity.entity_name, entity.continent, entity.CQ_zone, entity.ITU_zone); - } - return true; -} - -bool WorkedBefore::country_worked (QString const& country, QString const& mode, QString const& band) const -{ - if (mode.size ()) - { - if (band.size ()) - { - return - country.size () - && m_->worked_.get ().end () - != m_->worked_.get ().find (std::make_tuple (country, mode.toUpper (), band.toUpper ())); - } - else - { - // partial key lookup - return - country.size () - && m_->worked_.get ().end () - != m_->worked_.get ().find (std::make_tuple (country, mode.toUpper ())); - } - } - else - { - if (band.size ()) - { - return - country.size () - && m_->worked_.get ().end () - != m_->worked_.get ().find (std::make_tuple (country, band.toUpper ())); - } - else - { - // partial key lookup - return - country.size () - && m_->worked_.get ().end () - != m_->worked_.get ().find (country); - } - } -} - -bool WorkedBefore::grid_worked (QString const& grid, QString const& mode, QString const& band) const -{ - auto gridsquare = grid.left (4).toUpper (); - if (m_->configuration_->highlight_only_fields ()) - { - // can't use a direct set find operation or a set operation with - // a (CompatibleKey, CompatibleCompare) concept so we must - // partially scan the index - auto range = boost::make_iterator_range ( - m_->worked_.get ().lower_bound (gridsquare.left (2)) - , m_->worked_.get ().upper_bound (gridsquare.left (2) + "99")); - for (worked_entry const& worked : range) - { - if ((!mode.size () || mode.toUpper () == worked.mode_) - && (!band.size () || worked.band_ == band.toUpper ())) - { - return true; - } - } - } - else - { - if (mode.size ()) - { - if (band.size ()) - { - return m_->worked_.get ().end () - != m_->worked_.get ().find (std::make_tuple (gridsquare, mode.toUpper (), band.toUpper ())); - } - else - { - // partial key lookup - return m_->worked_.get ().end () - != m_->worked_.get ().find (std::make_tuple (gridsquare, mode.toUpper ())); - } - } - else - { - if (band.size ()) - { - return m_->worked_.get ().end () - != m_->worked_.get ().find (std::make_tuple (gridsquare, band.toUpper ())); - } - else - { - // partial key lookup - return m_->worked_.get ().end () - != m_->worked_.get ().find (gridsquare); - } - } - } - return false; -} - -bool WorkedBefore::call_worked (QString const& call, QString const& mode, QString const& band) const -{ - if (mode.size ()) - { - if (band.size ()) - { - return m_->worked_.get ().end () - != m_->worked_.get ().find (std::make_tuple (call.toUpper (), mode.toUpper (), band.toUpper ())); - } - else - { - // partial key lookup - return m_->worked_.get ().end () - != m_->worked_.get ().find (std::make_tuple (call.toUpper (), mode.toUpper ())); - } - } - else - { - if (band.size ()) - { - return m_->worked_.get ().end () - != m_->worked_.get ().find (std::make_tuple (call.toUpper (), band.toUpper ())); - } - else - { - // partial key lookup - return m_->worked_.get ().end () - != m_->worked_.get ().find (std::make_tuple (call.toUpper ())); - } - } -} - -bool WorkedBefore::continent_worked (Continent continent, QString const& mode, QString const& band) const -{ - if (mode.size ()) - { - if (band.size ()) - { - return m_->worked_.get ().end () - != m_->worked_.get ().find (std::make_tuple (continent, mode.toUpper (), band.toUpper ())); - } - else - { - // partial key lookup - return m_->worked_.get ().end () - != m_->worked_.get ().find (std::make_tuple (continent, mode.toUpper ())); - } - } - else - { - if (band.size ()) - { - return m_->worked_.get ().end () - != m_->worked_.get ().find (std::make_tuple (continent, band.toUpper ())); - } - else - { - // partial key lookup - return m_->worked_.get ().end () - != m_->worked_.get ().find (continent); - } - } -} - -bool WorkedBefore::CQ_zone_worked (int CQ_zone, QString const& mode, QString const& band) const -{ - if (mode.size ()) - { - if (band.size ()) - { - return m_->worked_.get ().end () - != m_->worked_.get ().find (std::make_tuple (CQ_zone, mode.toUpper (), band.toUpper ())); - } - else - { - // partial key lookup - return m_->worked_.get ().end () - != m_->worked_.get ().find (std::make_tuple (CQ_zone, mode.toUpper ())); - } - } - else - { - if (band.size ()) - { - return m_->worked_.get ().end () - != m_->worked_.get ().find (std::make_tuple (CQ_zone, band.toUpper ())); - } - else - { - // partial key lookup - return m_->worked_.get ().end () - != m_->worked_.get ().find (CQ_zone); - } - } -} - - -bool WorkedBefore::ITU_zone_worked (int ITU_zone, QString const& mode, QString const& band) const -{ - if (mode.size ()) - { - if (band.size ()) - { - return m_->worked_.get ().end () - != m_->worked_.get ().find (std::make_tuple (ITU_zone, mode.toUpper (), band.toUpper ())); - } - else - { - // partial key lookup - return m_->worked_.get ().end () - != m_->worked_.get ().find (std::make_tuple (ITU_zone, mode.toUpper ())); - } - } - else - { - if (band.size ()) - { - return m_->worked_.get ().end () - != m_->worked_.get ().find (std::make_tuple (ITU_zone, band.toUpper ())); - } - else - { - // partial key lookup - return m_->worked_.get ().end () - != m_->worked_.get ().find (ITU_zone); - } - } -} - - +#include "WorkedBefore.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "Configuration.hpp" +#include "revision_utils.hpp" +#include "Logger.hpp" +#include "qt_helpers.hpp" +#include "pimpl_impl.hpp" + +#include "moc_WorkedBefore.cpp" + +using namespace boost::multi_index; + +// hash function for QString members in hashed indexes +inline +std::size_t hash_value (QString const& s) +{ + return std::hash {} (s); +} + +// +// worked before set element +// +struct worked_entry +{ + explicit worked_entry (QString const& call, QString const& grid, QString const& band + , QString const& mode, QString const& country, AD1CCty::Continent continent + , int CQ_zone, int ITU_zone) + : call_ {call} + , grid_ {grid} + , band_ {band} + , mode_ {mode} + , country_ {country} + , continent_ {continent} + , CQ_zone_ {CQ_zone} + , ITU_zone_ {ITU_zone} + { + } + + QString call_; + QString grid_; + QString band_; + QString mode_; + QString country_; + AD1CCty::Continent continent_; + int CQ_zone_; + int ITU_zone_; +}; + +bool operator == (worked_entry const& lhs, worked_entry const& rhs) +{ + return + lhs.continent_ == rhs.continent_ // check 1st as it is fast + && lhs.CQ_zone_ == rhs.CQ_zone_ // ditto + && lhs.ITU_zone_ == rhs.ITU_zone_ // ditto + && lhs.call_ == rhs.call_ // check the rest in decreasing + && lhs.grid_ == rhs.grid_ // domain size order to shortcut + && lhs.country_ == rhs.country_ // differences as quickly as possible + && lhs.band_ == rhs.band_ + && lhs.mode_ == rhs.mode_; +} + +std::size_t hash_value (worked_entry const& we) +{ + std::size_t seed {0}; + boost::hash_combine (seed, we.call_); + boost::hash_combine (seed, we.grid_); + boost::hash_combine (seed, we.band_); + boost::hash_combine (seed, we.mode_); + boost::hash_combine (seed, we.country_); + boost::hash_combine (seed, we.continent_); + boost::hash_combine (seed, we.CQ_zone_); + boost::hash_combine (seed, we.ITU_zone_); + return seed; +} + +#if !defined (QT_NO_DEBUG_STREAM) +QDebug operator << (QDebug dbg, worked_entry const& e) +{ + QDebugStateSaver saver {dbg}; + dbg.nospace () << "worked_entry(" + << e.call_ << ", " + << e.grid_ << ", " + << e.band_ << ", " + << e.mode_ << ", " + << e.country_ << ", " + << e.continent_ << ", " + << e.CQ_zone_ << ", " + << e.ITU_zone_ << ')'; + return dbg; +} +#endif + +// less then predidate for the Continent enum class, needed for +// ordered indexes +struct Continent_less +{ + bool operator () (AD1CCty::Continent lhs, AD1CCty::Continent rhs) const + { + return static_cast (lhs) < static_cast (rhs); + } +}; + +// index tags +struct call_mode_band {}; +struct call_band {}; +struct grid_mode_band {}; +struct grid_band {}; +struct entity_mode_band {}; +struct entity_band {}; +struct continent_mode_band {}; +struct continent_band {}; +struct CQ_zone_mode_band {}; +struct CQ_zone_band {}; +struct ITU_zone_mode_band {}; +struct ITU_zone_band {}; + +// set with multiple ordered unique indexes that allow for optimally +// efficient determination of various categories of worked before +// status +typedef multi_index_container< + worked_entry, + indexed_by< + // basic unordered set constraint - we don't need duplicate worked entries + hashed_unique>, + + // + // The following indexes are used to discover worked before stuff. + // + // They are ordered so as to support partial lookups and + // non-unique because container inserts must be valid for all + // indexes. + // + + // call+mode+band + ordered_non_unique, + composite_key, + member, + member > >, + // call+band + ordered_non_unique, + composite_key, + member > >, + // grid+mode+band + ordered_non_unique, + composite_key, + member, + member > >, + // grid+band + ordered_non_unique, + composite_key, + member > >, + // country+mode+band + ordered_non_unique, + composite_key, + member, + member > >, + // country+band + ordered_non_unique, + composite_key, + member > >, + // continent+mode+band + ordered_non_unique, + composite_key, + member, + member >, + composite_key_compare, std::less > >, + // continent+band + ordered_non_unique, + composite_key, + member >, + composite_key_compare > >, + // CQ-zone+mode+band + ordered_non_unique, + composite_key, + member, + member > >, + // CQ-zone+band + ordered_non_unique, + composite_key, + member > >, + // ITU-zone+mode+band + ordered_non_unique, + composite_key, + member, + member > >, + // ITU-zone+band + ordered_non_unique, + composite_key, + member > > > + > worked_before_database_type; + +namespace +{ + auto const logFileName = "wsjtx_log.adi"; + + // Exception class suitable for using with QtConcurrent across + // thread boundaries + class LoaderException final + : public QException + { + public: + LoaderException (std::exception const& e) : error_ {e.what ()} {} + QString error () const {return error_;} + void raise () const override {throw *this;} + LoaderException * clone () const override {return new LoaderException {*this};} + private: + QString error_; + }; + + QString extractField (QString const& record, QString const& fieldName) + { + int fieldNameIndex = record.indexOf ('<' + fieldName + ':', 0, Qt::CaseInsensitive); + if (fieldNameIndex >=0) + { + int closingBracketIndex = record.indexOf('>',fieldNameIndex); + int fieldLengthIndex = record.indexOf(':',fieldNameIndex); // find the size delimiter + int dataTypeIndex = -1; + if (fieldLengthIndex >= 0) + { + dataTypeIndex = record.indexOf(':',fieldLengthIndex+1); // check for a second : indicating there is a data type + if (dataTypeIndex > closingBracketIndex) + dataTypeIndex = -1; // second : was found but it was beyond the closing > + } + else + { + throw LoaderException (std::runtime_error {QCoreApplication::translate ("WorkedBefore", "Invalid ADIF field %0: %1").arg (fieldName).arg (record).toLocal8Bit ()}); + } + + if (closingBracketIndex > fieldNameIndex && fieldLengthIndex > fieldNameIndex && fieldLengthIndex < closingBracketIndex) + { + int fieldLengthCharCount = closingBracketIndex - fieldLengthIndex -1; + if (dataTypeIndex >= 0) + fieldLengthCharCount -= 2; // data type indicator is always a colon followed by a single character + QString fieldLengthString = record.mid(fieldLengthIndex+1,fieldLengthCharCount); + int fieldLength = fieldLengthString.toInt(); + if (fieldLength > 0) + { + return record.mid(closingBracketIndex+1,fieldLength); + } + } + else + { + throw LoaderException (std::runtime_error {QCoreApplication::translate ("WorkedBefore", "Malformed ADIF field %0: %1").arg (fieldName).arg (record).toLocal8Bit ()}); + } + } + return QString {}; + } + + worked_before_database_type loader (QString const& path, AD1CCty const * prefixes) + { + worked_before_database_type worked; + QFile inputFile {path}; + if (inputFile.exists ()) + { + if (inputFile.open (QFile::ReadOnly)) + { + QTextStream in {&inputFile}; + QString buffer; + bool pre_read {false}; + int end_position {-1}; + + // skip optional header record + do + { + buffer += in.readLine () + '\n'; + if (buffer.startsWith (QChar {'<'})) // denotes no header + { + pre_read = true; + } + else + { + end_position = buffer.indexOf ("", 0, Qt::CaseInsensitive); + } + } + while (!in.atEnd () && !pre_read && end_position < 0); + if (!pre_read) // found header + { + if (end_position < 0) + { + throw LoaderException (std::runtime_error {QCoreApplication::translate ("WorkedBefore", "Invalid ADIF header").toLocal8Bit ()}); + } + buffer.remove (0, end_position + 5); + } + while (!in.atEnd ()) + { + end_position = buffer.indexOf ("", 0, Qt::CaseInsensitive); + do + { + if (!in.atEnd () && end_position < 0) + { + buffer += in.readLine () + '\n'; + } + } + while ((end_position = buffer.indexOf ("", 0, Qt::CaseInsensitive)) < 0 && !in.atEnd ()); + if (end_position >= 0) // require valid ADIF record + // with terminator + { + auto record = buffer.left (end_position + 5).trimmed (); + auto next_record = buffer.indexOf (QChar {'<'}, end_position + 5); + buffer.remove (0, next_record >=0 ? next_record : buffer.size ()); + record = record.mid (record.indexOf (QChar {'<'})); + auto call = extractField (record, "CALL"); + if (call.size ()) // require CALL field before we + // will parse a record + { + auto const& entity = prefixes->lookup (call); + auto mode = extractField (record, "MODE").toUpper (); + if (!mode.size () || "MFSK" == mode) + { + mode = extractField (record, "SUBMODE").toUpper (); + } + worked.emplace (call.toUpper () + , extractField (record, "GRIDSQUARE").left (4).toUpper () // not interested in 6-digit grids + , extractField (record, "BAND").toUpper () + , mode + , entity.entity_name + , entity.continent + , entity.CQ_zone + , entity.ITU_zone); + } + } + } + } + else + { + throw LoaderException (std::runtime_error {QCoreApplication::translate ("WorkedBefore", "Error opening ADIF log file for read: %0").arg (inputFile.errorString ()).toLocal8Bit ()}); + } + } + return worked; + } +} + +class WorkedBefore::impl final +{ +public: + impl (Configuration const * configuration) + : configuration_ {configuration} + , path_ {QDir {QStandardPaths::writableLocation (QStandardPaths::DataLocation)}.absoluteFilePath (logFileName)} + , prefixes_ {configuration} + { + } + + void reload () + { + prefixes_.reload (configuration_); + async_loader_ = QtConcurrent::run (loader, path_, &prefixes_); + loader_watcher_.setFuture (async_loader_); + } + + Configuration const * configuration_; + QString path_; + AD1CCty prefixes_; + QFutureWatcher loader_watcher_; + QFuture async_loader_; + worked_before_database_type worked_; +}; + +WorkedBefore::WorkedBefore (Configuration const * configuration) + : m_ {configuration} +{ + Q_ASSERT (configuration); + connect (&m_->loader_watcher_, &QFutureWatcher::finished, [this] () { + QString error; + size_t n {0}; + try + { + m_->worked_ = m_->loader_watcher_.result (); + n = m_->worked_.size (); + } + catch (LoaderException const& e) + { + error = e.error (); + } + QString cty_ver = m_->prefixes_.version(); + LOG_DEBUG(QString{"WorkedBefore::reload: CTY.DAT version %1"}.arg (cty_ver)); + Q_EMIT finished_loading (n, cty_ver, error); + }); + reload (); +} + +QString WorkedBefore::cty_version () const +{ + return m_->prefixes_.version (); +} + +void WorkedBefore::reload () +{ + m_->reload (); +} + +WorkedBefore::~WorkedBefore () +{ +} + +QString const& WorkedBefore::path () const +{ + return m_->path_; +} + +AD1CCty const * WorkedBefore::countries () const +{ + return &m_->prefixes_; +} + +bool WorkedBefore::add (QString const& call + , QString const& grid + , QString const& band + , QString const& mode + , QByteArray const& ADIF_record) +{ + if (call.size ()) + { + auto const& entity = m_->prefixes_.lookup (call); + QFile file {m_->path_}; + if (!file.open(QIODevice::Text | QIODevice::Append)) + { + return false; + } + else + { + QTextStream out {&file}; + if (!file.size ()) + { + auto ts = QDateTime::currentDateTimeUtc ().toString ("yyyyMMdd HHmmss"); + auto ver = version (true); + out << // new file + QString { + "ADIF Export\n" + "3.1.1\n" + "%0\n" + "WSJT-X\n" + "%2\n" + "" + }.arg (ts).arg (ver.size ()).arg (ver) + << +#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) + endl +#else + Qt::endl +#endif + ; + } + out << ADIF_record << " " << +#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) + endl +#else + Qt::endl +#endif + ; + } + m_->worked_.emplace (call.toUpper (), grid.left (4).toUpper (), band.toUpper (), mode.toUpper () + , entity.entity_name, entity.continent, entity.CQ_zone, entity.ITU_zone); + } + return true; +} + +bool WorkedBefore::country_worked (QString const& country, QString const& mode, QString const& band) const +{ + if (mode.size ()) + { + if (band.size ()) + { + return + country.size () + && m_->worked_.get ().end () + != m_->worked_.get ().find (std::make_tuple (country, mode.toUpper (), band.toUpper ())); + } + else + { + // partial key lookup + return + country.size () + && m_->worked_.get ().end () + != m_->worked_.get ().find (std::make_tuple (country, mode.toUpper ())); + } + } + else + { + if (band.size ()) + { + return + country.size () + && m_->worked_.get ().end () + != m_->worked_.get ().find (std::make_tuple (country, band.toUpper ())); + } + else + { + // partial key lookup + return + country.size () + && m_->worked_.get ().end () + != m_->worked_.get ().find (country); + } + } +} + +bool WorkedBefore::grid_worked (QString const& grid, QString const& mode, QString const& band) const +{ + auto gridsquare = grid.left (4).toUpper (); + if (m_->configuration_->highlight_only_fields ()) + { + // can't use a direct set find operation or a set operation with + // a (CompatibleKey, CompatibleCompare) concept so we must + // partially scan the index + auto range = boost::make_iterator_range ( + m_->worked_.get ().lower_bound (gridsquare.left (2)) + , m_->worked_.get ().upper_bound (gridsquare.left (2) + "99")); + for (worked_entry const& worked : range) + { + if ((!mode.size () || mode.toUpper () == worked.mode_) + && (!band.size () || worked.band_ == band.toUpper ())) + { + return true; + } + } + } + else + { + if (mode.size ()) + { + if (band.size ()) + { + return m_->worked_.get ().end () + != m_->worked_.get ().find (std::make_tuple (gridsquare, mode.toUpper (), band.toUpper ())); + } + else + { + // partial key lookup + return m_->worked_.get ().end () + != m_->worked_.get ().find (std::make_tuple (gridsquare, mode.toUpper ())); + } + } + else + { + if (band.size ()) + { + return m_->worked_.get ().end () + != m_->worked_.get ().find (std::make_tuple (gridsquare, band.toUpper ())); + } + else + { + // partial key lookup + return m_->worked_.get ().end () + != m_->worked_.get ().find (gridsquare); + } + } + } + return false; +} + +bool WorkedBefore::call_worked (QString const& call, QString const& mode, QString const& band) const +{ + if (mode.size ()) + { + if (band.size ()) + { + return m_->worked_.get ().end () + != m_->worked_.get ().find (std::make_tuple (call.toUpper (), mode.toUpper (), band.toUpper ())); + } + else + { + // partial key lookup + return m_->worked_.get ().end () + != m_->worked_.get ().find (std::make_tuple (call.toUpper (), mode.toUpper ())); + } + } + else + { + if (band.size ()) + { + return m_->worked_.get ().end () + != m_->worked_.get ().find (std::make_tuple (call.toUpper (), band.toUpper ())); + } + else + { + // partial key lookup + return m_->worked_.get ().end () + != m_->worked_.get ().find (std::make_tuple (call.toUpper ())); + } + } +} + +bool WorkedBefore::continent_worked (Continent continent, QString const& mode, QString const& band) const +{ + if (mode.size ()) + { + if (band.size ()) + { + return m_->worked_.get ().end () + != m_->worked_.get ().find (std::make_tuple (continent, mode.toUpper (), band.toUpper ())); + } + else + { + // partial key lookup + return m_->worked_.get ().end () + != m_->worked_.get ().find (std::make_tuple (continent, mode.toUpper ())); + } + } + else + { + if (band.size ()) + { + return m_->worked_.get ().end () + != m_->worked_.get ().find (std::make_tuple (continent, band.toUpper ())); + } + else + { + // partial key lookup + return m_->worked_.get ().end () + != m_->worked_.get ().find (continent); + } + } +} + +bool WorkedBefore::CQ_zone_worked (int CQ_zone, QString const& mode, QString const& band) const +{ + if (mode.size ()) + { + if (band.size ()) + { + return m_->worked_.get ().end () + != m_->worked_.get ().find (std::make_tuple (CQ_zone, mode.toUpper (), band.toUpper ())); + } + else + { + // partial key lookup + return m_->worked_.get ().end () + != m_->worked_.get ().find (std::make_tuple (CQ_zone, mode.toUpper ())); + } + } + else + { + if (band.size ()) + { + return m_->worked_.get ().end () + != m_->worked_.get ().find (std::make_tuple (CQ_zone, band.toUpper ())); + } + else + { + // partial key lookup + return m_->worked_.get ().end () + != m_->worked_.get ().find (CQ_zone); + } + } +} + + +bool WorkedBefore::ITU_zone_worked (int ITU_zone, QString const& mode, QString const& band) const +{ + if (mode.size ()) + { + if (band.size ()) + { + return m_->worked_.get ().end () + != m_->worked_.get ().find (std::make_tuple (ITU_zone, mode.toUpper (), band.toUpper ())); + } + else + { + // partial key lookup + return m_->worked_.get ().end () + != m_->worked_.get ().find (std::make_tuple (ITU_zone, mode.toUpper ())); + } + } + else + { + if (band.size ()) + { + return m_->worked_.get ().end () + != m_->worked_.get ().find (std::make_tuple (ITU_zone, band.toUpper ())); + } + else + { + // partial key lookup + return m_->worked_.get ().end () + != m_->worked_.get ().find (ITU_zone); + } + } +} + + diff --git a/logbook/WorkedBefore.hpp b/logbook/WorkedBefore.hpp index c1302f06..0be9d783 100755 --- a/logbook/WorkedBefore.hpp +++ b/logbook/WorkedBefore.hpp @@ -1,48 +1,48 @@ -#ifndef WORKWED_BEFORE_HPP_ -#define WORKWED_BEFORE_HPP_ - -#include -#include "AD1CCty.hpp" -#include "pimpl_h.hpp" - -class Configuration; -class CountryDat; -class QString; -class QByteArray; - -class WorkedBefore final - : public QObject -{ - Q_OBJECT - -public: - using Continent = AD1CCty::Continent; - - explicit WorkedBefore (Configuration const *); - ~WorkedBefore (); - - Q_SLOT void reload (); - Q_SLOT bool add (QString const& call - , QString const& grid - , QString const& band - , QString const& mode - , QByteArray const& ADIF_record); - - QString const& path () const; - AD1CCty const * countries () const; - bool country_worked (QString const& call, QString const& mode, QString const& band) const; - bool grid_worked (QString const& grid, QString const& mode, QString const& band) const; - bool call_worked (QString const& call, QString const& mode, QString const& band) const; - bool continent_worked (Continent continent, QString const& mode, QString const& band) const; - bool CQ_zone_worked (int CQ_zone, QString const& mode, QString const& band) const; - bool ITU_zone_worked (int ITU_zone, QString const& mode, QString const& band) const; - QString cty_version () const; - - Q_SIGNAL void finished_loading (int worked_before_record_count, QString const, QString const& error) const; - -private: - class impl; - pimpl m_; -}; - -#endif +#ifndef WORKWED_BEFORE_HPP_ +#define WORKWED_BEFORE_HPP_ + +#include +#include "AD1CCty.hpp" +#include "pimpl_h.hpp" + +class Configuration; +class CountryDat; +class QString; +class QByteArray; + +class WorkedBefore final + : public QObject +{ + Q_OBJECT + +public: + using Continent = AD1CCty::Continent; + + explicit WorkedBefore (Configuration const *); + ~WorkedBefore (); + + Q_SLOT void reload (); + Q_SLOT bool add (QString const& call + , QString const& grid + , QString const& band + , QString const& mode + , QByteArray const& ADIF_record); + + QString const& path () const; + AD1CCty const * countries () const; + bool country_worked (QString const& call, QString const& mode, QString const& band) const; + bool grid_worked (QString const& grid, QString const& mode, QString const& band) const; + bool call_worked (QString const& call, QString const& mode, QString const& band) const; + bool continent_worked (Continent continent, QString const& mode, QString const& band) const; + bool CQ_zone_worked (int CQ_zone, QString const& mode, QString const& band) const; + bool ITU_zone_worked (int ITU_zone, QString const& mode, QString const& band) const; + QString cty_version () const; + + Q_SIGNAL void finished_loading (int worked_before_record_count, QString const, QString const& error) const; + +private: + class impl; + pimpl m_; +}; + +#endif diff --git a/logbook/countriesworked.cpp b/logbook/countriesworked.cpp index 0caed9df..bcd73af7 100755 --- a/logbook/countriesworked.cpp +++ b/logbook/countriesworked.cpp @@ -1,24 +1,24 @@ -#include "countriesworked.h" - -#include - -#include "pimpl_impl.hpp" - -class CountriesWorkedimpl final -{ -public: - // element is country-name+band where the '+' is actual a character - // which allows an efficient lower_bound() search for country-name+ - // to check for ATNOs - std::set worked_; -}; - -void CountriesWorked::add (QString const& country, QString const& band) -{ - m_->worked_.insert (country + '+' + band); -} - -bool CountriesWorked::contains (QString const& country, QString const& band) const -{ - return m_->worked_.end () != m_->worked_.lower_bound (country + '+' + band); -} +#include "countriesworked.h" + +#include + +#include "pimpl_impl.hpp" + +class CountriesWorkedimpl final +{ +public: + // element is country-name+band where the '+' is actual a character + // which allows an efficient lower_bound() search for country-name+ + // to check for ATNOs + std::set worked_; +}; + +void CountriesWorked::add (QString const& country, QString const& band) +{ + m_->worked_.insert (country + '+' + band); +} + +bool CountriesWorked::contains (QString const& country, QString const& band) const +{ + return m_->worked_.end () != m_->worked_.lower_bound (country + '+' + band); +} diff --git a/logbook/countriesworked.h b/logbook/countriesworked.h index c485a164..ac8aaea6 100755 --- a/logbook/countriesworked.h +++ b/logbook/countriesworked.h @@ -1,24 +1,24 @@ -#ifndef COUNTRIES_WORKDED_H_ -#define COUNTRIES_WORKDED_H_ - -#include - -#include "pimpl_h.hpp" - -class QStringList; - -class CountriesWorked final -{ - public: - explicit CountriesWorked (QStringList const& countryNames); - ~CountriesWorked (); - - void add (QString const& country, QString const& band); - bool contains (QString const& country, QString const& band = QString {}) const; - - private: - class impl; - pimpl m_; -}; - -#endif +#ifndef COUNTRIES_WORKDED_H_ +#define COUNTRIES_WORKDED_H_ + +#include + +#include "pimpl_h.hpp" + +class QStringList; + +class CountriesWorked final +{ + public: + explicit CountriesWorked (QStringList const& countryNames); + ~CountriesWorked (); + + void add (QString const& country, QString const& band); + bool contains (QString const& country, QString const& band = QString {}) const; + + private: + class impl; + pimpl m_; +}; + +#endif diff --git a/logbook/logbook.cpp b/logbook/logbook.cpp index 3132a25a..2d434029 100755 --- a/logbook/logbook.cpp +++ b/logbook/logbook.cpp @@ -1,215 +1,215 @@ -#include "logbook.h" - -#include -#include "Configuration.hpp" -#include "AD1CCty.hpp" -#include "Multiplier.hpp" -#include "logbook/AD1CCty.hpp" -#include "models/CabrilloLog.hpp" -#include "models/FoxLog.hpp" - -#include "moc_logbook.cpp" - -LogBook::LogBook (Configuration const * configuration) - : config_ {configuration} - , worked_before_ {configuration} -{ - Q_ASSERT (configuration); - connect (&worked_before_, &WorkedBefore::finished_loading, this, &LogBook::finished_loading); -} - -LogBook::~LogBook () -{ -} - -void LogBook::match (QString const& call, QString const& mode, QString const& grid, - AD1CCty::Record const& looked_up, - bool& callB4, - bool& countryB4, - bool& gridB4, - bool& continentB4, - bool& CQZoneB4, - bool& ITUZoneB4, - QString const& band) const -{ - if (call.size() > 0) - { - auto const& mode_to_check = (config_ && !config_->highlight_by_mode ()) ? QString {} : mode; - callB4 = worked_before_.call_worked (call, mode_to_check, band); - gridB4 = worked_before_.grid_worked(grid, mode_to_check, band); - auto const& countryName = looked_up.entity_name; - if (countryName.size ()) - { - countryB4 = worked_before_.country_worked (countryName, mode_to_check, band); - continentB4 = worked_before_.continent_worked (looked_up.continent, mode_to_check, band); - CQZoneB4 = worked_before_.CQ_zone_worked (looked_up.CQ_zone, mode_to_check, band); - ITUZoneB4 = worked_before_.ITU_zone_worked (looked_up.ITU_zone, mode_to_check, band); - } - else - { - countryB4 = true; // we don't want to flag unknown entities - continentB4 = true; - CQZoneB4 = true; - ITUZoneB4 = true; - } - } -} - -bool LogBook::add (QString const& call - , QString const& grid - , QString const& band - , QString const& mode - , QByteArray const& ADIF_record) -{ - return worked_before_.add (call, grid, band, mode, ADIF_record); -} - -void LogBook::rescan () -{ - worked_before_.reload (); -} - -QString const LogBook::cty_version() const -{ - return worked_before_.cty_version(); -} - -QByteArray LogBook::QSOToADIF (QString const& hisCall, QString const& hisGrid, QString const& mode, - QString const& rptSent, QString const& rptRcvd, QDateTime const& dateTimeOn, - QDateTime const& dateTimeOff, QString const& band, QString const& comments, - QString const& name, QString const& strDialFreq, QString const& myCall, - QString const& myGrid, QString const& txPower, QString const& operator_call, - QString const& xSent, QString const& xRcvd, QString const& propmode) -{ - QString t; - t = "" + hisCall; - t += " " + hisGrid; - if (mode != "FT4" && mode != "FST4" && mode != "Q65") - { - t += " " + mode; - } - else - { - t += " MFSK " + mode; - } - t += " " + rptSent; - t += " " + rptRcvd; - t += " " + dateTimeOn.date().toString("yyyyMMdd"); - t += " " + dateTimeOn.time().toString("hhmmss"); - t += " " + dateTimeOff.date().toString("yyyyMMdd"); - t += " " + dateTimeOff.time().toString("hhmmss"); - t += " " + band; - t += " " + strDialFreq; - t += " " + myCall; - if(myGrid!="") t += " " + myGrid; - if(txPower!="") t += " " + txPower; - if(comments!="") t += " " + comments; - if(name!="") t += " " + name; - if(operator_call!="") t+=" " + operator_call; - if(propmode!="") t += " " + propmode; - if (xSent.size ()) - { - auto words = xSent.split (' ' -#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) - , QString::SkipEmptyParts -#else - , Qt::SkipEmptyParts -#endif - ); - if (words.size () > 1) - { - if (words.back ().toUInt ()) - { - // assume last word is a serial if there are at least - // two words and if it is positive numeric - t += " ' + words.back (); - } - else - { - if (words.front ().toUInt () && words.front ().size () > 3) // EU VHF contest mode - { - auto sn_text = words.front ().mid (2); - // assume first word is report+serial if there are - // at least two words and if the first word less the - // first two characters is a positive numeric - t += " ' + sn_text; - } - } - } - } - if (xRcvd.size ()) { - auto words = xRcvd.split (' ' -#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) - , QString::SkipEmptyParts -#else - , Qt::SkipEmptyParts -#endif - ); - if (words.size () == 2) - { - if (words.at (1).toUInt ()) - { - t += " " + words.at (1); - } - else if (words.at (0).toUInt () && words.at (0).size () > 3) // EU VHF contest exchange - { - // strip report and set SRX to serial - t += " " + words.at (0).mid (2); - } - else - { - if (Configuration::SpecialOperatingActivity::FIELD_DAY == config_->special_op_id ()) - { - // include DX as an ARRL_SECT value even though it is - // not in the ADIF spec ARRL_SECT enumeration, done - // because N1MM does the same - t += " ARRL-FIELD-DAY ' + xRcvd - + " ' - + words.at (0) + " ' + words.at (1); - } - else if (Configuration::SpecialOperatingActivity::RTTY == config_->special_op_id ()) - { - t += " " + words.at (1); - } - } - } - } - return t.toLatin1(); -} - -CabrilloLog * LogBook::contest_log () -{ - // lazy create of Cabrillo log object instance - if (!contest_log_) - { - contest_log_.reset (new CabrilloLog {config_}); - if (!multiplier_) - { - multiplier_.reset (new Multiplier {countries ()}); - } - connect (contest_log_.data (), &CabrilloLog::data_changed, [this] () { - multiplier_->reload (contest_log_.data ()); - }); - } - return contest_log_.data (); -} - -Multiplier const * LogBook::multiplier () const -{ - // lazy create of Multiplier object instance - if (!multiplier_) - { - multiplier_.reset (new Multiplier {countries ()}); - } - return multiplier_.data (); -} - -FoxLog * LogBook::fox_log () -{ - // lazy create of Fox log object instance - if (!fox_log_) - { - fox_log_.reset (new FoxLog {config_}); - } - return fox_log_.data (); -} +#include "logbook.h" + +#include +#include "Configuration.hpp" +#include "AD1CCty.hpp" +#include "Multiplier.hpp" +#include "logbook/AD1CCty.hpp" +#include "models/CabrilloLog.hpp" +#include "models/FoxLog.hpp" + +#include "moc_logbook.cpp" + +LogBook::LogBook (Configuration const * configuration) + : config_ {configuration} + , worked_before_ {configuration} +{ + Q_ASSERT (configuration); + connect (&worked_before_, &WorkedBefore::finished_loading, this, &LogBook::finished_loading); +} + +LogBook::~LogBook () +{ +} + +void LogBook::match (QString const& call, QString const& mode, QString const& grid, + AD1CCty::Record const& looked_up, + bool& callB4, + bool& countryB4, + bool& gridB4, + bool& continentB4, + bool& CQZoneB4, + bool& ITUZoneB4, + QString const& band) const +{ + if (call.size() > 0) + { + auto const& mode_to_check = (config_ && !config_->highlight_by_mode ()) ? QString {} : mode; + callB4 = worked_before_.call_worked (call, mode_to_check, band); + gridB4 = worked_before_.grid_worked(grid, mode_to_check, band); + auto const& countryName = looked_up.entity_name; + if (countryName.size ()) + { + countryB4 = worked_before_.country_worked (countryName, mode_to_check, band); + continentB4 = worked_before_.continent_worked (looked_up.continent, mode_to_check, band); + CQZoneB4 = worked_before_.CQ_zone_worked (looked_up.CQ_zone, mode_to_check, band); + ITUZoneB4 = worked_before_.ITU_zone_worked (looked_up.ITU_zone, mode_to_check, band); + } + else + { + countryB4 = true; // we don't want to flag unknown entities + continentB4 = true; + CQZoneB4 = true; + ITUZoneB4 = true; + } + } +} + +bool LogBook::add (QString const& call + , QString const& grid + , QString const& band + , QString const& mode + , QByteArray const& ADIF_record) +{ + return worked_before_.add (call, grid, band, mode, ADIF_record); +} + +void LogBook::rescan () +{ + worked_before_.reload (); +} + +QString const LogBook::cty_version() const +{ + return worked_before_.cty_version(); +} + +QByteArray LogBook::QSOToADIF (QString const& hisCall, QString const& hisGrid, QString const& mode, + QString const& rptSent, QString const& rptRcvd, QDateTime const& dateTimeOn, + QDateTime const& dateTimeOff, QString const& band, QString const& comments, + QString const& name, QString const& strDialFreq, QString const& myCall, + QString const& myGrid, QString const& txPower, QString const& operator_call, + QString const& xSent, QString const& xRcvd, QString const& propmode) +{ + QString t; + t = "" + hisCall; + t += " " + hisGrid; + if (mode != "FT4" && mode != "FST4" && mode != "Q65") + { + t += " " + mode; + } + else + { + t += " MFSK " + mode; + } + t += " " + rptSent; + t += " " + rptRcvd; + t += " " + dateTimeOn.date().toString("yyyyMMdd"); + t += " " + dateTimeOn.time().toString("hhmmss"); + t += " " + dateTimeOff.date().toString("yyyyMMdd"); + t += " " + dateTimeOff.time().toString("hhmmss"); + t += " " + band; + t += " " + strDialFreq; + t += " " + myCall; + if(myGrid!="") t += " " + myGrid; + if(txPower!="") t += " " + txPower; + if(comments!="") t += " " + comments; + if(name!="") t += " " + name; + if(operator_call!="") t+=" " + operator_call; + if(propmode!="") t += " " + propmode; + if (xSent.size ()) + { + auto words = xSent.split (' ' +#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) + , QString::SkipEmptyParts +#else + , Qt::SkipEmptyParts +#endif + ); + if (words.size () > 1) + { + if (words.back ().toUInt ()) + { + // assume last word is a serial if there are at least + // two words and if it is positive numeric + t += " ' + words.back (); + } + else + { + if (words.front ().toUInt () && words.front ().size () > 3) // EU VHF contest mode + { + auto sn_text = words.front ().mid (2); + // assume first word is report+serial if there are + // at least two words and if the first word less the + // first two characters is a positive numeric + t += " ' + sn_text; + } + } + } + } + if (xRcvd.size ()) { + auto words = xRcvd.split (' ' +#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) + , QString::SkipEmptyParts +#else + , Qt::SkipEmptyParts +#endif + ); + if (words.size () == 2) + { + if (words.at (1).toUInt ()) + { + t += " " + words.at (1); + } + else if (words.at (0).toUInt () && words.at (0).size () > 3) // EU VHF contest exchange + { + // strip report and set SRX to serial + t += " " + words.at (0).mid (2); + } + else + { + if (Configuration::SpecialOperatingActivity::FIELD_DAY == config_->special_op_id ()) + { + // include DX as an ARRL_SECT value even though it is + // not in the ADIF spec ARRL_SECT enumeration, done + // because N1MM does the same + t += " ARRL-FIELD-DAY ' + xRcvd + + " ' + + words.at (0) + " ' + words.at (1); + } + else if (Configuration::SpecialOperatingActivity::RTTY == config_->special_op_id ()) + { + t += " " + words.at (1); + } + } + } + } + return t.toLatin1(); +} + +CabrilloLog * LogBook::contest_log () +{ + // lazy create of Cabrillo log object instance + if (!contest_log_) + { + contest_log_.reset (new CabrilloLog {config_}); + if (!multiplier_) + { + multiplier_.reset (new Multiplier {countries ()}); + } + connect (contest_log_.data (), &CabrilloLog::data_changed, [this] () { + multiplier_->reload (contest_log_.data ()); + }); + } + return contest_log_.data (); +} + +Multiplier const * LogBook::multiplier () const +{ + // lazy create of Multiplier object instance + if (!multiplier_) + { + multiplier_.reset (new Multiplier {countries ()}); + } + return multiplier_.data (); +} + +FoxLog * LogBook::fox_log () +{ + // lazy create of Fox log object instance + if (!fox_log_) + { + fox_log_.reset (new FoxLog {config_}); + } + return fox_log_.data (); +} diff --git a/logbook/logbook.h b/logbook/logbook.h index 74931d46..96a17dd6 100755 --- a/logbook/logbook.h +++ b/logbook/logbook.h @@ -1,65 +1,65 @@ -// -*- Mode: C++ -*- -/* - * From an ADIF file and cty.dat, get a call's DXCC entity and its worked before status - */ - -#ifndef LOG_BOOK_H_ -#define LOG_BOOK_H_ - -#include -#include -#include - -#include "WorkedBefore.hpp" - -class Configuration; -class QByteArray; -class QDateTime; -class CabrilloLog; -class Multiplier; -class FoxLog; - -class LogBook final - : public QObject -{ - Q_OBJECT - -public: - LogBook (Configuration const *); - ~LogBook (); - QString const& path () const {return worked_before_.path ();} - bool add (QString const& call - , QString const& grid - , QString const& band - , QString const& mode - , QByteArray const& ADIF_record); - AD1CCty const * countries () const {return worked_before_.countries ();} - void rescan (); - void match (QString const& call, QString const& mode, QString const& grid, - AD1CCty::Record const&, bool& callB4, bool& countryB4, - bool &gridB4, bool &continentB4, bool& CQZoneB4, bool& ITUZoneB4, - QString const& currentBand = QString {}) const; - QByteArray QSOToADIF (QString const& hisCall, QString const& hisGrid, QString const& mode, - QString const& rptSent, QString const& rptRcvd, QDateTime const& dateTimeOn, - QDateTime const& dateTimeOff, QString const& band, QString const& comments, - QString const& name, QString const& strDialFreq, QString const& myCall, - QString const& m_myGrid, QString const& m_txPower, QString const& operator_call, - QString const& xSent, QString const& xRcvd, QString const& propmode); - - QString const cty_version() const; - - Q_SIGNAL void finished_loading (int worked_before_record_count, QString const cty_version, QString const& error) const; - - CabrilloLog * contest_log (); - Multiplier const * multiplier () const; - FoxLog * fox_log (); - -private: - Configuration const * config_; - WorkedBefore worked_before_; - QScopedPointer contest_log_; - QScopedPointer mutable multiplier_; - QScopedPointer fox_log_; -}; - -#endif +// -*- Mode: C++ -*- +/* + * From an ADIF file and cty.dat, get a call's DXCC entity and its worked before status + */ + +#ifndef LOG_BOOK_H_ +#define LOG_BOOK_H_ + +#include +#include +#include + +#include "WorkedBefore.hpp" + +class Configuration; +class QByteArray; +class QDateTime; +class CabrilloLog; +class Multiplier; +class FoxLog; + +class LogBook final + : public QObject +{ + Q_OBJECT + +public: + LogBook (Configuration const *); + ~LogBook (); + QString const& path () const {return worked_before_.path ();} + bool add (QString const& call + , QString const& grid + , QString const& band + , QString const& mode + , QByteArray const& ADIF_record); + AD1CCty const * countries () const {return worked_before_.countries ();} + void rescan (); + void match (QString const& call, QString const& mode, QString const& grid, + AD1CCty::Record const&, bool& callB4, bool& countryB4, + bool &gridB4, bool &continentB4, bool& CQZoneB4, bool& ITUZoneB4, + QString const& currentBand = QString {}) const; + QByteArray QSOToADIF (QString const& hisCall, QString const& hisGrid, QString const& mode, + QString const& rptSent, QString const& rptRcvd, QDateTime const& dateTimeOn, + QDateTime const& dateTimeOff, QString const& band, QString const& comments, + QString const& name, QString const& strDialFreq, QString const& myCall, + QString const& m_myGrid, QString const& m_txPower, QString const& operator_call, + QString const& xSent, QString const& xRcvd, QString const& propmode); + + QString const cty_version() const; + + Q_SIGNAL void finished_loading (int worked_before_record_count, QString const cty_version, QString const& error) const; + + CabrilloLog * contest_log (); + Multiplier const * multiplier () const; + FoxLog * fox_log (); + +private: + Configuration const * config_; + WorkedBefore worked_before_; + QScopedPointer contest_log_; + QScopedPointer mutable multiplier_; + QScopedPointer fox_log_; +}; + +#endif diff --git a/logbook/logbook.pri b/logbook/logbook.pri index ed0bdfc6..e67cdebd 100755 --- a/logbook/logbook.pri +++ b/logbook/logbook.pri @@ -1,13 +1,13 @@ -SOURCES += \ - logbook/countriesworked.cpp \ - logbook/logbook.cpp \ - logbook/AD1CCty.cpp \ - logbook/WorkedBefore.cpp \ - logbook/Multiplier.cpp - -HEADERS += \ - logbook/WorkedBefore.hpp \ - logbook/logbook.h \ - logbook/countriesworked.h \ - logbook/AD1CCty.hpp \ - logbook/Multiplier.hpp +SOURCES += \ + logbook/countriesworked.cpp \ + logbook/logbook.cpp \ + logbook/AD1CCty.cpp \ + logbook/WorkedBefore.cpp \ + logbook/Multiplier.cpp + +HEADERS += \ + logbook/WorkedBefore.hpp \ + logbook/logbook.h \ + logbook/countriesworked.h \ + logbook/AD1CCty.hpp \ + logbook/Multiplier.hpp -- cgit v1.2.3