File: fileawardmanager.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 (244 lines) | stat: -rw-r--r-- 10,128 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
/***************************************************************************
                          fileawardmanager.cpp  -  description
                             -------------------
    begin                : oct 2020
    copyright            : (C) 2020 by Jaime Robles
    user                : 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 "fileawardmanager.h"

FileAwardManager::FileAwardManager(DataProxy_SQLite *dp, World *injectedWorld, const QString &_parentFunction)
{
    Q_UNUSED(_parentFunction);
    dataProxy = dp;
    util = new Utilities(Q_FUNC_INFO);
    //world = new World(dataProxy, Q_FUNC_INFO);
    world = injectedWorld;
}

FileAwardManager::~FileAwardManager()
{
   //delete(dataProxy);
    delete(util);
    //delete(world);
}

bool FileAwardManager::importNewAwardFile()
{
    //qDebug() << Q_FUNC_INFO << " -  " ;
    QString fileName = QFileDialog::getOpenFileName(nullptr, tr("Open Award file"), util->getHomeDir(), tr("Award files (*.awa)"));
    //qDebug() << Q_FUNC_INFO << " -   - file: " << fileName ;
    QFile file( fileName );
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) /* Flawfinder: ignore */
    {
        //qDebug() << Q_FUNC_INFO << " -   File not found" << fileName;
        showError(tr("Award file not opened"), tr("KLog was not able to read the award file"), QString(tr("It was not possible to open the file %1 for reading.") ).arg(fileName));
        return false;
    }
    else
    {
        //qDebug() << Q_FUNC_INFO << " -   File opened";
    }

    QString line = QString();
    int number = 0;
    bool hasEOH = false;
    // id / name / shortname / prefix / regionalgroup / regionalid / dxcc / cqz / ituz / start_date / end_date / deleted
    QString sub_name = QString();
    QString sub_shortname = QString();
    QString sub_prefix = QString();
    QString sub_regionalGroup = QString();
    int sub_regionalID = -1;
    int sub_dxcc = -1;
    int sub_cqz = -1;
    int sub_ituz = -1;
    QDate sub_startDate = QDate();
    QDate sub_endDate = QDate();
    bool sub_deleted = false;

    qint64 pos; //Position in the file
    pos = file.pos();

    while ( !file.atEnd() )
    { // KLog first read the full file, to detect potential format errors and counting the number of references
        //qDebug() << Q_FUNC_INFO << " -  : First read, counting and looking for errors: " << line;

        line = file.readLine().trimmed().toUpper();
        //qDebug() << Q_FUNC_INFO << " -  : " << line;
        number = number + line.count("EOR>");
        if ((line.count("<EOH>")>0) )
        {
            hasEOH = true;
        }
        //qDebug() << " //FileAwardManager::importNewAwardFile in the while, end of loop" ;
    }
    //qDebug() << Q_FUNC_INFO << " -  : While finished " ;
    if (!hasEOH)
    {
        showError(tr("AWA wrong format"), tr("The AWA file does not have the right format"), QString(tr("AWA file does not have an <EOH> field") ));
        return false;
    }

    file.seek(pos);
    //bool haveAll = false;
    QStringList fields;
    QString aux; aux.clear();
    hasEOH = false;

    while ( !file.atEnd()   )
    {
        line = (file.readLine()).toUpper();
        fields.clear();
        fields << line.split("<", QT_SKIP);
        if (!hasEOH)
        { // Lets read the HEADER
            // IF EOH
            if (line.contains("<EOH>"))
            {
                hasEOH = true;
            }

            foreach (aux, fields)
            {
                //qDebug() << Q_FUNC_INFO << " -  : Reading header: " << aux;
                aux = aux.trimmed();
                aux = "<" + aux;
                QStringList adifField;
                adifField.clear();
                adifField << util->getValidADIFFieldAndData(aux);
                if (adifField.length()==2)
                {
                    if (adifField.at(0) == "SUBDIV_ENTITY" )
                    {
                        sub_dxcc = world->getQRZARRLId(adifField.at(1));
                    }
                    else if (adifField.at(0) == "SUBDIV_REFERENCES")
                    {
                        //regionalAward->addRegionalAwardNumberOfReferences(adifField.at(1).toInt());
                    }
                    else if (adifField.at(0) == "SUBDIV_VERSION")
                    {
                        //regionalAward->addRegionalAwardVersion(adifField.at(1).toInt());
                    }
                    else if (adifField.at(0) == "SUBDIV_SPONSOR")
                    {
                        //regionalAward->addRegionalAwardSponsor(adifField.at(1));
                    }
                    else if (adifField.at(0) == "SUBDIV_NAME")
                    {
                        //regionalAward->addRegionalAwardName(adifField.at(1));
                    }
                    else
                    {
                        //qDebug() << Q_FUNC_INFO << " -  : Invalid AWA field found HEADER" << adifField.at(0);
                    }
                }
                aux.clear();
            }
        }
        else
        { // NOT in EOH
            // Read line, MODIFY AWARD
            //qDebug() << Q_FUNC_INFO << " -  : Parsing : " << line;
            foreach (aux, fields)
            {
                aux = aux.trimmed();
                aux = "<" + aux;
                QStringList adifField;
                adifField.clear();
                adifField << util->getValidADIFFieldAndData(aux);
                //qDebug() << Q_FUNC_INFO << " -  : Length: " << QString::number(adifField.count());
                if (adifField.count()==2)
                {
                    //qDebug() << Q_FUNC_INFO << " -  : Parsing : (" << adifField.at(0) << "/" << adifField.at(1) << ")";
                    if (adifField.at(0) == "SUBDIV_REFNUMBER" )
                    {
                        sub_regionalID = adifField.at(1).toInt();
                    }
                    if (adifField.at(0) == "SUBDIV_DXCC" )
                    {
                        sub_dxcc = adifField.at(1).toInt();
                    }
                    if (adifField.at(0) == "SUBDIV_CQ" )
                    {
                        sub_cqz = adifField.at(1).toInt();
                    }
                    if (adifField.at(0) == "SUBDIV_ITU" )
                    {
                        sub_ituz = adifField.at(1).toInt();
                    }
                    else if (adifField.at(0) == "SUBDIV_PREF")
                    {
                        sub_prefix = adifField.at(1);
                    }
                    else if (adifField.at(0) == "SUBDIV_SHORT")
                    {
                            sub_shortname = adifField.at(1);
                    }
                    else if (adifField.at(0) == "SUBDIV_NAME")
                    {
                        sub_name = adifField.at(1);
                    }
                    else if (adifField.at(0) == "SUBDIV_GROUP")
                    {
                        sub_regionalGroup = adifField.at(1);
                    }
                    else if (adifField.at(0) == "EOR")
                    {// END OF REGISTRY
                        //qDebug() << Q_FUNC_INFO << " -  : EOR FOUND!\nLet's add the reference.";
                        QStringList _subDiv;
                        dataProxy->addDXCCEntitySubdivision(sub_name, sub_shortname, sub_prefix, sub_regionalGroup,
                                                            sub_regionalID, sub_dxcc, sub_cqz, sub_ituz, sub_startDate, sub_endDate, sub_deleted);
                        sub_name = QString();
                        sub_shortname = QString();
                        sub_prefix = QString();
                        sub_regionalGroup = QString();
                        sub_regionalID = -1;
                        sub_dxcc = -1;
                        sub_cqz = -1;
                        sub_ituz = -1;
                        sub_startDate = QDate();
                        sub_endDate = QDate();
                        sub_deleted = false;
                    }
                 else
                {
                    //qDebug() << Q_FUNC_INFO << " -  : Invalid AWA field found in BODY" << adifField.at(0);
                }
            }
            }
        }
    }
    return false;
}

void FileAwardManager::showError(const QString &_header, const QString &_msg, const QString &_detailedMsg)
{
    //qDebug() << "FileAwardManager::showError: " << _msg;
    QMessageBox msgBox;
    msgBox.setIcon(QMessageBox::Warning);
    msgBox.setWindowTitle(tr("KLog - %1").arg(_header));
    msgBox.setText(_msg);
    msgBox.setInformativeText(_detailedMsg);
    msgBox.setStandardButtons(QMessageBox::Ok);
    msgBox.exec();
}