diff options
Diffstat (limited to 'logbook')
| -rw-r--r-- | logbook/adif.cpp | 206 | ||||
| -rw-r--r-- | logbook/adif.h | 50 | ||||
| -rw-r--r-- | logbook/countriesworked.cpp | 42 | ||||
| -rw-r--r-- | logbook/countriesworked.h | 29 | ||||
| -rw-r--r-- | logbook/countrydat.cpp | 136 | ||||
| -rw-r--r-- | logbook/countrydat.h | 35 | ||||
| -rw-r--r-- | logbook/logbook.cpp | 99 | ||||
| -rw-r--r-- | logbook/logbook.h | 39 |
8 files changed, 636 insertions, 0 deletions
diff --git a/logbook/adif.cpp b/logbook/adif.cpp new file mode 100644 index 00000000..fb6b3bcd --- /dev/null +++ b/logbook/adif.cpp @@ -0,0 +1,206 @@ +#include "adif.h" + +#include <QFile> +#include <QTextStream> +#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:4>0417<TIME_OFF:4>0424<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:4>0525<TIME_OFF:4>0533<TX_PWR:1>3<STATION_CALLSIGN:6>VK3ACF<MY_GRIDSQUARE:6>qf22lb<eor> +<CALL:6:S>W4ABC> ... +*/ + +void ADIF::init(QString filename) +{ + _filename = filename; + _data.clear(); +} + + +QString ADIF::_extractField(const QString line, const QString fieldName) +{ + int fieldNameIndex = line.indexOf(fieldName,0,Qt::CaseInsensitive); + if (fieldNameIndex >=0) + { + int closingBracketIndex = line.indexOf('>',fieldNameIndex); + int fieldLengthIndex = line.indexOf(':',fieldNameIndex); // find the size delimiter + int dataTypeIndex = -1; + if (fieldLengthIndex >= 0) + { + dataTypeIndex = line.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 = line.mid(fieldLengthIndex+1,fieldLengthCharCount); + int fieldLength = fieldLengthString.toInt(); + if (fieldLength > 0) + { + QString field = line.mid(closingBracketIndex+1,fieldLength); + return field; + } + } + } + return ""; +} + + + +void ADIF::load() +{ + _data.clear(); + QFile inputFile(_filename); + if (inputFile.open(QIODevice::ReadOnly)) + { + QTextStream in(&inputFile); + while ( !in.atEnd() ) + { + QString line = in.readLine(); + QSO q; + q.call = _extractField(line,"CALL:"); + q.band = _extractField(line,"BAND:"); + q.mode = _extractField(line,"MODE:"); + q.date = _extractField(line,"QSO_DATE:"); + if (q.call != "") + _data.insert(q.call,q); + } + inputFile.close(); + } +} + + +void ADIF::add(const QString call, const QString band, const QString mode, const QString date) +{ + QSO q; + q.call = call; + q.band = band; + q.mode = mode; + q.date = date; + _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(const QString call, const QString band, const QString mode) +{ + 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)) + && + ((q.mode.compare("JT65",Qt::CaseInsensitive)==0) || (q.mode.compare("JT9",Qt::CaseInsensitive)==0)) + ) + || (mode.compare(q.mode,Qt::CaseInsensitive)==0) + || (mode=="") + || (q.mode=="") + ) + return true; + } + } + } + return false; +} + +QList<QString> ADIF::getCallList() +{ + QList<QString> p; + QMultiHash<QString,QSO>::const_iterator i = _data.constBegin(); + while (i != _data.constEnd()) + { + p << i.key(); + ++i; + } + return p; +} + +int ADIF::getCount() +{ + return _data.size(); +} + + +// open ADIF file and append the QSO details. Return true on success +bool ADIF::addQSOToFile(const QString hisCall, const QString hisGrid, const QString mode, const QString rptSent, const QString rptRcvd, const QString date, const QString time, const QString band, + const QString comments, const QString name, const QString strDialFreq, const QString m_myCall, const QString m_myGrid, const QString m_txPower) +{ + 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 + + 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>" + date; + t+=" <time_on:4>" + time; + 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; + t+=" <eor>"; + out << t << endl; + f2.close(); + } + return true; +} + +QString ADIF::bandFromFrequency(double dialFreq) +{ + QString band=""; + if(dialFreq>0.135 and dialFreq<0.139) band="2200m"; + else if(dialFreq>0.45 and dialFreq<0.55) band="630m"; + else if(dialFreq>1.8 and dialFreq<2.0) band="160m"; + else if(dialFreq>3.5 and dialFreq<4.0) band="80m"; + else if(dialFreq>5.1 and dialFreq<5.45) band="60m"; + else if(dialFreq>7.0 and dialFreq<7.3) band="40m"; + else if(dialFreq>10.0 and dialFreq<10.15) band="30m"; + else if(dialFreq>14.0 and dialFreq<14.35) band="20m"; + else if(dialFreq>18.068 and dialFreq<18.168) band="17m"; + else if(dialFreq>21.0 and dialFreq<21.45) band="15m"; + else if(dialFreq>24.890 and dialFreq<24.990) band="12m"; + else if(dialFreq>28.0 and dialFreq<29.7) band="10m"; + else if(dialFreq>50.0 and dialFreq<54.0) band="6m"; + else if(dialFreq>70.0 and dialFreq<71.0) band="4m"; + else if(dialFreq>144.0 and dialFreq<148.0) band="2m"; + else if(dialFreq>222.0 and dialFreq<225.0) band="1.25m"; + else if(dialFreq>420.0 and dialFreq<450.0) band="70cm"; + else if(dialFreq>902.0 and dialFreq<928.0) band="33cm"; + else if(dialFreq>1240.0 and dialFreq<1300.0) band="23cm"; + else if(dialFreq>2300.0 and dialFreq<2450.0) band="13cm"; + else if(dialFreq>3300.0 and dialFreq<3500.0) band="9cm"; + else if(dialFreq>5650.0 and dialFreq<5925.0) band="6cm"; + else if(dialFreq>10000.0 and dialFreq<10500.0) band="3cm"; + else if(dialFreq>24000.0 and dialFreq<24250.0) band="1.25cm"; + else if(dialFreq>47000.0 and dialFreq<47200.0) band="6mm"; + else if(dialFreq>75500.0 and dialFreq<81000.0) band="4mm"; + return band; +} diff --git a/logbook/adif.h b/logbook/adif.h new file mode 100644 index 00000000..7445d4aa --- /dev/null +++ b/logbook/adif.h @@ -0,0 +1,50 @@ +/* + * 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 ADIF +{ + public: + void init(QString filename); + void load(); + void add(const QString call, const QString band, const QString mode, const QString date); + bool match(const QString call, const QString band, const QString mode); + QList<QString> getCallList(); + int getCount(); + + // open ADIF file and append the QSO details. Return true on success + bool addQSOToFile(const QString hisCall, const QString hisGrid, const QString mode, const QString rptSent, const QString rptRcvd, const QString date, const QString time, const QString band, + const QString comments, const QString name, const QString strDialFreq, const QString m_myCall, const QString m_myGrid, const QString m_txPower); + + static QString bandFromFrequency(double dialFreq); + + private: + struct QSO + { + QString call,band,mode,date; + }; + + QMultiHash<QString, QSO> _data; + QString _filename; + + QString _extractField(const QString line, const QString fieldName); +}; + + +#endif + diff --git a/logbook/countriesworked.cpp b/logbook/countriesworked.cpp new file mode 100644 index 00000000..a70a4e1a --- /dev/null +++ b/logbook/countriesworked.cpp @@ -0,0 +1,42 @@ +#include "countriesworked.h" + +void CountriesWorked::init(const QStringList countryNames) +{ + _data.clear(); + foreach(QString name,countryNames) + _data.insert(name,false); +} + +void CountriesWorked::setAsWorked(const QString countryName) +{ + if (_data.contains(countryName)) + _data.insert(countryName,true); +} + +bool CountriesWorked::getHasWorked(const QString countryName) +{ + if (_data.contains(countryName)) + return _data.value(countryName); + + return false; +} + +int CountriesWorked::getWorkedCount() +{ + int count = 0; + foreach (bool value,_data) + if (value) + count += 1; + return count; +} + +int CountriesWorked::getSize() +{ + return _data.count(); +} + + + + + + diff --git a/logbook/countriesworked.h b/logbook/countriesworked.h new file mode 100644 index 00000000..34fcc0c0 --- /dev/null +++ b/logbook/countriesworked.h @@ -0,0 +1,29 @@ +/* + * maintains a list of country names that have been worked + * VK3ACF July 2013 + */ + +#ifndef __COUNTRIESWORKDED_H +#define __COUNTRIESWORKDED_H + +#include <QList> +#include <QString> +#include <QStringList> +#include <QHash> + + +class CountriesWorked +{ + public: + void init(const QStringList countryNames); + void setAsWorked(const QString countryName); + bool getHasWorked(const QString countryName); + int getWorkedCount(); + int getSize(); + + private: + QHash<QString, bool> _data; +}; + +#endif + diff --git a/logbook/countrydat.cpp b/logbook/countrydat.cpp new file mode 100644 index 00000000..1d88e059 --- /dev/null +++ b/logbook/countrydat.cpp @@ -0,0 +1,136 @@ +/* +#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> + + +void CountryDat::init(const QString filename) +{ + _filename = filename; + _data.clear(); +} + +QString CountryDat::_extractName(const QString line) +{ + 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) +{ + 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) +{ + line = line.remove(" \n"); + line = line.replace("=",""); + 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) + { + _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(const QString prefix) +{ + QString pf = prefix.toUpper(); + while(pf.length() >= 1) + { + if (_data.contains(pf)) + { + QString country = _data.value(pf); + return country; + } + pf = pf.left(pf.length()-1); + } + return ""; +} + + + diff --git a/logbook/countrydat.h b/logbook/countrydat.h new file mode 100644 index 00000000..cd550cf1 --- /dev/null +++ b/logbook/countrydat.h @@ -0,0 +1,35 @@ +/* + * 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(const QString prefix); // return country name or "" + QStringList getCountryNames() { return _countryNames; }; + + private: + QString _extractName(const QString line); + void _removeBrackets(QString &line, const QString a, const QString b); + QStringList _extractPrefix(QString &line, bool &more); + + QString _filename; + QStringList _countryNames; + QHash<QString, QString> _data; +}; + +#endif diff --git a/logbook/logbook.cpp b/logbook/logbook.cpp new file mode 100644 index 00000000..f074116b --- /dev/null +++ b/logbook/logbook.cpp @@ -0,0 +1,99 @@ +#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(); + + _worked.init(_countries.getCountryNames()); + + _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() +{ + 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; + } + } +} + +void LogBook::match(/*in*/const QString call, + /*out*/ QString &countryName, + bool &callWorkedBefore, + bool &countryWorkedBefore) +{ + 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); + if (countryName.length() > 0) // country was found + countryWorkedBefore = _worked.getHasWorked(countryName); + else + { + countryName = "where?"; //error: prefix not found + countryWorkedBefore = false; + } + } + //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) +{ + //qDebug() << "adding " << call << " as worked"; + _log.add(call,band,mode,date); + QString countryName = _countries.find(call); + if (countryName.length() > 0) + _worked.setAsWorked(countryName); +} + + + diff --git a/logbook/logbook.h b/logbook/logbook.h new file mode 100644 index 00000000..38ea9d18 --- /dev/null +++ b/logbook/logbook.h @@ -0,0 +1,39 @@ +/* + * 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 + + +#include <QString> +#include <QFont> + +#include "countrydat.h" +#include "countriesworked.h" +#include "adif.h" + +class QDir; + +class LogBook +{ +public: + void init(); + void match(/*in*/ const QString call, + /*out*/ QString &countryName, + bool &callWorkedBefore, + bool &countryWorkedBefore); + void addAsWorked(const QString call, const QString band, const QString mode, const QString date); + +private: + CountryDat _countries; + CountriesWorked _worked; + ADIF _log; + + void _setAlreadyWorkedFromLog(); + +}; + +#endif // LOGBOOK_H + |
