File: callsign.cpp

package info (click to toggle)
klog 2.4.2-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 12,228 kB
  • sloc: cpp: 51,678; makefile: 15
file content (444 lines) | stat: -rw-r--r-- 18,902 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/***************************************************************************
                          callsign.cpp  -  description
                             -------------------
    begin                : ago 2024
    copyright            : (C) 2024 by Jaime Robles
    email                : jaime@robles.es
 ***************************************************************************/

/*****************************************************************************
 * This file is part of KLog.                                                *
 *                                                                           *
 *    KLog is free software: you can redistribute it and/or modify           *
 *    it under the terms of the GNU General Public License as published by   *
 *    the Free Software Foundation, either version 3 of the License, or      *
 *    (at your option) any later version.                                    *
 *                                                                           *
 *    KLog is distributed in the hope that it will be useful,                *
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *    GNU General Public License for more details.                           *
 *                                                                           *
 *    You should have received a copy of the GNU General Public License      *
 *    along with KLog.  If not, see <https://www.gnu.org/licenses/>.         *
 *                                                                           *
 *****************************************************************************/
#include "callsign.h"

//TODO: Add support for FK/c, FO/a, FO/m, FT/g, FT/j, ... calls

Callsign::Callsign(const QString &callsign, QObject *parent) : QObject{parent}
{
    initialize(callsign);
}

Callsign::~Callsign(){}

void Callsign::operator()(const QString &newCallsign)
{
    initialize(newCallsign);
}

