1 /***************************************************************************
2                           world.cpp  -  description
3                              -------------------
4     begin                : sept 2011
5     copyright            : (C) 2011 by Jaime Robles
6     email                : jaime@robles.es
7  ***************************************************************************/
8 
9 /*****************************************************************************
10  * This file is part of KLog.                                             *
11  *                                                                           *
12  *    KLog is free software: you can redistribute it and/or modify        *
13  *    it under the terms of the GNU General Public License as published by   *
14  *    the Free Software Foundation, either version 3 of the License, or      *
15  *    (at your option) any later version.                                    *
16  *                                                                           *
17  *    KLog is distributed in the hope that it will be useful,             *
18  *    but WITHOUT ANY WARRANTY; without even the implied warranty of         *
19  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
20  *    GNU General Public License for more details.                           *
21  *                                                                           *
22  *    You should have received a copy of the GNU General Public License      *
23  *    along with KLog.  If not, see <https://www.gnu.org/licenses/>.       *
24  *                                                                           *
25  *****************************************************************************/
26 
27 #include "world.h"
28 
29 
30 /*
31  QHash<QString, int> worldPrefixes;
32 To insert a (key, value) pair into the hash, you can use operator[]():
33  hash["EA"] = 130;
34  hash["EA6"] = 131;
35  hash["EA8"] = 132;
36 
37 */
World(DataProxy_SQLite * dp,const QString & _parentFunction)38 World::World(DataProxy_SQLite *dp, const QString &_parentFunction)
39 {
40     //qDebug() << "World::World(0)" << _parentFunction << QT_ENDL;
41     constrid = 1;
42 
43     worldModel = new QSqlRelationalTableModel(this);
44     numberOfEntities = 0;
45     progressBarPosition = 0;
46     cqz = -1;
47     ituz = -1;
48     ret = false;
49     continentId = -1;
50     lat = 0.0;
51     lon = 0.0;
52     utc = 0.0;
53     klogDir = "";
54     klogVersion = "";
55     locator = new Locator();
56     created = false;
57     dataProxy = dp;
58     //awards = new Awards(Q_FUNC_INFO);
59       //qDebug() << "World::World(0) - END" << QT_ENDL;
60 }
61 
World(DataProxy_SQLite * dp,const QString & _klogDir,const QString & _parentFunction)62 World::World(DataProxy_SQLite *dp, const QString &_klogDir, const QString &_parentFunction)
63 {
64       //qDebug() << "World::World(1): Dir" << _klogDir << _parentFunction << QT_ENDL;
65     constrid = 2;
66     klogDir = _klogDir;
67     klogVersion = "";
68     worldModel = new QSqlRelationalTableModel(this);
69     numberOfEntities = 0;
70     progressBarPosition = 0;
71     created = false;
72     cqz = -1;
73     ituz = -1;
74     ret = false;
75     continentId = -1;
76     lat = 0.0;
77     lon = 0.0;
78     utc = 0.0;
79     //flagsDir=":/flags/";
80     locator = new Locator();
81     dataProxy = dp;
82       //qDebug() << "World::World(1): - END"  << QT_ENDL;
83 
84 }
85 
World(DataProxy_SQLite * dp,const QString & _klogDir,const QString & _klogVer,const QString & _parentFunction)86 World::World(DataProxy_SQLite *dp, const QString &_klogDir, const QString &_klogVer, const QString &_parentFunction)
87 {
88    //qDebug() << "World::World(2): Dir" << _klogDir << _parentFunction << QT_ENDL;
89     constrid = 3;
90     klogDir = _klogDir;
91     klogVersion = _klogVer;
92     //qDebug() << "World::World(2): 1" << QT_ENDL;
93     worldModel = new QSqlRelationalTableModel(this);
94     //qDebug() << "World::World(2): 2" << QT_ENDL;
95     numberOfEntities = 0;
96     progressBarPosition = 0;
97     created = false;
98     cqz = -1;
99     ituz = -1;
100     ret = false;
101     continentId = -1;
102     lat = 0.0;
103     lon = 0.0;
104     utc = 0.0;
105     //appDir = QString();
106     //flagsDir=":/flags/";
107       //qDebug() << "World::World(2): 2" << QT_ENDL;
108     locator = new Locator();
109       //qDebug() << "World::World(2): 3" << QT_ENDL;
110     dataProxy = dp;
111     //qDebug() << "World::World(2): - END" << QT_ENDL;
112 
113 }
114 
~World()115 World::~World()
116 {
117       //qDebug() << "World::~World" << QT_ENDL;
118 }
119 
120 
recreate(const QString & _worldFile)121 bool World::recreate(const QString &_worldFile)
122 {
123      //qDebug() << "World::recreate: " << _worldFile << QT_ENDL;
124 
125     QSqlQuery query;
126     if (query.exec("DELETE FROM entity"))
127     {
128           //qDebug() << "World::recreate: EMPTY entity"  << QT_ENDL;
129         if (query.exec("DELETE FROM prefixesofentity"))
130         {
131              //qDebug() << "World::recreate: EMPTY prefixesofentity"  << QT_ENDL;
132              if (create(_worldFile))
133              {
134                  return insertSpecialEntities ();
135              }
136         }
137         else
138         {//TODO: Manage the query error
139              //qDebug() << "World::recreate: FAILED TO EMPTY prefixesofentity"  << QT_ENDL;
140             emit queryError(Q_FUNC_INFO, query.lastError().databaseText(), query.lastError().nativeErrorCode(), query.lastQuery());
141             return false;
142         }
143     }
144     else
145     {//TODO: Manage the query error
146          //qDebug() << "World::recreate: FAILED TO EMPTY entity"  << QT_ENDL;
147         emit queryError(Q_FUNC_INFO, query.lastError().databaseText(), query.lastError().nativeErrorCode(), query.lastQuery());
148         return false;
149     }
150 
151     return false;
152 }
153 
create(const QString & _worldFile)154 bool World::create(const QString &_worldFile)
155 {
156        //qDebug() << "World::create: " << _worldFile << QT_ENDL;
157     //klogDir = _worldFile;
158        //qDebug() << "World::create: 2 " <<  klogDir << QT_ENDL;
159 
160 
161     if (readCTYCSV(_worldFile))
162     {
163         created = true;
164          //qDebug() << "World::create: TRUE"  << QT_ENDL;
165 
166     }else
167     {
168         created = false;
169          //qDebug() << "World::create: FALSE"  << QT_ENDL;
170 
171     }
172     if (created)
173     {
174         created = insertSpecialEntities();
175     }
176     if (created)
177     {
178         //dataProxy->updateISONames();
179         if (dataProxy->updateISONames())
180         {
181                //qDebug() << "World::create: updateISONames TRUE"  << QT_ENDL;
182         }
183         else
184         {
185                //qDebug() << "World::create: updateISONames FALSE"  << QT_ENDL;
186         }
187     }
188 
189        //qDebug() << "World::create: END"  << QT_ENDL;
190     return created;
191 }
192 
createWorldModel()193 void World::createWorldModel()
194 {
195     worldModel->setTable("entity");
196 
197     worldModel->setRelation(Entity_Continent, QSqlRelation("continent", "id", "shortname"));
198     worldModel->setSort(Entity_Name, Qt::AscendingOrder);
199     worldModel->setHeaderData(Entity_Name, Qt::Horizontal, tr("Entity"));
200     worldModel->setHeaderData(Entity_Continent, Qt::Horizontal, tr("Continent"));
201 
202     worldModel->select();
203 
204 }
205 
processLine(const QString & _line)206 QStringList World::processLine(const QString &_line)
207 {
208 
209        //qDebug() << "World::processLine: received: " << _line << QT_ENDL;
210     //QString queryString;
211     QStringList aa;
212     QString line;
213     line = (_line).simplified();
214     if ((line).count('\'')) // Replaces ' by _
215     //The error comes from "Cote d'Ivoire" that breaks the SQL
216     {
217         line.replace(QChar('\''), QChar('_'));
218     }
219        //qDebug() << "World::processLine: Received: " << line << QT_ENDL;
220     //QSqlQuery query1;
221 
222     nullValue=-1;
223 
224 
225     if ( (line.count(':') == 8 ) ) // First line of an Entity
226     { //United States:            05:  08:  NA:   43.00:    87.90:     5.0:  K:
227            //qDebug() << "World::processLine first: " << line << QT_ENDL;
228         numberOfEntities++;
229         list.clear();
230         list << line.split(':');
231         entityName = list[0];
232         cqz = list[1].toInt();
233         ituz = list[2].toInt();
234         continentName = (list[3]).simplified();
235 
236         continentId=-1;
237         lat = list[4].toDouble();
238         lon = list[5].toDouble();
239         utc  = ((list[6]).simplified()).toDouble();
240         //utc  = ((list[6]).simplified()).toInt();
241         prefix = list[7];
242         currentPrefix = prefix;
243 
244         continentId = dataProxy->getContinentIdFromContinentShortName(continentName);
245 
246         //queryString = "SELECT id FROM continent WHERE shortname=='" + continentName + "'";
247         //query1.exec(queryString);
248         //query1.next();
249         //if (query1.isValid())
250         //{
251         //    continentId = (query1.value(0)).toInt();
252         //}
253 
254 
255             //qDebug() << "World::processLine Query - Read/continenId: " << continentName << "/" << QString::number(continentId) << QT_ENDL;
256 
257         //queryString = QString("INSERT INTO entity (id, name, cqz, ituz, continent, latitude, longitude, utc, dxcc, mainprefix) VALUES (NULL,'%1','%2','%3','%4','%5','%6','%7','%8','%9');\n").arg(entityName).arg(cqz).arg(ituz).arg(QString::number(continentId)).arg(lat).arg(lon).arg(utc).arg(numberOfEntities).arg(prefix);
258         aa << entityName << QString::number(cqz) << QString::number(ituz) <<QString::number(continentId) << QString::number(lat) << QString::number(lon) << QString::number(utc) << QString::number(numberOfEntities) << prefix;
259         progressBarPosition++;
260         return aa;
261         //ret = query.exec(queryString);
262 
263     }
264     aa.clear();
265     return aa;
266 }
267 
268 
269 
270 
processLineP(const QString & _line,const int _processingEntity)271 QStringList World::processLineP(const QString &_line, const int _processingEntity){
272     //Returns QStringList: prefix << dxcc << cqz << ituz OR CurrentEntity as a number
273 
274        //qDebug() << "World::processLineP: Entity/received: " << QString::number(_processingEntity) << "/" << _line << QT_ENDL;
275     //QString queryString;
276     QString line;
277     int currentEntity = _processingEntity;
278     int _cqz, _ituz;
279     //_cqz = 0;
280     //_ituz = 0;
281     line = (_line).simplified();
282 
283 
284     if ((line).count('\'')) // Replaces ' by _
285     //The error comes from "Cote d'Ivoire" that breaks the SQL
286     {
287         line.replace(QChar('\''), QChar('_'));
288     }
289 
290        //qDebug() << "World::processLineP: Received: " << line << QT_ENDL;
291     //QSqlQuery _queryp;
292     QStringList aa, _list;
293     aa.clear();
294 
295     if ( (line.count(':') == 8 ) ) // First line of an Entity
296     { //United States:            05:  08:  NA:   43.00:    87.90:     5.0:  K:
297          currentEntity++;
298          aa << QString::number(currentEntity);
299          return aa;
300 
301     }
302     else if ( line.endsWith(';') ) // Last line of the Entity
303     { //    =WX4TM(4),=WX5S(3)[6],=WY5I(5)[8],=WY7I(4)[7],=WY7LL(4)[7],=WZ4F(4);
304            //qDebug() << "World::processLineP last line (; detected): " << line << QT_ENDL;
305         _cqz = dataProxy->getCQzFromEntity(currentEntity);
306         _ituz = dataProxy->getITUzFromEntity(currentEntity);
307 
308         line = line.remove(';');
309 
310         if (line.count(',') == 0) // Only one prefix in the final line
311         {
312             prefixAndZones = readZones(line, _cqz, _ituz);
313             //aa.clear();
314             aa << prefixAndZones.at(0) << QString::number(currentEntity) << prefixAndZones.at(1) << prefixAndZones.at(2);
315 
316 
317              //readZones returns a QStringList: prefix, CQz, ITUz
318              //Returns QStringList: prefix << dxcc << cqz << ituz OR CurrentEntity as a number
319             return aa;
320         }
321         else // More than just one prefix in the final line
322         {
323                //qDebug() << "World::processLineP Query (MORE one final)(line):" << line << QT_ENDL;
324             _list.clear();
325             _list << line.split(',');
326             aa.clear();
327             //queryString.clear();
328 
329             for (int i = 0; i < _list.size(); ++i)
330             {
331             // PROCESS THE LINE
332                    //qDebug() << "World::processLineP LastLine prefixes" << _list.at(i) << QT_ENDL;
333                 //readZones returns a QStringList: prefix, CQz, ITUz
334                 prefixAndZones = readZones(_list.at(i), _cqz, _ituz);
335                 //aa.clear();
336                 aa << prefixAndZones.at(0) << QString::number(currentEntity) << prefixAndZones.at(1) << prefixAndZones.at(2);
337                 //return aa;
338             }
339 
340                //qDebug() << "World::processLineP: END" <<endl;
341             return aa;
342 
343         }
344 
345     }
346     else // Lines of the middle...
347     {  //    =W4KW(4),=W4LC(4),=W4LSC(3)[6],=W4LWW(4),=W4NBS(4),=W4NI(4),=W4NTI(4),
348            //qDebug() << "World::processLine middle (no ; detected): " << line << QT_ENDL;
349 
350         _cqz = dataProxy->getCQzFromEntity(currentEntity);
351         _ituz = dataProxy->getITUzFromEntity(currentEntity);
352 
353         if (line.endsWith(','))
354         {
355             line.chop(1);
356         }
357 
358         if ((line.split(',')).size() == 1) // Only one prefix in the middle line
359         { // Not usual, added this check for sanity reasons only
360                //qDebug() << "World::processLine Query: (only one middle) " << QT_ENDL;
361             line = line.remove(',');
362             prefixAndZones = readZones(line, _cqz, _ituz);
363             aa.clear();
364             aa << prefixAndZones.at(0) << QString::number(currentEntity) << prefixAndZones.at(1) << prefixAndZones.at(2);
365 
366             return aa;
367         }
368         else
369         {
370                //qDebug() << "World::processLine Query: (MORE than one middle) " << QT_ENDL;
371             list.clear();
372             list << line.split(',');
373 
374             //queryString.clear();
375             aa.clear();
376             for (int i = 0; i < list.size(); ++i)
377             {
378             // PROCESS THE LINE
379                 prefixAndZones = readZones(list[i], _cqz, _ituz);
380 
381                 aa << prefixAndZones.at(0) << QString::number(currentEntity) << prefixAndZones.at(1) << prefixAndZones.at(2);
382 
383                 //ret = query.exec(queryString);
384             }
385         }
386     }
387     //aa.clear();
388     return aa;
389 }
390 
391 
readZones(const QString & pref,const int _cq,const int _itu)392 QStringList World::readZones (const QString &pref, const int _cq, const int _itu)
393 {
394     //Returns a QStringList: prefix, CQz, ITUz
395         //qDebug() << "World::readZones: (" << pref << "/" << QString::number(_cq) <<"/" << QString::number(_itu)<< ")" << QT_ENDL;
396 
397     QStringList result;
398     int cq = _cq;
399     int itu = _itu;
400     QString azone;
401     QString aux = pref;
402 
403     if (aux.startsWith("=")){
404         aux = aux.remove(0,1);
405 
406     }
407 
408     if(aux.count('[')==1) // Check if has special CQz
409     {
410            //qDebug() << "World::readZones DETECTED [ !!!!" << QT_ENDL;
411         azone = (aux.midRef(aux.indexOf('[')+1)).toString();
412            //qDebug() << "World::readZones (ITU)-1: " << aux << " right of " << QString::number(aux.indexOf('[')) << " = " << azone << QT_ENDL;
413         itu = (azone.left(azone.indexOf(']'))).toInt();
414 
415            //qDebug() << "World::readZones (ITU)-2: " << azone.left(azone.indexOf(']')) << QT_ENDL;
416         aux = aux.left(aux.indexOf('['));
417            //qDebug() << "World::readZones (ITU): "  << pref << "/" << QString::number(itu) << "/" << aux << QT_ENDL;
418     }
419 
420     if(aux.count('(')==1) // Check if has special CQz
421     {
422            //qDebug() << "World::readZones DETECTED ( !!!!" << QT_ENDL;
423         azone = (aux.midRef(aux.indexOf('(')+1)).toString();
424         cq = (azone.left(azone.indexOf(')'))).toInt();
425         aux = aux.left(aux.indexOf('('));
426            //qDebug() << "World::readZones (CQ): "  << pref << "/" << QString::number(cq) << "/" << aux << QT_ENDL;
427     }
428        //qDebug() << "World::readZones (Pref/CQ/ITU): "  << pref << "= " << aux <<"/" << QString::number(cq) << "/" << QString::number(itu) << QT_ENDL;
429 
430     result << aux << QString::number(cq) << QString::number(itu);
431        //qDebug() << "World::readZones (Pref/CQ/ITU): " << result << QT_ENDL;
432     return result;
433 
434 
435 }
436 
getPrefixId(const QString & _qrz)437 int World::getPrefixId(const QString &_qrz)
438 {
439     //qDebug() << "World::getPrefixId: -" << _qrz <<"-" << QT_ENDL;
440     //TODO: Instead of going from long to short, identify prefixes from the begining:
441     // character(may be number) + number
442 
443     if (_qrz.length() < 1)
444     {
445         return -1;
446     }
447     int entityID = 0;
448 
449     QString aux = changeSlashAndFindPrefix((_qrz).toUpper());
450 
451     while ((entityID <= 0) && (aux.length()>=1) )
452     {
453         //qDebug() << "World::getPrefixId: in the while before calling dataProxy: " << aux << QT_ENDL;
454         if (aux == "KG4")
455         {
456             if (_qrz.length()!=5)
457             {
458                 aux = "K";
459             }
460         }
461         entityID = dataProxy->getDXCCFromPrefix(aux);
462           //qDebug() << "World::getPrefixId: in the while After calling dataProxy" << QT_ENDL;
463 
464         //qDebug() << "World::getPrefixId: in the while" << aux << " = " <<  QString::number(entityID) << QT_ENDL;
465         if (entityID<=0)
466         {
467             aux.chop(1);
468         }
469 
470     }
471     //qDebug() << "World::getPrefixId: END: " <<  _qrz << QString::number(entityID) << QT_ENDL;
472     return entityID;
473 }
474 
getQRZEntityName(const QString & _qrz)475 QString World::getQRZEntityName(const QString &_qrz)
476 {
477         //qDebug() << "World::getQRZEntityName: " << _qrz << QT_ENDL;
478     if (_qrz.length() < 1 )
479     {
480         return QString();
481     }
482     //QString queryString;
483     //QSqlQuery query;
484     int prefixIDNumber = getPrefixId(_qrz);
485     return getEntityName(prefixIDNumber);
486 }
487 
getEntityName(const int _entityN)488 QString World::getEntityName(const int _entityN)
489 {
490     int prefixIDNumber = _entityN;
491     if (prefixIDNumber<=0)
492     {
493         return QString();
494     }
495     return dataProxy->getEntityNameFromId(prefixIDNumber);
496 }
497 
getQRZCqz(const QString & _qrz)498 int World::getQRZCqz(const QString &_qrz)
499 {
500         //qDebug() << "World::getQRZCqz: " << _qrz << QT_ENDL;
501 
502      if (_qrz.length() < 1 )
503      {
504          return -1;
505      }
506 
507      int i = -1;
508      QString aux = _qrz;
509 
510      while ((i <= 0) && (aux.length()>=1) )
511      {
512          i = dataProxy->getCQzFromPrefix(aux);
513          if (i<=0)
514          {
515              aux.chop(1);
516          }
517      }
518      return i;
519 
520 
521 
522     //int prefixIdNumber = getPrefixId(_qrz);
523     //return getEntityCqz(prefixIdNumber);
524 
525 }
526 
getQRZItuz(const QString & _qrz)527 int World::getQRZItuz(const QString &_qrz)
528 {
529        //qDebug() << "World::getQRZItuz: " << _qrz << QT_ENDL;
530     if (_qrz.length() < 1 )
531     {
532         return -1;
533     }
534 
535     int i = -1;
536     QString aux = _qrz;
537 
538     while ((i <= 0) && (aux.length()>=1) )
539     {
540         i = dataProxy->getITUzFromPrefix(aux);
541         if (i<=0)
542         {
543             aux.chop(1);
544         }
545     }
546     return i;
547 
548 }
549 
550 
getEntityCqz(const int _enti)551 int World::getEntityCqz(const int _enti)
552 {
553     if (_enti < 1 )
554     {
555         return -1;
556     }
557 
558     return dataProxy->getCQzFromEntity(_enti);
559 /*
560     QSqlQuery query;
561     QString queryString;
562 
563     //queryString = QString("SELECT cqz FROM prefixesofentity WHERE (mainprefix NOT LIKE '*%') AND dxcc='%1'").arg(prefixIDNumber);
564     queryString = "SELECT cqz FROM prefixesofentity WHERE dxcc=='" + QString::number(_enti) +"'";
565     query.exec(queryString);
566     query.next();
567        //qDebug() << "World::getEntityCqz: " << QString::number(_enti) << " = " <<  (query.value(0)).toInt() << QT_ENDL;
568 
569     if (query.isValid()){
570         return (query.value(0)).toInt();
571     }else{
572         return -1;
573     }
574 
575     return -1;
576 */
577 }
578 
579 
getEntityItuz(const int _enti)580 int World::getEntityItuz(const int _enti)
581 {
582     if (_enti < 1 )
583     {
584         return -1;
585     }
586     return dataProxy->getITUzFromEntity(_enti);
587 /*
588     QSqlQuery query;
589     QString queryString;
590 
591     queryString = "SELECT ituz FROM prefixesofentity WHERE dxcc=='" + QString::number(_enti) +"'";
592     query.exec(queryString);
593     query.next();
594        //qDebug() << "World::getEntityItuz: " << QString::number(_enti) << " = " <<  (query.value(0)).toInt() << QT_ENDL;
595 
596     if (query.isValid()){
597         return (query.value(0)).toInt();
598     }else{
599         return -1;
600     }
601 
602     return -1;
603 */
604 }
605 
getQRZARRLId(const QString & _qrz)606 int World::getQRZARRLId(const QString &_qrz)
607 {
608       //qDebug() << "World::getQRZARRLId" << _qrz << QT_ENDL;
609 
610     if (_qrz.length() < 1 )
611     {
612         return -1;
613     }
614 
615     int prefixIdNumber = getPrefixId(_qrz);
616     return prefixIdNumber;
617 
618 }
619 
getQRZEntityMainPrefix(const QString & _qrz)620 QString World::getQRZEntityMainPrefix(const QString &_qrz)
621 {
622     if (_qrz.length() < 1 )
623     {
624         return "";
625     }
626     int i = getQRZARRLId(_qrz);
627     return getEntityMainPrefix(i);
628 
629     //QString queryString;
630     //QSqlQuery query;
631 
632 
633 /*
634     queryString = "SELECT mainprefix FROM entity WHERE dxcc=='" + QString::number(i) +"'";
635 
636     if (!query.exec(queryString))
637     {
638         return "";
639     }
640     else
641     {
642         query.next();
643 
644            //qDebug() << "World::getQRZEntityMainPrefix(id/qrz): " << QString::number(i) << "/" <<_qrz << " = " <<  (query.value(0)).toString() << QT_ENDL;
645         if (query.isValid()){
646 
647             return (query.value(0)).toString();
648         }
649         else
650         {
651 
652             return "";
653         }
654     }
655 
656 
657 
658 
659     return "";
660 */
661 }
662 
663 
getEntityMainPrefix(const int _entityN)664 QString World::getEntityMainPrefix(const int _entityN)
665 {
666     if (_entityN <= 0 )
667     {
668         return QString();
669         //return tr("NONE");
670     }
671     return dataProxy->getEntityMainPrefix(_entityN);
672  /*
673     QString queryString;
674     QSqlQuery query;
675 
676     queryString = QString("SELECT mainprefix FROM entity WHERE (mainprefix NOT LIKE '*%') AND dxcc='%1'").arg(_entityN);
677 
678     //queryString = "SELECT mainprefix FROM entity WHERE dxcc=='" + QString::number(_entityN) +"'";
679 
680     if (!query.exec(queryString))
681     {
682         return "";
683     }
684     else
685     {
686         query.next();
687 
688         if (query.isValid()){
689             return (query.value(0)).toString();
690         }else{
691             return "";
692         }
693     }
694     return "";
695 */
696 }
697 
698 
isNewCQz(const int _cqz)699 bool World::isNewCQz(const int _cqz)
700 {
701     return dataProxy->isNewCQz(_cqz);
702 /*
703     QString queryString;
704     QSqlQuery query;
705     queryString = "SELECT id FROM log WHERE cqz=='" + QString::number(_cqz) +"'";
706 
707     if (!query.exec(queryString))
708     {
709         return false;
710     }
711     else
712     {
713         query.next();
714 
715         if (query.isValid()){
716             return true;
717         }else{
718             return false;
719         }
720     }
721     return false;
722 */
723 }
724 
isNewEntity(const int _entityN)725 bool World::isNewEntity(const int _entityN)
726 {
727     if (_entityN <= 0)
728     {
729         return false;
730     }
731     return dataProxy->isNewEntity(_entityN);
732 /*
733     QString queryString;
734     QSqlQuery query;
735     queryString = "SELECT id FROM log WHERE dxcc=='" + QString::number(_entityN) +"'";
736 
737     if (!query.exec(queryString))
738     {
739         return false;
740     }
741     else
742     {
743         query.next();
744 
745         if (query.isValid()){
746             return true;
747         }else{
748             return false;
749         }
750     }
751     return false;
752 */
753 }
754 
getQRZContinentShortName(const QString & _qrz)755 QString World::getQRZContinentShortName(const QString &_qrz)
756 {
757         //qDebug() << "World::getQRZContinentShortName: " << _qrz << QT_ENDL;
758     //QString continentNumber = getQRZContinentNumber (_qrz);
759     return getContinentShortName (getQRZARRLId(_qrz));
760 
761 }
762 
763 
getContinentShortName(const int _enti)764 QString World::getContinentShortName(const int _enti)
765 {
766        //qDebug() << "World::getQRZContinentShortName: " << QString::number(_enti) << QT_ENDL;
767 
768     //QString continentNumber = QString::number(_enti);
769 
770     if ( _enti < 0 )
771     {
772         return "--";
773     }
774     QString a = dataProxy->getContinentShortNameFromEntity(_enti);
775     if (a.length()!=2)
776     {
777         return "--";
778     }
779     else
780     {
781         return a;
782     }
783 
784 /*
785     QString queryString;
786     QSqlQuery query;
787     queryString = QString("SELECT continent FROM entity WHERE (mainprefix NOT LIKE '*%') AND dxcc='%1'").arg(_enti);
788     //queryString = "SELECT continent FROM entity WHERE dxcc=='" + continentNumber +"'";
789 
790     if (!query.exec(queryString))
791     {
792         return "--";
793     }
794     else
795     {
796         query.next();
797 
798         if ( !(query.isValid()) ) {
799                //qDebug() << "World::getQRZContinentShortName: NO VALID"  << QT_ENDL;
800             return "--";
801         }else{
802                //qDebug() << "World::getQRZContinentShortName: VALID"  << QT_ENDL;
803             return (query.value(0)).toString();
804         }
805     }
806 
807     return "--";
808 */
809 }
810 
getQRZContinentNumber(const QString & _qrz)811 QString World::getQRZContinentNumber(const QString &_qrz)
812 {
813        //qDebug() << "World::getQRZContinentNumber: " << _qrz << QT_ENDL;
814 
815     int i = getQRZARRLId(_qrz);
816     return QString::number(getContinentNumber(i));
817   /*
818     QString a;
819     QString queryString;
820     QSqlQuery query;
821 
822     queryString = QString("SELECT continent FROM entity WHERE (mainprefix NOT LIKE '*%') AND dxcc='%1'").arg(i);
823 
824     //queryString = "SELECT continent FROM entity WHERE dxcc=='" + QString::number(i) +"'";
825 
826 
827     if (!query.exec(queryString))
828     {
829         return "-1";
830     }
831     else
832     {
833         query.next();
834         if ( !(query.isValid()) ) {
835               //qDebug() << "World::getQRZContinentNumber(qrz/i/Cont): NO VALID"  << QT_ENDL;
836             return "-1";
837         }else{
838                //qDebug() << "World::getQRZContinentNumber(qrz/i/Cont): VALID"  << QT_ENDL;
839             a = (query.value(0)).toString();
840         }
841 
842 
843            //qDebug() << "World::getQRZContinentNumber(qrz/i/Cont): " <<_qrz << "/" << QString::number(i) << "/" <<  a << QT_ENDL;
844         return a;
845     }
846     return "-1";
847 */
848 }
849 
getContinentNumber(const int _enti)850 int World::getContinentNumber(const int _enti)
851 {
852        //qDebug() << "World::getQRZContinentNumber: " << QString::number(_enti) << QT_ENDL;
853     if (_enti <= 0)
854     {
855         return -1;
856     }
857     return dataProxy->getContinentIdFromEntity(_enti);
858 /*
859     int a = -1;
860 
861     QString queryString;
862     QSqlQuery query;
863     int i = _enti;
864     queryString = QString("SELECT continent FROM entity WHERE (mainprefix NOT LIKE '*%') AND dxcc='%1'").arg(i);
865     //queryString = "SELECT continent FROM entity WHERE dxcc=='" + QString::number(i) +"'";
866 
867 
868     if (!query.exec(queryString))
869     {
870         return -1;
871     }
872     else
873     {
874         query.next();
875         if ( !(query.isValid()) )
876         {
877             return -1;
878         }else{
879             a = (query.value(0)).toInt();
880             queryString = "SELECT id FROM continent where shortname=='" + query.value(0).toString() + "'";
881             if (query.exec(queryString))
882             {
883                 query.next();
884                 if(query.isValid())
885                 {
886                     return query.value(0).toInt();
887                 }
888                 else
889                 { // Value not valid
890                     return -1;
891                 }
892             }
893             else
894             { // Error in the query
895                 return -1;
896             }
897 
898         }
899         return a;
900     }
901     return -1;
902 */
903 }
904 
getQRZLongitude(const QString & _qrz)905 double World::getQRZLongitude(const QString &_qrz)
906 {
907 
908     int i = getQRZARRLId(_qrz);
909     return dataProxy->getLongitudeFromEntity(i);
910 /*
911     QString queryString;
912     QSqlQuery query;
913     queryString = "SELECT longitude FROM entity WHERE dxcc=='" + QString::number(i) +"'";
914 
915 
916     if (!query.exec(queryString))
917     {
918         return 0.0;
919     }
920     else
921     {
922         query.next();
923 
924         if ( !(query.isValid()) ) {
925                //qDebug() << "World::getQRZLongitude: NO VALID"  << QT_ENDL;
926             return 0.0;
927         }else{
928                //qDebug() << "World::getQRZLongitude: VALID"  << QT_ENDL;
929             return (query.value(0)).toDouble();
930         }
931 
932     }
933     return 0.0;
934 */
935 }
936 
getLongitude(const int _enti)937 double World::getLongitude(const int _enti)
938 {
939     if (_enti <= 0)
940     {
941         return 0.0;
942     }
943 
944     return dataProxy->getLongitudeFromEntity(_enti);
945 
946 
947 /*
948 
949     QString queryString;
950     QSqlQuery query;
951 
952     int i = _enti;
953     queryString = "SELECT longitude FROM entity WHERE dxcc=='" + QString::number(i) +"'";
954 
955 
956     if (!query.exec(queryString))
957     {
958         return 0.0;
959     }
960     else
961     {
962         query.next();
963 
964         if ( !(query.isValid()) ) {
965                //qDebug() << "World::getQRZLongitude: NO VALID"  << QT_ENDL;
966             return 0.0;
967         }else{
968                //qDebug() << "World::getQRZLongitude: VALID"  << QT_ENDL;
969             return (query.value(0)).toDouble();
970         }
971 
972     }
973     return 0.0;
974 */
975 }
976 
getQRZLatitude(const QString & _qrz)977 double World::getQRZLatitude(const QString &_qrz)
978 {
979 
980     int i = getQRZARRLId(_qrz);
981     return dataProxy->getLatitudeFromEntity(i);
982  /*
983     QString queryString;
984     QSqlQuery query;
985     queryString = "SELECT latitude FROM entity WHERE dxcc=='" + QString::number(i) +"'";
986 
987 
988     if (!query.exec(queryString))
989     {
990         return 0.0;
991     }
992     else
993     {
994         query.next();
995 
996         if ( !(query.isValid()) ) {
997                //qDebug() << "World::getQRZLatitud: NO VALID"  << QT_ENDL;
998             return 0.0;
999         }else{
1000                //qDebug() << "World::getQRZLatitud: VALID"  << QT_ENDL;
1001             return (query.value(0)).toDouble();
1002         }
1003     }
1004 
1005     return 0.0;
1006 */
1007 }
1008 
getLatitude(const int _enti)1009 double World::getLatitude(const int _enti)
1010 {
1011     if (_enti <= 0)
1012     {
1013         return 0.0;
1014     }
1015     return dataProxy->getLatitudeFromEntity(_enti);
1016 
1017 /*
1018     QString queryString;
1019     QSqlQuery query;
1020     int i = _enti;
1021     queryString = "SELECT latitude FROM entity WHERE dxcc=='" + QString::number(i) +"'";
1022 
1023 
1024     if (!query.exec(queryString))
1025     {
1026         return 0.0;
1027     }
1028     else
1029     {
1030         query.next();
1031 
1032         if ( !(query.isValid()) ) {
1033                //qDebug() << "World::getQRZLatitud: NO VALID"  << QT_ENDL;
1034             return 0.0;
1035         }else{
1036                //qDebug() << "World::getQRZLatitud: VALID"  << QT_ENDL;
1037             return (query.value(0)).toDouble();
1038         }
1039     }
1040 
1041     return 0.0;
1042 */
1043 }
1044 
getQRZLocator(const QString & _qrz)1045 QString World::getQRZLocator(const QString &_qrz)
1046 {
1047     if (_qrz.length() < 1)
1048     {
1049         return "";
1050     }
1051     return locator->getLocator(getQRZLongitude(_qrz), getQRZLatitude(_qrz));
1052 }
1053 
getLocator(const int _entityN)1054 QString World::getLocator(const int _entityN)
1055 {
1056     if (_entityN <= 0)
1057     {
1058         return "";
1059     }
1060     return locator->getLocator(getLongitude(_entityN), getLatitude(_entityN));
1061 }
1062 
1063 /*
1064 int World::getBandIdFromFreq(const QString fr)
1065 {
1066        //qDebug() << "World::getBandIdFromFreq: " << fr  << QT_ENDL;
1067     //Freq should be in MHz
1068     QSqlQuery query;
1069     QString queryString = QString("SELECT id FROM band WHERE lower <= '%1' and upper >= '%2'").arg(fr).arg(fr);
1070        //qDebug() << "World::getBandIdFromFreq query: " << queryString  << QT_ENDL;
1071 
1072 
1073     if (!query.exec(queryString))
1074     {
1075         return -1;
1076     }
1077     else
1078     {
1079         query.next();
1080 
1081         int b=-1;
1082         if (query.isValid())
1083         {
1084             b = (query.value(0)).toInt();
1085                //qDebug() << "World::getBandIdFromFreq value: " << QString::number(b)  << QT_ENDL;
1086             //return (query.value(0)).toInt();
1087             return b;
1088         }
1089         else
1090         {
1091                //qDebug() << "World::getBandIdFromFreq value not valid!!" << QT_ENDL;
1092             return -1;
1093         }
1094     }
1095 
1096     return -1;
1097 }
1098 */
1099 
1100 
1101 
getQRZEntityPrefixes(const QString & _qrz)1102 QString World::getQRZEntityPrefixes(const QString &_qrz)
1103 {
1104 
1105     int i = getQRZARRLId(_qrz);
1106     return dataProxy->getEntityMainPrefix(i);
1107 /*
1108     QString result;
1109     result = "";
1110     QString queryString;
1111     QSqlQuery query;
1112     queryString = "SELECT prefix FROM prefixesofentity WHERE dxcc=='" + QString::number(i) +"'";
1113 
1114 
1115     if (!query.exec(queryString))
1116     {
1117         return "";
1118     }
1119     else
1120     {
1121         while ( (query.next())) {
1122             if (query.isValid())
1123             {
1124                 result = result + ", " + (query.value(0)).toString();
1125             }
1126             else
1127             {
1128             }
1129         }
1130 
1131         if (result.length() < 1)
1132         {
1133             return result;
1134         }
1135         else
1136         {
1137             result = result.remove(0,2);
1138             return result;
1139         }
1140     }
1141 
1142     return "";
1143 */
1144 }
1145 
readCTYCSV(const QString & _worldFile)1146 bool World::readCTYCSV(const QString &_worldFile)
1147 {
1148      //qDebug() << "World::readCTYCSV(): " << _worldFile << QT_ENDL;
1149 
1150     QString tq;
1151     tq.clear();
1152     QString entityNumber;
1153     //entityNumber = "-1";
1154     //QString fileName;
1155     qint64 beginingOfFile;
1156     int numberOfLines = 0;
1157     //int errorCode = -1;
1158 
1159     QFile file( _worldFile );
1160     if (!file.open(QIODevice::ReadOnly | QIODevice::Text))  /* Flawfinder: ignore */
1161     {
1162           //qDebug() << "World::readCTYCSV() File not found: END FALSE" << fileName << QT_ENDL;
1163         return false;
1164     }
1165     else
1166     {
1167          //qDebug() << "World::readCTYCSV() File found: " << fileName << QT_ENDL;
1168     }
1169     beginingOfFile = file.pos();
1170 
1171 
1172 
1173     while (!file.atEnd()) {
1174         if ( (file.readLine()).contains(';')  )
1175         {
1176             numberOfEntities++;
1177         }
1178         numberOfLines++;
1179     }
1180       //qDebug() << "World::readCTYCSV() - numberOfEntities: " << QString::number(numberOfEntities) << QT_ENDL;
1181 
1182     // The file is readed twice: 1: Main entity data; 2: prefixes.
1183 
1184 
1185     // Starts with main data:
1186     file.seek(beginingOfFile);
1187     progressBarPosition = 0;
1188     QProgressDialog progress(tr("Reading cty.csv..."), tr("Abort reading"), 0, numberOfLines, this);
1189     progress.setWindowModality(Qt::ApplicationModal);
1190 
1191     numberOfEntities = 0; // Reset this variable to reuse it and assign the "dxcc" to the entities (temp solution)
1192 
1193     QSqlQuery query;    // Entity information
1194     QSqlQuery queryP;   // Prefixes information
1195 
1196     query.prepare("INSERT INTO entity (id, name, cqz, ituz, continent, latitude, longitude, utc, dxcc, mainprefix) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
1197     queryP.prepare("INSERT INTO prefixesofentity (id, prefix, dxcc, cqz, ituz) VALUES (?, ?, ?, ?, ?)");
1198 
1199     QSqlDatabase::database().transaction();
1200     QStringList T9StringList;
1201     T9StringList.clear();
1202 
1203     QStringList stringList, stringListPrefixes, stringListProcessedPrefix;
1204 
1205     int entN;
1206     //entN = -1;
1207 
1208     while (!file.atEnd()) {
1209         progress.setValue(progressBarPosition);
1210         progressBarPosition++;
1211         if (progress.wasCanceled())
1212             break;
1213         stringList.clear();
1214         stringListPrefixes.clear();
1215 //
1216 //     9Y,Trinidad & Tobago,90,SA,9,11,10.38,61.28,4.0,9Y 9Z;
1217 //     5A,Libya,436,AF,34,38,27.20,-16.60,-2.0,5A;
1218 // MainPref / Name / dxcc / Cont / CQ / ITU / LAT / LON / -UTC / prefixes
1219 //    0        1        2      3      4    5     6     7      8       9
1220 
1221         tq = file.readLine();
1222            //qDebug()  << "World::readCTYCSV(): Line: " << tq << QT_ENDL;
1223         tq = tq.simplified();
1224            //qDebug()  << "World::readCTYCSV(): Line simplified: " << tq << QT_ENDL;
1225         tq = tq.trimmed();
1226           //qDebug()  << "World::readCTYCSV(): Line trimmed: " << tq << QT_ENDL;
1227         tq.remove(QChar(';'), Qt::CaseInsensitive);
1228           //qDebug()  << "World::readCTYCSV(): Line without ;: " << tq << QT_ENDL;
1229 
1230         stringList << tq.split(',');
1231 
1232            //qDebug()  << "World::readCTYCSV(): Line stringList-0: " << stringList.at(0) << QT_ENDL;
1233 
1234         if (( stringList.at(0)).contains(QChar('*'), Qt::CaseInsensitive))
1235         { // This is a special Entity. Not really an ARRL Entity but interesting for the DXer.
1236           // From http://www.country-files.com/cty-dat-format
1237           // (A “*” preceding this prefix indicates that the country is on the DARC WAEDC list, and counts in CQ-sponsored contests, but not ARRL-sponsored contests).
1238             //entN = -1;
1239             entN = (stringList.at(2)).toInt() + 1000;
1240 
1241             while ( (dataProxy->getEntityMainPrefix(entN)).size()>0  )
1242             {
1243                   //qDebug()  << "World::readCTYCSV() entN: " << QString::number(entN) << QT_ENDL;
1244                    //qDebug()  << "World::readCTYCSV() dataProxy->getEntityMainPrefix: " << QString::number(entN) << QT_ENDL;
1245                 entN = entN + 1000;
1246             }
1247 
1248 
1249             entityNumber = QString::number(entN);
1250         }
1251         else
1252         {
1253             entityNumber = stringList.at(2);
1254         }
1255 
1256            //qDebug()  << "World::readCTYCSV(): Line stringList Length: " << QString::number(stringList.length()) << QT_ENDL;
1257 
1258         if (stringList.size()>=8 )
1259         {
1260 
1261         //(id, name, cqz, ituz, continent, latitude, longitude, utc, dxcc, mainprefix)
1262 
1263             query.addBindValue(QVariant(QVariant::Int));
1264             query.addBindValue(stringList.at(1)); // name
1265             query.addBindValue(stringList.at(4)); // CQ
1266             query.addBindValue(stringList.at(5)); // ITU
1267             query.addBindValue(stringList.at(3)); // Cont
1268             query.addBindValue(stringList.at(6)); // Lat
1269             query.addBindValue(stringList.at(7)); // Lon
1270             query.addBindValue(stringList.at(8)); // UTC
1271             //query.addBindValue(stringList.at(2)); // dxcc
1272             query.addBindValue(entityNumber); // dxcc
1273             query.addBindValue(stringList.at(0)); // Mainprefix
1274 
1275 
1276                //qDebug()  << "World::readCTYCSV(): Entity name: " << stringList.at(1) << QT_ENDL;
1277                //qDebug()  << "World::readCTYCSV(): Entity cqz:  " << stringList.at(4) << QT_ENDL;
1278                //qDebug()  << "World::readCTYCSV(): Entity ituz: " << stringList.at(5) << QT_ENDL;
1279                //qDebug()  << "World::readCTYCSV(): Entity cont: " << stringList.at(3) << QT_ENDL;
1280                //qDebug()  << "World::readCTYCSV(): Entity lat:  " << stringList.at(6) << QT_ENDL;
1281                //qDebug()  << "World::readCTYCSV(): Entity lon:  " << stringList.at(7) << QT_ENDL;
1282                //qDebug()  << "World::readCTYCSV(): Entity UTC:  " << stringList.at(8) << QT_ENDL;
1283                //qDebug()  << "World::readCTYCSV(): Entity ARRL: " << stringList.at(2) << QT_ENDL;
1284                //qDebug()  << "World::readCTYCSV(): Entity Pref: " << stringList.at(0) << QT_ENDL;
1285 
1286 
1287             if (query.exec())
1288             {
1289                    //qDebug()  << "World::readCTYDAT(): Entity data added: " <<  stringList.at(1)  << QT_ENDL;
1290             }
1291             else if (query.lastError().nativeErrorCode() == QString::number(19))
1292             {
1293                //qDebug()  << "World::readCTYDAT(): Entity data added: error19:  " <<  stringList.at(1)  << QT_ENDL;
1294                 emit queryError(Q_FUNC_INFO, query.lastError().databaseText(), query.lastError().nativeErrorCode(), query.lastQuery());
1295             }
1296             else
1297             {
1298                 //qDebug()  << "World::readCTYDAT(): Entity data added: error else:  " <<  stringList.at(1)  << QT_ENDL;
1299                 emit queryError(Q_FUNC_INFO, query.lastError().databaseText(), query.lastError().nativeErrorCode(), query.lastQuery());
1300                 //errorCode = query.lastError().nativeErrorCode();
1301 
1302 
1303                   //qDebug() << "World::readCTYCSV(): Entity data NOT added"  << QT_ENDL;
1304                   //qDebug() << "World::readCTYCSV(): LastQuery: " << query.lastQuery()  << QT_ENDL;
1305                   //qDebug() << "World::readCTYCSV(): LastError-data: " << query.lastError().databaseText()  << QT_ENDL;
1306                   //qDebug() << "World::readCTYCSV(): LastError-driver: " << query.lastError().driverText()  << QT_ENDL;
1307                   //qDebug() << "World::readCTYCSV(): LastError-n: " << QString::number(query.lastError().nativeErrorCode() ) << QT_ENDL;
1308 
1309             }
1310 
1311                 //qDebug()  << "World::readCTYCSV(): Entity ADDED or NOT"  << QT_ENDL;
1312             if (stringList.size()>8)
1313             {
1314                 tq = stringList.at(9);
1315                 stringListPrefixes << tq.split(' ');
1316                 tq = stringListPrefixes.last();
1317                 QString prefAux = QString();
1318 
1319                 for (int i = 0; i < stringListPrefixes.size(); ++i)
1320                 {
1321                     prefAux = stringListPrefixes.at(i);
1322                     //queryP.prepare("INSERT INTO prefixesofentity (id, prefix, dxcc, cqz, ituz) VALUES (?, ?, ?, ?, ?)");
1323                     //                                               0    1       2    3    4
1324                     //  (id, prefix, dxcc, cqz, ituz)
1325                       //qDebug()  << "World::readCTYCSV(): Prefix: " << stringListPrefixes.at(i) << QT_ENDL;
1326                     queryP.addBindValue(QVariant(QVariant::Int));
1327 
1328 
1329                     //readZones (const QString &pref, const int _cq, const int _itu)
1330                     //Returns a QStringList: prefix, CQz, ITUz
1331                     stringListProcessedPrefix.clear();
1332                     stringListProcessedPrefix << readZones(prefAux, (stringList.at(4)).toInt(), (stringList.at(5)).toInt());
1333 
1334                     if (prefAux.at(0)=='=')
1335                     { //TODO: Maybe there is a better way to identify exact calls instead of prefixes , identified with a = before he call.
1336                         prefAux.remove(0,1);
1337                     }
1338 
1339                     queryP.addBindValue(stringListProcessedPrefix.at(0));
1340                     queryP.addBindValue(entityNumber);
1341                     queryP.addBindValue(stringListProcessedPrefix.at(1));
1342                     queryP.addBindValue(stringListProcessedPrefix.at(2));
1343 
1344                     if (queryP.exec())
1345                     {// T9 is the former prefix of Bosnia and Herzegovina; it moved to E7 on 7 August 2007.
1346                      // YZ and 4N belonged to Yugoslavia, along with 4O (which went to successor state Montenegro)
1347                      // YT and YU (which went to successor state Serbia)
1348                            //qDebug()  << "World::readCTYCSV(): Prefix added: " << stringListPrefixes.at(i) << QT_ENDL;
1349 
1350 
1351                     }
1352                     else if (queryP.lastError().nativeErrorCode() == QString::number(19))
1353                     {}
1354                     else
1355                     {
1356                         //errorCode = queryP.lastError().nativeErrorCode();
1357                         emit queryError(Q_FUNC_INFO, queryP.lastError().databaseText(), queryP.lastError().nativeErrorCode(), queryP.lastQuery());
1358 
1359 
1360                           //qDebug() << "World::readCTYCSV(): Prefix data NOT added"  << QT_ENDL;
1361                           //qDebug() << "World::readCTYCSV(): Prefix LastQuery: " << query.lastQuery()  << QT_ENDL;
1362                           //qDebug() << "World::readCTYCSV(): Prefix LastError-data: " << query.lastError().databaseText()  << QT_ENDL;
1363                           //qDebug() << "World::readCTYCSV(): Prefix LastError-driver: " << query.lastError().driverText()  << QT_ENDL;
1364                           //qDebug() << "World::readCTYCSV(): Prefix LastError-n: " << QString::number(query.lastError().nativeErrorCode() ) << QT_ENDL;
1365                     }
1366 
1367                 }
1368             }
1369 
1370          }
1371 
1372           //qDebug()  << "World::readCTYCSV() tq: " << tq << QT_ENDL;
1373         progress.setLabelText("Reading cty.csv ... \nNow reading " + currentPrefix + " data");
1374           //qDebug() << "World::readCTYCSV() - progressBarPosition: " << QString::number(progressBarPosition) << QT_ENDL;
1375     }
1376 
1377 
1378     QSqlDatabase::database().commit();
1379 
1380     progress.setValue(numberOfLines);
1381     if (created)
1382     {
1383         dataProxy->updateISONames();
1384     }
1385 
1386       //qDebug()  << "World::readCTYCSV() END TRUE "  << QT_ENDL;
1387     return true;
1388 
1389 }
1390 
changeSlashAndFindPrefix(const QString & _qrz)1391 QString World::changeSlashAndFindPrefix(const QString &_qrz)
1392 {
1393       //qDebug() << "World::changeSlashAndFindPrefix: -"  << _qrz <<"-" << QT_ENDL;
1394 
1395 
1396     QString aux = _qrz.toUpper();
1397 
1398     if ((aux).count('\\')) // Replaces \ by / to ease operation.
1399     {
1400         aux.replace(QChar('\\'), QChar('/'));
1401     }
1402     else
1403     {
1404         return aux;
1405     }
1406 
1407     if (aux.count('/')) // / found! Checking different options
1408     {
1409         int iaux1, iaux2;
1410 
1411         if (aux.endsWith("/") )
1412         { // We look for calls ending in slash "/" or "\"
1413             aux.remove(aux.length()-1,1);
1414         }
1415         iaux1 = aux.indexOf('/');
1416             //qDebug() << "World::changeSlashAndFindPrefix: Slash found at: "  << QString::number(iaux1) << QT_ENDL;
1417 
1418         iaux2 = (aux.length())- iaux1; // iaux2 is the length of the second part
1419         if (iaux2 < 0){
1420             iaux2 = -iaux2;
1421         }
1422 
1423         if ( iaux1 < iaux2 ) { //Like in F/EA0XXX, we can simply take the first part as the prefix
1424             aux = aux.left(iaux1);
1425         }
1426         else
1427         {
1428             aux = aux.right(iaux2 -1);
1429         }
1430     }
1431     return aux;
1432 }
1433 
1434 
checkQRZValidFormat(const QString & _qrz)1435 bool World::checkQRZValidFormat(const QString &_qrz)
1436 {
1437         //qDebug()  << "World::checkQRZValidFormat: -" << _qrz <<"-" << QT_ENDL;
1438 
1439     QString aux = changeSlashAndFindPrefix(_qrz.toUpper());
1440 
1441     if (aux.length()<3)
1442     {
1443         return false;
1444     }
1445 
1446     // The format of a Call is defined here:
1447     // http://en.wikipedia.org/wiki/ITU_prefix_%28amateur_stations%29
1448     //bool prefixEnded = false;
1449     //int callLength = _qrz.length();
1450 
1451     //int sepPos = 0;
1452 
1453 
1454     int barPos = aux.indexOf('/');
1455 
1456     //QString prefix = QString();
1457     //QString suffix = QString();
1458     //QString separator = QString();
1459     // If barPos > 0 we have a complex call like F/EA0K
1460 
1461     QCharRef c = aux[aux.length()-1];
1462 
1463     if ( c.isDigit() && (barPos<=0)  )
1464     {
1465         return false;
1466     }
1467 
1468     //prefix = prefix + aux.at(0);
1469 
1470     if ( ( aux.at(1) ).isDigit() )
1471     { // W3 A6
1472         if ((aux.at(2)).isDigit() )
1473         { // A60
1474             //prefix = prefix + aux.at(1);
1475             //separator = aux.at(2);
1476             //sepPos = 2;
1477         }
1478         else
1479         { // W3A
1480             //separator = aux.at(1);
1481             //sepPos = 1;
1482         }
1483     }
1484     else
1485     { // EA 3D 2M
1486 
1487         if ((aux.at(2)).isDigit() )
1488         { // EA0
1489 
1490             //prefix = prefix + aux.at(1);
1491             //separator = aux.at(2);
1492             //sepPos = 2;
1493 
1494         }
1495         else
1496         { // 3DA
1497             if (aux.length() < 4)
1498             {
1499                 //prefix = prefix + aux.at(2);
1500                 return false;
1501             }
1502 
1503             if ((aux.at(3)).isDigit())
1504             { // 3DA0
1505                 //prefix = prefix + aux.at(1);
1506                 //prefix = prefix + aux.at(2);
1507                 //separator = aux.at(3);
1508                 //sepPos = 3;
1509                 if (aux.length() < 5)
1510                 { // 3DA0
1511                     //prefix = prefix + aux.at(3);
1512                     return false;
1513                 }
1514             }
1515             else
1516             {
1517                 return false;
1518             }
1519         }
1520     }
1521 
1522     /*
1523     for (int i = sepPos+1; i < aux.length(); i++)
1524     {
1525         suffix = suffix + aux.at(i);
1526     }
1527     */
1528 
1529     return true;
1530 }
1531 
getEntitiesNames()1532 QStringList World::getEntitiesNames()
1533 {
1534        //qDebug()  << "World::getEntitiesNames"  << QT_ENDL;
1535      return dataProxy->getEntitiesNames();
1536 }
1537 
getHowManyEntities()1538 int World::getHowManyEntities()
1539 {
1540     return dataProxy->getHowManyEntities();
1541 }
1542 
insertSpecialEntities()1543 bool World::insertSpecialEntities()
1544 { //https://en.wikipedia.org/wiki/Non-ITU_prefix
1545     //qDebug() << Q_FUNC_INFO << QT_ENDL;
1546     // T9 to E7
1547     // 4N & YZ to 4O
1548     int entityID = dataProxy->getEntityIdFromMainPrefix("E7");
1549     int cqz = dataProxy->getCQZFromId(entityID);
1550     int ituz = dataProxy->getITUzFromEntity(entityID);
1551     QString queryString = QString("INSERT INTO prefixesofentity (prefix, dxcc, cqz, ituz) VALUES ('%1', '%2', '%3', '%4') ").arg("T9").arg(entityID).arg(cqz).arg(ituz);
1552     QSqlQuery query;
1553     bool sqlOK = query.exec(queryString);
1554 
1555     if (!sqlOK)
1556     {
1557         query.finish();
1558         //qDebug() << Q_FUNC_INFO << " : T9 not added " << QT_ENDL;
1559         return false;
1560     }
1561     entityID = dataProxy->getEntityIdFromMainPrefix("4O");
1562     cqz = dataProxy->getCQzFromEntity(entityID);
1563     ituz = dataProxy->getITUzFromEntity(entityID);
1564     queryString = QString("INSERT INTO prefixesofentity (prefix, dxcc, cqz, ituz) VALUES ('%1', '%2', '%3', '%4') ").arg("4N").arg(entityID).arg(cqz).arg(ituz);
1565 
1566     sqlOK = query.exec(queryString);
1567 
1568     if (!sqlOK)
1569     {
1570         query.finish();
1571         //qDebug() << Q_FUNC_INFO << " : 4N not added " << QT_ENDL;
1572         return false;
1573     }
1574     queryString = QString("INSERT INTO prefixesofentity (prefix, dxcc, cqz, ituz) VALUES ('%1', '%2', '%3', '%4') ").arg("YZ").arg(entityID).arg(cqz).arg(ituz);
1575 
1576     sqlOK = query.exec(queryString);
1577 
1578     if (!sqlOK)
1579     {
1580         query.finish();
1581         //qDebug() << Q_FUNC_INFO << " : YZ not added " << QT_ENDL;
1582         return false;
1583     }
1584 
1585     return true;
1586 
1587     //qDebug() << Q_FUNC_INFO << " - END" << QT_ENDL;
1588 
1589 }
1590 
hasSpecialEntities()1591 bool World::hasSpecialEntities()
1592 { // Checks if T9 is added to the list of prefixes to validate if special prefixes have been added.
1593     //qDebug() << Q_FUNC_INFO << QT_ENDL;
1594     QString queryString = QString("SELECT dxcc from prefixesofentity WHERE prefix='T9'");
1595     QSqlQuery query;
1596     bool sqlOK = query.exec(queryString);
1597 
1598     if (sqlOK)
1599     {
1600         query.next();
1601         if (query.isValid())
1602         {
1603             if ((query.value(0)).toInt()>1)
1604             {
1605                 query.finish();
1606                 return true;
1607             }
1608         }
1609 
1610     }
1611     else
1612     {
1613         emit queryError(Q_FUNC_INFO, query.lastError().databaseText(), query.lastError().nativeErrorCode(), query.lastQuery());
1614 
1615     }
1616     query.finish();
1617     return false;
1618 
1619 }
1620 
1621 
1622