2
0

dbcompletiondata.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. *openPilotLog - A FOSS Pilot Logbook Application
  3. *Copyright (C) 2020-2022 Felix Turowsky
  4. *
  5. *This program is free software: you can redistribute it and/or modify
  6. *it under the terms of the GNU General Public License as published by
  7. *the Free Software Foundation, either version 3 of the License, or
  8. *(at your option) any later version.
  9. *
  10. *This program is distributed in the hope that it will be useful,
  11. *but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. *GNU General Public License for more details.
  14. *
  15. *You should have received a copy of the GNU General Public License
  16. *along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. #include "src/database/database.h"
  19. #include "dbcompletiondata.h"
  20. namespace OPL {
  21. QT_DEPRECATED
  22. void DbCompletionData::init()
  23. {
  24. LOG << "Initialising Completion Data...";
  25. // retreive user modifiable data
  26. pilotsIdMap = getIdMap(CompleterTarget::PilotNames);
  27. tailsIdMap = getIdMap(CompleterTarget::Registrations);
  28. pilotList = getCompletionList(CompleterTarget::PilotNames);
  29. tailsList = getCompletionList(CompleterTarget::Registrations);
  30. // For tails, also provide completion for registration stripped of the '-' character
  31. QStringList tails_list = getCompletionList(CompleterTarget::Registrations);
  32. for (auto &reg : tails_list) {
  33. if(reg.contains(QLatin1Char('-'))) { // check to avoid duplication if reg has no '-'
  34. QString copy = reg;
  35. reg.remove(QLatin1Char('-'));
  36. reg = copy + " (" + reg + QLatin1Char(')');
  37. }
  38. }
  39. tailsList = tails_list;
  40. // retreive default data
  41. airportIcaoIdMap = getIdMap(CompleterTarget::AirportIdentifierICAO);
  42. airportIataIdMap = getIdMap(CompleterTarget::AirportIdentifierIATA);
  43. airportNameIdMap = getIdMap(CompleterTarget::AirportNames);
  44. airportList = getCompletionList(CompleterTarget::AirportIdentifier);
  45. // retreive default data
  46. airportsMapICAO = getIdMap(CompleterTarget::AirportIdentifierICAO);
  47. airportsMapIATA = getIdMap(CompleterTarget::AirportIdentifierIATA);
  48. }
  49. QT_DEPRECATED
  50. void DbCompletionData::update()
  51. {
  52. updatePilots();
  53. updateTails();
  54. updateAirports();
  55. };
  56. QT_DEPRECATED
  57. void DbCompletionData::updateTails()
  58. {
  59. DEB << "Updating Tails...";
  60. tailsIdMap = getIdMap(CompleterTarget::Registrations);
  61. tailsList = getCompletionList(CompleterTarget::Registrations);
  62. }
  63. QT_DEPRECATED
  64. void DbCompletionData::updatePilots()
  65. {
  66. DEB << "Updating Pilots...";
  67. pilotsIdMap = getIdMap(CompleterTarget::PilotNames);
  68. pilotList = getCompletionList(CompleterTarget::PilotNames);
  69. }
  70. QT_DEPRECATED
  71. void DbCompletionData::updateAirports()
  72. {
  73. DEB << "Updating Airports...";
  74. airportIcaoIdMap = getIdMap(CompleterTarget::AirportIdentifierICAO);
  75. airportIataIdMap = getIdMap(CompleterTarget::AirportIdentifierIATA);
  76. airportNameIdMap = getIdMap(CompleterTarget::AirportNames);
  77. airportList = getCompletionList(CompleterTarget::AirportIdentifier);
  78. }
  79. QT_DEPRECATED
  80. const QStringList DbCompletionData::getCompletionList(CompleterTarget target)
  81. {
  82. QString statement;
  83. switch (target) {
  84. case CompleterTarget::PilotNames:
  85. statement.append(QStringLiteral("SELECT lastname||', '||firstname FROM pilots"));
  86. break;
  87. case CompleterTarget::AircraftTypes:
  88. statement.append(QStringLiteral("SELECT make||' '||model FROM aircraft WHERE model IS NOT NULL AND variant IS NULL "
  89. "UNION "
  90. "SELECT make||' '||model||'-'||variant FROM aircraft WHERE variant IS NOT NULL"));
  91. break;
  92. case CompleterTarget::AirportIdentifier:
  93. statement.append(QStringLiteral("SELECT icao FROM airports UNION SELECT iata FROM airports"));
  94. break;
  95. case CompleterTarget::Registrations:
  96. statement.append(QStringLiteral("SELECT registration FROM tails"));
  97. break;
  98. case CompleterTarget::Companies:
  99. statement.append(QStringLiteral("SELECT company FROM pilots"));
  100. break;
  101. default:
  102. DEB << "Not a valid completer target for this function.";
  103. return QStringList();
  104. }
  105. QSqlQuery query;
  106. query.prepare(statement);
  107. query.setForwardOnly(true);
  108. query.exec();
  109. QStringList completer_list;
  110. while (query.next())
  111. completer_list.append(query.value(0).toString());
  112. completer_list.sort();
  113. completer_list.removeAll(QString());
  114. completer_list.removeDuplicates();
  115. return completer_list;
  116. }
  117. QT_DEPRECATED
  118. const QHash<int, QString> DbCompletionData::getIdMap(CompleterTarget target)
  119. {
  120. QString statement;
  121. switch (target) {
  122. case CompleterTarget::PilotNames:
  123. statement.append(QStringLiteral("SELECT ROWID, lastname||', '||firstname FROM pilots"));
  124. break;
  125. case CompleterTarget::AircraftTypes:
  126. statement.append(QStringLiteral("SELECT ROWID, make||' '||model FROM aircraft WHERE model IS NOT NULL AND variant IS NULL "
  127. "UNION "
  128. "SELECT ROWID, make||' '||model||'-'||variant FROM aircraft WHERE variant IS NOT NULL"));
  129. break;
  130. case CompleterTarget::AirportIdentifierICAO:
  131. statement.append(QStringLiteral("SELECT ROWID, icao FROM airports"));
  132. break;
  133. case CompleterTarget::AirportIdentifierIATA:
  134. statement.append(QStringLiteral("SELECT ROWID, iata FROM airports WHERE iata NOT NULL"));
  135. break;
  136. case CompleterTarget::AirportNames:
  137. statement.append(QStringLiteral("SELECT ROWID, name FROM airports"));
  138. break;
  139. case CompleterTarget::Registrations:
  140. statement.append(QStringLiteral("SELECT ROWID, registration FROM tails"));
  141. break;
  142. default:
  143. return {};
  144. }
  145. QSqlQuery query;
  146. query.setForwardOnly(true);
  147. query.prepare(statement);
  148. query.exec();
  149. auto id_map = QHash<int, QString>();
  150. while (query.next())
  151. id_map.insert(query.value(0).toInt(), query.value(1).toString());
  152. return id_map;
  153. }
  154. QT_DEPRECATED
  155. const QHash<int, QString> &DbCompletionData::getAirportsMapICAO() const
  156. {
  157. return airportsMapICAO;
  158. }
  159. QT_DEPRECATED
  160. const QHash<int, QString> &DbCompletionData::getAirportsMapIATA() const
  161. {
  162. return airportsMapIATA;
  163. }
  164. } // namespace OPL