void Callsign::initialize(const QString &callsign)
{

    clear(); // Clear existing data
    fullCall = callsign.toUpper();


    bool hostAreaNumberExist1       = false;   // Helper to check if the value exists or not
    bool hostSpecialNumberExist1    = false;   // Helper to check if the value exists or not
    bool homeAreaNumberExist        = false;   // Helper to check if the value exists or not
    bool hostAreaNumberExist2       = false;   // Helper to check if the value exists or not
    bool hostSpecialNumberExist2    = false;   // Helper to check if the value exists or not
    bool homeSpecialAreaNumberExist = false;   // Helper to check if the value exists or not

    // KB1/EA4K/QRP
    QString hostFullPrefix1;                    // KB1
    QString hostNormalPrefix1;                  // KB
    //int     hostAreaNumber1;                    // 1
    QString hostSpecialPrefix1;                 // 3D2 (in 3D2/EA4K)
    int     hostSpecialNumber1;                 // 0 (in 3D20/EA4K)(not sure if this usecase will exist!)

    QString hostFullPrefix2;                    // VU2 in EA4K/VU2
    QString hostNormalPrefix2;                  // VU in EA4K/VU2
    //int     hostAreaNumber2;                    // 2 in EA4K/VU2
    QString hostSpecialPrefix2;                 // 3D2 (in EA4K/3D2)
    //int     hostSpecialNumber2;                 // 0 (in EA4K/3D20)(not sure if this usecase will exist!)
    QString homeNormalPrefix;                   // EA   in EA4K
    QString homeSpecialPrefix;                  // 3D2  in 3D2A
    // int     homeSpecialAreaNumber;              // 0    in 3D20A
    QString suffix;                             // String containing Suffixes like QRP in EA4K/QRP but also 3D2 in EA4K/3D2

    if (fullCall.contains('\\'))                // Lets normalize complx calls
        fullCall.replace('\\', '/');

    QString string2test = fullCall;

    QRegularExpression      callsignRE  = callsignRegEx();
    QRegularExpression      prefnRE     = prefixRegEx();
    QRegularExpressionMatch match       = callsignRE.match(string2test);
    QRegularExpressionMatch matchPrefix = prefnRE.match(string2test);
    // Needed fields for a call are:
    // - hostFullPrefix (QString)
    // - hostPrefix     (QString)
    // - hostAreaNumber (int)
    // - homeCallsign
    // - homeFullPrefix (QString)
    // - homePrefix     (QString)
    // - homeAreaNumber (int)
    // - homeSuffix     (QString)
    // - suffix         (QString)
    if ( match.hasMatch() )
    {
        //qDebug() << Q_FUNC_INFO << " - 10";
        //it is a valid callsign
        valid                       = true;
        prefValid                   = true;

        hostFullPrefix1             = match.captured("hostfullprefix1");
        hostNormalPrefix1           = match.captured("hostnormalprefix1");
        int hostAreaNumber1         = match.captured("hostareanumber1").toInt(&hostAreaNumberExist1);
        hostSpecialPrefix1          = match.captured("hostspecialprefix1");
        hostSpecialNumber1          = match.captured("hostspecialareanumber1").toInt(&hostSpecialNumberExist1);

        //qDebug() << Q_FUNC_INFO << " - 40";
        homeCallsign                = match.captured("homecall");
        homeFullPrefix              = match.captured("homefullprefix");
        //qDebug() << Q_FUNC_INFO << " homeFullPrefix: " << homeFullPrefix;
        homeNormalPrefix            = match.captured("homenormalprefix");
        homeSpecialPrefix           = match.captured("homespecialprefix");

        int homeSpecialAreaNumber   = match.captured("homespecialareanumber").toInt(&homeSpecialAreaNumberExist);
        homeAreaNumber              = match.captured("homeareanumber").toInt(&homeAreaNumberExist);

        homeSuffix                  = match.captured("homesuffix");
        suffix                      = match.captured("suffix");

        hostFullPrefix2             = match.captured("hostfullprefix2");
        hostNormalPrefix2           = match.captured("hostnormalprefix2");
        int hostAreaNumber2         = match.captured("hostareanumber2").toInt(&hostAreaNumberExist2);
        hostSpecialPrefix2          = match.captured("hostspecialprefix2");
        int hostSpecialNumber2      = match.captured("hostspecialareanumber2").toInt(&hostSpecialNumberExist2);

        generalSuffix               = match.captured("generalsuffix");

        // Now we have all raw data, let's select what is valid

        if (!hostFullPrefix1.isEmpty())
        {
            //qDebug() << Q_FUNC_INFO << " - 100";
            hostFullPrefix = hostFullPrefix1;
            if (!hostNormalPrefix1.isEmpty())
            {
                //qDebug() << Q_FUNC_INFO << " - 110";
                hostPrefix = hostNormalPrefix1;
                if (hostAreaNumberExist1)
                {
                    //qDebug() << Q_FUNC_INFO << " - 120";
                    hostAreaNumber = hostAreaNumber1;
                }
                else
                {
                    hostAreaNumber = -1;
                }
            }
            else if (!hostSpecialPrefix1.isEmpty())
            {
                //qDebug() << Q_FUNC_INFO << " - 130";
                hostPrefix = hostSpecialPrefix1;
                if (hostSpecialNumberExist1)
                {
                    //qDebug() << Q_FUNC_INFO << " - 140";
                    hostAreaNumber = hostSpecialNumber1;
                }
                else
                {
                    hostAreaNumber = -1;
                }
            }
        }
        else if (!hostFullPrefix2.isEmpty())
        {
            //qDebug() << Q_FUNC_INFO << " - 200";
            hostFullPrefix = hostFullPrefix2;
            if (!hostNormalPrefix2.isEmpty())
            {
                //qDebug() << Q_FUNC_INFO << " - 210";
                hostPrefix = hostNormalPrefix2;
                if (hostAreaNumberExist2)
                {
                    //qDebug() << Q_FUNC_INFO << " - 220";
                    hostAreaNumber = hostAreaNumber2;
                }
                else
                {
                    //qDebug() << Q_FUNC_INFO << " - 230";
                    hostAreaNumber = -1;
                }
            }
            else if (!hostSpecialPrefix2.isEmpty())
            {
                //qDebug() << Q_FUNC_INFO << " - 240";
                hostPrefix = hostSpecialPrefix2;
                if (hostSpecialNumberExist2)
                {
                    //qDebug() << Q_FUNC_INFO << " - 250";
                    hostAreaNumber = hostSpecialNumber2;
                }
                else
                {
                    //qDebug() << Q_FUNC_INFO << " - 260";
                    hostAreaNumber = -1;
                }
            }
        }
        else
        {
            //qDebug() << Q_FUNC_INFO << " - 250";
            hostAreaNumber = -1;
        }


        if (homeSpecialPrefix.length()<=0)          // There is no special prefix
        {
            homePrefix = homeNormalPrefix;
        }
        else
        {
            homePrefix = homeSpecialPrefix;
            homeAreaNumber = -1;
            if (homeSpecialAreaNumberExist)         // Not all Special prefixes have area number // 3D2A vs 3D20A
                homeAreaNumber = homeSpecialAreaNumber;
        }

        //qDebug() << Q_FUNC_INFO << " - 300";

        if (!generalSuffix.isEmpty())               // looking for cases like EA4K/6 where the right call should be EA6/EA4K
        {
            //qDebug() << Q_FUNC_INFO << " - 301";
            bool generalSuffixIsANumber = false;
            int areaN = generalSuffix.toInt(&generalSuffixIsANumber);
            if (generalSuffixIsANumber)
            {
                //qDebug() << Q_FUNC_INFO << " - 320";
                hostPrefix = homePrefix;
                hostAreaNumber = areaN;
                hostFullPrefix = hostPrefix + generalSuffix;
                generalSuffix.clear();
            }
            //qDebug() << Q_FUNC_INFO << " - 399";
        }

        if (hostFullPrefix.isEmpty() && (homeFullPrefix.length()>=0))
        {
            hostFullPrefix  = homeFullPrefix;
            hostPrefix      = homePrefix;
            hostAreaNumber  = homeAreaNumber;
        }

      //qDebug() << Q_FUNC_INFO << " - @ hostFullPrefix     : " << hostFullPrefix;
      //qDebug() << Q_FUNC_INFO << " - @ hostPrefix         : " << hostPrefix;
      //qDebug() << Q_FUNC_INFO << " - @ hostAreaNumber     : " << QString::number(hostAreaNumber);
      //qDebug() << Q_FUNC_INFO << " - @ homeCallsign       : " << homeCallsign;
      //qDebug() << Q_FUNC_INFO << " - @ homeFullPrefix     : " << homeFullPrefix;
      //qDebug() << Q_FUNC_INFO << " - @ homePrefix         : " << homePrefix;
      //qDebug() << Q_FUNC_INFO << " - @ homeNormalPrefix   : " << homeNormalPrefix;
      //qDebug() << Q_FUNC_INFO << " - @ homeAreaNumber     : " << QString::number(homeAreaNumber);
      //qDebug() << Q_FUNC_INFO << " - @ homeSuffix         : " << homeSuffix;
      //qDebug() << Q_FUNC_INFO << " - @ suffix             : " << generalSuffix;

    }
    else if ( matchPrefix.hasMatch() )
    {
        //qDebug() << Q_FUNC_INFO << " - 50";
        bool prefixHostSpecialNumberExist = false;

        prefValid = true;

        hostFullPrefix          = matchPrefix.captured("hostprefix");
        hostPrefix              = matchPrefix.captured("hostnormalprefix");
        QString prefixHostSpecialPrefix      = matchPrefix.captured("hostspecialprefix");
        int _hostSpecialNumber  = matchPrefix.captured("hostspecialareanumber").toInt(&prefixHostSpecialNumberExist);
        hostAreaNumber          = matchPrefix.captured("hostareanumber").toInt(&homeAreaNumberExist);

        //qDebug() << Q_FUNC_INFO << " - hostFullPrefix:      " << hostFullPrefix;
        //qDebug() << Q_FUNC_INFO << " - hostPrefix:          " << hostPrefix;
        //qDebug() << Q_FUNC_INFO << " - hostSpecialPrefix:   " << prefixHostSpecialPrefix;

        // Now we have the data, let's select the good one


        if (prefixHostSpecialPrefix.length() > 0)
        {
            hostPrefix      = prefixHostSpecialPrefix;
            if (prefixHostSpecialNumberExist)
            {
                hostAreaNumber  = _hostSpecialNumber;
            }
            else
            {
                hostAreaNumber  = -1;
            }
        }
        else
        {
            if (!homeAreaNumberExist)
                hostAreaNumber = -1;
        }



        //qDebug() << Q_FUNC_INFO << " - @ hostFullPrefix     : " << hostFullPrefix;
        //qDebug() << Q_FUNC_INFO << " - @ hostPrefix         : " << hostPrefix;
        //qDebug() << Q_FUNC_INFO << " - @ hostAreaNumber     : " << QString::number(hostAreaNumber);
    }
    else
    {
        //qDebug() << Q_FUNC_INFO << " - 60";
        //it is an invalid callsign
        clear();
    }
    //qDebug() << Q_FUNC_INFO << " - 100";
}

