Fork me on GitHub
2.15.20
FGx
A cross-platform launcher for FlightGear
aircraftdata.cpp
Go to the documentation of this file.
1 // -=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-
2 // FGx FlightGear Launcher // aircraftdata.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 <QTextStream>
11 #include <QProgressDialog>
12 #include <QStringList>
13 #include <QDir>
14 #include <QXmlQuery>
15 #include <QDomDocument>
16 #include <QDomNode>
17 
18 #include "aircraft/aircraftdata.h"
19 
20 #include "utilities/utilities.h"
21 #include "utilities/messagebox.h"
22 
23 //AircraftData::AircraftData()
24 //{
25 //}
26 
27 bool AircraftData::import(QProgressDialog &progress, MainObject *mainObject){
28 
29  int c = 0;
30  int found = 0;
31 
32  progress.setRange(0, 2000);
33  progress.setWindowTitle("Scanning Aircraft Directories");
34  progress.show();
35  progress.repaint();
36 
37  // Removing cache file, if exists()
38  if (QFile::exists(mainObject->data_file("aircrafts.txt"))) {
39  outLog("*** FGx aircrafts/hangar data reload: cache file exists!");
40  QFile::remove(mainObject->data_file("airports.txt"));
41  outLog("*** FGx aircrafts/hangar data reload: REMOVED AIRPORTS CACHE FILE");
42  }
43 
44  //= Cache File
45  QFile cacheFile( mainObject->data_file("aircraft.txt") );
46  if(!cacheFile.open(QIODevice::WriteOnly | QIODevice::Text)){
47  qDebug() << "TODO Open error cachce file=ssssssssssssssssssssssssssssssssssssssss";
48  return true;
49  }
50 
51 
52 
53  QTextStream out(&cacheFile);
54 
55  QString aircraft_base_path = mainObject->X->aircraft_path();
56  //= Get files Entries from Aircaft/ directory
57  QDir aircraftDir(aircraft_base_path);
58  qDebug() << "aircraft_path()=" << aircraft_base_path;
59 
60  aircraftDir.setFilter( QDir::Dirs | QDir::NoSymLinks | QDir::NoDotAndDotDot);
61 
62  QStringList entries = aircraftDir.entryList();
63  progress.setRange(0, entries.size() + 20);
64 
65  for( QStringList::ConstIterator entry=entries.begin(); entry!=entries.end(); ++entry ){
66 
67  // Filter out default dir names, should be a QDir name filter?
68  if (*entry != "Instruments" && *entry != "Instruments-3d" && *entry != "Generic") {
69  //qDebug() << "ENTRY+" << *entry;
70  progress.setValue(c);
71  progress.setLabelText(*entry);
72  progress.repaint();
73 
74  //** get the List of *-set.xml files in dir
75  QDir dir( mainObject->X->aircraft_path(*entry) );
76  QStringList filters;
77  filters << "*-set.xml";
78  QStringList list_xml = dir.entryList(filters);
79 
80  if(list_xml.count() > 0){ // << Scan MOdels
81  QString xml_file;
82 
83 
84  //** Add Path Node
85  QString directory = QString(*entry);
86  //** Add Models
87  for (int i = 0; i < list_xml.size(); ++i){
88 
89  xml_file = QString(list_xml.at(i));
90 
91  QString file_path = mainObject->X->aircraft_path(*entry);
92  file_path.append("/");
93  file_path.append(xml_file);
94 
96  mi.filter_path = aircraft_base_path;
97  QStringList lines;
98  lines << mi.dir << mi.aero << mi.description << mi.fdm << mi.authors << mi.xml_file << mi.file_path << mi.filter_path;
99  out << lines.join("\t") << "\n";
100 
101  found++;
102 
103  if(progress.wasCanceled()){
104  //qDebug() << "Progress cancelled!";
105  progress.hide();
106  return true;
107  }
108  c++;
109  }
110 
111  } /* list_xml.count() > 0 */
112  } /* entry != INstruments etc */
113  } /* loop entries.() */
114 
115  cacheFile.close();
116  return false;
117 }
118 
119 /* \brief Returns all -set.xml files in a directory. Recus is for custom folder and selecting a parent */
120 QFileInfoList AircraftData::get_xml_set_files(QString dir_path, bool recus){
121  QDir dir( dir_path );
122  QStringList filters;
123  filters << "*-set.xml";
124  QFileInfoList setList = dir.entryInfoList(filters);
125  if(recus){
126  QFileInfoList allentries = dir.entryInfoList();
127  for(int i = 0; i < allentries.length(); i++){
128  //qDebug() << "--" << allentries.at(i).absoluteFilePath();
129  if(allentries.at(i).isDir()){
130  QFileInfoList subset = AircraftData::get_xml_set_files(allentries.at(i).absoluteFilePath(), false);
131  for(int ii = 0; ii < subset.length(); ii++){
132  setList.append(subset.at(ii));
133  }
134  }
135  }
136  }
137  return setList;
138 }
139 
140 /* \brief Parses the xml-set file by querying DOM */
142 
143  QFileInfo fInfo(xml_set_path);
144 
145  ModelInfo mi;
146  mi.ok = false;
147  mi.file_path = xml_set_path;
148  //mi.dir_path = fInfo.dir().absolutePath();
149  mi.dir = fInfo.dir().dirName();
150 
151  // the model = filename without --set.xml
152  mi.aero = fInfo.fileName();
153  mi.aero.chop(8);
154 
155 
156  QFile xmlFile( xml_set_path );
157  if (xmlFile.open(QIODevice::ReadOnly | QIODevice::Text)){
158 
159  /* The file content is converted to UTF-8.
160  Some files are Windows, encoding and throw error with QxmlQuery etc
161  Its a hack and don't quite understand whats happening.. said pedro
162  */
163  QString xmlString = QString(xmlFile.readAll()).toUtf8();
164 
165  QXmlQuery query;
166  query.setFocus(xmlString);
167  //query.setFocus(&xmlFile); << Because file is not QTF8 using sting instead
168  query.setQuery("PropertyList/sim");
169  if (query.isValid()){
170 
171  QString res;
172  query.evaluateTo(&res);
173  xmlFile.close();
174 
175  QDomDocument dom;
176  dom.setContent("" + res + "");
177  QDomNodeList nodes = dom.elementsByTagName("sim");
178 
179  QDomNode n = nodes.at(0);
180  mi.description = n.firstChildElement("description").text();
181  mi.authors = n.firstChildElement("author").text().trimmed().replace(("\n"),"");
182  mi.fdm = n.firstChildElement("flight-model").text();
183  mi.ok = true;
184 
185  } /* !query.isValid() */
186  } /* xmlFile.open() */
187 
188  return mi;
189 }
The MainObject is an extended QObject, and the main "controller" for FGx.
Definition: mainobject.h:66
static ModelInfo read_model_xml(QString dir)
QString fdm
Definition: modelinfo.h:16
bool ok
Definition: modelinfo.h:12
QString aero
Definition: modelinfo.h:13
QString xml_file
Definition: modelinfo.h:14
QString data_file(QString file_name)
Path to a data file eg data_file("airports.txt")
Definition: mainobject.cpp:438
QString aircraft_path()
The path to the /Aircraft directory.
XSettingsModel * X
Definition: mainobject.h:86
QString file_path
Definition: modelinfo.h:19
QString dir
Definition: modelinfo.h:15
QString description
Definition: modelinfo.h:18
QString filter_path
Definition: modelinfo.h:20
static QFileInfoList get_xml_set_files(QString dir, bool recus)
static bool import(QProgressDialog &progress, MainObject *mainObject)
QString authors
Definition: modelinfo.h:17