Fork me on GitHub
2.15.20
FGx
A cross-platform launcher for FlightGear
airportsdata.cpp
Go to the documentation of this file.
1 // -=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-
2 // FGx FlightGear Launcher // airportsdata.cpp
3 // -=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-
4 // (c) 2010-2012
5 // Yves Sablonier, Pete Morgan
6 // Geoff McLane
7 // GNU GPLv2, see main.cpp and shipped licence.txt for further information
8 
9 #include <QFile>
10 #include <QHash>
11 #include <QProgressDialog>
12 #include <QByteArray>
13 #include <QFile>
14 #include <QTextStream>
15 #include <QDirIterator>
16 #include <QTime>
17 
18 #include "airports/airportsdata.h"
19 #include "utilities/utilities.h"
20 #include "utilities/fgx_gzlib.h" /* FGx interface to glib */
21 
22 #define USE_LOCAL_ZLIB
23 #undef ADD_EXTRA_DEBUG
24 
25 
26 bool AirportsData::import(QProgressDialog &progress, MainObject *mainObject){
27 
28  qDebug() << "IMPORT AIRPORT APTDAT";
29  QHash<QString, QString> airports;
30  QString msg;
31  QTime tm;
32  int ms;
33 
34  progress.setValue(0);
35  progress.setWindowTitle("Scanning Airport Directories");
36  progress.setRange(0, 50000);
37 
38 
39  int c = 0;
40  int found = 0;
41  int air_added = 0;
42  ms = tm.restart();
43 
44  // Removing cache file, if exists()
45  if (QFile::exists(mainObject->data_file("airports.txt"))) {
46  outLog("*** FGx airportsdata reload: cache file exists!");
47  QFile::remove(mainObject->data_file("airports.txt"));
48  outLog("*** FGx airportsdata reload: REMOVED AIRPORTS CACHE FILE");
49  }
50 
51  //= Cache File
52  QFile cacheFile( mainObject->data_file("airports.txt") );
53  if(!cacheFile.open(QIODevice::WriteOnly | QIODevice::Text)){
54  qDebug() << "TODO Open error cachce file=";
55  return true;
56  }
57  QTextStream out(&cacheFile);
58 
59 
60  //================================================
61  //* Lets Loop the directories
62  //* Get out aiports path from setings and get the the subdir also
63  QDirIterator loopAirportsFiles( mainObject->X->airports_path(), QDirIterator::Subdirectories );
64  QString xFileName;
65 
66  msg = "FGx airportsdata reload: Scanning XML files in "+mainObject->X->airports_path();
67  outLog(msg);
68  progress.setWindowTitle(msg);
69  progress.setRange(0, 50000);
70 
71  // Check the fgfs additional argument list,
72  // and/or any additional scenery path inputs
73  // *** take care NOT to duplicate ***
74  QStringList fgfs_args = mainObject->X->get_fgfs_args();
75  int i, ind;
76  QDir d;
77  QString path;
78 #ifdef Q_OS_WIN
79  QChar psep(';');
80 #else
81  QChar psep(':');
82 #endif
83 
84  // AIIIIII, found the doubler !, said yves very very loud - well done said pete ;-)
85  for (i = 0; i < fgfs_args.size(); i++) {
86  msg = fgfs_args.at(i);
87  ind = msg.indexOf(QChar('"'));
88  if (ind == 0)
89  msg = msg.mid(1,msg.length()-2);
90  if (msg.indexOf("--fg-scenery=") == 0) {
91  // got a scenery folder to scan
92  msg = msg.mid(13);
93  ind = msg.indexOf(QChar('"'));
94  if (ind == 0)
95  msg = msg.mid(1,msg.length()-2);
96  QStringList path_list = msg.split(psep);
97  int pathnumber = 0;
98  for( QStringList::ConstIterator entry = path_list.begin(); entry != path_list.end(); entry++) {
99  path = *entry;
100  pathnumber = pathnumber + 1;
101  if (d.exists(path)) {
102  // we have a PATH to check, but we are ONLY checking 'Airports'
103  if ( !(path.indexOf(QChar('/')) == (path.size()-1)) &&
104  !(path.indexOf(QChar('\\')) == (path.size()-1)) )
105  path.append("/");
106  path.append("Airports"); // XML is only in here
107  if (!d.exists(path))
108  continue;
109  QDirIterator loopFiles( path, QDirIterator::Subdirectories );
110  while (loopFiles.hasNext()) {
111 
112  //= Get file handle if there is one
113  xFileName = loopFiles.next();
114 
115  //= Check if file entry is a *.threshold.xml - cos this is what we want
116  if(xFileName.endsWith(".threshold.xml") ){
117 
118  //= Split out "CODE.threshold.xml" with a "."
119  QFileInfo fileInfoThreshold(xFileName);
120  QString airport_code = fileInfoThreshold.fileName().split(".").at(0);
121 
122  //* Update progress
123  if(c % 100 == 0){
124  progress.setValue(c);
125  progress.setLabelText(xFileName);
126  progress.repaint();
127  }
128 
129  QString airport_name("");
130  if(airports.contains(airport_code)){
131  airport_name = airports.value(airport_code);
132  }
133 
134  QStringList cols; // missing in middle is description ??
135  cols << airport_code << airport_name << fileInfoThreshold.absoluteDir().absolutePath() << QString::number(pathnumber);
136 
137  out << cols.join("\t").append("\n");
138  air_added++;
139 
140  found++;
141  }
142 
143  if(progress.wasCanceled()){
144  progress.hide();
145  return true;
146  }
147  c++;
148  }
149  }
150  }
151  }
152  }
153 
154 
155  cacheFile.close();
156 
157  msg.sprintf("*** FGx airportsdata reload: Walked %d files, found %d threshold.xml, appended %d to cache",
158  c, found, air_added);
159  outLog(msg+", in "+getElapTimeStg(tm.elapsed()));
160  progress.hide();
161  return false;
162 }
QStringList get_fgfs_args()
The MainObject is an extended QObject, and the main "controller" for FGx.
Definition: mainobject.h:66
static bool import(QProgressDialog &progress, MainObject *mainObject)
QString data_file(QString file_name)
Path to a data file eg data_file("airports.txt")
Definition: mainobject.cpp:438
XSettingsModel * X
Definition: mainobject.h:86
QString airports_path()
Return the absolute path to the /Airports directory.