MauiKit Image Tools

modules/geolocation/citiesdb.cpp
1#include "citiesdb.h"
2
3#include "city.h"
4#include <QDebug>
5
6#include <QFile>
7#include <QDir>
8
9#include <QStandardPaths>
10#include <QUuid>
11
12#include <QSqlDriver>
13#include <QSqlError>
14#include <QSqlQuery>
15
16
17static QString resolveDBFile()
18{
19#if defined(Q_OS_ANDROID)
20
21 QFile file(QStandardPaths::locate(QStandardPaths::GenericDataLocation, "/org/mauikit/imagetools/cities.db"));
22 if(!file.exists())
23 {
24 if(QFile::copy(":/android_rcc_bundle/qml/org/mauikit/imagetools/cities.db", QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)+"/cities.db"))
25 {
26 qDebug() << "Cities DB File was copied to";
27 }else
28 {
29 qDebug() << "Cities DB File was NOT copied to";
30
31 }
32 }
34#else
35 return QStandardPaths::locate(QStandardPaths::GenericDataLocation, "/org/mauikit/imagetools/cities.db");
36#endif
37}
38
39
40CitiesDB::CitiesDB(QObject *)
41{
42 QString DBFile = resolveDBFile();
43
44 if(QSqlDatabase::isDriverAvailable(QStringLiteral("QSQLITE")))
45 {
46 qDebug() << "opening Cities DB";
47 m_db = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"), QUuid::createUuid().toString());
48
49 m_db.setDatabaseName(DBFile);
50 qDebug() << "Cities DB NAME" << m_db.connectionName();
51 qDebug() << DBFile;
52
53 if(!m_db.open())
54 {
55 qWarning() << "Cities::DatabaseConnect - ERROR: " << m_db.lastError().text();
56 m_error = true;
57 }else
58 {
59 m_error = false;
60 }
61 }
62 else
63 {
64 qWarning() << "Cities::DatabaseConnect - ERROR: no driver " << QStringLiteral("QSQLITE") << " available";
65 m_error = true;
66 }
67}
68
69City* CitiesDB::findCity(double latitude, double longitude)
70{
71 if(m_error)
72 {
73 return nullptr;
74 }
75
76 QSqlQuery query(m_db);
77 query.prepare("SELECT * FROM CITIES where lat = ? and lon = ?");
78 query.addBindValue(latitude);
79 query.addBindValue(longitude);
80
81 if(!query.exec())
82 {
83 qWarning() << "Cities::FindCity - ERROR: " << query.lastError().text();
84 }
85
86 if(query.first())
87 {
88 return new City(query.value("id").toString(), query.value("name").toString(), query.value("tz").toString(), query.value("country").toString(),query.value("lat").toDouble(),query.value("lon").toDouble());
89 }
90
91 qWarning() << "City not found";
92
93 return nullptr;
94}
95
96City *CitiesDB::city(const QString &cityId)
97{
98 if(m_error)
99 {
100 return nullptr;
101 }
102
103 QSqlQuery query(m_db);
104 query.prepare("SELECT c.id, c.name, co.name as country, c.lat, c.lon, c.tz FROM CITIES c inner join COUNTRIES co on c.country = co.id where c.id = ?");
105 query.addBindValue(cityId);
106
107 if(!query.exec())
108 {
109 qWarning() << "Cities::city - ERROR: " << query.lastError().text();
110 }
111
112 if(query.first())
113 {
114 return new City(query.value("id").toString(), query.value("name").toString(), query.value("tz").toString(), query.value("country").toString(),query.value("lat").toDouble(),query.value("lon").toDouble(), this);
115 }
116
117 return nullptr;
118}
119
120std::vector< point_t > CitiesDB::cities()
121{
122 std::vector< point_t > res;
123 QSqlQuery query(m_db);
124 query.prepare("SELECT lat, lon FROM CITIES");
125
126 if(!query.exec())
127 {
128 qWarning() << "Cities::ParsingCities - ERROR: " << query.lastError().text();
129 m_error = true;
130 }
131
132 while(query.next())
133 {
134 double lat = query.value("lat").toDouble();
135 double lon = query.value("lon").toDouble();
136 res.push_back({lat, lon});
137 }
138
139 m_error = false;
140 return res;
141}
142
143
144bool CitiesDB::error() const
145{
146 return m_error;
147}
148
A class for representing the GPS coordinates and information of a city.
char * toString(const EngineQuery &query)
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
bool copy(const QString &fileName, const QString &newName)
T & first()
T value(qsizetype i) const const
QSqlDatabase addDatabase(QSqlDriver *driver, const QString &connectionName)
QString connectionName() const const
bool isDriverAvailable(const QString &name)
QSqlError lastError() const const
void setDatabaseName(const QString &name)
QString text() const const
QString locate(StandardLocation type, const QString &fileName, LocateOptions options)
QString writableLocation(StandardLocation type)
QUuid createUuid()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:04:14 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.