File indexing completed on 2024-05-05 03:50:46

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2017 Spencer Brown <spencerbrown991@gmail.com>
0004 //
0005 
0006 #include "NotesModel.h"
0007 #include "NotesItem.h"
0008 
0009 #include "MarbleGlobal.h"
0010 #include "MarbleModel.h"
0011 #include "GeoDataCoordinates.h"
0012 #include "GeoDataLatLonAltBox.h"
0013 #include "Planet.h"
0014 
0015 #include <QString>
0016 #include <QUrl>
0017 #include <QJsonDocument>
0018 #include <QJsonArray>
0019 #include <QJsonObject>
0020 
0021 #include <QUrlQuery>
0022 
0023 using namespace Marble;
0024 
0025 NotesModel::NotesModel(const MarbleModel *marbleModel, QObject *parent)
0026     : AbstractDataPluginModel("Notes", marbleModel, parent)
0027 {
0028 }
0029 
0030 void NotesModel::getAdditionalItems(const GeoDataLatLonAltBox& box, qint32 number)
0031 {
0032     double left = box.west(GeoDataCoordinates::Degree);
0033     double bottom = box.south(GeoDataCoordinates::Degree);
0034     double right = box.east(GeoDataCoordinates::Degree);
0035     double top = box.north(GeoDataCoordinates::Degree);
0036 
0037     QString bboxValue;
0038     bboxValue.append(QString::number(left)).append(",").append(QString::number(bottom)).append(",").append(QString::number(right)).append(",").append(QString::number(top));
0039 
0040     QUrl osmNotesApiUrl("https://api.openstreetmap.org/api/0.6/notes.json");
0041     QUrlQuery urlQuery;
0042     urlQuery.addQueryItem("bbox", bboxValue);
0043     urlQuery.addQueryItem("limit", QString::number(number));
0044     osmNotesApiUrl.setQuery(urlQuery);
0045 
0046     downloadDescriptionFile(osmNotesApiUrl);
0047 }
0048 
0049 void NotesModel::parseFile(const QByteArray& file)
0050 {
0051     QJsonDocument jsonDoc = QJsonDocument::fromJson(file);
0052     QJsonValue features = jsonDoc.object().value(QStringLiteral("features"));
0053 
0054     if (features.isArray()) {
0055         QList<AbstractDataPluginItem*> items;
0056 
0057         QJsonArray jsonArray = features.toArray();
0058         for (auto const jsonRef : jsonArray) {
0059             QJsonObject jsonObj = jsonRef.toObject();
0060             QJsonObject geometry = jsonObj.value(QStringLiteral("geometry")).toObject();
0061             QJsonArray coordinates = geometry.value(QStringLiteral("coordinates")).toArray();
0062             double lon = coordinates.at(0).toDouble();
0063             double lat = coordinates.at(1).toDouble();
0064 
0065             QJsonObject noteProperties = jsonObj.value(QStringLiteral("properties")).toObject();
0066             QJsonArray noteComments = noteProperties.value(QStringLiteral("comments")).toArray();
0067 
0068             QString id = QString::number(noteProperties.value(QStringLiteral("id")).toInt());
0069 
0070             QDateTime dateCreated = QDateTime::fromString(noteProperties.value(QStringLiteral("date_created")).toString(), Qt::ISODate);
0071             QDateTime dateClosed = QDateTime::fromString(noteProperties.value(QStringLiteral("closed_at")).toString(), Qt::ISODate);
0072             QString noteStatus = noteProperties.value(QStringLiteral("status")).toString();
0073 
0074             NotesItem *item = new NotesItem(this);
0075             item->setId(id);
0076             item->setCoordinate(GeoDataCoordinates(lon, lat, 0.0, GeoDataCoordinates::Degree));
0077             item->setDateCreated(dateCreated);
0078             item->setNoteStatus(noteStatus);
0079             item->setDateClosed(dateClosed);
0080 
0081             for (auto const commentRef : noteComments) {
0082                 QJsonObject commentObj = commentRef.toObject();
0083                 QDateTime date = QDateTime::fromString(commentObj.value("date").toString(), Qt::ISODate);
0084                 QString user = commentObj.value("user").toString();
0085                 QString text = commentObj.value("text").toString();
0086                 int uid = commentObj.value("uid").toInt();
0087                 Comment comment(date, text, user, uid);
0088                 item->addComment(comment);
0089             }
0090 
0091             items << item;
0092         }
0093 
0094         addItemsToList(items);
0095     }
0096 }
0097 
0098 #include "moc_NotesModel.cpp"