QRegularExpression Callsign::callsignRegEx()
{
    // Returns a REGEX that matches a hamradio callsign
    return QRegularExpression(callsignRegExString(), QRegularExpression::CaseInsensitiveOption);
}

QRegularExpression Callsign::prefixRegEx()
{
    // Returns a REGEX that matches a hamradio prefix
    return QRegularExpression(prefixRegExString(), QRegularExpression::CaseInsensitiveOption);
}

QString Callsign::callsignRegExString()
{
    // This REGEX matches full callsigns including prefixes and suffixes
    QString aux = getSpecialPrefixes();
    return QString("^((?<hostfullprefix1>((?<hostspecialprefix1>%1)(?<hostspecialareanumber1>[0-9]*))|(?<hostnormalprefix1>[A-Z]{2}|B|F|G|I|K|M|N|R|U|W|[A-Z][0-9]|[0-9][A-Z])(?<hostareanumber1>[0-9]*))\\/)?(?<homecall>(?<homefullprefix>(?<homespecialprefix>%2)(?<homespecialareanumber>[0-9]*)|(?<homenormalprefix>B|F|G|I|K|M|N|R|U|W|[A-Z][0-9]|[A-Z]{1,2}|[0-9][A-Z])(?<homeareanumber>[0-9]+))(?<homesuffix>[A-Z]+))(\\/(?<suffix>(?<hostfullprefix2>(?<hostspecialprefix2>%3)(?<hostspecialareanumber2>[0-9]*)|(?<hostnormalprefix2>B|F|G|I|K|M|N|R|U|W|[A-Z][0-9]|[A-Z]{2}|[0-9][A-Z])(?<hostareanumber2>[0-9]*))|(?<generalsuffix>[A-Z0-9]*)))?$").arg(aux).arg(aux).arg(aux);
}

