diff options
| author | Christoph Berg <myon@debian.org> | 2018-12-19 22:58:32 +0100 |
|---|---|---|
| committer | Christoph Berg <myon@debian.org> | 2018-12-19 22:58:32 +0100 |
| commit | e0737c56c1e384e05064a0c6e1ac46979371041b (patch) | |
| tree | a0123d694a2d36c43dabdf0a3907392fce11d457 /logbook | |
| parent | b4aebf2d137b892f53503b13785ecae900062ff7 (diff) | |
New upstream version 2.0.0+repackupstream/2.0.0+repack
Diffstat (limited to 'logbook')
| -rw-r--r-- | logbook/AD1CCty.cpp | 409 | ||||
| -rw-r--r-- | logbook/AD1CCty.hpp | 55 | ||||
| -rw-r--r-- | logbook/WorkedBefore.cpp | 645 | ||||
| -rw-r--r-- | logbook/WorkedBefore.hpp | 46 | ||||
| -rw-r--r-- | logbook/adif.cpp | 231 | ||||
| -rw-r--r-- | logbook/adif.h | 55 | ||||
| -rw-r--r-- | logbook/countriesworked.cpp | 50 | ||||
| -rw-r--r-- | logbook/countriesworked.h | 31 | ||||
| -rw-r--r-- | logbook/countrydat.cpp | 160 | ||||
| -rw-r--r-- | logbook/countrydat.h | 36 | ||||
| -rw-r--r-- | logbook/logbook.cpp | 173 | ||||
| -rw-r--r-- | logbook/logbook.h | 60 | ||||
| -rw-r--r-- | logbook/logbook.pri | 11 |
13 files changed, 1323 insertions, 639 deletions
diff --git a/logbook/AD1CCty.cpp b/logbook/AD1CCty.cpp new file mode 100644 index 00000000..58ed375f --- /dev/null +++ b/logbook/AD1CCty.cpp @@ -0,0 +1,409 @@ +#include "AD1CCty.hpp" + +#include <string> +#include <stdexcept> +#include <algorithm> +#include <boost/multi_index_container.hpp> +#include <boost/multi_index/hashed_index.hpp> +#include <boost/multi_index/ordered_index.hpp> +#include <boost/multi_index/key_extractors.hpp> +#include <boost/lambda/lambda.hpp> +#include <boost/lexical_cast.hpp> +#include <QString> +#include <QStandardPaths> +#include <QDir> +#include <QFile> +#include <QTextStream> +#include <QDebug> +#include <QDebugStateSaver> +#include "Radio.hpp" +#include "pimpl_impl.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<tag<id>, member<entity, int, &entity::id_> >, + hashed_unique<tag<primary_prefix>, member<entity, QString, &entity::primary_prefix_>, 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<const_mem_fun<prefix, QString, &prefix::prefix_key> > > + > prefixes_type; + +class AD1CCty::impl final +{ +public: + explicit impl () + { + } + + Record fixup (QString call, prefix const& p) const + { + call = call.toUpper (); + using entity_by_id = entities_type::index<id>::type; + 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 + e = entities_.project<id> (entities_.get<primary_prefix> ().find ("K")); + } + else + { + e = entities_.get<id> ().find (p.entity_id_); + } + + 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<int> (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; + } + + QString path_; + 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"; + } +} + +AD1CCty::AD1CCty () +{ + QDir dataPath {QStandardPaths::writableLocation (QStandardPaths::DataLocation)}; + m_->path_ = dataPath.exists (file_name) + ? dataPath.absoluteFilePath (file_name) // user override + : QString {":/"} + file_name; // or original in the resources FS + QFile file {m_->path_}; + if (file.open (QFile::ReadOnly)) + { + int entity_id = 0; + int line_number {0}; + 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; + m_->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<int> (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<std::string> (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; + } + m_->prefixes_.emplace (prefix, exact, entity_id); + } + } + } + } + } +} + +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 (call, *p); + } + } + while (search_prefix.size ()) + { + auto p = m_->prefixes_.find (search_prefix); + if (p != m_->prefixes_.end ()) + { + if (!p->exact_ || call.size () == search_prefix.size ()) + { + return m_->fixup (call, *p); + } + } + search_prefix = search_prefix.left (search_prefix.size () - 1); + } + } + return Record {}; +} diff --git a/logbook/AD1CCty.hpp b/logbook/AD1CCty.hpp new file mode 100644 index 00000000..37cada3e --- /dev/null +++ b/logbook/AD1CCty.hpp @@ -0,0 +1,55 @@ +#ifndef AD1C_CTY_HPP_ +#define AD1C_CTY_HPP_ + +#include <boost/core/noncopyable.hpp> +#include <QObject> +#include "pimpl_h.hpp" + +// +// AD1CCty - Fast access database of Jim Reisert, AD1C's, cty.dat +// entity and entity override information file. +// +class AD1CCty final + : public QObject + , private boost::noncopyable +{ + 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 (); + ~AD1CCty (); + Record lookup (QString const& call) const; + +private: + class impl; + pimpl<impl> m_; +}; + +#if !defined (QT_NO_DEBUG_STREAM) +QDebug operator << (QDebug, AD1CCty::Record const&); +#endif + +#endif diff --git a/logbook/WorkedBefore.cpp b/logbook/WorkedBefore.cpp new file mode 100644 index 00000000..160e1fa8 --- /dev/null +++ b/logbook/WorkedBefore.cpp @@ -0,0 +1,645 @@ +#include "WorkedBefore.hpp" + +#include <functional> +#include <stdexcept> +#include <boost/functional/hash.hpp> +#include <boost/multi_index_container.hpp> +#include <boost/multi_index/hashed_index.hpp> +#include <boost/multi_index/ordered_index.hpp> +#include <boost/multi_index/key_extractors.hpp> +#include <QtConcurrent/QtConcurrentRun> +#include <QFuture> +#include <QFutureWatcher> +#include <QChar> +#include <QString> +#include <QByteArray> +#include <QStandardPaths> +#include <QDir> +#include <QFileInfo> +#include <QFile> +#include <QTextStream> + +#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<QString> {} (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<int> (lhs) < static_cast<int> (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<identity<worked_entry>>, + + // + // 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<tag<call_mode_band>, + composite_key<worked_entry, + member<worked_entry, QString, &worked_entry::call_>, + member<worked_entry, QString, &worked_entry::mode_>, + member<worked_entry, QString, &worked_entry::band_> > >, + // call+band + ordered_non_unique<tag<call_band>, + composite_key<worked_entry, + member<worked_entry, QString, &worked_entry::call_>, + member<worked_entry, QString, &worked_entry::band_> > >, + // grid+mode+band + ordered_non_unique<tag<grid_mode_band>, + composite_key<worked_entry, + member<worked_entry, QString, &worked_entry::grid_>, + member<worked_entry, QString, &worked_entry::mode_>, + member<worked_entry, QString, &worked_entry::band_> > >, + // grid+band + ordered_non_unique<tag<grid_band>, + composite_key<worked_entry, + member<worked_entry, QString, &worked_entry::grid_>, + member<worked_entry, QString, &worked_entry::band_> > >, + // country+mode+band + ordered_non_unique<tag<entity_mode_band>, + composite_key<worked_entry, + member<worked_entry, QString, &worked_entry::country_>, + member<worked_entry, QString, &worked_entry::mode_>, + member<worked_entry, QString, &worked_entry::band_> > >, + // country+band + ordered_non_unique<tag<entity_band>, + composite_key<worked_entry, + member<worked_entry, QString, &worked_entry::country_>, + member<worked_entry, QString, &worked_entry::band_> > >, + // continent+mode+band + ordered_non_unique<tag<continent_mode_band>, + composite_key<worked_entry, + member<worked_entry, AD1CCty::Continent, &worked_entry::continent_>, + member<worked_entry, QString, &worked_entry::mode_>, + member<worked_entry, QString, &worked_entry::band_> >, + composite_key_compare<Continent_less, std::less<QString>, std::less<QString> > >, + // continent+band + ordered_non_unique<tag<continent_band>, + composite_key<worked_entry, + member<worked_entry, AD1CCty::Continent, &worked_entry::continent_>, + member<worked_entry, QString, &worked_entry::band_> >, + composite_key_compare<Continent_less, std::less<QString> > >, + // CQ-zone+mode+band + ordered_non_unique<tag<CQ_zone_mode_band>, + composite_key<worked_entry, + member<worked_entry, int, &worked_entry::CQ_zone_>, + member<worked_entry, QString, &worked_entry::mode_>, + member<worked_entry, QString, &worked_entry::band_> > >, + // CQ-zone+band + ordered_non_unique<tag<CQ_zone_band>, + composite_key<worked_entry, + member<worked_entry, int, &worked_entry::CQ_zone_>, + member<worked_entry, QString, &worked_entry::band_> > >, + // ITU-zone+mode+band + ordered_non_unique<tag<ITU_zone_mode_band>, + composite_key<worked_entry, + member<worked_entry, int, &worked_entry::ITU_zone_>, + member<worked_entry, QString, &worked_entry::mode_>, + member<worked_entry, QString, &worked_entry::band_> > >, + // ITU-zone+band + ordered_non_unique<tag<ITU_zone_band>, + composite_key<worked_entry, + member<worked_entry, int, &worked_entry::ITU_zone_>, + member<worked_entry, QString, &worked_entry::band_> > > > + > worked_before_database_type; + +namespace +{ + auto const logFileName = "wsjtx_log.adi"; + + // Expception 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 {"Invalid ADIF field " + fieldName.toStdString () + ": " + record.toStdString ()}); + } + + 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 {"Malformed ADIF field " + fieldName.toStdString () + ": " + record.toStdString ()}); + } + } + 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 ("<EOH>", 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 {"Invalid ADIF header"}); + } + buffer.remove (0, end_position + 5); + } + while (!in.atEnd ()) + { + end_position = buffer.indexOf ("<EOR>", 0, Qt::CaseInsensitive); + do + { + if (!in.atEnd () && end_position < 0) + { + buffer += in.readLine () + '\n'; + } + } + while ((end_position = buffer.indexOf ("<EOR>", 0, Qt::CaseInsensitive)) < 0 && !in.atEnd ()); + if (end_position < 0) + { + throw LoaderException (std::runtime_error {"Invalid ADIF record starting at: " + buffer.left (40).toStdString ()}); + } + 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 ()) + { + auto const& entity = prefixes->lookup (call); + worked.emplace (call.toUpper () + , extractField (record, "GRIDSQUARE").left (4).toUpper () // not interested in 6-digit grids + , extractField (record, "BAND").toUpper () + , extractField (record, "MODE").toUpper () + , entity.entity_name + , entity.continent + , entity.CQ_zone + , entity.ITU_zone); + } + else + { + throw LoaderException (std::runtime_error {"Invalid ADIF record with no CALL: " + record.toStdString ()}); + } + } + } + else + { + throw LoaderException (std::runtime_error {"Error opening ADIF log file for read: " + inputFile.errorString ().toStdString ()}); + } + } + return worked; + } +} + +class WorkedBefore::impl final +{ +public: + impl () + : path_ {QDir {QStandardPaths::writableLocation (QStandardPaths::DataLocation)}.absoluteFilePath (logFileName)} + { + } + + void reload () + { + async_loader_ = QtConcurrent::run (loader, path_, &prefixes_); + loader_watcher_.setFuture (async_loader_); + } + + QString path_; + AD1CCty prefixes_; + QFutureWatcher<worked_before_database_type> loader_watcher_; + QFuture<worked_before_database_type> async_loader_; + worked_before_database_type worked_; +}; + +WorkedBefore::WorkedBefore () +{ + connect (&m_->loader_watcher_, &QFutureWatcher<worked_before_database_type>::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 (); + } + Q_EMIT finished_loading (n, error); + }); + reload (); +} + +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 ()) + { + out << "WSJT-X ADIF Export<eoh>" << endl; // new file + } + out << ADIF_record << " <eor>" << endl; + } + 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<entity_mode_band> ().end () + != m_->worked_.get<entity_mode_band> ().find (std::make_tuple (country, mode.toUpper (), band.toUpper ())); + } + else + { + // partial key lookup + return + country.size () + && m_->worked_.get<entity_mode_band> ().end () + != m_->worked_.get<entity_mode_band> ().find (std::make_tuple (country, mode.toUpper ())); + } + } + else + { + if (band.size ()) + { + return + country.size () + && m_->worked_.get<entity_band> ().end () + != m_->worked_.get<entity_band> ().find (std::make_tuple (country, band.toUpper ())); + } + else + { + // partial key lookup + return + country.size () + && m_->worked_.get<entity_band> ().end () + != m_->worked_.get<entity_band> ().find (country); + } + } +} + +bool WorkedBefore::grid_worked (QString const& grid, QString const& mode, QString const& band) const +{ + if (mode.size ()) + { + if (band.size ()) + { + return m_->worked_.get<grid_mode_band> ().end () + != m_->worked_.get<grid_mode_band> ().find (std::make_tuple (grid.left (4).toUpper (), mode.toUpper (), band.toUpper ())); + } + else + { + // partial key lookup + return m_->worked_.get<grid_mode_band> ().end () + != m_->worked_.get<grid_mode_band> ().find (std::make_tuple (grid.left (4).toUpper (), mode.toUpper ())); + } + } + else + { + if (band.size ()) + { + return m_->worked_.get<grid_band> ().end () + != m_->worked_.get<grid_band> ().find (std::make_tuple (grid.left (4).toUpper (), band.toUpper ())); + } + else + { + // partial key lookup + return m_->worked_.get<grid_band> ().end () + != m_->worked_.get<grid_band> ().find (grid.left (4).toUpper ()); + } + } +} + +bool WorkedBefore::call_worked (QString const& call, QString const& mode, QString const& band) const +{ + if (mode.size ()) + { + if (band.size ()) + { + return m_->worked_.get<call_mode_band> ().end () + != m_->worked_.get<call_mode_band> ().find (std::make_tuple (call.toUpper (), mode.toUpper (), band.toUpper ())); + } + else + { + // partial key lookup + return m_->worked_.get<call_mode_band> ().end () + != m_->worked_.get<call_mode_band> ().find (std::make_tuple (call.toUpper (), mode.toUpper ())); + } + } + else + { + if (band.size ()) + { + return m_->worked_.get<call_band> ().end () + != m_->worked_.get<call_band> ().find (std::make_tuple (call.toUpper (), band.toUpper ())); + } + else + { + // partial key lookup + return m_->worked_.get<call_band> ().end () + != m_->worked_.get<call_band> ().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<continent_mode_band> ().end () + != m_->worked_.get<continent_mode_band> ().find (std::make_tuple (continent, mode.toUpper (), band.toUpper ())); + } + else + { + // partial key lookup + return m_->worked_.get<continent_mode_band> ().end () + != m_->worked_.get<continent_mode_band> ().find (std::make_tuple (continent, mode.toUpper ())); + } + } + else + { + if (band.size ()) + { + return m_->worked_.get<continent_band> ().end () + != m_->worked_.get<continent_band> ().find (std::make_tuple (continent, band.toUpper ())); + } + else + { + // partial key lookup + return m_->worked_.get<continent_band> ().end () + != m_->worked_.get<continent_band> ().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<CQ_zone_mode_band> ().end () + != m_->worked_.get<CQ_zone_mode_band> ().find (std::make_tuple (CQ_zone, mode.toUpper (), band.toUpper ())); + } + else + { + // partial key lookup + return m_->worked_.get<CQ_zone_mode_band> ().end () + != m_->worked_.get<CQ_zone_mode_band> ().find (std::make_tuple (CQ_zone, mode.toUpper ())); + } + } + else + { + if (band.size ()) + { + return m_->worked_.get<CQ_zone_band> ().end () + != m_->worked_.get<CQ_zone_band> ().find (std::make_tuple (CQ_zone, band.toUpper ())); + } + else + { + // partial key lookup + return m_->worked_.get<CQ_zone_band> ().end () + != m_->worked_.get<CQ_zone_band> ().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<ITU_zone_mode_band> ().end () + != m_->worked_.get<ITU_zone_mode_band> ().find (std::make_tuple (ITU_zone, mode.toUpper (), band.toUpper ())); + } + else + { + // partial key lookup + return m_->worked_.get<ITU_zone_mode_band> ().end () + != m_->worked_.get<ITU_zone_mode_band> ().find (std::make_tuple (ITU_zone, mode.toUpper ())); + } + } + else + { + if (band.size ()) + { + return m_->worked_.get<ITU_zone_band> ().end () + != m_->worked_.get<ITU_zone_band> ().find (std::make_tuple (ITU_zone, band.toUpper ())); + } + else + { + // partial key lookup + return m_->worked_.get<ITU_zone_band> ().end () + != m_->worked_.get<ITU_zone_band> ().find (ITU_zone); + } + } +} diff --git a/logbook/WorkedBefore.hpp b/logbook/WorkedBefore.hpp new file mode 100644 index 00000000..90a31813 --- /dev/null +++ b/logbook/WorkedBefore.hpp @@ -0,0 +1,46 @@ +#ifndef WORKWED_BEFORE_HPP_ +#define WORKWED_BEFORE_HPP_ + +#include <QObject> +#include "AD1CCty.hpp" +#include "pimpl_h.hpp" + +class CountryDat; +class QString; +class QByteArray; + +class WorkedBefore final + : public QObject +{ + Q_OBJECT + +public: + using Continent = AD1CCty::Continent; + + explicit WorkedBefore (); + ~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; + + Q_SIGNAL void finished_loading (int worked_before_record_count, QString const& error) const; + +private: + class impl; + pimpl<impl> m_; +}; + +#endif diff --git a/logbook/adif.cpp b/logbook/adif.cpp deleted file mode 100644 index a1be7de5..00000000 --- a/logbook/adif.cpp +++ /dev/null @@ -1,231 +0,0 @@ -#include "adif.h" - -#include <QFile> -#include <QTextStream> -#include <QDateTime> -#include <QDebug> - -/* -<CALL:4>W1XT<BAND:3>20m<FREQ:6>14.076<GRIDSQUARE:4>DM33<MODE:4>JT65<RST_RCVD:3>-21<RST_SENT:3>-14<QSO_DATE:8>20110422<TIME_ON:6>041712<TIME_OFF:6>042435<TX_PWR:1>4<COMMENT:34>1st JT65A QSO. Him: mag loop 20W<STATION_CALLSIGN:6>VK3ACF<MY_GRIDSQUARE:6>qf22lb<eor> -<CALL:6>IK1SOW<BAND:3>20m<FREQ:6>14.076<GRIDSQUARE:4>JN35<MODE:4>JT65<RST_RCVD:3>-19<RST_SENT:3>-11<QSO_DATE:8>20110422<TIME_ON:6>052501<TIME_OFF:6>053359<TX_PWR:1>3<STATION_CALLSIGN:6>VK3ACF<MY_GRIDSQUARE:6>qf22lb<eor> -<CALL:6:S>W4ABC> ... -*/ - -void ADIF::init(QString const& filename) -{ - _filename = filename; - _data.clear(); -} - - -QString ADIF::extractField(QString const& record, QString const& fieldName) const -{ - 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 > - } - - 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) - { - QString field = record.mid(closingBracketIndex+1,fieldLength); - return field; - } - } - } - return ""; -} - - - -void ADIF::load() -{ - _data.clear(); - QFile inputFile(_filename); - if (inputFile.open(QIODevice::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 ("<EOH>", 0, Qt::CaseInsensitive); - } - } - while (!in.atEnd () && !pre_read && end_position < 0); - if (!pre_read) // found header - { - buffer.remove (0, end_position + 5); - } - while (buffer.size () || !in.atEnd ()) - { - do - { - end_position = buffer.indexOf ("<EOR>", 0, Qt::CaseInsensitive); - if (!in.atEnd () && end_position < 0) - { - buffer += in.readLine () + '\n'; - } - } - while (!in.atEnd () && end_position < 0); - int record_length {end_position >= 0 ? end_position + 5 : -1}; - auto record = buffer.left (record_length).trimmed (); - auto next_record = buffer.indexOf (QChar {'<'}, record_length); - buffer.remove (0, next_record >=0 ? next_record : buffer.size ()); - record = record.mid (record.indexOf (QChar {'<'})); - add (extractField (record, "CALL") - , extractField (record, "BAND") - , extractField (record, "MODE") - , extractField (record, "QSO_DATE")); - } - inputFile.close (); - } -} - - -void ADIF::add(QString const& call, QString const& band, QString const& mode, QString const& date) -{ - QSO q; - q.call = call; - q.band = band; - q.mode = mode; - q.date = date; - if (q.call.size ()) - { - _data.insert(q.call,q); - // qDebug() << "Added as worked:" << call << band << mode << date; - } -} - -// return true if in the log same band and mode (where JT65 == JT9) -bool ADIF::match(QString const& call, QString const& band, QString const& mode) const -{ - QList<QSO> qsos = _data.values(call); - if (qsos.size()>0) - { - QSO q; - foreach(q,qsos) - { - if ( (band.compare(q.band,Qt::CaseInsensitive) == 0) - || (band=="") - || (q.band=="")) - { - if ( - ( - ((mode.compare("JT65",Qt::CaseInsensitive)==0) || - (mode.compare("JT9",Qt::CaseInsensitive)==0) || - (mode.compare("FT8",Qt::CaseInsensitive)==0)) - && - ((q.mode.compare("JT65",Qt::CaseInsensitive)==0) || - (q.mode.compare("JT9",Qt::CaseInsensitive)==0) || - (q.mode.compare("FT8",Qt::CaseInsensitive)==0)) - ) - || (mode.compare(q.mode,Qt::CaseInsensitive)==0) - || (mode=="") - || (q.mode=="") - ) - return true; - } - } - } - return false; -} - -QList<QString> ADIF::getCallList() const -{ - QList<QString> p; - QMultiHash<QString,QSO>::const_iterator i = _data.constBegin(); - while (i != _data.constEnd()) - { - p << i.key(); - ++i; - } - return p; -} - - - -int ADIF::getCount() const -{ - return _data.size(); -} - -QByteArray ADIF::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& m_myCall - , QString const& m_myGrid, QString const& m_txPower, QString const& operator_call) -{ - QString t; - t = "<call:" + QString::number(hisCall.length()) + ">" + hisCall; - t += " <gridsquare:" + QString::number(hisGrid.length()) + ">" + hisGrid; - t += " <mode:" + QString::number(mode.length()) + ">" + mode; - t += " <rst_sent:" + QString::number(rptSent.length()) + ">" + rptSent; - t += " <rst_rcvd:" + QString::number(rptRcvd.length()) + ">" + rptRcvd; - t += " <qso_date:8>" + dateTimeOn.date().toString("yyyyMMdd"); - t += " <time_on:6>" + dateTimeOn.time().toString("hhmmss"); - t += " <qso_date_off:8>" + dateTimeOff.date().toString("yyyyMMdd"); - t += " <time_off:6>" + dateTimeOff.time().toString("hhmmss"); - t += " <band:" + QString::number(band.length()) + ">" + band; - t += " <freq:" + QString::number(strDialFreq.length()) + ">" + strDialFreq; - t += " <station_callsign:" + QString::number(m_myCall.length()) + ">" + - m_myCall; - t += " <my_gridsquare:" + QString::number(m_myGrid.length()) + ">" + - m_myGrid; - if (m_txPower != "") - t += " <tx_pwr:" + QString::number(m_txPower.length()) + - ">" + m_txPower; - if (comments != "") - t += " <comment:" + QString::number(comments.length()) + - ">" + comments; - if (name != "") - t += " <name:" + QString::number(name.length()) + - ">" + name; - if (operator_call!="") - t+=" <operator:" + QString::number(operator_call.length()) + - ">" + operator_call; - return t.toLatin1 (); -} - - -// open ADIF file and append the QSO details. Return true on success -bool ADIF::addQSOToFile(QByteArray const& ADIF_record) -{ - QFile f2(_filename); - if (!f2.open(QIODevice::Text | QIODevice::Append)) - return false; - else - { - QTextStream out(&f2); - if (f2.size()==0) - out << "WSJT-X ADIF Export<eoh>" << endl; // new file - - out << ADIF_record << " <eor>" << endl; - f2.close(); - } - return true; -} diff --git a/logbook/adif.h b/logbook/adif.h deleted file mode 100644 index 727ca995..00000000 --- a/logbook/adif.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Reads an ADIF log file into memory - * Searches log for call, band and mode - * VK3ACF July 2013 - */ - - -#ifndef __ADIF_H -#define __ADIF_H - -#if defined (QT5) -#include <QList> -#include <QString> -#include <QMultiHash> -#else -#include <QtGui> -#endif - -class QDateTime; - -class ADIF -{ - public: - void init(QString const& filename); - void load(); - void add(QString const& call, QString const& band, QString const& mode, QString const& date); - bool match(QString const& call, QString const& band, QString const& mode) const; - QList<QString> getCallList() const; - int getCount() const; - - // open ADIF file and append the QSO details. Return true on success - bool addQSOToFile(QByteArray const& ADIF_record); - - 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& m_myCall, QString const& m_myGrid - , QString const& m_txPower, QString const& operator_call); - - -private: - struct QSO - { - QString call,band,mode,date; - }; - - QMultiHash<QString, QSO> _data; - QString _filename; - - QString extractField(QString const& line, QString const& fieldName) const; -}; - - -#endif - diff --git a/logbook/countriesworked.cpp b/logbook/countriesworked.cpp index 6a991f8a..bcd73af7 100644 --- a/logbook/countriesworked.cpp +++ b/logbook/countriesworked.cpp @@ -1,42 +1,24 @@ #include "countriesworked.h" -void CountriesWorked::init(const QStringList countryNames) -{ - _data.clear(); - foreach(QString name,countryNames) - _data.insert(name,false); -} +#include <set> + +#include "pimpl_impl.hpp" -void CountriesWorked::setAsWorked(const QString countryName) +class CountriesWorkedimpl final { - if (_data.contains(countryName)) - _data.insert(countryName,true); -} - -bool CountriesWorked::getHasWorked(const QString countryName) const -{ - if (_data.contains(countryName)) - return _data.value(countryName); +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<QString> worked_; +}; - return false; -} - -int CountriesWorked::getWorkedCount() const +void CountriesWorked::add (QString const& country, QString const& band) { - int count = 0; - foreach (bool value,_data) - if (value) - count += 1; - return count; -} - -int CountriesWorked::getSize() const + m_->worked_.insert (country + '+' + band); +} + +bool CountriesWorked::contains (QString const& country, QString const& band) const { - return _data.count(); + return m_->worked_.end () != m_->worked_.lower_bound (country + '+' + band); } - - - - - - diff --git a/logbook/countriesworked.h b/logbook/countriesworked.h index fedfa752..ac8aaea6 100644 --- a/logbook/countriesworked.h +++ b/logbook/countriesworked.h @@ -1,29 +1,24 @@ -/* - * maintains a list of country names that have been worked - * VK3ACF July 2013 - */ +#ifndef COUNTRIES_WORKDED_H_ +#define COUNTRIES_WORKDED_H_ -#ifndef __COUNTRIESWORKDED_H -#define __COUNTRIESWORKDED_H - -#include <QList> #include <QString> -#include <QStringList> -#include <QHash> +#include "pimpl_h.hpp" + +class QStringList; -class CountriesWorked +class CountriesWorked final { public: - void init(const QStringList countryNames); - void setAsWorked(const QString countryName); - bool getHasWorked(const QString countryName) const; - int getWorkedCount() const; - int getSize() const; + 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: - QHash<QString, bool> _data; + class impl; + pimpl<impl> m_; }; #endif - diff --git a/logbook/countrydat.cpp b/logbook/countrydat.cpp deleted file mode 100644 index d1997a97..00000000 --- a/logbook/countrydat.cpp +++ /dev/null @@ -1,160 +0,0 @@ -/* -#Sov Mil Order of Malta: 15: 28: EU: 41.90: -12.43: -1.0: 1A: - #1A; -#Spratly Islands: 26: 50: AS: 9.88: -114.23: -8.0: 1S: - #1S,9M0,BV9S; -#Monaco: 14: 27: EU: 43.73: -7.40: -1.0: 3A: - #3A; -#Heard Island: 39: 68: AF: -53.08: -73.50: -5.0: VK0H: - #=VK0IR; -#Macquarie Island: 30: 60: OC: -54.60: -158.88: -10.0: VK0M: - #=VK0KEV; -#Cocos-Keeling: 29: 54: OC: -12.15: -96.82: -6.5: VK9C: - #AX9C,AX9Y,VH9C,VH9Y,VI9C,VI9Y,VJ9C,VJ9Y,VK9C,VK9Y,VL9C,VL9Y,VM9C,VM9Y, - #VN9C,VN9Y,VZ9C,VZ9Y,=VK9AA; -*/ - - -#include "countrydat.h" -#include <QFile> -#include <QTextStream> -#include <QDebug> -#include "Radio.hpp" - -void CountryDat::init(const QString filename) -{ - _filename = filename; - _data.clear(); -} - -QString CountryDat::_extractName(const QString line) const -{ - int s1 = line.indexOf(':'); - if (s1>=0) - { - QString name = line.mid(0,s1); - return name; - } - return ""; -} - -void CountryDat::_removeBrackets(QString &line, const QString a, const QString b) const -{ - int s1 = line.indexOf(a); - while (s1 >= 0) - { - int s2 = line.indexOf(b); - line = line.mid(0,s1) + line.mid(s2+1,-1); - s1 = line.indexOf(a); - } -} - -QStringList CountryDat::_extractPrefix(QString &line, bool &more) const -{ - line = line.remove(" \n"); - line = line.replace(" ",""); - - _removeBrackets(line,"(",")"); - _removeBrackets(line,"[","]"); - _removeBrackets(line,"<",">"); - _removeBrackets(line,"~","~"); - - int s1 = line.indexOf(';'); - more = true; - if (s1 >= 0) - { - line = line.mid(0,s1); - more = false; - } - - QStringList r = line.split(','); - - return r; -} - - -void CountryDat::load() -{ - _data.clear(); - _countryNames.clear(); //used by countriesWorked - - QFile inputFile(_filename); - if (inputFile.open(QIODevice::ReadOnly)) - { - QTextStream in(&inputFile); - while ( !in.atEnd() ) - { - QString line1 = in.readLine(); - if ( !in.atEnd() ) - { - QString line2 = in.readLine(); - - QString name = _extractName(line1); - if (name.length()>0) - { - QString continent=line1.mid(36,2); - QString principalPrefix=line1.mid(69,4); - int i1=principalPrefix.indexOf(":"); - if(i1>0) principalPrefix=principalPrefix.mid(0,i1); - name += "; " + principalPrefix + "; " + continent; - _countryNames << name; - bool more = true; - QStringList prefixs; - while (more) - { - QStringList p = _extractPrefix(line2,more); - prefixs += p; - if (more) - line2 = in.readLine(); - } - - QString p; - foreach(p,prefixs) - { - if (p.length() > 0) - _data.insert(p,name); - } - } - } - } - inputFile.close(); - } -} - -// return country name else "" -QString CountryDat::find(QString call) const -{ - call = call.toUpper (); - - // check for exact match first - if (_data.contains ("=" + call)) - { - return fixup (_data.value ("=" + call), call); - } - - auto prefix = Radio::effective_prefix (call); - auto match_candidate = prefix; - while (match_candidate.size () >= 1) - { - if (_data.contains (match_candidate)) - { - return fixup (_data.value (match_candidate), prefix); - } - match_candidate = match_candidate.left (match_candidate.size () - 1); - } - return QString {}; -} - -QString CountryDat::fixup (QString country, QString const& call) const -{ - // - // deal with special rules that cty.dat does not cope with - // - - // KG4 2x1 and 2x3 calls that map to Gitmo are mainland US not Gitmo - if (call.startsWith ("KG4") && call.size () != 5 && call.size () != 3) - { - country.replace ("Guantanamo Bay; KG4; NA", "United States; K; NA"); - } - return country; -} diff --git a/logbook/countrydat.h b/logbook/countrydat.h deleted file mode 100644 index d0430892..00000000 --- a/logbook/countrydat.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Reads cty.dat file - * Establishes a map between prefixes and their country names - * VK3ACF July 2013 - */ - - -#ifndef __COUNTRYDAT_H -#define __COUNTRYDAT_H - - -#include <QString> -#include <QStringList> -#include <QHash> - - -class CountryDat -{ -public: - void init(const QString filename); - void load(); - QString find(QString prefix) const; // return country name or "" - QStringList getCountryNames() const { return _countryNames; }; - -private: - QString _extractName(const QString line) const; - void _removeBrackets(QString &line, const QString a, const QString b) const; - QStringList _extractPrefix(QString &line, bool &more) const; - QString fixup (QString country, QString const& call) const; - - QString _filename; - QStringList _countryNames; - QHash<QString, QString> _data; -}; - -#endif diff --git a/logbook/logbook.cpp b/logbook/logbook.cpp index e938910d..b84b65c1 100644 --- a/logbook/logbook.cpp +++ b/logbook/logbook.cpp @@ -1,101 +1,112 @@ #include "logbook.h" -#include <QDebug> -#include <QFontMetrics> -#include <QStandardPaths> -#include <QDir> -namespace -{ - auto logFileName = "wsjtx_log.adi"; - auto countryFileName = "cty.dat"; -} - -void LogBook::init() -{ - QDir dataPath {QStandardPaths::writableLocation (QStandardPaths::DataLocation)}; - QString countryDataFilename; - if (dataPath.exists (countryFileName)) - { - // User override - countryDataFilename = dataPath.absoluteFilePath (countryFileName); - } - else - { - countryDataFilename = QString {":/"} + countryFileName; - } - - _countries.init(countryDataFilename); - _countries.load(); +#include <QDateTime> +#include "Configuration.hpp" +#include "AD1CCty.hpp" - _worked.init(_countries.getCountryNames()); +#include "moc_logbook.cpp" - _log.init(dataPath.absoluteFilePath (logFileName)); - _log.load(); - - _setAlreadyWorkedFromLog(); - - /* - int QSOcount = _log.getCount(); - int count = _worked.getWorkedCount(); - qDebug() << QSOcount << "QSOs and" << count << "countries worked in file" << logFilename; - */ - - // QString call = "ok1ct"; - // QString countryName; - // bool callWorkedBefore,countryWorkedBefore; - // match(/*in*/call, /*out*/ countryName,callWorkedBefore,countryWorkedBefore); - // qDebug() << countryName; - -} - - -void LogBook::_setAlreadyWorkedFromLog() +LogBook::LogBook (Configuration const * configuration) + : config_ {configuration} { - QList<QString> calls = _log.getCallList(); - QString c; - foreach(c,calls) - { - QString countryName = _countries.find(c); - if (countryName.length() > 0) - { - _worked.setAsWorked(countryName); - //qDebug() << countryName << " worked " << c; - } - } + connect (&worked_before_, &WorkedBefore::finished_loading, this, &LogBook::finished_loading); } -void LogBook::match(/*in*/const QString call, - /*out*/ QString &countryName, - bool &callWorkedBefore, - bool &countryWorkedBefore) const +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.length() > 0) { - QString currentMode = "JT9"; // JT65 == JT9 in ADIF::match() - QString currentBand = ""; // match any band - callWorkedBefore = _log.match(call,currentBand,currentMode); - countryName = _countries.find(call); -// qDebug() << "B" << countryName; - - if (countryName.length() > 0) // country was found - countryWorkedBefore = _worked.getHasWorked(countryName); + 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 { - countryName = "where?"; //error: prefix not found - countryWorkedBefore = false; + countryB4 = true; // we don't want to flag unknown entities + continentB4 = true; + CQZoneB4 = true; + ITUZoneB4 = true; } } - //qDebug() << "Logbook:" << call << ":" << countryName << "Cty B4:" << countryWorkedBefore << "call B4:" << callWorkedBefore; } -void LogBook::addAsWorked(const QString call, const QString band, const QString mode, const QString date) +bool LogBook::add (QString const& call + , QString const& grid + , QString const& band + , QString const& mode + , QByteArray const& ADIF_record) { - //qDebug() << "adding " << call << " as worked"; - _log.add(call,band,mode,date); - QString countryName = _countries.find(call); - if (countryName.length() > 0) - _worked.setAsWorked(countryName); + return worked_before_.add (call, grid, band, mode, ADIF_record); } +void LogBook::rescan () +{ + worked_before_.reload (); +} - +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 t; + t = "<call:" + QString::number(hisCall.length()) + ">" + hisCall; + t += " <gridsquare:" + QString::number(hisGrid.length()) + ">" + hisGrid; + t += " <mode:" + QString::number(mode.length()) + ">" + mode; + t += " <rst_sent:" + QString::number(rptSent.length()) + ">" + rptSent; + t += " <rst_rcvd:" + QString::number(rptRcvd.length()) + ">" + rptRcvd; + t += " <qso_date:8>" + dateTimeOn.date().toString("yyyyMMdd"); + t += " <time_on:6>" + dateTimeOn.time().toString("hhmmss"); + t += " <qso_date_off:8>" + dateTimeOff.date().toString("yyyyMMdd"); + t += " <time_off:6>" + dateTimeOff.time().toString("hhmmss"); + t += " <band:" + QString::number(band.length()) + ">" + band; + t += " <freq:" + QString::number(strDialFreq.length()) + ">" + strDialFreq; + t += " <station_callsign:" + QString::number(myCall.length()) + ">" + myCall; + t += " <my_gridsquare:" + QString::number(myGrid.length()) + ">" + myGrid; + if(txPower!="") t += " <tx_pwr:" + QString::number(txPower.length()) + ">" + txPower; + if(comments!="") t += " <comment:" + QString::number(comments.length()) + ">" + comments; + if(name!="") t += " <name:" + QString::number(name.length()) + ">" + name; + if(operator_call!="") t+=" <operator:" + QString::number(operator_call.length()) + ">" + operator_call; + if (xSent.size ()) + { + auto words = xSent.split (' ', QString::SkipEmptyParts); + if (words.size () > 1) + { + bool ok; + auto sn = words.back ().toUInt (&ok); + if (ok && sn) + { + // assume last word is a serial if there are at least + // two words and if it is positive numeric + t += " <STX:" + QString::number (words.back ().size ()) + '>' + words.back (); + } + } + } + if (xRcvd.size ()) { + QString t1=""; + if(xRcvd.split(" ").size()==2) t1=xRcvd.split(" ").at(1); + if(t1.toInt()>0) { + t += " <SRX:" + QString::number(t1.length()) + ">" + t1; + } else { + t += " <STATE:" + QString::number(t1.length()) + ">" + t1; + } + } + return t.toLatin1(); +} diff --git a/logbook/logbook.h b/logbook/logbook.h index 4b80974d..1850aa01 100644 --- a/logbook/logbook.h +++ b/logbook/logbook.h @@ -1,39 +1,51 @@ +// -*- Mode: C++ -*- /* * From an ADIF file and cty.dat, get a call's DXCC entity and its worked before status - * VK3ACF July 2013 */ -#ifndef LOGBOOK_H -#define LOGBOOK_H - +#ifndef LOG_BOOK_H_ +#define LOG_BOOK_H_ +#include <QObject> #include <QString> -#include <QFont> -#include "countrydat.h" -#include "countriesworked.h" -#include "adif.h" +#include "WorkedBefore.hpp" -class QDir; +class Configuration; +class QByteArray; +class QDateTime; -class LogBook +class LogBook final + : public QObject { + Q_OBJECT + public: - void init(); - void match(/*in*/ const QString call, - /*out*/ QString &countryName, - bool &callWorkedBefore, - bool &countryWorkedBefore) const; - void addAsWorked(const QString call, const QString band, const QString mode, const QString date); + LogBook (Configuration const *); + 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; + static 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); + + Q_SIGNAL void finished_loading (int worked_before_record_count, QString const& error) const; private: - CountryDat _countries; - CountriesWorked _worked; - ADIF _log; - - void _setAlreadyWorkedFromLog(); - + Configuration const * config_; + WorkedBefore worked_before_; }; -#endif // LOGBOOK_H - +#endif diff --git a/logbook/logbook.pri b/logbook/logbook.pri new file mode 100644 index 00000000..fcac6252 --- /dev/null +++ b/logbook/logbook.pri @@ -0,0 +1,11 @@ +SOURCES += \ + logbook/countriesworked.cpp \ + logbook/logbook.cpp \ + logbook/AD1CCty.cpp \ + logbook/WorkedBefore.cpp + +HEADERS += \ + logbook/WorkedBefore.hpp \ + logbook/logbook.h \ + logbook/countriesworked.h \ + logbook/AD1CCty.hpp |