QString Callsign::prefixRegExString()
{
    //qDebug() << Q_FUNC_INFO;
    // Returns a REGEX string that matches a hamradio prefix like F in F/EA4K/QRP or EA6 in EA6/EA4K
    QString aux = getSpecialPrefixes();
    return QString("^(?<hostprefix>(?<hostspecialprefix>%1)(?<hostspecialareanumber>[0-9]*)|(?<hostnormalprefix>[A-Z]{2}|B|F|G|I|K|M|N|R|U|W|[A-Z][0-9]|[0-9][A-Z])(?<hostareanumber>[0-9]*))").arg(aux);
}

QString Callsign::getSpecialPrefixes()
{
    return QString("3D2|3D6|3D2C|3D2R|3DA|4U1I|4U1U|4U1V|KH7K|PY0F|PY0S|PY0T|R1FJ|SV2A|VK0H|VK0M|VK9C|VK9L|VK9M|VK9N|VK9W|VK9X|VP2E|VP2M|VP2V");
}
    // if getHomeIfEmpty is true and no hostprefix is identified, it will return homePrefix
QString Callsign::getCallsign()                             {return fullCall;}
QString Callsign::getHostFullPrefix()    {return hostFullPrefix;}
QString Callsign::getHostPrefix()
{
    //qDebug() << Q_FUNC_INFO << " - hostPrefix: " << hostPrefix;
    //qDebug() << Q_FUNC_INFO << " - homePrefix: " << homePrefix;
    return hostPrefix;
}
int Callsign::getHostAreaNumber()        {return hostAreaNumber;}

QString Callsign::getHomeCallsign()     {return homeCallsign;}
QString Callsign::getHomeFullPrefix()   {return homeFullPrefix;}
QString Callsign::getHomePrefix()       {return homePrefix;}
int Callsign::getHomeAreaNumber()       {return homeAreaNumber;}

QString Callsign::getHomeSuffix()       {return homeSuffix;}
QString Callsign::getSuffix()           {return generalSuffix;}



bool Callsign::isValid()
{
    return valid;
}

bool Callsign::isValidPrefix()
{
    return prefValid;
}

bool Callsign::isSimple()
{
    if (fullCall.contains('/'))
        return false;
    if (fullCall.contains('\\'))
        return false;
    return valid;
}

void Callsign::clear()
{
    fullCall.clear();
    hostFullPrefix.clear();
    hostPrefix.clear();
    homeSuffix.clear();
    generalSuffix.clear();
    homeCallsign.clear();           // EA4K
    homeFullPrefix.clear();         // EA4
    homePrefix.clear();             // EA

    hostAreaNumber          = -1;
    homeAreaNumber          = -1;       // 4
    valid                   = false;            // The entered string is a correct callsign
    prefValid               = false;            // The entered string is a correct prefix
}

bool Callsign::isAOneLetterHostPrefix()
{
    QList<QChar> validFirstLettersOnly = {'B', 'F', 'G', 'I', 'K', 'M', 'N', 'R', 'U', 'W'};
    if (hostPrefix.length() == 1)
        return validFirstLettersOnly.contains (hostPrefix);
    return false;
}

// Based on wiki information
// https://en.wikipedia.org/wiki/Amateur_radio_call_signs
/*
QStringList Callsign::secondarySpecialSuffixes =
    {
        "A",   // operator at a secondary location registered with the licensing authorities
        "AM",  // aeronautical mobile
        "M",   // mobile operation
        "MM",  // marine mobile
        "P",   // portable operation
        "QRP",  // QRP - unofficial
        "R",    // repeaters
        "B",    // beacon
        "LGT"   // 'LIGHTHOUSE' or 'LIGHTSHIP'  - unofficial
        "LH"    // LightHouse

bool Utilities::isAValidOperatingSuffix (const QString &_c)
{
    //TODO: This list should be moved to Callsign
    //qDebug() << QString("%1-%2").arg(Q_FUNC_INFO).arg(parentName) << _c;
    QStringList validSuffixes = {"A", "P", "Q", "AM", "M", "MM", "LH", "R", "J", "FF", "QRP", "QRPP", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};
    return validSuffixes.contains (_c);
}

};
*/
// https:// cqwpx.com/rules.htm
/*
1. A PREFIX is the letter/numeral combination which forms the first part of the amateur call.
Examples: N8, W8, WD8, HG1, HG19, KC2, OE2, OE25, LY1000, etc. Any difference in the numbering, lettering,
or order of same shall count as a separate prefix.

A station operating from a DXCC entity different from that indicated by its call sign is required to sign portable.
The portable prefix must be an authorized prefix of the country/call area of operation.
In cases of portable operation, the portable designator will then become
the prefix. Example: N8BJQ operating from Wake Island would sign N8BJQ/KH9 or N8BJQ/NH9.
KH6XXX operating from Ohio must use an authorized prefix for the U.S. 8th district (/W8, /AD8, etc.).

Portable designators without numbers will be assigned a zero (Ø) after the second letter of the portable
designator to form the prefix.
Example: PA/N8BJQ would become PAØ. All calls without numbers will be assigned a zero (Ø) after the first
two letters to form the prefix. Example: XEFTJW would count as XEØ. Maritime mobile, mobile, /A, /E, /J, /P,
or other license class identifiers do not count as prefixes.
*/